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