fs: use block_device name vsprintf helper
[linux-2.6-block.git] / fs / ext2 / xattr.c
1 /*
2  * linux/fs/ext2/xattr.c
3  *
4  * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
5  *
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  * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
10  *  Red Hat Inc.
11  *
12  */
13
14 /*
15  * Extended attributes are stored on disk blocks allocated outside of
16  * any inode. The i_file_acl field is then made to point to this allocated
17  * block. If all extended attributes of an inode are identical, these
18  * inodes may share the same extended attribute block. Such situations
19  * are automatically detected by keeping a cache of recent attribute block
20  * numbers and hashes over the block's contents in memory.
21  *
22  *
23  * Extended attribute block layout:
24  *
25  *   +------------------+
26  *   | header           |
27  *   | entry 1          | |
28  *   | entry 2          | | growing downwards
29  *   | entry 3          | v
30  *   | four null bytes  |
31  *   | . . .            |
32  *   | value 1          | ^
33  *   | value 3          | | growing upwards
34  *   | value 2          | |
35  *   +------------------+
36  *
37  * The block header is followed by multiple entry descriptors. These entry
38  * descriptors are variable in size, and aligned to EXT2_XATTR_PAD
39  * byte boundaries. The entry descriptors are sorted by attribute name,
40  * so that two extended attribute blocks can be compared efficiently.
41  *
42  * Attribute values are aligned to the end of the block, stored in
43  * no specific order. They are also padded to EXT2_XATTR_PAD byte
44  * boundaries. No additional gaps are left between them.
45  *
46  * Locking strategy
47  * ----------------
48  * EXT2_I(inode)->i_file_acl is protected by EXT2_I(inode)->xattr_sem.
49  * EA blocks are only changed if they are exclusive to an inode, so
50  * holding xattr_sem also means that nothing but the EA block's reference
51  * count will change. Multiple writers to an EA block are synchronized
52  * by the bh lock. No more than a single bh lock is held at any time
53  * to avoid deadlocks.
54  */
55
56 #include <linux/buffer_head.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #include <linux/mbcache.h>
60 #include <linux/quotaops.h>
61 #include <linux/rwsem.h>
62 #include <linux/security.h>
63 #include "ext2.h"
64 #include "xattr.h"
65 #include "acl.h"
66
67 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
68 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
69 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
70 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
71
72 #ifdef EXT2_XATTR_DEBUG
73 # define ea_idebug(inode, f...) do { \
74                 printk(KERN_DEBUG "inode %s:%ld: ", \
75                         inode->i_sb->s_id, inode->i_ino); \
76                 printk(f); \
77                 printk("\n"); \
78         } while (0)
79 # define ea_bdebug(bh, f...) do { \
80                 printk(KERN_DEBUG "block %pg:%lu: ", \
81                         bh->b_bdev, (unsigned long) bh->b_blocknr); \
82                 printk(f); \
83                 printk("\n"); \
84         } while (0)
85 #else
86 # define ea_idebug(f...)
87 # define ea_bdebug(f...)
88 #endif
89
90 static int ext2_xattr_set2(struct inode *, struct buffer_head *,
91                            struct ext2_xattr_header *);
92
93 static int ext2_xattr_cache_insert(struct buffer_head *);
94 static struct buffer_head *ext2_xattr_cache_find(struct inode *,
95                                                  struct ext2_xattr_header *);
96 static void ext2_xattr_rehash(struct ext2_xattr_header *,
97                               struct ext2_xattr_entry *);
98
99 static struct mb_cache *ext2_xattr_cache;
100
101 static const struct xattr_handler *ext2_xattr_handler_map[] = {
102         [EXT2_XATTR_INDEX_USER]              = &ext2_xattr_user_handler,
103 #ifdef CONFIG_EXT2_FS_POSIX_ACL
104         [EXT2_XATTR_INDEX_POSIX_ACL_ACCESS]  = &posix_acl_access_xattr_handler,
105         [EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
106 #endif
107         [EXT2_XATTR_INDEX_TRUSTED]           = &ext2_xattr_trusted_handler,
108 #ifdef CONFIG_EXT2_FS_SECURITY
109         [EXT2_XATTR_INDEX_SECURITY]          = &ext2_xattr_security_handler,
110 #endif
111 };
112
113 const struct xattr_handler *ext2_xattr_handlers[] = {
114         &ext2_xattr_user_handler,
115         &ext2_xattr_trusted_handler,
116 #ifdef CONFIG_EXT2_FS_POSIX_ACL
117         &posix_acl_access_xattr_handler,
118         &posix_acl_default_xattr_handler,
119 #endif
120 #ifdef CONFIG_EXT2_FS_SECURITY
121         &ext2_xattr_security_handler,
122 #endif
123         NULL
124 };
125
126 static inline const struct xattr_handler *
127 ext2_xattr_handler(int name_index)
128 {
129         const struct xattr_handler *handler = NULL;
130
131         if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
132                 handler = ext2_xattr_handler_map[name_index];
133         return handler;
134 }
135
136 /*
137  * ext2_xattr_get()
138  *
139  * Copy an extended attribute into the buffer
140  * provided, or compute the buffer size required.
141  * Buffer is NULL to compute the size of the buffer required.
142  *
143  * Returns a negative error number on failure, or the number of bytes
144  * used / required on success.
145  */
146 int
147 ext2_xattr_get(struct inode *inode, int name_index, const char *name,
148                void *buffer, size_t buffer_size)
149 {
150         struct buffer_head *bh = NULL;
151         struct ext2_xattr_entry *entry;
152         size_t name_len, size;
153         char *end;
154         int error;
155
156         ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
157                   name_index, name, buffer, (long)buffer_size);
158
159         if (name == NULL)
160                 return -EINVAL;
161         name_len = strlen(name);
162         if (name_len > 255)
163                 return -ERANGE;
164
165         down_read(&EXT2_I(inode)->xattr_sem);
166         error = -ENODATA;
167         if (!EXT2_I(inode)->i_file_acl)
168                 goto cleanup;
169         ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
170         bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
171         error = -EIO;
172         if (!bh)
173                 goto cleanup;
174         ea_bdebug(bh, "b_count=%d, refcount=%d",
175                 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
176         end = bh->b_data + bh->b_size;
177         if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
178             HDR(bh)->h_blocks != cpu_to_le32(1)) {
179 bad_block:      ext2_error(inode->i_sb, "ext2_xattr_get",
180                         "inode %ld: bad block %d", inode->i_ino,
181                         EXT2_I(inode)->i_file_acl);
182                 error = -EIO;
183                 goto cleanup;
184         }
185
186         /* find named attribute */
187         entry = FIRST_ENTRY(bh);
188         while (!IS_LAST_ENTRY(entry)) {
189                 struct ext2_xattr_entry *next =
190                         EXT2_XATTR_NEXT(entry);
191                 if ((char *)next >= end)
192                         goto bad_block;
193                 if (name_index == entry->e_name_index &&
194                     name_len == entry->e_name_len &&
195                     memcmp(name, entry->e_name, name_len) == 0)
196                         goto found;
197                 entry = next;
198         }
199         if (ext2_xattr_cache_insert(bh))
200                 ea_idebug(inode, "cache insert failed");
201         error = -ENODATA;
202         goto cleanup;
203 found:
204         /* check the buffer size */
205         if (entry->e_value_block != 0)
206                 goto bad_block;
207         size = le32_to_cpu(entry->e_value_size);
208         if (size > inode->i_sb->s_blocksize ||
209             le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
210                 goto bad_block;
211
212         if (ext2_xattr_cache_insert(bh))
213                 ea_idebug(inode, "cache insert failed");
214         if (buffer) {
215                 error = -ERANGE;
216                 if (size > buffer_size)
217                         goto cleanup;
218                 /* return value of attribute */
219                 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
220                         size);
221         }
222         error = size;
223
224 cleanup:
225         brelse(bh);
226         up_read(&EXT2_I(inode)->xattr_sem);
227
228         return error;
229 }
230
231 /*
232  * ext2_xattr_list()
233  *
234  * Copy a list of attribute names into the buffer
235  * provided, or compute the buffer size required.
236  * Buffer is NULL to compute the size of the buffer required.
237  *
238  * Returns a negative error number on failure, or the number of bytes
239  * used / required on success.
240  */
241 static int
242 ext2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
243 {
244         struct inode *inode = d_inode(dentry);
245         struct buffer_head *bh = NULL;
246         struct ext2_xattr_entry *entry;
247         char *end;
248         size_t rest = buffer_size;
249         int error;
250
251         ea_idebug(inode, "buffer=%p, buffer_size=%ld",
252                   buffer, (long)buffer_size);
253
254         down_read(&EXT2_I(inode)->xattr_sem);
255         error = 0;
256         if (!EXT2_I(inode)->i_file_acl)
257                 goto cleanup;
258         ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
259         bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
260         error = -EIO;
261         if (!bh)
262                 goto cleanup;
263         ea_bdebug(bh, "b_count=%d, refcount=%d",
264                 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
265         end = bh->b_data + bh->b_size;
266         if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
267             HDR(bh)->h_blocks != cpu_to_le32(1)) {
268 bad_block:      ext2_error(inode->i_sb, "ext2_xattr_list",
269                         "inode %ld: bad block %d", inode->i_ino,
270                         EXT2_I(inode)->i_file_acl);
271                 error = -EIO;
272                 goto cleanup;
273         }
274
275         /* check the on-disk data structure */
276         entry = FIRST_ENTRY(bh);
277         while (!IS_LAST_ENTRY(entry)) {
278                 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(entry);
279
280                 if ((char *)next >= end)
281                         goto bad_block;
282                 entry = next;
283         }
284         if (ext2_xattr_cache_insert(bh))
285                 ea_idebug(inode, "cache insert failed");
286
287         /* list the attribute names */
288         for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
289              entry = EXT2_XATTR_NEXT(entry)) {
290                 const struct xattr_handler *handler =
291                         ext2_xattr_handler(entry->e_name_index);
292
293                 if (handler) {
294                         size_t size = handler->list(handler, dentry, buffer,
295                                                     rest, entry->e_name,
296                                                     entry->e_name_len);
297                         if (buffer) {
298                                 if (size > rest) {
299                                         error = -ERANGE;
300                                         goto cleanup;
301                                 }
302                                 buffer += size;
303                         }
304                         rest -= size;
305                 }
306         }
307         error = buffer_size - rest;  /* total size */
308
309 cleanup:
310         brelse(bh);
311         up_read(&EXT2_I(inode)->xattr_sem);
312
313         return error;
314 }
315
316 /*
317  * Inode operation listxattr()
318  *
319  * d_inode(dentry)->i_mutex: don't care
320  */
321 ssize_t
322 ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
323 {
324         return ext2_xattr_list(dentry, buffer, size);
325 }
326
327 /*
328  * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
329  * not set, set it.
330  */
331 static void ext2_xattr_update_super_block(struct super_block *sb)
332 {
333         if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
334                 return;
335
336         spin_lock(&EXT2_SB(sb)->s_lock);
337         EXT2_SET_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR);
338         spin_unlock(&EXT2_SB(sb)->s_lock);
339         mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
340 }
341
342 /*
343  * ext2_xattr_set()
344  *
345  * Create, replace or remove an extended attribute for this inode.  Value
346  * is NULL to remove an existing extended attribute, and non-NULL to
347  * either replace an existing extended attribute, or create a new extended
348  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
349  * specify that an extended attribute must exist and must not exist
350  * previous to the call, respectively.
351  *
352  * Returns 0, or a negative error number on failure.
353  */
354 int
355 ext2_xattr_set(struct inode *inode, int name_index, const char *name,
356                const void *value, size_t value_len, int flags)
357 {
358         struct super_block *sb = inode->i_sb;
359         struct buffer_head *bh = NULL;
360         struct ext2_xattr_header *header = NULL;
361         struct ext2_xattr_entry *here, *last;
362         size_t name_len, free, min_offs = sb->s_blocksize;
363         int not_found = 1, error;
364         char *end;
365         
366         /*
367          * header -- Points either into bh, or to a temporarily
368          *           allocated buffer.
369          * here -- The named entry found, or the place for inserting, within
370          *         the block pointed to by header.
371          * last -- Points right after the last named entry within the block
372          *         pointed to by header.
373          * min_offs -- The offset of the first value (values are aligned
374          *             towards the end of the block).
375          * end -- Points right after the block pointed to by header.
376          */
377         
378         ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
379                   name_index, name, value, (long)value_len);
380
381         if (value == NULL)
382                 value_len = 0;
383         if (name == NULL)
384                 return -EINVAL;
385         name_len = strlen(name);
386         if (name_len > 255 || value_len > sb->s_blocksize)
387                 return -ERANGE;
388         down_write(&EXT2_I(inode)->xattr_sem);
389         if (EXT2_I(inode)->i_file_acl) {
390                 /* The inode already has an extended attribute block. */
391                 bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
392                 error = -EIO;
393                 if (!bh)
394                         goto cleanup;
395                 ea_bdebug(bh, "b_count=%d, refcount=%d",
396                         atomic_read(&(bh->b_count)),
397                         le32_to_cpu(HDR(bh)->h_refcount));
398                 header = HDR(bh);
399                 end = bh->b_data + bh->b_size;
400                 if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
401                     header->h_blocks != cpu_to_le32(1)) {
402 bad_block:              ext2_error(sb, "ext2_xattr_set",
403                                 "inode %ld: bad block %d", inode->i_ino, 
404                                    EXT2_I(inode)->i_file_acl);
405                         error = -EIO;
406                         goto cleanup;
407                 }
408                 /* Find the named attribute. */
409                 here = FIRST_ENTRY(bh);
410                 while (!IS_LAST_ENTRY(here)) {
411                         struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(here);
412                         if ((char *)next >= end)
413                                 goto bad_block;
414                         if (!here->e_value_block && here->e_value_size) {
415                                 size_t offs = le16_to_cpu(here->e_value_offs);
416                                 if (offs < min_offs)
417                                         min_offs = offs;
418                         }
419                         not_found = name_index - here->e_name_index;
420                         if (!not_found)
421                                 not_found = name_len - here->e_name_len;
422                         if (!not_found)
423                                 not_found = memcmp(name, here->e_name,name_len);
424                         if (not_found <= 0)
425                                 break;
426                         here = next;
427                 }
428                 last = here;
429                 /* We still need to compute min_offs and last. */
430                 while (!IS_LAST_ENTRY(last)) {
431                         struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
432                         if ((char *)next >= end)
433                                 goto bad_block;
434                         if (!last->e_value_block && last->e_value_size) {
435                                 size_t offs = le16_to_cpu(last->e_value_offs);
436                                 if (offs < min_offs)
437                                         min_offs = offs;
438                         }
439                         last = next;
440                 }
441
442                 /* Check whether we have enough space left. */
443                 free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
444         } else {
445                 /* We will use a new extended attribute block. */
446                 free = sb->s_blocksize -
447                         sizeof(struct ext2_xattr_header) - sizeof(__u32);
448                 here = last = NULL;  /* avoid gcc uninitialized warning. */
449         }
450
451         if (not_found) {
452                 /* Request to remove a nonexistent attribute? */
453                 error = -ENODATA;
454                 if (flags & XATTR_REPLACE)
455                         goto cleanup;
456                 error = 0;
457                 if (value == NULL)
458                         goto cleanup;
459         } else {
460                 /* Request to create an existing attribute? */
461                 error = -EEXIST;
462                 if (flags & XATTR_CREATE)
463                         goto cleanup;
464                 if (!here->e_value_block && here->e_value_size) {
465                         size_t size = le32_to_cpu(here->e_value_size);
466
467                         if (le16_to_cpu(here->e_value_offs) + size > 
468                             sb->s_blocksize || size > sb->s_blocksize)
469                                 goto bad_block;
470                         free += EXT2_XATTR_SIZE(size);
471                 }
472                 free += EXT2_XATTR_LEN(name_len);
473         }
474         error = -ENOSPC;
475         if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
476                 goto cleanup;
477
478         /* Here we know that we can set the new attribute. */
479
480         if (header) {
481                 struct mb_cache_entry *ce;
482
483                 /* assert(header == HDR(bh)); */
484                 ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev,
485                                         bh->b_blocknr);
486                 lock_buffer(bh);
487                 if (header->h_refcount == cpu_to_le32(1)) {
488                         ea_bdebug(bh, "modifying in-place");
489                         if (ce)
490                                 mb_cache_entry_free(ce);
491                         /* keep the buffer locked while modifying it. */
492                 } else {
493                         int offset;
494
495                         if (ce)
496                                 mb_cache_entry_release(ce);
497                         unlock_buffer(bh);
498                         ea_bdebug(bh, "cloning");
499                         header = kmalloc(bh->b_size, GFP_KERNEL);
500                         error = -ENOMEM;
501                         if (header == NULL)
502                                 goto cleanup;
503                         memcpy(header, HDR(bh), bh->b_size);
504                         header->h_refcount = cpu_to_le32(1);
505
506                         offset = (char *)here - bh->b_data;
507                         here = ENTRY((char *)header + offset);
508                         offset = (char *)last - bh->b_data;
509                         last = ENTRY((char *)header + offset);
510                 }
511         } else {
512                 /* Allocate a buffer where we construct the new block. */
513                 header = kzalloc(sb->s_blocksize, GFP_KERNEL);
514                 error = -ENOMEM;
515                 if (header == NULL)
516                         goto cleanup;
517                 end = (char *)header + sb->s_blocksize;
518                 header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
519                 header->h_blocks = header->h_refcount = cpu_to_le32(1);
520                 last = here = ENTRY(header+1);
521         }
522
523         /* Iff we are modifying the block in-place, bh is locked here. */
524
525         if (not_found) {
526                 /* Insert the new name. */
527                 size_t size = EXT2_XATTR_LEN(name_len);
528                 size_t rest = (char *)last - (char *)here;
529                 memmove((char *)here + size, here, rest);
530                 memset(here, 0, size);
531                 here->e_name_index = name_index;
532                 here->e_name_len = name_len;
533                 memcpy(here->e_name, name, name_len);
534         } else {
535                 if (!here->e_value_block && here->e_value_size) {
536                         char *first_val = (char *)header + min_offs;
537                         size_t offs = le16_to_cpu(here->e_value_offs);
538                         char *val = (char *)header + offs;
539                         size_t size = EXT2_XATTR_SIZE(
540                                 le32_to_cpu(here->e_value_size));
541
542                         if (size == EXT2_XATTR_SIZE(value_len)) {
543                                 /* The old and the new value have the same
544                                    size. Just replace. */
545                                 here->e_value_size = cpu_to_le32(value_len);
546                                 memset(val + size - EXT2_XATTR_PAD, 0,
547                                        EXT2_XATTR_PAD); /* Clear pad bytes. */
548                                 memcpy(val, value, value_len);
549                                 goto skip_replace;
550                         }
551
552                         /* Remove the old value. */
553                         memmove(first_val + size, first_val, val - first_val);
554                         memset(first_val, 0, size);
555                         here->e_value_offs = 0;
556                         min_offs += size;
557
558                         /* Adjust all value offsets. */
559                         last = ENTRY(header+1);
560                         while (!IS_LAST_ENTRY(last)) {
561                                 size_t o = le16_to_cpu(last->e_value_offs);
562                                 if (!last->e_value_block && o < offs)
563                                         last->e_value_offs =
564                                                 cpu_to_le16(o + size);
565                                 last = EXT2_XATTR_NEXT(last);
566                         }
567                 }
568                 if (value == NULL) {
569                         /* Remove the old name. */
570                         size_t size = EXT2_XATTR_LEN(name_len);
571                         last = ENTRY((char *)last - size);
572                         memmove(here, (char*)here + size,
573                                 (char*)last - (char*)here);
574                         memset(last, 0, size);
575                 }
576         }
577
578         if (value != NULL) {
579                 /* Insert the new value. */
580                 here->e_value_size = cpu_to_le32(value_len);
581                 if (value_len) {
582                         size_t size = EXT2_XATTR_SIZE(value_len);
583                         char *val = (char *)header + min_offs - size;
584                         here->e_value_offs =
585                                 cpu_to_le16((char *)val - (char *)header);
586                         memset(val + size - EXT2_XATTR_PAD, 0,
587                                EXT2_XATTR_PAD); /* Clear the pad bytes. */
588                         memcpy(val, value, value_len);
589                 }
590         }
591
592 skip_replace:
593         if (IS_LAST_ENTRY(ENTRY(header+1))) {
594                 /* This block is now empty. */
595                 if (bh && header == HDR(bh))
596                         unlock_buffer(bh);  /* we were modifying in-place. */
597                 error = ext2_xattr_set2(inode, bh, NULL);
598         } else {
599                 ext2_xattr_rehash(header, here);
600                 if (bh && header == HDR(bh))
601                         unlock_buffer(bh);  /* we were modifying in-place. */
602                 error = ext2_xattr_set2(inode, bh, header);
603         }
604
605 cleanup:
606         brelse(bh);
607         if (!(bh && header == HDR(bh)))
608                 kfree(header);
609         up_write(&EXT2_I(inode)->xattr_sem);
610
611         return error;
612 }
613
614 /*
615  * Second half of ext2_xattr_set(): Update the file system.
616  */
617 static int
618 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
619                 struct ext2_xattr_header *header)
620 {
621         struct super_block *sb = inode->i_sb;
622         struct buffer_head *new_bh = NULL;
623         int error;
624
625         if (header) {
626                 new_bh = ext2_xattr_cache_find(inode, header);
627                 if (new_bh) {
628                         /* We found an identical block in the cache. */
629                         if (new_bh == old_bh) {
630                                 ea_bdebug(new_bh, "keeping this block");
631                         } else {
632                                 /* The old block is released after updating
633                                    the inode.  */
634                                 ea_bdebug(new_bh, "reusing block");
635
636                                 error = dquot_alloc_block(inode, 1);
637                                 if (error) {
638                                         unlock_buffer(new_bh);
639                                         goto cleanup;
640                                 }
641                                 le32_add_cpu(&HDR(new_bh)->h_refcount, 1);
642                                 ea_bdebug(new_bh, "refcount now=%d",
643                                         le32_to_cpu(HDR(new_bh)->h_refcount));
644                         }
645                         unlock_buffer(new_bh);
646                 } else if (old_bh && header == HDR(old_bh)) {
647                         /* Keep this block. No need to lock the block as we
648                            don't need to change the reference count. */
649                         new_bh = old_bh;
650                         get_bh(new_bh);
651                         ext2_xattr_cache_insert(new_bh);
652                 } else {
653                         /* We need to allocate a new block */
654                         ext2_fsblk_t goal = ext2_group_first_block_no(sb,
655                                                 EXT2_I(inode)->i_block_group);
656                         int block = ext2_new_block(inode, goal, &error);
657                         if (error)
658                                 goto cleanup;
659                         ea_idebug(inode, "creating block %d", block);
660
661                         new_bh = sb_getblk(sb, block);
662                         if (unlikely(!new_bh)) {
663                                 ext2_free_blocks(inode, block, 1);
664                                 mark_inode_dirty(inode);
665                                 error = -ENOMEM;
666                                 goto cleanup;
667                         }
668                         lock_buffer(new_bh);
669                         memcpy(new_bh->b_data, header, new_bh->b_size);
670                         set_buffer_uptodate(new_bh);
671                         unlock_buffer(new_bh);
672                         ext2_xattr_cache_insert(new_bh);
673                         
674                         ext2_xattr_update_super_block(sb);
675                 }
676                 mark_buffer_dirty(new_bh);
677                 if (IS_SYNC(inode)) {
678                         sync_dirty_buffer(new_bh);
679                         error = -EIO;
680                         if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
681                                 goto cleanup;
682                 }
683         }
684
685         /* Update the inode. */
686         EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
687         inode->i_ctime = CURRENT_TIME_SEC;
688         if (IS_SYNC(inode)) {
689                 error = sync_inode_metadata(inode, 1);
690                 /* In case sync failed due to ENOSPC the inode was actually
691                  * written (only some dirty data were not) so we just proceed
692                  * as if nothing happened and cleanup the unused block */
693                 if (error && error != -ENOSPC) {
694                         if (new_bh && new_bh != old_bh) {
695                                 dquot_free_block_nodirty(inode, 1);
696                                 mark_inode_dirty(inode);
697                         }
698                         goto cleanup;
699                 }
700         } else
701                 mark_inode_dirty(inode);
702
703         error = 0;
704         if (old_bh && old_bh != new_bh) {
705                 struct mb_cache_entry *ce;
706
707                 /*
708                  * If there was an old block and we are no longer using it,
709                  * release the old block.
710                  */
711                 ce = mb_cache_entry_get(ext2_xattr_cache, old_bh->b_bdev,
712                                         old_bh->b_blocknr);
713                 lock_buffer(old_bh);
714                 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
715                         /* Free the old block. */
716                         if (ce)
717                                 mb_cache_entry_free(ce);
718                         ea_bdebug(old_bh, "freeing");
719                         ext2_free_blocks(inode, old_bh->b_blocknr, 1);
720                         mark_inode_dirty(inode);
721                         /* We let our caller release old_bh, so we
722                          * need to duplicate the buffer before. */
723                         get_bh(old_bh);
724                         bforget(old_bh);
725                 } else {
726                         /* Decrement the refcount only. */
727                         le32_add_cpu(&HDR(old_bh)->h_refcount, -1);
728                         if (ce)
729                                 mb_cache_entry_release(ce);
730                         dquot_free_block_nodirty(inode, 1);
731                         mark_inode_dirty(inode);
732                         mark_buffer_dirty(old_bh);
733                         ea_bdebug(old_bh, "refcount now=%d",
734                                 le32_to_cpu(HDR(old_bh)->h_refcount));
735                 }
736                 unlock_buffer(old_bh);
737         }
738
739 cleanup:
740         brelse(new_bh);
741
742         return error;
743 }
744
745 /*
746  * ext2_xattr_delete_inode()
747  *
748  * Free extended attribute resources associated with this inode. This
749  * is called immediately before an inode is freed.
750  */
751 void
752 ext2_xattr_delete_inode(struct inode *inode)
753 {
754         struct buffer_head *bh = NULL;
755         struct mb_cache_entry *ce;
756
757         down_write(&EXT2_I(inode)->xattr_sem);
758         if (!EXT2_I(inode)->i_file_acl)
759                 goto cleanup;
760         bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
761         if (!bh) {
762                 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
763                         "inode %ld: block %d read error", inode->i_ino,
764                         EXT2_I(inode)->i_file_acl);
765                 goto cleanup;
766         }
767         ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
768         if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
769             HDR(bh)->h_blocks != cpu_to_le32(1)) {
770                 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
771                         "inode %ld: bad block %d", inode->i_ino,
772                         EXT2_I(inode)->i_file_acl);
773                 goto cleanup;
774         }
775         ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev, bh->b_blocknr);
776         lock_buffer(bh);
777         if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
778                 if (ce)
779                         mb_cache_entry_free(ce);
780                 ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
781                 get_bh(bh);
782                 bforget(bh);
783                 unlock_buffer(bh);
784         } else {
785                 le32_add_cpu(&HDR(bh)->h_refcount, -1);
786                 if (ce)
787                         mb_cache_entry_release(ce);
788                 ea_bdebug(bh, "refcount now=%d",
789                         le32_to_cpu(HDR(bh)->h_refcount));
790                 unlock_buffer(bh);
791                 mark_buffer_dirty(bh);
792                 if (IS_SYNC(inode))
793                         sync_dirty_buffer(bh);
794                 dquot_free_block_nodirty(inode, 1);
795         }
796         EXT2_I(inode)->i_file_acl = 0;
797
798 cleanup:
799         brelse(bh);
800         up_write(&EXT2_I(inode)->xattr_sem);
801 }
802
803 /*
804  * ext2_xattr_put_super()
805  *
806  * This is called when a file system is unmounted.
807  */
808 void
809 ext2_xattr_put_super(struct super_block *sb)
810 {
811         mb_cache_shrink(sb->s_bdev);
812 }
813
814
815 /*
816  * ext2_xattr_cache_insert()
817  *
818  * Create a new entry in the extended attribute cache, and insert
819  * it unless such an entry is already in the cache.
820  *
821  * Returns 0, or a negative error number on failure.
822  */
823 static int
824 ext2_xattr_cache_insert(struct buffer_head *bh)
825 {
826         __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
827         struct mb_cache_entry *ce;
828         int error;
829
830         ce = mb_cache_entry_alloc(ext2_xattr_cache, GFP_NOFS);
831         if (!ce)
832                 return -ENOMEM;
833         error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
834         if (error) {
835                 mb_cache_entry_free(ce);
836                 if (error == -EBUSY) {
837                         ea_bdebug(bh, "already in cache (%d cache entries)",
838                                 atomic_read(&ext2_xattr_cache->c_entry_count));
839                         error = 0;
840                 }
841         } else {
842                 ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
843                           atomic_read(&ext2_xattr_cache->c_entry_count));
844                 mb_cache_entry_release(ce);
845         }
846         return error;
847 }
848
849 /*
850  * ext2_xattr_cmp()
851  *
852  * Compare two extended attribute blocks for equality.
853  *
854  * Returns 0 if the blocks are equal, 1 if they differ, and
855  * a negative error number on errors.
856  */
857 static int
858 ext2_xattr_cmp(struct ext2_xattr_header *header1,
859                struct ext2_xattr_header *header2)
860 {
861         struct ext2_xattr_entry *entry1, *entry2;
862
863         entry1 = ENTRY(header1+1);
864         entry2 = ENTRY(header2+1);
865         while (!IS_LAST_ENTRY(entry1)) {
866                 if (IS_LAST_ENTRY(entry2))
867                         return 1;
868                 if (entry1->e_hash != entry2->e_hash ||
869                     entry1->e_name_index != entry2->e_name_index ||
870                     entry1->e_name_len != entry2->e_name_len ||
871                     entry1->e_value_size != entry2->e_value_size ||
872                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
873                         return 1;
874                 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
875                         return -EIO;
876                 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
877                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
878                            le32_to_cpu(entry1->e_value_size)))
879                         return 1;
880
881                 entry1 = EXT2_XATTR_NEXT(entry1);
882                 entry2 = EXT2_XATTR_NEXT(entry2);
883         }
884         if (!IS_LAST_ENTRY(entry2))
885                 return 1;
886         return 0;
887 }
888
889 /*
890  * ext2_xattr_cache_find()
891  *
892  * Find an identical extended attribute block.
893  *
894  * Returns a locked buffer head to the block found, or NULL if such
895  * a block was not found or an error occurred.
896  */
897 static struct buffer_head *
898 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
899 {
900         __u32 hash = le32_to_cpu(header->h_hash);
901         struct mb_cache_entry *ce;
902
903         if (!header->h_hash)
904                 return NULL;  /* never share */
905         ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
906 again:
907         ce = mb_cache_entry_find_first(ext2_xattr_cache, inode->i_sb->s_bdev,
908                                        hash);
909         while (ce) {
910                 struct buffer_head *bh;
911
912                 if (IS_ERR(ce)) {
913                         if (PTR_ERR(ce) == -EAGAIN)
914                                 goto again;
915                         break;
916                 }
917
918                 bh = sb_bread(inode->i_sb, ce->e_block);
919                 if (!bh) {
920                         ext2_error(inode->i_sb, "ext2_xattr_cache_find",
921                                 "inode %ld: block %ld read error",
922                                 inode->i_ino, (unsigned long) ce->e_block);
923                 } else {
924                         lock_buffer(bh);
925                         if (le32_to_cpu(HDR(bh)->h_refcount) >
926                                    EXT2_XATTR_REFCOUNT_MAX) {
927                                 ea_idebug(inode, "block %ld refcount %d>%d",
928                                           (unsigned long) ce->e_block,
929                                           le32_to_cpu(HDR(bh)->h_refcount),
930                                           EXT2_XATTR_REFCOUNT_MAX);
931                         } else if (!ext2_xattr_cmp(header, HDR(bh))) {
932                                 ea_bdebug(bh, "b_count=%d",
933                                           atomic_read(&(bh->b_count)));
934                                 mb_cache_entry_release(ce);
935                                 return bh;
936                         }
937                         unlock_buffer(bh);
938                         brelse(bh);
939                 }
940                 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
941         }
942         return NULL;
943 }
944
945 #define NAME_HASH_SHIFT 5
946 #define VALUE_HASH_SHIFT 16
947
948 /*
949  * ext2_xattr_hash_entry()
950  *
951  * Compute the hash of an extended attribute.
952  */
953 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
954                                          struct ext2_xattr_entry *entry)
955 {
956         __u32 hash = 0;
957         char *name = entry->e_name;
958         int n;
959
960         for (n=0; n < entry->e_name_len; n++) {
961                 hash = (hash << NAME_HASH_SHIFT) ^
962                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
963                        *name++;
964         }
965
966         if (entry->e_value_block == 0 && entry->e_value_size != 0) {
967                 __le32 *value = (__le32 *)((char *)header +
968                         le16_to_cpu(entry->e_value_offs));
969                 for (n = (le32_to_cpu(entry->e_value_size) +
970                      EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
971                         hash = (hash << VALUE_HASH_SHIFT) ^
972                                (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
973                                le32_to_cpu(*value++);
974                 }
975         }
976         entry->e_hash = cpu_to_le32(hash);
977 }
978
979 #undef NAME_HASH_SHIFT
980 #undef VALUE_HASH_SHIFT
981
982 #define BLOCK_HASH_SHIFT 16
983
984 /*
985  * ext2_xattr_rehash()
986  *
987  * Re-compute the extended attribute hash value after an entry has changed.
988  */
989 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
990                               struct ext2_xattr_entry *entry)
991 {
992         struct ext2_xattr_entry *here;
993         __u32 hash = 0;
994         
995         ext2_xattr_hash_entry(header, entry);
996         here = ENTRY(header+1);
997         while (!IS_LAST_ENTRY(here)) {
998                 if (!here->e_hash) {
999                         /* Block is not shared if an entry's hash value == 0 */
1000                         hash = 0;
1001                         break;
1002                 }
1003                 hash = (hash << BLOCK_HASH_SHIFT) ^
1004                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1005                        le32_to_cpu(here->e_hash);
1006                 here = EXT2_XATTR_NEXT(here);
1007         }
1008         header->h_hash = cpu_to_le32(hash);
1009 }
1010
1011 #undef BLOCK_HASH_SHIFT
1012
1013 int __init
1014 init_ext2_xattr(void)
1015 {
1016         ext2_xattr_cache = mb_cache_create("ext2_xattr", 6);
1017         if (!ext2_xattr_cache)
1018                 return -ENOMEM;
1019         return 0;
1020 }
1021
1022 void
1023 exit_ext2_xattr(void)
1024 {
1025         mb_cache_destroy(ext2_xattr_cache);
1026 }