This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / ext4 / xattr.c
1 /*
2  * linux/fs/ext4/xattr.c
3  *
4  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5  *
6  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7  * Ext4 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  * EXT4_I(inode)->i_file_acl is protected by EXT4_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/ext4_jbd2.h>
57 #include <linux/ext4_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 ext4_xattr_header *)((bh)->b_data))
66 #define ENTRY(ptr) ((struct ext4_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 ext4_xattr_ibody_header *) \
72                 ((void *)raw_inode + \
73                  EXT4_GOOD_OLD_INODE_SIZE + \
74                  EXT4_I(inode)->i_extra_isize))
75 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
76
77 #ifdef EXT4_XATTR_DEBUG
78 # define ea_idebug(inode, f...) do { \
79                 printk(KERN_DEBUG "inode %s:%lu: ", \
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 ext4_xattr_cache_insert(struct buffer_head *);
98 static struct buffer_head *ext4_xattr_cache_find(struct inode *,
99                                                  struct ext4_xattr_header *,
100                                                  struct mb_cache_entry **);
101 static void ext4_xattr_rehash(struct ext4_xattr_header *,
102                               struct ext4_xattr_entry *);
103
104 static struct mb_cache *ext4_xattr_cache;
105
106 static struct xattr_handler *ext4_xattr_handler_map[] = {
107         [EXT4_XATTR_INDEX_USER]              = &ext4_xattr_user_handler,
108 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
109         [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS]  = &ext4_xattr_acl_access_handler,
110         [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
111 #endif
112         [EXT4_XATTR_INDEX_TRUSTED]           = &ext4_xattr_trusted_handler,
113 #ifdef CONFIG_EXT4DEV_FS_SECURITY
114         [EXT4_XATTR_INDEX_SECURITY]          = &ext4_xattr_security_handler,
115 #endif
116 };
117
118 struct xattr_handler *ext4_xattr_handlers[] = {
119         &ext4_xattr_user_handler,
120         &ext4_xattr_trusted_handler,
121 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
122         &ext4_xattr_acl_access_handler,
123         &ext4_xattr_acl_default_handler,
124 #endif
125 #ifdef CONFIG_EXT4DEV_FS_SECURITY
126         &ext4_xattr_security_handler,
127 #endif
128         NULL
129 };
130
131 static inline struct xattr_handler *
132 ext4_xattr_handler(int name_index)
133 {
134         struct xattr_handler *handler = NULL;
135
136         if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
137                 handler = ext4_xattr_handler_map[name_index];
138         return handler;
139 }
140
141 /*
142  * Inode operation listxattr()
143  *
144  * dentry->d_inode->i_mutex: don't care
145  */
146 ssize_t
147 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
148 {
149         return ext4_xattr_list(dentry->d_inode, buffer, size);
150 }
151
152 static int
153 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
154 {
155         while (!IS_LAST_ENTRY(entry)) {
156                 struct ext4_xattr_entry *next = EXT4_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 ext4_xattr_check_block(struct buffer_head *bh)
166 {
167         int error;
168
169         if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
170             BHDR(bh)->h_blocks != cpu_to_le32(1))
171                 return -EIO;
172         error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
173         return error;
174 }
175
176 static inline int
177 ext4_xattr_check_entry(struct ext4_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 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
189                       const char *name, size_t size, int sorted)
190 {
191         struct ext4_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 = EXT4_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 && ext4_xattr_check_entry(entry, size))
210                         return -EIO;
211         return cmp ? -ENODATA : 0;
212 }
213
214 static int
215 ext4_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 ext4_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 (!EXT4_I(inode)->i_file_acl)
228                 goto cleanup;
229         ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
230         bh = sb_bread(inode->i_sb, EXT4_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 (ext4_xattr_check_block(bh)) {
236 bad_block:      ext4_error(inode->i_sb, __FUNCTION__,
237                            "inode %lu: bad block %llu", inode->i_ino,
238                            EXT4_I(inode)->i_file_acl);
239                 error = -EIO;
240                 goto cleanup;
241         }
242         ext4_xattr_cache_insert(bh);
243         entry = BFIRST(bh);
244         error = ext4_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 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
266                      void *buffer, size_t buffer_size)
267 {
268         struct ext4_xattr_ibody_header *header;
269         struct ext4_xattr_entry *entry;
270         struct ext4_inode *raw_inode;
271         struct ext4_iloc iloc;
272         size_t size;
273         void *end;
274         int error;
275
276         if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR))
277                 return -ENODATA;
278         error = ext4_get_inode_loc(inode, &iloc);
279         if (error)
280                 return error;
281         raw_inode = ext4_raw_inode(&iloc);
282         header = IHDR(inode, raw_inode);
283         entry = IFIRST(header);
284         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
285         error = ext4_xattr_check_names(entry, end);
286         if (error)
287                 goto cleanup;
288         error = ext4_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  * ext4_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 ext4_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(&EXT4_I(inode)->xattr_sem);
324         error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
325                                      buffer_size);
326         if (error == -ENODATA)
327                 error = ext4_xattr_block_get(inode, name_index, name, buffer,
328                                              buffer_size);
329         up_read(&EXT4_I(inode)->xattr_sem);
330         return error;
331 }
332
333 static int
334 ext4_xattr_list_entries(struct inode *inode, struct ext4_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 = EXT4_XATTR_NEXT(entry)) {
340                 struct xattr_handler *handler =
341                         ext4_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 static int
359 ext4_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 (!EXT4_I(inode)->i_file_acl)
369                 goto cleanup;
370         ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
371         bh = sb_bread(inode->i_sb, EXT4_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 (ext4_xattr_check_block(bh)) {
378                 ext4_error(inode->i_sb, __FUNCTION__,
379                            "inode %lu: bad block %llu", inode->i_ino,
380                            EXT4_I(inode)->i_file_acl);
381                 error = -EIO;
382                 goto cleanup;
383         }
384         ext4_xattr_cache_insert(bh);
385         error = ext4_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 ext4_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
395 {
396         struct ext4_xattr_ibody_header *header;
397         struct ext4_inode *raw_inode;
398         struct ext4_iloc iloc;
399         void *end;
400         int error;
401
402         if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR))
403                 return 0;
404         error = ext4_get_inode_loc(inode, &iloc);
405         if (error)
406                 return error;
407         raw_inode = ext4_raw_inode(&iloc);
408         header = IHDR(inode, raw_inode);
409         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
410         error = ext4_xattr_check_names(IFIRST(header), end);
411         if (error)
412                 goto cleanup;
413         error = ext4_xattr_list_entries(inode, IFIRST(header),
414                                         buffer, buffer_size);
415
416 cleanup:
417         brelse(iloc.bh);
418         return error;
419 }
420
421 /*
422  * ext4_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 ext4_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
433 {
434         int i_error, b_error;
435
436         down_read(&EXT4_I(inode)->xattr_sem);
437         i_error = ext4_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 = ext4_xattr_block_list(inode, buffer, buffer_size);
446                 if (b_error < 0)
447                         i_error = 0;
448         }
449         up_read(&EXT4_I(inode)->xattr_sem);
450         return i_error + b_error;
451 }
452
453 /*
454  * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
455  * not set, set it.
456  */
457 static void ext4_xattr_update_super_block(handle_t *handle,
458                                           struct super_block *sb)
459 {
460         if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
461                 return;
462
463         if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
464                 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
465                 sb->s_dirt = 1;
466                 ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
467         }
468 }
469
470 /*
471  * Release the xattr block BH: If the reference count is > 1, decrement
472  * it; otherwise free the block.
473  */
474 static void
475 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
476                          struct buffer_head *bh)
477 {
478         struct mb_cache_entry *ce = NULL;
479
480         ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
481         if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
482                 ea_bdebug(bh, "refcount now=0; freeing");
483                 if (ce)
484                         mb_cache_entry_free(ce);
485                 ext4_free_blocks(handle, inode, bh->b_blocknr, 1);
486                 get_bh(bh);
487                 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
488         } else {
489                 if (ext4_journal_get_write_access(handle, bh) == 0) {
490                         lock_buffer(bh);
491                         BHDR(bh)->h_refcount = cpu_to_le32(
492                                 le32_to_cpu(BHDR(bh)->h_refcount) - 1);
493                         ext4_journal_dirty_metadata(handle, bh);
494                         if (IS_SYNC(inode))
495                                 handle->h_sync = 1;
496                         DLIMIT_FREE_BLOCK(inode, 1);
497                         DQUOT_FREE_BLOCK(inode, 1);
498                         unlock_buffer(bh);
499                         ea_bdebug(bh, "refcount now=%d; releasing",
500                                   le32_to_cpu(BHDR(bh)->h_refcount));
501                 }
502                 if (ce)
503                         mb_cache_entry_release(ce);
504         }
505 }
506
507 struct ext4_xattr_info {
508         int name_index;
509         const char *name;
510         const void *value;
511         size_t value_len;
512 };
513
514 struct ext4_xattr_search {
515         struct ext4_xattr_entry *first;
516         void *base;
517         void *end;
518         struct ext4_xattr_entry *here;
519         int not_found;
520 };
521
522 static int
523 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
524 {
525         struct ext4_xattr_entry *last;
526         size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
527
528         /* Compute min_offs and last. */
529         last = s->first;
530         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
531                 if (!last->e_value_block && last->e_value_size) {
532                         size_t offs = le16_to_cpu(last->e_value_offs);
533                         if (offs < min_offs)
534                                 min_offs = offs;
535                 }
536         }
537         free = min_offs - ((void *)last - s->base) - sizeof(__u32);
538         if (!s->not_found) {
539                 if (!s->here->e_value_block && s->here->e_value_size) {
540                         size_t size = le32_to_cpu(s->here->e_value_size);
541                         free += EXT4_XATTR_SIZE(size);
542                 }
543                 free += EXT4_XATTR_LEN(name_len);
544         }
545         if (i->value) {
546                 if (free < EXT4_XATTR_SIZE(i->value_len) ||
547                     free < EXT4_XATTR_LEN(name_len) +
548                            EXT4_XATTR_SIZE(i->value_len))
549                         return -ENOSPC;
550         }
551
552         if (i->value && s->not_found) {
553                 /* Insert the new name. */
554                 size_t size = EXT4_XATTR_LEN(name_len);
555                 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
556                 memmove((void *)s->here + size, s->here, rest);
557                 memset(s->here, 0, size);
558                 s->here->e_name_index = i->name_index;
559                 s->here->e_name_len = name_len;
560                 memcpy(s->here->e_name, i->name, name_len);
561         } else {
562                 if (!s->here->e_value_block && s->here->e_value_size) {
563                         void *first_val = s->base + min_offs;
564                         size_t offs = le16_to_cpu(s->here->e_value_offs);
565                         void *val = s->base + offs;
566                         size_t size = EXT4_XATTR_SIZE(
567                                 le32_to_cpu(s->here->e_value_size));
568
569                         if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
570                                 /* The old and the new value have the same
571                                    size. Just replace. */
572                                 s->here->e_value_size =
573                                         cpu_to_le32(i->value_len);
574                                 memset(val + size - EXT4_XATTR_PAD, 0,
575                                        EXT4_XATTR_PAD); /* Clear pad bytes. */
576                                 memcpy(val, i->value, i->value_len);
577                                 return 0;
578                         }
579
580                         /* Remove the old value. */
581                         memmove(first_val + size, first_val, val - first_val);
582                         memset(first_val, 0, size);
583                         s->here->e_value_size = 0;
584                         s->here->e_value_offs = 0;
585                         min_offs += size;
586
587                         /* Adjust all value offsets. */
588                         last = s->first;
589                         while (!IS_LAST_ENTRY(last)) {
590                                 size_t o = le16_to_cpu(last->e_value_offs);
591                                 if (!last->e_value_block &&
592                                     last->e_value_size && o < offs)
593                                         last->e_value_offs =
594                                                 cpu_to_le16(o + size);
595                                 last = EXT4_XATTR_NEXT(last);
596                         }
597                 }
598                 if (!i->value) {
599                         /* Remove the old name. */
600                         size_t size = EXT4_XATTR_LEN(name_len);
601                         last = ENTRY((void *)last - size);
602                         memmove(s->here, (void *)s->here + size,
603                                 (void *)last - (void *)s->here + sizeof(__u32));
604                         memset(last, 0, size);
605                 }
606         }
607
608         if (i->value) {
609                 /* Insert the new value. */
610                 s->here->e_value_size = cpu_to_le32(i->value_len);
611                 if (i->value_len) {
612                         size_t size = EXT4_XATTR_SIZE(i->value_len);
613                         void *val = s->base + min_offs - size;
614                         s->here->e_value_offs = cpu_to_le16(min_offs - size);
615                         memset(val + size - EXT4_XATTR_PAD, 0,
616                                EXT4_XATTR_PAD); /* Clear the pad bytes. */
617                         memcpy(val, i->value, i->value_len);
618                 }
619         }
620         return 0;
621 }
622
623 struct ext4_xattr_block_find {
624         struct ext4_xattr_search s;
625         struct buffer_head *bh;
626 };
627
628 static int
629 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
630                       struct ext4_xattr_block_find *bs)
631 {
632         struct super_block *sb = inode->i_sb;
633         int error;
634
635         ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
636                   i->name_index, i->name, i->value, (long)i->value_len);
637
638         if (EXT4_I(inode)->i_file_acl) {
639                 /* The inode already has an extended attribute block. */
640                 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
641                 error = -EIO;
642                 if (!bs->bh)
643                         goto cleanup;
644                 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
645                         atomic_read(&(bs->bh->b_count)),
646                         le32_to_cpu(BHDR(bs->bh)->h_refcount));
647                 if (ext4_xattr_check_block(bs->bh)) {
648                         ext4_error(sb, __FUNCTION__,
649                                 "inode %lu: bad block %llu", inode->i_ino,
650                                 EXT4_I(inode)->i_file_acl);
651                         error = -EIO;
652                         goto cleanup;
653                 }
654                 /* Find the named attribute. */
655                 bs->s.base = BHDR(bs->bh);
656                 bs->s.first = BFIRST(bs->bh);
657                 bs->s.end = bs->bh->b_data + bs->bh->b_size;
658                 bs->s.here = bs->s.first;
659                 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
660                                               i->name, bs->bh->b_size, 1);
661                 if (error && error != -ENODATA)
662                         goto cleanup;
663                 bs->s.not_found = error;
664         }
665         error = 0;
666
667 cleanup:
668         return error;
669 }
670
671 static int
672 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
673                      struct ext4_xattr_info *i,
674                      struct ext4_xattr_block_find *bs)
675 {
676         struct super_block *sb = inode->i_sb;
677         struct buffer_head *new_bh = NULL;
678         struct ext4_xattr_search *s = &bs->s;
679         struct mb_cache_entry *ce = NULL;
680         int error;
681
682 #define header(x) ((struct ext4_xattr_header *)(x))
683
684         if (i->value && i->value_len > sb->s_blocksize)
685                 return -ENOSPC;
686         if (s->base) {
687                 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
688                                         bs->bh->b_blocknr);
689                 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
690                         if (ce) {
691                                 mb_cache_entry_free(ce);
692                                 ce = NULL;
693                         }
694                         ea_bdebug(bs->bh, "modifying in-place");
695                         error = ext4_journal_get_write_access(handle, bs->bh);
696                         if (error)
697                                 goto cleanup;
698                         lock_buffer(bs->bh);
699                         error = ext4_xattr_set_entry(i, s);
700                         if (!error) {
701                                 if (!IS_LAST_ENTRY(s->first))
702                                         ext4_xattr_rehash(header(s->base),
703                                                           s->here);
704                                 ext4_xattr_cache_insert(bs->bh);
705                         }
706                         unlock_buffer(bs->bh);
707                         if (error == -EIO)
708                                 goto bad_block;
709                         if (!error)
710                                 error = ext4_journal_dirty_metadata(handle,
711                                                                     bs->bh);
712                         if (error)
713                                 goto cleanup;
714                         goto inserted;
715                 } else {
716                         int offset = (char *)s->here - bs->bh->b_data;
717
718                         if (ce) {
719                                 mb_cache_entry_release(ce);
720                                 ce = NULL;
721                         }
722                         ea_bdebug(bs->bh, "cloning");
723                         s->base = kmalloc(bs->bh->b_size, GFP_KERNEL);
724                         error = -ENOMEM;
725                         if (s->base == NULL)
726                                 goto cleanup;
727                         memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
728                         s->first = ENTRY(header(s->base)+1);
729                         header(s->base)->h_refcount = cpu_to_le32(1);
730                         s->here = ENTRY(s->base + offset);
731                         s->end = s->base + bs->bh->b_size;
732                 }
733         } else {
734                 /* Allocate a buffer where we construct the new block. */
735                 s->base = kmalloc(sb->s_blocksize, GFP_KERNEL);
736                 /* assert(header == s->base) */
737                 error = -ENOMEM;
738                 if (s->base == NULL)
739                         goto cleanup;
740                 memset(s->base, 0, sb->s_blocksize);
741                 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
742                 header(s->base)->h_blocks = cpu_to_le32(1);
743                 header(s->base)->h_refcount = cpu_to_le32(1);
744                 s->first = ENTRY(header(s->base)+1);
745                 s->here = ENTRY(header(s->base)+1);
746                 s->end = s->base + sb->s_blocksize;
747         }
748
749         error = ext4_xattr_set_entry(i, s);
750         if (error == -EIO)
751                 goto bad_block;
752         if (error)
753                 goto cleanup;
754         if (!IS_LAST_ENTRY(s->first))
755                 ext4_xattr_rehash(header(s->base), s->here);
756
757 inserted:
758         if (!IS_LAST_ENTRY(s->first)) {
759                 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
760                 if (new_bh) {
761                         /* We found an identical block in the cache. */
762                         if (new_bh == bs->bh)
763                                 ea_bdebug(new_bh, "keeping");
764                         else {
765                                 error = -ENOSPC;
766                                 if (DLIMIT_ALLOC_BLOCK(inode, 1))
767                                         goto cleanup;
768                                 /* The old block is released after updating
769                                    the inode. */
770                                 error = -EDQUOT;
771                                 if (DQUOT_ALLOC_BLOCK(inode, 1))
772                                         goto cleanup_dlimit;
773                                 error = ext4_journal_get_write_access(handle,
774                                                                       new_bh);
775                                 if (error)
776                                         goto cleanup_dquot;
777                                 lock_buffer(new_bh);
778                                 BHDR(new_bh)->h_refcount = cpu_to_le32(1 +
779                                         le32_to_cpu(BHDR(new_bh)->h_refcount));
780                                 ea_bdebug(new_bh, "reusing; refcount now=%d",
781                                         le32_to_cpu(BHDR(new_bh)->h_refcount));
782                                 unlock_buffer(new_bh);
783                                 error = ext4_journal_dirty_metadata(handle,
784                                                                     new_bh);
785                                 if (error)
786                                         goto cleanup_dquot;
787                         }
788                         mb_cache_entry_release(ce);
789                         ce = NULL;
790                 } else if (bs->bh && s->base == bs->bh->b_data) {
791                         /* We were modifying this block in-place. */
792                         ea_bdebug(bs->bh, "keeping this block");
793                         new_bh = bs->bh;
794                         get_bh(new_bh);
795                 } else {
796                         /* We need to allocate a new block */
797                         ext4_fsblk_t goal = le32_to_cpu(
798                                         EXT4_SB(sb)->s_es->s_first_data_block) +
799                                 (ext4_fsblk_t)EXT4_I(inode)->i_block_group *
800                                 EXT4_BLOCKS_PER_GROUP(sb);
801                         ext4_fsblk_t block = ext4_new_block(handle, inode,
802                                                         goal, &error);
803                         if (error)
804                                 goto cleanup;
805                         ea_idebug(inode, "creating block %d", block);
806
807                         new_bh = sb_getblk(sb, block);
808                         if (!new_bh) {
809 getblk_failed:
810                                 ext4_free_blocks(handle, inode, block, 1);
811                                 error = -EIO;
812                                 goto cleanup;
813                         }
814                         lock_buffer(new_bh);
815                         error = ext4_journal_get_create_access(handle, new_bh);
816                         if (error) {
817                                 unlock_buffer(new_bh);
818                                 goto getblk_failed;
819                         }
820                         memcpy(new_bh->b_data, s->base, new_bh->b_size);
821                         set_buffer_uptodate(new_bh);
822                         unlock_buffer(new_bh);
823                         ext4_xattr_cache_insert(new_bh);
824                         error = ext4_journal_dirty_metadata(handle, new_bh);
825                         if (error)
826                                 goto cleanup;
827                 }
828         }
829
830         /* Update the inode. */
831         EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
832
833         /* Drop the previous xattr block. */
834         if (bs->bh && bs->bh != new_bh)
835                 ext4_xattr_release_block(handle, inode, bs->bh);
836         error = 0;
837
838 cleanup:
839         if (ce)
840                 mb_cache_entry_release(ce);
841         brelse(new_bh);
842         if (!(bs->bh && s->base == bs->bh->b_data))
843                 kfree(s->base);
844
845         return error;
846
847 cleanup_dquot:
848         DQUOT_FREE_BLOCK(inode, 1);
849 cleanup_dlimit:
850         DLIMIT_FREE_BLOCK(inode, 1);
851         goto cleanup;
852
853 bad_block:
854         ext4_error(inode->i_sb, __FUNCTION__,
855                    "inode %lu: bad block %llu", inode->i_ino,
856                    EXT4_I(inode)->i_file_acl);
857         goto cleanup;
858
859 #undef header
860 }
861
862 struct ext4_xattr_ibody_find {
863         struct ext4_xattr_search s;
864         struct ext4_iloc iloc;
865 };
866
867 static int
868 ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
869                       struct ext4_xattr_ibody_find *is)
870 {
871         struct ext4_xattr_ibody_header *header;
872         struct ext4_inode *raw_inode;
873         int error;
874
875         if (EXT4_I(inode)->i_extra_isize == 0)
876                 return 0;
877         raw_inode = ext4_raw_inode(&is->iloc);
878         header = IHDR(inode, raw_inode);
879         is->s.base = is->s.first = IFIRST(header);
880         is->s.here = is->s.first;
881         is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
882         if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
883                 error = ext4_xattr_check_names(IFIRST(header), is->s.end);
884                 if (error)
885                         return error;
886                 /* Find the named attribute. */
887                 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
888                                               i->name, is->s.end -
889                                               (void *)is->s.base, 0);
890                 if (error && error != -ENODATA)
891                         return error;
892                 is->s.not_found = error;
893         }
894         return 0;
895 }
896
897 static int
898 ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
899                      struct ext4_xattr_info *i,
900                      struct ext4_xattr_ibody_find *is)
901 {
902         struct ext4_xattr_ibody_header *header;
903         struct ext4_xattr_search *s = &is->s;
904         int error;
905
906         if (EXT4_I(inode)->i_extra_isize == 0)
907                 return -ENOSPC;
908         error = ext4_xattr_set_entry(i, s);
909         if (error)
910                 return error;
911         header = IHDR(inode, ext4_raw_inode(&is->iloc));
912         if (!IS_LAST_ENTRY(s->first)) {
913                 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
914                 EXT4_I(inode)->i_state |= EXT4_STATE_XATTR;
915         } else {
916                 header->h_magic = cpu_to_le32(0);
917                 EXT4_I(inode)->i_state &= ~EXT4_STATE_XATTR;
918         }
919         return 0;
920 }
921
922 /*
923  * ext4_xattr_set_handle()
924  *
925  * Create, replace or remove an extended attribute for this inode. Buffer
926  * is NULL to remove an existing extended attribute, and non-NULL to
927  * either replace an existing extended attribute, or create a new extended
928  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
929  * specify that an extended attribute must exist and must not exist
930  * previous to the call, respectively.
931  *
932  * Returns 0, or a negative error number on failure.
933  */
934 int
935 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
936                       const char *name, const void *value, size_t value_len,
937                       int flags)
938 {
939         struct ext4_xattr_info i = {
940                 .name_index = name_index,
941                 .name = name,
942                 .value = value,
943                 .value_len = value_len,
944
945         };
946         struct ext4_xattr_ibody_find is = {
947                 .s = { .not_found = -ENODATA, },
948         };
949         struct ext4_xattr_block_find bs = {
950                 .s = { .not_found = -ENODATA, },
951         };
952         int error;
953
954         if (!name)
955                 return -EINVAL;
956         if (strlen(name) > 255)
957                 return -ERANGE;
958         down_write(&EXT4_I(inode)->xattr_sem);
959         error = ext4_get_inode_loc(inode, &is.iloc);
960         if (error)
961                 goto cleanup;
962
963         if (EXT4_I(inode)->i_state & EXT4_STATE_NEW) {
964                 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
965                 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
966                 EXT4_I(inode)->i_state &= ~EXT4_STATE_NEW;
967         }
968
969         error = ext4_xattr_ibody_find(inode, &i, &is);
970         if (error)
971                 goto cleanup;
972         if (is.s.not_found)
973                 error = ext4_xattr_block_find(inode, &i, &bs);
974         if (error)
975                 goto cleanup;
976         if (is.s.not_found && bs.s.not_found) {
977                 error = -ENODATA;
978                 if (flags & XATTR_REPLACE)
979                         goto cleanup;
980                 error = 0;
981                 if (!value)
982                         goto cleanup;
983         } else {
984                 error = -EEXIST;
985                 if (flags & XATTR_CREATE)
986                         goto cleanup;
987         }
988         error = ext4_journal_get_write_access(handle, is.iloc.bh);
989         if (error)
990                 goto cleanup;
991         if (!value) {
992                 if (!is.s.not_found)
993                         error = ext4_xattr_ibody_set(handle, inode, &i, &is);
994                 else if (!bs.s.not_found)
995                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
996         } else {
997                 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
998                 if (!error && !bs.s.not_found) {
999                         i.value = NULL;
1000                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
1001                 } else if (error == -ENOSPC) {
1002                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
1003                         if (error)
1004                                 goto cleanup;
1005                         if (!is.s.not_found) {
1006                                 i.value = NULL;
1007                                 error = ext4_xattr_ibody_set(handle, inode, &i,
1008                                                              &is);
1009                         }
1010                 }
1011         }
1012         if (!error) {
1013                 ext4_xattr_update_super_block(handle, inode->i_sb);
1014                 inode->i_ctime = CURRENT_TIME_SEC;
1015                 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1016                 /*
1017                  * The bh is consumed by ext4_mark_iloc_dirty, even with
1018                  * error != 0.
1019                  */
1020                 is.iloc.bh = NULL;
1021                 if (IS_SYNC(inode))
1022                         handle->h_sync = 1;
1023         }
1024
1025 cleanup:
1026         brelse(is.iloc.bh);
1027         brelse(bs.bh);
1028         up_write(&EXT4_I(inode)->xattr_sem);
1029         return error;
1030 }
1031
1032 /*
1033  * ext4_xattr_set()
1034  *
1035  * Like ext4_xattr_set_handle, but start from an inode. This extended
1036  * attribute modification is a filesystem transaction by itself.
1037  *
1038  * Returns 0, or a negative error number on failure.
1039  */
1040 int
1041 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1042                const void *value, size_t value_len, int flags)
1043 {
1044         handle_t *handle;
1045         int error, retries = 0;
1046
1047 retry:
1048         handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
1049         if (IS_ERR(handle)) {
1050                 error = PTR_ERR(handle);
1051         } else {
1052                 int error2;
1053
1054                 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1055                                               value, value_len, flags);
1056                 error2 = ext4_journal_stop(handle);
1057                 if (error == -ENOSPC &&
1058                     ext4_should_retry_alloc(inode->i_sb, &retries))
1059                         goto retry;
1060                 if (error == 0)
1061                         error = error2;
1062         }
1063
1064         return error;
1065 }
1066
1067 /*
1068  * ext4_xattr_delete_inode()
1069  *
1070  * Free extended attribute resources associated with this inode. This
1071  * is called immediately before an inode is freed. We have exclusive
1072  * access to the inode.
1073  */
1074 void
1075 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1076 {
1077         struct buffer_head *bh = NULL;
1078
1079         if (!EXT4_I(inode)->i_file_acl)
1080                 goto cleanup;
1081         bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1082         if (!bh) {
1083                 ext4_error(inode->i_sb, __FUNCTION__,
1084                         "inode %lu: block %llu read error", inode->i_ino,
1085                         EXT4_I(inode)->i_file_acl);
1086                 goto cleanup;
1087         }
1088         if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1089             BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1090                 ext4_error(inode->i_sb, __FUNCTION__,
1091                         "inode %lu: bad block %llu", inode->i_ino,
1092                         EXT4_I(inode)->i_file_acl);
1093                 goto cleanup;
1094         }
1095         ext4_xattr_release_block(handle, inode, bh);
1096         EXT4_I(inode)->i_file_acl = 0;
1097
1098 cleanup:
1099         brelse(bh);
1100 }
1101
1102 /*
1103  * ext4_xattr_put_super()
1104  *
1105  * This is called when a file system is unmounted.
1106  */
1107 void
1108 ext4_xattr_put_super(struct super_block *sb)
1109 {
1110         mb_cache_shrink(sb->s_bdev);
1111 }
1112
1113 /*
1114  * ext4_xattr_cache_insert()
1115  *
1116  * Create a new entry in the extended attribute cache, and insert
1117  * it unless such an entry is already in the cache.
1118  *
1119  * Returns 0, or a negative error number on failure.
1120  */
1121 static void
1122 ext4_xattr_cache_insert(struct buffer_head *bh)
1123 {
1124         __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1125         struct mb_cache_entry *ce;
1126         int error;
1127
1128         ce = mb_cache_entry_alloc(ext4_xattr_cache);
1129         if (!ce) {
1130                 ea_bdebug(bh, "out of memory");
1131                 return;
1132         }
1133         error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
1134         if (error) {
1135                 mb_cache_entry_free(ce);
1136                 if (error == -EBUSY) {
1137                         ea_bdebug(bh, "already in cache");
1138                         error = 0;
1139                 }
1140         } else {
1141                 ea_bdebug(bh, "inserting [%x]", (int)hash);
1142                 mb_cache_entry_release(ce);
1143         }
1144 }
1145
1146 /*
1147  * ext4_xattr_cmp()
1148  *
1149  * Compare two extended attribute blocks for equality.
1150  *
1151  * Returns 0 if the blocks are equal, 1 if they differ, and
1152  * a negative error number on errors.
1153  */
1154 static int
1155 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1156                struct ext4_xattr_header *header2)
1157 {
1158         struct ext4_xattr_entry *entry1, *entry2;
1159
1160         entry1 = ENTRY(header1+1);
1161         entry2 = ENTRY(header2+1);
1162         while (!IS_LAST_ENTRY(entry1)) {
1163                 if (IS_LAST_ENTRY(entry2))
1164                         return 1;
1165                 if (entry1->e_hash != entry2->e_hash ||
1166                     entry1->e_name_index != entry2->e_name_index ||
1167                     entry1->e_name_len != entry2->e_name_len ||
1168                     entry1->e_value_size != entry2->e_value_size ||
1169                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1170                         return 1;
1171                 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1172                         return -EIO;
1173                 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1174                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1175                            le32_to_cpu(entry1->e_value_size)))
1176                         return 1;
1177
1178                 entry1 = EXT4_XATTR_NEXT(entry1);
1179                 entry2 = EXT4_XATTR_NEXT(entry2);
1180         }
1181         if (!IS_LAST_ENTRY(entry2))
1182                 return 1;
1183         return 0;
1184 }
1185
1186 /*
1187  * ext4_xattr_cache_find()
1188  *
1189  * Find an identical extended attribute block.
1190  *
1191  * Returns a pointer to the block found, or NULL if such a block was
1192  * not found or an error occurred.
1193  */
1194 static struct buffer_head *
1195 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1196                       struct mb_cache_entry **pce)
1197 {
1198         __u32 hash = le32_to_cpu(header->h_hash);
1199         struct mb_cache_entry *ce;
1200
1201         if (!header->h_hash)
1202                 return NULL;  /* never share */
1203         ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1204 again:
1205         ce = mb_cache_entry_find_first(ext4_xattr_cache, 0,
1206                                        inode->i_sb->s_bdev, hash);
1207         while (ce) {
1208                 struct buffer_head *bh;
1209
1210                 if (IS_ERR(ce)) {
1211                         if (PTR_ERR(ce) == -EAGAIN)
1212                                 goto again;
1213                         break;
1214                 }
1215                 bh = sb_bread(inode->i_sb, ce->e_block);
1216                 if (!bh) {
1217                         ext4_error(inode->i_sb, __FUNCTION__,
1218                                 "inode %lu: block %lu read error",
1219                                 inode->i_ino, (unsigned long) ce->e_block);
1220                 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1221                                 EXT4_XATTR_REFCOUNT_MAX) {
1222                         ea_idebug(inode, "block %lu refcount %d>=%d",
1223                                   (unsigned long) ce->e_block,
1224                                   le32_to_cpu(BHDR(bh)->h_refcount),
1225                                           EXT4_XATTR_REFCOUNT_MAX);
1226                 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1227                         *pce = ce;
1228                         return bh;
1229                 }
1230                 brelse(bh);
1231                 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1232         }
1233         return NULL;
1234 }
1235
1236 #define NAME_HASH_SHIFT 5
1237 #define VALUE_HASH_SHIFT 16
1238
1239 /*
1240  * ext4_xattr_hash_entry()
1241  *
1242  * Compute the hash of an extended attribute.
1243  */
1244 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1245                                          struct ext4_xattr_entry *entry)
1246 {
1247         __u32 hash = 0;
1248         char *name = entry->e_name;
1249         int n;
1250
1251         for (n=0; n < entry->e_name_len; n++) {
1252                 hash = (hash << NAME_HASH_SHIFT) ^
1253                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1254                        *name++;
1255         }
1256
1257         if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1258                 __le32 *value = (__le32 *)((char *)header +
1259                         le16_to_cpu(entry->e_value_offs));
1260                 for (n = (le32_to_cpu(entry->e_value_size) +
1261                      EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1262                         hash = (hash << VALUE_HASH_SHIFT) ^
1263                                (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1264                                le32_to_cpu(*value++);
1265                 }
1266         }
1267         entry->e_hash = cpu_to_le32(hash);
1268 }
1269
1270 #undef NAME_HASH_SHIFT
1271 #undef VALUE_HASH_SHIFT
1272
1273 #define BLOCK_HASH_SHIFT 16
1274
1275 /*
1276  * ext4_xattr_rehash()
1277  *
1278  * Re-compute the extended attribute hash value after an entry has changed.
1279  */
1280 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1281                               struct ext4_xattr_entry *entry)
1282 {
1283         struct ext4_xattr_entry *here;
1284         __u32 hash = 0;
1285
1286         ext4_xattr_hash_entry(header, entry);
1287         here = ENTRY(header+1);
1288         while (!IS_LAST_ENTRY(here)) {
1289                 if (!here->e_hash) {
1290                         /* Block is not shared if an entry's hash value == 0 */
1291                         hash = 0;
1292                         break;
1293                 }
1294                 hash = (hash << BLOCK_HASH_SHIFT) ^
1295                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1296                        le32_to_cpu(here->e_hash);
1297                 here = EXT4_XATTR_NEXT(here);
1298         }
1299         header->h_hash = cpu_to_le32(hash);
1300 }
1301
1302 #undef BLOCK_HASH_SHIFT
1303
1304 int __init
1305 init_ext4_xattr(void)
1306 {
1307         ext4_xattr_cache = mb_cache_create("ext4_xattr", NULL,
1308                 sizeof(struct mb_cache_entry) +
1309                 sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
1310         if (!ext4_xattr_cache)
1311                 return -ENOMEM;
1312         return 0;
1313 }
1314
1315 void
1316 exit_ext4_xattr(void)
1317 {
1318         if (ext4_xattr_cache)
1319                 mb_cache_destroy(ext4_xattr_cache);
1320         ext4_xattr_cache = NULL;
1321 }