blob: 408373819e34eddb7dec372b9d518189d54b4242 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext3/xattr.c
3 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 *
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Ext3 code with a lot of help from Eric Jarman <ejarman@acm.org>.
8 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
11 * Red Hat Inc.
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
14 */
15
16/*
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
24 *
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
27 *
28 * +------------------+
29 * | header |
30 * | entry 1 | |
31 * | entry 2 | | growing downwards
32 * | entry 3 | v
33 * | four null bytes |
34 * | . . . |
35 * | value 1 | ^
36 * | value 3 | | growing upwards
37 * | value 2 | |
38 * +------------------+
39 *
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
43 *
44 * Locking strategy
45 * ----------------
46 * EXT3_I(inode)->i_file_acl is protected by EXT3_I(inode)->xattr_sem.
47 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
50 * by the buffer lock.
51 */
52
53#include <linux/init.h>
54#include <linux/fs.h>
55#include <linux/slab.h>
56#include <linux/ext3_jbd.h>
57#include <linux/ext3_fs.h>
58#include <linux/mbcache.h>
59#include <linux/quotaops.h>
60#include <linux/rwsem.h>
61#include "xattr.h"
62#include "acl.h"
63
64#define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
65#define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
66#define BFIRST(bh) ENTRY(BHDR(bh)+1)
67#define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
68
69#define IHDR(inode, raw_inode) \
70 ((struct ext3_xattr_ibody_header *) \
71 ((void *)raw_inode + \
72 EXT3_GOOD_OLD_INODE_SIZE + \
73 EXT3_I(inode)->i_extra_isize))
74#define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
75
76#ifdef EXT3_XATTR_DEBUG
77# define ea_idebug(inode, f...) do { \
Eric Sandeeneee194e2006-09-27 01:49:30 -070078 printk(KERN_DEBUG "inode %s:%lu: ", \
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 inode->i_sb->s_id, inode->i_ino); \
80 printk(f); \
81 printk("\n"); \
82 } while (0)
83# define ea_bdebug(bh, f...) do { \
84 char b[BDEVNAME_SIZE]; \
85 printk(KERN_DEBUG "block %s:%lu: ", \
86 bdevname(bh->b_bdev, b), \
87 (unsigned long) bh->b_blocknr); \
88 printk(f); \
89 printk("\n"); \
90 } while (0)
91#else
92# define ea_idebug(f...)
93# define ea_bdebug(f...)
94#endif
95
96static void ext3_xattr_cache_insert(struct buffer_head *);
97static struct buffer_head *ext3_xattr_cache_find(struct inode *,
98 struct ext3_xattr_header *,
99 struct mb_cache_entry **);
100static void ext3_xattr_rehash(struct ext3_xattr_header *,
101 struct ext3_xattr_entry *);
102
103static struct mb_cache *ext3_xattr_cache;
104
105static struct xattr_handler *ext3_xattr_handler_map[] = {
106 [EXT3_XATTR_INDEX_USER] = &ext3_xattr_user_handler,
107#ifdef CONFIG_EXT3_FS_POSIX_ACL
108 [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext3_xattr_acl_access_handler,
109 [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
110#endif
111 [EXT3_XATTR_INDEX_TRUSTED] = &ext3_xattr_trusted_handler,
112#ifdef CONFIG_EXT3_FS_SECURITY
113 [EXT3_XATTR_INDEX_SECURITY] = &ext3_xattr_security_handler,
114#endif
115};
116
117struct xattr_handler *ext3_xattr_handlers[] = {
118 &ext3_xattr_user_handler,
119 &ext3_xattr_trusted_handler,
120#ifdef CONFIG_EXT3_FS_POSIX_ACL
121 &ext3_xattr_acl_access_handler,
122 &ext3_xattr_acl_default_handler,
123#endif
124#ifdef CONFIG_EXT3_FS_SECURITY
125 &ext3_xattr_security_handler,
126#endif
127 NULL
128};
129
130static inline struct xattr_handler *
131ext3_xattr_handler(int name_index)
132{
133 struct xattr_handler *handler = NULL;
134
135 if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
136 handler = ext3_xattr_handler_map[name_index];
137 return handler;
138}
139
140/*
141 * Inode operation listxattr()
142 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800143 * dentry->d_inode->i_mutex: don't care
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
145ssize_t
146ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
147{
148 return ext3_xattr_list(dentry->d_inode, buffer, size);
149}
150
151static int
152ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
153{
154 while (!IS_LAST_ENTRY(entry)) {
155 struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
156 if ((void *)next >= end)
157 return -EIO;
158 entry = next;
159 }
160 return 0;
161}
162
163static inline int
164ext3_xattr_check_block(struct buffer_head *bh)
165{
166 int error;
167
168 if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
169 BHDR(bh)->h_blocks != cpu_to_le32(1))
170 return -EIO;
171 error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
172 return error;
173}
174
175static inline int
176ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
177{
178 size_t value_size = le32_to_cpu(entry->e_value_size);
179
180 if (entry->e_value_block != 0 || value_size > size ||
181 le16_to_cpu(entry->e_value_offs) + value_size > size)
182 return -EIO;
183 return 0;
184}
185
186static int
187ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
188 const char *name, size_t size, int sorted)
189{
190 struct ext3_xattr_entry *entry;
191 size_t name_len;
192 int cmp = 1;
193
194 if (name == NULL)
195 return -EINVAL;
196 name_len = strlen(name);
197 entry = *pentry;
198 for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
199 cmp = name_index - entry->e_name_index;
200 if (!cmp)
201 cmp = name_len - entry->e_name_len;
202 if (!cmp)
203 cmp = memcmp(name, entry->e_name, name_len);
204 if (cmp <= 0 && (sorted || cmp == 0))
205 break;
206 }
207 *pentry = entry;
208 if (!cmp && ext3_xattr_check_entry(entry, size))
209 return -EIO;
210 return cmp ? -ENODATA : 0;
211}
212
Ben Dooks381be252005-10-30 15:02:56 -0800213static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
215 void *buffer, size_t buffer_size)
216{
217 struct buffer_head *bh = NULL;
218 struct ext3_xattr_entry *entry;
219 size_t size;
220 int error;
221
222 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
223 name_index, name, buffer, (long)buffer_size);
224
225 error = -ENODATA;
226 if (!EXT3_I(inode)->i_file_acl)
227 goto cleanup;
Mingming Cao1c2bf372006-06-25 05:48:06 -0700228 ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
230 if (!bh)
231 goto cleanup;
232 ea_bdebug(bh, "b_count=%d, refcount=%d",
233 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
234 if (ext3_xattr_check_block(bh)) {
235bad_block: ext3_error(inode->i_sb, __FUNCTION__,
Eric Sandeeneee194e2006-09-27 01:49:30 -0700236 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 EXT3_I(inode)->i_file_acl);
238 error = -EIO;
239 goto cleanup;
240 }
241 ext3_xattr_cache_insert(bh);
242 entry = BFIRST(bh);
243 error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
244 if (error == -EIO)
245 goto bad_block;
246 if (error)
247 goto cleanup;
248 size = le32_to_cpu(entry->e_value_size);
249 if (buffer) {
250 error = -ERANGE;
251 if (size > buffer_size)
252 goto cleanup;
253 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
254 size);
255 }
256 error = size;
257
258cleanup:
259 brelse(bh);
260 return error;
261}
262
263static int
264ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
265 void *buffer, size_t buffer_size)
266{
267 struct ext3_xattr_ibody_header *header;
268 struct ext3_xattr_entry *entry;
269 struct ext3_inode *raw_inode;
270 struct ext3_iloc iloc;
271 size_t size;
272 void *end;
273 int error;
274
275 if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
276 return -ENODATA;
277 error = ext3_get_inode_loc(inode, &iloc);
278 if (error)
279 return error;
280 raw_inode = ext3_raw_inode(&iloc);
281 header = IHDR(inode, raw_inode);
282 entry = IFIRST(header);
283 end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
284 error = ext3_xattr_check_names(entry, end);
285 if (error)
286 goto cleanup;
287 error = ext3_xattr_find_entry(&entry, name_index, name,
288 end - (void *)entry, 0);
289 if (error)
290 goto cleanup;
291 size = le32_to_cpu(entry->e_value_size);
292 if (buffer) {
293 error = -ERANGE;
294 if (size > buffer_size)
295 goto cleanup;
296 memcpy(buffer, (void *)IFIRST(header) +
297 le16_to_cpu(entry->e_value_offs), size);
298 }
299 error = size;
300
301cleanup:
302 brelse(iloc.bh);
303 return error;
304}
305
306/*
307 * ext3_xattr_get()
308 *
309 * Copy an extended attribute into the buffer
310 * provided, or compute the buffer size required.
311 * Buffer is NULL to compute the size of the buffer required.
312 *
313 * Returns a negative error number on failure, or the number of bytes
314 * used / required on success.
315 */
316int
317ext3_xattr_get(struct inode *inode, int name_index, const char *name,
318 void *buffer, size_t buffer_size)
319{
320 int error;
321
322 down_read(&EXT3_I(inode)->xattr_sem);
323 error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
324 buffer_size);
325 if (error == -ENODATA)
326 error = ext3_xattr_block_get(inode, name_index, name, buffer,
327 buffer_size);
328 up_read(&EXT3_I(inode)->xattr_sem);
329 return error;
330}
331
332static int
333ext3_xattr_list_entries(struct inode *inode, struct ext3_xattr_entry *entry,
334 char *buffer, size_t buffer_size)
335{
336 size_t rest = buffer_size;
337
338 for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
339 struct xattr_handler *handler =
340 ext3_xattr_handler(entry->e_name_index);
341
342 if (handler) {
343 size_t size = handler->list(inode, buffer, rest,
344 entry->e_name,
345 entry->e_name_len);
346 if (buffer) {
347 if (size > rest)
348 return -ERANGE;
349 buffer += size;
350 }
351 rest -= size;
352 }
353 }
354 return buffer_size - rest;
355}
356
Ben Dooks381be252005-10-30 15:02:56 -0800357static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358ext3_xattr_block_list(struct inode *inode, char *buffer, size_t buffer_size)
359{
360 struct buffer_head *bh = NULL;
361 int error;
362
363 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
364 buffer, (long)buffer_size);
365
366 error = 0;
367 if (!EXT3_I(inode)->i_file_acl)
368 goto cleanup;
Mingming Cao1c2bf372006-06-25 05:48:06 -0700369 ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
371 error = -EIO;
372 if (!bh)
373 goto cleanup;
374 ea_bdebug(bh, "b_count=%d, refcount=%d",
375 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
376 if (ext3_xattr_check_block(bh)) {
377 ext3_error(inode->i_sb, __FUNCTION__,
Eric Sandeeneee194e2006-09-27 01:49:30 -0700378 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 EXT3_I(inode)->i_file_acl);
380 error = -EIO;
381 goto cleanup;
382 }
383 ext3_xattr_cache_insert(bh);
384 error = ext3_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size);
385
386cleanup:
387 brelse(bh);
388
389 return error;
390}
391
392static int
393ext3_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
394{
395 struct ext3_xattr_ibody_header *header;
396 struct ext3_inode *raw_inode;
397 struct ext3_iloc iloc;
398 void *end;
399 int error;
400
401 if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
402 return 0;
403 error = ext3_get_inode_loc(inode, &iloc);
404 if (error)
405 return error;
406 raw_inode = ext3_raw_inode(&iloc);
407 header = IHDR(inode, raw_inode);
408 end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
409 error = ext3_xattr_check_names(IFIRST(header), end);
410 if (error)
411 goto cleanup;
412 error = ext3_xattr_list_entries(inode, IFIRST(header),
413 buffer, buffer_size);
414
415cleanup:
416 brelse(iloc.bh);
417 return error;
418}
419
420/*
421 * ext3_xattr_list()
422 *
423 * Copy a list of attribute names into the buffer
424 * provided, or compute the buffer size required.
425 * Buffer is NULL to compute the size of the buffer required.
426 *
427 * Returns a negative error number on failure, or the number of bytes
428 * used / required on success.
429 */
430int
431ext3_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
432{
433 int i_error, b_error;
434
435 down_read(&EXT3_I(inode)->xattr_sem);
436 i_error = ext3_xattr_ibody_list(inode, buffer, buffer_size);
437 if (i_error < 0) {
438 b_error = 0;
439 } else {
440 if (buffer) {
441 buffer += i_error;
442 buffer_size -= i_error;
443 }
444 b_error = ext3_xattr_block_list(inode, buffer, buffer_size);
445 if (b_error < 0)
446 i_error = 0;
447 }
448 up_read(&EXT3_I(inode)->xattr_sem);
449 return i_error + b_error;
450}
451
452/*
453 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
454 * not set, set it.
455 */
456static void ext3_xattr_update_super_block(handle_t *handle,
457 struct super_block *sb)
458{
459 if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
460 return;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
Andreas Gruenbachered2908f2006-12-06 20:36:16 -0800463 EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 sb->s_dirt = 1;
465 ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/*
470 * Release the xattr block BH: If the reference count is > 1, decrement
471 * it; otherwise free the block.
472 */
473static void
474ext3_xattr_release_block(handle_t *handle, struct inode *inode,
475 struct buffer_head *bh)
476{
477 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800478 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800481 error = ext3_journal_get_write_access(handle, bh);
482 if (error)
483 goto out;
484
485 lock_buffer(bh);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
488 ea_bdebug(bh, "refcount now=0; freeing");
489 if (ce)
490 mb_cache_entry_free(ce);
491 ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
492 get_bh(bh);
493 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
494 } else {
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800495 BHDR(bh)->h_refcount = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 le32_to_cpu(BHDR(bh)->h_refcount) - 1);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800497 error = ext3_journal_dirty_metadata(handle, bh);
Andrew Morton105fd102007-03-23 00:10:02 -0700498 if (IS_SYNC(inode))
499 handle->h_sync = 1;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800500 DQUOT_FREE_BLOCK(inode, 1);
501 ea_bdebug(bh, "refcount now=%d; releasing",
502 le32_to_cpu(BHDR(bh)->h_refcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (ce)
504 mb_cache_entry_release(ce);
505 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800506 unlock_buffer(bh);
507out:
508 ext3_std_error(inode->i_sb, error);
509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512struct ext3_xattr_info {
513 int name_index;
514 const char *name;
515 const void *value;
516 size_t value_len;
517};
518
519struct ext3_xattr_search {
520 struct ext3_xattr_entry *first;
521 void *base;
522 void *end;
523 struct ext3_xattr_entry *here;
524 int not_found;
525};
526
527static int
528ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
529{
530 struct ext3_xattr_entry *last;
531 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
532
533 /* Compute min_offs and last. */
534 last = s->first;
535 for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
536 if (!last->e_value_block && last->e_value_size) {
537 size_t offs = le16_to_cpu(last->e_value_offs);
538 if (offs < min_offs)
539 min_offs = offs;
540 }
541 }
542 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
543 if (!s->not_found) {
544 if (!s->here->e_value_block && s->here->e_value_size) {
545 size_t size = le32_to_cpu(s->here->e_value_size);
546 free += EXT3_XATTR_SIZE(size);
547 }
548 free += EXT3_XATTR_LEN(name_len);
549 }
550 if (i->value) {
551 if (free < EXT3_XATTR_SIZE(i->value_len) ||
552 free < EXT3_XATTR_LEN(name_len) +
553 EXT3_XATTR_SIZE(i->value_len))
554 return -ENOSPC;
555 }
556
557 if (i->value && s->not_found) {
558 /* Insert the new name. */
559 size_t size = EXT3_XATTR_LEN(name_len);
560 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
561 memmove((void *)s->here + size, s->here, rest);
562 memset(s->here, 0, size);
563 s->here->e_name_index = i->name_index;
564 s->here->e_name_len = name_len;
565 memcpy(s->here->e_name, i->name, name_len);
566 } else {
567 if (!s->here->e_value_block && s->here->e_value_size) {
568 void *first_val = s->base + min_offs;
569 size_t offs = le16_to_cpu(s->here->e_value_offs);
570 void *val = s->base + offs;
571 size_t size = EXT3_XATTR_SIZE(
572 le32_to_cpu(s->here->e_value_size));
573
574 if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
575 /* The old and the new value have the same
576 size. Just replace. */
577 s->here->e_value_size =
578 cpu_to_le32(i->value_len);
579 memset(val + size - EXT3_XATTR_PAD, 0,
580 EXT3_XATTR_PAD); /* Clear pad bytes. */
581 memcpy(val, i->value, i->value_len);
582 return 0;
583 }
584
585 /* Remove the old value. */
586 memmove(first_val + size, first_val, val - first_val);
587 memset(first_val, 0, size);
588 s->here->e_value_size = 0;
589 s->here->e_value_offs = 0;
590 min_offs += size;
591
592 /* Adjust all value offsets. */
593 last = s->first;
594 while (!IS_LAST_ENTRY(last)) {
595 size_t o = le16_to_cpu(last->e_value_offs);
596 if (!last->e_value_block &&
597 last->e_value_size && o < offs)
598 last->e_value_offs =
599 cpu_to_le16(o + size);
600 last = EXT3_XATTR_NEXT(last);
601 }
602 }
603 if (!i->value) {
604 /* Remove the old name. */
605 size_t size = EXT3_XATTR_LEN(name_len);
606 last = ENTRY((void *)last - size);
607 memmove(s->here, (void *)s->here + size,
608 (void *)last - (void *)s->here + sizeof(__u32));
609 memset(last, 0, size);
610 }
611 }
612
613 if (i->value) {
614 /* Insert the new value. */
615 s->here->e_value_size = cpu_to_le32(i->value_len);
616 if (i->value_len) {
617 size_t size = EXT3_XATTR_SIZE(i->value_len);
618 void *val = s->base + min_offs - size;
619 s->here->e_value_offs = cpu_to_le16(min_offs - size);
620 memset(val + size - EXT3_XATTR_PAD, 0,
621 EXT3_XATTR_PAD); /* Clear the pad bytes. */
622 memcpy(val, i->value, i->value_len);
623 }
624 }
625 return 0;
626}
627
628struct ext3_xattr_block_find {
629 struct ext3_xattr_search s;
630 struct buffer_head *bh;
631};
632
Ben Dooks381be252005-10-30 15:02:56 -0800633static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
635 struct ext3_xattr_block_find *bs)
636{
637 struct super_block *sb = inode->i_sb;
638 int error;
639
640 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
641 i->name_index, i->name, i->value, (long)i->value_len);
642
643 if (EXT3_I(inode)->i_file_acl) {
644 /* The inode already has an extended attribute block. */
645 bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
646 error = -EIO;
647 if (!bs->bh)
648 goto cleanup;
649 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
650 atomic_read(&(bs->bh->b_count)),
651 le32_to_cpu(BHDR(bs->bh)->h_refcount));
652 if (ext3_xattr_check_block(bs->bh)) {
653 ext3_error(sb, __FUNCTION__,
Eric Sandeeneee194e2006-09-27 01:49:30 -0700654 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 EXT3_I(inode)->i_file_acl);
656 error = -EIO;
657 goto cleanup;
658 }
659 /* Find the named attribute. */
660 bs->s.base = BHDR(bs->bh);
661 bs->s.first = BFIRST(bs->bh);
662 bs->s.end = bs->bh->b_data + bs->bh->b_size;
663 bs->s.here = bs->s.first;
664 error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
665 i->name, bs->bh->b_size, 1);
666 if (error && error != -ENODATA)
667 goto cleanup;
668 bs->s.not_found = error;
669 }
670 error = 0;
671
672cleanup:
673 return error;
674}
675
676static int
677ext3_xattr_block_set(handle_t *handle, struct inode *inode,
678 struct ext3_xattr_info *i,
679 struct ext3_xattr_block_find *bs)
680{
681 struct super_block *sb = inode->i_sb;
682 struct buffer_head *new_bh = NULL;
683 struct ext3_xattr_search *s = &bs->s;
684 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800685 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687#define header(x) ((struct ext3_xattr_header *)(x))
688
689 if (i->value && i->value_len > sb->s_blocksize)
690 return -ENOSPC;
691 if (s->base) {
692 ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
693 bs->bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800694 error = ext3_journal_get_write_access(handle, bs->bh);
695 if (error)
696 goto cleanup;
697 lock_buffer(bs->bh);
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
700 if (ce) {
701 mb_cache_entry_free(ce);
702 ce = NULL;
703 }
704 ea_bdebug(bs->bh, "modifying in-place");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 error = ext3_xattr_set_entry(i, s);
706 if (!error) {
707 if (!IS_LAST_ENTRY(s->first))
708 ext3_xattr_rehash(header(s->base),
709 s->here);
710 ext3_xattr_cache_insert(bs->bh);
711 }
712 unlock_buffer(bs->bh);
713 if (error == -EIO)
714 goto bad_block;
715 if (!error)
716 error = ext3_journal_dirty_metadata(handle,
717 bs->bh);
718 if (error)
719 goto cleanup;
720 goto inserted;
721 } else {
722 int offset = (char *)s->here - bs->bh->b_data;
723
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800724 unlock_buffer(bs->bh);
725 journal_release_buffer(handle, bs->bh);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (ce) {
728 mb_cache_entry_release(ce);
729 ce = NULL;
730 }
731 ea_bdebug(bs->bh, "cloning");
732 s->base = kmalloc(bs->bh->b_size, GFP_KERNEL);
733 error = -ENOMEM;
734 if (s->base == NULL)
735 goto cleanup;
736 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
737 s->first = ENTRY(header(s->base)+1);
738 header(s->base)->h_refcount = cpu_to_le32(1);
739 s->here = ENTRY(s->base + offset);
740 s->end = s->base + bs->bh->b_size;
741 }
742 } else {
743 /* Allocate a buffer where we construct the new block. */
Mingming Cao8c3478a2007-10-18 23:39:20 -0700744 s->base = kzalloc(sb->s_blocksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* assert(header == s->base) */
746 error = -ENOMEM;
747 if (s->base == NULL)
748 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
750 header(s->base)->h_blocks = cpu_to_le32(1);
751 header(s->base)->h_refcount = cpu_to_le32(1);
752 s->first = ENTRY(header(s->base)+1);
753 s->here = ENTRY(header(s->base)+1);
754 s->end = s->base + sb->s_blocksize;
755 }
756
757 error = ext3_xattr_set_entry(i, s);
758 if (error == -EIO)
759 goto bad_block;
760 if (error)
761 goto cleanup;
762 if (!IS_LAST_ENTRY(s->first))
763 ext3_xattr_rehash(header(s->base), s->here);
764
765inserted:
766 if (!IS_LAST_ENTRY(s->first)) {
767 new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
768 if (new_bh) {
769 /* We found an identical block in the cache. */
770 if (new_bh == bs->bh)
771 ea_bdebug(new_bh, "keeping");
772 else {
773 /* The old block is released after updating
774 the inode. */
775 error = -EDQUOT;
776 if (DQUOT_ALLOC_BLOCK(inode, 1))
777 goto cleanup;
778 error = ext3_journal_get_write_access(handle,
779 new_bh);
780 if (error)
781 goto cleanup_dquot;
782 lock_buffer(new_bh);
783 BHDR(new_bh)->h_refcount = cpu_to_le32(1 +
784 le32_to_cpu(BHDR(new_bh)->h_refcount));
785 ea_bdebug(new_bh, "reusing; refcount now=%d",
786 le32_to_cpu(BHDR(new_bh)->h_refcount));
787 unlock_buffer(new_bh);
788 error = ext3_journal_dirty_metadata(handle,
789 new_bh);
790 if (error)
791 goto cleanup_dquot;
792 }
793 mb_cache_entry_release(ce);
794 ce = NULL;
795 } else if (bs->bh && s->base == bs->bh->b_data) {
796 /* We were modifying this block in-place. */
797 ea_bdebug(bs->bh, "keeping this block");
798 new_bh = bs->bh;
799 get_bh(new_bh);
800 } else {
801 /* We need to allocate a new block */
Mingming Cao1c2bf372006-06-25 05:48:06 -0700802 ext3_fsblk_t goal = le32_to_cpu(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 EXT3_SB(sb)->s_es->s_first_data_block) +
Mingming Cao1c2bf372006-06-25 05:48:06 -0700804 (ext3_fsblk_t)EXT3_I(inode)->i_block_group *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 EXT3_BLOCKS_PER_GROUP(sb);
Mingming Cao1c2bf372006-06-25 05:48:06 -0700806 ext3_fsblk_t block = ext3_new_block(handle, inode,
807 goal, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (error)
809 goto cleanup;
810 ea_idebug(inode, "creating block %d", block);
811
812 new_bh = sb_getblk(sb, block);
813 if (!new_bh) {
814getblk_failed:
815 ext3_free_blocks(handle, inode, block, 1);
816 error = -EIO;
817 goto cleanup;
818 }
819 lock_buffer(new_bh);
820 error = ext3_journal_get_create_access(handle, new_bh);
821 if (error) {
822 unlock_buffer(new_bh);
823 goto getblk_failed;
824 }
825 memcpy(new_bh->b_data, s->base, new_bh->b_size);
826 set_buffer_uptodate(new_bh);
827 unlock_buffer(new_bh);
828 ext3_xattr_cache_insert(new_bh);
829 error = ext3_journal_dirty_metadata(handle, new_bh);
830 if (error)
831 goto cleanup;
832 }
833 }
834
835 /* Update the inode. */
836 EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
837
838 /* Drop the previous xattr block. */
839 if (bs->bh && bs->bh != new_bh)
840 ext3_xattr_release_block(handle, inode, bs->bh);
841 error = 0;
842
843cleanup:
844 if (ce)
845 mb_cache_entry_release(ce);
846 brelse(new_bh);
847 if (!(bs->bh && s->base == bs->bh->b_data))
848 kfree(s->base);
849
850 return error;
851
852cleanup_dquot:
853 DQUOT_FREE_BLOCK(inode, 1);
854 goto cleanup;
855
856bad_block:
857 ext3_error(inode->i_sb, __FUNCTION__,
Eric Sandeeneee194e2006-09-27 01:49:30 -0700858 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 EXT3_I(inode)->i_file_acl);
860 goto cleanup;
861
862#undef header
863}
864
865struct ext3_xattr_ibody_find {
866 struct ext3_xattr_search s;
867 struct ext3_iloc iloc;
868};
869
Ben Dooks381be252005-10-30 15:02:56 -0800870static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
872 struct ext3_xattr_ibody_find *is)
873{
874 struct ext3_xattr_ibody_header *header;
875 struct ext3_inode *raw_inode;
876 int error;
877
878 if (EXT3_I(inode)->i_extra_isize == 0)
879 return 0;
880 raw_inode = ext3_raw_inode(&is->iloc);
881 header = IHDR(inode, raw_inode);
882 is->s.base = is->s.first = IFIRST(header);
883 is->s.here = is->s.first;
884 is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
885 if (EXT3_I(inode)->i_state & EXT3_STATE_XATTR) {
886 error = ext3_xattr_check_names(IFIRST(header), is->s.end);
887 if (error)
888 return error;
889 /* Find the named attribute. */
890 error = ext3_xattr_find_entry(&is->s.here, i->name_index,
891 i->name, is->s.end -
892 (void *)is->s.base, 0);
893 if (error && error != -ENODATA)
894 return error;
895 is->s.not_found = error;
896 }
897 return 0;
898}
899
900static int
901ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
902 struct ext3_xattr_info *i,
903 struct ext3_xattr_ibody_find *is)
904{
905 struct ext3_xattr_ibody_header *header;
906 struct ext3_xattr_search *s = &is->s;
907 int error;
908
909 if (EXT3_I(inode)->i_extra_isize == 0)
910 return -ENOSPC;
911 error = ext3_xattr_set_entry(i, s);
912 if (error)
913 return error;
914 header = IHDR(inode, ext3_raw_inode(&is->iloc));
915 if (!IS_LAST_ENTRY(s->first)) {
916 header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
917 EXT3_I(inode)->i_state |= EXT3_STATE_XATTR;
918 } else {
919 header->h_magic = cpu_to_le32(0);
920 EXT3_I(inode)->i_state &= ~EXT3_STATE_XATTR;
921 }
922 return 0;
923}
924
925/*
926 * ext3_xattr_set_handle()
927 *
928 * Create, replace or remove an extended attribute for this inode. Buffer
929 * is NULL to remove an existing extended attribute, and non-NULL to
930 * either replace an existing extended attribute, or create a new extended
931 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
932 * specify that an extended attribute must exist and must not exist
933 * previous to the call, respectively.
934 *
935 * Returns 0, or a negative error number on failure.
936 */
937int
938ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
939 const char *name, const void *value, size_t value_len,
940 int flags)
941{
942 struct ext3_xattr_info i = {
943 .name_index = name_index,
944 .name = name,
945 .value = value,
946 .value_len = value_len,
947
948 };
949 struct ext3_xattr_ibody_find is = {
950 .s = { .not_found = -ENODATA, },
951 };
952 struct ext3_xattr_block_find bs = {
953 .s = { .not_found = -ENODATA, },
954 };
955 int error;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (!name)
958 return -EINVAL;
959 if (strlen(name) > 255)
960 return -ERANGE;
961 down_write(&EXT3_I(inode)->xattr_sem);
962 error = ext3_get_inode_loc(inode, &is.iloc);
963 if (error)
964 goto cleanup;
965
966 if (EXT3_I(inode)->i_state & EXT3_STATE_NEW) {
967 struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
968 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
969 EXT3_I(inode)->i_state &= ~EXT3_STATE_NEW;
970 }
971
972 error = ext3_xattr_ibody_find(inode, &i, &is);
973 if (error)
974 goto cleanup;
975 if (is.s.not_found)
976 error = ext3_xattr_block_find(inode, &i, &bs);
977 if (error)
978 goto cleanup;
979 if (is.s.not_found && bs.s.not_found) {
980 error = -ENODATA;
981 if (flags & XATTR_REPLACE)
982 goto cleanup;
983 error = 0;
984 if (!value)
985 goto cleanup;
986 } else {
987 error = -EEXIST;
988 if (flags & XATTR_CREATE)
989 goto cleanup;
990 }
991 error = ext3_journal_get_write_access(handle, is.iloc.bh);
992 if (error)
993 goto cleanup;
994 if (!value) {
995 if (!is.s.not_found)
996 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
997 else if (!bs.s.not_found)
998 error = ext3_xattr_block_set(handle, inode, &i, &bs);
999 } else {
1000 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1001 if (!error && !bs.s.not_found) {
1002 i.value = NULL;
1003 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1004 } else if (error == -ENOSPC) {
1005 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1006 if (error)
1007 goto cleanup;
1008 if (!is.s.not_found) {
1009 i.value = NULL;
1010 error = ext3_xattr_ibody_set(handle, inode, &i,
1011 &is);
1012 }
1013 }
1014 }
1015 if (!error) {
1016 ext3_xattr_update_super_block(handle, inode->i_sb);
1017 inode->i_ctime = CURRENT_TIME_SEC;
1018 error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
1019 /*
1020 * The bh is consumed by ext3_mark_iloc_dirty, even with
1021 * error != 0.
1022 */
1023 is.iloc.bh = NULL;
1024 if (IS_SYNC(inode))
1025 handle->h_sync = 1;
1026 }
1027
1028cleanup:
1029 brelse(is.iloc.bh);
1030 brelse(bs.bh);
1031 up_write(&EXT3_I(inode)->xattr_sem);
1032 return error;
1033}
1034
1035/*
1036 * ext3_xattr_set()
1037 *
1038 * Like ext3_xattr_set_handle, but start from an inode. This extended
1039 * attribute modification is a filesystem transaction by itself.
1040 *
1041 * Returns 0, or a negative error number on failure.
1042 */
1043int
1044ext3_xattr_set(struct inode *inode, int name_index, const char *name,
1045 const void *value, size_t value_len, int flags)
1046{
1047 handle_t *handle;
1048 int error, retries = 0;
1049
1050retry:
Jan Kara1f545872005-06-23 22:01:04 -07001051 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (IS_ERR(handle)) {
1053 error = PTR_ERR(handle);
1054 } else {
1055 int error2;
1056
1057 error = ext3_xattr_set_handle(handle, inode, name_index, name,
1058 value, value_len, flags);
1059 error2 = ext3_journal_stop(handle);
1060 if (error == -ENOSPC &&
1061 ext3_should_retry_alloc(inode->i_sb, &retries))
1062 goto retry;
1063 if (error == 0)
1064 error = error2;
1065 }
1066
1067 return error;
1068}
1069
1070/*
1071 * ext3_xattr_delete_inode()
1072 *
1073 * Free extended attribute resources associated with this inode. This
1074 * is called immediately before an inode is freed. We have exclusive
1075 * access to the inode.
1076 */
1077void
1078ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
1079{
1080 struct buffer_head *bh = NULL;
1081
1082 if (!EXT3_I(inode)->i_file_acl)
1083 goto cleanup;
1084 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
1085 if (!bh) {
1086 ext3_error(inode->i_sb, __FUNCTION__,
Eric Sandeeneee194e2006-09-27 01:49:30 -07001087 "inode %lu: block "E3FSBLK" read error", inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 EXT3_I(inode)->i_file_acl);
1089 goto cleanup;
1090 }
1091 if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
1092 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1093 ext3_error(inode->i_sb, __FUNCTION__,
Eric Sandeeneee194e2006-09-27 01:49:30 -07001094 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 EXT3_I(inode)->i_file_acl);
1096 goto cleanup;
1097 }
1098 ext3_xattr_release_block(handle, inode, bh);
1099 EXT3_I(inode)->i_file_acl = 0;
1100
1101cleanup:
1102 brelse(bh);
1103}
1104
1105/*
1106 * ext3_xattr_put_super()
1107 *
1108 * This is called when a file system is unmounted.
1109 */
1110void
1111ext3_xattr_put_super(struct super_block *sb)
1112{
Andreas Gruenbacher8c52ab42005-07-27 11:45:15 -07001113 mb_cache_shrink(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
1116/*
1117 * ext3_xattr_cache_insert()
1118 *
1119 * Create a new entry in the extended attribute cache, and insert
1120 * it unless such an entry is already in the cache.
1121 *
1122 * Returns 0, or a negative error number on failure.
1123 */
1124static void
1125ext3_xattr_cache_insert(struct buffer_head *bh)
1126{
1127 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1128 struct mb_cache_entry *ce;
1129 int error;
1130
1131 ce = mb_cache_entry_alloc(ext3_xattr_cache);
1132 if (!ce) {
1133 ea_bdebug(bh, "out of memory");
1134 return;
1135 }
1136 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
1137 if (error) {
1138 mb_cache_entry_free(ce);
1139 if (error == -EBUSY) {
1140 ea_bdebug(bh, "already in cache");
1141 error = 0;
1142 }
1143 } else {
1144 ea_bdebug(bh, "inserting [%x]", (int)hash);
1145 mb_cache_entry_release(ce);
1146 }
1147}
1148
1149/*
1150 * ext3_xattr_cmp()
1151 *
1152 * Compare two extended attribute blocks for equality.
1153 *
1154 * Returns 0 if the blocks are equal, 1 if they differ, and
1155 * a negative error number on errors.
1156 */
1157static int
1158ext3_xattr_cmp(struct ext3_xattr_header *header1,
1159 struct ext3_xattr_header *header2)
1160{
1161 struct ext3_xattr_entry *entry1, *entry2;
1162
1163 entry1 = ENTRY(header1+1);
1164 entry2 = ENTRY(header2+1);
1165 while (!IS_LAST_ENTRY(entry1)) {
1166 if (IS_LAST_ENTRY(entry2))
1167 return 1;
1168 if (entry1->e_hash != entry2->e_hash ||
1169 entry1->e_name_index != entry2->e_name_index ||
1170 entry1->e_name_len != entry2->e_name_len ||
1171 entry1->e_value_size != entry2->e_value_size ||
1172 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1173 return 1;
1174 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1175 return -EIO;
1176 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1177 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1178 le32_to_cpu(entry1->e_value_size)))
1179 return 1;
1180
1181 entry1 = EXT3_XATTR_NEXT(entry1);
1182 entry2 = EXT3_XATTR_NEXT(entry2);
1183 }
1184 if (!IS_LAST_ENTRY(entry2))
1185 return 1;
1186 return 0;
1187}
1188
1189/*
1190 * ext3_xattr_cache_find()
1191 *
1192 * Find an identical extended attribute block.
1193 *
1194 * Returns a pointer to the block found, or NULL if such a block was
1195 * not found or an error occurred.
1196 */
1197static struct buffer_head *
1198ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
1199 struct mb_cache_entry **pce)
1200{
1201 __u32 hash = le32_to_cpu(header->h_hash);
1202 struct mb_cache_entry *ce;
1203
1204 if (!header->h_hash)
1205 return NULL; /* never share */
1206 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1207again:
1208 ce = mb_cache_entry_find_first(ext3_xattr_cache, 0,
1209 inode->i_sb->s_bdev, hash);
1210 while (ce) {
1211 struct buffer_head *bh;
1212
1213 if (IS_ERR(ce)) {
1214 if (PTR_ERR(ce) == -EAGAIN)
1215 goto again;
1216 break;
1217 }
1218 bh = sb_bread(inode->i_sb, ce->e_block);
1219 if (!bh) {
1220 ext3_error(inode->i_sb, __FUNCTION__,
Eric Sandeeneee194e2006-09-27 01:49:30 -07001221 "inode %lu: block %lu read error",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 inode->i_ino, (unsigned long) ce->e_block);
1223 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1224 EXT3_XATTR_REFCOUNT_MAX) {
Mingming Cao1c2bf372006-06-25 05:48:06 -07001225 ea_idebug(inode, "block %lu refcount %d>=%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 (unsigned long) ce->e_block,
1227 le32_to_cpu(BHDR(bh)->h_refcount),
1228 EXT3_XATTR_REFCOUNT_MAX);
1229 } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
1230 *pce = ce;
1231 return bh;
1232 }
1233 brelse(bh);
1234 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1235 }
1236 return NULL;
1237}
1238
1239#define NAME_HASH_SHIFT 5
1240#define VALUE_HASH_SHIFT 16
1241
1242/*
1243 * ext3_xattr_hash_entry()
1244 *
1245 * Compute the hash of an extended attribute.
1246 */
1247static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1248 struct ext3_xattr_entry *entry)
1249{
1250 __u32 hash = 0;
1251 char *name = entry->e_name;
1252 int n;
1253
1254 for (n=0; n < entry->e_name_len; n++) {
1255 hash = (hash << NAME_HASH_SHIFT) ^
1256 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1257 *name++;
1258 }
1259
1260 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1261 __le32 *value = (__le32 *)((char *)header +
1262 le16_to_cpu(entry->e_value_offs));
1263 for (n = (le32_to_cpu(entry->e_value_size) +
1264 EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1265 hash = (hash << VALUE_HASH_SHIFT) ^
1266 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1267 le32_to_cpu(*value++);
1268 }
1269 }
1270 entry->e_hash = cpu_to_le32(hash);
1271}
1272
1273#undef NAME_HASH_SHIFT
1274#undef VALUE_HASH_SHIFT
1275
1276#define BLOCK_HASH_SHIFT 16
1277
1278/*
1279 * ext3_xattr_rehash()
1280 *
1281 * Re-compute the extended attribute hash value after an entry has changed.
1282 */
1283static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1284 struct ext3_xattr_entry *entry)
1285{
1286 struct ext3_xattr_entry *here;
1287 __u32 hash = 0;
1288
1289 ext3_xattr_hash_entry(header, entry);
1290 here = ENTRY(header+1);
1291 while (!IS_LAST_ENTRY(here)) {
1292 if (!here->e_hash) {
1293 /* Block is not shared if an entry's hash value == 0 */
1294 hash = 0;
1295 break;
1296 }
1297 hash = (hash << BLOCK_HASH_SHIFT) ^
1298 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1299 le32_to_cpu(here->e_hash);
1300 here = EXT3_XATTR_NEXT(here);
1301 }
1302 header->h_hash = cpu_to_le32(hash);
1303}
1304
1305#undef BLOCK_HASH_SHIFT
1306
1307int __init
1308init_ext3_xattr(void)
1309{
1310 ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
1311 sizeof(struct mb_cache_entry) +
1312 sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
1313 if (!ext3_xattr_cache)
1314 return -ENOMEM;
1315 return 0;
1316}
1317
1318void
1319exit_ext3_xattr(void)
1320{
1321 if (ext3_xattr_cache)
1322 mb_cache_destroy(ext3_xattr_cache);
1323 ext3_xattr_cache = NULL;
1324}