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