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