1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/ext4/xattr.c
4 *
5 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 *
7 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
8 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
9 * Extended attributes for symlinks and special files added per
10 * suggestion of Luka Renko <luka.renko@hermes.si>.
11 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
12 * Red Hat Inc.
13 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
14 * and Andreas Gruenbacher <agruen@suse.de>.
15 */
16
17 /*
18 * Extended attributes are stored directly in inodes (on file systems with
19 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
20 * field contains the block number if an inode uses an additional block. All
21 * attributes must fit in the inode and one additional block. Blocks that
22 * contain the identical set of attributes may be shared among several inodes.
23 * Identical blocks are detected by keeping a cache of blocks that have
24 * recently been accessed.
25 *
26 * The attributes in inodes and on blocks have a different header; the entries
27 * are stored in the same format:
28 *
29 * +------------------+
30 * | header |
31 * | entry 1 | |
32 * | entry 2 | | growing downwards
33 * | entry 3 | v
34 * | four null bytes |
35 * | . . . |
36 * | value 1 | ^
37 * | value 3 | | growing upwards
38 * | value 2 | |
39 * +------------------+
40 *
41 * The header is followed by multiple entry descriptors. In disk blocks, the
42 * entry descriptors are kept sorted. In inodes, they are unsorted. The
43 * attribute values are aligned to the end of the block in no specific order.
44 *
45 * Locking strategy
46 * ----------------
47 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
48 * EA blocks are only changed if they are exclusive to an inode, so
49 * holding xattr_sem also means that nothing but the EA block's reference
50 * count can change. Multiple writers to the same block are synchronized
51 * by the buffer lock.
52 */
53
54 #include <linux/init.h>
55 #include <linux/fs.h>
56 #include <linux/slab.h>
57 #include <linux/mbcache.h>
58 #include <linux/quotaops.h>
59 #include <linux/iversion.h>
60 #include "ext4_jbd2.h"
61 #include "ext4.h"
62 #include "xattr.h"
63 #include "acl.h"
64
65 #ifdef EXT4_XATTR_DEBUG
66 # define ea_idebug(inode, fmt, ...) \
67 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
68 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
69 # define ea_bdebug(bh, fmt, ...) \
70 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
71 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
72 #else
73 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
74 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
75 #endif
76
77 static void ext4_xattr_block_cache_insert(struct mb_cache *,
78 struct buffer_head *);
79 static struct buffer_head *
80 ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
81 struct mb_cache_entry **);
82 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
83 size_t value_count);
84 static void ext4_xattr_rehash(struct ext4_xattr_header *);
85
86 static const struct xattr_handler * const ext4_xattr_handler_map[] = {
87 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
88 #ifdef CONFIG_EXT4_FS_POSIX_ACL
89 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
90 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
91 #endif
92 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
93 #ifdef CONFIG_EXT4_FS_SECURITY
94 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
95 #endif
96 [EXT4_XATTR_INDEX_HURD] = &ext4_xattr_hurd_handler,
97 };
98
99 const struct xattr_handler *ext4_xattr_handlers[] = {
100 &ext4_xattr_user_handler,
101 &ext4_xattr_trusted_handler,
102 #ifdef CONFIG_EXT4_FS_POSIX_ACL
103 &posix_acl_access_xattr_handler,
104 &posix_acl_default_xattr_handler,
105 #endif
106 #ifdef CONFIG_EXT4_FS_SECURITY
107 &ext4_xattr_security_handler,
108 #endif
109 &ext4_xattr_hurd_handler,
110 NULL
111 };
112
113 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
114 inode->i_sb->s_fs_info)->s_ea_block_cache)
115
116 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
117 inode->i_sb->s_fs_info)->s_ea_inode_cache)
118
119 static int
120 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
121 struct inode *inode);
122
123 #ifdef CONFIG_LOCKDEP
ext4_xattr_inode_set_class(struct inode * ea_inode)124 void ext4_xattr_inode_set_class(struct inode *ea_inode)
125 {
126 struct ext4_inode_info *ei = EXT4_I(ea_inode);
127
128 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
129 (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
130 lockdep_set_subclass(&ei->i_data_sem, I_DATA_SEM_EA);
131 }
132 #endif
133
ext4_xattr_block_csum(struct inode * inode,sector_t block_nr,struct ext4_xattr_header * hdr)134 static __le32 ext4_xattr_block_csum(struct inode *inode,
135 sector_t block_nr,
136 struct ext4_xattr_header *hdr)
137 {
138 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
139 __u32 csum;
140 __le64 dsk_block_nr = cpu_to_le64(block_nr);
141 __u32 dummy_csum = 0;
142 int offset = offsetof(struct ext4_xattr_header, h_checksum);
143
144 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
145 sizeof(dsk_block_nr));
146 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
147 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
148 offset += sizeof(dummy_csum);
149 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
150 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
151
152 return cpu_to_le32(csum);
153 }
154
ext4_xattr_block_csum_verify(struct inode * inode,struct buffer_head * bh)155 static int ext4_xattr_block_csum_verify(struct inode *inode,
156 struct buffer_head *bh)
157 {
158 struct ext4_xattr_header *hdr = BHDR(bh);
159 int ret = 1;
160
161 if (ext4_has_metadata_csum(inode->i_sb)) {
162 lock_buffer(bh);
163 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
164 bh->b_blocknr, hdr));
165 unlock_buffer(bh);
166 }
167 return ret;
168 }
169
ext4_xattr_block_csum_set(struct inode * inode,struct buffer_head * bh)170 static void ext4_xattr_block_csum_set(struct inode *inode,
171 struct buffer_head *bh)
172 {
173 if (ext4_has_metadata_csum(inode->i_sb))
174 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
175 bh->b_blocknr, BHDR(bh));
176 }
177
178 static inline const struct xattr_handler *
ext4_xattr_handler(int name_index)179 ext4_xattr_handler(int name_index)
180 {
181 const struct xattr_handler *handler = NULL;
182
183 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
184 handler = ext4_xattr_handler_map[name_index];
185 return handler;
186 }
187
188 static int
ext4_xattr_check_entries(struct ext4_xattr_entry * entry,void * end,void * value_start)189 ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
190 void *value_start)
191 {
192 struct ext4_xattr_entry *e = entry;
193
194 /* Find the end of the names list */
195 while (!IS_LAST_ENTRY(e)) {
196 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
197 if ((void *)next >= end)
198 return -EFSCORRUPTED;
199 if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
200 return -EFSCORRUPTED;
201 e = next;
202 }
203
204 /* Check the values */
205 while (!IS_LAST_ENTRY(entry)) {
206 u32 size = le32_to_cpu(entry->e_value_size);
207
208 if (size > EXT4_XATTR_SIZE_MAX)
209 return -EFSCORRUPTED;
210
211 if (size != 0 && entry->e_value_inum == 0) {
212 u16 offs = le16_to_cpu(entry->e_value_offs);
213 void *value;
214
215 /*
216 * The value cannot overlap the names, and the value
217 * with padding cannot extend beyond 'end'. Check both
218 * the padded and unpadded sizes, since the size may
219 * overflow to 0 when adding padding.
220 */
221 if (offs > end - value_start)
222 return -EFSCORRUPTED;
223 value = value_start + offs;
224 if (value < (void *)e + sizeof(u32) ||
225 size > end - value ||
226 EXT4_XATTR_SIZE(size) > end - value)
227 return -EFSCORRUPTED;
228 }
229 entry = EXT4_XATTR_NEXT(entry);
230 }
231
232 return 0;
233 }
234
235 static inline int
__ext4_xattr_check_block(struct inode * inode,struct buffer_head * bh,const char * function,unsigned int line)236 __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
237 const char *function, unsigned int line)
238 {
239 int error = -EFSCORRUPTED;
240
241 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
242 BHDR(bh)->h_blocks != cpu_to_le32(1))
243 goto errout;
244 if (buffer_verified(bh))
245 return 0;
246
247 error = -EFSBADCRC;
248 if (!ext4_xattr_block_csum_verify(inode, bh))
249 goto errout;
250 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
251 bh->b_data);
252 errout:
253 if (error)
254 __ext4_error_inode(inode, function, line, 0, -error,
255 "corrupted xattr block %llu",
256 (unsigned long long) bh->b_blocknr);
257 else
258 set_buffer_verified(bh);
259 return error;
260 }
261
262 #define ext4_xattr_check_block(inode, bh) \
263 __ext4_xattr_check_block((inode), (bh), __func__, __LINE__)
264
265
266 static int
__xattr_check_inode(struct inode * inode,struct ext4_xattr_ibody_header * header,void * end,const char * function,unsigned int line)267 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
268 void *end, const char *function, unsigned int line)
269 {
270 int error = -EFSCORRUPTED;
271
272 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
273 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
274 goto errout;
275 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
276 errout:
277 if (error)
278 __ext4_error_inode(inode, function, line, 0, -error,
279 "corrupted in-inode xattr");
280 return error;
281 }
282
283 #define xattr_check_inode(inode, header, end) \
284 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
285
286 static int
xattr_find_entry(struct inode * inode,struct ext4_xattr_entry ** pentry,void * end,int name_index,const char * name,int sorted)287 xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
288 void *end, int name_index, const char *name, int sorted)
289 {
290 struct ext4_xattr_entry *entry, *next;
291 size_t name_len;
292 int cmp = 1;
293
294 if (name == NULL)
295 return -EINVAL;
296 name_len = strlen(name);
297 for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
298 next = EXT4_XATTR_NEXT(entry);
299 if ((void *) next >= end) {
300 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
301 return -EFSCORRUPTED;
302 }
303 cmp = name_index - entry->e_name_index;
304 if (!cmp)
305 cmp = name_len - entry->e_name_len;
306 if (!cmp)
307 cmp = memcmp(name, entry->e_name, name_len);
308 if (cmp <= 0 && (sorted || cmp == 0))
309 break;
310 }
311 *pentry = entry;
312 return cmp ? -ENODATA : 0;
313 }
314
315 static u32
ext4_xattr_inode_hash(struct ext4_sb_info * sbi,const void * buffer,size_t size)316 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
317 {
318 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
319 }
320
ext4_xattr_inode_get_ref(struct inode * ea_inode)321 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
322 {
323 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
324 (u32) inode_peek_iversion_raw(ea_inode);
325 }
326
ext4_xattr_inode_set_ref(struct inode * ea_inode,u64 ref_count)327 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
328 {
329 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
330 inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
331 }
332
ext4_xattr_inode_get_hash(struct inode * ea_inode)333 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
334 {
335 return (u32)ea_inode->i_atime.tv_sec;
336 }
337
ext4_xattr_inode_set_hash(struct inode * ea_inode,u32 hash)338 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
339 {
340 ea_inode->i_atime.tv_sec = hash;
341 }
342
343 /*
344 * Read the EA value from an inode.
345 */
ext4_xattr_inode_read(struct inode * ea_inode,void * buf,size_t size)346 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
347 {
348 int blocksize = 1 << ea_inode->i_blkbits;
349 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
350 int tail_size = (size % blocksize) ?: blocksize;
351 struct buffer_head *bhs_inline[8];
352 struct buffer_head **bhs = bhs_inline;
353 int i, ret;
354
355 if (bh_count > ARRAY_SIZE(bhs_inline)) {
356 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
357 if (!bhs)
358 return -ENOMEM;
359 }
360
361 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
362 true /* wait */, bhs);
363 if (ret)
364 goto free_bhs;
365
366 for (i = 0; i < bh_count; i++) {
367 /* There shouldn't be any holes in ea_inode. */
368 if (!bhs[i]) {
369 ret = -EFSCORRUPTED;
370 goto put_bhs;
371 }
372 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
373 i < bh_count - 1 ? blocksize : tail_size);
374 }
375 ret = 0;
376 put_bhs:
377 for (i = 0; i < bh_count; i++)
378 brelse(bhs[i]);
379 free_bhs:
380 if (bhs != bhs_inline)
381 kfree(bhs);
382 return ret;
383 }
384
385 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
386
ext4_xattr_inode_iget(struct inode * parent,unsigned long ea_ino,u32 ea_inode_hash,struct inode ** ea_inode)387 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
388 u32 ea_inode_hash, struct inode **ea_inode)
389 {
390 struct inode *inode;
391 int err;
392
393 /*
394 * We have to check for this corruption early as otherwise
395 * iget_locked() could wait indefinitely for the state of our
396 * parent inode.
397 */
398 if (parent->i_ino == ea_ino) {
399 ext4_error(parent->i_sb,
400 "Parent and EA inode have the same ino %lu", ea_ino);
401 return -EFSCORRUPTED;
402 }
403
404 inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_EA_INODE);
405 if (IS_ERR(inode)) {
406 err = PTR_ERR(inode);
407 ext4_error(parent->i_sb,
408 "error while reading EA inode %lu err=%d", ea_ino,
409 err);
410 return err;
411 }
412 ext4_xattr_inode_set_class(inode);
413
414 /*
415 * Check whether this is an old Lustre-style xattr inode. Lustre
416 * implementation does not have hash validation, rather it has a
417 * backpointer from ea_inode to the parent inode.
418 */
419 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
420 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
421 inode->i_generation == parent->i_generation) {
422 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
423 ext4_xattr_inode_set_ref(inode, 1);
424 } else {
425 inode_lock(inode);
426 inode->i_flags |= S_NOQUOTA;
427 inode_unlock(inode);
428 }
429
430 *ea_inode = inode;
431 return 0;
432 }
433
434 /* Remove entry from mbcache when EA inode is getting evicted */
ext4_evict_ea_inode(struct inode * inode)435 void ext4_evict_ea_inode(struct inode *inode)
436 {
437 struct mb_cache_entry *oe;
438
439 if (!EA_INODE_CACHE(inode))
440 return;
441 /* Wait for entry to get unused so that we can remove it */
442 while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode),
443 ext4_xattr_inode_get_hash(inode), inode->i_ino))) {
444 mb_cache_entry_wait_unused(oe);
445 mb_cache_entry_put(EA_INODE_CACHE(inode), oe);
446 }
447 }
448
449 static int
ext4_xattr_inode_verify_hashes(struct inode * ea_inode,struct ext4_xattr_entry * entry,void * buffer,size_t size)450 ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
451 struct ext4_xattr_entry *entry, void *buffer,
452 size_t size)
453 {
454 u32 hash;
455
456 /* Verify stored hash matches calculated hash. */
457 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
458 if (hash != ext4_xattr_inode_get_hash(ea_inode))
459 return -EFSCORRUPTED;
460
461 if (entry) {
462 __le32 e_hash, tmp_data;
463
464 /* Verify entry hash. */
465 tmp_data = cpu_to_le32(hash);
466 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
467 &tmp_data, 1);
468 if (e_hash != entry->e_hash)
469 return -EFSCORRUPTED;
470 }
471 return 0;
472 }
473
474 /*
475 * Read xattr value from the EA inode.
476 */
477 static int
ext4_xattr_inode_get(struct inode * inode,struct ext4_xattr_entry * entry,void * buffer,size_t size)478 ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
479 void *buffer, size_t size)
480 {
481 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
482 struct inode *ea_inode;
483 int err;
484
485 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
486 le32_to_cpu(entry->e_hash), &ea_inode);
487 if (err) {
488 ea_inode = NULL;
489 goto out;
490 }
491
492 if (i_size_read(ea_inode) != size) {
493 ext4_warning_inode(ea_inode,
494 "ea_inode file size=%llu entry size=%zu",
495 i_size_read(ea_inode), size);
496 err = -EFSCORRUPTED;
497 goto out;
498 }
499
500 err = ext4_xattr_inode_read(ea_inode, buffer, size);
501 if (err)
502 goto out;
503
504 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
505 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
506 size);
507 if (err) {
508 ext4_warning_inode(ea_inode,
509 "EA inode hash validation failed");
510 goto out;
511 }
512
513 if (ea_inode_cache)
514 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
515 ext4_xattr_inode_get_hash(ea_inode),
516 ea_inode->i_ino, true /* reusable */);
517 }
518 out:
519 iput(ea_inode);
520 return err;
521 }
522
523 static int
ext4_xattr_block_get(struct inode * inode,int name_index,const char * name,void * buffer,size_t buffer_size)524 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
525 void *buffer, size_t buffer_size)
526 {
527 struct buffer_head *bh = NULL;
528 struct ext4_xattr_entry *entry;
529 size_t size;
530 void *end;
531 int error;
532 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
533
534 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
535 name_index, name, buffer, (long)buffer_size);
536
537 if (!EXT4_I(inode)->i_file_acl)
538 return -ENODATA;
539 ea_idebug(inode, "reading block %llu",
540 (unsigned long long)EXT4_I(inode)->i_file_acl);
541 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
542 if (IS_ERR(bh))
543 return PTR_ERR(bh);
544 ea_bdebug(bh, "b_count=%d, refcount=%d",
545 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
546 error = ext4_xattr_check_block(inode, bh);
547 if (error)
548 goto cleanup;
549 ext4_xattr_block_cache_insert(ea_block_cache, bh);
550 entry = BFIRST(bh);
551 end = bh->b_data + bh->b_size;
552 error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
553 if (error)
554 goto cleanup;
555 size = le32_to_cpu(entry->e_value_size);
556 error = -ERANGE;
557 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
558 goto cleanup;
559 if (buffer) {
560 if (size > buffer_size)
561 goto cleanup;
562 if (entry->e_value_inum) {
563 error = ext4_xattr_inode_get(inode, entry, buffer,
564 size);
565 if (error)
566 goto cleanup;
567 } else {
568 u16 offset = le16_to_cpu(entry->e_value_offs);
569 void *p = bh->b_data + offset;
570
571 if (unlikely(p + size > end))
572 goto cleanup;
573 memcpy(buffer, p, size);
574 }
575 }
576 error = size;
577
578 cleanup:
579 brelse(bh);
580 return error;
581 }
582
583 int
ext4_xattr_ibody_get(struct inode * inode,int name_index,const char * name,void * buffer,size_t buffer_size)584 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
585 void *buffer, size_t buffer_size)
586 {
587 struct ext4_xattr_ibody_header *header;
588 struct ext4_xattr_entry *entry;
589 struct ext4_inode *raw_inode;
590 struct ext4_iloc iloc;
591 size_t size;
592 void *end;
593 int error;
594
595 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
596 return -ENODATA;
597 error = ext4_get_inode_loc(inode, &iloc);
598 if (error)
599 return error;
600 raw_inode = ext4_raw_inode(&iloc);
601 header = IHDR(inode, raw_inode);
602 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
603 error = xattr_check_inode(inode, header, end);
604 if (error)
605 goto cleanup;
606 entry = IFIRST(header);
607 error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
608 if (error)
609 goto cleanup;
610 size = le32_to_cpu(entry->e_value_size);
611 error = -ERANGE;
612 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
613 goto cleanup;
614 if (buffer) {
615 if (size > buffer_size)
616 goto cleanup;
617 if (entry->e_value_inum) {
618 error = ext4_xattr_inode_get(inode, entry, buffer,
619 size);
620 if (error)
621 goto cleanup;
622 } else {
623 u16 offset = le16_to_cpu(entry->e_value_offs);
624 void *p = (void *)IFIRST(header) + offset;
625
626 if (unlikely(p + size > end))
627 goto cleanup;
628 memcpy(buffer, p, size);
629 }
630 }
631 error = size;
632
633 cleanup:
634 brelse(iloc.bh);
635 return error;
636 }
637
638 /*
639 * ext4_xattr_get()
640 *
641 * Copy an extended attribute into the buffer
642 * provided, or compute the buffer size required.
643 * Buffer is NULL to compute the size of the buffer required.
644 *
645 * Returns a negative error number on failure, or the number of bytes
646 * used / required on success.
647 */
648 int
ext4_xattr_get(struct inode * inode,int name_index,const char * name,void * buffer,size_t buffer_size)649 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
650 void *buffer, size_t buffer_size)
651 {
652 int error;
653
654 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
655 return -EIO;
656
657 if (strlen(name) > 255)
658 return -ERANGE;
659
660 down_read(&EXT4_I(inode)->xattr_sem);
661 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
662 buffer_size);
663 if (error == -ENODATA)
664 error = ext4_xattr_block_get(inode, name_index, name, buffer,
665 buffer_size);
666 up_read(&EXT4_I(inode)->xattr_sem);
667 return error;
668 }
669
670 static int
ext4_xattr_list_entries(struct dentry * dentry,struct ext4_xattr_entry * entry,char * buffer,size_t buffer_size)671 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
672 char *buffer, size_t buffer_size)
673 {
674 size_t rest = buffer_size;
675
676 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
677 const struct xattr_handler *handler =
678 ext4_xattr_handler(entry->e_name_index);
679
680 if (handler && (!handler->list || handler->list(dentry))) {
681 const char *prefix = handler->prefix ?: handler->name;
682 size_t prefix_len = strlen(prefix);
683 size_t size = prefix_len + entry->e_name_len + 1;
684
685 if (buffer) {
686 if (size > rest)
687 return -ERANGE;
688 memcpy(buffer, prefix, prefix_len);
689 buffer += prefix_len;
690 memcpy(buffer, entry->e_name, entry->e_name_len);
691 buffer += entry->e_name_len;
692 *buffer++ = 0;
693 }
694 rest -= size;
695 }
696 }
697 return buffer_size - rest; /* total size */
698 }
699
700 static int
ext4_xattr_block_list(struct dentry * dentry,char * buffer,size_t buffer_size)701 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
702 {
703 struct inode *inode = d_inode(dentry);
704 struct buffer_head *bh = NULL;
705 int error;
706
707 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
708 buffer, (long)buffer_size);
709
710 if (!EXT4_I(inode)->i_file_acl)
711 return 0;
712 ea_idebug(inode, "reading block %llu",
713 (unsigned long long)EXT4_I(inode)->i_file_acl);
714 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
715 if (IS_ERR(bh))
716 return PTR_ERR(bh);
717 ea_bdebug(bh, "b_count=%d, refcount=%d",
718 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
719 error = ext4_xattr_check_block(inode, bh);
720 if (error)
721 goto cleanup;
722 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
723 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer,
724 buffer_size);
725 cleanup:
726 brelse(bh);
727 return error;
728 }
729
730 static int
ext4_xattr_ibody_list(struct dentry * dentry,char * buffer,size_t buffer_size)731 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
732 {
733 struct inode *inode = d_inode(dentry);
734 struct ext4_xattr_ibody_header *header;
735 struct ext4_inode *raw_inode;
736 struct ext4_iloc iloc;
737 void *end;
738 int error;
739
740 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
741 return 0;
742 error = ext4_get_inode_loc(inode, &iloc);
743 if (error)
744 return error;
745 raw_inode = ext4_raw_inode(&iloc);
746 header = IHDR(inode, raw_inode);
747 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
748 error = xattr_check_inode(inode, header, end);
749 if (error)
750 goto cleanup;
751 error = ext4_xattr_list_entries(dentry, IFIRST(header),
752 buffer, buffer_size);
753
754 cleanup:
755 brelse(iloc.bh);
756 return error;
757 }
758
759 /*
760 * Inode operation listxattr()
761 *
762 * d_inode(dentry)->i_rwsem: don't care
763 *
764 * Copy a list of attribute names into the buffer
765 * provided, or compute the buffer size required.
766 * Buffer is NULL to compute the size of the buffer required.
767 *
768 * Returns a negative error number on failure, or the number of bytes
769 * used / required on success.
770 */
771 ssize_t
ext4_listxattr(struct dentry * dentry,char * buffer,size_t buffer_size)772 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
773 {
774 int ret, ret2;
775
776 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
777 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
778 if (ret < 0)
779 goto errout;
780 if (buffer) {
781 buffer += ret;
782 buffer_size -= ret;
783 }
784 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
785 if (ret < 0)
786 goto errout;
787 ret += ret2;
788 errout:
789 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
790 return ret;
791 }
792
793 /*
794 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
795 * not set, set it.
796 */
ext4_xattr_update_super_block(handle_t * handle,struct super_block * sb)797 static void ext4_xattr_update_super_block(handle_t *handle,
798 struct super_block *sb)
799 {
800 if (ext4_has_feature_xattr(sb))
801 return;
802
803 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
804 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
805 ext4_set_feature_xattr(sb);
806 ext4_handle_dirty_super(handle, sb);
807 }
808 }
809
ext4_get_inode_usage(struct inode * inode,qsize_t * usage)810 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
811 {
812 struct ext4_iloc iloc = { .bh = NULL };
813 struct buffer_head *bh = NULL;
814 struct ext4_inode *raw_inode;
815 struct ext4_xattr_ibody_header *header;
816 struct ext4_xattr_entry *entry;
817 qsize_t ea_inode_refs = 0;
818 void *end;
819 int ret;
820
821 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
822
823 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
824 ret = ext4_get_inode_loc(inode, &iloc);
825 if (ret)
826 goto out;
827 raw_inode = ext4_raw_inode(&iloc);
828 header = IHDR(inode, raw_inode);
829 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
830 ret = xattr_check_inode(inode, header, end);
831 if (ret)
832 goto out;
833
834 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
835 entry = EXT4_XATTR_NEXT(entry))
836 if (entry->e_value_inum)
837 ea_inode_refs++;
838 }
839
840 if (EXT4_I(inode)->i_file_acl) {
841 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
842 if (IS_ERR(bh)) {
843 ret = PTR_ERR(bh);
844 bh = NULL;
845 goto out;
846 }
847
848 ret = ext4_xattr_check_block(inode, bh);
849 if (ret)
850 goto out;
851
852 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
853 entry = EXT4_XATTR_NEXT(entry))
854 if (entry->e_value_inum)
855 ea_inode_refs++;
856 }
857 *usage = ea_inode_refs + 1;
858 ret = 0;
859 out:
860 brelse(iloc.bh);
861 brelse(bh);
862 return ret;
863 }
864
round_up_cluster(struct inode * inode,size_t length)865 static inline size_t round_up_cluster(struct inode *inode, size_t length)
866 {
867 struct super_block *sb = inode->i_sb;
868 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
869 inode->i_blkbits);
870 size_t mask = ~(cluster_size - 1);
871
872 return (length + cluster_size - 1) & mask;
873 }
874
ext4_xattr_inode_alloc_quota(struct inode * inode,size_t len)875 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
876 {
877 int err;
878
879 err = dquot_alloc_inode(inode);
880 if (err)
881 return err;
882 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
883 if (err)
884 dquot_free_inode(inode);
885 return err;
886 }
887
ext4_xattr_inode_free_quota(struct inode * parent,struct inode * ea_inode,size_t len)888 static void ext4_xattr_inode_free_quota(struct inode *parent,
889 struct inode *ea_inode,
890 size_t len)
891 {
892 if (ea_inode &&
893 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
894 return;
895 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
896 dquot_free_inode(parent);
897 }
898
__ext4_xattr_set_credits(struct super_block * sb,struct inode * inode,struct buffer_head * block_bh,size_t value_len,bool is_create)899 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
900 struct buffer_head *block_bh, size_t value_len,
901 bool is_create)
902 {
903 int credits;
904 int blocks;
905
906 /*
907 * 1) Owner inode update
908 * 2) Ref count update on old xattr block
909 * 3) new xattr block
910 * 4) block bitmap update for new xattr block
911 * 5) group descriptor for new xattr block
912 * 6) block bitmap update for old xattr block
913 * 7) group descriptor for old block
914 *
915 * 6 & 7 can happen if we have two racing threads T_a and T_b
916 * which are each trying to set an xattr on inodes I_a and I_b
917 * which were both initially sharing an xattr block.
918 */
919 credits = 7;
920
921 /* Quota updates. */
922 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
923
924 /*
925 * In case of inline data, we may push out the data to a block,
926 * so we need to reserve credits for this eventuality
927 */
928 if (inode && ext4_has_inline_data(inode))
929 credits += ext4_writepage_trans_blocks(inode) + 1;
930
931 /* We are done if ea_inode feature is not enabled. */
932 if (!ext4_has_feature_ea_inode(sb))
933 return credits;
934
935 /* New ea_inode, inode map, block bitmap, group descriptor. */
936 credits += 4;
937
938 /* Data blocks. */
939 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
940
941 /* Indirection block or one level of extent tree. */
942 blocks += 1;
943
944 /* Block bitmap and group descriptor updates for each block. */
945 credits += blocks * 2;
946
947 /* Blocks themselves. */
948 credits += blocks;
949
950 if (!is_create) {
951 /* Dereference ea_inode holding old xattr value.
952 * Old ea_inode, inode map, block bitmap, group descriptor.
953 */
954 credits += 4;
955
956 /* Data blocks for old ea_inode. */
957 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
958
959 /* Indirection block or one level of extent tree for old
960 * ea_inode.
961 */
962 blocks += 1;
963
964 /* Block bitmap and group descriptor updates for each block. */
965 credits += blocks * 2;
966 }
967
968 /* We may need to clone the existing xattr block in which case we need
969 * to increment ref counts for existing ea_inodes referenced by it.
970 */
971 if (block_bh) {
972 struct ext4_xattr_entry *entry = BFIRST(block_bh);
973
974 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
975 if (entry->e_value_inum)
976 /* Ref count update on ea_inode. */
977 credits += 1;
978 }
979 return credits;
980 }
981
ext4_xattr_inode_update_ref(handle_t * handle,struct inode * ea_inode,int ref_change)982 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
983 int ref_change)
984 {
985 struct ext4_iloc iloc;
986 s64 ref_count;
987 int ret;
988
989 inode_lock(ea_inode);
990
991 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
992 if (ret)
993 goto out;
994
995 ref_count = ext4_xattr_inode_get_ref(ea_inode);
996 ref_count += ref_change;
997 ext4_xattr_inode_set_ref(ea_inode, ref_count);
998
999 if (ref_change > 0) {
1000 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1001 ea_inode->i_ino, ref_count);
1002
1003 if (ref_count == 1) {
1004 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1005 ea_inode->i_ino, ea_inode->i_nlink);
1006
1007 set_nlink(ea_inode, 1);
1008 ext4_orphan_del(handle, ea_inode);
1009 }
1010 } else {
1011 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1012 ea_inode->i_ino, ref_count);
1013
1014 if (ref_count == 0) {
1015 WARN_ONCE(ea_inode->i_nlink != 1,
1016 "EA inode %lu i_nlink=%u",
1017 ea_inode->i_ino, ea_inode->i_nlink);
1018
1019 clear_nlink(ea_inode);
1020 ext4_orphan_add(handle, ea_inode);
1021 }
1022 }
1023
1024 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1025 if (ret)
1026 ext4_warning_inode(ea_inode,
1027 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1028 out:
1029 inode_unlock(ea_inode);
1030 return ret;
1031 }
1032
ext4_xattr_inode_inc_ref(handle_t * handle,struct inode * ea_inode)1033 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1034 {
1035 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1036 }
1037
ext4_xattr_inode_dec_ref(handle_t * handle,struct inode * ea_inode)1038 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1039 {
1040 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1041 }
1042
ext4_xattr_inode_inc_ref_all(handle_t * handle,struct inode * parent,struct ext4_xattr_entry * first)1043 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1044 struct ext4_xattr_entry *first)
1045 {
1046 struct inode *ea_inode;
1047 struct ext4_xattr_entry *entry;
1048 struct ext4_xattr_entry *failed_entry;
1049 unsigned int ea_ino;
1050 int err, saved_err;
1051
1052 for (entry = first; !IS_LAST_ENTRY(entry);
1053 entry = EXT4_XATTR_NEXT(entry)) {
1054 if (!entry->e_value_inum)
1055 continue;
1056 ea_ino = le32_to_cpu(entry->e_value_inum);
1057 err = ext4_xattr_inode_iget(parent, ea_ino,
1058 le32_to_cpu(entry->e_hash),
1059 &ea_inode);
1060 if (err)
1061 goto cleanup;
1062 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1063 if (err) {
1064 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1065 iput(ea_inode);
1066 goto cleanup;
1067 }
1068 iput(ea_inode);
1069 }
1070 return 0;
1071
1072 cleanup:
1073 saved_err = err;
1074 failed_entry = entry;
1075
1076 for (entry = first; entry != failed_entry;
1077 entry = EXT4_XATTR_NEXT(entry)) {
1078 if (!entry->e_value_inum)
1079 continue;
1080 ea_ino = le32_to_cpu(entry->e_value_inum);
1081 err = ext4_xattr_inode_iget(parent, ea_ino,
1082 le32_to_cpu(entry->e_hash),
1083 &ea_inode);
1084 if (err) {
1085 ext4_warning(parent->i_sb,
1086 "cleanup ea_ino %u iget error %d", ea_ino,
1087 err);
1088 continue;
1089 }
1090 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1091 if (err)
1092 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1093 err);
1094 iput(ea_inode);
1095 }
1096 return saved_err;
1097 }
1098
ext4_xattr_restart_fn(handle_t * handle,struct inode * inode,struct buffer_head * bh,bool block_csum,bool dirty)1099 static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
1100 struct buffer_head *bh, bool block_csum, bool dirty)
1101 {
1102 int error;
1103
1104 if (bh && dirty) {
1105 if (block_csum)
1106 ext4_xattr_block_csum_set(inode, bh);
1107 error = ext4_handle_dirty_metadata(handle, NULL, bh);
1108 if (error) {
1109 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
1110 error);
1111 return error;
1112 }
1113 }
1114 return 0;
1115 }
1116
1117 static void
ext4_xattr_inode_dec_ref_all(handle_t * handle,struct inode * parent,struct buffer_head * bh,struct ext4_xattr_entry * first,bool block_csum,struct ext4_xattr_inode_array ** ea_inode_array,int extra_credits,bool skip_quota)1118 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1119 struct buffer_head *bh,
1120 struct ext4_xattr_entry *first, bool block_csum,
1121 struct ext4_xattr_inode_array **ea_inode_array,
1122 int extra_credits, bool skip_quota)
1123 {
1124 struct inode *ea_inode;
1125 struct ext4_xattr_entry *entry;
1126 bool dirty = false;
1127 unsigned int ea_ino;
1128 int err;
1129 int credits;
1130
1131 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1132 credits = 2 + extra_credits;
1133
1134 for (entry = first; !IS_LAST_ENTRY(entry);
1135 entry = EXT4_XATTR_NEXT(entry)) {
1136 if (!entry->e_value_inum)
1137 continue;
1138 ea_ino = le32_to_cpu(entry->e_value_inum);
1139 err = ext4_xattr_inode_iget(parent, ea_ino,
1140 le32_to_cpu(entry->e_hash),
1141 &ea_inode);
1142 if (err)
1143 continue;
1144
1145 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1146 if (err) {
1147 ext4_warning_inode(ea_inode,
1148 "Expand inode array err=%d", err);
1149 iput(ea_inode);
1150 continue;
1151 }
1152
1153 err = ext4_journal_ensure_credits_fn(handle, credits, credits,
1154 ext4_free_metadata_revoke_credits(parent->i_sb, 1),
1155 ext4_xattr_restart_fn(handle, parent, bh, block_csum,
1156 dirty));
1157 if (err < 0) {
1158 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1159 err);
1160 continue;
1161 }
1162 if (err > 0) {
1163 err = ext4_journal_get_write_access(handle, bh);
1164 if (err) {
1165 ext4_warning_inode(ea_inode,
1166 "Re-get write access err=%d",
1167 err);
1168 continue;
1169 }
1170 }
1171
1172 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1173 if (err) {
1174 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1175 err);
1176 continue;
1177 }
1178
1179 if (!skip_quota)
1180 ext4_xattr_inode_free_quota(parent, ea_inode,
1181 le32_to_cpu(entry->e_value_size));
1182
1183 /*
1184 * Forget about ea_inode within the same transaction that
1185 * decrements the ref count. This avoids duplicate decrements in
1186 * case the rest of the work spills over to subsequent
1187 * transactions.
1188 */
1189 entry->e_value_inum = 0;
1190 entry->e_value_size = 0;
1191
1192 dirty = true;
1193 }
1194
1195 if (dirty) {
1196 /*
1197 * Note that we are deliberately skipping csum calculation for
1198 * the final update because we do not expect any journal
1199 * restarts until xattr block is freed.
1200 */
1201
1202 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1203 if (err)
1204 ext4_warning_inode(parent,
1205 "handle dirty metadata err=%d", err);
1206 }
1207 }
1208
1209 /*
1210 * Release the xattr block BH: If the reference count is > 1, decrement it;
1211 * otherwise free the block.
1212 */
1213 static void
ext4_xattr_release_block(handle_t * handle,struct inode * inode,struct buffer_head * bh,struct ext4_xattr_inode_array ** ea_inode_array,int extra_credits)1214 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1215 struct buffer_head *bh,
1216 struct ext4_xattr_inode_array **ea_inode_array,
1217 int extra_credits)
1218 {
1219 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1220 u32 hash, ref;
1221 int error = 0;
1222
1223 BUFFER_TRACE(bh, "get_write_access");
1224 error = ext4_journal_get_write_access(handle, bh);
1225 if (error)
1226 goto out;
1227
1228 retry_ref:
1229 lock_buffer(bh);
1230 hash = le32_to_cpu(BHDR(bh)->h_hash);
1231 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1232 if (ref == 1) {
1233 ea_bdebug(bh, "refcount now=0; freeing");
1234 /*
1235 * This must happen under buffer lock for
1236 * ext4_xattr_block_set() to reliably detect freed block
1237 */
1238 if (ea_block_cache) {
1239 struct mb_cache_entry *oe;
1240
1241 oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
1242 bh->b_blocknr);
1243 if (oe) {
1244 unlock_buffer(bh);
1245 mb_cache_entry_wait_unused(oe);
1246 mb_cache_entry_put(ea_block_cache, oe);
1247 goto retry_ref;
1248 }
1249 }
1250 get_bh(bh);
1251 unlock_buffer(bh);
1252
1253 if (ext4_has_feature_ea_inode(inode->i_sb))
1254 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1255 BFIRST(bh),
1256 true /* block_csum */,
1257 ea_inode_array,
1258 extra_credits,
1259 true /* skip_quota */);
1260 ext4_free_blocks(handle, inode, bh, 0, 1,
1261 EXT4_FREE_BLOCKS_METADATA |
1262 EXT4_FREE_BLOCKS_FORGET);
1263 } else {
1264 ref--;
1265 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1266 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1267 struct mb_cache_entry *ce;
1268
1269 if (ea_block_cache) {
1270 ce = mb_cache_entry_get(ea_block_cache, hash,
1271 bh->b_blocknr);
1272 if (ce) {
1273 set_bit(MBE_REUSABLE_B, &ce->e_flags);
1274 mb_cache_entry_put(ea_block_cache, ce);
1275 }
1276 }
1277 }
1278
1279 ext4_xattr_block_csum_set(inode, bh);
1280 /*
1281 * Beware of this ugliness: Releasing of xattr block references
1282 * from different inodes can race and so we have to protect
1283 * from a race where someone else frees the block (and releases
1284 * its journal_head) before we are done dirtying the buffer. In
1285 * nojournal mode this race is harmless and we actually cannot
1286 * call ext4_handle_dirty_metadata() with locked buffer as
1287 * that function can call sync_dirty_buffer() so for that case
1288 * we handle the dirtying after unlocking the buffer.
1289 */
1290 if (ext4_handle_valid(handle))
1291 error = ext4_handle_dirty_metadata(handle, inode, bh);
1292 unlock_buffer(bh);
1293 if (!ext4_handle_valid(handle))
1294 error = ext4_handle_dirty_metadata(handle, inode, bh);
1295 if (IS_SYNC(inode))
1296 ext4_handle_sync(handle);
1297 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1298 ea_bdebug(bh, "refcount now=%d; releasing",
1299 le32_to_cpu(BHDR(bh)->h_refcount));
1300 }
1301 out:
1302 ext4_std_error(inode->i_sb, error);
1303 return;
1304 }
1305
1306 /*
1307 * Find the available free space for EAs. This also returns the total number of
1308 * bytes used by EA entries.
1309 */
ext4_xattr_free_space(struct ext4_xattr_entry * last,size_t * min_offs,void * base,int * total)1310 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1311 size_t *min_offs, void *base, int *total)
1312 {
1313 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1314 if (!last->e_value_inum && last->e_value_size) {
1315 size_t offs = le16_to_cpu(last->e_value_offs);
1316 if (offs < *min_offs)
1317 *min_offs = offs;
1318 }
1319 if (total)
1320 *total += EXT4_XATTR_LEN(last->e_name_len);
1321 }
1322 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1323 }
1324
1325 /*
1326 * Write the value of the EA in an inode.
1327 */
ext4_xattr_inode_write(handle_t * handle,struct inode * ea_inode,const void * buf,int bufsize)1328 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1329 const void *buf, int bufsize)
1330 {
1331 struct buffer_head *bh = NULL;
1332 unsigned long block = 0;
1333 int blocksize = ea_inode->i_sb->s_blocksize;
1334 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1335 int csize, wsize = 0;
1336 int ret = 0, ret2 = 0;
1337 int retries = 0;
1338
1339 retry:
1340 while (ret >= 0 && ret < max_blocks) {
1341 struct ext4_map_blocks map;
1342 map.m_lblk = block += ret;
1343 map.m_len = max_blocks -= ret;
1344
1345 ret = ext4_map_blocks(handle, ea_inode, &map,
1346 EXT4_GET_BLOCKS_CREATE);
1347 if (ret <= 0) {
1348 ext4_mark_inode_dirty(handle, ea_inode);
1349 if (ret == -ENOSPC &&
1350 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1351 ret = 0;
1352 goto retry;
1353 }
1354 break;
1355 }
1356 }
1357
1358 if (ret < 0)
1359 return ret;
1360
1361 block = 0;
1362 while (wsize < bufsize) {
1363 brelse(bh);
1364 csize = (bufsize - wsize) > blocksize ? blocksize :
1365 bufsize - wsize;
1366 bh = ext4_getblk(handle, ea_inode, block, 0);
1367 if (IS_ERR(bh))
1368 return PTR_ERR(bh);
1369 if (!bh) {
1370 WARN_ON_ONCE(1);
1371 EXT4_ERROR_INODE(ea_inode,
1372 "ext4_getblk() return bh = NULL");
1373 return -EFSCORRUPTED;
1374 }
1375 ret = ext4_journal_get_write_access(handle, bh);
1376 if (ret)
1377 goto out;
1378
1379 memcpy(bh->b_data, buf, csize);
1380 set_buffer_uptodate(bh);
1381 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1382
1383 buf += csize;
1384 wsize += csize;
1385 block += 1;
1386 }
1387
1388 inode_lock(ea_inode);
1389 i_size_write(ea_inode, wsize);
1390 ext4_update_i_disksize(ea_inode, wsize);
1391 inode_unlock(ea_inode);
1392
1393 ret2 = ext4_mark_inode_dirty(handle, ea_inode);
1394 if (unlikely(ret2 && !ret))
1395 ret = ret2;
1396
1397 out:
1398 brelse(bh);
1399
1400 return ret;
1401 }
1402
1403 /*
1404 * Create an inode to store the value of a large EA.
1405 */
ext4_xattr_inode_create(handle_t * handle,struct inode * inode,u32 hash)1406 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1407 struct inode *inode, u32 hash)
1408 {
1409 struct inode *ea_inode = NULL;
1410 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1411 int err;
1412
1413 if (inode->i_sb->s_root == NULL) {
1414 ext4_warning(inode->i_sb,
1415 "refuse to create EA inode when umounting");
1416 WARN_ON(1);
1417 return ERR_PTR(-EINVAL);
1418 }
1419
1420 /*
1421 * Let the next inode be the goal, so we try and allocate the EA inode
1422 * in the same group, or nearby one.
1423 */
1424 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1425 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1426 EXT4_EA_INODE_FL);
1427 if (!IS_ERR(ea_inode)) {
1428 ea_inode->i_op = &ext4_file_inode_operations;
1429 ea_inode->i_fop = &ext4_file_operations;
1430 ext4_set_aops(ea_inode);
1431 ext4_xattr_inode_set_class(ea_inode);
1432 unlock_new_inode(ea_inode);
1433 ext4_xattr_inode_set_ref(ea_inode, 1);
1434 ext4_xattr_inode_set_hash(ea_inode, hash);
1435 err = ext4_mark_inode_dirty(handle, ea_inode);
1436 if (!err)
1437 err = ext4_inode_attach_jinode(ea_inode);
1438 if (err) {
1439 if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1440 ext4_warning_inode(ea_inode,
1441 "cleanup dec ref error %d", err);
1442 iput(ea_inode);
1443 return ERR_PTR(err);
1444 }
1445
1446 /*
1447 * Xattr inodes are shared therefore quota charging is performed
1448 * at a higher level.
1449 */
1450 dquot_free_inode(ea_inode);
1451 dquot_drop(ea_inode);
1452 inode_lock(ea_inode);
1453 ea_inode->i_flags |= S_NOQUOTA;
1454 inode_unlock(ea_inode);
1455 }
1456
1457 return ea_inode;
1458 }
1459
1460 static struct inode *
ext4_xattr_inode_cache_find(struct inode * inode,const void * value,size_t value_len,u32 hash)1461 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1462 size_t value_len, u32 hash)
1463 {
1464 struct inode *ea_inode;
1465 struct mb_cache_entry *ce;
1466 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1467 void *ea_data;
1468
1469 if (!ea_inode_cache)
1470 return NULL;
1471
1472 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1473 if (!ce)
1474 return NULL;
1475
1476 WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
1477 !(current->flags & PF_MEMALLOC_NOFS));
1478
1479 ea_data = kvmalloc(value_len, GFP_KERNEL);
1480 if (!ea_data) {
1481 mb_cache_entry_put(ea_inode_cache, ce);
1482 return NULL;
1483 }
1484
1485 while (ce) {
1486 ea_inode = ext4_iget(inode->i_sb, ce->e_value,
1487 EXT4_IGET_EA_INODE);
1488 if (IS_ERR(ea_inode))
1489 goto next_entry;
1490 ext4_xattr_inode_set_class(ea_inode);
1491 if (i_size_read(ea_inode) == value_len &&
1492 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1493 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1494 value_len) &&
1495 !memcmp(value, ea_data, value_len)) {
1496 mb_cache_entry_touch(ea_inode_cache, ce);
1497 mb_cache_entry_put(ea_inode_cache, ce);
1498 kvfree(ea_data);
1499 return ea_inode;
1500 }
1501 iput(ea_inode);
1502 next_entry:
1503 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1504 }
1505 kvfree(ea_data);
1506 return NULL;
1507 }
1508
1509 /*
1510 * Add value of the EA in an inode.
1511 */
ext4_xattr_inode_lookup_create(handle_t * handle,struct inode * inode,const void * value,size_t value_len,struct inode ** ret_inode)1512 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1513 const void *value, size_t value_len,
1514 struct inode **ret_inode)
1515 {
1516 struct inode *ea_inode;
1517 u32 hash;
1518 int err;
1519
1520 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1521 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1522 if (ea_inode) {
1523 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1524 if (err) {
1525 iput(ea_inode);
1526 return err;
1527 }
1528
1529 *ret_inode = ea_inode;
1530 return 0;
1531 }
1532
1533 /* Create an inode for the EA value */
1534 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1535 if (IS_ERR(ea_inode))
1536 return PTR_ERR(ea_inode);
1537
1538 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1539 if (err) {
1540 ext4_xattr_inode_dec_ref(handle, ea_inode);
1541 iput(ea_inode);
1542 return err;
1543 }
1544
1545 if (EA_INODE_CACHE(inode))
1546 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1547 ea_inode->i_ino, true /* reusable */);
1548
1549 *ret_inode = ea_inode;
1550 return 0;
1551 }
1552
1553 /*
1554 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1555 * feature is enabled.
1556 */
1557 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1558
ext4_xattr_set_entry(struct ext4_xattr_info * i,struct ext4_xattr_search * s,handle_t * handle,struct inode * inode,bool is_block)1559 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1560 struct ext4_xattr_search *s,
1561 handle_t *handle, struct inode *inode,
1562 bool is_block)
1563 {
1564 struct ext4_xattr_entry *last, *next;
1565 struct ext4_xattr_entry *here = s->here;
1566 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1567 int in_inode = i->in_inode;
1568 struct inode *old_ea_inode = NULL;
1569 struct inode *new_ea_inode = NULL;
1570 size_t old_size, new_size;
1571 int ret;
1572
1573 /* Space used by old and new values. */
1574 old_size = (!s->not_found && !here->e_value_inum) ?
1575 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1576 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1577
1578 /*
1579 * Optimization for the simple case when old and new values have the
1580 * same padded sizes. Not applicable if external inodes are involved.
1581 */
1582 if (new_size && new_size == old_size) {
1583 size_t offs = le16_to_cpu(here->e_value_offs);
1584 void *val = s->base + offs;
1585
1586 here->e_value_size = cpu_to_le32(i->value_len);
1587 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1588 memset(val, 0, new_size);
1589 } else {
1590 memcpy(val, i->value, i->value_len);
1591 /* Clear padding bytes. */
1592 memset(val + i->value_len, 0, new_size - i->value_len);
1593 }
1594 goto update_hash;
1595 }
1596
1597 /* Compute min_offs and last. */
1598 last = s->first;
1599 for (; !IS_LAST_ENTRY(last); last = next) {
1600 next = EXT4_XATTR_NEXT(last);
1601 if ((void *)next >= s->end) {
1602 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
1603 ret = -EFSCORRUPTED;
1604 goto out;
1605 }
1606 if (!last->e_value_inum && last->e_value_size) {
1607 size_t offs = le16_to_cpu(last->e_value_offs);
1608 if (offs < min_offs)
1609 min_offs = offs;
1610 }
1611 }
1612
1613 /* Check whether we have enough space. */
1614 if (i->value) {
1615 size_t free;
1616
1617 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1618 if (!s->not_found)
1619 free += EXT4_XATTR_LEN(name_len) + old_size;
1620
1621 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1622 ret = -ENOSPC;
1623 goto out;
1624 }
1625
1626 /*
1627 * If storing the value in an external inode is an option,
1628 * reserve space for xattr entries/names in the external
1629 * attribute block so that a long value does not occupy the
1630 * whole space and prevent further entries being added.
1631 */
1632 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1633 new_size && is_block &&
1634 (min_offs + old_size - new_size) <
1635 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1636 ret = -ENOSPC;
1637 goto out;
1638 }
1639 }
1640
1641 /*
1642 * Getting access to old and new ea inodes is subject to failures.
1643 * Finish that work before doing any modifications to the xattr data.
1644 */
1645 if (!s->not_found && here->e_value_inum) {
1646 ret = ext4_xattr_inode_iget(inode,
1647 le32_to_cpu(here->e_value_inum),
1648 le32_to_cpu(here->e_hash),
1649 &old_ea_inode);
1650 if (ret) {
1651 old_ea_inode = NULL;
1652 goto out;
1653 }
1654 }
1655 if (i->value && in_inode) {
1656 WARN_ON_ONCE(!i->value_len);
1657
1658 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1659 if (ret)
1660 goto out;
1661
1662 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1663 i->value_len,
1664 &new_ea_inode);
1665 if (ret) {
1666 new_ea_inode = NULL;
1667 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
1668 goto out;
1669 }
1670 }
1671
1672 if (old_ea_inode) {
1673 /* We are ready to release ref count on the old_ea_inode. */
1674 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1675 if (ret) {
1676 /* Release newly required ref count on new_ea_inode. */
1677 if (new_ea_inode) {
1678 int err;
1679
1680 err = ext4_xattr_inode_dec_ref(handle,
1681 new_ea_inode);
1682 if (err)
1683 ext4_warning_inode(new_ea_inode,
1684 "dec ref new_ea_inode err=%d",
1685 err);
1686 ext4_xattr_inode_free_quota(inode, new_ea_inode,
1687 i->value_len);
1688 }
1689 goto out;
1690 }
1691
1692 ext4_xattr_inode_free_quota(inode, old_ea_inode,
1693 le32_to_cpu(here->e_value_size));
1694 }
1695
1696 /* No failures allowed past this point. */
1697
1698 if (!s->not_found && here->e_value_size && !here->e_value_inum) {
1699 /* Remove the old value. */
1700 void *first_val = s->base + min_offs;
1701 size_t offs = le16_to_cpu(here->e_value_offs);
1702 void *val = s->base + offs;
1703
1704 memmove(first_val + old_size, first_val, val - first_val);
1705 memset(first_val, 0, old_size);
1706 min_offs += old_size;
1707
1708 /* Adjust all value offsets. */
1709 last = s->first;
1710 while (!IS_LAST_ENTRY(last)) {
1711 size_t o = le16_to_cpu(last->e_value_offs);
1712
1713 if (!last->e_value_inum &&
1714 last->e_value_size && o < offs)
1715 last->e_value_offs = cpu_to_le16(o + old_size);
1716 last = EXT4_XATTR_NEXT(last);
1717 }
1718 }
1719
1720 if (!i->value) {
1721 /* Remove old name. */
1722 size_t size = EXT4_XATTR_LEN(name_len);
1723
1724 last = ENTRY((void *)last - size);
1725 memmove(here, (void *)here + size,
1726 (void *)last - (void *)here + sizeof(__u32));
1727 memset(last, 0, size);
1728
1729 /*
1730 * Update i_inline_off - moved ibody region might contain
1731 * system.data attribute. Handling a failure here won't
1732 * cause other complications for setting an xattr.
1733 */
1734 if (!is_block && ext4_has_inline_data(inode)) {
1735 ret = ext4_find_inline_data_nolock(inode);
1736 if (ret) {
1737 ext4_warning_inode(inode,
1738 "unable to update i_inline_off");
1739 goto out;
1740 }
1741 }
1742 } else if (s->not_found) {
1743 /* Insert new name. */
1744 size_t size = EXT4_XATTR_LEN(name_len);
1745 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1746
1747 memmove((void *)here + size, here, rest);
1748 memset(here, 0, size);
1749 here->e_name_index = i->name_index;
1750 here->e_name_len = name_len;
1751 memcpy(here->e_name, i->name, name_len);
1752 } else {
1753 /* This is an update, reset value info. */
1754 here->e_value_inum = 0;
1755 here->e_value_offs = 0;
1756 here->e_value_size = 0;
1757 }
1758
1759 if (i->value) {
1760 /* Insert new value. */
1761 if (in_inode) {
1762 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1763 } else if (i->value_len) {
1764 void *val = s->base + min_offs - new_size;
1765
1766 here->e_value_offs = cpu_to_le16(min_offs - new_size);
1767 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1768 memset(val, 0, new_size);
1769 } else {
1770 memcpy(val, i->value, i->value_len);
1771 /* Clear padding bytes. */
1772 memset(val + i->value_len, 0,
1773 new_size - i->value_len);
1774 }
1775 }
1776 here->e_value_size = cpu_to_le32(i->value_len);
1777 }
1778
1779 update_hash:
1780 if (i->value) {
1781 __le32 hash = 0;
1782
1783 /* Entry hash calculation. */
1784 if (in_inode) {
1785 __le32 crc32c_hash;
1786
1787 /*
1788 * Feed crc32c hash instead of the raw value for entry
1789 * hash calculation. This is to avoid walking
1790 * potentially long value buffer again.
1791 */
1792 crc32c_hash = cpu_to_le32(
1793 ext4_xattr_inode_get_hash(new_ea_inode));
1794 hash = ext4_xattr_hash_entry(here->e_name,
1795 here->e_name_len,
1796 &crc32c_hash, 1);
1797 } else if (is_block) {
1798 __le32 *value = s->base + le16_to_cpu(
1799 here->e_value_offs);
1800
1801 hash = ext4_xattr_hash_entry(here->e_name,
1802 here->e_name_len, value,
1803 new_size >> 2);
1804 }
1805 here->e_hash = hash;
1806 }
1807
1808 if (is_block)
1809 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1810
1811 ret = 0;
1812 out:
1813 iput(old_ea_inode);
1814 iput(new_ea_inode);
1815 return ret;
1816 }
1817
1818 struct ext4_xattr_block_find {
1819 struct ext4_xattr_search s;
1820 struct buffer_head *bh;
1821 };
1822
1823 static int
ext4_xattr_block_find(struct inode * inode,struct ext4_xattr_info * i,struct ext4_xattr_block_find * bs)1824 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1825 struct ext4_xattr_block_find *bs)
1826 {
1827 struct super_block *sb = inode->i_sb;
1828 int error;
1829
1830 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1831 i->name_index, i->name, i->value, (long)i->value_len);
1832
1833 if (EXT4_I(inode)->i_file_acl) {
1834 /* The inode already has an extended attribute block. */
1835 bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
1836 if (IS_ERR(bs->bh)) {
1837 error = PTR_ERR(bs->bh);
1838 bs->bh = NULL;
1839 return error;
1840 }
1841 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1842 atomic_read(&(bs->bh->b_count)),
1843 le32_to_cpu(BHDR(bs->bh)->h_refcount));
1844 error = ext4_xattr_check_block(inode, bs->bh);
1845 if (error)
1846 return error;
1847 /* Find the named attribute. */
1848 bs->s.base = BHDR(bs->bh);
1849 bs->s.first = BFIRST(bs->bh);
1850 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1851 bs->s.here = bs->s.first;
1852 error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
1853 i->name_index, i->name, 1);
1854 if (error && error != -ENODATA)
1855 return error;
1856 bs->s.not_found = error;
1857 }
1858 return 0;
1859 }
1860
1861 static int
ext4_xattr_block_set(handle_t * handle,struct inode * inode,struct ext4_xattr_info * i,struct ext4_xattr_block_find * bs)1862 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1863 struct ext4_xattr_info *i,
1864 struct ext4_xattr_block_find *bs)
1865 {
1866 struct super_block *sb = inode->i_sb;
1867 struct buffer_head *new_bh = NULL;
1868 struct ext4_xattr_search s_copy = bs->s;
1869 struct ext4_xattr_search *s = &s_copy;
1870 struct mb_cache_entry *ce = NULL;
1871 int error = 0;
1872 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1873 struct inode *ea_inode = NULL, *tmp_inode;
1874 size_t old_ea_inode_quota = 0;
1875 unsigned int ea_ino;
1876
1877
1878 #define header(x) ((struct ext4_xattr_header *)(x))
1879
1880 if (s->base) {
1881 int offset = (char *)s->here - bs->bh->b_data;
1882
1883 BUFFER_TRACE(bs->bh, "get_write_access");
1884 error = ext4_journal_get_write_access(handle, bs->bh);
1885 if (error)
1886 goto cleanup;
1887 lock_buffer(bs->bh);
1888
1889 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1890 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1891
1892 /*
1893 * This must happen under buffer lock for
1894 * ext4_xattr_block_set() to reliably detect modified
1895 * block
1896 */
1897 if (ea_block_cache) {
1898 struct mb_cache_entry *oe;
1899
1900 oe = mb_cache_entry_delete_or_get(ea_block_cache,
1901 hash, bs->bh->b_blocknr);
1902 if (oe) {
1903 /*
1904 * Xattr block is getting reused. Leave
1905 * it alone.
1906 */
1907 mb_cache_entry_put(ea_block_cache, oe);
1908 goto clone_block;
1909 }
1910 }
1911 ea_bdebug(bs->bh, "modifying in-place");
1912 error = ext4_xattr_set_entry(i, s, handle, inode,
1913 true /* is_block */);
1914 ext4_xattr_block_csum_set(inode, bs->bh);
1915 unlock_buffer(bs->bh);
1916 if (error == -EFSCORRUPTED)
1917 goto bad_block;
1918 if (!error)
1919 error = ext4_handle_dirty_metadata(handle,
1920 inode,
1921 bs->bh);
1922 if (error)
1923 goto cleanup;
1924 goto inserted;
1925 }
1926 clone_block:
1927 unlock_buffer(bs->bh);
1928 ea_bdebug(bs->bh, "cloning");
1929 s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS);
1930 error = -ENOMEM;
1931 if (s->base == NULL)
1932 goto cleanup;
1933 s->first = ENTRY(header(s->base)+1);
1934 header(s->base)->h_refcount = cpu_to_le32(1);
1935 s->here = ENTRY(s->base + offset);
1936 s->end = s->base + bs->bh->b_size;
1937
1938 /*
1939 * If existing entry points to an xattr inode, we need
1940 * to prevent ext4_xattr_set_entry() from decrementing
1941 * ref count on it because the reference belongs to the
1942 * original block. In this case, make the entry look
1943 * like it has an empty value.
1944 */
1945 if (!s->not_found && s->here->e_value_inum) {
1946 ea_ino = le32_to_cpu(s->here->e_value_inum);
1947 error = ext4_xattr_inode_iget(inode, ea_ino,
1948 le32_to_cpu(s->here->e_hash),
1949 &tmp_inode);
1950 if (error)
1951 goto cleanup;
1952
1953 if (!ext4_test_inode_state(tmp_inode,
1954 EXT4_STATE_LUSTRE_EA_INODE)) {
1955 /*
1956 * Defer quota free call for previous
1957 * inode until success is guaranteed.
1958 */
1959 old_ea_inode_quota = le32_to_cpu(
1960 s->here->e_value_size);
1961 }
1962 iput(tmp_inode);
1963
1964 s->here->e_value_inum = 0;
1965 s->here->e_value_size = 0;
1966 }
1967 } else {
1968 /* Allocate a buffer where we construct the new block. */
1969 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
1970 /* assert(header == s->base) */
1971 error = -ENOMEM;
1972 if (s->base == NULL)
1973 goto cleanup;
1974 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1975 header(s->base)->h_blocks = cpu_to_le32(1);
1976 header(s->base)->h_refcount = cpu_to_le32(1);
1977 s->first = ENTRY(header(s->base)+1);
1978 s->here = ENTRY(header(s->base)+1);
1979 s->end = s->base + sb->s_blocksize;
1980 }
1981
1982 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
1983 if (error == -EFSCORRUPTED)
1984 goto bad_block;
1985 if (error)
1986 goto cleanup;
1987
1988 if (i->value && s->here->e_value_inum) {
1989 /*
1990 * A ref count on ea_inode has been taken as part of the call to
1991 * ext4_xattr_set_entry() above. We would like to drop this
1992 * extra ref but we have to wait until the xattr block is
1993 * initialized and has its own ref count on the ea_inode.
1994 */
1995 ea_ino = le32_to_cpu(s->here->e_value_inum);
1996 error = ext4_xattr_inode_iget(inode, ea_ino,
1997 le32_to_cpu(s->here->e_hash),
1998 &ea_inode);
1999 if (error) {
2000 ea_inode = NULL;
2001 goto cleanup;
2002 }
2003 }
2004
2005 inserted:
2006 if (!IS_LAST_ENTRY(s->first)) {
2007 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
2008 &ce);
2009 if (new_bh) {
2010 /* We found an identical block in the cache. */
2011 if (new_bh == bs->bh)
2012 ea_bdebug(new_bh, "keeping");
2013 else {
2014 u32 ref;
2015
2016 #ifdef EXT4_XATTR_DEBUG
2017 WARN_ON_ONCE(dquot_initialize_needed(inode));
2018 #endif
2019 /* The old block is released after updating
2020 the inode. */
2021 error = dquot_alloc_block(inode,
2022 EXT4_C2B(EXT4_SB(sb), 1));
2023 if (error)
2024 goto cleanup;
2025 BUFFER_TRACE(new_bh, "get_write_access");
2026 error = ext4_journal_get_write_access(handle,
2027 new_bh);
2028 if (error)
2029 goto cleanup_dquot;
2030 lock_buffer(new_bh);
2031 /*
2032 * We have to be careful about races with
2033 * adding references to xattr block. Once we
2034 * hold buffer lock xattr block's state is
2035 * stable so we can check the additional
2036 * reference fits.
2037 */
2038 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2039 if (ref > EXT4_XATTR_REFCOUNT_MAX) {
2040 /*
2041 * Undo everything and check mbcache
2042 * again.
2043 */
2044 unlock_buffer(new_bh);
2045 dquot_free_block(inode,
2046 EXT4_C2B(EXT4_SB(sb),
2047 1));
2048 brelse(new_bh);
2049 mb_cache_entry_put(ea_block_cache, ce);
2050 ce = NULL;
2051 new_bh = NULL;
2052 goto inserted;
2053 }
2054 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2055 if (ref == EXT4_XATTR_REFCOUNT_MAX)
2056 clear_bit(MBE_REUSABLE_B, &ce->e_flags);
2057 ea_bdebug(new_bh, "reusing; refcount now=%d",
2058 ref);
2059 ext4_xattr_block_csum_set(inode, new_bh);
2060 unlock_buffer(new_bh);
2061 error = ext4_handle_dirty_metadata(handle,
2062 inode,
2063 new_bh);
2064 if (error)
2065 goto cleanup_dquot;
2066 }
2067 mb_cache_entry_touch(ea_block_cache, ce);
2068 mb_cache_entry_put(ea_block_cache, ce);
2069 ce = NULL;
2070 } else if (bs->bh && s->base == bs->bh->b_data) {
2071 /* We were modifying this block in-place. */
2072 ea_bdebug(bs->bh, "keeping this block");
2073 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
2074 new_bh = bs->bh;
2075 get_bh(new_bh);
2076 } else {
2077 /* We need to allocate a new block */
2078 ext4_fsblk_t goal, block;
2079
2080 #ifdef EXT4_XATTR_DEBUG
2081 WARN_ON_ONCE(dquot_initialize_needed(inode));
2082 #endif
2083 goal = ext4_group_first_block_no(sb,
2084 EXT4_I(inode)->i_block_group);
2085 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2086 NULL, &error);
2087 if (error)
2088 goto cleanup;
2089
2090 ea_idebug(inode, "creating block %llu",
2091 (unsigned long long)block);
2092
2093 new_bh = sb_getblk(sb, block);
2094 if (unlikely(!new_bh)) {
2095 error = -ENOMEM;
2096 getblk_failed:
2097 ext4_free_blocks(handle, inode, NULL, block, 1,
2098 EXT4_FREE_BLOCKS_METADATA);
2099 goto cleanup;
2100 }
2101 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2102 ENTRY(header(s->base)+1));
2103 if (error)
2104 goto getblk_failed;
2105 if (ea_inode) {
2106 /* Drop the extra ref on ea_inode. */
2107 error = ext4_xattr_inode_dec_ref(handle,
2108 ea_inode);
2109 if (error)
2110 ext4_warning_inode(ea_inode,
2111 "dec ref error=%d",
2112 error);
2113 iput(ea_inode);
2114 ea_inode = NULL;
2115 }
2116
2117 lock_buffer(new_bh);
2118 error = ext4_journal_get_create_access(handle, new_bh);
2119 if (error) {
2120 unlock_buffer(new_bh);
2121 error = -EIO;
2122 goto getblk_failed;
2123 }
2124 memcpy(new_bh->b_data, s->base, new_bh->b_size);
2125 ext4_xattr_block_csum_set(inode, new_bh);
2126 set_buffer_uptodate(new_bh);
2127 unlock_buffer(new_bh);
2128 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
2129 error = ext4_handle_dirty_metadata(handle, inode,
2130 new_bh);
2131 if (error)
2132 goto cleanup;
2133 }
2134 }
2135
2136 if (old_ea_inode_quota)
2137 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
2138
2139 /* Update the inode. */
2140 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2141
2142 /* Drop the previous xattr block. */
2143 if (bs->bh && bs->bh != new_bh) {
2144 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2145
2146 ext4_xattr_release_block(handle, inode, bs->bh,
2147 &ea_inode_array,
2148 0 /* extra_credits */);
2149 ext4_xattr_inode_array_free(ea_inode_array);
2150 }
2151 error = 0;
2152
2153 cleanup:
2154 if (ea_inode) {
2155 int error2;
2156
2157 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2158 if (error2)
2159 ext4_warning_inode(ea_inode, "dec ref error=%d",
2160 error2);
2161
2162 /* If there was an error, revert the quota charge. */
2163 if (error)
2164 ext4_xattr_inode_free_quota(inode, ea_inode,
2165 i_size_read(ea_inode));
2166 iput(ea_inode);
2167 }
2168 if (ce)
2169 mb_cache_entry_put(ea_block_cache, ce);
2170 brelse(new_bh);
2171 if (!(bs->bh && s->base == bs->bh->b_data))
2172 kfree(s->base);
2173
2174 return error;
2175
2176 cleanup_dquot:
2177 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
2178 goto cleanup;
2179
2180 bad_block:
2181 EXT4_ERROR_INODE(inode, "bad block %llu",
2182 EXT4_I(inode)->i_file_acl);
2183 goto cleanup;
2184
2185 #undef header
2186 }
2187
ext4_xattr_ibody_find(struct inode * inode,struct ext4_xattr_info * i,struct ext4_xattr_ibody_find * is)2188 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2189 struct ext4_xattr_ibody_find *is)
2190 {
2191 struct ext4_xattr_ibody_header *header;
2192 struct ext4_inode *raw_inode;
2193 int error;
2194
2195 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2196 return 0;
2197
2198 raw_inode = ext4_raw_inode(&is->iloc);
2199 header = IHDR(inode, raw_inode);
2200 is->s.base = is->s.first = IFIRST(header);
2201 is->s.here = is->s.first;
2202 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2203 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2204 error = xattr_check_inode(inode, header, is->s.end);
2205 if (error)
2206 return error;
2207 /* Find the named attribute. */
2208 error = xattr_find_entry(inode, &is->s.here, is->s.end,
2209 i->name_index, i->name, 0);
2210 if (error && error != -ENODATA)
2211 return error;
2212 is->s.not_found = error;
2213 }
2214 return 0;
2215 }
2216
ext4_xattr_ibody_set(handle_t * handle,struct inode * inode,struct ext4_xattr_info * i,struct ext4_xattr_ibody_find * is)2217 int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2218 struct ext4_xattr_info *i,
2219 struct ext4_xattr_ibody_find *is)
2220 {
2221 struct ext4_xattr_ibody_header *header;
2222 struct ext4_xattr_search *s = &is->s;
2223 int error;
2224
2225 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2226 return -ENOSPC;
2227
2228 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2229 if (error)
2230 return error;
2231 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2232 if (!IS_LAST_ENTRY(s->first)) {
2233 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2234 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2235 } else {
2236 header->h_magic = cpu_to_le32(0);
2237 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2238 }
2239 return 0;
2240 }
2241
ext4_xattr_value_same(struct ext4_xattr_search * s,struct ext4_xattr_info * i)2242 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2243 struct ext4_xattr_info *i)
2244 {
2245 void *value;
2246
2247 /* When e_value_inum is set the value is stored externally. */
2248 if (s->here->e_value_inum)
2249 return 0;
2250 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2251 return 0;
2252 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2253 return !memcmp(value, i->value, i->value_len);
2254 }
2255
ext4_xattr_get_block(struct inode * inode)2256 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2257 {
2258 struct buffer_head *bh;
2259 int error;
2260
2261 if (!EXT4_I(inode)->i_file_acl)
2262 return NULL;
2263 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2264 if (IS_ERR(bh))
2265 return bh;
2266 error = ext4_xattr_check_block(inode, bh);
2267 if (error) {
2268 brelse(bh);
2269 return ERR_PTR(error);
2270 }
2271 return bh;
2272 }
2273
2274 /*
2275 * ext4_xattr_set_handle()
2276 *
2277 * Create, replace or remove an extended attribute for this inode. Value
2278 * is NULL to remove an existing extended attribute, and non-NULL to
2279 * either replace an existing extended attribute, or create a new extended
2280 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2281 * specify that an extended attribute must exist and must not exist
2282 * previous to the call, respectively.
2283 *
2284 * Returns 0, or a negative error number on failure.
2285 */
2286 int
ext4_xattr_set_handle(handle_t * handle,struct inode * inode,int name_index,const char * name,const void * value,size_t value_len,int flags)2287 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2288 const char *name, const void *value, size_t value_len,
2289 int flags)
2290 {
2291 struct ext4_xattr_info i = {
2292 .name_index = name_index,
2293 .name = name,
2294 .value = value,
2295 .value_len = value_len,
2296 .in_inode = 0,
2297 };
2298 struct ext4_xattr_ibody_find is = {
2299 .s = { .not_found = -ENODATA, },
2300 };
2301 struct ext4_xattr_block_find bs = {
2302 .s = { .not_found = -ENODATA, },
2303 };
2304 int no_expand;
2305 int error;
2306
2307 if (!name)
2308 return -EINVAL;
2309 if (strlen(name) > 255)
2310 return -ERANGE;
2311
2312 ext4_write_lock_xattr(inode, &no_expand);
2313
2314 /* Check journal credits under write lock. */
2315 if (ext4_handle_valid(handle)) {
2316 struct buffer_head *bh;
2317 int credits;
2318
2319 bh = ext4_xattr_get_block(inode);
2320 if (IS_ERR(bh)) {
2321 error = PTR_ERR(bh);
2322 goto cleanup;
2323 }
2324
2325 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2326 value_len,
2327 flags & XATTR_CREATE);
2328 brelse(bh);
2329
2330 if (jbd2_handle_buffer_credits(handle) < credits) {
2331 error = -ENOSPC;
2332 goto cleanup;
2333 }
2334 WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
2335 }
2336
2337 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2338 if (error)
2339 goto cleanup;
2340
2341 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2342 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2343 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2344 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2345 }
2346
2347 error = ext4_xattr_ibody_find(inode, &i, &is);
2348 if (error)
2349 goto cleanup;
2350 if (is.s.not_found)
2351 error = ext4_xattr_block_find(inode, &i, &bs);
2352 if (error)
2353 goto cleanup;
2354 if (is.s.not_found && bs.s.not_found) {
2355 error = -ENODATA;
2356 if (flags & XATTR_REPLACE)
2357 goto cleanup;
2358 error = 0;
2359 if (!value)
2360 goto cleanup;
2361 } else {
2362 error = -EEXIST;
2363 if (flags & XATTR_CREATE)
2364 goto cleanup;
2365 }
2366
2367 if (!value) {
2368 if (!is.s.not_found)
2369 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2370 else if (!bs.s.not_found)
2371 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2372 } else {
2373 error = 0;
2374 /* Xattr value did not change? Save us some work and bail out */
2375 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2376 goto cleanup;
2377 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2378 goto cleanup;
2379
2380 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2381 (EXT4_XATTR_SIZE(i.value_len) >
2382 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2383 i.in_inode = 1;
2384 retry_inode:
2385 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2386 if (!error && !bs.s.not_found) {
2387 i.value = NULL;
2388 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2389 } else if (error == -ENOSPC) {
2390 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2391 brelse(bs.bh);
2392 bs.bh = NULL;
2393 error = ext4_xattr_block_find(inode, &i, &bs);
2394 if (error)
2395 goto cleanup;
2396 }
2397 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2398 if (!error && !is.s.not_found) {
2399 i.value = NULL;
2400 error = ext4_xattr_ibody_set(handle, inode, &i,
2401 &is);
2402 } else if (error == -ENOSPC) {
2403 /*
2404 * Xattr does not fit in the block, store at
2405 * external inode if possible.
2406 */
2407 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2408 i.value_len && !i.in_inode) {
2409 i.in_inode = 1;
2410 goto retry_inode;
2411 }
2412 }
2413 }
2414 }
2415 if (!error) {
2416 ext4_xattr_update_super_block(handle, inode->i_sb);
2417 inode->i_ctime = current_time(inode);
2418 if (!value)
2419 no_expand = 0;
2420 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2421 /*
2422 * The bh is consumed by ext4_mark_iloc_dirty, even with
2423 * error != 0.
2424 */
2425 is.iloc.bh = NULL;
2426 if (IS_SYNC(inode))
2427 ext4_handle_sync(handle);
2428 }
2429 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
2430
2431 cleanup:
2432 brelse(is.iloc.bh);
2433 brelse(bs.bh);
2434 ext4_write_unlock_xattr(inode, &no_expand);
2435 return error;
2436 }
2437
ext4_xattr_set_credits(struct inode * inode,size_t value_len,bool is_create,int * credits)2438 int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2439 bool is_create, int *credits)
2440 {
2441 struct buffer_head *bh;
2442 int err;
2443
2444 *credits = 0;
2445
2446 if (!EXT4_SB(inode->i_sb)->s_journal)
2447 return 0;
2448
2449 down_read(&EXT4_I(inode)->xattr_sem);
2450
2451 bh = ext4_xattr_get_block(inode);
2452 if (IS_ERR(bh)) {
2453 err = PTR_ERR(bh);
2454 } else {
2455 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2456 value_len, is_create);
2457 brelse(bh);
2458 err = 0;
2459 }
2460
2461 up_read(&EXT4_I(inode)->xattr_sem);
2462 return err;
2463 }
2464
2465 /*
2466 * ext4_xattr_set()
2467 *
2468 * Like ext4_xattr_set_handle, but start from an inode. This extended
2469 * attribute modification is a filesystem transaction by itself.
2470 *
2471 * Returns 0, or a negative error number on failure.
2472 */
2473 int
ext4_xattr_set(struct inode * inode,int name_index,const char * name,const void * value,size_t value_len,int flags)2474 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2475 const void *value, size_t value_len, int flags)
2476 {
2477 handle_t *handle;
2478 struct super_block *sb = inode->i_sb;
2479 int error, retries = 0;
2480 int credits;
2481
2482 error = dquot_initialize(inode);
2483 if (error)
2484 return error;
2485
2486 retry:
2487 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2488 &credits);
2489 if (error)
2490 return error;
2491
2492 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2493 if (IS_ERR(handle)) {
2494 error = PTR_ERR(handle);
2495 } else {
2496 int error2;
2497
2498 error = ext4_xattr_set_handle(handle, inode, name_index, name,
2499 value, value_len, flags);
2500 error2 = ext4_journal_stop(handle);
2501 if (error == -ENOSPC &&
2502 ext4_should_retry_alloc(sb, &retries))
2503 goto retry;
2504 if (error == 0)
2505 error = error2;
2506 }
2507 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
2508
2509 return error;
2510 }
2511
2512 /*
2513 * Shift the EA entries in the inode to create space for the increased
2514 * i_extra_isize.
2515 */
ext4_xattr_shift_entries(struct ext4_xattr_entry * entry,int value_offs_shift,void * to,void * from,size_t n)2516 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2517 int value_offs_shift, void *to,
2518 void *from, size_t n)
2519 {
2520 struct ext4_xattr_entry *last = entry;
2521 int new_offs;
2522
2523 /* We always shift xattr headers further thus offsets get lower */
2524 BUG_ON(value_offs_shift > 0);
2525
2526 /* Adjust the value offsets of the entries */
2527 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2528 if (!last->e_value_inum && last->e_value_size) {
2529 new_offs = le16_to_cpu(last->e_value_offs) +
2530 value_offs_shift;
2531 last->e_value_offs = cpu_to_le16(new_offs);
2532 }
2533 }
2534 /* Shift the entries by n bytes */
2535 memmove(to, from, n);
2536 }
2537
2538 /*
2539 * Move xattr pointed to by 'entry' from inode into external xattr block
2540 */
ext4_xattr_move_to_block(handle_t * handle,struct inode * inode,struct ext4_inode * raw_inode,struct ext4_xattr_entry * entry)2541 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2542 struct ext4_inode *raw_inode,
2543 struct ext4_xattr_entry *entry)
2544 {
2545 struct ext4_xattr_ibody_find *is = NULL;
2546 struct ext4_xattr_block_find *bs = NULL;
2547 char *buffer = NULL, *b_entry_name = NULL;
2548 size_t value_size = le32_to_cpu(entry->e_value_size);
2549 struct ext4_xattr_info i = {
2550 .value = NULL,
2551 .value_len = 0,
2552 .name_index = entry->e_name_index,
2553 .in_inode = !!entry->e_value_inum,
2554 };
2555 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2556 int needs_kvfree = 0;
2557 int error;
2558
2559 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2560 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2561 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2562 if (!is || !bs || !b_entry_name) {
2563 error = -ENOMEM;
2564 goto out;
2565 }
2566
2567 is->s.not_found = -ENODATA;
2568 bs->s.not_found = -ENODATA;
2569 is->iloc.bh = NULL;
2570 bs->bh = NULL;
2571
2572 /* Save the entry name and the entry value */
2573 if (entry->e_value_inum) {
2574 buffer = kvmalloc(value_size, GFP_NOFS);
2575 if (!buffer) {
2576 error = -ENOMEM;
2577 goto out;
2578 }
2579 needs_kvfree = 1;
2580 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
2581 if (error)
2582 goto out;
2583 } else {
2584 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2585 buffer = (void *)IFIRST(header) + value_offs;
2586 }
2587
2588 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2589 b_entry_name[entry->e_name_len] = '\0';
2590 i.name = b_entry_name;
2591
2592 error = ext4_get_inode_loc(inode, &is->iloc);
2593 if (error)
2594 goto out;
2595
2596 error = ext4_xattr_ibody_find(inode, &i, is);
2597 if (error)
2598 goto out;
2599
2600 i.value = buffer;
2601 i.value_len = value_size;
2602 error = ext4_xattr_block_find(inode, &i, bs);
2603 if (error)
2604 goto out;
2605
2606 /* Move ea entry from the inode into the block */
2607 error = ext4_xattr_block_set(handle, inode, &i, bs);
2608 if (error)
2609 goto out;
2610
2611 /* Remove the chosen entry from the inode */
2612 i.value = NULL;
2613 i.value_len = 0;
2614 error = ext4_xattr_ibody_set(handle, inode, &i, is);
2615
2616 out:
2617 kfree(b_entry_name);
2618 if (needs_kvfree && buffer)
2619 kvfree(buffer);
2620 if (is)
2621 brelse(is->iloc.bh);
2622 if (bs)
2623 brelse(bs->bh);
2624 kfree(is);
2625 kfree(bs);
2626
2627 return error;
2628 }
2629
ext4_xattr_make_inode_space(handle_t * handle,struct inode * inode,struct ext4_inode * raw_inode,int isize_diff,size_t ifree,size_t bfree,int * total_ino)2630 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2631 struct ext4_inode *raw_inode,
2632 int isize_diff, size_t ifree,
2633 size_t bfree, int *total_ino)
2634 {
2635 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2636 struct ext4_xattr_entry *small_entry;
2637 struct ext4_xattr_entry *entry;
2638 struct ext4_xattr_entry *last;
2639 unsigned int entry_size; /* EA entry size */
2640 unsigned int total_size; /* EA entry size + value size */
2641 unsigned int min_total_size;
2642 int error;
2643
2644 while (isize_diff > ifree) {
2645 entry = NULL;
2646 small_entry = NULL;
2647 min_total_size = ~0U;
2648 last = IFIRST(header);
2649 /* Find the entry best suited to be pushed into EA block */
2650 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2651 /* never move system.data out of the inode */
2652 if ((last->e_name_len == 4) &&
2653 (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
2654 !memcmp(last->e_name, "data", 4))
2655 continue;
2656 total_size = EXT4_XATTR_LEN(last->e_name_len);
2657 if (!last->e_value_inum)
2658 total_size += EXT4_XATTR_SIZE(
2659 le32_to_cpu(last->e_value_size));
2660 if (total_size <= bfree &&
2661 total_size < min_total_size) {
2662 if (total_size + ifree < isize_diff) {
2663 small_entry = last;
2664 } else {
2665 entry = last;
2666 min_total_size = total_size;
2667 }
2668 }
2669 }
2670
2671 if (entry == NULL) {
2672 if (small_entry == NULL)
2673 return -ENOSPC;
2674 entry = small_entry;
2675 }
2676
2677 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2678 total_size = entry_size;
2679 if (!entry->e_value_inum)
2680 total_size += EXT4_XATTR_SIZE(
2681 le32_to_cpu(entry->e_value_size));
2682 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2683 entry);
2684 if (error)
2685 return error;
2686
2687 *total_ino -= entry_size;
2688 ifree += total_size;
2689 bfree -= total_size;
2690 }
2691
2692 return 0;
2693 }
2694
2695 /*
2696 * Expand an inode by new_extra_isize bytes when EAs are present.
2697 * Returns 0 on success or negative error number on failure.
2698 */
ext4_expand_extra_isize_ea(struct inode * inode,int new_extra_isize,struct ext4_inode * raw_inode,handle_t * handle)2699 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2700 struct ext4_inode *raw_inode, handle_t *handle)
2701 {
2702 struct ext4_xattr_ibody_header *header;
2703 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2704 static unsigned int mnt_count;
2705 size_t min_offs;
2706 size_t ifree, bfree;
2707 int total_ino;
2708 void *base, *end;
2709 int error = 0, tried_min_extra_isize = 0;
2710 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
2711 int isize_diff; /* How much do we need to grow i_extra_isize */
2712
2713 retry:
2714 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2715 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2716 return 0;
2717
2718 header = IHDR(inode, raw_inode);
2719
2720 /*
2721 * Check if enough free space is available in the inode to shift the
2722 * entries ahead by new_extra_isize.
2723 */
2724
2725 base = IFIRST(header);
2726 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2727 min_offs = end - base;
2728 total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
2729
2730 error = xattr_check_inode(inode, header, end);
2731 if (error)
2732 goto cleanup;
2733
2734 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2735 if (ifree >= isize_diff)
2736 goto shift;
2737
2738 /*
2739 * Enough free space isn't available in the inode, check if
2740 * EA block can hold new_extra_isize bytes.
2741 */
2742 if (EXT4_I(inode)->i_file_acl) {
2743 struct buffer_head *bh;
2744
2745 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2746 if (IS_ERR(bh)) {
2747 error = PTR_ERR(bh);
2748 goto cleanup;
2749 }
2750 error = ext4_xattr_check_block(inode, bh);
2751 if (error) {
2752 brelse(bh);
2753 goto cleanup;
2754 }
2755 base = BHDR(bh);
2756 end = bh->b_data + bh->b_size;
2757 min_offs = end - base;
2758 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2759 NULL);
2760 brelse(bh);
2761 if (bfree + ifree < isize_diff) {
2762 if (!tried_min_extra_isize && s_min_extra_isize) {
2763 tried_min_extra_isize++;
2764 new_extra_isize = s_min_extra_isize;
2765 goto retry;
2766 }
2767 error = -ENOSPC;
2768 goto cleanup;
2769 }
2770 } else {
2771 bfree = inode->i_sb->s_blocksize;
2772 }
2773
2774 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2775 isize_diff, ifree, bfree,
2776 &total_ino);
2777 if (error) {
2778 if (error == -ENOSPC && !tried_min_extra_isize &&
2779 s_min_extra_isize) {
2780 tried_min_extra_isize++;
2781 new_extra_isize = s_min_extra_isize;
2782 goto retry;
2783 }
2784 goto cleanup;
2785 }
2786 shift:
2787 /* Adjust the offsets and shift the remaining entries ahead */
2788 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2789 - new_extra_isize, (void *)raw_inode +
2790 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2791 (void *)header, total_ino);
2792 EXT4_I(inode)->i_extra_isize = new_extra_isize;
2793
2794 if (ext4_has_inline_data(inode))
2795 error = ext4_find_inline_data_nolock(inode);
2796
2797 cleanup:
2798 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
2799 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2800 inode->i_ino);
2801 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2802 }
2803 return error;
2804 }
2805
2806 #define EIA_INCR 16 /* must be 2^n */
2807 #define EIA_MASK (EIA_INCR - 1)
2808
2809 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2810 * If @ea_inode_array is new or full it will be grown and the old
2811 * contents copied over.
2812 */
2813 static int
ext4_expand_inode_array(struct ext4_xattr_inode_array ** ea_inode_array,struct inode * inode)2814 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2815 struct inode *inode)
2816 {
2817 if (*ea_inode_array == NULL) {
2818 /*
2819 * Start with 15 inodes, so it fits into a power-of-two size.
2820 * If *ea_inode_array is NULL, this is essentially offsetof()
2821 */
2822 (*ea_inode_array) =
2823 kmalloc(offsetof(struct ext4_xattr_inode_array,
2824 inodes[EIA_MASK]),
2825 GFP_NOFS);
2826 if (*ea_inode_array == NULL)
2827 return -ENOMEM;
2828 (*ea_inode_array)->count = 0;
2829 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2830 /* expand the array once all 15 + n * 16 slots are full */
2831 struct ext4_xattr_inode_array *new_array = NULL;
2832 int count = (*ea_inode_array)->count;
2833
2834 /* if new_array is NULL, this is essentially offsetof() */
2835 new_array = kmalloc(
2836 offsetof(struct ext4_xattr_inode_array,
2837 inodes[count + EIA_INCR]),
2838 GFP_NOFS);
2839 if (new_array == NULL)
2840 return -ENOMEM;
2841 memcpy(new_array, *ea_inode_array,
2842 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2843 kfree(*ea_inode_array);
2844 *ea_inode_array = new_array;
2845 }
2846 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
2847 return 0;
2848 }
2849
2850 /*
2851 * ext4_xattr_delete_inode()
2852 *
2853 * Free extended attribute resources associated with this inode. Traverse
2854 * all entries and decrement reference on any xattr inodes associated with this
2855 * inode. This is called immediately before an inode is freed. We have exclusive
2856 * access to the inode. If an orphan inode is deleted it will also release its
2857 * references on xattr block and xattr inodes.
2858 */
ext4_xattr_delete_inode(handle_t * handle,struct inode * inode,struct ext4_xattr_inode_array ** ea_inode_array,int extra_credits)2859 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2860 struct ext4_xattr_inode_array **ea_inode_array,
2861 int extra_credits)
2862 {
2863 struct buffer_head *bh = NULL;
2864 struct ext4_xattr_ibody_header *header;
2865 struct ext4_iloc iloc = { .bh = NULL };
2866 struct ext4_xattr_entry *entry;
2867 struct inode *ea_inode;
2868 int error;
2869
2870 error = ext4_journal_ensure_credits(handle, extra_credits,
2871 ext4_free_metadata_revoke_credits(inode->i_sb, 1));
2872 if (error < 0) {
2873 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2874 goto cleanup;
2875 }
2876
2877 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2878 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2879
2880 error = ext4_get_inode_loc(inode, &iloc);
2881 if (error) {
2882 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2883 goto cleanup;
2884 }
2885
2886 error = ext4_journal_get_write_access(handle, iloc.bh);
2887 if (error) {
2888 EXT4_ERROR_INODE(inode, "write access (error %d)",
2889 error);
2890 goto cleanup;
2891 }
2892
2893 header = IHDR(inode, ext4_raw_inode(&iloc));
2894 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2895 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2896 IFIRST(header),
2897 false /* block_csum */,
2898 ea_inode_array,
2899 extra_credits,
2900 false /* skip_quota */);
2901 }
2902
2903 if (EXT4_I(inode)->i_file_acl) {
2904 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2905 if (IS_ERR(bh)) {
2906 error = PTR_ERR(bh);
2907 if (error == -EIO) {
2908 EXT4_ERROR_INODE_ERR(inode, EIO,
2909 "block %llu read error",
2910 EXT4_I(inode)->i_file_acl);
2911 }
2912 bh = NULL;
2913 goto cleanup;
2914 }
2915 error = ext4_xattr_check_block(inode, bh);
2916 if (error)
2917 goto cleanup;
2918
2919 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2920 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2921 entry = EXT4_XATTR_NEXT(entry)) {
2922 if (!entry->e_value_inum)
2923 continue;
2924 error = ext4_xattr_inode_iget(inode,
2925 le32_to_cpu(entry->e_value_inum),
2926 le32_to_cpu(entry->e_hash),
2927 &ea_inode);
2928 if (error)
2929 continue;
2930 ext4_xattr_inode_free_quota(inode, ea_inode,
2931 le32_to_cpu(entry->e_value_size));
2932 iput(ea_inode);
2933 }
2934
2935 }
2936
2937 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2938 extra_credits);
2939 /*
2940 * Update i_file_acl value in the same transaction that releases
2941 * block.
2942 */
2943 EXT4_I(inode)->i_file_acl = 0;
2944 error = ext4_mark_inode_dirty(handle, inode);
2945 if (error) {
2946 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2947 error);
2948 goto cleanup;
2949 }
2950 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
2951 }
2952 error = 0;
2953 cleanup:
2954 brelse(iloc.bh);
2955 brelse(bh);
2956 return error;
2957 }
2958
ext4_xattr_inode_array_free(struct ext4_xattr_inode_array * ea_inode_array)2959 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
2960 {
2961 int idx;
2962
2963 if (ea_inode_array == NULL)
2964 return;
2965
2966 for (idx = 0; idx < ea_inode_array->count; ++idx)
2967 iput(ea_inode_array->inodes[idx]);
2968 kfree(ea_inode_array);
2969 }
2970
2971 /*
2972 * ext4_xattr_block_cache_insert()
2973 *
2974 * Create a new entry in the extended attribute block cache, and insert
2975 * it unless such an entry is already in the cache.
2976 *
2977 * Returns 0, or a negative error number on failure.
2978 */
2979 static void
ext4_xattr_block_cache_insert(struct mb_cache * ea_block_cache,struct buffer_head * bh)2980 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2981 struct buffer_head *bh)
2982 {
2983 struct ext4_xattr_header *header = BHDR(bh);
2984 __u32 hash = le32_to_cpu(header->h_hash);
2985 int reusable = le32_to_cpu(header->h_refcount) <
2986 EXT4_XATTR_REFCOUNT_MAX;
2987 int error;
2988
2989 if (!ea_block_cache)
2990 return;
2991 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
2992 bh->b_blocknr, reusable);
2993 if (error) {
2994 if (error == -EBUSY)
2995 ea_bdebug(bh, "already in cache");
2996 } else
2997 ea_bdebug(bh, "inserting [%x]", (int)hash);
2998 }
2999
3000 /*
3001 * ext4_xattr_cmp()
3002 *
3003 * Compare two extended attribute blocks for equality.
3004 *
3005 * Returns 0 if the blocks are equal, 1 if they differ, and
3006 * a negative error number on errors.
3007 */
3008 static int
ext4_xattr_cmp(struct ext4_xattr_header * header1,struct ext4_xattr_header * header2)3009 ext4_xattr_cmp(struct ext4_xattr_header *header1,
3010 struct ext4_xattr_header *header2)
3011 {
3012 struct ext4_xattr_entry *entry1, *entry2;
3013
3014 entry1 = ENTRY(header1+1);
3015 entry2 = ENTRY(header2+1);
3016 while (!IS_LAST_ENTRY(entry1)) {
3017 if (IS_LAST_ENTRY(entry2))
3018 return 1;
3019 if (entry1->e_hash != entry2->e_hash ||
3020 entry1->e_name_index != entry2->e_name_index ||
3021 entry1->e_name_len != entry2->e_name_len ||
3022 entry1->e_value_size != entry2->e_value_size ||
3023 entry1->e_value_inum != entry2->e_value_inum ||
3024 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3025 return 1;
3026 if (!entry1->e_value_inum &&
3027 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
3028 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3029 le32_to_cpu(entry1->e_value_size)))
3030 return 1;
3031
3032 entry1 = EXT4_XATTR_NEXT(entry1);
3033 entry2 = EXT4_XATTR_NEXT(entry2);
3034 }
3035 if (!IS_LAST_ENTRY(entry2))
3036 return 1;
3037 return 0;
3038 }
3039
3040 /*
3041 * ext4_xattr_block_cache_find()
3042 *
3043 * Find an identical extended attribute block.
3044 *
3045 * Returns a pointer to the block found, or NULL if such a block was
3046 * not found or an error occurred.
3047 */
3048 static struct buffer_head *
ext4_xattr_block_cache_find(struct inode * inode,struct ext4_xattr_header * header,struct mb_cache_entry ** pce)3049 ext4_xattr_block_cache_find(struct inode *inode,
3050 struct ext4_xattr_header *header,
3051 struct mb_cache_entry **pce)
3052 {
3053 __u32 hash = le32_to_cpu(header->h_hash);
3054 struct mb_cache_entry *ce;
3055 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
3056
3057 if (!ea_block_cache)
3058 return NULL;
3059 if (!header->h_hash)
3060 return NULL; /* never share */
3061 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
3062 ce = mb_cache_entry_find_first(ea_block_cache, hash);
3063 while (ce) {
3064 struct buffer_head *bh;
3065
3066 bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
3067 if (IS_ERR(bh)) {
3068 if (PTR_ERR(bh) == -ENOMEM)
3069 return NULL;
3070 bh = NULL;
3071 EXT4_ERROR_INODE(inode, "block %lu read error",
3072 (unsigned long)ce->e_value);
3073 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
3074 *pce = ce;
3075 return bh;
3076 }
3077 brelse(bh);
3078 ce = mb_cache_entry_find_next(ea_block_cache, ce);
3079 }
3080 return NULL;
3081 }
3082
3083 #define NAME_HASH_SHIFT 5
3084 #define VALUE_HASH_SHIFT 16
3085
3086 /*
3087 * ext4_xattr_hash_entry()
3088 *
3089 * Compute the hash of an extended attribute.
3090 */
ext4_xattr_hash_entry(char * name,size_t name_len,__le32 * value,size_t value_count)3091 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3092 size_t value_count)
3093 {
3094 __u32 hash = 0;
3095
3096 while (name_len--) {
3097 hash = (hash << NAME_HASH_SHIFT) ^
3098 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3099 *name++;
3100 }
3101 while (value_count--) {
3102 hash = (hash << VALUE_HASH_SHIFT) ^
3103 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3104 le32_to_cpu(*value++);
3105 }
3106 return cpu_to_le32(hash);
3107 }
3108
3109 #undef NAME_HASH_SHIFT
3110 #undef VALUE_HASH_SHIFT
3111
3112 #define BLOCK_HASH_SHIFT 16
3113
3114 /*
3115 * ext4_xattr_rehash()
3116 *
3117 * Re-compute the extended attribute hash value after an entry has changed.
3118 */
ext4_xattr_rehash(struct ext4_xattr_header * header)3119 static void ext4_xattr_rehash(struct ext4_xattr_header *header)
3120 {
3121 struct ext4_xattr_entry *here;
3122 __u32 hash = 0;
3123
3124 here = ENTRY(header+1);
3125 while (!IS_LAST_ENTRY(here)) {
3126 if (!here->e_hash) {
3127 /* Block is not shared if an entry's hash value == 0 */
3128 hash = 0;
3129 break;
3130 }
3131 hash = (hash << BLOCK_HASH_SHIFT) ^
3132 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3133 le32_to_cpu(here->e_hash);
3134 here = EXT4_XATTR_NEXT(here);
3135 }
3136 header->h_hash = cpu_to_le32(hash);
3137 }
3138
3139 #undef BLOCK_HASH_SHIFT
3140
3141 #define HASH_BUCKET_BITS 10
3142
3143 struct mb_cache *
ext4_xattr_create_cache(void)3144 ext4_xattr_create_cache(void)
3145 {
3146 return mb_cache_create(HASH_BUCKET_BITS);
3147 }
3148
ext4_xattr_destroy_cache(struct mb_cache * cache)3149 void ext4_xattr_destroy_cache(struct mb_cache *cache)
3150 {
3151 if (cache)
3152 mb_cache_destroy(cache);
3153 }
3154
3155