Pull release into acpica branch
[linux-2.6] / fs / ext3 / xattr.c
1 /*
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 { \
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
95
96 static void ext3_xattr_cache_insert(struct buffer_head *);
97 static struct buffer_head *ext3_xattr_cache_find(struct inode *,
98                                                  struct ext3_xattr_header *,
99                                                  struct mb_cache_entry **);
100 static void ext3_xattr_rehash(struct ext3_xattr_header *,
101                               struct ext3_xattr_entry *);
102
103 static struct mb_cache *ext3_xattr_cache;
104
105 static 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
117 struct 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
130 static inline struct xattr_handler *
131 ext3_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  *
143  * dentry->d_inode->i_mutex: don't care
144  */
145 ssize_t
146 ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
147 {
148         return ext3_xattr_list(dentry->d_inode, buffer, size);
149 }
150
151 static int
152 ext3_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
163 static inline int
164 ext3_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
175 static inline int
176 ext3_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
186 static int
187 ext3_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
213 static int
214 ext3_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;
228         ea_idebug(inode, "reading block %d", EXT3_I(inode)->i_file_acl);
229         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)) {
235 bad_block:      ext3_error(inode->i_sb, __FUNCTION__,
236                            "inode %ld: bad block %d", inode->i_ino,
237                            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
258 cleanup:
259         brelse(bh);
260         return error;
261 }
262
263 static int
264 ext3_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
301 cleanup:
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  */
316 int
317 ext3_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
332 static int
333 ext3_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
357 static int
358 ext3_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;
369         ea_idebug(inode, "reading block %d", EXT3_I(inode)->i_file_acl);
370         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__,
378                            "inode %ld: bad block %d", inode->i_ino,
379                            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
386 cleanup:
387         brelse(bh);
388
389         return error;
390 }
391
392 static int
393 ext3_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
415 cleanup:
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  */
430 int
431 ext3_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  */
456 static 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
462         lock_super(sb);
463         if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
464                 EXT3_SB(sb)->s_es->s_feature_compat |=
465                         cpu_to_le32(EXT3_FEATURE_COMPAT_EXT_ATTR);
466                 sb->s_dirt = 1;
467                 ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
468         }
469         unlock_super(sb);
470 }
471
472 /*
473  * Release the xattr block BH: If the reference count is > 1, decrement
474  * it; otherwise free the block.
475  */
476 static void
477 ext3_xattr_release_block(handle_t *handle, struct inode *inode,
478                          struct buffer_head *bh)
479 {
480         struct mb_cache_entry *ce = NULL;
481
482         ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
483         if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
484                 ea_bdebug(bh, "refcount now=0; freeing");
485                 if (ce)
486                         mb_cache_entry_free(ce);
487                 ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
488                 get_bh(bh);
489                 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
490         } else {
491                 if (ext3_journal_get_write_access(handle, bh) == 0) {
492                         lock_buffer(bh);
493                         BHDR(bh)->h_refcount = cpu_to_le32(
494                                 le32_to_cpu(BHDR(bh)->h_refcount) - 1);
495                         ext3_journal_dirty_metadata(handle, bh);
496                         if (IS_SYNC(inode))
497                                 handle->h_sync = 1;
498                         DQUOT_FREE_BLOCK(inode, 1);
499                         unlock_buffer(bh);
500                         ea_bdebug(bh, "refcount now=%d; releasing",
501                                   le32_to_cpu(BHDR(bh)->h_refcount));
502                 }
503                 if (ce)
504                         mb_cache_entry_release(ce);
505         }
506 }
507
508 struct ext3_xattr_info {
509         int name_index;
510         const char *name;
511         const void *value;
512         size_t value_len;
513 };
514
515 struct ext3_xattr_search {
516         struct ext3_xattr_entry *first;
517         void *base;
518         void *end;
519         struct ext3_xattr_entry *here;
520         int not_found;
521 };
522
523 static int
524 ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
525 {
526         struct ext3_xattr_entry *last;
527         size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
528
529         /* Compute min_offs and last. */
530         last = s->first;
531         for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
532                 if (!last->e_value_block && last->e_value_size) {
533                         size_t offs = le16_to_cpu(last->e_value_offs);
534                         if (offs < min_offs)
535                                 min_offs = offs;
536                 }
537         }
538         free = min_offs - ((void *)last - s->base) - sizeof(__u32);
539         if (!s->not_found) {
540                 if (!s->here->e_value_block && s->here->e_value_size) {
541                         size_t size = le32_to_cpu(s->here->e_value_size);
542                         free += EXT3_XATTR_SIZE(size);
543                 }
544                 free += EXT3_XATTR_LEN(name_len);
545         }
546         if (i->value) {
547                 if (free < EXT3_XATTR_SIZE(i->value_len) ||
548                     free < EXT3_XATTR_LEN(name_len) +
549                            EXT3_XATTR_SIZE(i->value_len))
550                         return -ENOSPC;
551         }
552
553         if (i->value && s->not_found) {
554                 /* Insert the new name. */
555                 size_t size = EXT3_XATTR_LEN(name_len);
556                 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
557                 memmove((void *)s->here + size, s->here, rest);
558                 memset(s->here, 0, size);
559                 s->here->e_name_index = i->name_index;
560                 s->here->e_name_len = name_len;
561                 memcpy(s->here->e_name, i->name, name_len);
562         } else {
563                 if (!s->here->e_value_block && s->here->e_value_size) {
564                         void *first_val = s->base + min_offs;
565                         size_t offs = le16_to_cpu(s->here->e_value_offs);
566                         void *val = s->base + offs;
567                         size_t size = EXT3_XATTR_SIZE(
568                                 le32_to_cpu(s->here->e_value_size));
569
570                         if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
571                                 /* The old and the new value have the same
572                                    size. Just replace. */
573                                 s->here->e_value_size =
574                                         cpu_to_le32(i->value_len);
575                                 memset(val + size - EXT3_XATTR_PAD, 0,
576                                        EXT3_XATTR_PAD); /* Clear pad bytes. */
577                                 memcpy(val, i->value, i->value_len);
578                                 return 0;
579                         }
580
581                         /* Remove the old value. */
582                         memmove(first_val + size, first_val, val - first_val);
583                         memset(first_val, 0, size);
584                         s->here->e_value_size = 0;
585                         s->here->e_value_offs = 0;
586                         min_offs += size;
587
588                         /* Adjust all value offsets. */
589                         last = s->first;
590                         while (!IS_LAST_ENTRY(last)) {
591                                 size_t o = le16_to_cpu(last->e_value_offs);
592                                 if (!last->e_value_block &&
593                                     last->e_value_size && o < offs)
594                                         last->e_value_offs =
595                                                 cpu_to_le16(o + size);
596                                 last = EXT3_XATTR_NEXT(last);
597                         }
598                 }
599                 if (!i->value) {
600                         /* Remove the old name. */
601                         size_t size = EXT3_XATTR_LEN(name_len);
602                         last = ENTRY((void *)last - size);
603                         memmove(s->here, (void *)s->here + size,
604                                 (void *)last - (void *)s->here + sizeof(__u32));
605                         memset(last, 0, size);
606                 }
607         }
608
609         if (i->value) {
610                 /* Insert the new value. */
611                 s->here->e_value_size = cpu_to_le32(i->value_len);
612                 if (i->value_len) {
613                         size_t size = EXT3_XATTR_SIZE(i->value_len);
614                         void *val = s->base + min_offs - size;
615                         s->here->e_value_offs = cpu_to_le16(min_offs - size);
616                         memset(val + size - EXT3_XATTR_PAD, 0,
617                                EXT3_XATTR_PAD); /* Clear the pad bytes. */
618                         memcpy(val, i->value, i->value_len);
619                 }
620         }
621         return 0;
622 }
623
624 struct ext3_xattr_block_find {
625         struct ext3_xattr_search s;
626         struct buffer_head *bh;
627 };
628
629 static int
630 ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
631                       struct ext3_xattr_block_find *bs)
632 {
633         struct super_block *sb = inode->i_sb;
634         int error;
635
636         ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
637                   i->name_index, i->name, i->value, (long)i->value_len);
638
639         if (EXT3_I(inode)->i_file_acl) {
640                 /* The inode already has an extended attribute block. */
641                 bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
642                 error = -EIO;
643                 if (!bs->bh)
644                         goto cleanup;
645                 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
646                         atomic_read(&(bs->bh->b_count)),
647                         le32_to_cpu(BHDR(bs->bh)->h_refcount));
648                 if (ext3_xattr_check_block(bs->bh)) {
649                         ext3_error(sb, __FUNCTION__,
650                                 "inode %ld: bad block %d", inode->i_ino,
651                                 EXT3_I(inode)->i_file_acl);
652                         error = -EIO;
653                         goto cleanup;
654                 }
655                 /* Find the named attribute. */
656                 bs->s.base = BHDR(bs->bh);
657                 bs->s.first = BFIRST(bs->bh);
658                 bs->s.end = bs->bh->b_data + bs->bh->b_size;
659                 bs->s.here = bs->s.first;
660                 error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
661                                               i->name, bs->bh->b_size, 1);
662                 if (error && error != -ENODATA)
663                         goto cleanup;
664                 bs->s.not_found = error;
665         }
666         error = 0;
667
668 cleanup:
669         return error;
670 }
671
672 static int
673 ext3_xattr_block_set(handle_t *handle, struct inode *inode,
674                      struct ext3_xattr_info *i,
675                      struct ext3_xattr_block_find *bs)
676 {
677         struct super_block *sb = inode->i_sb;
678         struct buffer_head *new_bh = NULL;
679         struct ext3_xattr_search *s = &bs->s;
680         struct mb_cache_entry *ce = NULL;
681         int error;
682
683 #define header(x) ((struct ext3_xattr_header *)(x))
684
685         if (i->value && i->value_len > sb->s_blocksize)
686                 return -ENOSPC;
687         if (s->base) {
688                 ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
689                                         bs->bh->b_blocknr);
690                 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
691                         if (ce) {
692                                 mb_cache_entry_free(ce);
693                                 ce = NULL;
694                         }
695                         ea_bdebug(bs->bh, "modifying in-place");
696                         error = ext3_journal_get_write_access(handle, bs->bh);
697                         if (error)
698                                 goto cleanup;
699                         lock_buffer(bs->bh);
700                         error = ext3_xattr_set_entry(i, s);
701                         if (!error) {
702                                 if (!IS_LAST_ENTRY(s->first))
703                                         ext3_xattr_rehash(header(s->base),
704                                                           s->here);
705                                 ext3_xattr_cache_insert(bs->bh);
706                         }
707                         unlock_buffer(bs->bh);
708                         if (error == -EIO)
709                                 goto bad_block;
710                         if (!error)
711                                 error = ext3_journal_dirty_metadata(handle,
712                                                                     bs->bh);
713                         if (error)
714                                 goto cleanup;
715                         goto inserted;
716                 } else {
717                         int offset = (char *)s->here - bs->bh->b_data;
718
719                         if (ce) {
720                                 mb_cache_entry_release(ce);
721                                 ce = NULL;
722                         }
723                         ea_bdebug(bs->bh, "cloning");
724                         s->base = kmalloc(bs->bh->b_size, GFP_KERNEL);
725                         error = -ENOMEM;
726                         if (s->base == NULL)
727                                 goto cleanup;
728                         memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
729                         s->first = ENTRY(header(s->base)+1);
730                         header(s->base)->h_refcount = cpu_to_le32(1);
731                         s->here = ENTRY(s->base + offset);
732                         s->end = s->base + bs->bh->b_size;
733                 }
734         } else {
735                 /* Allocate a buffer where we construct the new block. */
736                 s->base = kmalloc(sb->s_blocksize, GFP_KERNEL);
737                 /* assert(header == s->base) */
738                 error = -ENOMEM;
739                 if (s->base == NULL)
740                         goto cleanup;
741                 memset(s->base, 0, sb->s_blocksize);
742                 header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
743                 header(s->base)->h_blocks = cpu_to_le32(1);
744                 header(s->base)->h_refcount = cpu_to_le32(1);
745                 s->first = ENTRY(header(s->base)+1);
746                 s->here = ENTRY(header(s->base)+1);
747                 s->end = s->base + sb->s_blocksize;
748         }
749
750         error = ext3_xattr_set_entry(i, s);
751         if (error == -EIO)
752                 goto bad_block;
753         if (error)
754                 goto cleanup;
755         if (!IS_LAST_ENTRY(s->first))
756                 ext3_xattr_rehash(header(s->base), s->here);
757
758 inserted:
759         if (!IS_LAST_ENTRY(s->first)) {
760                 new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
761                 if (new_bh) {
762                         /* We found an identical block in the cache. */
763                         if (new_bh == bs->bh)
764                                 ea_bdebug(new_bh, "keeping");
765                         else {
766                                 /* The old block is released after updating
767                                    the inode. */
768                                 error = -EDQUOT;
769                                 if (DQUOT_ALLOC_BLOCK(inode, 1))
770                                         goto cleanup;
771                                 error = ext3_journal_get_write_access(handle,
772                                                                       new_bh);
773                                 if (error)
774                                         goto cleanup_dquot;
775                                 lock_buffer(new_bh);
776                                 BHDR(new_bh)->h_refcount = cpu_to_le32(1 +
777                                         le32_to_cpu(BHDR(new_bh)->h_refcount));
778                                 ea_bdebug(new_bh, "reusing; refcount now=%d",
779                                         le32_to_cpu(BHDR(new_bh)->h_refcount));
780                                 unlock_buffer(new_bh);
781                                 error = ext3_journal_dirty_metadata(handle,
782                                                                     new_bh);
783                                 if (error)
784                                         goto cleanup_dquot;
785                         }
786                         mb_cache_entry_release(ce);
787                         ce = NULL;
788                 } else if (bs->bh && s->base == bs->bh->b_data) {
789                         /* We were modifying this block in-place. */
790                         ea_bdebug(bs->bh, "keeping this block");
791                         new_bh = bs->bh;
792                         get_bh(new_bh);
793                 } else {
794                         /* We need to allocate a new block */
795                         int goal = le32_to_cpu(
796                                         EXT3_SB(sb)->s_es->s_first_data_block) +
797                                 EXT3_I(inode)->i_block_group *
798                                 EXT3_BLOCKS_PER_GROUP(sb);
799                         int block = ext3_new_block(handle, inode, goal, &error);
800                         if (error)
801                                 goto cleanup;
802                         ea_idebug(inode, "creating block %d", block);
803
804                         new_bh = sb_getblk(sb, block);
805                         if (!new_bh) {
806 getblk_failed:
807                                 ext3_free_blocks(handle, inode, block, 1);
808                                 error = -EIO;
809                                 goto cleanup;
810                         }
811                         lock_buffer(new_bh);
812                         error = ext3_journal_get_create_access(handle, new_bh);
813                         if (error) {
814                                 unlock_buffer(new_bh);
815                                 goto getblk_failed;
816                         }
817                         memcpy(new_bh->b_data, s->base, new_bh->b_size);
818                         set_buffer_uptodate(new_bh);
819                         unlock_buffer(new_bh);
820                         ext3_xattr_cache_insert(new_bh);
821                         error = ext3_journal_dirty_metadata(handle, new_bh);
822                         if (error)
823                                 goto cleanup;
824                 }
825         }
826
827         /* Update the inode. */
828         EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
829
830         /* Drop the previous xattr block. */
831         if (bs->bh && bs->bh != new_bh)
832                 ext3_xattr_release_block(handle, inode, bs->bh);
833         error = 0;
834
835 cleanup:
836         if (ce)
837                 mb_cache_entry_release(ce);
838         brelse(new_bh);
839         if (!(bs->bh && s->base == bs->bh->b_data))
840                 kfree(s->base);
841
842         return error;
843
844 cleanup_dquot:
845         DQUOT_FREE_BLOCK(inode, 1);
846         goto cleanup;
847
848 bad_block:
849         ext3_error(inode->i_sb, __FUNCTION__,
850                    "inode %ld: bad block %d", inode->i_ino,
851                    EXT3_I(inode)->i_file_acl);
852         goto cleanup;
853
854 #undef header
855 }
856
857 struct ext3_xattr_ibody_find {
858         struct ext3_xattr_search s;
859         struct ext3_iloc iloc;
860 };
861
862 static int
863 ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
864                       struct ext3_xattr_ibody_find *is)
865 {
866         struct ext3_xattr_ibody_header *header;
867         struct ext3_inode *raw_inode;
868         int error;
869
870         if (EXT3_I(inode)->i_extra_isize == 0)
871                 return 0;
872         raw_inode = ext3_raw_inode(&is->iloc);
873         header = IHDR(inode, raw_inode);
874         is->s.base = is->s.first = IFIRST(header);
875         is->s.here = is->s.first;
876         is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
877         if (EXT3_I(inode)->i_state & EXT3_STATE_XATTR) {
878                 error = ext3_xattr_check_names(IFIRST(header), is->s.end);
879                 if (error)
880                         return error;
881                 /* Find the named attribute. */
882                 error = ext3_xattr_find_entry(&is->s.here, i->name_index,
883                                               i->name, is->s.end -
884                                               (void *)is->s.base, 0);
885                 if (error && error != -ENODATA)
886                         return error;
887                 is->s.not_found = error;
888         }
889         return 0;
890 }
891
892 static int
893 ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
894                      struct ext3_xattr_info *i,
895                      struct ext3_xattr_ibody_find *is)
896 {
897         struct ext3_xattr_ibody_header *header;
898         struct ext3_xattr_search *s = &is->s;
899         int error;
900
901         if (EXT3_I(inode)->i_extra_isize == 0)
902                 return -ENOSPC;
903         error = ext3_xattr_set_entry(i, s);
904         if (error)
905                 return error;
906         header = IHDR(inode, ext3_raw_inode(&is->iloc));
907         if (!IS_LAST_ENTRY(s->first)) {
908                 header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
909                 EXT3_I(inode)->i_state |= EXT3_STATE_XATTR;
910         } else {
911                 header->h_magic = cpu_to_le32(0);
912                 EXT3_I(inode)->i_state &= ~EXT3_STATE_XATTR;
913         }
914         return 0;
915 }
916
917 /*
918  * ext3_xattr_set_handle()
919  *
920  * Create, replace or remove an extended attribute for this inode. Buffer
921  * is NULL to remove an existing extended attribute, and non-NULL to
922  * either replace an existing extended attribute, or create a new extended
923  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
924  * specify that an extended attribute must exist and must not exist
925  * previous to the call, respectively.
926  *
927  * Returns 0, or a negative error number on failure.
928  */
929 int
930 ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
931                       const char *name, const void *value, size_t value_len,
932                       int flags)
933 {
934         struct ext3_xattr_info i = {
935                 .name_index = name_index,
936                 .name = name,
937                 .value = value,
938                 .value_len = value_len,
939
940         };
941         struct ext3_xattr_ibody_find is = {
942                 .s = { .not_found = -ENODATA, },
943         };
944         struct ext3_xattr_block_find bs = {
945                 .s = { .not_found = -ENODATA, },
946         };
947         int error;
948
949         if (!name)
950                 return -EINVAL;
951         if (strlen(name) > 255)
952                 return -ERANGE;
953         down_write(&EXT3_I(inode)->xattr_sem);
954         error = ext3_get_inode_loc(inode, &is.iloc);
955         if (error)
956                 goto cleanup;
957
958         if (EXT3_I(inode)->i_state & EXT3_STATE_NEW) {
959                 struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
960                 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
961                 EXT3_I(inode)->i_state &= ~EXT3_STATE_NEW;
962         }
963
964         error = ext3_xattr_ibody_find(inode, &i, &is);
965         if (error)
966                 goto cleanup;
967         if (is.s.not_found)
968                 error = ext3_xattr_block_find(inode, &i, &bs);
969         if (error)
970                 goto cleanup;
971         if (is.s.not_found && bs.s.not_found) {
972                 error = -ENODATA;
973                 if (flags & XATTR_REPLACE)
974                         goto cleanup;
975                 error = 0;
976                 if (!value)
977                         goto cleanup;
978         } else {
979                 error = -EEXIST;
980                 if (flags & XATTR_CREATE)
981                         goto cleanup;
982         }
983         error = ext3_journal_get_write_access(handle, is.iloc.bh);
984         if (error)
985                 goto cleanup;
986         if (!value) {
987                 if (!is.s.not_found)
988                         error = ext3_xattr_ibody_set(handle, inode, &i, &is);
989                 else if (!bs.s.not_found)
990                         error = ext3_xattr_block_set(handle, inode, &i, &bs);
991         } else {
992                 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
993                 if (!error && !bs.s.not_found) {
994                         i.value = NULL;
995                         error = ext3_xattr_block_set(handle, inode, &i, &bs);
996                 } else if (error == -ENOSPC) {
997                         error = ext3_xattr_block_set(handle, inode, &i, &bs);
998                         if (error)
999                                 goto cleanup;
1000                         if (!is.s.not_found) {
1001                                 i.value = NULL;
1002                                 error = ext3_xattr_ibody_set(handle, inode, &i,
1003                                                              &is);
1004                         }
1005                 }
1006         }
1007         if (!error) {
1008                 ext3_xattr_update_super_block(handle, inode->i_sb);
1009                 inode->i_ctime = CURRENT_TIME_SEC;
1010                 error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
1011                 /*
1012                  * The bh is consumed by ext3_mark_iloc_dirty, even with
1013                  * error != 0.
1014                  */
1015                 is.iloc.bh = NULL;
1016                 if (IS_SYNC(inode))
1017                         handle->h_sync = 1;
1018         }
1019
1020 cleanup:
1021         brelse(is.iloc.bh);
1022         brelse(bs.bh);
1023         up_write(&EXT3_I(inode)->xattr_sem);
1024         return error;
1025 }
1026
1027 /*
1028  * ext3_xattr_set()
1029  *
1030  * Like ext3_xattr_set_handle, but start from an inode. This extended
1031  * attribute modification is a filesystem transaction by itself.
1032  *
1033  * Returns 0, or a negative error number on failure.
1034  */
1035 int
1036 ext3_xattr_set(struct inode *inode, int name_index, const char *name,
1037                const void *value, size_t value_len, int flags)
1038 {
1039         handle_t *handle;
1040         int error, retries = 0;
1041
1042 retry:
1043         handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
1044         if (IS_ERR(handle)) {
1045                 error = PTR_ERR(handle);
1046         } else {
1047                 int error2;
1048
1049                 error = ext3_xattr_set_handle(handle, inode, name_index, name,
1050                                               value, value_len, flags);
1051                 error2 = ext3_journal_stop(handle);
1052                 if (error == -ENOSPC &&
1053                     ext3_should_retry_alloc(inode->i_sb, &retries))
1054                         goto retry;
1055                 if (error == 0)
1056                         error = error2;
1057         }
1058
1059         return error;
1060 }
1061
1062 /*
1063  * ext3_xattr_delete_inode()
1064  *
1065  * Free extended attribute resources associated with this inode. This
1066  * is called immediately before an inode is freed. We have exclusive
1067  * access to the inode.
1068  */
1069 void
1070 ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
1071 {
1072         struct buffer_head *bh = NULL;
1073
1074         if (!EXT3_I(inode)->i_file_acl)
1075                 goto cleanup;
1076         bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
1077         if (!bh) {
1078                 ext3_error(inode->i_sb, __FUNCTION__,
1079                         "inode %ld: block %d read error", inode->i_ino,
1080                         EXT3_I(inode)->i_file_acl);
1081                 goto cleanup;
1082         }
1083         if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
1084             BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1085                 ext3_error(inode->i_sb, __FUNCTION__,
1086                         "inode %ld: bad block %d", inode->i_ino,
1087                         EXT3_I(inode)->i_file_acl);
1088                 goto cleanup;
1089         }
1090         ext3_xattr_release_block(handle, inode, bh);
1091         EXT3_I(inode)->i_file_acl = 0;
1092
1093 cleanup:
1094         brelse(bh);
1095 }
1096
1097 /*
1098  * ext3_xattr_put_super()
1099  *
1100  * This is called when a file system is unmounted.
1101  */
1102 void
1103 ext3_xattr_put_super(struct super_block *sb)
1104 {
1105         mb_cache_shrink(sb->s_bdev);
1106 }
1107
1108 /*
1109  * ext3_xattr_cache_insert()
1110  *
1111  * Create a new entry in the extended attribute cache, and insert
1112  * it unless such an entry is already in the cache.
1113  *
1114  * Returns 0, or a negative error number on failure.
1115  */
1116 static void
1117 ext3_xattr_cache_insert(struct buffer_head *bh)
1118 {
1119         __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1120         struct mb_cache_entry *ce;
1121         int error;
1122
1123         ce = mb_cache_entry_alloc(ext3_xattr_cache);
1124         if (!ce) {
1125                 ea_bdebug(bh, "out of memory");
1126                 return;
1127         }
1128         error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
1129         if (error) {
1130                 mb_cache_entry_free(ce);
1131                 if (error == -EBUSY) {
1132                         ea_bdebug(bh, "already in cache");
1133                         error = 0;
1134                 }
1135         } else {
1136                 ea_bdebug(bh, "inserting [%x]", (int)hash);
1137                 mb_cache_entry_release(ce);
1138         }
1139 }
1140
1141 /*
1142  * ext3_xattr_cmp()
1143  *
1144  * Compare two extended attribute blocks for equality.
1145  *
1146  * Returns 0 if the blocks are equal, 1 if they differ, and
1147  * a negative error number on errors.
1148  */
1149 static int
1150 ext3_xattr_cmp(struct ext3_xattr_header *header1,
1151                struct ext3_xattr_header *header2)
1152 {
1153         struct ext3_xattr_entry *entry1, *entry2;
1154
1155         entry1 = ENTRY(header1+1);
1156         entry2 = ENTRY(header2+1);
1157         while (!IS_LAST_ENTRY(entry1)) {
1158                 if (IS_LAST_ENTRY(entry2))
1159                         return 1;
1160                 if (entry1->e_hash != entry2->e_hash ||
1161                     entry1->e_name_index != entry2->e_name_index ||
1162                     entry1->e_name_len != entry2->e_name_len ||
1163                     entry1->e_value_size != entry2->e_value_size ||
1164                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1165                         return 1;
1166                 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1167                         return -EIO;
1168                 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1169                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1170                            le32_to_cpu(entry1->e_value_size)))
1171                         return 1;
1172
1173                 entry1 = EXT3_XATTR_NEXT(entry1);
1174                 entry2 = EXT3_XATTR_NEXT(entry2);
1175         }
1176         if (!IS_LAST_ENTRY(entry2))
1177                 return 1;
1178         return 0;
1179 }
1180
1181 /*
1182  * ext3_xattr_cache_find()
1183  *
1184  * Find an identical extended attribute block.
1185  *
1186  * Returns a pointer to the block found, or NULL if such a block was
1187  * not found or an error occurred.
1188  */
1189 static struct buffer_head *
1190 ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
1191                       struct mb_cache_entry **pce)
1192 {
1193         __u32 hash = le32_to_cpu(header->h_hash);
1194         struct mb_cache_entry *ce;
1195
1196         if (!header->h_hash)
1197                 return NULL;  /* never share */
1198         ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1199 again:
1200         ce = mb_cache_entry_find_first(ext3_xattr_cache, 0,
1201                                        inode->i_sb->s_bdev, hash);
1202         while (ce) {
1203                 struct buffer_head *bh;
1204
1205                 if (IS_ERR(ce)) {
1206                         if (PTR_ERR(ce) == -EAGAIN)
1207                                 goto again;
1208                         break;
1209                 }
1210                 bh = sb_bread(inode->i_sb, ce->e_block);
1211                 if (!bh) {
1212                         ext3_error(inode->i_sb, __FUNCTION__,
1213                                 "inode %ld: block %ld read error",
1214                                 inode->i_ino, (unsigned long) ce->e_block);
1215                 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1216                                 EXT3_XATTR_REFCOUNT_MAX) {
1217                         ea_idebug(inode, "block %ld refcount %d>=%d",
1218                                   (unsigned long) ce->e_block,
1219                                   le32_to_cpu(BHDR(bh)->h_refcount),
1220                                           EXT3_XATTR_REFCOUNT_MAX);
1221                 } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
1222                         *pce = ce;
1223                         return bh;
1224                 }
1225                 brelse(bh);
1226                 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1227         }
1228         return NULL;
1229 }
1230
1231 #define NAME_HASH_SHIFT 5
1232 #define VALUE_HASH_SHIFT 16
1233
1234 /*
1235  * ext3_xattr_hash_entry()
1236  *
1237  * Compute the hash of an extended attribute.
1238  */
1239 static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1240                                          struct ext3_xattr_entry *entry)
1241 {
1242         __u32 hash = 0;
1243         char *name = entry->e_name;
1244         int n;
1245
1246         for (n=0; n < entry->e_name_len; n++) {
1247                 hash = (hash << NAME_HASH_SHIFT) ^
1248                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1249                        *name++;
1250         }
1251
1252         if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1253                 __le32 *value = (__le32 *)((char *)header +
1254                         le16_to_cpu(entry->e_value_offs));
1255                 for (n = (le32_to_cpu(entry->e_value_size) +
1256                      EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1257                         hash = (hash << VALUE_HASH_SHIFT) ^
1258                                (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1259                                le32_to_cpu(*value++);
1260                 }
1261         }
1262         entry->e_hash = cpu_to_le32(hash);
1263 }
1264
1265 #undef NAME_HASH_SHIFT
1266 #undef VALUE_HASH_SHIFT
1267
1268 #define BLOCK_HASH_SHIFT 16
1269
1270 /*
1271  * ext3_xattr_rehash()
1272  *
1273  * Re-compute the extended attribute hash value after an entry has changed.
1274  */
1275 static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1276                               struct ext3_xattr_entry *entry)
1277 {
1278         struct ext3_xattr_entry *here;
1279         __u32 hash = 0;
1280
1281         ext3_xattr_hash_entry(header, entry);
1282         here = ENTRY(header+1);
1283         while (!IS_LAST_ENTRY(here)) {
1284                 if (!here->e_hash) {
1285                         /* Block is not shared if an entry's hash value == 0 */
1286                         hash = 0;
1287                         break;
1288                 }
1289                 hash = (hash << BLOCK_HASH_SHIFT) ^
1290                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1291                        le32_to_cpu(here->e_hash);
1292                 here = EXT3_XATTR_NEXT(here);
1293         }
1294         header->h_hash = cpu_to_le32(hash);
1295 }
1296
1297 #undef BLOCK_HASH_SHIFT
1298
1299 int __init
1300 init_ext3_xattr(void)
1301 {
1302         ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
1303                 sizeof(struct mb_cache_entry) +
1304                 sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
1305         if (!ext3_xattr_cache)
1306                 return -ENOMEM;
1307         return 0;
1308 }
1309
1310 void
1311 exit_ext3_xattr(void)
1312 {
1313         if (ext3_xattr_cache)
1314                 mb_cache_destroy(ext3_xattr_cache);
1315         ext3_xattr_cache = NULL;
1316 }