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