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