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