2 * linux/fs/ext4/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
8 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
28 * +------------------+
31 * | entry 2 | | growing downwards
36 * | value 3 | | growing upwards
38 * +------------------+
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
46 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
47 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
53 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/mbcache.h>
57 #include <linux/quotaops.h>
58 #include "ext4_jbd2.h"
63 #ifdef EXT4_XATTR_DEBUG
64 # define ea_idebug(inode, fmt, ...) \
65 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
66 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
67 # define ea_bdebug(bh, fmt, ...) \
68 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
69 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
71 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
72 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
75 static void ext4_xattr_cache_insert(struct mb_cache
*, struct buffer_head
*);
76 static struct buffer_head
*ext4_xattr_cache_find(struct inode
*,
77 struct ext4_xattr_header
*,
78 struct mb_cache_entry
**);
79 static void ext4_xattr_rehash(struct ext4_xattr_header
*,
80 struct ext4_xattr_entry
*);
81 static int ext4_xattr_list(struct dentry
*dentry
, char *buffer
,
84 static const struct xattr_handler
*ext4_xattr_handler_map
[] = {
85 [EXT4_XATTR_INDEX_USER
] = &ext4_xattr_user_handler
,
86 #ifdef CONFIG_EXT4_FS_POSIX_ACL
87 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
] = &posix_acl_access_xattr_handler
,
88 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &posix_acl_default_xattr_handler
,
90 [EXT4_XATTR_INDEX_TRUSTED
] = &ext4_xattr_trusted_handler
,
91 #ifdef CONFIG_EXT4_FS_SECURITY
92 [EXT4_XATTR_INDEX_SECURITY
] = &ext4_xattr_security_handler
,
96 const struct xattr_handler
*ext4_xattr_handlers
[] = {
97 &ext4_xattr_user_handler
,
98 &ext4_xattr_trusted_handler
,
99 #ifdef CONFIG_EXT4_FS_POSIX_ACL
100 &posix_acl_access_xattr_handler
,
101 &posix_acl_default_xattr_handler
,
103 #ifdef CONFIG_EXT4_FS_SECURITY
104 &ext4_xattr_security_handler
,
109 #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_mb_cache)
112 static __le32
ext4_xattr_block_csum(struct inode
*inode
,
114 struct ext4_xattr_header
*hdr
)
116 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
118 __le64 dsk_block_nr
= cpu_to_le64(block_nr
);
119 __u32 dummy_csum
= 0;
120 int offset
= offsetof(struct ext4_xattr_header
, h_checksum
);
122 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&dsk_block_nr
,
123 sizeof(dsk_block_nr
));
124 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
, offset
);
125 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, sizeof(dummy_csum
));
126 offset
+= sizeof(dummy_csum
);
127 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
+ offset
,
128 EXT4_BLOCK_SIZE(inode
->i_sb
) - offset
);
130 return cpu_to_le32(csum
);
133 static int ext4_xattr_block_csum_verify(struct inode
*inode
,
135 struct ext4_xattr_header
*hdr
)
137 if (ext4_has_metadata_csum(inode
->i_sb
) &&
138 (hdr
->h_checksum
!= ext4_xattr_block_csum(inode
, block_nr
, hdr
)))
143 static void ext4_xattr_block_csum_set(struct inode
*inode
,
145 struct ext4_xattr_header
*hdr
)
147 if (!ext4_has_metadata_csum(inode
->i_sb
))
150 hdr
->h_checksum
= ext4_xattr_block_csum(inode
, block_nr
, hdr
);
153 static inline int ext4_handle_dirty_xattr_block(handle_t
*handle
,
155 struct buffer_head
*bh
)
157 ext4_xattr_block_csum_set(inode
, bh
->b_blocknr
, BHDR(bh
));
158 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
161 static inline const struct xattr_handler
*
162 ext4_xattr_handler(int name_index
)
164 const struct xattr_handler
*handler
= NULL
;
166 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
167 handler
= ext4_xattr_handler_map
[name_index
];
172 * Inode operation listxattr()
174 * d_inode(dentry)->i_mutex: don't care
177 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
179 return ext4_xattr_list(dentry
, buffer
, size
);
183 ext4_xattr_check_names(struct ext4_xattr_entry
*entry
, void *end
,
186 struct ext4_xattr_entry
*e
= entry
;
188 /* Find the end of the names list */
189 while (!IS_LAST_ENTRY(e
)) {
190 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(e
);
191 if ((void *)next
>= end
)
192 return -EFSCORRUPTED
;
196 /* Check the values */
197 while (!IS_LAST_ENTRY(entry
)) {
198 if (entry
->e_value_block
!= 0)
199 return -EFSCORRUPTED
;
200 if (entry
->e_value_size
!= 0) {
201 u16 offs
= le16_to_cpu(entry
->e_value_offs
);
202 u32 size
= le32_to_cpu(entry
->e_value_size
);
206 * The value cannot overlap the names, and the value
207 * with padding cannot extend beyond 'end'. Check both
208 * the padded and unpadded sizes, since the size may
209 * overflow to 0 when adding padding.
211 if (offs
> end
- value_start
)
212 return -EFSCORRUPTED
;
213 value
= value_start
+ offs
;
214 if (value
< (void *)e
+ sizeof(u32
) ||
215 size
> end
- value
||
216 EXT4_XATTR_SIZE(size
) > end
- value
)
217 return -EFSCORRUPTED
;
219 entry
= EXT4_XATTR_NEXT(entry
);
226 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
230 if (buffer_verified(bh
))
233 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
234 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
235 return -EFSCORRUPTED
;
236 if (!ext4_xattr_block_csum_verify(inode
, bh
->b_blocknr
, BHDR(bh
)))
238 error
= ext4_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
,
241 set_buffer_verified(bh
);
246 __xattr_check_inode(struct inode
*inode
, struct ext4_xattr_ibody_header
*header
,
247 void *end
, const char *function
, unsigned int line
)
249 int error
= -EFSCORRUPTED
;
251 if (end
- (void *)header
< sizeof(*header
) + sizeof(u32
) ||
252 (header
->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
)))
254 error
= ext4_xattr_check_names(IFIRST(header
), end
, IFIRST(header
));
257 __ext4_error_inode(inode
, function
, line
, 0,
258 "corrupted in-inode xattr");
262 #define xattr_check_inode(inode, header, end) \
263 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
266 ext4_xattr_check_entry(struct ext4_xattr_entry
*entry
, size_t size
)
268 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
270 if (entry
->e_value_block
!= 0 || value_size
> size
||
271 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
272 return -EFSCORRUPTED
;
277 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
278 const char *name
, size_t size
, int sorted
)
280 struct ext4_xattr_entry
*entry
;
286 name_len
= strlen(name
);
288 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
289 cmp
= name_index
- entry
->e_name_index
;
291 cmp
= name_len
- entry
->e_name_len
;
293 cmp
= memcmp(name
, entry
->e_name
, name_len
);
294 if (cmp
<= 0 && (sorted
|| cmp
== 0))
298 if (!cmp
&& ext4_xattr_check_entry(entry
, size
))
299 return -EFSCORRUPTED
;
300 return cmp
? -ENODATA
: 0;
304 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
305 void *buffer
, size_t buffer_size
)
307 struct buffer_head
*bh
= NULL
;
308 struct ext4_xattr_entry
*entry
;
311 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
313 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
314 name_index
, name
, buffer
, (long)buffer_size
);
317 if (!EXT4_I(inode
)->i_file_acl
)
319 ea_idebug(inode
, "reading block %llu",
320 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
321 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
324 ea_bdebug(bh
, "b_count=%d, refcount=%d",
325 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
326 if (ext4_xattr_check_block(inode
, bh
)) {
328 EXT4_ERROR_INODE(inode
, "bad block %llu",
329 EXT4_I(inode
)->i_file_acl
);
330 error
= -EFSCORRUPTED
;
333 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
335 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
336 if (error
== -EFSCORRUPTED
)
340 size
= le32_to_cpu(entry
->e_value_size
);
343 if (size
> buffer_size
)
345 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
356 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
357 void *buffer
, size_t buffer_size
)
359 struct ext4_xattr_ibody_header
*header
;
360 struct ext4_xattr_entry
*entry
;
361 struct ext4_inode
*raw_inode
;
362 struct ext4_iloc iloc
;
367 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
369 error
= ext4_get_inode_loc(inode
, &iloc
);
372 raw_inode
= ext4_raw_inode(&iloc
);
373 header
= IHDR(inode
, raw_inode
);
374 entry
= IFIRST(header
);
375 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
376 error
= xattr_check_inode(inode
, header
, end
);
379 error
= ext4_xattr_find_entry(&entry
, name_index
, name
,
380 end
- (void *)entry
, 0);
383 size
= le32_to_cpu(entry
->e_value_size
);
386 if (size
> buffer_size
)
388 memcpy(buffer
, (void *)IFIRST(header
) +
389 le16_to_cpu(entry
->e_value_offs
), size
);
401 * Copy an extended attribute into the buffer
402 * provided, or compute the buffer size required.
403 * Buffer is NULL to compute the size of the buffer required.
405 * Returns a negative error number on failure, or the number of bytes
406 * used / required on success.
409 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
410 void *buffer
, size_t buffer_size
)
414 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
417 if (strlen(name
) > 255)
420 down_read(&EXT4_I(inode
)->xattr_sem
);
421 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
423 if (error
== -ENODATA
)
424 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
426 up_read(&EXT4_I(inode
)->xattr_sem
);
431 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
432 char *buffer
, size_t buffer_size
)
434 size_t rest
= buffer_size
;
436 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
437 const struct xattr_handler
*handler
=
438 ext4_xattr_handler(entry
->e_name_index
);
440 if (handler
&& (!handler
->list
|| handler
->list(dentry
))) {
441 const char *prefix
= handler
->prefix
?: handler
->name
;
442 size_t prefix_len
= strlen(prefix
);
443 size_t size
= prefix_len
+ entry
->e_name_len
+ 1;
448 memcpy(buffer
, prefix
, prefix_len
);
449 buffer
+= prefix_len
;
450 memcpy(buffer
, entry
->e_name
, entry
->e_name_len
);
451 buffer
+= entry
->e_name_len
;
457 return buffer_size
- rest
; /* total size */
461 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
463 struct inode
*inode
= d_inode(dentry
);
464 struct buffer_head
*bh
= NULL
;
466 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
468 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
469 buffer
, (long)buffer_size
);
472 if (!EXT4_I(inode
)->i_file_acl
)
474 ea_idebug(inode
, "reading block %llu",
475 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
476 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
480 ea_bdebug(bh
, "b_count=%d, refcount=%d",
481 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
482 if (ext4_xattr_check_block(inode
, bh
)) {
483 EXT4_ERROR_INODE(inode
, "bad block %llu",
484 EXT4_I(inode
)->i_file_acl
);
485 error
= -EFSCORRUPTED
;
488 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
489 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
498 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
500 struct inode
*inode
= d_inode(dentry
);
501 struct ext4_xattr_ibody_header
*header
;
502 struct ext4_inode
*raw_inode
;
503 struct ext4_iloc iloc
;
507 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
509 error
= ext4_get_inode_loc(inode
, &iloc
);
512 raw_inode
= ext4_raw_inode(&iloc
);
513 header
= IHDR(inode
, raw_inode
);
514 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
515 error
= xattr_check_inode(inode
, header
, end
);
518 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
519 buffer
, buffer_size
);
529 * Copy a list of attribute names into the buffer
530 * provided, or compute the buffer size required.
531 * Buffer is NULL to compute the size of the buffer required.
533 * Returns a negative error number on failure, or the number of bytes
534 * used / required on success.
537 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
541 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
542 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
549 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
554 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
559 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
562 static void ext4_xattr_update_super_block(handle_t
*handle
,
563 struct super_block
*sb
)
565 if (ext4_has_feature_xattr(sb
))
568 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
569 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
570 ext4_set_feature_xattr(sb
);
571 ext4_handle_dirty_super(handle
, sb
);
576 * Release the xattr block BH: If the reference count is > 1, decrement it;
577 * otherwise free the block.
580 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
581 struct buffer_head
*bh
)
583 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
587 BUFFER_TRACE(bh
, "get_write_access");
588 error
= ext4_journal_get_write_access(handle
, bh
);
593 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
594 ref
= le32_to_cpu(BHDR(bh
)->h_refcount
);
596 ea_bdebug(bh
, "refcount now=0; freeing");
598 * This must happen under buffer lock for
599 * ext4_xattr_block_set() to reliably detect freed block
601 mb_cache_entry_delete_block(ext4_mb_cache
, hash
, bh
->b_blocknr
);
604 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
605 EXT4_FREE_BLOCKS_METADATA
|
606 EXT4_FREE_BLOCKS_FORGET
);
609 BHDR(bh
)->h_refcount
= cpu_to_le32(ref
);
610 if (ref
== EXT4_XATTR_REFCOUNT_MAX
- 1) {
611 struct mb_cache_entry
*ce
;
613 ce
= mb_cache_entry_get(ext4_mb_cache
, hash
,
617 mb_cache_entry_put(ext4_mb_cache
, ce
);
622 * Beware of this ugliness: Releasing of xattr block references
623 * from different inodes can race and so we have to protect
624 * from a race where someone else frees the block (and releases
625 * its journal_head) before we are done dirtying the buffer. In
626 * nojournal mode this race is harmless and we actually cannot
627 * call ext4_handle_dirty_xattr_block() with locked buffer as
628 * that function can call sync_dirty_buffer() so for that case
629 * we handle the dirtying after unlocking the buffer.
631 if (ext4_handle_valid(handle
))
632 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
635 if (!ext4_handle_valid(handle
))
636 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
639 ext4_handle_sync(handle
);
640 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
641 ea_bdebug(bh
, "refcount now=%d; releasing",
642 le32_to_cpu(BHDR(bh
)->h_refcount
));
645 ext4_std_error(inode
->i_sb
, error
);
650 * Find the available free space for EAs. This also returns the total number of
651 * bytes used by EA entries.
653 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
654 size_t *min_offs
, void *base
, int *total
)
656 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
657 if (last
->e_value_size
) {
658 size_t offs
= le16_to_cpu(last
->e_value_offs
);
659 if (offs
< *min_offs
)
663 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
665 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
669 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
)
671 struct ext4_xattr_entry
*last
;
672 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
674 /* Compute min_offs and last. */
676 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
677 if (last
->e_value_size
) {
678 size_t offs
= le16_to_cpu(last
->e_value_offs
);
683 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
685 if (s
->here
->e_value_size
) {
686 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
687 free
+= EXT4_XATTR_SIZE(size
);
689 free
+= EXT4_XATTR_LEN(name_len
);
692 if (free
< EXT4_XATTR_LEN(name_len
) +
693 EXT4_XATTR_SIZE(i
->value_len
))
697 if (i
->value
&& s
->not_found
) {
698 /* Insert the new name. */
699 size_t size
= EXT4_XATTR_LEN(name_len
);
700 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
701 memmove((void *)s
->here
+ size
, s
->here
, rest
);
702 memset(s
->here
, 0, size
);
703 s
->here
->e_name_index
= i
->name_index
;
704 s
->here
->e_name_len
= name_len
;
705 memcpy(s
->here
->e_name
, i
->name
, name_len
);
707 if (s
->here
->e_value_size
) {
708 void *first_val
= s
->base
+ min_offs
;
709 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
710 void *val
= s
->base
+ offs
;
711 size_t size
= EXT4_XATTR_SIZE(
712 le32_to_cpu(s
->here
->e_value_size
));
714 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
715 /* The old and the new value have the same
716 size. Just replace. */
717 s
->here
->e_value_size
=
718 cpu_to_le32(i
->value_len
);
719 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
720 memset(val
, 0, size
);
722 /* Clear pad bytes first. */
723 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
725 memcpy(val
, i
->value
, i
->value_len
);
730 /* Remove the old value. */
731 memmove(first_val
+ size
, first_val
, val
- first_val
);
732 memset(first_val
, 0, size
);
733 s
->here
->e_value_size
= 0;
734 s
->here
->e_value_offs
= 0;
737 /* Adjust all value offsets. */
739 while (!IS_LAST_ENTRY(last
)) {
740 size_t o
= le16_to_cpu(last
->e_value_offs
);
741 if (last
->e_value_size
&& o
< offs
)
743 cpu_to_le16(o
+ size
);
744 last
= EXT4_XATTR_NEXT(last
);
748 /* Remove the old name. */
749 size_t size
= EXT4_XATTR_LEN(name_len
);
750 last
= ENTRY((void *)last
- size
);
751 memmove(s
->here
, (void *)s
->here
+ size
,
752 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
753 memset(last
, 0, size
);
758 /* Insert the new value. */
759 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
761 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
762 void *val
= s
->base
+ min_offs
- size
;
763 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
764 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
765 memset(val
, 0, size
);
767 /* Clear the pad bytes first. */
768 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
770 memcpy(val
, i
->value
, i
->value_len
);
777 struct ext4_xattr_block_find
{
778 struct ext4_xattr_search s
;
779 struct buffer_head
*bh
;
783 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
784 struct ext4_xattr_block_find
*bs
)
786 struct super_block
*sb
= inode
->i_sb
;
789 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
790 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
792 if (EXT4_I(inode
)->i_file_acl
) {
793 /* The inode already has an extended attribute block. */
794 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
798 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
799 atomic_read(&(bs
->bh
->b_count
)),
800 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
801 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
802 EXT4_ERROR_INODE(inode
, "bad block %llu",
803 EXT4_I(inode
)->i_file_acl
);
804 error
= -EFSCORRUPTED
;
807 /* Find the named attribute. */
808 bs
->s
.base
= BHDR(bs
->bh
);
809 bs
->s
.first
= BFIRST(bs
->bh
);
810 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
811 bs
->s
.here
= bs
->s
.first
;
812 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
813 i
->name
, bs
->bh
->b_size
, 1);
814 if (error
&& error
!= -ENODATA
)
816 bs
->s
.not_found
= error
;
825 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
826 struct ext4_xattr_info
*i
,
827 struct ext4_xattr_block_find
*bs
)
829 struct super_block
*sb
= inode
->i_sb
;
830 struct buffer_head
*new_bh
= NULL
;
831 struct ext4_xattr_search
*s
= &bs
->s
;
832 struct mb_cache_entry
*ce
= NULL
;
834 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
836 #define header(x) ((struct ext4_xattr_header *)(x))
838 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
841 BUFFER_TRACE(bs
->bh
, "get_write_access");
842 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
847 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
848 __u32 hash
= le32_to_cpu(BHDR(bs
->bh
)->h_hash
);
851 * This must happen under buffer lock for
852 * ext4_xattr_block_set() to reliably detect modified
855 mb_cache_entry_delete_block(ext4_mb_cache
, hash
,
857 ea_bdebug(bs
->bh
, "modifying in-place");
858 error
= ext4_xattr_set_entry(i
, s
);
860 if (!IS_LAST_ENTRY(s
->first
))
861 ext4_xattr_rehash(header(s
->base
),
863 ext4_xattr_cache_insert(ext4_mb_cache
,
866 unlock_buffer(bs
->bh
);
867 if (error
== -EFSCORRUPTED
)
870 error
= ext4_handle_dirty_xattr_block(handle
,
877 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
879 unlock_buffer(bs
->bh
);
880 ea_bdebug(bs
->bh
, "cloning");
881 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
885 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
886 s
->first
= ENTRY(header(s
->base
)+1);
887 header(s
->base
)->h_refcount
= cpu_to_le32(1);
888 s
->here
= ENTRY(s
->base
+ offset
);
889 s
->end
= s
->base
+ bs
->bh
->b_size
;
892 /* Allocate a buffer where we construct the new block. */
893 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
894 /* assert(header == s->base) */
898 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
899 header(s
->base
)->h_blocks
= cpu_to_le32(1);
900 header(s
->base
)->h_refcount
= cpu_to_le32(1);
901 s
->first
= ENTRY(header(s
->base
)+1);
902 s
->here
= ENTRY(header(s
->base
)+1);
903 s
->end
= s
->base
+ sb
->s_blocksize
;
906 error
= ext4_xattr_set_entry(i
, s
);
907 if (error
== -EFSCORRUPTED
)
911 if (!IS_LAST_ENTRY(s
->first
))
912 ext4_xattr_rehash(header(s
->base
), s
->here
);
915 if (!IS_LAST_ENTRY(s
->first
)) {
916 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
918 /* We found an identical block in the cache. */
919 if (new_bh
== bs
->bh
)
920 ea_bdebug(new_bh
, "keeping");
924 /* The old block is released after updating
926 error
= dquot_alloc_block(inode
,
927 EXT4_C2B(EXT4_SB(sb
), 1));
930 BUFFER_TRACE(new_bh
, "get_write_access");
931 error
= ext4_journal_get_write_access(handle
,
937 * We have to be careful about races with
938 * freeing, rehashing or adding references to
939 * xattr block. Once we hold buffer lock xattr
940 * block's state is stable so we can check
941 * whether the block got freed / rehashed or
942 * not. Since we unhash mbcache entry under
943 * buffer lock when freeing / rehashing xattr
944 * block, checking whether entry is still
945 * hashed is reliable. Same rules hold for
946 * e_reusable handling.
948 if (hlist_bl_unhashed(&ce
->e_hash_list
) ||
951 * Undo everything and check mbcache
954 unlock_buffer(new_bh
);
955 dquot_free_block(inode
,
956 EXT4_C2B(EXT4_SB(sb
),
959 mb_cache_entry_put(ext4_mb_cache
, ce
);
964 ref
= le32_to_cpu(BHDR(new_bh
)->h_refcount
) + 1;
965 BHDR(new_bh
)->h_refcount
= cpu_to_le32(ref
);
966 if (ref
>= EXT4_XATTR_REFCOUNT_MAX
)
968 ea_bdebug(new_bh
, "reusing; refcount now=%d",
970 unlock_buffer(new_bh
);
971 error
= ext4_handle_dirty_xattr_block(handle
,
977 mb_cache_entry_touch(ext4_mb_cache
, ce
);
978 mb_cache_entry_put(ext4_mb_cache
, ce
);
980 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
981 /* We were modifying this block in-place. */
982 ea_bdebug(bs
->bh
, "keeping this block");
986 /* We need to allocate a new block */
987 ext4_fsblk_t goal
, block
;
989 goal
= ext4_group_first_block_no(sb
,
990 EXT4_I(inode
)->i_block_group
);
992 /* non-extent files can't have physical blocks past 2^32 */
993 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
994 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
996 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
1001 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
1002 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
1004 ea_idebug(inode
, "creating block %llu",
1005 (unsigned long long)block
);
1007 new_bh
= sb_getblk(sb
, block
);
1008 if (unlikely(!new_bh
)) {
1011 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
1012 EXT4_FREE_BLOCKS_METADATA
);
1015 lock_buffer(new_bh
);
1016 error
= ext4_journal_get_create_access(handle
, new_bh
);
1018 unlock_buffer(new_bh
);
1022 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
1023 set_buffer_uptodate(new_bh
);
1024 unlock_buffer(new_bh
);
1025 ext4_xattr_cache_insert(ext4_mb_cache
, new_bh
);
1026 error
= ext4_handle_dirty_xattr_block(handle
,
1033 /* Update the inode. */
1034 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
1036 /* Drop the previous xattr block. */
1037 if (bs
->bh
&& bs
->bh
!= new_bh
)
1038 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
1043 mb_cache_entry_put(ext4_mb_cache
, ce
);
1045 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
1051 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
1055 EXT4_ERROR_INODE(inode
, "bad block %llu",
1056 EXT4_I(inode
)->i_file_acl
);
1062 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
1063 struct ext4_xattr_ibody_find
*is
)
1065 struct ext4_xattr_ibody_header
*header
;
1066 struct ext4_inode
*raw_inode
;
1069 if (EXT4_I(inode
)->i_extra_isize
== 0)
1071 raw_inode
= ext4_raw_inode(&is
->iloc
);
1072 header
= IHDR(inode
, raw_inode
);
1073 is
->s
.base
= is
->s
.first
= IFIRST(header
);
1074 is
->s
.here
= is
->s
.first
;
1075 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1076 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
1077 error
= xattr_check_inode(inode
, header
, is
->s
.end
);
1080 /* Find the named attribute. */
1081 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
1082 i
->name
, is
->s
.end
-
1083 (void *)is
->s
.base
, 0);
1084 if (error
&& error
!= -ENODATA
)
1086 is
->s
.not_found
= error
;
1091 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
1092 struct ext4_xattr_info
*i
,
1093 struct ext4_xattr_ibody_find
*is
)
1095 struct ext4_xattr_ibody_header
*header
;
1096 struct ext4_xattr_search
*s
= &is
->s
;
1099 if (EXT4_I(inode
)->i_extra_isize
== 0)
1101 error
= ext4_xattr_set_entry(i
, s
);
1103 if (error
== -ENOSPC
&&
1104 ext4_has_inline_data(inode
)) {
1105 error
= ext4_try_to_evict_inline_data(handle
, inode
,
1106 EXT4_XATTR_LEN(strlen(i
->name
) +
1107 EXT4_XATTR_SIZE(i
->value_len
)));
1110 error
= ext4_xattr_ibody_find(inode
, i
, is
);
1113 error
= ext4_xattr_set_entry(i
, s
);
1118 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1119 if (!IS_LAST_ENTRY(s
->first
)) {
1120 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1121 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1123 header
->h_magic
= cpu_to_le32(0);
1124 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1129 static int ext4_xattr_ibody_set(struct inode
*inode
,
1130 struct ext4_xattr_info
*i
,
1131 struct ext4_xattr_ibody_find
*is
)
1133 struct ext4_xattr_ibody_header
*header
;
1134 struct ext4_xattr_search
*s
= &is
->s
;
1137 if (EXT4_I(inode
)->i_extra_isize
== 0)
1139 error
= ext4_xattr_set_entry(i
, s
);
1142 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1143 if (!IS_LAST_ENTRY(s
->first
)) {
1144 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1145 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1147 header
->h_magic
= cpu_to_le32(0);
1148 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1153 static int ext4_xattr_value_same(struct ext4_xattr_search
*s
,
1154 struct ext4_xattr_info
*i
)
1158 if (le32_to_cpu(s
->here
->e_value_size
) != i
->value_len
)
1160 value
= ((void *)s
->base
) + le16_to_cpu(s
->here
->e_value_offs
);
1161 return !memcmp(value
, i
->value
, i
->value_len
);
1165 * ext4_xattr_set_handle()
1167 * Create, replace or remove an extended attribute for this inode. Value
1168 * is NULL to remove an existing extended attribute, and non-NULL to
1169 * either replace an existing extended attribute, or create a new extended
1170 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1171 * specify that an extended attribute must exist and must not exist
1172 * previous to the call, respectively.
1174 * Returns 0, or a negative error number on failure.
1177 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1178 const char *name
, const void *value
, size_t value_len
,
1181 struct ext4_xattr_info i
= {
1182 .name_index
= name_index
,
1185 .value_len
= value_len
,
1188 struct ext4_xattr_ibody_find is
= {
1189 .s
= { .not_found
= -ENODATA
, },
1191 struct ext4_xattr_block_find bs
= {
1192 .s
= { .not_found
= -ENODATA
, },
1199 if (strlen(name
) > 255)
1201 ext4_write_lock_xattr(inode
, &no_expand
);
1203 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1207 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1208 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1209 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1210 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1213 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1217 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1220 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1222 if (flags
& XATTR_REPLACE
)
1229 if (flags
& XATTR_CREATE
)
1233 if (!is
.s
.not_found
)
1234 error
= ext4_xattr_ibody_set(inode
, &i
, &is
);
1235 else if (!bs
.s
.not_found
)
1236 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1239 /* Xattr value did not change? Save us some work and bail out */
1240 if (!is
.s
.not_found
&& ext4_xattr_value_same(&is
.s
, &i
))
1242 if (!bs
.s
.not_found
&& ext4_xattr_value_same(&bs
.s
, &i
))
1245 error
= ext4_xattr_ibody_set(inode
, &i
, &is
);
1246 if (!error
&& !bs
.s
.not_found
) {
1248 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1249 } else if (error
== -ENOSPC
) {
1250 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1251 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1255 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1258 if (!is
.s
.not_found
) {
1260 error
= ext4_xattr_ibody_set(inode
, &i
, &is
);
1265 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1266 inode
->i_ctime
= current_time(inode
);
1269 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1271 * The bh is consumed by ext4_mark_iloc_dirty, even with
1276 ext4_handle_sync(handle
);
1282 ext4_write_unlock_xattr(inode
, &no_expand
);
1289 * Like ext4_xattr_set_handle, but start from an inode. This extended
1290 * attribute modification is a filesystem transaction by itself.
1292 * Returns 0, or a negative error number on failure.
1295 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1296 const void *value
, size_t value_len
, int flags
)
1299 int error
, retries
= 0;
1300 int credits
= ext4_jbd2_credits_xattr(inode
);
1303 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
1304 if (IS_ERR(handle
)) {
1305 error
= PTR_ERR(handle
);
1309 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1310 value
, value_len
, flags
);
1311 error2
= ext4_journal_stop(handle
);
1312 if (error
== -ENOSPC
&&
1313 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1323 * Shift the EA entries in the inode to create space for the increased
1326 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1327 int value_offs_shift
, void *to
,
1328 void *from
, size_t n
)
1330 struct ext4_xattr_entry
*last
= entry
;
1333 /* We always shift xattr headers further thus offsets get lower */
1334 BUG_ON(value_offs_shift
> 0);
1336 /* Adjust the value offsets of the entries */
1337 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1338 if (last
->e_value_size
) {
1339 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1341 last
->e_value_offs
= cpu_to_le16(new_offs
);
1344 /* Shift the entries by n bytes */
1345 memmove(to
, from
, n
);
1349 * Move xattr pointed to by 'entry' from inode into external xattr block
1351 static int ext4_xattr_move_to_block(handle_t
*handle
, struct inode
*inode
,
1352 struct ext4_inode
*raw_inode
,
1353 struct ext4_xattr_entry
*entry
)
1355 struct ext4_xattr_ibody_find
*is
= NULL
;
1356 struct ext4_xattr_block_find
*bs
= NULL
;
1357 char *buffer
= NULL
, *b_entry_name
= NULL
;
1358 size_t value_offs
, value_size
;
1359 struct ext4_xattr_info i
= {
1362 .name_index
= entry
->e_name_index
,
1364 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
1367 value_offs
= le16_to_cpu(entry
->e_value_offs
);
1368 value_size
= le32_to_cpu(entry
->e_value_size
);
1370 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1371 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1372 buffer
= kmalloc(value_size
, GFP_NOFS
);
1373 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1374 if (!is
|| !bs
|| !buffer
|| !b_entry_name
) {
1379 is
->s
.not_found
= -ENODATA
;
1380 bs
->s
.not_found
= -ENODATA
;
1384 /* Save the entry name and the entry value */
1385 memcpy(buffer
, (void *)IFIRST(header
) + value_offs
, value_size
);
1386 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1387 b_entry_name
[entry
->e_name_len
] = '\0';
1388 i
.name
= b_entry_name
;
1390 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1394 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1398 /* Remove the chosen entry from the inode */
1399 error
= ext4_xattr_ibody_set(inode
, &i
, is
);
1403 i
.name
= b_entry_name
;
1405 i
.value_len
= value_size
;
1406 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1410 /* Add entry which was removed from the inode into the block */
1411 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1416 kfree(b_entry_name
);
1419 brelse(is
->iloc
.bh
);
1426 static int ext4_xattr_make_inode_space(handle_t
*handle
, struct inode
*inode
,
1427 struct ext4_inode
*raw_inode
,
1428 int isize_diff
, size_t ifree
,
1429 size_t bfree
, int *total_ino
)
1431 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
1432 struct ext4_xattr_entry
*small_entry
;
1433 struct ext4_xattr_entry
*entry
;
1434 struct ext4_xattr_entry
*last
;
1435 unsigned int entry_size
; /* EA entry size */
1436 unsigned int total_size
; /* EA entry size + value size */
1437 unsigned int min_total_size
;
1440 while (isize_diff
> ifree
) {
1443 min_total_size
= ~0U;
1444 last
= IFIRST(header
);
1445 /* Find the entry best suited to be pushed into EA block */
1446 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1448 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1449 EXT4_XATTR_LEN(last
->e_name_len
);
1450 if (total_size
<= bfree
&&
1451 total_size
< min_total_size
) {
1452 if (total_size
+ ifree
< isize_diff
) {
1456 min_total_size
= total_size
;
1461 if (entry
== NULL
) {
1462 if (small_entry
== NULL
)
1464 entry
= small_entry
;
1467 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1468 total_size
= entry_size
+
1469 EXT4_XATTR_SIZE(le32_to_cpu(entry
->e_value_size
));
1470 error
= ext4_xattr_move_to_block(handle
, inode
, raw_inode
,
1475 *total_ino
-= entry_size
;
1476 ifree
+= total_size
;
1477 bfree
-= total_size
;
1484 * Expand an inode by new_extra_isize bytes when EAs are present.
1485 * Returns 0 on success or negative error number on failure.
1487 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1488 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1490 struct ext4_xattr_ibody_header
*header
;
1491 struct buffer_head
*bh
= NULL
;
1493 size_t ifree
, bfree
;
1496 int error
= 0, tried_min_extra_isize
= 0;
1497 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1498 int isize_diff
; /* How much do we need to grow i_extra_isize */
1501 if (ext4_write_trylock_xattr(inode
, &no_expand
) == 0)
1505 isize_diff
= new_extra_isize
- EXT4_I(inode
)->i_extra_isize
;
1506 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
1509 header
= IHDR(inode
, raw_inode
);
1512 * Check if enough free space is available in the inode to shift the
1513 * entries ahead by new_extra_isize.
1516 base
= IFIRST(header
);
1517 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1518 min_offs
= end
- base
;
1519 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1521 error
= xattr_check_inode(inode
, header
, end
);
1525 ifree
= ext4_xattr_free_space(base
, &min_offs
, base
, &total_ino
);
1526 if (ifree
>= isize_diff
)
1530 * Enough free space isn't available in the inode, check if
1531 * EA block can hold new_extra_isize bytes.
1533 if (EXT4_I(inode
)->i_file_acl
) {
1534 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1538 if (ext4_xattr_check_block(inode
, bh
)) {
1539 EXT4_ERROR_INODE(inode
, "bad block %llu",
1540 EXT4_I(inode
)->i_file_acl
);
1541 error
= -EFSCORRUPTED
;
1545 end
= bh
->b_data
+ bh
->b_size
;
1546 min_offs
= end
- base
;
1547 bfree
= ext4_xattr_free_space(BFIRST(bh
), &min_offs
, base
,
1549 if (bfree
+ ifree
< isize_diff
) {
1550 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1551 tried_min_extra_isize
++;
1552 new_extra_isize
= s_min_extra_isize
;
1560 bfree
= inode
->i_sb
->s_blocksize
;
1563 error
= ext4_xattr_make_inode_space(handle
, inode
, raw_inode
,
1564 isize_diff
, ifree
, bfree
,
1567 if (error
== -ENOSPC
&& !tried_min_extra_isize
&&
1568 s_min_extra_isize
) {
1569 tried_min_extra_isize
++;
1570 new_extra_isize
= s_min_extra_isize
;
1577 /* Adjust the offsets and shift the remaining entries ahead */
1578 ext4_xattr_shift_entries(IFIRST(header
), EXT4_I(inode
)->i_extra_isize
1579 - new_extra_isize
, (void *)raw_inode
+
1580 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1581 (void *)header
, total_ino
);
1582 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1585 ext4_write_unlock_xattr(inode
, &no_expand
);
1591 * Inode size expansion failed; don't try again
1594 ext4_write_unlock_xattr(inode
, &no_expand
);
1601 * ext4_xattr_delete_inode()
1603 * Free extended attribute resources associated with this inode. This
1604 * is called immediately before an inode is freed. We have exclusive
1605 * access to the inode.
1608 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1610 struct buffer_head
*bh
= NULL
;
1612 if (!EXT4_I(inode
)->i_file_acl
)
1614 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1616 EXT4_ERROR_INODE(inode
, "block %llu read error",
1617 EXT4_I(inode
)->i_file_acl
);
1620 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1621 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1622 EXT4_ERROR_INODE(inode
, "bad block %llu",
1623 EXT4_I(inode
)->i_file_acl
);
1626 ext4_xattr_release_block(handle
, inode
, bh
);
1627 EXT4_I(inode
)->i_file_acl
= 0;
1634 * ext4_xattr_cache_insert()
1636 * Create a new entry in the extended attribute cache, and insert
1637 * it unless such an entry is already in the cache.
1639 * Returns 0, or a negative error number on failure.
1642 ext4_xattr_cache_insert(struct mb_cache
*ext4_mb_cache
, struct buffer_head
*bh
)
1644 struct ext4_xattr_header
*header
= BHDR(bh
);
1645 __u32 hash
= le32_to_cpu(header
->h_hash
);
1646 int reusable
= le32_to_cpu(header
->h_refcount
) <
1647 EXT4_XATTR_REFCOUNT_MAX
;
1650 error
= mb_cache_entry_create(ext4_mb_cache
, GFP_NOFS
, hash
,
1651 bh
->b_blocknr
, reusable
);
1653 if (error
== -EBUSY
)
1654 ea_bdebug(bh
, "already in cache");
1656 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1662 * Compare two extended attribute blocks for equality.
1664 * Returns 0 if the blocks are equal, 1 if they differ, and
1665 * a negative error number on errors.
1668 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1669 struct ext4_xattr_header
*header2
)
1671 struct ext4_xattr_entry
*entry1
, *entry2
;
1673 entry1
= ENTRY(header1
+1);
1674 entry2
= ENTRY(header2
+1);
1675 while (!IS_LAST_ENTRY(entry1
)) {
1676 if (IS_LAST_ENTRY(entry2
))
1678 if (entry1
->e_hash
!= entry2
->e_hash
||
1679 entry1
->e_name_index
!= entry2
->e_name_index
||
1680 entry1
->e_name_len
!= entry2
->e_name_len
||
1681 entry1
->e_value_size
!= entry2
->e_value_size
||
1682 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1684 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1685 return -EFSCORRUPTED
;
1686 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1687 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1688 le32_to_cpu(entry1
->e_value_size
)))
1691 entry1
= EXT4_XATTR_NEXT(entry1
);
1692 entry2
= EXT4_XATTR_NEXT(entry2
);
1694 if (!IS_LAST_ENTRY(entry2
))
1700 * ext4_xattr_cache_find()
1702 * Find an identical extended attribute block.
1704 * Returns a pointer to the block found, or NULL if such a block was
1705 * not found or an error occurred.
1707 static struct buffer_head
*
1708 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1709 struct mb_cache_entry
**pce
)
1711 __u32 hash
= le32_to_cpu(header
->h_hash
);
1712 struct mb_cache_entry
*ce
;
1713 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
1715 if (!header
->h_hash
)
1716 return NULL
; /* never share */
1717 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1718 ce
= mb_cache_entry_find_first(ext4_mb_cache
, hash
);
1720 struct buffer_head
*bh
;
1722 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1724 EXT4_ERROR_INODE(inode
, "block %lu read error",
1725 (unsigned long) ce
->e_block
);
1726 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1731 ce
= mb_cache_entry_find_next(ext4_mb_cache
, ce
);
1736 #define NAME_HASH_SHIFT 5
1737 #define VALUE_HASH_SHIFT 16
1740 * ext4_xattr_hash_entry()
1742 * Compute the hash of an extended attribute.
1744 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1745 struct ext4_xattr_entry
*entry
)
1748 char *name
= entry
->e_name
;
1751 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1752 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1753 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1757 if (entry
->e_value_size
!= 0) {
1758 __le32
*value
= (__le32
*)((char *)header
+
1759 le16_to_cpu(entry
->e_value_offs
));
1760 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1761 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1762 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1763 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1764 le32_to_cpu(*value
++);
1767 entry
->e_hash
= cpu_to_le32(hash
);
1770 #undef NAME_HASH_SHIFT
1771 #undef VALUE_HASH_SHIFT
1773 #define BLOCK_HASH_SHIFT 16
1776 * ext4_xattr_rehash()
1778 * Re-compute the extended attribute hash value after an entry has changed.
1780 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1781 struct ext4_xattr_entry
*entry
)
1783 struct ext4_xattr_entry
*here
;
1786 ext4_xattr_hash_entry(header
, entry
);
1787 here
= ENTRY(header
+1);
1788 while (!IS_LAST_ENTRY(here
)) {
1789 if (!here
->e_hash
) {
1790 /* Block is not shared if an entry's hash value == 0 */
1794 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1795 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1796 le32_to_cpu(here
->e_hash
);
1797 here
= EXT4_XATTR_NEXT(here
);
1799 header
->h_hash
= cpu_to_le32(hash
);
1802 #undef BLOCK_HASH_SHIFT
1804 #define HASH_BUCKET_BITS 10
1807 ext4_xattr_create_cache(void)
1809 return mb_cache_create(HASH_BUCKET_BITS
);
1812 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
1815 mb_cache_destroy(cache
);