ext4: add sanity checking to count_overhead()
[pandora-kernel.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/mbcache.h>
57 #include <linux/quotaops.h>
58 #include <linux/rwsem.h>
59 #include "ext4_jbd2.h"
60 #include "ext4.h"
61 #include "xattr.h"
62 #include "acl.h"
63
64 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
65 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
66 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
67 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
68
69 #ifdef EXT4_XATTR_DEBUG
70 # define ea_idebug(inode, f...) do { \
71                 printk(KERN_DEBUG "inode %s:%lu: ", \
72                         inode->i_sb->s_id, inode->i_ino); \
73                 printk(f); \
74                 printk("\n"); \
75         } while (0)
76 # define ea_bdebug(bh, f...) do { \
77                 char b[BDEVNAME_SIZE]; \
78                 printk(KERN_DEBUG "block %s:%lu: ", \
79                         bdevname(bh->b_bdev, b), \
80                         (unsigned long) bh->b_blocknr); \
81                 printk(f); \
82                 printk("\n"); \
83         } while (0)
84 #else
85 # define ea_idebug(f...)
86 # define ea_bdebug(f...)
87 #endif
88
89 static void ext4_xattr_cache_insert(struct buffer_head *);
90 static struct buffer_head *ext4_xattr_cache_find(struct inode *,
91                                                  struct ext4_xattr_header *,
92                                                  struct mb_cache_entry **);
93 static void ext4_xattr_rehash(struct ext4_xattr_header *,
94                               struct ext4_xattr_entry *);
95 static int ext4_xattr_list(struct dentry *dentry, char *buffer,
96                            size_t buffer_size);
97
98 static struct mb_cache *ext4_xattr_cache;
99
100 static const struct xattr_handler *ext4_xattr_handler_map[] = {
101         [EXT4_XATTR_INDEX_USER]              = &ext4_xattr_user_handler,
102 #ifdef CONFIG_EXT4_FS_POSIX_ACL
103         [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS]  = &ext4_xattr_acl_access_handler,
104         [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
105 #endif
106         [EXT4_XATTR_INDEX_TRUSTED]           = &ext4_xattr_trusted_handler,
107 #ifdef CONFIG_EXT4_FS_SECURITY
108         [EXT4_XATTR_INDEX_SECURITY]          = &ext4_xattr_security_handler,
109 #endif
110 };
111
112 const struct xattr_handler *ext4_xattr_handlers[] = {
113         &ext4_xattr_user_handler,
114         &ext4_xattr_trusted_handler,
115 #ifdef CONFIG_EXT4_FS_POSIX_ACL
116         &ext4_xattr_acl_access_handler,
117         &ext4_xattr_acl_default_handler,
118 #endif
119 #ifdef CONFIG_EXT4_FS_SECURITY
120         &ext4_xattr_security_handler,
121 #endif
122         NULL
123 };
124
125 static inline const struct xattr_handler *
126 ext4_xattr_handler(int name_index)
127 {
128         const struct xattr_handler *handler = NULL;
129
130         if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
131                 handler = ext4_xattr_handler_map[name_index];
132         return handler;
133 }
134
135 /*
136  * Inode operation listxattr()
137  *
138  * dentry->d_inode->i_mutex: don't care
139  */
140 ssize_t
141 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
142 {
143         return ext4_xattr_list(dentry, buffer, size);
144 }
145
146 static int
147 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
148                        void *value_start)
149 {
150         struct ext4_xattr_entry *e = entry;
151
152         while (!IS_LAST_ENTRY(e)) {
153                 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
154                 if ((void *)next >= end)
155                         return -EIO;
156                 e = next;
157         }
158
159         while (!IS_LAST_ENTRY(entry)) {
160                 if (entry->e_value_size != 0 &&
161                     (value_start + le16_to_cpu(entry->e_value_offs) <
162                      (void *)e + sizeof(__u32) ||
163                      value_start + le16_to_cpu(entry->e_value_offs) +
164                     le32_to_cpu(entry->e_value_size) > end))
165                         return -EIO;
166                 entry = EXT4_XATTR_NEXT(entry);
167         }
168
169         return 0;
170 }
171
172 static inline int
173 ext4_xattr_check_block(struct buffer_head *bh)
174 {
175         int error;
176
177         if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
178             BHDR(bh)->h_blocks != cpu_to_le32(1))
179                 return -EIO;
180         error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
181                                        bh->b_data);
182         return error;
183 }
184
185 static inline int
186 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
187 {
188         size_t value_size = le32_to_cpu(entry->e_value_size);
189
190         if (entry->e_value_block != 0 || value_size > size ||
191             le16_to_cpu(entry->e_value_offs) + value_size > size)
192                 return -EIO;
193         return 0;
194 }
195
196 static int
197 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
198                       const char *name, size_t size, int sorted)
199 {
200         struct ext4_xattr_entry *entry;
201         size_t name_len;
202         int cmp = 1;
203
204         if (name == NULL)
205                 return -EINVAL;
206         name_len = strlen(name);
207         entry = *pentry;
208         for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
209                 cmp = name_index - entry->e_name_index;
210                 if (!cmp)
211                         cmp = name_len - entry->e_name_len;
212                 if (!cmp)
213                         cmp = memcmp(name, entry->e_name, name_len);
214                 if (cmp <= 0 && (sorted || cmp == 0))
215                         break;
216         }
217         *pentry = entry;
218         if (!cmp && ext4_xattr_check_entry(entry, size))
219                         return -EIO;
220         return cmp ? -ENODATA : 0;
221 }
222
223 static int
224 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
225                      void *buffer, size_t buffer_size)
226 {
227         struct buffer_head *bh = NULL;
228         struct ext4_xattr_entry *entry;
229         size_t size;
230         int error;
231
232         ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
233                   name_index, name, buffer, (long)buffer_size);
234
235         error = -ENODATA;
236         if (!EXT4_I(inode)->i_file_acl)
237                 goto cleanup;
238         ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
239         bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
240         if (!bh)
241                 goto cleanup;
242         ea_bdebug(bh, "b_count=%d, refcount=%d",
243                 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
244         if (ext4_xattr_check_block(bh)) {
245 bad_block:
246                 EXT4_ERROR_INODE(inode, "bad block %llu",
247                                  EXT4_I(inode)->i_file_acl);
248                 error = -EIO;
249                 goto cleanup;
250         }
251         ext4_xattr_cache_insert(bh);
252         entry = BFIRST(bh);
253         error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
254         if (error == -EIO)
255                 goto bad_block;
256         if (error)
257                 goto cleanup;
258         size = le32_to_cpu(entry->e_value_size);
259         if (buffer) {
260                 error = -ERANGE;
261                 if (size > buffer_size)
262                         goto cleanup;
263                 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
264                        size);
265         }
266         error = size;
267
268 cleanup:
269         brelse(bh);
270         return error;
271 }
272
273 static int
274 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
275                      void *buffer, size_t buffer_size)
276 {
277         struct ext4_xattr_ibody_header *header;
278         struct ext4_xattr_entry *entry;
279         struct ext4_inode *raw_inode;
280         struct ext4_iloc iloc;
281         size_t size;
282         void *end;
283         int error;
284
285         if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
286                 return -ENODATA;
287         error = ext4_get_inode_loc(inode, &iloc);
288         if (error)
289                 return error;
290         raw_inode = ext4_raw_inode(&iloc);
291         header = IHDR(inode, raw_inode);
292         entry = IFIRST(header);
293         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
294         error = ext4_xattr_check_names(entry, end, entry);
295         if (error)
296                 goto cleanup;
297         error = ext4_xattr_find_entry(&entry, name_index, name,
298                                       end - (void *)entry, 0);
299         if (error)
300                 goto cleanup;
301         size = le32_to_cpu(entry->e_value_size);
302         if (buffer) {
303                 error = -ERANGE;
304                 if (size > buffer_size)
305                         goto cleanup;
306                 memcpy(buffer, (void *)IFIRST(header) +
307                        le16_to_cpu(entry->e_value_offs), size);
308         }
309         error = size;
310
311 cleanup:
312         brelse(iloc.bh);
313         return error;
314 }
315
316 /*
317  * ext4_xattr_get()
318  *
319  * Copy an extended attribute into the buffer
320  * provided, or compute the buffer size required.
321  * Buffer is NULL to compute the size of the buffer required.
322  *
323  * Returns a negative error number on failure, or the number of bytes
324  * used / required on success.
325  */
326 int
327 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
328                void *buffer, size_t buffer_size)
329 {
330         int error;
331
332         down_read(&EXT4_I(inode)->xattr_sem);
333         error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
334                                      buffer_size);
335         if (error == -ENODATA)
336                 error = ext4_xattr_block_get(inode, name_index, name, buffer,
337                                              buffer_size);
338         up_read(&EXT4_I(inode)->xattr_sem);
339         return error;
340 }
341
342 static int
343 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
344                         char *buffer, size_t buffer_size)
345 {
346         size_t rest = buffer_size;
347
348         for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
349                 const struct xattr_handler *handler =
350                         ext4_xattr_handler(entry->e_name_index);
351
352                 if (handler) {
353                         size_t size = handler->list(dentry, buffer, rest,
354                                                     entry->e_name,
355                                                     entry->e_name_len,
356                                                     handler->flags);
357                         if (buffer) {
358                                 if (size > rest)
359                                         return -ERANGE;
360                                 buffer += size;
361                         }
362                         rest -= size;
363                 }
364         }
365         return buffer_size - rest;
366 }
367
368 static int
369 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
370 {
371         struct inode *inode = dentry->d_inode;
372         struct buffer_head *bh = NULL;
373         int error;
374
375         ea_idebug(inode, "buffer=%p, buffer_size=%ld",
376                   buffer, (long)buffer_size);
377
378         error = 0;
379         if (!EXT4_I(inode)->i_file_acl)
380                 goto cleanup;
381         ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
382         bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
383         error = -EIO;
384         if (!bh)
385                 goto cleanup;
386         ea_bdebug(bh, "b_count=%d, refcount=%d",
387                 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
388         if (ext4_xattr_check_block(bh)) {
389                 EXT4_ERROR_INODE(inode, "bad block %llu",
390                                  EXT4_I(inode)->i_file_acl);
391                 error = -EIO;
392                 goto cleanup;
393         }
394         ext4_xattr_cache_insert(bh);
395         error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
396
397 cleanup:
398         brelse(bh);
399
400         return error;
401 }
402
403 static int
404 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
405 {
406         struct inode *inode = dentry->d_inode;
407         struct ext4_xattr_ibody_header *header;
408         struct ext4_inode *raw_inode;
409         struct ext4_iloc iloc;
410         void *end;
411         int error;
412
413         if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
414                 return 0;
415         error = ext4_get_inode_loc(inode, &iloc);
416         if (error)
417                 return error;
418         raw_inode = ext4_raw_inode(&iloc);
419         header = IHDR(inode, raw_inode);
420         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
421         error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
422         if (error)
423                 goto cleanup;
424         error = ext4_xattr_list_entries(dentry, IFIRST(header),
425                                         buffer, buffer_size);
426
427 cleanup:
428         brelse(iloc.bh);
429         return error;
430 }
431
432 /*
433  * ext4_xattr_list()
434  *
435  * Copy a list of attribute names into the buffer
436  * provided, or compute the buffer size required.
437  * Buffer is NULL to compute the size of the buffer required.
438  *
439  * Returns a negative error number on failure, or the number of bytes
440  * used / required on success.
441  */
442 static int
443 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
444 {
445         int ret, ret2;
446
447         down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
448         ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
449         if (ret < 0)
450                 goto errout;
451         if (buffer) {
452                 buffer += ret;
453                 buffer_size -= ret;
454         }
455         ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
456         if (ret < 0)
457                 goto errout;
458         ret += ret2;
459 errout:
460         up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
461         return ret;
462 }
463
464 /*
465  * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
466  * not set, set it.
467  */
468 static void ext4_xattr_update_super_block(handle_t *handle,
469                                           struct super_block *sb)
470 {
471         if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
472                 return;
473
474         if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
475                 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
476                 ext4_handle_dirty_super(handle, sb);
477         }
478 }
479
480 /*
481  * Release the xattr block BH: If the reference count is > 1, decrement
482  * it; otherwise free the block.
483  */
484 static void
485 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
486                          struct buffer_head *bh)
487 {
488         struct mb_cache_entry *ce = NULL;
489         int error = 0;
490
491         ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
492         error = ext4_journal_get_write_access(handle, bh);
493         if (error)
494                 goto out;
495
496         lock_buffer(bh);
497         if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
498                 ea_bdebug(bh, "refcount now=0; freeing");
499                 if (ce)
500                         mb_cache_entry_free(ce);
501                 get_bh(bh);
502                 ext4_free_blocks(handle, inode, bh, 0, 1,
503                                  EXT4_FREE_BLOCKS_METADATA |
504                                  EXT4_FREE_BLOCKS_FORGET);
505                 unlock_buffer(bh);
506         } else {
507                 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
508                 if (ce)
509                         mb_cache_entry_release(ce);
510                 unlock_buffer(bh);
511                 error = ext4_handle_dirty_metadata(handle, inode, bh);
512                 if (IS_SYNC(inode))
513                         ext4_handle_sync(handle);
514                 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
515                 ea_bdebug(bh, "refcount now=%d; releasing",
516                           le32_to_cpu(BHDR(bh)->h_refcount));
517         }
518 out:
519         ext4_std_error(inode->i_sb, error);
520         return;
521 }
522
523 /*
524  * Find the available free space for EAs. This also returns the total number of
525  * bytes used by EA entries.
526  */
527 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
528                                     size_t *min_offs, void *base, int *total)
529 {
530         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
531                 *total += EXT4_XATTR_LEN(last->e_name_len);
532                 if (!last->e_value_block && last->e_value_size) {
533                         size_t offs = le16_to_cpu(last->e_value_offs);
534                         if (offs < *min_offs)
535                                 *min_offs = offs;
536                 }
537         }
538         return (*min_offs - ((void *)last - base) - sizeof(__u32));
539 }
540
541 struct ext4_xattr_info {
542         int name_index;
543         const char *name;
544         const void *value;
545         size_t value_len;
546 };
547
548 struct ext4_xattr_search {
549         struct ext4_xattr_entry *first;
550         void *base;
551         void *end;
552         struct ext4_xattr_entry *here;
553         int not_found;
554 };
555
556 static int
557 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
558 {
559         struct ext4_xattr_entry *last;
560         size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
561
562         /* Compute min_offs and last. */
563         last = s->first;
564         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
565                 if (!last->e_value_block && last->e_value_size) {
566                         size_t offs = le16_to_cpu(last->e_value_offs);
567                         if (offs < min_offs)
568                                 min_offs = offs;
569                 }
570         }
571         free = min_offs - ((void *)last - s->base) - sizeof(__u32);
572         if (!s->not_found) {
573                 if (!s->here->e_value_block && s->here->e_value_size) {
574                         size_t size = le32_to_cpu(s->here->e_value_size);
575                         free += EXT4_XATTR_SIZE(size);
576                 }
577                 free += EXT4_XATTR_LEN(name_len);
578         }
579         if (i->value) {
580                 if (free < EXT4_XATTR_SIZE(i->value_len) ||
581                     free < EXT4_XATTR_LEN(name_len) +
582                            EXT4_XATTR_SIZE(i->value_len))
583                         return -ENOSPC;
584         }
585
586         if (i->value && s->not_found) {
587                 /* Insert the new name. */
588                 size_t size = EXT4_XATTR_LEN(name_len);
589                 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
590                 memmove((void *)s->here + size, s->here, rest);
591                 memset(s->here, 0, size);
592                 s->here->e_name_index = i->name_index;
593                 s->here->e_name_len = name_len;
594                 memcpy(s->here->e_name, i->name, name_len);
595         } else {
596                 if (!s->here->e_value_block && s->here->e_value_size) {
597                         void *first_val = s->base + min_offs;
598                         size_t offs = le16_to_cpu(s->here->e_value_offs);
599                         void *val = s->base + offs;
600                         size_t size = EXT4_XATTR_SIZE(
601                                 le32_to_cpu(s->here->e_value_size));
602
603                         if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
604                                 /* The old and the new value have the same
605                                    size. Just replace. */
606                                 s->here->e_value_size =
607                                         cpu_to_le32(i->value_len);
608                                 memset(val + size - EXT4_XATTR_PAD, 0,
609                                        EXT4_XATTR_PAD); /* Clear pad bytes. */
610                                 memcpy(val, i->value, i->value_len);
611                                 return 0;
612                         }
613
614                         /* Remove the old value. */
615                         memmove(first_val + size, first_val, val - first_val);
616                         memset(first_val, 0, size);
617                         s->here->e_value_size = 0;
618                         s->here->e_value_offs = 0;
619                         min_offs += size;
620
621                         /* Adjust all value offsets. */
622                         last = s->first;
623                         while (!IS_LAST_ENTRY(last)) {
624                                 size_t o = le16_to_cpu(last->e_value_offs);
625                                 if (!last->e_value_block &&
626                                     last->e_value_size && o < offs)
627                                         last->e_value_offs =
628                                                 cpu_to_le16(o + size);
629                                 last = EXT4_XATTR_NEXT(last);
630                         }
631                 }
632                 if (!i->value) {
633                         /* Remove the old name. */
634                         size_t size = EXT4_XATTR_LEN(name_len);
635                         last = ENTRY((void *)last - size);
636                         memmove(s->here, (void *)s->here + size,
637                                 (void *)last - (void *)s->here + sizeof(__u32));
638                         memset(last, 0, size);
639                 }
640         }
641
642         if (i->value) {
643                 /* Insert the new value. */
644                 s->here->e_value_size = cpu_to_le32(i->value_len);
645                 if (i->value_len) {
646                         size_t size = EXT4_XATTR_SIZE(i->value_len);
647                         void *val = s->base + min_offs - size;
648                         s->here->e_value_offs = cpu_to_le16(min_offs - size);
649                         memset(val + size - EXT4_XATTR_PAD, 0,
650                                EXT4_XATTR_PAD); /* Clear the pad bytes. */
651                         memcpy(val, i->value, i->value_len);
652                 }
653         }
654         return 0;
655 }
656
657 struct ext4_xattr_block_find {
658         struct ext4_xattr_search s;
659         struct buffer_head *bh;
660 };
661
662 static int
663 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
664                       struct ext4_xattr_block_find *bs)
665 {
666         struct super_block *sb = inode->i_sb;
667         int error;
668
669         ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
670                   i->name_index, i->name, i->value, (long)i->value_len);
671
672         if (EXT4_I(inode)->i_file_acl) {
673                 /* The inode already has an extended attribute block. */
674                 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
675                 error = -EIO;
676                 if (!bs->bh)
677                         goto cleanup;
678                 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
679                         atomic_read(&(bs->bh->b_count)),
680                         le32_to_cpu(BHDR(bs->bh)->h_refcount));
681                 if (ext4_xattr_check_block(bs->bh)) {
682                         EXT4_ERROR_INODE(inode, "bad block %llu",
683                                          EXT4_I(inode)->i_file_acl);
684                         error = -EIO;
685                         goto cleanup;
686                 }
687                 /* Find the named attribute. */
688                 bs->s.base = BHDR(bs->bh);
689                 bs->s.first = BFIRST(bs->bh);
690                 bs->s.end = bs->bh->b_data + bs->bh->b_size;
691                 bs->s.here = bs->s.first;
692                 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
693                                               i->name, bs->bh->b_size, 1);
694                 if (error && error != -ENODATA)
695                         goto cleanup;
696                 bs->s.not_found = error;
697         }
698         error = 0;
699
700 cleanup:
701         return error;
702 }
703
704 static int
705 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
706                      struct ext4_xattr_info *i,
707                      struct ext4_xattr_block_find *bs)
708 {
709         struct super_block *sb = inode->i_sb;
710         struct buffer_head *new_bh = NULL;
711         struct ext4_xattr_search *s = &bs->s;
712         struct mb_cache_entry *ce = NULL;
713         int error = 0;
714
715 #define header(x) ((struct ext4_xattr_header *)(x))
716
717         if (i->value && i->value_len > sb->s_blocksize)
718                 return -ENOSPC;
719         if (s->base) {
720                 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
721                                         bs->bh->b_blocknr);
722                 error = ext4_journal_get_write_access(handle, bs->bh);
723                 if (error)
724                         goto cleanup;
725                 lock_buffer(bs->bh);
726
727                 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
728                         if (ce) {
729                                 mb_cache_entry_free(ce);
730                                 ce = NULL;
731                         }
732                         ea_bdebug(bs->bh, "modifying in-place");
733                         error = ext4_xattr_set_entry(i, s);
734                         if (!error) {
735                                 if (!IS_LAST_ENTRY(s->first))
736                                         ext4_xattr_rehash(header(s->base),
737                                                           s->here);
738                                 ext4_xattr_cache_insert(bs->bh);
739                         }
740                         unlock_buffer(bs->bh);
741                         if (error == -EIO)
742                                 goto bad_block;
743                         if (!error)
744                                 error = ext4_handle_dirty_metadata(handle,
745                                                                    inode,
746                                                                    bs->bh);
747                         if (error)
748                                 goto cleanup;
749                         goto inserted;
750                 } else {
751                         int offset = (char *)s->here - bs->bh->b_data;
752
753                         unlock_buffer(bs->bh);
754                         ext4_handle_release_buffer(handle, bs->bh);
755                         if (ce) {
756                                 mb_cache_entry_release(ce);
757                                 ce = NULL;
758                         }
759                         ea_bdebug(bs->bh, "cloning");
760                         s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
761                         error = -ENOMEM;
762                         if (s->base == NULL)
763                                 goto cleanup;
764                         memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
765                         s->first = ENTRY(header(s->base)+1);
766                         header(s->base)->h_refcount = cpu_to_le32(1);
767                         s->here = ENTRY(s->base + offset);
768                         s->end = s->base + bs->bh->b_size;
769                 }
770         } else {
771                 /* Allocate a buffer where we construct the new block. */
772                 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
773                 /* assert(header == s->base) */
774                 error = -ENOMEM;
775                 if (s->base == NULL)
776                         goto cleanup;
777                 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
778                 header(s->base)->h_blocks = cpu_to_le32(1);
779                 header(s->base)->h_refcount = cpu_to_le32(1);
780                 s->first = ENTRY(header(s->base)+1);
781                 s->here = ENTRY(header(s->base)+1);
782                 s->end = s->base + sb->s_blocksize;
783         }
784
785         error = ext4_xattr_set_entry(i, s);
786         if (error == -EIO)
787                 goto bad_block;
788         if (error)
789                 goto cleanup;
790         if (!IS_LAST_ENTRY(s->first))
791                 ext4_xattr_rehash(header(s->base), s->here);
792
793 inserted:
794         if (!IS_LAST_ENTRY(s->first)) {
795                 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
796                 if (new_bh) {
797                         /* We found an identical block in the cache. */
798                         if (new_bh == bs->bh)
799                                 ea_bdebug(new_bh, "keeping");
800                         else {
801                                 /* The old block is released after updating
802                                    the inode. */
803                                 error = dquot_alloc_block(inode,
804                                                 EXT4_C2B(EXT4_SB(sb), 1));
805                                 if (error)
806                                         goto cleanup;
807                                 error = ext4_journal_get_write_access(handle,
808                                                                       new_bh);
809                                 if (error)
810                                         goto cleanup_dquot;
811                                 lock_buffer(new_bh);
812                                 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
813                                 ea_bdebug(new_bh, "reusing; refcount now=%d",
814                                         le32_to_cpu(BHDR(new_bh)->h_refcount));
815                                 unlock_buffer(new_bh);
816                                 error = ext4_handle_dirty_metadata(handle,
817                                                                    inode,
818                                                                    new_bh);
819                                 if (error)
820                                         goto cleanup_dquot;
821                         }
822                         mb_cache_entry_release(ce);
823                         ce = NULL;
824                 } else if (bs->bh && s->base == bs->bh->b_data) {
825                         /* We were modifying this block in-place. */
826                         ea_bdebug(bs->bh, "keeping this block");
827                         new_bh = bs->bh;
828                         get_bh(new_bh);
829                 } else {
830                         /* We need to allocate a new block */
831                         ext4_fsblk_t goal, block;
832
833                         goal = ext4_group_first_block_no(sb,
834                                                 EXT4_I(inode)->i_block_group);
835
836                         /* non-extent files can't have physical blocks past 2^32 */
837                         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
838                                 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
839
840                         /*
841                          * take i_data_sem because we will test
842                          * i_delalloc_reserved_flag in ext4_mb_new_blocks
843                          */
844                         down_read((&EXT4_I(inode)->i_data_sem));
845                         block = ext4_new_meta_blocks(handle, inode, goal, 0,
846                                                      NULL, &error);
847                         up_read((&EXT4_I(inode)->i_data_sem));
848                         if (error)
849                                 goto cleanup;
850
851                         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
852                                 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
853
854                         ea_idebug(inode, "creating block %d", block);
855
856                         new_bh = sb_getblk(sb, block);
857                         if (!new_bh) {
858                                 error = -ENOMEM;
859 getblk_failed:
860                                 ext4_free_blocks(handle, inode, NULL, block, 1,
861                                                  EXT4_FREE_BLOCKS_METADATA);
862                                 goto cleanup;
863                         }
864                         lock_buffer(new_bh);
865                         error = ext4_journal_get_create_access(handle, new_bh);
866                         if (error) {
867                                 unlock_buffer(new_bh);
868                                 error = -EIO;
869                                 goto getblk_failed;
870                         }
871                         memcpy(new_bh->b_data, s->base, new_bh->b_size);
872                         set_buffer_uptodate(new_bh);
873                         unlock_buffer(new_bh);
874                         ext4_xattr_cache_insert(new_bh);
875                         error = ext4_handle_dirty_metadata(handle,
876                                                            inode, new_bh);
877                         if (error)
878                                 goto cleanup;
879                 }
880         }
881
882         /* Update the inode. */
883         EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
884
885         /* Drop the previous xattr block. */
886         if (bs->bh && bs->bh != new_bh)
887                 ext4_xattr_release_block(handle, inode, bs->bh);
888         error = 0;
889
890 cleanup:
891         if (ce)
892                 mb_cache_entry_release(ce);
893         brelse(new_bh);
894         if (!(bs->bh && s->base == bs->bh->b_data))
895                 kfree(s->base);
896
897         return error;
898
899 cleanup_dquot:
900         dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
901         goto cleanup;
902
903 bad_block:
904         EXT4_ERROR_INODE(inode, "bad block %llu",
905                          EXT4_I(inode)->i_file_acl);
906         goto cleanup;
907
908 #undef header
909 }
910
911 struct ext4_xattr_ibody_find {
912         struct ext4_xattr_search s;
913         struct ext4_iloc iloc;
914 };
915
916 static int
917 ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
918                       struct ext4_xattr_ibody_find *is)
919 {
920         struct ext4_xattr_ibody_header *header;
921         struct ext4_inode *raw_inode;
922         int error;
923
924         if (EXT4_I(inode)->i_extra_isize == 0)
925                 return 0;
926         raw_inode = ext4_raw_inode(&is->iloc);
927         header = IHDR(inode, raw_inode);
928         is->s.base = is->s.first = IFIRST(header);
929         is->s.here = is->s.first;
930         is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
931         if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
932                 error = ext4_xattr_check_names(IFIRST(header), is->s.end,
933                                                IFIRST(header));
934                 if (error)
935                         return error;
936                 /* Find the named attribute. */
937                 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
938                                               i->name, is->s.end -
939                                               (void *)is->s.base, 0);
940                 if (error && error != -ENODATA)
941                         return error;
942                 is->s.not_found = error;
943         }
944         return 0;
945 }
946
947 static int
948 ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
949                      struct ext4_xattr_info *i,
950                      struct ext4_xattr_ibody_find *is)
951 {
952         struct ext4_xattr_ibody_header *header;
953         struct ext4_xattr_search *s = &is->s;
954         int error;
955
956         if (EXT4_I(inode)->i_extra_isize == 0)
957                 return -ENOSPC;
958         error = ext4_xattr_set_entry(i, s);
959         if (error)
960                 return error;
961         header = IHDR(inode, ext4_raw_inode(&is->iloc));
962         if (!IS_LAST_ENTRY(s->first)) {
963                 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
964                 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
965         } else {
966                 header->h_magic = cpu_to_le32(0);
967                 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
968         }
969         return 0;
970 }
971
972 /*
973  * ext4_xattr_set_handle()
974  *
975  * Create, replace or remove an extended attribute for this inode.  Value
976  * is NULL to remove an existing extended attribute, and non-NULL to
977  * either replace an existing extended attribute, or create a new extended
978  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
979  * specify that an extended attribute must exist and must not exist
980  * previous to the call, respectively.
981  *
982  * Returns 0, or a negative error number on failure.
983  */
984 int
985 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
986                       const char *name, const void *value, size_t value_len,
987                       int flags)
988 {
989         struct ext4_xattr_info i = {
990                 .name_index = name_index,
991                 .name = name,
992                 .value = value,
993                 .value_len = value_len,
994
995         };
996         struct ext4_xattr_ibody_find is = {
997                 .s = { .not_found = -ENODATA, },
998         };
999         struct ext4_xattr_block_find bs = {
1000                 .s = { .not_found = -ENODATA, },
1001         };
1002         unsigned long no_expand;
1003         int error;
1004
1005         if (!name)
1006                 return -EINVAL;
1007         if (strlen(name) > 255)
1008                 return -ERANGE;
1009         down_write(&EXT4_I(inode)->xattr_sem);
1010         no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1011         ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1012
1013         error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1014         if (error)
1015                 goto cleanup;
1016
1017         if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1018                 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1019                 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1020                 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1021         }
1022
1023         error = ext4_xattr_ibody_find(inode, &i, &is);
1024         if (error)
1025                 goto cleanup;
1026         if (is.s.not_found)
1027                 error = ext4_xattr_block_find(inode, &i, &bs);
1028         if (error)
1029                 goto cleanup;
1030         if (is.s.not_found && bs.s.not_found) {
1031                 error = -ENODATA;
1032                 if (flags & XATTR_REPLACE)
1033                         goto cleanup;
1034                 error = 0;
1035                 if (!value)
1036                         goto cleanup;
1037         } else {
1038                 error = -EEXIST;
1039                 if (flags & XATTR_CREATE)
1040                         goto cleanup;
1041         }
1042         if (!value) {
1043                 if (!is.s.not_found)
1044                         error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1045                 else if (!bs.s.not_found)
1046                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
1047         } else {
1048                 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1049                 if (!error && !bs.s.not_found) {
1050                         i.value = NULL;
1051                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
1052                 } else if (error == -ENOSPC) {
1053                         if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1054                                 error = ext4_xattr_block_find(inode, &i, &bs);
1055                                 if (error)
1056                                         goto cleanup;
1057                         }
1058                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
1059                         if (error)
1060                                 goto cleanup;
1061                         if (!is.s.not_found) {
1062                                 i.value = NULL;
1063                                 error = ext4_xattr_ibody_set(handle, inode, &i,
1064                                                              &is);
1065                         }
1066                 }
1067         }
1068         if (!error) {
1069                 ext4_xattr_update_super_block(handle, inode->i_sb);
1070                 inode->i_ctime = ext4_current_time(inode);
1071                 if (!value)
1072                         ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1073                 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1074                 /*
1075                  * The bh is consumed by ext4_mark_iloc_dirty, even with
1076                  * error != 0.
1077                  */
1078                 is.iloc.bh = NULL;
1079                 if (IS_SYNC(inode))
1080                         ext4_handle_sync(handle);
1081         }
1082
1083 cleanup:
1084         brelse(is.iloc.bh);
1085         brelse(bs.bh);
1086         if (no_expand == 0)
1087                 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1088         up_write(&EXT4_I(inode)->xattr_sem);
1089         return error;
1090 }
1091
1092 /*
1093  * ext4_xattr_set()
1094  *
1095  * Like ext4_xattr_set_handle, but start from an inode. This extended
1096  * attribute modification is a filesystem transaction by itself.
1097  *
1098  * Returns 0, or a negative error number on failure.
1099  */
1100 int
1101 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1102                const void *value, size_t value_len, int flags)
1103 {
1104         handle_t *handle;
1105         int error, retries = 0;
1106
1107 retry:
1108         handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
1109         if (IS_ERR(handle)) {
1110                 error = PTR_ERR(handle);
1111         } else {
1112                 int error2;
1113
1114                 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1115                                               value, value_len, flags);
1116                 error2 = ext4_journal_stop(handle);
1117                 if (error == -ENOSPC &&
1118                     ext4_should_retry_alloc(inode->i_sb, &retries))
1119                         goto retry;
1120                 if (error == 0)
1121                         error = error2;
1122         }
1123
1124         return error;
1125 }
1126
1127 /*
1128  * Shift the EA entries in the inode to create space for the increased
1129  * i_extra_isize.
1130  */
1131 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1132                                      int value_offs_shift, void *to,
1133                                      void *from, size_t n, int blocksize)
1134 {
1135         struct ext4_xattr_entry *last = entry;
1136         int new_offs;
1137
1138         /* Adjust the value offsets of the entries */
1139         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1140                 if (!last->e_value_block && last->e_value_size) {
1141                         new_offs = le16_to_cpu(last->e_value_offs) +
1142                                                         value_offs_shift;
1143                         BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1144                                  > blocksize);
1145                         last->e_value_offs = cpu_to_le16(new_offs);
1146                 }
1147         }
1148         /* Shift the entries by n bytes */
1149         memmove(to, from, n);
1150 }
1151
1152 /*
1153  * Expand an inode by new_extra_isize bytes when EAs are present.
1154  * Returns 0 on success or negative error number on failure.
1155  */
1156 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1157                                struct ext4_inode *raw_inode, handle_t *handle)
1158 {
1159         struct ext4_xattr_ibody_header *header;
1160         struct ext4_xattr_entry *entry, *last, *first;
1161         struct buffer_head *bh = NULL;
1162         struct ext4_xattr_ibody_find *is = NULL;
1163         struct ext4_xattr_block_find *bs = NULL;
1164         char *buffer = NULL, *b_entry_name = NULL;
1165         size_t min_offs, free;
1166         int total_ino, total_blk;
1167         void *base, *start, *end;
1168         int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
1169         int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1170
1171         down_write(&EXT4_I(inode)->xattr_sem);
1172 retry:
1173         if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1174                 up_write(&EXT4_I(inode)->xattr_sem);
1175                 return 0;
1176         }
1177
1178         header = IHDR(inode, raw_inode);
1179         entry = IFIRST(header);
1180
1181         /*
1182          * Check if enough free space is available in the inode to shift the
1183          * entries ahead by new_extra_isize.
1184          */
1185
1186         base = start = entry;
1187         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1188         min_offs = end - base;
1189         last = entry;
1190         total_ino = sizeof(struct ext4_xattr_ibody_header);
1191
1192         free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1193         if (free >= new_extra_isize) {
1194                 entry = IFIRST(header);
1195                 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1196                                 - new_extra_isize, (void *)raw_inode +
1197                                 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1198                                 (void *)header, total_ino,
1199                                 inode->i_sb->s_blocksize);
1200                 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1201                 error = 0;
1202                 goto cleanup;
1203         }
1204
1205         /*
1206          * Enough free space isn't available in the inode, check if
1207          * EA block can hold new_extra_isize bytes.
1208          */
1209         if (EXT4_I(inode)->i_file_acl) {
1210                 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1211                 error = -EIO;
1212                 if (!bh)
1213                         goto cleanup;
1214                 if (ext4_xattr_check_block(bh)) {
1215                         EXT4_ERROR_INODE(inode, "bad block %llu",
1216                                          EXT4_I(inode)->i_file_acl);
1217                         error = -EIO;
1218                         goto cleanup;
1219                 }
1220                 base = BHDR(bh);
1221                 first = BFIRST(bh);
1222                 end = bh->b_data + bh->b_size;
1223                 min_offs = end - base;
1224                 free = ext4_xattr_free_space(first, &min_offs, base,
1225                                              &total_blk);
1226                 if (free < new_extra_isize) {
1227                         if (!tried_min_extra_isize && s_min_extra_isize) {
1228                                 tried_min_extra_isize++;
1229                                 new_extra_isize = s_min_extra_isize;
1230                                 brelse(bh);
1231                                 goto retry;
1232                         }
1233                         error = -1;
1234                         goto cleanup;
1235                 }
1236         } else {
1237                 free = inode->i_sb->s_blocksize;
1238         }
1239
1240         while (new_extra_isize > 0) {
1241                 size_t offs, size, entry_size;
1242                 struct ext4_xattr_entry *small_entry = NULL;
1243                 struct ext4_xattr_info i = {
1244                         .value = NULL,
1245                         .value_len = 0,
1246                 };
1247                 unsigned int total_size;  /* EA entry size + value size */
1248                 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1249                 unsigned int min_total_size = ~0U;
1250
1251                 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1252                 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1253                 if (!is || !bs) {
1254                         error = -ENOMEM;
1255                         goto cleanup;
1256                 }
1257
1258                 is->s.not_found = -ENODATA;
1259                 bs->s.not_found = -ENODATA;
1260                 is->iloc.bh = NULL;
1261                 bs->bh = NULL;
1262
1263                 last = IFIRST(header);
1264                 /* Find the entry best suited to be pushed into EA block */
1265                 entry = NULL;
1266                 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1267                         total_size =
1268                         EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1269                                         EXT4_XATTR_LEN(last->e_name_len);
1270                         if (total_size <= free && total_size < min_total_size) {
1271                                 if (total_size < new_extra_isize) {
1272                                         small_entry = last;
1273                                 } else {
1274                                         entry = last;
1275                                         min_total_size = total_size;
1276                                 }
1277                         }
1278                 }
1279
1280                 if (entry == NULL) {
1281                         if (small_entry) {
1282                                 entry = small_entry;
1283                         } else {
1284                                 if (!tried_min_extra_isize &&
1285                                     s_min_extra_isize) {
1286                                         tried_min_extra_isize++;
1287                                         new_extra_isize = s_min_extra_isize;
1288                                         kfree(is); is = NULL;
1289                                         kfree(bs); bs = NULL;
1290                                         brelse(bh);
1291                                         goto retry;
1292                                 }
1293                                 error = -1;
1294                                 goto cleanup;
1295                         }
1296                 }
1297                 offs = le16_to_cpu(entry->e_value_offs);
1298                 size = le32_to_cpu(entry->e_value_size);
1299                 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1300                 i.name_index = entry->e_name_index,
1301                 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1302                 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1303                 if (!buffer || !b_entry_name) {
1304                         error = -ENOMEM;
1305                         goto cleanup;
1306                 }
1307                 /* Save the entry name and the entry value */
1308                 memcpy(buffer, (void *)IFIRST(header) + offs,
1309                        EXT4_XATTR_SIZE(size));
1310                 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1311                 b_entry_name[entry->e_name_len] = '\0';
1312                 i.name = b_entry_name;
1313
1314                 error = ext4_get_inode_loc(inode, &is->iloc);
1315                 if (error)
1316                         goto cleanup;
1317
1318                 error = ext4_xattr_ibody_find(inode, &i, is);
1319                 if (error)
1320                         goto cleanup;
1321
1322                 /* Remove the chosen entry from the inode */
1323                 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1324                 if (error)
1325                         goto cleanup;
1326
1327                 entry = IFIRST(header);
1328                 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1329                         shift_bytes = new_extra_isize;
1330                 else
1331                         shift_bytes = entry_size + size;
1332                 /* Adjust the offsets and shift the remaining entries ahead */
1333                 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1334                         shift_bytes, (void *)raw_inode +
1335                         EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1336                         (void *)header, total_ino - entry_size,
1337                         inode->i_sb->s_blocksize);
1338
1339                 extra_isize += shift_bytes;
1340                 new_extra_isize -= shift_bytes;
1341                 EXT4_I(inode)->i_extra_isize = extra_isize;
1342
1343                 i.name = b_entry_name;
1344                 i.value = buffer;
1345                 i.value_len = size;
1346                 error = ext4_xattr_block_find(inode, &i, bs);
1347                 if (error)
1348                         goto cleanup;
1349
1350                 /* Add entry which was removed from the inode into the block */
1351                 error = ext4_xattr_block_set(handle, inode, &i, bs);
1352                 if (error)
1353                         goto cleanup;
1354                 kfree(b_entry_name);
1355                 kfree(buffer);
1356                 b_entry_name = NULL;
1357                 buffer = NULL;
1358                 brelse(is->iloc.bh);
1359                 kfree(is);
1360                 kfree(bs);
1361         }
1362         brelse(bh);
1363         up_write(&EXT4_I(inode)->xattr_sem);
1364         return 0;
1365
1366 cleanup:
1367         kfree(b_entry_name);
1368         kfree(buffer);
1369         if (is)
1370                 brelse(is->iloc.bh);
1371         kfree(is);
1372         kfree(bs);
1373         brelse(bh);
1374         up_write(&EXT4_I(inode)->xattr_sem);
1375         return error;
1376 }
1377
1378
1379
1380 /*
1381  * ext4_xattr_delete_inode()
1382  *
1383  * Free extended attribute resources associated with this inode. This
1384  * is called immediately before an inode is freed. We have exclusive
1385  * access to the inode.
1386  */
1387 void
1388 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1389 {
1390         struct buffer_head *bh = NULL;
1391
1392         if (!EXT4_I(inode)->i_file_acl)
1393                 goto cleanup;
1394         bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1395         if (!bh) {
1396                 EXT4_ERROR_INODE(inode, "block %llu read error",
1397                                  EXT4_I(inode)->i_file_acl);
1398                 goto cleanup;
1399         }
1400         if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1401             BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1402                 EXT4_ERROR_INODE(inode, "bad block %llu",
1403                                  EXT4_I(inode)->i_file_acl);
1404                 goto cleanup;
1405         }
1406         ext4_xattr_release_block(handle, inode, bh);
1407         EXT4_I(inode)->i_file_acl = 0;
1408
1409 cleanup:
1410         brelse(bh);
1411 }
1412
1413 /*
1414  * ext4_xattr_put_super()
1415  *
1416  * This is called when a file system is unmounted.
1417  */
1418 void
1419 ext4_xattr_put_super(struct super_block *sb)
1420 {
1421         mb_cache_shrink(sb->s_bdev);
1422 }
1423
1424 /*
1425  * ext4_xattr_cache_insert()
1426  *
1427  * Create a new entry in the extended attribute cache, and insert
1428  * it unless such an entry is already in the cache.
1429  *
1430  * Returns 0, or a negative error number on failure.
1431  */
1432 static void
1433 ext4_xattr_cache_insert(struct buffer_head *bh)
1434 {
1435         __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1436         struct mb_cache_entry *ce;
1437         int error;
1438
1439         ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
1440         if (!ce) {
1441                 ea_bdebug(bh, "out of memory");
1442                 return;
1443         }
1444         error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1445         if (error) {
1446                 mb_cache_entry_free(ce);
1447                 if (error == -EBUSY) {
1448                         ea_bdebug(bh, "already in cache");
1449                         error = 0;
1450                 }
1451         } else {
1452                 ea_bdebug(bh, "inserting [%x]", (int)hash);
1453                 mb_cache_entry_release(ce);
1454         }
1455 }
1456
1457 /*
1458  * ext4_xattr_cmp()
1459  *
1460  * Compare two extended attribute blocks for equality.
1461  *
1462  * Returns 0 if the blocks are equal, 1 if they differ, and
1463  * a negative error number on errors.
1464  */
1465 static int
1466 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1467                struct ext4_xattr_header *header2)
1468 {
1469         struct ext4_xattr_entry *entry1, *entry2;
1470
1471         entry1 = ENTRY(header1+1);
1472         entry2 = ENTRY(header2+1);
1473         while (!IS_LAST_ENTRY(entry1)) {
1474                 if (IS_LAST_ENTRY(entry2))
1475                         return 1;
1476                 if (entry1->e_hash != entry2->e_hash ||
1477                     entry1->e_name_index != entry2->e_name_index ||
1478                     entry1->e_name_len != entry2->e_name_len ||
1479                     entry1->e_value_size != entry2->e_value_size ||
1480                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1481                         return 1;
1482                 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1483                         return -EIO;
1484                 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1485                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1486                            le32_to_cpu(entry1->e_value_size)))
1487                         return 1;
1488
1489                 entry1 = EXT4_XATTR_NEXT(entry1);
1490                 entry2 = EXT4_XATTR_NEXT(entry2);
1491         }
1492         if (!IS_LAST_ENTRY(entry2))
1493                 return 1;
1494         return 0;
1495 }
1496
1497 /*
1498  * ext4_xattr_cache_find()
1499  *
1500  * Find an identical extended attribute block.
1501  *
1502  * Returns a pointer to the block found, or NULL if such a block was
1503  * not found or an error occurred.
1504  */
1505 static struct buffer_head *
1506 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1507                       struct mb_cache_entry **pce)
1508 {
1509         __u32 hash = le32_to_cpu(header->h_hash);
1510         struct mb_cache_entry *ce;
1511
1512         if (!header->h_hash)
1513                 return NULL;  /* never share */
1514         ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1515 again:
1516         ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
1517                                        hash);
1518         while (ce) {
1519                 struct buffer_head *bh;
1520
1521                 if (IS_ERR(ce)) {
1522                         if (PTR_ERR(ce) == -EAGAIN)
1523                                 goto again;
1524                         break;
1525                 }
1526                 bh = sb_bread(inode->i_sb, ce->e_block);
1527                 if (!bh) {
1528                         EXT4_ERROR_INODE(inode, "block %lu read error",
1529                                          (unsigned long) ce->e_block);
1530                 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1531                                 EXT4_XATTR_REFCOUNT_MAX) {
1532                         ea_idebug(inode, "block %lu refcount %d>=%d",
1533                                   (unsigned long) ce->e_block,
1534                                   le32_to_cpu(BHDR(bh)->h_refcount),
1535                                           EXT4_XATTR_REFCOUNT_MAX);
1536                 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1537                         *pce = ce;
1538                         return bh;
1539                 }
1540                 brelse(bh);
1541                 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1542         }
1543         return NULL;
1544 }
1545
1546 #define NAME_HASH_SHIFT 5
1547 #define VALUE_HASH_SHIFT 16
1548
1549 /*
1550  * ext4_xattr_hash_entry()
1551  *
1552  * Compute the hash of an extended attribute.
1553  */
1554 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1555                                          struct ext4_xattr_entry *entry)
1556 {
1557         __u32 hash = 0;
1558         char *name = entry->e_name;
1559         int n;
1560
1561         for (n = 0; n < entry->e_name_len; n++) {
1562                 hash = (hash << NAME_HASH_SHIFT) ^
1563                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1564                        *name++;
1565         }
1566
1567         if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1568                 __le32 *value = (__le32 *)((char *)header +
1569                         le16_to_cpu(entry->e_value_offs));
1570                 for (n = (le32_to_cpu(entry->e_value_size) +
1571                      EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1572                         hash = (hash << VALUE_HASH_SHIFT) ^
1573                                (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1574                                le32_to_cpu(*value++);
1575                 }
1576         }
1577         entry->e_hash = cpu_to_le32(hash);
1578 }
1579
1580 #undef NAME_HASH_SHIFT
1581 #undef VALUE_HASH_SHIFT
1582
1583 #define BLOCK_HASH_SHIFT 16
1584
1585 /*
1586  * ext4_xattr_rehash()
1587  *
1588  * Re-compute the extended attribute hash value after an entry has changed.
1589  */
1590 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1591                               struct ext4_xattr_entry *entry)
1592 {
1593         struct ext4_xattr_entry *here;
1594         __u32 hash = 0;
1595
1596         ext4_xattr_hash_entry(header, entry);
1597         here = ENTRY(header+1);
1598         while (!IS_LAST_ENTRY(here)) {
1599                 if (!here->e_hash) {
1600                         /* Block is not shared if an entry's hash value == 0 */
1601                         hash = 0;
1602                         break;
1603                 }
1604                 hash = (hash << BLOCK_HASH_SHIFT) ^
1605                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1606                        le32_to_cpu(here->e_hash);
1607                 here = EXT4_XATTR_NEXT(here);
1608         }
1609         header->h_hash = cpu_to_le32(hash);
1610 }
1611
1612 #undef BLOCK_HASH_SHIFT
1613
1614 int __init
1615 ext4_init_xattr(void)
1616 {
1617         ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
1618         if (!ext4_xattr_cache)
1619                 return -ENOMEM;
1620         return 0;
1621 }
1622
1623 void
1624 ext4_exit_xattr(void)
1625 {
1626         if (ext4_xattr_cache)
1627                 mb_cache_destroy(ext4_xattr_cache);
1628         ext4_xattr_cache = NULL;
1629 }