initial commit with v2.6.9
[linux-2.6.9-moxart.git] / fs / ext2 / xattr.c
blobc8642719c17038009fc0c51b1cd311b708a097fa
1 /*
2 * linux/fs/ext2/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Extended attributes for symlinks and special files added per
8 * suggestion of Luka Renko <luka.renko@hermes.si>.
9 */
12 * Extended attributes are stored on disk blocks allocated outside of
13 * any inode. The i_file_acl field is then made to point to this allocated
14 * block. If all extended attributes of an inode are identical, these
15 * inodes may share the same extended attribute block. Such situations
16 * are automatically detected by keeping a cache of recent attribute block
17 * numbers and hashes over the block's contents in memory.
20 * Extended attribute block layout:
22 * +------------------+
23 * | header |
24 * ¦ entry 1 | |
25 * | entry 2 | | growing downwards
26 * | entry 3 | v
27 * | four null bytes |
28 * | . . . |
29 * | value 1 | ^
30 * | value 3 | | growing upwards
31 * | value 2 | |
32 * +------------------+
34 * The block header is followed by multiple entry descriptors. These entry
35 * descriptors are variable in size, and alligned to EXT2_XATTR_PAD
36 * byte boundaries. The entry descriptors are sorted by attribute name,
37 * so that two extended attribute blocks can be compared efficiently.
39 * Attribute values are aligned to the end of the block, stored in
40 * no specific order. They are also padded to EXT2_XATTR_PAD byte
41 * boundaries. No additional gaps are left between them.
43 * Locking strategy
44 * ----------------
45 * EXT2_I(inode)->i_file_acl is protected by EXT2_I(inode)->xattr_sem.
46 * EA blocks are only changed if they are exclusive to an inode, so
47 * holding xattr_sem also means that nothing but the EA block's reference
48 * count will change. Multiple writers to an EA block are synchronized
49 * by the bh lock. No more than a single bh lock is held at any time
50 * to avoid deadlocks.
53 #include <linux/buffer_head.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/mbcache.h>
58 #include <linux/quotaops.h>
59 #include <linux/rwsem.h>
60 #include "ext2.h"
61 #include "xattr.h"
62 #include "acl.h"
64 /* These symbols may be needed by a module. */
65 EXPORT_SYMBOL(ext2_xattr_register);
66 EXPORT_SYMBOL(ext2_xattr_unregister);
67 EXPORT_SYMBOL(ext2_xattr_get);
68 EXPORT_SYMBOL(ext2_xattr_list);
69 EXPORT_SYMBOL(ext2_xattr_set);
71 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
72 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
73 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
74 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
76 #ifdef EXT2_XATTR_DEBUG
77 # define ea_idebug(inode, f...) do { \
78 printk(KERN_DEBUG "inode %s:%ld: ", \
79 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
96 static int ext2_xattr_set2(struct inode *, struct buffer_head *,
97 struct ext2_xattr_header *);
99 static int ext2_xattr_cache_insert(struct buffer_head *);
100 static struct buffer_head *ext2_xattr_cache_find(struct inode *,
101 struct ext2_xattr_header *);
102 static void ext2_xattr_cache_remove(struct buffer_head *);
103 static void ext2_xattr_rehash(struct ext2_xattr_header *,
104 struct ext2_xattr_entry *);
106 static struct mb_cache *ext2_xattr_cache;
107 static struct ext2_xattr_handler *ext2_xattr_handlers[EXT2_XATTR_INDEX_MAX];
108 static rwlock_t ext2_handler_lock = RW_LOCK_UNLOCKED;
111 ext2_xattr_register(int name_index, struct ext2_xattr_handler *handler)
113 int error = -EINVAL;
115 if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX) {
116 write_lock(&ext2_handler_lock);
117 if (!ext2_xattr_handlers[name_index-1]) {
118 ext2_xattr_handlers[name_index-1] = handler;
119 error = 0;
121 write_unlock(&ext2_handler_lock);
123 return error;
126 void
127 ext2_xattr_unregister(int name_index, struct ext2_xattr_handler *handler)
129 if (name_index > 0 || name_index <= EXT2_XATTR_INDEX_MAX) {
130 write_lock(&ext2_handler_lock);
131 ext2_xattr_handlers[name_index-1] = NULL;
132 write_unlock(&ext2_handler_lock);
136 static inline const char *
137 strcmp_prefix(const char *a, const char *a_prefix)
139 while (*a_prefix && *a == *a_prefix) {
140 a++;
141 a_prefix++;
143 return *a_prefix ? NULL : a;
147 * Decode the extended attribute name, and translate it into
148 * the name_index and name suffix.
150 static struct ext2_xattr_handler *
151 ext2_xattr_resolve_name(const char **name)
153 struct ext2_xattr_handler *handler = NULL;
154 int i;
156 if (!*name)
157 return NULL;
158 read_lock(&ext2_handler_lock);
159 for (i=0; i<EXT2_XATTR_INDEX_MAX; i++) {
160 if (ext2_xattr_handlers[i]) {
161 const char *n = strcmp_prefix(*name,
162 ext2_xattr_handlers[i]->prefix);
163 if (n) {
164 handler = ext2_xattr_handlers[i];
165 *name = n;
166 break;
170 read_unlock(&ext2_handler_lock);
171 return handler;
174 static inline struct ext2_xattr_handler *
175 ext2_xattr_handler(int name_index)
177 struct ext2_xattr_handler *handler = NULL;
178 if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX) {
179 read_lock(&ext2_handler_lock);
180 handler = ext2_xattr_handlers[name_index-1];
181 read_unlock(&ext2_handler_lock);
183 return handler;
187 * Inode operation getxattr()
189 * dentry->d_inode->i_sem: don't care
191 ssize_t
192 ext2_getxattr(struct dentry *dentry, const char *name,
193 void *buffer, size_t size)
195 struct ext2_xattr_handler *handler;
196 struct inode *inode = dentry->d_inode;
198 handler = ext2_xattr_resolve_name(&name);
199 if (!handler)
200 return -EOPNOTSUPP;
201 return handler->get(inode, name, buffer, size);
205 * Inode operation listxattr()
207 * dentry->d_inode->i_sem: don't care
209 ssize_t
210 ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
212 return ext2_xattr_list(dentry->d_inode, buffer, size);
216 * Inode operation setxattr()
218 * dentry->d_inode->i_sem: down
221 ext2_setxattr(struct dentry *dentry, const char *name,
222 const void *value, size_t size, int flags)
224 struct ext2_xattr_handler *handler;
225 struct inode *inode = dentry->d_inode;
227 if (size == 0)
228 value = ""; /* empty EA, do not remove */
229 handler = ext2_xattr_resolve_name(&name);
230 if (!handler)
231 return -EOPNOTSUPP;
232 return handler->set(inode, name, value, size, flags);
236 * Inode operation removexattr()
238 * dentry->d_inode->i_sem: down
241 ext2_removexattr(struct dentry *dentry, const char *name)
243 struct ext2_xattr_handler *handler;
244 struct inode *inode = dentry->d_inode;
246 handler = ext2_xattr_resolve_name(&name);
247 if (!handler)
248 return -EOPNOTSUPP;
249 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
253 * ext2_xattr_get()
255 * Copy an extended attribute into the buffer
256 * provided, or compute the buffer size required.
257 * Buffer is NULL to compute the size of the buffer required.
259 * Returns a negative error number on failure, or the number of bytes
260 * used / required on success.
263 ext2_xattr_get(struct inode *inode, int name_index, const char *name,
264 void *buffer, size_t buffer_size)
266 struct buffer_head *bh = NULL;
267 struct ext2_xattr_entry *entry;
268 size_t name_len, size;
269 char *end;
270 int error;
272 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
273 name_index, name, buffer, (long)buffer_size);
275 if (name == NULL)
276 return -EINVAL;
277 down_read(&EXT2_I(inode)->xattr_sem);
278 error = -ENODATA;
279 if (!EXT2_I(inode)->i_file_acl)
280 goto cleanup;
281 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
282 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
283 error = -EIO;
284 if (!bh)
285 goto cleanup;
286 ea_bdebug(bh, "b_count=%d, refcount=%d",
287 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
288 end = bh->b_data + bh->b_size;
289 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
290 HDR(bh)->h_blocks != cpu_to_le32(1)) {
291 bad_block: ext2_error(inode->i_sb, "ext2_xattr_get",
292 "inode %ld: bad block %d", inode->i_ino,
293 EXT2_I(inode)->i_file_acl);
294 error = -EIO;
295 goto cleanup;
297 /* find named attribute */
298 name_len = strlen(name);
300 error = -ERANGE;
301 if (name_len > 255)
302 goto cleanup;
303 entry = FIRST_ENTRY(bh);
304 while (!IS_LAST_ENTRY(entry)) {
305 struct ext2_xattr_entry *next =
306 EXT2_XATTR_NEXT(entry);
307 if ((char *)next >= end)
308 goto bad_block;
309 if (name_index == entry->e_name_index &&
310 name_len == entry->e_name_len &&
311 memcmp(name, entry->e_name, name_len) == 0)
312 goto found;
313 entry = next;
315 /* Check the remaining name entries */
316 while (!IS_LAST_ENTRY(entry)) {
317 struct ext2_xattr_entry *next =
318 EXT2_XATTR_NEXT(entry);
319 if ((char *)next >= end)
320 goto bad_block;
321 entry = next;
323 if (ext2_xattr_cache_insert(bh))
324 ea_idebug(inode, "cache insert failed");
325 error = -ENODATA;
326 goto cleanup;
327 found:
328 /* check the buffer size */
329 if (entry->e_value_block != 0)
330 goto bad_block;
331 size = le32_to_cpu(entry->e_value_size);
332 if (size > inode->i_sb->s_blocksize ||
333 le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
334 goto bad_block;
336 if (ext2_xattr_cache_insert(bh))
337 ea_idebug(inode, "cache insert failed");
338 if (buffer) {
339 error = -ERANGE;
340 if (size > buffer_size)
341 goto cleanup;
342 /* return value of attribute */
343 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
344 size);
346 error = size;
348 cleanup:
349 brelse(bh);
350 up_read(&EXT2_I(inode)->xattr_sem);
352 return error;
356 * ext2_xattr_list()
358 * Copy a list of attribute names into the buffer
359 * provided, or compute the buffer size required.
360 * Buffer is NULL to compute the size of the buffer required.
362 * Returns a negative error number on failure, or the number of bytes
363 * used / required on success.
366 ext2_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
368 struct buffer_head *bh = NULL;
369 struct ext2_xattr_entry *entry;
370 size_t size = 0;
371 char *buf, *end;
372 int error;
374 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
375 buffer, (long)buffer_size);
377 down_read(&EXT2_I(inode)->xattr_sem);
378 error = 0;
379 if (!EXT2_I(inode)->i_file_acl)
380 goto cleanup;
381 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
382 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
383 error = -EIO;
384 if (!bh)
385 goto cleanup;
386 ea_bdebug(bh, "b_count=%d, refcount=%d",
387 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
388 end = bh->b_data + bh->b_size;
389 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
390 HDR(bh)->h_blocks != cpu_to_le32(1)) {
391 bad_block: ext2_error(inode->i_sb, "ext2_xattr_list",
392 "inode %ld: bad block %d", inode->i_ino,
393 EXT2_I(inode)->i_file_acl);
394 error = -EIO;
395 goto cleanup;
397 /* compute the size required for the list of attribute names */
398 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
399 entry = EXT2_XATTR_NEXT(entry)) {
400 struct ext2_xattr_handler *handler;
401 struct ext2_xattr_entry *next =
402 EXT2_XATTR_NEXT(entry);
403 if ((char *)next >= end)
404 goto bad_block;
406 handler = ext2_xattr_handler(entry->e_name_index);
407 if (handler)
408 size += handler->list(NULL, inode, entry->e_name,
409 entry->e_name_len);
412 if (ext2_xattr_cache_insert(bh))
413 ea_idebug(inode, "cache insert failed");
414 if (!buffer) {
415 error = size;
416 goto cleanup;
417 } else {
418 error = -ERANGE;
419 if (size > buffer_size)
420 goto cleanup;
423 /* list the attribute names */
424 buf = buffer;
425 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
426 entry = EXT2_XATTR_NEXT(entry)) {
427 struct ext2_xattr_handler *handler;
429 handler = ext2_xattr_handler(entry->e_name_index);
430 if (handler)
431 buf += handler->list(buf, inode, entry->e_name,
432 entry->e_name_len);
434 error = size;
436 cleanup:
437 brelse(bh);
438 up_read(&EXT2_I(inode)->xattr_sem);
440 return error;
444 * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
445 * not set, set it.
447 static void ext2_xattr_update_super_block(struct super_block *sb)
449 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
450 return;
452 lock_super(sb);
453 EXT2_SB(sb)->s_es->s_feature_compat |=
454 cpu_to_le32(EXT2_FEATURE_COMPAT_EXT_ATTR);
455 sb->s_dirt = 1;
456 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
457 unlock_super(sb);
461 * ext2_xattr_set()
463 * Create, replace or remove an extended attribute for this inode. Buffer
464 * is NULL to remove an existing extended attribute, and non-NULL to
465 * either replace an existing extended attribute, or create a new extended
466 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
467 * specify that an extended attribute must exist and must not exist
468 * previous to the call, respectively.
470 * Returns 0, or a negative error number on failure.
473 ext2_xattr_set(struct inode *inode, int name_index, const char *name,
474 const void *value, size_t value_len, int flags)
476 struct super_block *sb = inode->i_sb;
477 struct buffer_head *bh = NULL;
478 struct ext2_xattr_header *header = NULL;
479 struct ext2_xattr_entry *here, *last;
480 size_t name_len, free, min_offs = sb->s_blocksize;
481 int not_found = 1, error;
482 char *end;
485 * header -- Points either into bh, or to a temporarily
486 * allocated buffer.
487 * here -- The named entry found, or the place for inserting, within
488 * the block pointed to by header.
489 * last -- Points right after the last named entry within the block
490 * pointed to by header.
491 * min_offs -- The offset of the first value (values are aligned
492 * towards the end of the block).
493 * end -- Points right after the block pointed to by header.
496 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
497 name_index, name, value, (long)value_len);
499 if (IS_RDONLY(inode))
500 return -EROFS;
501 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
502 return -EPERM;
503 if (value == NULL)
504 value_len = 0;
505 if (name == NULL)
506 return -EINVAL;
507 name_len = strlen(name);
508 if (name_len > 255 || value_len > sb->s_blocksize)
509 return -ERANGE;
510 down_write(&EXT2_I(inode)->xattr_sem);
511 if (EXT2_I(inode)->i_file_acl) {
512 /* The inode already has an extended attribute block. */
513 bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
514 error = -EIO;
515 if (!bh)
516 goto cleanup;
517 ea_bdebug(bh, "b_count=%d, refcount=%d",
518 atomic_read(&(bh->b_count)),
519 le32_to_cpu(HDR(bh)->h_refcount));
520 header = HDR(bh);
521 end = bh->b_data + bh->b_size;
522 if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
523 header->h_blocks != cpu_to_le32(1)) {
524 bad_block: ext2_error(sb, "ext2_xattr_set",
525 "inode %ld: bad block %d", inode->i_ino,
526 EXT2_I(inode)->i_file_acl);
527 error = -EIO;
528 goto cleanup;
530 /* Find the named attribute. */
531 here = FIRST_ENTRY(bh);
532 while (!IS_LAST_ENTRY(here)) {
533 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(here);
534 if ((char *)next >= end)
535 goto bad_block;
536 if (!here->e_value_block && here->e_value_size) {
537 size_t offs = le16_to_cpu(here->e_value_offs);
538 if (offs < min_offs)
539 min_offs = offs;
541 not_found = name_index - here->e_name_index;
542 if (!not_found)
543 not_found = name_len - here->e_name_len;
544 if (!not_found)
545 not_found = memcmp(name, here->e_name,name_len);
546 if (not_found <= 0)
547 break;
548 here = next;
550 last = here;
551 /* We still need to compute min_offs and last. */
552 while (!IS_LAST_ENTRY(last)) {
553 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
554 if ((char *)next >= end)
555 goto bad_block;
556 if (!last->e_value_block && last->e_value_size) {
557 size_t offs = le16_to_cpu(last->e_value_offs);
558 if (offs < min_offs)
559 min_offs = offs;
561 last = next;
564 /* Check whether we have enough space left. */
565 free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
566 } else {
567 /* We will use a new extended attribute block. */
568 free = sb->s_blocksize -
569 sizeof(struct ext2_xattr_header) - sizeof(__u32);
570 here = last = NULL; /* avoid gcc uninitialized warning. */
573 if (not_found) {
574 /* Request to remove a nonexistent attribute? */
575 error = -ENODATA;
576 if (flags & XATTR_REPLACE)
577 goto cleanup;
578 error = 0;
579 if (value == NULL)
580 goto cleanup;
581 } else {
582 /* Request to create an existing attribute? */
583 error = -EEXIST;
584 if (flags & XATTR_CREATE)
585 goto cleanup;
586 if (!here->e_value_block && here->e_value_size) {
587 size_t size = le32_to_cpu(here->e_value_size);
589 if (le16_to_cpu(here->e_value_offs) + size >
590 sb->s_blocksize || size > sb->s_blocksize)
591 goto bad_block;
592 free += EXT2_XATTR_SIZE(size);
594 free += EXT2_XATTR_LEN(name_len);
596 error = -ENOSPC;
597 if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
598 goto cleanup;
600 /* Here we know that we can set the new attribute. */
602 if (header) {
603 /* assert(header == HDR(bh)); */
604 lock_buffer(bh);
605 if (header->h_refcount == cpu_to_le32(1)) {
606 ea_bdebug(bh, "modifying in-place");
607 ext2_xattr_cache_remove(bh);
608 /* keep the buffer locked while modifying it. */
609 } else {
610 int offset;
612 unlock_buffer(bh);
613 ea_bdebug(bh, "cloning");
614 header = kmalloc(bh->b_size, GFP_KERNEL);
615 error = -ENOMEM;
616 if (header == NULL)
617 goto cleanup;
618 memcpy(header, HDR(bh), bh->b_size);
619 header->h_refcount = cpu_to_le32(1);
621 offset = (char *)here - bh->b_data;
622 here = ENTRY((char *)header + offset);
623 offset = (char *)last - bh->b_data;
624 last = ENTRY((char *)header + offset);
626 } else {
627 /* Allocate a buffer where we construct the new block. */
628 header = kmalloc(sb->s_blocksize, GFP_KERNEL);
629 error = -ENOMEM;
630 if (header == NULL)
631 goto cleanup;
632 memset(header, 0, sb->s_blocksize);
633 end = (char *)header + sb->s_blocksize;
634 header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
635 header->h_blocks = header->h_refcount = cpu_to_le32(1);
636 last = here = ENTRY(header+1);
639 /* Iff we are modifying the block in-place, bh is locked here. */
641 if (not_found) {
642 /* Insert the new name. */
643 size_t size = EXT2_XATTR_LEN(name_len);
644 size_t rest = (char *)last - (char *)here;
645 memmove((char *)here + size, here, rest);
646 memset(here, 0, size);
647 here->e_name_index = name_index;
648 here->e_name_len = name_len;
649 memcpy(here->e_name, name, name_len);
650 } else {
651 if (!here->e_value_block && here->e_value_size) {
652 char *first_val = (char *)header + min_offs;
653 size_t offs = le16_to_cpu(here->e_value_offs);
654 char *val = (char *)header + offs;
655 size_t size = EXT2_XATTR_SIZE(
656 le32_to_cpu(here->e_value_size));
658 if (size == EXT2_XATTR_SIZE(value_len)) {
659 /* The old and the new value have the same
660 size. Just replace. */
661 here->e_value_size = cpu_to_le32(value_len);
662 memset(val + size - EXT2_XATTR_PAD, 0,
663 EXT2_XATTR_PAD); /* Clear pad bytes. */
664 memcpy(val, value, value_len);
665 goto skip_replace;
668 /* Remove the old value. */
669 memmove(first_val + size, first_val, val - first_val);
670 memset(first_val, 0, size);
671 here->e_value_offs = 0;
672 min_offs += size;
674 /* Adjust all value offsets. */
675 last = ENTRY(header+1);
676 while (!IS_LAST_ENTRY(last)) {
677 size_t o = le16_to_cpu(last->e_value_offs);
678 if (!last->e_value_block && o < offs)
679 last->e_value_offs =
680 cpu_to_le16(o + size);
681 last = EXT2_XATTR_NEXT(last);
684 if (value == NULL) {
685 /* Remove the old name. */
686 size_t size = EXT2_XATTR_LEN(name_len);
687 last = ENTRY((char *)last - size);
688 memmove(here, (char*)here + size,
689 (char*)last - (char*)here);
690 memset(last, 0, size);
694 if (value != NULL) {
695 /* Insert the new value. */
696 here->e_value_size = cpu_to_le32(value_len);
697 if (value_len) {
698 size_t size = EXT2_XATTR_SIZE(value_len);
699 char *val = (char *)header + min_offs - size;
700 here->e_value_offs =
701 cpu_to_le16((char *)val - (char *)header);
702 memset(val + size - EXT2_XATTR_PAD, 0,
703 EXT2_XATTR_PAD); /* Clear the pad bytes. */
704 memcpy(val, value, value_len);
708 skip_replace:
709 if (IS_LAST_ENTRY(ENTRY(header+1))) {
710 /* This block is now empty. */
711 if (bh && header == HDR(bh))
712 unlock_buffer(bh); /* we were modifying in-place. */
713 error = ext2_xattr_set2(inode, bh, NULL);
714 } else {
715 ext2_xattr_rehash(header, here);
716 if (bh && header == HDR(bh))
717 unlock_buffer(bh); /* we were modifying in-place. */
718 error = ext2_xattr_set2(inode, bh, header);
721 cleanup:
722 brelse(bh);
723 if (!(bh && header == HDR(bh)))
724 kfree(header);
725 up_write(&EXT2_I(inode)->xattr_sem);
727 return error;
731 * Second half of ext2_xattr_set(): Update the file system.
733 static int
734 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
735 struct ext2_xattr_header *header)
737 struct super_block *sb = inode->i_sb;
738 struct buffer_head *new_bh = NULL;
739 int error;
741 if (header) {
742 new_bh = ext2_xattr_cache_find(inode, header);
743 if (new_bh) {
744 /* We found an identical block in the cache. */
745 if (new_bh == old_bh) {
746 ea_bdebug(new_bh, "keeping this block");
747 } else {
748 /* The old block is released after updating
749 the inode. */
750 ea_bdebug(new_bh, "reusing block");
752 error = -EDQUOT;
753 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
754 unlock_buffer(new_bh);
755 goto cleanup;
757 HDR(new_bh)->h_refcount = cpu_to_le32(1 +
758 le32_to_cpu(HDR(new_bh)->h_refcount));
759 ea_bdebug(new_bh, "refcount now=%d",
760 le32_to_cpu(HDR(new_bh)->h_refcount));
762 unlock_buffer(new_bh);
763 } else if (old_bh && header == HDR(old_bh)) {
764 /* Keep this block. No need to lock the block as we
765 don't need to change the reference count. */
766 new_bh = old_bh;
767 get_bh(new_bh);
768 ext2_xattr_cache_insert(new_bh);
769 } else {
770 /* We need to allocate a new block */
771 int goal = le32_to_cpu(EXT2_SB(sb)->s_es->
772 s_first_data_block) +
773 EXT2_I(inode)->i_block_group *
774 EXT2_BLOCKS_PER_GROUP(sb);
775 int block = ext2_new_block(inode, goal,
776 NULL, NULL, &error);
777 if (error)
778 goto cleanup;
779 ea_idebug(inode, "creating block %d", block);
781 new_bh = sb_getblk(sb, block);
782 if (!new_bh) {
783 ext2_free_blocks(inode, block, 1);
784 error = -EIO;
785 goto cleanup;
787 lock_buffer(new_bh);
788 memcpy(new_bh->b_data, header, new_bh->b_size);
789 set_buffer_uptodate(new_bh);
790 unlock_buffer(new_bh);
791 ext2_xattr_cache_insert(new_bh);
793 ext2_xattr_update_super_block(sb);
795 mark_buffer_dirty(new_bh);
796 if (IS_SYNC(inode)) {
797 sync_dirty_buffer(new_bh);
798 error = -EIO;
799 if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
800 goto cleanup;
804 /* Update the inode. */
805 EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
806 inode->i_ctime = CURRENT_TIME;
807 if (IS_SYNC(inode)) {
808 error = ext2_sync_inode (inode);
809 if (error)
810 goto cleanup;
811 } else
812 mark_inode_dirty(inode);
814 error = 0;
815 if (old_bh && old_bh != new_bh) {
817 * If there was an old block and we are no longer using it,
818 * release the old block.
820 lock_buffer(old_bh);
821 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
822 /* Free the old block. */
823 ea_bdebug(old_bh, "freeing");
824 ext2_free_blocks(inode, old_bh->b_blocknr, 1);
825 /* We let our caller release old_bh, so we
826 * need to duplicate the buffer before. */
827 get_bh(old_bh);
828 bforget(old_bh);
829 } else {
830 /* Decrement the refcount only. */
831 HDR(old_bh)->h_refcount = cpu_to_le32(
832 le32_to_cpu(HDR(old_bh)->h_refcount) - 1);
833 DQUOT_FREE_BLOCK(inode, 1);
834 mark_buffer_dirty(old_bh);
835 ea_bdebug(old_bh, "refcount now=%d",
836 le32_to_cpu(HDR(old_bh)->h_refcount));
838 unlock_buffer(old_bh);
841 cleanup:
842 brelse(new_bh);
844 return error;
848 * ext2_xattr_delete_inode()
850 * Free extended attribute resources associated with this inode. This
851 * is called immediately before an inode is freed.
853 void
854 ext2_xattr_delete_inode(struct inode *inode)
856 struct buffer_head *bh = NULL;
858 down_write(&EXT2_I(inode)->xattr_sem);
859 if (!EXT2_I(inode)->i_file_acl)
860 goto cleanup;
861 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
862 if (!bh) {
863 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
864 "inode %ld: block %d read error", inode->i_ino,
865 EXT2_I(inode)->i_file_acl);
866 goto cleanup;
868 ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
869 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
870 HDR(bh)->h_blocks != cpu_to_le32(1)) {
871 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
872 "inode %ld: bad block %d", inode->i_ino,
873 EXT2_I(inode)->i_file_acl);
874 goto cleanup;
876 lock_buffer(bh);
877 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
878 ext2_xattr_cache_remove(bh);
879 ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
880 get_bh(bh);
881 bforget(bh);
882 } else {
883 HDR(bh)->h_refcount = cpu_to_le32(
884 le32_to_cpu(HDR(bh)->h_refcount) - 1);
885 mark_buffer_dirty(bh);
886 if (IS_SYNC(inode))
887 sync_dirty_buffer(bh);
888 DQUOT_FREE_BLOCK(inode, 1);
890 ea_bdebug(bh, "refcount now=%d", le32_to_cpu(HDR(bh)->h_refcount) - 1);
891 unlock_buffer(bh);
892 EXT2_I(inode)->i_file_acl = 0;
894 cleanup:
895 brelse(bh);
896 up_write(&EXT2_I(inode)->xattr_sem);
900 * ext2_xattr_put_super()
902 * This is called when a file system is unmounted.
904 void
905 ext2_xattr_put_super(struct super_block *sb)
907 mb_cache_shrink(ext2_xattr_cache, sb->s_bdev);
912 * ext2_xattr_cache_insert()
914 * Create a new entry in the extended attribute cache, and insert
915 * it unless such an entry is already in the cache.
917 * Returns 0, or a negative error number on failure.
919 static int
920 ext2_xattr_cache_insert(struct buffer_head *bh)
922 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
923 struct mb_cache_entry *ce;
924 int error;
926 ce = mb_cache_entry_alloc(ext2_xattr_cache);
927 if (!ce)
928 return -ENOMEM;
929 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
930 if (error) {
931 mb_cache_entry_free(ce);
932 if (error == -EBUSY) {
933 ea_bdebug(bh, "already in cache (%d cache entries)",
934 atomic_read(&ext2_xattr_cache->c_entry_count));
935 error = 0;
937 } else {
938 ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
939 atomic_read(&ext2_xattr_cache->c_entry_count));
940 mb_cache_entry_release(ce);
942 return error;
946 * ext2_xattr_cmp()
948 * Compare two extended attribute blocks for equality.
950 * Returns 0 if the blocks are equal, 1 if they differ, and
951 * a negative error number on errors.
953 static int
954 ext2_xattr_cmp(struct ext2_xattr_header *header1,
955 struct ext2_xattr_header *header2)
957 struct ext2_xattr_entry *entry1, *entry2;
959 entry1 = ENTRY(header1+1);
960 entry2 = ENTRY(header2+1);
961 while (!IS_LAST_ENTRY(entry1)) {
962 if (IS_LAST_ENTRY(entry2))
963 return 1;
964 if (entry1->e_hash != entry2->e_hash ||
965 entry1->e_name_len != entry2->e_name_len ||
966 entry1->e_value_size != entry2->e_value_size ||
967 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
968 return 1;
969 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
970 return -EIO;
971 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
972 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
973 le32_to_cpu(entry1->e_value_size)))
974 return 1;
976 entry1 = EXT2_XATTR_NEXT(entry1);
977 entry2 = EXT2_XATTR_NEXT(entry2);
979 if (!IS_LAST_ENTRY(entry2))
980 return 1;
981 return 0;
985 * ext2_xattr_cache_find()
987 * Find an identical extended attribute block.
989 * Returns a locked buffer head to the block found, or NULL if such
990 * a block was not found or an error occurred.
992 static struct buffer_head *
993 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
995 __u32 hash = le32_to_cpu(header->h_hash);
996 struct mb_cache_entry *ce;
998 if (!header->h_hash)
999 return NULL; /* never share */
1000 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1001 ce = mb_cache_entry_find_first(ext2_xattr_cache, 0,
1002 inode->i_sb->s_bdev, hash);
1003 while (ce) {
1004 struct buffer_head *bh = sb_bread(inode->i_sb, ce->e_block);
1006 if (!bh) {
1007 ext2_error(inode->i_sb, "ext2_xattr_cache_find",
1008 "inode %ld: block %ld read error",
1009 inode->i_ino, (unsigned long) ce->e_block);
1010 } else {
1011 lock_buffer(bh);
1012 if (le32_to_cpu(HDR(bh)->h_refcount) >
1013 EXT2_XATTR_REFCOUNT_MAX) {
1014 ea_idebug(inode, "block %ld refcount %d>%d",
1015 (unsigned long) ce->e_block,
1016 le32_to_cpu(HDR(bh)->h_refcount),
1017 EXT2_XATTR_REFCOUNT_MAX);
1018 } else if (!ext2_xattr_cmp(header, HDR(bh))) {
1019 ea_bdebug(bh, "b_count=%d",
1020 atomic_read(&(bh->b_count)));
1021 mb_cache_entry_release(ce);
1022 return bh;
1024 unlock_buffer(bh);
1025 brelse(bh);
1027 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1029 return NULL;
1033 * ext2_xattr_cache_remove()
1035 * Remove the cache entry of a block from the cache. Called when a
1036 * block becomes invalid.
1038 static void
1039 ext2_xattr_cache_remove(struct buffer_head *bh)
1041 struct mb_cache_entry *ce;
1043 ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev, bh->b_blocknr);
1044 if (ce) {
1045 ea_bdebug(bh, "removing (%d cache entries remaining)",
1046 atomic_read(&ext2_xattr_cache->c_entry_count)-1);
1047 mb_cache_entry_free(ce);
1048 } else
1049 ea_bdebug(bh, "no cache entry");
1052 #define NAME_HASH_SHIFT 5
1053 #define VALUE_HASH_SHIFT 16
1056 * ext2_xattr_hash_entry()
1058 * Compute the hash of an extended attribute.
1060 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
1061 struct ext2_xattr_entry *entry)
1063 __u32 hash = 0;
1064 char *name = entry->e_name;
1065 int n;
1067 for (n=0; n < entry->e_name_len; n++) {
1068 hash = (hash << NAME_HASH_SHIFT) ^
1069 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1070 *name++;
1073 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1074 __le32 *value = (__le32 *)((char *)header +
1075 le16_to_cpu(entry->e_value_offs));
1076 for (n = (le32_to_cpu(entry->e_value_size) +
1077 EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
1078 hash = (hash << VALUE_HASH_SHIFT) ^
1079 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1080 le32_to_cpu(*value++);
1083 entry->e_hash = cpu_to_le32(hash);
1086 #undef NAME_HASH_SHIFT
1087 #undef VALUE_HASH_SHIFT
1089 #define BLOCK_HASH_SHIFT 16
1092 * ext2_xattr_rehash()
1094 * Re-compute the extended attribute hash value after an entry has changed.
1096 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
1097 struct ext2_xattr_entry *entry)
1099 struct ext2_xattr_entry *here;
1100 __u32 hash = 0;
1102 ext2_xattr_hash_entry(header, entry);
1103 here = ENTRY(header+1);
1104 while (!IS_LAST_ENTRY(here)) {
1105 if (!here->e_hash) {
1106 /* Block is not shared if an entry's hash value == 0 */
1107 hash = 0;
1108 break;
1110 hash = (hash << BLOCK_HASH_SHIFT) ^
1111 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1112 le32_to_cpu(here->e_hash);
1113 here = EXT2_XATTR_NEXT(here);
1115 header->h_hash = cpu_to_le32(hash);
1118 #undef BLOCK_HASH_SHIFT
1120 int __init
1121 init_ext2_xattr(void)
1123 int err;
1125 err = ext2_xattr_register(EXT2_XATTR_INDEX_USER,
1126 &ext2_xattr_user_handler);
1127 if (err)
1128 return err;
1129 err = ext2_xattr_register(EXT2_XATTR_INDEX_TRUSTED,
1130 &ext2_xattr_trusted_handler);
1131 if (err)
1132 goto out;
1133 #ifdef CONFIG_EXT2_FS_SECURITY
1134 err = ext2_xattr_register(EXT2_XATTR_INDEX_SECURITY,
1135 &ext2_xattr_security_handler);
1136 if (err)
1137 goto out1;
1138 #endif
1139 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1140 err = init_ext2_acl();
1141 if (err)
1142 goto out2;
1143 #endif
1144 ext2_xattr_cache = mb_cache_create("ext2_xattr", NULL,
1145 sizeof(struct mb_cache_entry) +
1146 sizeof(struct mb_cache_entry_index), 1, 6);
1147 if (!ext2_xattr_cache) {
1148 err = -ENOMEM;
1149 goto out3;
1151 return 0;
1152 out3:
1153 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1154 exit_ext2_acl();
1155 out2:
1156 #endif
1157 #ifdef CONFIG_EXT2_FS_SECURITY
1158 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY,
1159 &ext2_xattr_security_handler);
1160 out1:
1161 #endif
1162 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED,
1163 &ext2_xattr_trusted_handler);
1164 out:
1165 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER,
1166 &ext2_xattr_user_handler);
1167 return err;
1170 void
1171 exit_ext2_xattr(void)
1173 mb_cache_destroy(ext2_xattr_cache);
1174 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1175 exit_ext2_acl();
1176 #endif
1177 #ifdef CONFIG_EXT2_FS_SECURITY
1178 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY,
1179 &ext2_xattr_security_handler);
1180 #endif
1181 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED,
1182 &ext2_xattr_trusted_handler);
1183 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER,
1184 &ext2_xattr_user_handler);