ext4: remove unused header files
[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 "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         __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_metadata_csum(inode->i_sb) &&
145             (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
146                 return 0;
147         return 1;
148 }
149
150 static void ext4_xattr_block_csum_set(struct inode *inode,
151                                       sector_t block_nr,
152                                       struct ext4_xattr_header *hdr)
153 {
154         if (!ext4_has_metadata_csum(inode->i_sb))
155                 return;
156
157         hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
158 }
159
160 static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
161                                                 struct inode *inode,
162                                                 struct buffer_head *bh)
163 {
164         ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
165         return ext4_handle_dirty_metadata(handle, inode, bh);
166 }
167
168 static inline const struct xattr_handler *
169 ext4_xattr_handler(int name_index)
170 {
171         const struct xattr_handler *handler = NULL;
172
173         if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
174                 handler = ext4_xattr_handler_map[name_index];
175         return handler;
176 }
177
178 /*
179  * Inode operation listxattr()
180  *
181  * dentry->d_inode->i_mutex: don't care
182  */
183 ssize_t
184 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
185 {
186         return ext4_xattr_list(dentry, buffer, size);
187 }
188
189 static int
190 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
191                        void *value_start)
192 {
193         struct ext4_xattr_entry *e = entry;
194
195         while (!IS_LAST_ENTRY(e)) {
196                 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
197                 if ((void *)next >= end)
198                         return -EIO;
199                 e = next;
200         }
201
202         while (!IS_LAST_ENTRY(entry)) {
203                 if (entry->e_value_size != 0 &&
204                     (value_start + le16_to_cpu(entry->e_value_offs) <
205                      (void *)e + sizeof(__u32) ||
206                      value_start + le16_to_cpu(entry->e_value_offs) +
207                     le32_to_cpu(entry->e_value_size) > end))
208                         return -EIO;
209                 entry = EXT4_XATTR_NEXT(entry);
210         }
211
212         return 0;
213 }
214
215 static inline int
216 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
217 {
218         int error;
219
220         if (buffer_verified(bh))
221                 return 0;
222
223         if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
224             BHDR(bh)->h_blocks != cpu_to_le32(1))
225                 return -EIO;
226         if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
227                 return -EIO;
228         error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
229                                        bh->b_data);
230         if (!error)
231                 set_buffer_verified(bh);
232         return error;
233 }
234
235 static inline int
236 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
237 {
238         size_t value_size = le32_to_cpu(entry->e_value_size);
239
240         if (entry->e_value_block != 0 || value_size > size ||
241             le16_to_cpu(entry->e_value_offs) + value_size > size)
242                 return -EIO;
243         return 0;
244 }
245
246 static int
247 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
248                       const char *name, size_t size, int sorted)
249 {
250         struct ext4_xattr_entry *entry;
251         size_t name_len;
252         int cmp = 1;
253
254         if (name == NULL)
255                 return -EINVAL;
256         name_len = strlen(name);
257         entry = *pentry;
258         for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
259                 cmp = name_index - entry->e_name_index;
260                 if (!cmp)
261                         cmp = name_len - entry->e_name_len;
262                 if (!cmp)
263                         cmp = memcmp(name, entry->e_name, name_len);
264                 if (cmp <= 0 && (sorted || cmp == 0))
265                         break;
266         }
267         *pentry = entry;
268         if (!cmp && ext4_xattr_check_entry(entry, size))
269                         return -EIO;
270         return cmp ? -ENODATA : 0;
271 }
272
273 static int
274 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
275                      void *buffer, size_t buffer_size)
276 {
277         struct buffer_head *bh = NULL;
278         struct ext4_xattr_entry *entry;
279         size_t size;
280         int error;
281         struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
282
283         ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
284                   name_index, name, buffer, (long)buffer_size);
285
286         error = -ENODATA;
287         if (!EXT4_I(inode)->i_file_acl)
288                 goto cleanup;
289         ea_idebug(inode, "reading block %llu",
290                   (unsigned long long)EXT4_I(inode)->i_file_acl);
291         bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
292         if (!bh)
293                 goto cleanup;
294         ea_bdebug(bh, "b_count=%d, refcount=%d",
295                 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
296         if (ext4_xattr_check_block(inode, bh)) {
297 bad_block:
298                 EXT4_ERROR_INODE(inode, "bad block %llu",
299                                  EXT4_I(inode)->i_file_acl);
300                 error = -EIO;
301                 goto cleanup;
302         }
303         ext4_xattr_cache_insert(ext4_mb_cache, bh);
304         entry = BFIRST(bh);
305         error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
306         if (error == -EIO)
307                 goto bad_block;
308         if (error)
309                 goto cleanup;
310         size = le32_to_cpu(entry->e_value_size);
311         if (buffer) {
312                 error = -ERANGE;
313                 if (size > buffer_size)
314                         goto cleanup;
315                 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
316                        size);
317         }
318         error = size;
319
320 cleanup:
321         brelse(bh);
322         return error;
323 }
324
325 int
326 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
327                      void *buffer, size_t buffer_size)
328 {
329         struct ext4_xattr_ibody_header *header;
330         struct ext4_xattr_entry *entry;
331         struct ext4_inode *raw_inode;
332         struct ext4_iloc iloc;
333         size_t size;
334         void *end;
335         int error;
336
337         if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
338                 return -ENODATA;
339         error = ext4_get_inode_loc(inode, &iloc);
340         if (error)
341                 return error;
342         raw_inode = ext4_raw_inode(&iloc);
343         header = IHDR(inode, raw_inode);
344         entry = IFIRST(header);
345         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
346         error = ext4_xattr_check_names(entry, end, entry);
347         if (error)
348                 goto cleanup;
349         error = ext4_xattr_find_entry(&entry, name_index, name,
350                                       end - (void *)entry, 0);
351         if (error)
352                 goto cleanup;
353         size = le32_to_cpu(entry->e_value_size);
354         if (buffer) {
355                 error = -ERANGE;
356                 if (size > buffer_size)
357                         goto cleanup;
358                 memcpy(buffer, (void *)IFIRST(header) +
359                        le16_to_cpu(entry->e_value_offs), size);
360         }
361         error = size;
362
363 cleanup:
364         brelse(iloc.bh);
365         return error;
366 }
367
368 /*
369  * ext4_xattr_get()
370  *
371  * Copy an extended attribute into the buffer
372  * provided, or compute the buffer size required.
373  * Buffer is NULL to compute the size of the buffer required.
374  *
375  * Returns a negative error number on failure, or the number of bytes
376  * used / required on success.
377  */
378 int
379 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
380                void *buffer, size_t buffer_size)
381 {
382         int error;
383
384         if (strlen(name) > 255)
385                 return -ERANGE;
386
387         down_read(&EXT4_I(inode)->xattr_sem);
388         error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
389                                      buffer_size);
390         if (error == -ENODATA)
391                 error = ext4_xattr_block_get(inode, name_index, name, buffer,
392                                              buffer_size);
393         up_read(&EXT4_I(inode)->xattr_sem);
394         return error;
395 }
396
397 static int
398 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
399                         char *buffer, size_t buffer_size)
400 {
401         size_t rest = buffer_size;
402
403         for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
404                 const struct xattr_handler *handler =
405                         ext4_xattr_handler(entry->e_name_index);
406
407                 if (handler) {
408                         size_t size = handler->list(dentry, buffer, rest,
409                                                     entry->e_name,
410                                                     entry->e_name_len,
411                                                     handler->flags);
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 = dentry->d_inode;
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 = -EIO;
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 = dentry->d_inode;
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(dentry->d_inode)->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(dentry->d_inode)->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_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
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_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
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_SIZE(i->value_len) ||
642                     free < EXT4_XATTR_LEN(name_len) +
643                            EXT4_XATTR_SIZE(i->value_len))
644                         return -ENOSPC;
645         }
646
647         if (i->value && s->not_found) {
648                 /* Insert the new name. */
649                 size_t size = EXT4_XATTR_LEN(name_len);
650                 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
651                 memmove((void *)s->here + size, s->here, rest);
652                 memset(s->here, 0, size);
653                 s->here->e_name_index = i->name_index;
654                 s->here->e_name_len = name_len;
655                 memcpy(s->here->e_name, i->name, name_len);
656         } else {
657                 if (!s->here->e_value_block && s->here->e_value_size) {
658                         void *first_val = s->base + min_offs;
659                         size_t offs = le16_to_cpu(s->here->e_value_offs);
660                         void *val = s->base + offs;
661                         size_t size = EXT4_XATTR_SIZE(
662                                 le32_to_cpu(s->here->e_value_size));
663
664                         if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
665                                 /* The old and the new value have the same
666                                    size. Just replace. */
667                                 s->here->e_value_size =
668                                         cpu_to_le32(i->value_len);
669                                 if (i->value == EXT4_ZERO_XATTR_VALUE) {
670                                         memset(val, 0, size);
671                                 } else {
672                                         /* Clear pad bytes first. */
673                                         memset(val + size - EXT4_XATTR_PAD, 0,
674                                                EXT4_XATTR_PAD);
675                                         memcpy(val, i->value, i->value_len);
676                                 }
677                                 return 0;
678                         }
679
680                         /* Remove the old value. */
681                         memmove(first_val + size, first_val, val - first_val);
682                         memset(first_val, 0, size);
683                         s->here->e_value_size = 0;
684                         s->here->e_value_offs = 0;
685                         min_offs += size;
686
687                         /* Adjust all value offsets. */
688                         last = s->first;
689                         while (!IS_LAST_ENTRY(last)) {
690                                 size_t o = le16_to_cpu(last->e_value_offs);
691                                 if (!last->e_value_block &&
692                                     last->e_value_size && o < offs)
693                                         last->e_value_offs =
694                                                 cpu_to_le16(o + size);
695                                 last = EXT4_XATTR_NEXT(last);
696                         }
697                 }
698                 if (!i->value) {
699                         /* Remove the old name. */
700                         size_t size = EXT4_XATTR_LEN(name_len);
701                         last = ENTRY((void *)last - size);
702                         memmove(s->here, (void *)s->here + size,
703                                 (void *)last - (void *)s->here + sizeof(__u32));
704                         memset(last, 0, size);
705                 }
706         }
707
708         if (i->value) {
709                 /* Insert the new value. */
710                 s->here->e_value_size = cpu_to_le32(i->value_len);
711                 if (i->value_len) {
712                         size_t size = EXT4_XATTR_SIZE(i->value_len);
713                         void *val = s->base + min_offs - size;
714                         s->here->e_value_offs = cpu_to_le16(min_offs - size);
715                         if (i->value == EXT4_ZERO_XATTR_VALUE) {
716                                 memset(val, 0, size);
717                         } else {
718                                 /* Clear the pad bytes first. */
719                                 memset(val + size - EXT4_XATTR_PAD, 0,
720                                        EXT4_XATTR_PAD);
721                                 memcpy(val, i->value, i->value_len);
722                         }
723                 }
724         }
725         return 0;
726 }
727
728 struct ext4_xattr_block_find {
729         struct ext4_xattr_search s;
730         struct buffer_head *bh;
731 };
732
733 static int
734 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
735                       struct ext4_xattr_block_find *bs)
736 {
737         struct super_block *sb = inode->i_sb;
738         int error;
739
740         ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
741                   i->name_index, i->name, i->value, (long)i->value_len);
742
743         if (EXT4_I(inode)->i_file_acl) {
744                 /* The inode already has an extended attribute block. */
745                 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
746                 error = -EIO;
747                 if (!bs->bh)
748                         goto cleanup;
749                 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
750                         atomic_read(&(bs->bh->b_count)),
751                         le32_to_cpu(BHDR(bs->bh)->h_refcount));
752                 if (ext4_xattr_check_block(inode, bs->bh)) {
753                         EXT4_ERROR_INODE(inode, "bad block %llu",
754                                          EXT4_I(inode)->i_file_acl);
755                         error = -EIO;
756                         goto cleanup;
757                 }
758                 /* Find the named attribute. */
759                 bs->s.base = BHDR(bs->bh);
760                 bs->s.first = BFIRST(bs->bh);
761                 bs->s.end = bs->bh->b_data + bs->bh->b_size;
762                 bs->s.here = bs->s.first;
763                 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
764                                               i->name, bs->bh->b_size, 1);
765                 if (error && error != -ENODATA)
766                         goto cleanup;
767                 bs->s.not_found = error;
768         }
769         error = 0;
770
771 cleanup:
772         return error;
773 }
774
775 static int
776 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
777                      struct ext4_xattr_info *i,
778                      struct ext4_xattr_block_find *bs)
779 {
780         struct super_block *sb = inode->i_sb;
781         struct buffer_head *new_bh = NULL;
782         struct ext4_xattr_search *s = &bs->s;
783         struct mb_cache_entry *ce = NULL;
784         int error = 0;
785         struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
786
787 #define header(x) ((struct ext4_xattr_header *)(x))
788
789         if (i->value && i->value_len > sb->s_blocksize)
790                 return -ENOSPC;
791         if (s->base) {
792                 ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
793                                         bs->bh->b_blocknr);
794                 BUFFER_TRACE(bs->bh, "get_write_access");
795                 error = ext4_journal_get_write_access(handle, bs->bh);
796                 if (error)
797                         goto cleanup;
798                 lock_buffer(bs->bh);
799
800                 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
801                         if (ce) {
802                                 mb_cache_entry_free(ce);
803                                 ce = NULL;
804                         }
805                         ea_bdebug(bs->bh, "modifying in-place");
806                         error = ext4_xattr_set_entry(i, s);
807                         if (!error) {
808                                 if (!IS_LAST_ENTRY(s->first))
809                                         ext4_xattr_rehash(header(s->base),
810                                                           s->here);
811                                 ext4_xattr_cache_insert(ext4_mb_cache,
812                                         bs->bh);
813                         }
814                         unlock_buffer(bs->bh);
815                         if (error == -EIO)
816                                 goto bad_block;
817                         if (!error)
818                                 error = ext4_handle_dirty_xattr_block(handle,
819                                                                       inode,
820                                                                       bs->bh);
821                         if (error)
822                                 goto cleanup;
823                         goto inserted;
824                 } else {
825                         int offset = (char *)s->here - bs->bh->b_data;
826
827                         unlock_buffer(bs->bh);
828                         if (ce) {
829                                 mb_cache_entry_release(ce);
830                                 ce = NULL;
831                         }
832                         ea_bdebug(bs->bh, "cloning");
833                         s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
834                         error = -ENOMEM;
835                         if (s->base == NULL)
836                                 goto cleanup;
837                         memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
838                         s->first = ENTRY(header(s->base)+1);
839                         header(s->base)->h_refcount = cpu_to_le32(1);
840                         s->here = ENTRY(s->base + offset);
841                         s->end = s->base + bs->bh->b_size;
842                 }
843         } else {
844                 /* Allocate a buffer where we construct the new block. */
845                 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
846                 /* assert(header == s->base) */
847                 error = -ENOMEM;
848                 if (s->base == NULL)
849                         goto cleanup;
850                 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
851                 header(s->base)->h_blocks = cpu_to_le32(1);
852                 header(s->base)->h_refcount = cpu_to_le32(1);
853                 s->first = ENTRY(header(s->base)+1);
854                 s->here = ENTRY(header(s->base)+1);
855                 s->end = s->base + sb->s_blocksize;
856         }
857
858         error = ext4_xattr_set_entry(i, s);
859         if (error == -EIO)
860                 goto bad_block;
861         if (error)
862                 goto cleanup;
863         if (!IS_LAST_ENTRY(s->first))
864                 ext4_xattr_rehash(header(s->base), s->here);
865
866 inserted:
867         if (!IS_LAST_ENTRY(s->first)) {
868                 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
869                 if (new_bh) {
870                         /* We found an identical block in the cache. */
871                         if (new_bh == bs->bh)
872                                 ea_bdebug(new_bh, "keeping");
873                         else {
874                                 /* The old block is released after updating
875                                    the inode. */
876                                 error = dquot_alloc_block(inode,
877                                                 EXT4_C2B(EXT4_SB(sb), 1));
878                                 if (error)
879                                         goto cleanup;
880                                 BUFFER_TRACE(new_bh, "get_write_access");
881                                 error = ext4_journal_get_write_access(handle,
882                                                                       new_bh);
883                                 if (error)
884                                         goto cleanup_dquot;
885                                 lock_buffer(new_bh);
886                                 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
887                                 ea_bdebug(new_bh, "reusing; refcount now=%d",
888                                         le32_to_cpu(BHDR(new_bh)->h_refcount));
889                                 unlock_buffer(new_bh);
890                                 error = ext4_handle_dirty_xattr_block(handle,
891                                                                       inode,
892                                                                       new_bh);
893                                 if (error)
894                                         goto cleanup_dquot;
895                         }
896                         mb_cache_entry_release(ce);
897                         ce = NULL;
898                 } else if (bs->bh && s->base == bs->bh->b_data) {
899                         /* We were modifying this block in-place. */
900                         ea_bdebug(bs->bh, "keeping this block");
901                         new_bh = bs->bh;
902                         get_bh(new_bh);
903                 } else {
904                         /* We need to allocate a new block */
905                         ext4_fsblk_t goal, block;
906
907                         goal = ext4_group_first_block_no(sb,
908                                                 EXT4_I(inode)->i_block_group);
909
910                         /* non-extent files can't have physical blocks past 2^32 */
911                         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
912                                 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
913
914                         block = ext4_new_meta_blocks(handle, inode, goal, 0,
915                                                      NULL, &error);
916                         if (error)
917                                 goto cleanup;
918
919                         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
920                                 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
921
922                         ea_idebug(inode, "creating block %llu",
923                                   (unsigned long long)block);
924
925                         new_bh = sb_getblk(sb, block);
926                         if (unlikely(!new_bh)) {
927                                 error = -ENOMEM;
928 getblk_failed:
929                                 ext4_free_blocks(handle, inode, NULL, block, 1,
930                                                  EXT4_FREE_BLOCKS_METADATA);
931                                 goto cleanup;
932                         }
933                         lock_buffer(new_bh);
934                         error = ext4_journal_get_create_access(handle, new_bh);
935                         if (error) {
936                                 unlock_buffer(new_bh);
937                                 error = -EIO;
938                                 goto getblk_failed;
939                         }
940                         memcpy(new_bh->b_data, s->base, new_bh->b_size);
941                         set_buffer_uptodate(new_bh);
942                         unlock_buffer(new_bh);
943                         ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
944                         error = ext4_handle_dirty_xattr_block(handle,
945                                                               inode, new_bh);
946                         if (error)
947                                 goto cleanup;
948                 }
949         }
950
951         /* Update the inode. */
952         EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
953
954         /* Drop the previous xattr block. */
955         if (bs->bh && bs->bh != new_bh)
956                 ext4_xattr_release_block(handle, inode, bs->bh);
957         error = 0;
958
959 cleanup:
960         if (ce)
961                 mb_cache_entry_release(ce);
962         brelse(new_bh);
963         if (!(bs->bh && s->base == bs->bh->b_data))
964                 kfree(s->base);
965
966         return error;
967
968 cleanup_dquot:
969         dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
970         goto cleanup;
971
972 bad_block:
973         EXT4_ERROR_INODE(inode, "bad block %llu",
974                          EXT4_I(inode)->i_file_acl);
975         goto cleanup;
976
977 #undef header
978 }
979
980 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
981                           struct ext4_xattr_ibody_find *is)
982 {
983         struct ext4_xattr_ibody_header *header;
984         struct ext4_inode *raw_inode;
985         int error;
986
987         if (EXT4_I(inode)->i_extra_isize == 0)
988                 return 0;
989         raw_inode = ext4_raw_inode(&is->iloc);
990         header = IHDR(inode, raw_inode);
991         is->s.base = is->s.first = IFIRST(header);
992         is->s.here = is->s.first;
993         is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
994         if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
995                 error = ext4_xattr_check_names(IFIRST(header), is->s.end,
996                                                IFIRST(header));
997                 if (error)
998                         return error;
999                 /* Find the named attribute. */
1000                 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
1001                                               i->name, is->s.end -
1002                                               (void *)is->s.base, 0);
1003                 if (error && error != -ENODATA)
1004                         return error;
1005                 is->s.not_found = error;
1006         }
1007         return 0;
1008 }
1009
1010 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1011                                 struct ext4_xattr_info *i,
1012                                 struct ext4_xattr_ibody_find *is)
1013 {
1014         struct ext4_xattr_ibody_header *header;
1015         struct ext4_xattr_search *s = &is->s;
1016         int error;
1017
1018         if (EXT4_I(inode)->i_extra_isize == 0)
1019                 return -ENOSPC;
1020         error = ext4_xattr_set_entry(i, s);
1021         if (error) {
1022                 if (error == -ENOSPC &&
1023                     ext4_has_inline_data(inode)) {
1024                         error = ext4_try_to_evict_inline_data(handle, inode,
1025                                         EXT4_XATTR_LEN(strlen(i->name) +
1026                                         EXT4_XATTR_SIZE(i->value_len)));
1027                         if (error)
1028                                 return error;
1029                         error = ext4_xattr_ibody_find(inode, i, is);
1030                         if (error)
1031                                 return error;
1032                         error = ext4_xattr_set_entry(i, s);
1033                 }
1034                 if (error)
1035                         return error;
1036         }
1037         header = IHDR(inode, ext4_raw_inode(&is->iloc));
1038         if (!IS_LAST_ENTRY(s->first)) {
1039                 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1040                 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1041         } else {
1042                 header->h_magic = cpu_to_le32(0);
1043                 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1044         }
1045         return 0;
1046 }
1047
1048 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1049                                 struct ext4_xattr_info *i,
1050                                 struct ext4_xattr_ibody_find *is)
1051 {
1052         struct ext4_xattr_ibody_header *header;
1053         struct ext4_xattr_search *s = &is->s;
1054         int error;
1055
1056         if (EXT4_I(inode)->i_extra_isize == 0)
1057                 return -ENOSPC;
1058         error = ext4_xattr_set_entry(i, s);
1059         if (error)
1060                 return error;
1061         header = IHDR(inode, ext4_raw_inode(&is->iloc));
1062         if (!IS_LAST_ENTRY(s->first)) {
1063                 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1064                 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1065         } else {
1066                 header->h_magic = cpu_to_le32(0);
1067                 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1068         }
1069         return 0;
1070 }
1071
1072 /*
1073  * ext4_xattr_set_handle()
1074  *
1075  * Create, replace or remove an extended attribute for this inode.  Value
1076  * is NULL to remove an existing extended attribute, and non-NULL to
1077  * either replace an existing extended attribute, or create a new extended
1078  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1079  * specify that an extended attribute must exist and must not exist
1080  * previous to the call, respectively.
1081  *
1082  * Returns 0, or a negative error number on failure.
1083  */
1084 int
1085 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
1086                       const char *name, const void *value, size_t value_len,
1087                       int flags)
1088 {
1089         struct ext4_xattr_info i = {
1090                 .name_index = name_index,
1091                 .name = name,
1092                 .value = value,
1093                 .value_len = value_len,
1094
1095         };
1096         struct ext4_xattr_ibody_find is = {
1097                 .s = { .not_found = -ENODATA, },
1098         };
1099         struct ext4_xattr_block_find bs = {
1100                 .s = { .not_found = -ENODATA, },
1101         };
1102         unsigned long no_expand;
1103         int error;
1104
1105         if (!name)
1106                 return -EINVAL;
1107         if (strlen(name) > 255)
1108                 return -ERANGE;
1109         down_write(&EXT4_I(inode)->xattr_sem);
1110         no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1111         ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1112
1113         error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1114         if (error)
1115                 goto cleanup;
1116
1117         if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1118                 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1119                 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1120                 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1121         }
1122
1123         error = ext4_xattr_ibody_find(inode, &i, &is);
1124         if (error)
1125                 goto cleanup;
1126         if (is.s.not_found)
1127                 error = ext4_xattr_block_find(inode, &i, &bs);
1128         if (error)
1129                 goto cleanup;
1130         if (is.s.not_found && bs.s.not_found) {
1131                 error = -ENODATA;
1132                 if (flags & XATTR_REPLACE)
1133                         goto cleanup;
1134                 error = 0;
1135                 if (!value)
1136                         goto cleanup;
1137         } else {
1138                 error = -EEXIST;
1139                 if (flags & XATTR_CREATE)
1140                         goto cleanup;
1141         }
1142         if (!value) {
1143                 if (!is.s.not_found)
1144                         error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1145                 else if (!bs.s.not_found)
1146                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
1147         } else {
1148                 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1149                 if (!error && !bs.s.not_found) {
1150                         i.value = NULL;
1151                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
1152                 } else if (error == -ENOSPC) {
1153                         if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1154                                 error = ext4_xattr_block_find(inode, &i, &bs);
1155                                 if (error)
1156                                         goto cleanup;
1157                         }
1158                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
1159                         if (error)
1160                                 goto cleanup;
1161                         if (!is.s.not_found) {
1162                                 i.value = NULL;
1163                                 error = ext4_xattr_ibody_set(handle, inode, &i,
1164                                                              &is);
1165                         }
1166                 }
1167         }
1168         if (!error) {
1169                 ext4_xattr_update_super_block(handle, inode->i_sb);
1170                 inode->i_ctime = ext4_current_time(inode);
1171                 if (!value)
1172                         ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1173                 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1174                 /*
1175                  * The bh is consumed by ext4_mark_iloc_dirty, even with
1176                  * error != 0.
1177                  */
1178                 is.iloc.bh = NULL;
1179                 if (IS_SYNC(inode))
1180                         ext4_handle_sync(handle);
1181         }
1182
1183 cleanup:
1184         brelse(is.iloc.bh);
1185         brelse(bs.bh);
1186         if (no_expand == 0)
1187                 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1188         up_write(&EXT4_I(inode)->xattr_sem);
1189         return error;
1190 }
1191
1192 /*
1193  * ext4_xattr_set()
1194  *
1195  * Like ext4_xattr_set_handle, but start from an inode. This extended
1196  * attribute modification is a filesystem transaction by itself.
1197  *
1198  * Returns 0, or a negative error number on failure.
1199  */
1200 int
1201 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1202                const void *value, size_t value_len, int flags)
1203 {
1204         handle_t *handle;
1205         int error, retries = 0;
1206         int credits = ext4_jbd2_credits_xattr(inode);
1207
1208 retry:
1209         handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
1210         if (IS_ERR(handle)) {
1211                 error = PTR_ERR(handle);
1212         } else {
1213                 int error2;
1214
1215                 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1216                                               value, value_len, flags);
1217                 error2 = ext4_journal_stop(handle);
1218                 if (error == -ENOSPC &&
1219                     ext4_should_retry_alloc(inode->i_sb, &retries))
1220                         goto retry;
1221                 if (error == 0)
1222                         error = error2;
1223         }
1224
1225         return error;
1226 }
1227
1228 /*
1229  * Shift the EA entries in the inode to create space for the increased
1230  * i_extra_isize.
1231  */
1232 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1233                                      int value_offs_shift, void *to,
1234                                      void *from, size_t n, int blocksize)
1235 {
1236         struct ext4_xattr_entry *last = entry;
1237         int new_offs;
1238
1239         /* Adjust the value offsets of the entries */
1240         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1241                 if (!last->e_value_block && last->e_value_size) {
1242                         new_offs = le16_to_cpu(last->e_value_offs) +
1243                                                         value_offs_shift;
1244                         BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1245                                  > blocksize);
1246                         last->e_value_offs = cpu_to_le16(new_offs);
1247                 }
1248         }
1249         /* Shift the entries by n bytes */
1250         memmove(to, from, n);
1251 }
1252
1253 /*
1254  * Expand an inode by new_extra_isize bytes when EAs are present.
1255  * Returns 0 on success or negative error number on failure.
1256  */
1257 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1258                                struct ext4_inode *raw_inode, handle_t *handle)
1259 {
1260         struct ext4_xattr_ibody_header *header;
1261         struct ext4_xattr_entry *entry, *last, *first;
1262         struct buffer_head *bh = NULL;
1263         struct ext4_xattr_ibody_find *is = NULL;
1264         struct ext4_xattr_block_find *bs = NULL;
1265         char *buffer = NULL, *b_entry_name = NULL;
1266         size_t min_offs, free;
1267         int total_ino;
1268         void *base, *start, *end;
1269         int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
1270         int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1271
1272         down_write(&EXT4_I(inode)->xattr_sem);
1273 retry:
1274         if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1275                 up_write(&EXT4_I(inode)->xattr_sem);
1276                 return 0;
1277         }
1278
1279         header = IHDR(inode, raw_inode);
1280         entry = IFIRST(header);
1281
1282         /*
1283          * Check if enough free space is available in the inode to shift the
1284          * entries ahead by new_extra_isize.
1285          */
1286
1287         base = start = entry;
1288         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1289         min_offs = end - base;
1290         last = entry;
1291         total_ino = sizeof(struct ext4_xattr_ibody_header);
1292
1293         free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1294         if (free >= new_extra_isize) {
1295                 entry = IFIRST(header);
1296                 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1297                                 - new_extra_isize, (void *)raw_inode +
1298                                 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1299                                 (void *)header, total_ino,
1300                                 inode->i_sb->s_blocksize);
1301                 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1302                 error = 0;
1303                 goto cleanup;
1304         }
1305
1306         /*
1307          * Enough free space isn't available in the inode, check if
1308          * EA block can hold new_extra_isize bytes.
1309          */
1310         if (EXT4_I(inode)->i_file_acl) {
1311                 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1312                 error = -EIO;
1313                 if (!bh)
1314                         goto cleanup;
1315                 if (ext4_xattr_check_block(inode, bh)) {
1316                         EXT4_ERROR_INODE(inode, "bad block %llu",
1317                                          EXT4_I(inode)->i_file_acl);
1318                         error = -EIO;
1319                         goto cleanup;
1320                 }
1321                 base = BHDR(bh);
1322                 first = BFIRST(bh);
1323                 end = bh->b_data + bh->b_size;
1324                 min_offs = end - base;
1325                 free = ext4_xattr_free_space(first, &min_offs, base, NULL);
1326                 if (free < new_extra_isize) {
1327                         if (!tried_min_extra_isize && s_min_extra_isize) {
1328                                 tried_min_extra_isize++;
1329                                 new_extra_isize = s_min_extra_isize;
1330                                 brelse(bh);
1331                                 goto retry;
1332                         }
1333                         error = -1;
1334                         goto cleanup;
1335                 }
1336         } else {
1337                 free = inode->i_sb->s_blocksize;
1338         }
1339
1340         while (new_extra_isize > 0) {
1341                 size_t offs, size, entry_size;
1342                 struct ext4_xattr_entry *small_entry = NULL;
1343                 struct ext4_xattr_info i = {
1344                         .value = NULL,
1345                         .value_len = 0,
1346                 };
1347                 unsigned int total_size;  /* EA entry size + value size */
1348                 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1349                 unsigned int min_total_size = ~0U;
1350
1351                 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1352                 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1353                 if (!is || !bs) {
1354                         error = -ENOMEM;
1355                         goto cleanup;
1356                 }
1357
1358                 is->s.not_found = -ENODATA;
1359                 bs->s.not_found = -ENODATA;
1360                 is->iloc.bh = NULL;
1361                 bs->bh = NULL;
1362
1363                 last = IFIRST(header);
1364                 /* Find the entry best suited to be pushed into EA block */
1365                 entry = NULL;
1366                 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1367                         total_size =
1368                         EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1369                                         EXT4_XATTR_LEN(last->e_name_len);
1370                         if (total_size <= free && total_size < min_total_size) {
1371                                 if (total_size < new_extra_isize) {
1372                                         small_entry = last;
1373                                 } else {
1374                                         entry = last;
1375                                         min_total_size = total_size;
1376                                 }
1377                         }
1378                 }
1379
1380                 if (entry == NULL) {
1381                         if (small_entry) {
1382                                 entry = small_entry;
1383                         } else {
1384                                 if (!tried_min_extra_isize &&
1385                                     s_min_extra_isize) {
1386                                         tried_min_extra_isize++;
1387                                         new_extra_isize = s_min_extra_isize;
1388                                         kfree(is); is = NULL;
1389                                         kfree(bs); bs = NULL;
1390                                         brelse(bh);
1391                                         goto retry;
1392                                 }
1393                                 error = -1;
1394                                 goto cleanup;
1395                         }
1396                 }
1397                 offs = le16_to_cpu(entry->e_value_offs);
1398                 size = le32_to_cpu(entry->e_value_size);
1399                 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1400                 i.name_index = entry->e_name_index,
1401                 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1402                 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1403                 if (!buffer || !b_entry_name) {
1404                         error = -ENOMEM;
1405                         goto cleanup;
1406                 }
1407                 /* Save the entry name and the entry value */
1408                 memcpy(buffer, (void *)IFIRST(header) + offs,
1409                        EXT4_XATTR_SIZE(size));
1410                 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1411                 b_entry_name[entry->e_name_len] = '\0';
1412                 i.name = b_entry_name;
1413
1414                 error = ext4_get_inode_loc(inode, &is->iloc);
1415                 if (error)
1416                         goto cleanup;
1417
1418                 error = ext4_xattr_ibody_find(inode, &i, is);
1419                 if (error)
1420                         goto cleanup;
1421
1422                 /* Remove the chosen entry from the inode */
1423                 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1424                 if (error)
1425                         goto cleanup;
1426
1427                 entry = IFIRST(header);
1428                 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1429                         shift_bytes = new_extra_isize;
1430                 else
1431                         shift_bytes = entry_size + size;
1432                 /* Adjust the offsets and shift the remaining entries ahead */
1433                 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1434                         shift_bytes, (void *)raw_inode +
1435                         EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1436                         (void *)header, total_ino - entry_size,
1437                         inode->i_sb->s_blocksize);
1438
1439                 extra_isize += shift_bytes;
1440                 new_extra_isize -= shift_bytes;
1441                 EXT4_I(inode)->i_extra_isize = extra_isize;
1442
1443                 i.name = b_entry_name;
1444                 i.value = buffer;
1445                 i.value_len = size;
1446                 error = ext4_xattr_block_find(inode, &i, bs);
1447                 if (error)
1448                         goto cleanup;
1449
1450                 /* Add entry which was removed from the inode into the block */
1451                 error = ext4_xattr_block_set(handle, inode, &i, bs);
1452                 if (error)
1453                         goto cleanup;
1454                 kfree(b_entry_name);
1455                 kfree(buffer);
1456                 b_entry_name = NULL;
1457                 buffer = NULL;
1458                 brelse(is->iloc.bh);
1459                 kfree(is);
1460                 kfree(bs);
1461         }
1462         brelse(bh);
1463         up_write(&EXT4_I(inode)->xattr_sem);
1464         return 0;
1465
1466 cleanup:
1467         kfree(b_entry_name);
1468         kfree(buffer);
1469         if (is)
1470                 brelse(is->iloc.bh);
1471         kfree(is);
1472         kfree(bs);
1473         brelse(bh);
1474         up_write(&EXT4_I(inode)->xattr_sem);
1475         return error;
1476 }
1477
1478
1479
1480 /*
1481  * ext4_xattr_delete_inode()
1482  *
1483  * Free extended attribute resources associated with this inode. This
1484  * is called immediately before an inode is freed. We have exclusive
1485  * access to the inode.
1486  */
1487 void
1488 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1489 {
1490         struct buffer_head *bh = NULL;
1491
1492         if (!EXT4_I(inode)->i_file_acl)
1493                 goto cleanup;
1494         bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1495         if (!bh) {
1496                 EXT4_ERROR_INODE(inode, "block %llu read error",
1497                                  EXT4_I(inode)->i_file_acl);
1498                 goto cleanup;
1499         }
1500         if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1501             BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1502                 EXT4_ERROR_INODE(inode, "bad block %llu",
1503                                  EXT4_I(inode)->i_file_acl);
1504                 goto cleanup;
1505         }
1506         ext4_xattr_release_block(handle, inode, bh);
1507         EXT4_I(inode)->i_file_acl = 0;
1508
1509 cleanup:
1510         brelse(bh);
1511 }
1512
1513 /*
1514  * ext4_xattr_put_super()
1515  *
1516  * This is called when a file system is unmounted.
1517  */
1518 void
1519 ext4_xattr_put_super(struct super_block *sb)
1520 {
1521         mb_cache_shrink(sb->s_bdev);
1522 }
1523
1524 /*
1525  * ext4_xattr_cache_insert()
1526  *
1527  * Create a new entry in the extended attribute cache, and insert
1528  * it unless such an entry is already in the cache.
1529  *
1530  * Returns 0, or a negative error number on failure.
1531  */
1532 static void
1533 ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
1534 {
1535         __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1536         struct mb_cache_entry *ce;
1537         int error;
1538
1539         ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
1540         if (!ce) {
1541                 ea_bdebug(bh, "out of memory");
1542                 return;
1543         }
1544         error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1545         if (error) {
1546                 mb_cache_entry_free(ce);
1547                 if (error == -EBUSY) {
1548                         ea_bdebug(bh, "already in cache");
1549                         error = 0;
1550                 }
1551         } else {
1552                 ea_bdebug(bh, "inserting [%x]", (int)hash);
1553                 mb_cache_entry_release(ce);
1554         }
1555 }
1556
1557 /*
1558  * ext4_xattr_cmp()
1559  *
1560  * Compare two extended attribute blocks for equality.
1561  *
1562  * Returns 0 if the blocks are equal, 1 if they differ, and
1563  * a negative error number on errors.
1564  */
1565 static int
1566 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1567                struct ext4_xattr_header *header2)
1568 {
1569         struct ext4_xattr_entry *entry1, *entry2;
1570
1571         entry1 = ENTRY(header1+1);
1572         entry2 = ENTRY(header2+1);
1573         while (!IS_LAST_ENTRY(entry1)) {
1574                 if (IS_LAST_ENTRY(entry2))
1575                         return 1;
1576                 if (entry1->e_hash != entry2->e_hash ||
1577                     entry1->e_name_index != entry2->e_name_index ||
1578                     entry1->e_name_len != entry2->e_name_len ||
1579                     entry1->e_value_size != entry2->e_value_size ||
1580                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1581                         return 1;
1582                 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1583                         return -EIO;
1584                 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1585                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1586                            le32_to_cpu(entry1->e_value_size)))
1587                         return 1;
1588
1589                 entry1 = EXT4_XATTR_NEXT(entry1);
1590                 entry2 = EXT4_XATTR_NEXT(entry2);
1591         }
1592         if (!IS_LAST_ENTRY(entry2))
1593                 return 1;
1594         return 0;
1595 }
1596
1597 /*
1598  * ext4_xattr_cache_find()
1599  *
1600  * Find an identical extended attribute block.
1601  *
1602  * Returns a pointer to the block found, or NULL if such a block was
1603  * not found or an error occurred.
1604  */
1605 static struct buffer_head *
1606 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1607                       struct mb_cache_entry **pce)
1608 {
1609         __u32 hash = le32_to_cpu(header->h_hash);
1610         struct mb_cache_entry *ce;
1611         struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
1612
1613         if (!header->h_hash)
1614                 return NULL;  /* never share */
1615         ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1616 again:
1617         ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
1618                                        hash);
1619         while (ce) {
1620                 struct buffer_head *bh;
1621
1622                 if (IS_ERR(ce)) {
1623                         if (PTR_ERR(ce) == -EAGAIN)
1624                                 goto again;
1625                         break;
1626                 }
1627                 bh = sb_bread(inode->i_sb, ce->e_block);
1628                 if (!bh) {
1629                         EXT4_ERROR_INODE(inode, "block %lu read error",
1630                                          (unsigned long) ce->e_block);
1631                 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1632                                 EXT4_XATTR_REFCOUNT_MAX) {
1633                         ea_idebug(inode, "block %lu refcount %d>=%d",
1634                                   (unsigned long) ce->e_block,
1635                                   le32_to_cpu(BHDR(bh)->h_refcount),
1636                                           EXT4_XATTR_REFCOUNT_MAX);
1637                 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1638                         *pce = ce;
1639                         return bh;
1640                 }
1641                 brelse(bh);
1642                 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1643         }
1644         return NULL;
1645 }
1646
1647 #define NAME_HASH_SHIFT 5
1648 #define VALUE_HASH_SHIFT 16
1649
1650 /*
1651  * ext4_xattr_hash_entry()
1652  *
1653  * Compute the hash of an extended attribute.
1654  */
1655 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1656                                          struct ext4_xattr_entry *entry)
1657 {
1658         __u32 hash = 0;
1659         char *name = entry->e_name;
1660         int n;
1661
1662         for (n = 0; n < entry->e_name_len; n++) {
1663                 hash = (hash << NAME_HASH_SHIFT) ^
1664                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1665                        *name++;
1666         }
1667
1668         if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1669                 __le32 *value = (__le32 *)((char *)header +
1670                         le16_to_cpu(entry->e_value_offs));
1671                 for (n = (le32_to_cpu(entry->e_value_size) +
1672                      EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1673                         hash = (hash << VALUE_HASH_SHIFT) ^
1674                                (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1675                                le32_to_cpu(*value++);
1676                 }
1677         }
1678         entry->e_hash = cpu_to_le32(hash);
1679 }
1680
1681 #undef NAME_HASH_SHIFT
1682 #undef VALUE_HASH_SHIFT
1683
1684 #define BLOCK_HASH_SHIFT 16
1685
1686 /*
1687  * ext4_xattr_rehash()
1688  *
1689  * Re-compute the extended attribute hash value after an entry has changed.
1690  */
1691 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1692                               struct ext4_xattr_entry *entry)
1693 {
1694         struct ext4_xattr_entry *here;
1695         __u32 hash = 0;
1696
1697         ext4_xattr_hash_entry(header, entry);
1698         here = ENTRY(header+1);
1699         while (!IS_LAST_ENTRY(here)) {
1700                 if (!here->e_hash) {
1701                         /* Block is not shared if an entry's hash value == 0 */
1702                         hash = 0;
1703                         break;
1704                 }
1705                 hash = (hash << BLOCK_HASH_SHIFT) ^
1706                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1707                        le32_to_cpu(here->e_hash);
1708                 here = EXT4_XATTR_NEXT(here);
1709         }
1710         header->h_hash = cpu_to_le32(hash);
1711 }
1712
1713 #undef BLOCK_HASH_SHIFT
1714
1715 #define HASH_BUCKET_BITS        10
1716
1717 struct mb_cache *
1718 ext4_xattr_create_cache(char *name)
1719 {
1720         return mb_cache_create(name, HASH_BUCKET_BITS);
1721 }
1722
1723 void ext4_xattr_destroy_cache(struct mb_cache *cache)
1724 {
1725         if (cache)
1726                 mb_cache_destroy(cache);
1727 }
1728