]>
Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/xattr.c |
ac27a0ec DK |
3 | * |
4 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <[email protected]> | |
5 | * | |
6 | * Fix by Harrison Xing <[email protected]>. | |
617ba13b | 7 | * Ext4 code with a lot of help from Eric Jarman <[email protected]>. |
ac27a0ec DK |
8 | * Extended attributes for symlinks and special files added per |
9 | * suggestion of Luka Renko <[email protected]>. | |
10 | * xattr consolidation Copyright (c) 2004 James Morris <[email protected]>, | |
11 | * Red Hat Inc. | |
12 | * ea-in-inode support by Alex Tomas <[email protected]> aka bzzz | |
13 | * and Andreas Gruenbacher <[email protected]>. | |
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 **); | |
617ba13b MC |
80 | static void ext4_xattr_rehash(struct ext4_xattr_header *, |
81 | struct ext4_xattr_entry *); | |
ac27a0ec | 82 | |
d6006186 | 83 | static const struct xattr_handler * const ext4_xattr_handler_map[] = { |
617ba13b | 84 | [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler, |
03010a33 | 85 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
64e178a7 CH |
86 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, |
87 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, | |
ac27a0ec | 88 | #endif |
617ba13b | 89 | [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler, |
03010a33 | 90 | #ifdef CONFIG_EXT4_FS_SECURITY |
617ba13b | 91 | [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler, |
ac27a0ec DK |
92 | #endif |
93 | }; | |
94 | ||
11e27528 | 95 | const struct xattr_handler *ext4_xattr_handlers[] = { |
617ba13b MC |
96 | &ext4_xattr_user_handler, |
97 | &ext4_xattr_trusted_handler, | |
03010a33 | 98 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
64e178a7 CH |
99 | &posix_acl_access_xattr_handler, |
100 | &posix_acl_default_xattr_handler, | |
ac27a0ec | 101 | #endif |
03010a33 | 102 | #ifdef CONFIG_EXT4_FS_SECURITY |
617ba13b | 103 | &ext4_xattr_security_handler, |
ac27a0ec DK |
104 | #endif |
105 | NULL | |
106 | }; | |
107 | ||
47387409 TE |
108 | #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \ |
109 | inode->i_sb->s_fs_info)->s_ea_block_cache) | |
9c191f70 | 110 | |
33d201e0 TE |
111 | #ifdef CONFIG_LOCKDEP |
112 | void ext4_xattr_inode_set_class(struct inode *ea_inode) | |
113 | { | |
114 | lockdep_set_subclass(&ea_inode->i_rwsem, 1); | |
115 | } | |
116 | #endif | |
117 | ||
cc8e94fd DW |
118 | static __le32 ext4_xattr_block_csum(struct inode *inode, |
119 | sector_t block_nr, | |
120 | struct ext4_xattr_header *hdr) | |
121 | { | |
122 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
d6a77105 | 123 | __u32 csum; |
d6a77105 | 124 | __le64 dsk_block_nr = cpu_to_le64(block_nr); |
b47820ed DJ |
125 | __u32 dummy_csum = 0; |
126 | int offset = offsetof(struct ext4_xattr_header, h_checksum); | |
cc8e94fd | 127 | |
d6a77105 TT |
128 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr, |
129 | sizeof(dsk_block_nr)); | |
b47820ed DJ |
130 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset); |
131 | csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); | |
132 | offset += sizeof(dummy_csum); | |
133 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset, | |
134 | EXT4_BLOCK_SIZE(inode->i_sb) - offset); | |
41eb70dd | 135 | |
cc8e94fd DW |
136 | return cpu_to_le32(csum); |
137 | } | |
138 | ||
139 | static int ext4_xattr_block_csum_verify(struct inode *inode, | |
dac7a4b4 | 140 | struct buffer_head *bh) |
cc8e94fd | 141 | { |
dac7a4b4 TT |
142 | struct ext4_xattr_header *hdr = BHDR(bh); |
143 | int ret = 1; | |
cc8e94fd | 144 | |
dac7a4b4 TT |
145 | if (ext4_has_metadata_csum(inode->i_sb)) { |
146 | lock_buffer(bh); | |
147 | ret = (hdr->h_checksum == ext4_xattr_block_csum(inode, | |
148 | bh->b_blocknr, hdr)); | |
149 | unlock_buffer(bh); | |
150 | } | |
151 | return ret; | |
cc8e94fd DW |
152 | } |
153 | ||
dac7a4b4 TT |
154 | static void ext4_xattr_block_csum_set(struct inode *inode, |
155 | struct buffer_head *bh) | |
cc8e94fd | 156 | { |
dac7a4b4 TT |
157 | if (ext4_has_metadata_csum(inode->i_sb)) |
158 | BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode, | |
159 | bh->b_blocknr, BHDR(bh)); | |
cc8e94fd DW |
160 | } |
161 | ||
11e27528 | 162 | static inline const struct xattr_handler * |
617ba13b | 163 | ext4_xattr_handler(int name_index) |
ac27a0ec | 164 | { |
11e27528 | 165 | const struct xattr_handler *handler = NULL; |
ac27a0ec | 166 | |
617ba13b MC |
167 | if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) |
168 | handler = ext4_xattr_handler_map[name_index]; | |
ac27a0ec DK |
169 | return handler; |
170 | } | |
171 | ||
ac27a0ec | 172 | static int |
2c4f9923 EB |
173 | ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end, |
174 | void *value_start) | |
ac27a0ec | 175 | { |
a0626e75 DW |
176 | struct ext4_xattr_entry *e = entry; |
177 | ||
d7614cc1 | 178 | /* Find the end of the names list */ |
a0626e75 DW |
179 | while (!IS_LAST_ENTRY(e)) { |
180 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); | |
ac27a0ec | 181 | if ((void *)next >= end) |
6a797d27 | 182 | return -EFSCORRUPTED; |
a0626e75 | 183 | e = next; |
ac27a0ec | 184 | } |
a0626e75 | 185 | |
d7614cc1 | 186 | /* Check the values */ |
a0626e75 | 187 | while (!IS_LAST_ENTRY(entry)) { |
e50e5129 AD |
188 | if (entry->e_value_size != 0 && |
189 | entry->e_value_inum == 0) { | |
d7614cc1 EB |
190 | u16 offs = le16_to_cpu(entry->e_value_offs); |
191 | u32 size = le32_to_cpu(entry->e_value_size); | |
192 | void *value; | |
193 | ||
194 | /* | |
195 | * The value cannot overlap the names, and the value | |
196 | * with padding cannot extend beyond 'end'. Check both | |
197 | * the padded and unpadded sizes, since the size may | |
198 | * overflow to 0 when adding padding. | |
199 | */ | |
200 | if (offs > end - value_start) | |
201 | return -EFSCORRUPTED; | |
202 | value = value_start + offs; | |
203 | if (value < (void *)e + sizeof(u32) || | |
204 | size > end - value || | |
205 | EXT4_XATTR_SIZE(size) > end - value) | |
206 | return -EFSCORRUPTED; | |
207 | } | |
a0626e75 DW |
208 | entry = EXT4_XATTR_NEXT(entry); |
209 | } | |
210 | ||
ac27a0ec DK |
211 | return 0; |
212 | } | |
213 | ||
214 | static inline int | |
cc8e94fd | 215 | ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh) |
ac27a0ec | 216 | { |
cc8e94fd DW |
217 | int error; |
218 | ||
219 | if (buffer_verified(bh)) | |
220 | return 0; | |
221 | ||
617ba13b | 222 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
ac27a0ec | 223 | BHDR(bh)->h_blocks != cpu_to_le32(1)) |
6a797d27 | 224 | return -EFSCORRUPTED; |
dac7a4b4 | 225 | if (!ext4_xattr_block_csum_verify(inode, bh)) |
6a797d27 | 226 | return -EFSBADCRC; |
2c4f9923 EB |
227 | error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size, |
228 | bh->b_data); | |
cc8e94fd DW |
229 | if (!error) |
230 | set_buffer_verified(bh); | |
231 | return error; | |
ac27a0ec DK |
232 | } |
233 | ||
9e92f48c TT |
234 | static int |
235 | __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header, | |
236 | void *end, const char *function, unsigned int line) | |
237 | { | |
9e92f48c TT |
238 | int error = -EFSCORRUPTED; |
239 | ||
290ab230 | 240 | if (end - (void *)header < sizeof(*header) + sizeof(u32) || |
19962509 | 241 | (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC))) |
9e92f48c | 242 | goto errout; |
2c4f9923 | 243 | error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header)); |
9e92f48c TT |
244 | errout: |
245 | if (error) | |
246 | __ext4_error_inode(inode, function, line, 0, | |
247 | "corrupted in-inode xattr"); | |
248 | return error; | |
249 | } | |
250 | ||
251 | #define xattr_check_inode(inode, header, end) \ | |
252 | __xattr_check_inode((inode), (header), (end), __func__, __LINE__) | |
253 | ||
ac27a0ec | 254 | static int |
617ba13b | 255 | ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index, |
6ba644b9 | 256 | const char *name, int sorted) |
ac27a0ec | 257 | { |
617ba13b | 258 | struct ext4_xattr_entry *entry; |
ac27a0ec DK |
259 | size_t name_len; |
260 | int cmp = 1; | |
261 | ||
262 | if (name == NULL) | |
263 | return -EINVAL; | |
264 | name_len = strlen(name); | |
265 | entry = *pentry; | |
617ba13b | 266 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
ac27a0ec DK |
267 | cmp = name_index - entry->e_name_index; |
268 | if (!cmp) | |
269 | cmp = name_len - entry->e_name_len; | |
270 | if (!cmp) | |
271 | cmp = memcmp(name, entry->e_name, name_len); | |
272 | if (cmp <= 0 && (sorted || cmp == 0)) | |
273 | break; | |
274 | } | |
275 | *pentry = entry; | |
ac27a0ec DK |
276 | return cmp ? -ENODATA : 0; |
277 | } | |
278 | ||
e50e5129 AD |
279 | /* |
280 | * Read the EA value from an inode. | |
281 | */ | |
90966693 | 282 | static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size) |
e50e5129 AD |
283 | { |
284 | unsigned long block = 0; | |
285 | struct buffer_head *bh = NULL; | |
90966693 TE |
286 | int blocksize = ea_inode->i_sb->s_blocksize; |
287 | size_t csize, copied = 0; | |
e50e5129 | 288 | |
90966693 TE |
289 | while (copied < size) { |
290 | csize = (size - copied) > blocksize ? blocksize : size - copied; | |
e50e5129 | 291 | bh = ext4_bread(NULL, ea_inode, block, 0); |
90966693 | 292 | if (IS_ERR(bh)) |
e50e5129 | 293 | return PTR_ERR(bh); |
90966693 TE |
294 | if (!bh) |
295 | return -EFSCORRUPTED; | |
296 | ||
e50e5129 AD |
297 | memcpy(buf, bh->b_data, csize); |
298 | brelse(bh); | |
299 | ||
300 | buf += csize; | |
301 | block += 1; | |
90966693 | 302 | copied += csize; |
e50e5129 | 303 | } |
e50e5129 AD |
304 | return 0; |
305 | } | |
306 | ||
bab79b04 TE |
307 | static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, |
308 | struct inode **ea_inode) | |
e50e5129 | 309 | { |
bab79b04 TE |
310 | struct inode *inode; |
311 | int err; | |
e50e5129 | 312 | |
bab79b04 TE |
313 | inode = ext4_iget(parent->i_sb, ea_ino); |
314 | if (IS_ERR(inode)) { | |
315 | err = PTR_ERR(inode); | |
e50e5129 | 316 | ext4_error(parent->i_sb, "error while reading EA inode %lu " |
bab79b04 TE |
317 | "err=%d", ea_ino, err); |
318 | return err; | |
e50e5129 AD |
319 | } |
320 | ||
bab79b04 TE |
321 | if (is_bad_inode(inode)) { |
322 | ext4_error(parent->i_sb, "error while reading EA inode %lu " | |
323 | "is_bad_inode", ea_ino); | |
324 | err = -EIO; | |
325 | goto error; | |
326 | } | |
327 | ||
328 | if (EXT4_XATTR_INODE_GET_PARENT(inode) != parent->i_ino || | |
329 | inode->i_generation != parent->i_generation) { | |
e50e5129 | 330 | ext4_error(parent->i_sb, "Backpointer from EA inode %lu " |
bab79b04 TE |
331 | "to parent is invalid.", ea_ino); |
332 | err = -EINVAL; | |
e50e5129 AD |
333 | goto error; |
334 | } | |
335 | ||
bab79b04 | 336 | if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { |
e50e5129 AD |
337 | ext4_error(parent->i_sb, "EA inode %lu does not have " |
338 | "EXT4_EA_INODE_FL flag set.\n", ea_ino); | |
bab79b04 | 339 | err = -EINVAL; |
e50e5129 AD |
340 | goto error; |
341 | } | |
342 | ||
bab79b04 TE |
343 | *ea_inode = inode; |
344 | return 0; | |
e50e5129 | 345 | error: |
bab79b04 TE |
346 | iput(inode); |
347 | return err; | |
e50e5129 AD |
348 | } |
349 | ||
350 | /* | |
351 | * Read the value from the EA inode. | |
352 | */ | |
353 | static int | |
354 | ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer, | |
90966693 | 355 | size_t size) |
e50e5129 | 356 | { |
bab79b04 TE |
357 | struct inode *ea_inode; |
358 | int ret; | |
e50e5129 | 359 | |
bab79b04 TE |
360 | ret = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode); |
361 | if (ret) | |
362 | return ret; | |
e50e5129 | 363 | |
bab79b04 | 364 | ret = ext4_xattr_inode_read(ea_inode, buffer, size); |
e50e5129 AD |
365 | iput(ea_inode); |
366 | ||
bab79b04 | 367 | return ret; |
e50e5129 AD |
368 | } |
369 | ||
ac27a0ec | 370 | static int |
617ba13b | 371 | ext4_xattr_block_get(struct inode *inode, int name_index, const char *name, |
ac27a0ec DK |
372 | void *buffer, size_t buffer_size) |
373 | { | |
374 | struct buffer_head *bh = NULL; | |
617ba13b | 375 | struct ext4_xattr_entry *entry; |
ac27a0ec DK |
376 | size_t size; |
377 | int error; | |
47387409 | 378 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
ac27a0ec DK |
379 | |
380 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", | |
381 | name_index, name, buffer, (long)buffer_size); | |
382 | ||
383 | error = -ENODATA; | |
617ba13b | 384 | if (!EXT4_I(inode)->i_file_acl) |
ac27a0ec | 385 | goto cleanup; |
ace36ad4 JP |
386 | ea_idebug(inode, "reading block %llu", |
387 | (unsigned long long)EXT4_I(inode)->i_file_acl); | |
617ba13b | 388 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
ac27a0ec DK |
389 | if (!bh) |
390 | goto cleanup; | |
391 | ea_bdebug(bh, "b_count=%d, refcount=%d", | |
392 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); | |
cc8e94fd | 393 | if (ext4_xattr_check_block(inode, bh)) { |
24676da4 TT |
394 | EXT4_ERROR_INODE(inode, "bad block %llu", |
395 | EXT4_I(inode)->i_file_acl); | |
6a797d27 | 396 | error = -EFSCORRUPTED; |
ac27a0ec DK |
397 | goto cleanup; |
398 | } | |
47387409 | 399 | ext4_xattr_block_cache_insert(ea_block_cache, bh); |
ac27a0ec | 400 | entry = BFIRST(bh); |
6ba644b9 | 401 | error = ext4_xattr_find_entry(&entry, name_index, name, 1); |
ac27a0ec DK |
402 | if (error) |
403 | goto cleanup; | |
404 | size = le32_to_cpu(entry->e_value_size); | |
405 | if (buffer) { | |
406 | error = -ERANGE; | |
407 | if (size > buffer_size) | |
408 | goto cleanup; | |
e50e5129 AD |
409 | if (entry->e_value_inum) { |
410 | error = ext4_xattr_inode_get(inode, | |
411 | le32_to_cpu(entry->e_value_inum), | |
90966693 | 412 | buffer, size); |
e50e5129 AD |
413 | if (error) |
414 | goto cleanup; | |
415 | } else { | |
416 | memcpy(buffer, bh->b_data + | |
417 | le16_to_cpu(entry->e_value_offs), size); | |
418 | } | |
ac27a0ec DK |
419 | } |
420 | error = size; | |
421 | ||
422 | cleanup: | |
423 | brelse(bh); | |
424 | return error; | |
425 | } | |
426 | ||
879b3825 | 427 | int |
617ba13b | 428 | ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, |
ac27a0ec DK |
429 | void *buffer, size_t buffer_size) |
430 | { | |
617ba13b MC |
431 | struct ext4_xattr_ibody_header *header; |
432 | struct ext4_xattr_entry *entry; | |
433 | struct ext4_inode *raw_inode; | |
434 | struct ext4_iloc iloc; | |
ac27a0ec DK |
435 | size_t size; |
436 | void *end; | |
437 | int error; | |
438 | ||
19f5fb7a | 439 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
ac27a0ec | 440 | return -ENODATA; |
617ba13b | 441 | error = ext4_get_inode_loc(inode, &iloc); |
ac27a0ec DK |
442 | if (error) |
443 | return error; | |
617ba13b | 444 | raw_inode = ext4_raw_inode(&iloc); |
ac27a0ec | 445 | header = IHDR(inode, raw_inode); |
617ba13b | 446 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
9e92f48c | 447 | error = xattr_check_inode(inode, header, end); |
ac27a0ec DK |
448 | if (error) |
449 | goto cleanup; | |
6ba644b9 EB |
450 | entry = IFIRST(header); |
451 | error = ext4_xattr_find_entry(&entry, name_index, name, 0); | |
ac27a0ec DK |
452 | if (error) |
453 | goto cleanup; | |
454 | size = le32_to_cpu(entry->e_value_size); | |
455 | if (buffer) { | |
456 | error = -ERANGE; | |
457 | if (size > buffer_size) | |
458 | goto cleanup; | |
e50e5129 AD |
459 | if (entry->e_value_inum) { |
460 | error = ext4_xattr_inode_get(inode, | |
461 | le32_to_cpu(entry->e_value_inum), | |
90966693 | 462 | buffer, size); |
e50e5129 AD |
463 | if (error) |
464 | goto cleanup; | |
465 | } else { | |
466 | memcpy(buffer, (void *)IFIRST(header) + | |
467 | le16_to_cpu(entry->e_value_offs), size); | |
468 | } | |
ac27a0ec DK |
469 | } |
470 | error = size; | |
471 | ||
472 | cleanup: | |
473 | brelse(iloc.bh); | |
474 | return error; | |
475 | } | |
476 | ||
477 | /* | |
617ba13b | 478 | * ext4_xattr_get() |
ac27a0ec DK |
479 | * |
480 | * Copy an extended attribute into the buffer | |
481 | * provided, or compute the buffer size required. | |
482 | * Buffer is NULL to compute the size of the buffer required. | |
483 | * | |
484 | * Returns a negative error number on failure, or the number of bytes | |
485 | * used / required on success. | |
486 | */ | |
487 | int | |
617ba13b | 488 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, |
ac27a0ec DK |
489 | void *buffer, size_t buffer_size) |
490 | { | |
491 | int error; | |
492 | ||
0db1ff22 TT |
493 | if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) |
494 | return -EIO; | |
495 | ||
230b8c1a ZZ |
496 | if (strlen(name) > 255) |
497 | return -ERANGE; | |
498 | ||
617ba13b MC |
499 | down_read(&EXT4_I(inode)->xattr_sem); |
500 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, | |
ac27a0ec DK |
501 | buffer_size); |
502 | if (error == -ENODATA) | |
617ba13b | 503 | error = ext4_xattr_block_get(inode, name_index, name, buffer, |
ac27a0ec | 504 | buffer_size); |
617ba13b | 505 | up_read(&EXT4_I(inode)->xattr_sem); |
ac27a0ec DK |
506 | return error; |
507 | } | |
508 | ||
509 | static int | |
431547b3 | 510 | ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, |
ac27a0ec DK |
511 | char *buffer, size_t buffer_size) |
512 | { | |
513 | size_t rest = buffer_size; | |
514 | ||
617ba13b | 515 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
11e27528 | 516 | const struct xattr_handler *handler = |
617ba13b | 517 | ext4_xattr_handler(entry->e_name_index); |
ac27a0ec | 518 | |
764a5c6b AG |
519 | if (handler && (!handler->list || handler->list(dentry))) { |
520 | const char *prefix = handler->prefix ?: handler->name; | |
521 | size_t prefix_len = strlen(prefix); | |
522 | size_t size = prefix_len + entry->e_name_len + 1; | |
523 | ||
ac27a0ec DK |
524 | if (buffer) { |
525 | if (size > rest) | |
526 | return -ERANGE; | |
764a5c6b AG |
527 | memcpy(buffer, prefix, prefix_len); |
528 | buffer += prefix_len; | |
529 | memcpy(buffer, entry->e_name, entry->e_name_len); | |
530 | buffer += entry->e_name_len; | |
531 | *buffer++ = 0; | |
ac27a0ec DK |
532 | } |
533 | rest -= size; | |
534 | } | |
535 | } | |
764a5c6b | 536 | return buffer_size - rest; /* total size */ |
ac27a0ec DK |
537 | } |
538 | ||
539 | static int | |
431547b3 | 540 | ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
ac27a0ec | 541 | { |
2b0143b5 | 542 | struct inode *inode = d_inode(dentry); |
ac27a0ec DK |
543 | struct buffer_head *bh = NULL; |
544 | int error; | |
545 | ||
546 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", | |
547 | buffer, (long)buffer_size); | |
548 | ||
549 | error = 0; | |
617ba13b | 550 | if (!EXT4_I(inode)->i_file_acl) |
ac27a0ec | 551 | goto cleanup; |
ace36ad4 JP |
552 | ea_idebug(inode, "reading block %llu", |
553 | (unsigned long long)EXT4_I(inode)->i_file_acl); | |
617ba13b | 554 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
ac27a0ec DK |
555 | error = -EIO; |
556 | if (!bh) | |
557 | goto cleanup; | |
558 | ea_bdebug(bh, "b_count=%d, refcount=%d", | |
559 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); | |
cc8e94fd | 560 | if (ext4_xattr_check_block(inode, bh)) { |
24676da4 TT |
561 | EXT4_ERROR_INODE(inode, "bad block %llu", |
562 | EXT4_I(inode)->i_file_acl); | |
6a797d27 | 563 | error = -EFSCORRUPTED; |
ac27a0ec DK |
564 | goto cleanup; |
565 | } | |
47387409 | 566 | ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh); |
431547b3 | 567 | error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size); |
ac27a0ec DK |
568 | |
569 | cleanup: | |
570 | brelse(bh); | |
571 | ||
572 | return error; | |
573 | } | |
574 | ||
575 | static int | |
431547b3 | 576 | ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
ac27a0ec | 577 | { |
2b0143b5 | 578 | struct inode *inode = d_inode(dentry); |
617ba13b MC |
579 | struct ext4_xattr_ibody_header *header; |
580 | struct ext4_inode *raw_inode; | |
581 | struct ext4_iloc iloc; | |
ac27a0ec DK |
582 | void *end; |
583 | int error; | |
584 | ||
19f5fb7a | 585 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
ac27a0ec | 586 | return 0; |
617ba13b | 587 | error = ext4_get_inode_loc(inode, &iloc); |
ac27a0ec DK |
588 | if (error) |
589 | return error; | |
617ba13b | 590 | raw_inode = ext4_raw_inode(&iloc); |
ac27a0ec | 591 | header = IHDR(inode, raw_inode); |
617ba13b | 592 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
9e92f48c | 593 | error = xattr_check_inode(inode, header, end); |
ac27a0ec DK |
594 | if (error) |
595 | goto cleanup; | |
431547b3 | 596 | error = ext4_xattr_list_entries(dentry, IFIRST(header), |
ac27a0ec DK |
597 | buffer, buffer_size); |
598 | ||
599 | cleanup: | |
600 | brelse(iloc.bh); | |
601 | return error; | |
602 | } | |
603 | ||
604 | /* | |
ba7ea1d8 EB |
605 | * Inode operation listxattr() |
606 | * | |
607 | * d_inode(dentry)->i_rwsem: don't care | |
ac27a0ec DK |
608 | * |
609 | * Copy a list of attribute names into the buffer | |
610 | * provided, or compute the buffer size required. | |
611 | * Buffer is NULL to compute the size of the buffer required. | |
612 | * | |
613 | * Returns a negative error number on failure, or the number of bytes | |
614 | * used / required on success. | |
615 | */ | |
ba7ea1d8 EB |
616 | ssize_t |
617 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) | |
ac27a0ec | 618 | { |
eaeef867 | 619 | int ret, ret2; |
ac27a0ec | 620 | |
2b0143b5 | 621 | down_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
eaeef867 TT |
622 | ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); |
623 | if (ret < 0) | |
624 | goto errout; | |
625 | if (buffer) { | |
626 | buffer += ret; | |
627 | buffer_size -= ret; | |
ac27a0ec | 628 | } |
eaeef867 TT |
629 | ret = ext4_xattr_block_list(dentry, buffer, buffer_size); |
630 | if (ret < 0) | |
631 | goto errout; | |
632 | ret += ret2; | |
633 | errout: | |
2b0143b5 | 634 | up_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
eaeef867 | 635 | return ret; |
ac27a0ec DK |
636 | } |
637 | ||
638 | /* | |
617ba13b | 639 | * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is |
ac27a0ec DK |
640 | * not set, set it. |
641 | */ | |
617ba13b | 642 | static void ext4_xattr_update_super_block(handle_t *handle, |
ac27a0ec DK |
643 | struct super_block *sb) |
644 | { | |
e2b911c5 | 645 | if (ext4_has_feature_xattr(sb)) |
ac27a0ec DK |
646 | return; |
647 | ||
5d601255 | 648 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); |
617ba13b | 649 | if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) { |
e2b911c5 | 650 | ext4_set_feature_xattr(sb); |
a0375156 | 651 | ext4_handle_dirty_super(handle, sb); |
ac27a0ec | 652 | } |
ac27a0ec DK |
653 | } |
654 | ||
655 | /* | |
ec4cb1aa JK |
656 | * Release the xattr block BH: If the reference count is > 1, decrement it; |
657 | * otherwise free the block. | |
ac27a0ec DK |
658 | */ |
659 | static void | |
617ba13b | 660 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, |
ac27a0ec DK |
661 | struct buffer_head *bh) |
662 | { | |
47387409 | 663 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
6048c64b | 664 | u32 hash, ref; |
8a2bfdcb | 665 | int error = 0; |
ac27a0ec | 666 | |
5d601255 | 667 | BUFFER_TRACE(bh, "get_write_access"); |
8a2bfdcb MC |
668 | error = ext4_journal_get_write_access(handle, bh); |
669 | if (error) | |
670 | goto out; | |
671 | ||
672 | lock_buffer(bh); | |
6048c64b AG |
673 | hash = le32_to_cpu(BHDR(bh)->h_hash); |
674 | ref = le32_to_cpu(BHDR(bh)->h_refcount); | |
675 | if (ref == 1) { | |
ac27a0ec | 676 | ea_bdebug(bh, "refcount now=0; freeing"); |
82939d79 JK |
677 | /* |
678 | * This must happen under buffer lock for | |
679 | * ext4_xattr_block_set() to reliably detect freed block | |
680 | */ | |
47387409 | 681 | mb_cache_entry_delete(ea_block_cache, hash, bh->b_blocknr); |
ac27a0ec | 682 | get_bh(bh); |
ec4cb1aa | 683 | unlock_buffer(bh); |
e6362609 TT |
684 | ext4_free_blocks(handle, inode, bh, 0, 1, |
685 | EXT4_FREE_BLOCKS_METADATA | | |
686 | EXT4_FREE_BLOCKS_FORGET); | |
ac27a0ec | 687 | } else { |
6048c64b AG |
688 | ref--; |
689 | BHDR(bh)->h_refcount = cpu_to_le32(ref); | |
690 | if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) { | |
691 | struct mb_cache_entry *ce; | |
692 | ||
47387409 | 693 | ce = mb_cache_entry_get(ea_block_cache, hash, |
6048c64b AG |
694 | bh->b_blocknr); |
695 | if (ce) { | |
696 | ce->e_reusable = 1; | |
47387409 | 697 | mb_cache_entry_put(ea_block_cache, ce); |
6048c64b AG |
698 | } |
699 | } | |
700 | ||
dac7a4b4 | 701 | ext4_xattr_block_csum_set(inode, bh); |
ec4cb1aa JK |
702 | /* |
703 | * Beware of this ugliness: Releasing of xattr block references | |
704 | * from different inodes can race and so we have to protect | |
705 | * from a race where someone else frees the block (and releases | |
706 | * its journal_head) before we are done dirtying the buffer. In | |
707 | * nojournal mode this race is harmless and we actually cannot | |
dac7a4b4 | 708 | * call ext4_handle_dirty_metadata() with locked buffer as |
ec4cb1aa JK |
709 | * that function can call sync_dirty_buffer() so for that case |
710 | * we handle the dirtying after unlocking the buffer. | |
711 | */ | |
712 | if (ext4_handle_valid(handle)) | |
dac7a4b4 | 713 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
c1bb05a6 | 714 | unlock_buffer(bh); |
ec4cb1aa | 715 | if (!ext4_handle_valid(handle)) |
dac7a4b4 | 716 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
8a2bfdcb | 717 | if (IS_SYNC(inode)) |
0390131b | 718 | ext4_handle_sync(handle); |
1231b3a1 | 719 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1)); |
8a2bfdcb MC |
720 | ea_bdebug(bh, "refcount now=%d; releasing", |
721 | le32_to_cpu(BHDR(bh)->h_refcount)); | |
ac27a0ec | 722 | } |
8a2bfdcb MC |
723 | out: |
724 | ext4_std_error(inode->i_sb, error); | |
725 | return; | |
ac27a0ec DK |
726 | } |
727 | ||
6dd4ee7c KS |
728 | /* |
729 | * Find the available free space for EAs. This also returns the total number of | |
730 | * bytes used by EA entries. | |
731 | */ | |
732 | static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, | |
733 | size_t *min_offs, void *base, int *total) | |
734 | { | |
735 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | |
e50e5129 | 736 | if (!last->e_value_inum && last->e_value_size) { |
6dd4ee7c KS |
737 | size_t offs = le16_to_cpu(last->e_value_offs); |
738 | if (offs < *min_offs) | |
739 | *min_offs = offs; | |
740 | } | |
7b1b2c1b TT |
741 | if (total) |
742 | *total += EXT4_XATTR_LEN(last->e_name_len); | |
6dd4ee7c KS |
743 | } |
744 | return (*min_offs - ((void *)last - base) - sizeof(__u32)); | |
745 | } | |
746 | ||
e50e5129 AD |
747 | /* |
748 | * Write the value of the EA in an inode. | |
749 | */ | |
750 | static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode, | |
751 | const void *buf, int bufsize) | |
752 | { | |
753 | struct buffer_head *bh = NULL; | |
754 | unsigned long block = 0; | |
755 | unsigned blocksize = ea_inode->i_sb->s_blocksize; | |
756 | unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits; | |
757 | int csize, wsize = 0; | |
758 | int ret = 0; | |
759 | int retries = 0; | |
760 | ||
761 | retry: | |
762 | while (ret >= 0 && ret < max_blocks) { | |
763 | struct ext4_map_blocks map; | |
764 | map.m_lblk = block += ret; | |
765 | map.m_len = max_blocks -= ret; | |
766 | ||
767 | ret = ext4_map_blocks(handle, ea_inode, &map, | |
768 | EXT4_GET_BLOCKS_CREATE); | |
769 | if (ret <= 0) { | |
770 | ext4_mark_inode_dirty(handle, ea_inode); | |
771 | if (ret == -ENOSPC && | |
772 | ext4_should_retry_alloc(ea_inode->i_sb, &retries)) { | |
773 | ret = 0; | |
774 | goto retry; | |
775 | } | |
776 | break; | |
777 | } | |
778 | } | |
779 | ||
780 | if (ret < 0) | |
781 | return ret; | |
782 | ||
783 | block = 0; | |
784 | while (wsize < bufsize) { | |
785 | if (bh != NULL) | |
786 | brelse(bh); | |
787 | csize = (bufsize - wsize) > blocksize ? blocksize : | |
788 | bufsize - wsize; | |
789 | bh = ext4_getblk(handle, ea_inode, block, 0); | |
790 | if (IS_ERR(bh)) | |
791 | return PTR_ERR(bh); | |
792 | ret = ext4_journal_get_write_access(handle, bh); | |
793 | if (ret) | |
794 | goto out; | |
795 | ||
796 | memcpy(bh->b_data, buf, csize); | |
797 | set_buffer_uptodate(bh); | |
798 | ext4_handle_dirty_metadata(handle, ea_inode, bh); | |
799 | ||
800 | buf += csize; | |
801 | wsize += csize; | |
802 | block += 1; | |
803 | } | |
804 | ||
805 | inode_lock(ea_inode); | |
806 | i_size_write(ea_inode, wsize); | |
807 | ext4_update_i_disksize(ea_inode, wsize); | |
808 | inode_unlock(ea_inode); | |
809 | ||
810 | ext4_mark_inode_dirty(handle, ea_inode); | |
811 | ||
812 | out: | |
813 | brelse(bh); | |
814 | ||
815 | return ret; | |
816 | } | |
817 | ||
818 | /* | |
819 | * Create an inode to store the value of a large EA. | |
820 | */ | |
821 | static struct inode *ext4_xattr_inode_create(handle_t *handle, | |
822 | struct inode *inode) | |
823 | { | |
824 | struct inode *ea_inode = NULL; | |
9e1ba001 | 825 | uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) }; |
bd3b963b | 826 | int err; |
e50e5129 AD |
827 | |
828 | /* | |
829 | * Let the next inode be the goal, so we try and allocate the EA inode | |
830 | * in the same group, or nearby one. | |
831 | */ | |
832 | ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode, | |
9e1ba001 | 833 | S_IFREG | 0600, NULL, inode->i_ino + 1, owner, |
1b917ed8 | 834 | EXT4_EA_INODE_FL); |
e50e5129 AD |
835 | if (!IS_ERR(ea_inode)) { |
836 | ea_inode->i_op = &ext4_file_inode_operations; | |
837 | ea_inode->i_fop = &ext4_file_operations; | |
838 | ext4_set_aops(ea_inode); | |
33d201e0 | 839 | ext4_xattr_inode_set_class(ea_inode); |
e50e5129 AD |
840 | ea_inode->i_generation = inode->i_generation; |
841 | EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL; | |
842 | ||
843 | /* | |
844 | * A back-pointer from EA inode to parent inode will be useful | |
845 | * for e2fsck. | |
846 | */ | |
847 | EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino); | |
848 | unlock_new_inode(ea_inode); | |
bd3b963b TE |
849 | err = ext4_inode_attach_jinode(ea_inode); |
850 | if (err) { | |
851 | iput(ea_inode); | |
852 | return ERR_PTR(err); | |
853 | } | |
e50e5129 AD |
854 | } |
855 | ||
856 | return ea_inode; | |
857 | } | |
858 | ||
859 | /* | |
860 | * Unlink the inode storing the value of the EA. | |
861 | */ | |
862 | int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino) | |
863 | { | |
864 | struct inode *ea_inode = NULL; | |
865 | int err; | |
866 | ||
bab79b04 | 867 | err = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode); |
e50e5129 AD |
868 | if (err) |
869 | return err; | |
870 | ||
871 | clear_nlink(ea_inode); | |
872 | iput(ea_inode); | |
873 | ||
874 | return 0; | |
875 | } | |
876 | ||
877 | /* | |
878 | * Add value of the EA in an inode. | |
879 | */ | |
880 | static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode, | |
881 | unsigned long *ea_ino, const void *value, | |
882 | size_t value_len) | |
883 | { | |
884 | struct inode *ea_inode; | |
885 | int err; | |
886 | ||
887 | /* Create an inode for the EA value */ | |
888 | ea_inode = ext4_xattr_inode_create(handle, inode); | |
889 | if (IS_ERR(ea_inode)) | |
890 | return PTR_ERR(ea_inode); | |
891 | ||
892 | err = ext4_xattr_inode_write(handle, ea_inode, value, value_len); | |
893 | if (err) | |
894 | clear_nlink(ea_inode); | |
895 | else | |
896 | *ea_ino = ea_inode->i_ino; | |
897 | ||
898 | iput(ea_inode); | |
899 | ||
900 | return err; | |
901 | } | |
902 | ||
903 | static int ext4_xattr_set_entry(struct ext4_xattr_info *i, | |
904 | struct ext4_xattr_search *s, | |
905 | handle_t *handle, struct inode *inode) | |
ac27a0ec | 906 | { |
617ba13b | 907 | struct ext4_xattr_entry *last; |
ac27a0ec | 908 | size_t free, min_offs = s->end - s->base, name_len = strlen(i->name); |
e50e5129 AD |
909 | int in_inode = i->in_inode; |
910 | int rc; | |
911 | ||
ac27a0ec DK |
912 | /* Compute min_offs and last. */ |
913 | last = s->first; | |
617ba13b | 914 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
e50e5129 | 915 | if (!last->e_value_inum && last->e_value_size) { |
ac27a0ec DK |
916 | size_t offs = le16_to_cpu(last->e_value_offs); |
917 | if (offs < min_offs) | |
918 | min_offs = offs; | |
919 | } | |
920 | } | |
921 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); | |
922 | if (!s->not_found) { | |
e50e5129 AD |
923 | if (!in_inode && |
924 | !s->here->e_value_inum && s->here->e_value_size) { | |
ac27a0ec | 925 | size_t size = le32_to_cpu(s->here->e_value_size); |
617ba13b | 926 | free += EXT4_XATTR_SIZE(size); |
ac27a0ec | 927 | } |
617ba13b | 928 | free += EXT4_XATTR_LEN(name_len); |
ac27a0ec DK |
929 | } |
930 | if (i->value) { | |
e50e5129 AD |
931 | size_t value_len = EXT4_XATTR_SIZE(i->value_len); |
932 | ||
933 | if (in_inode) | |
934 | value_len = 0; | |
935 | ||
936 | if (free < EXT4_XATTR_LEN(name_len) + value_len) | |
ac27a0ec DK |
937 | return -ENOSPC; |
938 | } | |
939 | ||
940 | if (i->value && s->not_found) { | |
941 | /* Insert the new name. */ | |
617ba13b | 942 | size_t size = EXT4_XATTR_LEN(name_len); |
ac27a0ec DK |
943 | size_t rest = (void *)last - (void *)s->here + sizeof(__u32); |
944 | memmove((void *)s->here + size, s->here, rest); | |
945 | memset(s->here, 0, size); | |
946 | s->here->e_name_index = i->name_index; | |
947 | s->here->e_name_len = name_len; | |
948 | memcpy(s->here->e_name, i->name, name_len); | |
949 | } else { | |
e50e5129 AD |
950 | if (!s->here->e_value_inum && s->here->e_value_size && |
951 | s->here->e_value_offs > 0) { | |
ac27a0ec DK |
952 | void *first_val = s->base + min_offs; |
953 | size_t offs = le16_to_cpu(s->here->e_value_offs); | |
954 | void *val = s->base + offs; | |
617ba13b | 955 | size_t size = EXT4_XATTR_SIZE( |
ac27a0ec DK |
956 | le32_to_cpu(s->here->e_value_size)); |
957 | ||
617ba13b | 958 | if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) { |
ac27a0ec DK |
959 | /* The old and the new value have the same |
960 | size. Just replace. */ | |
961 | s->here->e_value_size = | |
962 | cpu_to_le32(i->value_len); | |
bd9926e8 TT |
963 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
964 | memset(val, 0, size); | |
965 | } else { | |
966 | /* Clear pad bytes first. */ | |
967 | memset(val + size - EXT4_XATTR_PAD, 0, | |
968 | EXT4_XATTR_PAD); | |
969 | memcpy(val, i->value, i->value_len); | |
970 | } | |
ac27a0ec DK |
971 | return 0; |
972 | } | |
973 | ||
974 | /* Remove the old value. */ | |
975 | memmove(first_val + size, first_val, val - first_val); | |
976 | memset(first_val, 0, size); | |
977 | s->here->e_value_size = 0; | |
978 | s->here->e_value_offs = 0; | |
979 | min_offs += size; | |
980 | ||
981 | /* Adjust all value offsets. */ | |
982 | last = s->first; | |
983 | while (!IS_LAST_ENTRY(last)) { | |
984 | size_t o = le16_to_cpu(last->e_value_offs); | |
e50e5129 AD |
985 | if (!last->e_value_inum && |
986 | last->e_value_size && o < offs) | |
ac27a0ec DK |
987 | last->e_value_offs = |
988 | cpu_to_le16(o + size); | |
617ba13b | 989 | last = EXT4_XATTR_NEXT(last); |
ac27a0ec DK |
990 | } |
991 | } | |
e50e5129 AD |
992 | if (s->here->e_value_inum) { |
993 | ext4_xattr_inode_unlink(inode, | |
994 | le32_to_cpu(s->here->e_value_inum)); | |
995 | s->here->e_value_inum = 0; | |
996 | } | |
ac27a0ec DK |
997 | if (!i->value) { |
998 | /* Remove the old name. */ | |
617ba13b | 999 | size_t size = EXT4_XATTR_LEN(name_len); |
ac27a0ec DK |
1000 | last = ENTRY((void *)last - size); |
1001 | memmove(s->here, (void *)s->here + size, | |
1002 | (void *)last - (void *)s->here + sizeof(__u32)); | |
1003 | memset(last, 0, size); | |
1004 | } | |
1005 | } | |
1006 | ||
1007 | if (i->value) { | |
1008 | /* Insert the new value. */ | |
e50e5129 AD |
1009 | if (in_inode) { |
1010 | unsigned long ea_ino = | |
1011 | le32_to_cpu(s->here->e_value_inum); | |
1012 | rc = ext4_xattr_inode_set(handle, inode, &ea_ino, | |
1013 | i->value, i->value_len); | |
1014 | if (rc) | |
1015 | goto out; | |
1016 | s->here->e_value_inum = cpu_to_le32(ea_ino); | |
1017 | s->here->e_value_offs = 0; | |
1018 | } else if (i->value_len) { | |
617ba13b | 1019 | size_t size = EXT4_XATTR_SIZE(i->value_len); |
ac27a0ec DK |
1020 | void *val = s->base + min_offs - size; |
1021 | s->here->e_value_offs = cpu_to_le16(min_offs - size); | |
e50e5129 | 1022 | s->here->e_value_inum = 0; |
bd9926e8 TT |
1023 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
1024 | memset(val, 0, size); | |
1025 | } else { | |
1026 | /* Clear the pad bytes first. */ | |
1027 | memset(val + size - EXT4_XATTR_PAD, 0, | |
1028 | EXT4_XATTR_PAD); | |
1029 | memcpy(val, i->value, i->value_len); | |
1030 | } | |
ac27a0ec | 1031 | } |
e50e5129 | 1032 | s->here->e_value_size = cpu_to_le32(i->value_len); |
ac27a0ec | 1033 | } |
e50e5129 AD |
1034 | |
1035 | out: | |
1036 | return rc; | |
ac27a0ec DK |
1037 | } |
1038 | ||
617ba13b MC |
1039 | struct ext4_xattr_block_find { |
1040 | struct ext4_xattr_search s; | |
ac27a0ec DK |
1041 | struct buffer_head *bh; |
1042 | }; | |
1043 | ||
1044 | static int | |
617ba13b MC |
1045 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, |
1046 | struct ext4_xattr_block_find *bs) | |
ac27a0ec DK |
1047 | { |
1048 | struct super_block *sb = inode->i_sb; | |
1049 | int error; | |
1050 | ||
1051 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", | |
1052 | i->name_index, i->name, i->value, (long)i->value_len); | |
1053 | ||
617ba13b | 1054 | if (EXT4_I(inode)->i_file_acl) { |
ac27a0ec | 1055 | /* The inode already has an extended attribute block. */ |
617ba13b | 1056 | bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl); |
ac27a0ec DK |
1057 | error = -EIO; |
1058 | if (!bs->bh) | |
1059 | goto cleanup; | |
1060 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", | |
1061 | atomic_read(&(bs->bh->b_count)), | |
1062 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); | |
cc8e94fd | 1063 | if (ext4_xattr_check_block(inode, bs->bh)) { |
24676da4 TT |
1064 | EXT4_ERROR_INODE(inode, "bad block %llu", |
1065 | EXT4_I(inode)->i_file_acl); | |
6a797d27 | 1066 | error = -EFSCORRUPTED; |
ac27a0ec DK |
1067 | goto cleanup; |
1068 | } | |
1069 | /* Find the named attribute. */ | |
1070 | bs->s.base = BHDR(bs->bh); | |
1071 | bs->s.first = BFIRST(bs->bh); | |
1072 | bs->s.end = bs->bh->b_data + bs->bh->b_size; | |
1073 | bs->s.here = bs->s.first; | |
617ba13b | 1074 | error = ext4_xattr_find_entry(&bs->s.here, i->name_index, |
6ba644b9 | 1075 | i->name, 1); |
ac27a0ec DK |
1076 | if (error && error != -ENODATA) |
1077 | goto cleanup; | |
1078 | bs->s.not_found = error; | |
1079 | } | |
1080 | error = 0; | |
1081 | ||
1082 | cleanup: | |
1083 | return error; | |
1084 | } | |
1085 | ||
1086 | static int | |
617ba13b MC |
1087 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, |
1088 | struct ext4_xattr_info *i, | |
1089 | struct ext4_xattr_block_find *bs) | |
ac27a0ec DK |
1090 | { |
1091 | struct super_block *sb = inode->i_sb; | |
1092 | struct buffer_head *new_bh = NULL; | |
b347e2bc TE |
1093 | struct ext4_xattr_search s_copy = bs->s; |
1094 | struct ext4_xattr_search *s = &s_copy; | |
7a2508e1 | 1095 | struct mb_cache_entry *ce = NULL; |
8a2bfdcb | 1096 | int error = 0; |
47387409 | 1097 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
ac27a0ec | 1098 | |
617ba13b | 1099 | #define header(x) ((struct ext4_xattr_header *)(x)) |
ac27a0ec | 1100 | |
ac27a0ec | 1101 | if (s->base) { |
5d601255 | 1102 | BUFFER_TRACE(bs->bh, "get_write_access"); |
8a2bfdcb MC |
1103 | error = ext4_journal_get_write_access(handle, bs->bh); |
1104 | if (error) | |
1105 | goto cleanup; | |
1106 | lock_buffer(bs->bh); | |
1107 | ||
ac27a0ec | 1108 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { |
82939d79 JK |
1109 | __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash); |
1110 | ||
1111 | /* | |
1112 | * This must happen under buffer lock for | |
1113 | * ext4_xattr_block_set() to reliably detect modified | |
1114 | * block | |
1115 | */ | |
47387409 | 1116 | mb_cache_entry_delete(ea_block_cache, hash, |
c07dfcb4 | 1117 | bs->bh->b_blocknr); |
ac27a0ec | 1118 | ea_bdebug(bs->bh, "modifying in-place"); |
e50e5129 | 1119 | error = ext4_xattr_set_entry(i, s, handle, inode); |
ac27a0ec DK |
1120 | if (!error) { |
1121 | if (!IS_LAST_ENTRY(s->first)) | |
617ba13b | 1122 | ext4_xattr_rehash(header(s->base), |
ac27a0ec | 1123 | s->here); |
47387409 TE |
1124 | ext4_xattr_block_cache_insert(ea_block_cache, |
1125 | bs->bh); | |
ac27a0ec | 1126 | } |
dac7a4b4 | 1127 | ext4_xattr_block_csum_set(inode, bs->bh); |
ac27a0ec | 1128 | unlock_buffer(bs->bh); |
6a797d27 | 1129 | if (error == -EFSCORRUPTED) |
ac27a0ec DK |
1130 | goto bad_block; |
1131 | if (!error) | |
dac7a4b4 TT |
1132 | error = ext4_handle_dirty_metadata(handle, |
1133 | inode, | |
1134 | bs->bh); | |
ac27a0ec DK |
1135 | if (error) |
1136 | goto cleanup; | |
1137 | goto inserted; | |
1138 | } else { | |
1139 | int offset = (char *)s->here - bs->bh->b_data; | |
1140 | ||
8a2bfdcb | 1141 | unlock_buffer(bs->bh); |
ac27a0ec | 1142 | ea_bdebug(bs->bh, "cloning"); |
216553c4 | 1143 | s->base = kmalloc(bs->bh->b_size, GFP_NOFS); |
ac27a0ec DK |
1144 | error = -ENOMEM; |
1145 | if (s->base == NULL) | |
1146 | goto cleanup; | |
1147 | memcpy(s->base, BHDR(bs->bh), bs->bh->b_size); | |
1148 | s->first = ENTRY(header(s->base)+1); | |
1149 | header(s->base)->h_refcount = cpu_to_le32(1); | |
1150 | s->here = ENTRY(s->base + offset); | |
1151 | s->end = s->base + bs->bh->b_size; | |
1152 | } | |
1153 | } else { | |
1154 | /* Allocate a buffer where we construct the new block. */ | |
216553c4 | 1155 | s->base = kzalloc(sb->s_blocksize, GFP_NOFS); |
ac27a0ec DK |
1156 | /* assert(header == s->base) */ |
1157 | error = -ENOMEM; | |
1158 | if (s->base == NULL) | |
1159 | goto cleanup; | |
617ba13b | 1160 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
ac27a0ec DK |
1161 | header(s->base)->h_blocks = cpu_to_le32(1); |
1162 | header(s->base)->h_refcount = cpu_to_le32(1); | |
1163 | s->first = ENTRY(header(s->base)+1); | |
1164 | s->here = ENTRY(header(s->base)+1); | |
1165 | s->end = s->base + sb->s_blocksize; | |
1166 | } | |
1167 | ||
e50e5129 | 1168 | error = ext4_xattr_set_entry(i, s, handle, inode); |
6a797d27 | 1169 | if (error == -EFSCORRUPTED) |
ac27a0ec DK |
1170 | goto bad_block; |
1171 | if (error) | |
1172 | goto cleanup; | |
1173 | if (!IS_LAST_ENTRY(s->first)) | |
617ba13b | 1174 | ext4_xattr_rehash(header(s->base), s->here); |
ac27a0ec DK |
1175 | |
1176 | inserted: | |
1177 | if (!IS_LAST_ENTRY(s->first)) { | |
47387409 TE |
1178 | new_bh = ext4_xattr_block_cache_find(inode, header(s->base), |
1179 | &ce); | |
ac27a0ec DK |
1180 | if (new_bh) { |
1181 | /* We found an identical block in the cache. */ | |
1182 | if (new_bh == bs->bh) | |
1183 | ea_bdebug(new_bh, "keeping"); | |
1184 | else { | |
6048c64b AG |
1185 | u32 ref; |
1186 | ||
b8cb5a54 TE |
1187 | WARN_ON_ONCE(dquot_initialize_needed(inode)); |
1188 | ||
ac27a0ec DK |
1189 | /* The old block is released after updating |
1190 | the inode. */ | |
1231b3a1 LC |
1191 | error = dquot_alloc_block(inode, |
1192 | EXT4_C2B(EXT4_SB(sb), 1)); | |
5dd4056d | 1193 | if (error) |
ac27a0ec | 1194 | goto cleanup; |
5d601255 | 1195 | BUFFER_TRACE(new_bh, "get_write_access"); |
617ba13b | 1196 | error = ext4_journal_get_write_access(handle, |
ac27a0ec DK |
1197 | new_bh); |
1198 | if (error) | |
1199 | goto cleanup_dquot; | |
1200 | lock_buffer(new_bh); | |
82939d79 JK |
1201 | /* |
1202 | * We have to be careful about races with | |
6048c64b AG |
1203 | * freeing, rehashing or adding references to |
1204 | * xattr block. Once we hold buffer lock xattr | |
1205 | * block's state is stable so we can check | |
1206 | * whether the block got freed / rehashed or | |
1207 | * not. Since we unhash mbcache entry under | |
1208 | * buffer lock when freeing / rehashing xattr | |
1209 | * block, checking whether entry is still | |
1210 | * hashed is reliable. Same rules hold for | |
1211 | * e_reusable handling. | |
82939d79 | 1212 | */ |
6048c64b AG |
1213 | if (hlist_bl_unhashed(&ce->e_hash_list) || |
1214 | !ce->e_reusable) { | |
82939d79 JK |
1215 | /* |
1216 | * Undo everything and check mbcache | |
1217 | * again. | |
1218 | */ | |
1219 | unlock_buffer(new_bh); | |
1220 | dquot_free_block(inode, | |
1221 | EXT4_C2B(EXT4_SB(sb), | |
1222 | 1)); | |
1223 | brelse(new_bh); | |
47387409 | 1224 | mb_cache_entry_put(ea_block_cache, ce); |
82939d79 JK |
1225 | ce = NULL; |
1226 | new_bh = NULL; | |
1227 | goto inserted; | |
1228 | } | |
6048c64b AG |
1229 | ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1; |
1230 | BHDR(new_bh)->h_refcount = cpu_to_le32(ref); | |
1231 | if (ref >= EXT4_XATTR_REFCOUNT_MAX) | |
1232 | ce->e_reusable = 0; | |
ac27a0ec | 1233 | ea_bdebug(new_bh, "reusing; refcount now=%d", |
6048c64b | 1234 | ref); |
dac7a4b4 | 1235 | ext4_xattr_block_csum_set(inode, new_bh); |
ac27a0ec | 1236 | unlock_buffer(new_bh); |
dac7a4b4 TT |
1237 | error = ext4_handle_dirty_metadata(handle, |
1238 | inode, | |
1239 | new_bh); | |
ac27a0ec DK |
1240 | if (error) |
1241 | goto cleanup_dquot; | |
1242 | } | |
47387409 TE |
1243 | mb_cache_entry_touch(ea_block_cache, ce); |
1244 | mb_cache_entry_put(ea_block_cache, ce); | |
ac27a0ec DK |
1245 | ce = NULL; |
1246 | } else if (bs->bh && s->base == bs->bh->b_data) { | |
1247 | /* We were modifying this block in-place. */ | |
1248 | ea_bdebug(bs->bh, "keeping this block"); | |
1249 | new_bh = bs->bh; | |
1250 | get_bh(new_bh); | |
1251 | } else { | |
1252 | /* We need to allocate a new block */ | |
fb0a387d ES |
1253 | ext4_fsblk_t goal, block; |
1254 | ||
b8cb5a54 TE |
1255 | WARN_ON_ONCE(dquot_initialize_needed(inode)); |
1256 | ||
fb0a387d | 1257 | goal = ext4_group_first_block_no(sb, |
d00a6d7b | 1258 | EXT4_I(inode)->i_block_group); |
fb0a387d ES |
1259 | |
1260 | /* non-extent files can't have physical blocks past 2^32 */ | |
12e9b892 | 1261 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
fb0a387d ES |
1262 | goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; |
1263 | ||
55f020db AH |
1264 | block = ext4_new_meta_blocks(handle, inode, goal, 0, |
1265 | NULL, &error); | |
ac27a0ec DK |
1266 | if (error) |
1267 | goto cleanup; | |
fb0a387d | 1268 | |
12e9b892 | 1269 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
fb0a387d ES |
1270 | BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS); |
1271 | ||
ace36ad4 JP |
1272 | ea_idebug(inode, "creating block %llu", |
1273 | (unsigned long long)block); | |
ac27a0ec DK |
1274 | |
1275 | new_bh = sb_getblk(sb, block); | |
aebf0243 | 1276 | if (unlikely(!new_bh)) { |
860d21e2 | 1277 | error = -ENOMEM; |
ac27a0ec | 1278 | getblk_failed: |
7dc57615 | 1279 | ext4_free_blocks(handle, inode, NULL, block, 1, |
e6362609 | 1280 | EXT4_FREE_BLOCKS_METADATA); |
ac27a0ec DK |
1281 | goto cleanup; |
1282 | } | |
1283 | lock_buffer(new_bh); | |
617ba13b | 1284 | error = ext4_journal_get_create_access(handle, new_bh); |
ac27a0ec DK |
1285 | if (error) { |
1286 | unlock_buffer(new_bh); | |
860d21e2 | 1287 | error = -EIO; |
ac27a0ec DK |
1288 | goto getblk_failed; |
1289 | } | |
1290 | memcpy(new_bh->b_data, s->base, new_bh->b_size); | |
dac7a4b4 | 1291 | ext4_xattr_block_csum_set(inode, new_bh); |
ac27a0ec DK |
1292 | set_buffer_uptodate(new_bh); |
1293 | unlock_buffer(new_bh); | |
47387409 | 1294 | ext4_xattr_block_cache_insert(ea_block_cache, new_bh); |
dac7a4b4 TT |
1295 | error = ext4_handle_dirty_metadata(handle, inode, |
1296 | new_bh); | |
ac27a0ec DK |
1297 | if (error) |
1298 | goto cleanup; | |
1299 | } | |
1300 | } | |
1301 | ||
1302 | /* Update the inode. */ | |
617ba13b | 1303 | EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0; |
ac27a0ec DK |
1304 | |
1305 | /* Drop the previous xattr block. */ | |
1306 | if (bs->bh && bs->bh != new_bh) | |
617ba13b | 1307 | ext4_xattr_release_block(handle, inode, bs->bh); |
ac27a0ec DK |
1308 | error = 0; |
1309 | ||
1310 | cleanup: | |
1311 | if (ce) | |
47387409 | 1312 | mb_cache_entry_put(ea_block_cache, ce); |
ac27a0ec DK |
1313 | brelse(new_bh); |
1314 | if (!(bs->bh && s->base == bs->bh->b_data)) | |
1315 | kfree(s->base); | |
1316 | ||
1317 | return error; | |
1318 | ||
1319 | cleanup_dquot: | |
1231b3a1 | 1320 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1)); |
ac27a0ec DK |
1321 | goto cleanup; |
1322 | ||
1323 | bad_block: | |
24676da4 TT |
1324 | EXT4_ERROR_INODE(inode, "bad block %llu", |
1325 | EXT4_I(inode)->i_file_acl); | |
ac27a0ec DK |
1326 | goto cleanup; |
1327 | ||
1328 | #undef header | |
1329 | } | |
1330 | ||
879b3825 TM |
1331 | int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, |
1332 | struct ext4_xattr_ibody_find *is) | |
ac27a0ec | 1333 | { |
617ba13b MC |
1334 | struct ext4_xattr_ibody_header *header; |
1335 | struct ext4_inode *raw_inode; | |
ac27a0ec DK |
1336 | int error; |
1337 | ||
617ba13b | 1338 | if (EXT4_I(inode)->i_extra_isize == 0) |
ac27a0ec | 1339 | return 0; |
617ba13b | 1340 | raw_inode = ext4_raw_inode(&is->iloc); |
ac27a0ec DK |
1341 | header = IHDR(inode, raw_inode); |
1342 | is->s.base = is->s.first = IFIRST(header); | |
1343 | is->s.here = is->s.first; | |
617ba13b | 1344 | is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
19f5fb7a | 1345 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
9e92f48c | 1346 | error = xattr_check_inode(inode, header, is->s.end); |
ac27a0ec DK |
1347 | if (error) |
1348 | return error; | |
1349 | /* Find the named attribute. */ | |
617ba13b | 1350 | error = ext4_xattr_find_entry(&is->s.here, i->name_index, |
6ba644b9 | 1351 | i->name, 0); |
ac27a0ec DK |
1352 | if (error && error != -ENODATA) |
1353 | return error; | |
1354 | is->s.not_found = error; | |
1355 | } | |
1356 | return 0; | |
1357 | } | |
1358 | ||
0d812f77 TM |
1359 | int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode, |
1360 | struct ext4_xattr_info *i, | |
1361 | struct ext4_xattr_ibody_find *is) | |
1362 | { | |
1363 | struct ext4_xattr_ibody_header *header; | |
1364 | struct ext4_xattr_search *s = &is->s; | |
1365 | int error; | |
1366 | ||
1367 | if (EXT4_I(inode)->i_extra_isize == 0) | |
1368 | return -ENOSPC; | |
e50e5129 | 1369 | error = ext4_xattr_set_entry(i, s, handle, inode); |
0d812f77 TM |
1370 | if (error) { |
1371 | if (error == -ENOSPC && | |
1372 | ext4_has_inline_data(inode)) { | |
1373 | error = ext4_try_to_evict_inline_data(handle, inode, | |
1374 | EXT4_XATTR_LEN(strlen(i->name) + | |
1375 | EXT4_XATTR_SIZE(i->value_len))); | |
1376 | if (error) | |
1377 | return error; | |
1378 | error = ext4_xattr_ibody_find(inode, i, is); | |
1379 | if (error) | |
1380 | return error; | |
e50e5129 | 1381 | error = ext4_xattr_set_entry(i, s, handle, inode); |
0d812f77 TM |
1382 | } |
1383 | if (error) | |
1384 | return error; | |
1385 | } | |
1386 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); | |
1387 | if (!IS_LAST_ENTRY(s->first)) { | |
1388 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); | |
1389 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); | |
1390 | } else { | |
1391 | header->h_magic = cpu_to_le32(0); | |
1392 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); | |
1393 | } | |
1394 | return 0; | |
1395 | } | |
1396 | ||
e50e5129 | 1397 | static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode, |
0d812f77 TM |
1398 | struct ext4_xattr_info *i, |
1399 | struct ext4_xattr_ibody_find *is) | |
ac27a0ec | 1400 | { |
617ba13b MC |
1401 | struct ext4_xattr_ibody_header *header; |
1402 | struct ext4_xattr_search *s = &is->s; | |
ac27a0ec DK |
1403 | int error; |
1404 | ||
617ba13b | 1405 | if (EXT4_I(inode)->i_extra_isize == 0) |
ac27a0ec | 1406 | return -ENOSPC; |
e50e5129 | 1407 | error = ext4_xattr_set_entry(i, s, handle, inode); |
ac27a0ec DK |
1408 | if (error) |
1409 | return error; | |
617ba13b | 1410 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
ac27a0ec | 1411 | if (!IS_LAST_ENTRY(s->first)) { |
617ba13b | 1412 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
19f5fb7a | 1413 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
ac27a0ec DK |
1414 | } else { |
1415 | header->h_magic = cpu_to_le32(0); | |
19f5fb7a | 1416 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
ac27a0ec DK |
1417 | } |
1418 | return 0; | |
1419 | } | |
1420 | ||
3fd16462 JK |
1421 | static int ext4_xattr_value_same(struct ext4_xattr_search *s, |
1422 | struct ext4_xattr_info *i) | |
1423 | { | |
1424 | void *value; | |
1425 | ||
0bd454c0 TE |
1426 | /* When e_value_inum is set the value is stored externally. */ |
1427 | if (s->here->e_value_inum) | |
1428 | return 0; | |
3fd16462 JK |
1429 | if (le32_to_cpu(s->here->e_value_size) != i->value_len) |
1430 | return 0; | |
1431 | value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs); | |
1432 | return !memcmp(value, i->value, i->value_len); | |
1433 | } | |
1434 | ||
ac27a0ec | 1435 | /* |
617ba13b | 1436 | * ext4_xattr_set_handle() |
ac27a0ec | 1437 | * |
6e9510b0 | 1438 | * Create, replace or remove an extended attribute for this inode. Value |
ac27a0ec DK |
1439 | * is NULL to remove an existing extended attribute, and non-NULL to |
1440 | * either replace an existing extended attribute, or create a new extended | |
1441 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE | |
1442 | * specify that an extended attribute must exist and must not exist | |
1443 | * previous to the call, respectively. | |
1444 | * | |
1445 | * Returns 0, or a negative error number on failure. | |
1446 | */ | |
1447 | int | |
617ba13b | 1448 | ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, |
ac27a0ec DK |
1449 | const char *name, const void *value, size_t value_len, |
1450 | int flags) | |
1451 | { | |
617ba13b | 1452 | struct ext4_xattr_info i = { |
ac27a0ec DK |
1453 | .name_index = name_index, |
1454 | .name = name, | |
1455 | .value = value, | |
1456 | .value_len = value_len, | |
e50e5129 | 1457 | .in_inode = 0, |
ac27a0ec | 1458 | }; |
617ba13b | 1459 | struct ext4_xattr_ibody_find is = { |
ac27a0ec DK |
1460 | .s = { .not_found = -ENODATA, }, |
1461 | }; | |
617ba13b | 1462 | struct ext4_xattr_block_find bs = { |
ac27a0ec DK |
1463 | .s = { .not_found = -ENODATA, }, |
1464 | }; | |
c755e251 | 1465 | int no_expand; |
ac27a0ec DK |
1466 | int error; |
1467 | ||
1468 | if (!name) | |
1469 | return -EINVAL; | |
1470 | if (strlen(name) > 255) | |
1471 | return -ERANGE; | |
b8cb5a54 | 1472 | |
c755e251 | 1473 | ext4_write_lock_xattr(inode, &no_expand); |
4d20c685 | 1474 | |
c1a5d5f6 TE |
1475 | /* Check journal credits under write lock. */ |
1476 | if (ext4_handle_valid(handle)) { | |
1477 | int credits; | |
1478 | ||
1479 | credits = ext4_xattr_set_credits(inode, value_len); | |
1480 | if (!ext4_handle_has_enough_credits(handle, credits)) { | |
1481 | error = -ENOSPC; | |
1482 | goto cleanup; | |
1483 | } | |
1484 | } | |
1485 | ||
66543617 | 1486 | error = ext4_reserve_inode_write(handle, inode, &is.iloc); |
86ebfd08 ES |
1487 | if (error) |
1488 | goto cleanup; | |
1489 | ||
19f5fb7a | 1490 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) { |
617ba13b MC |
1491 | struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); |
1492 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); | |
19f5fb7a | 1493 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); |
ac27a0ec DK |
1494 | } |
1495 | ||
617ba13b | 1496 | error = ext4_xattr_ibody_find(inode, &i, &is); |
ac27a0ec DK |
1497 | if (error) |
1498 | goto cleanup; | |
1499 | if (is.s.not_found) | |
617ba13b | 1500 | error = ext4_xattr_block_find(inode, &i, &bs); |
ac27a0ec DK |
1501 | if (error) |
1502 | goto cleanup; | |
1503 | if (is.s.not_found && bs.s.not_found) { | |
1504 | error = -ENODATA; | |
1505 | if (flags & XATTR_REPLACE) | |
1506 | goto cleanup; | |
1507 | error = 0; | |
1508 | if (!value) | |
1509 | goto cleanup; | |
1510 | } else { | |
1511 | error = -EEXIST; | |
1512 | if (flags & XATTR_CREATE) | |
1513 | goto cleanup; | |
1514 | } | |
ac27a0ec DK |
1515 | if (!value) { |
1516 | if (!is.s.not_found) | |
e50e5129 | 1517 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
ac27a0ec | 1518 | else if (!bs.s.not_found) |
617ba13b | 1519 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
ac27a0ec | 1520 | } else { |
3fd16462 JK |
1521 | error = 0; |
1522 | /* Xattr value did not change? Save us some work and bail out */ | |
1523 | if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i)) | |
1524 | goto cleanup; | |
1525 | if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i)) | |
1526 | goto cleanup; | |
1527 | ||
b347e2bc TE |
1528 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
1529 | (EXT4_XATTR_SIZE(i.value_len) > | |
1530 | EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize))) | |
1531 | i.in_inode = 1; | |
1532 | retry_inode: | |
e50e5129 | 1533 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
ac27a0ec DK |
1534 | if (!error && !bs.s.not_found) { |
1535 | i.value = NULL; | |
617ba13b | 1536 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
ac27a0ec | 1537 | } else if (error == -ENOSPC) { |
7e01c8e5 TY |
1538 | if (EXT4_I(inode)->i_file_acl && !bs.s.base) { |
1539 | error = ext4_xattr_block_find(inode, &i, &bs); | |
1540 | if (error) | |
1541 | goto cleanup; | |
1542 | } | |
617ba13b | 1543 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
b347e2bc | 1544 | if (!error && !is.s.not_found) { |
ac27a0ec | 1545 | i.value = NULL; |
e50e5129 AD |
1546 | error = ext4_xattr_ibody_set(handle, inode, &i, |
1547 | &is); | |
b347e2bc TE |
1548 | } else if (error == -ENOSPC) { |
1549 | /* | |
1550 | * Xattr does not fit in the block, store at | |
1551 | * external inode if possible. | |
1552 | */ | |
1553 | if (ext4_has_feature_ea_inode(inode->i_sb) && | |
1554 | !i.in_inode) { | |
1555 | i.in_inode = 1; | |
1556 | goto retry_inode; | |
1557 | } | |
ac27a0ec DK |
1558 | } |
1559 | } | |
1560 | } | |
1561 | if (!error) { | |
617ba13b | 1562 | ext4_xattr_update_super_block(handle, inode->i_sb); |
eeca7ea1 | 1563 | inode->i_ctime = current_time(inode); |
6dd4ee7c | 1564 | if (!value) |
c755e251 | 1565 | no_expand = 0; |
617ba13b | 1566 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
ac27a0ec | 1567 | /* |
617ba13b | 1568 | * The bh is consumed by ext4_mark_iloc_dirty, even with |
ac27a0ec DK |
1569 | * error != 0. |
1570 | */ | |
1571 | is.iloc.bh = NULL; | |
1572 | if (IS_SYNC(inode)) | |
0390131b | 1573 | ext4_handle_sync(handle); |
ac27a0ec DK |
1574 | } |
1575 | ||
1576 | cleanup: | |
1577 | brelse(is.iloc.bh); | |
1578 | brelse(bs.bh); | |
c755e251 | 1579 | ext4_write_unlock_xattr(inode, &no_expand); |
ac27a0ec DK |
1580 | return error; |
1581 | } | |
1582 | ||
c1a5d5f6 TE |
1583 | int ext4_xattr_set_credits(struct inode *inode, size_t value_len) |
1584 | { | |
1585 | struct super_block *sb = inode->i_sb; | |
1586 | int credits; | |
1587 | ||
1588 | if (!EXT4_SB(sb)->s_journal) | |
1589 | return 0; | |
1590 | ||
1591 | credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb); | |
1592 | ||
1593 | /* | |
1594 | * In case of inline data, we may push out the data to a block, | |
1595 | * so we need to reserve credits for this eventuality | |
1596 | */ | |
1597 | if (ext4_has_inline_data(inode)) | |
1598 | credits += ext4_writepage_trans_blocks(inode) + 1; | |
1599 | ||
1600 | if (ext4_has_feature_ea_inode(sb)) { | |
1601 | int nrblocks = (value_len + sb->s_blocksize - 1) >> | |
1602 | sb->s_blocksize_bits; | |
1603 | ||
1604 | /* For new inode */ | |
1605 | credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3; | |
1606 | ||
1607 | /* For data blocks of EA inode */ | |
1608 | credits += ext4_meta_trans_blocks(inode, nrblocks, 0); | |
1609 | } | |
1610 | return credits; | |
1611 | } | |
1612 | ||
ac27a0ec | 1613 | /* |
617ba13b | 1614 | * ext4_xattr_set() |
ac27a0ec | 1615 | * |
617ba13b | 1616 | * Like ext4_xattr_set_handle, but start from an inode. This extended |
ac27a0ec DK |
1617 | * attribute modification is a filesystem transaction by itself. |
1618 | * | |
1619 | * Returns 0, or a negative error number on failure. | |
1620 | */ | |
1621 | int | |
617ba13b | 1622 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, |
ac27a0ec DK |
1623 | const void *value, size_t value_len, int flags) |
1624 | { | |
1625 | handle_t *handle; | |
e50e5129 | 1626 | struct super_block *sb = inode->i_sb; |
ac27a0ec | 1627 | int error, retries = 0; |
c1a5d5f6 | 1628 | int credits; |
ac27a0ec | 1629 | |
b8cb5a54 TE |
1630 | error = dquot_initialize(inode); |
1631 | if (error) | |
1632 | return error; | |
e50e5129 | 1633 | |
ac27a0ec | 1634 | retry: |
c1a5d5f6 | 1635 | credits = ext4_xattr_set_credits(inode, value_len); |
9924a92a | 1636 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
ac27a0ec DK |
1637 | if (IS_ERR(handle)) { |
1638 | error = PTR_ERR(handle); | |
1639 | } else { | |
1640 | int error2; | |
1641 | ||
617ba13b | 1642 | error = ext4_xattr_set_handle(handle, inode, name_index, name, |
ac27a0ec | 1643 | value, value_len, flags); |
617ba13b | 1644 | error2 = ext4_journal_stop(handle); |
ac27a0ec | 1645 | if (error == -ENOSPC && |
e50e5129 | 1646 | ext4_should_retry_alloc(sb, &retries)) |
ac27a0ec DK |
1647 | goto retry; |
1648 | if (error == 0) | |
1649 | error = error2; | |
1650 | } | |
1651 | ||
1652 | return error; | |
1653 | } | |
1654 | ||
6dd4ee7c KS |
1655 | /* |
1656 | * Shift the EA entries in the inode to create space for the increased | |
1657 | * i_extra_isize. | |
1658 | */ | |
1659 | static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, | |
1660 | int value_offs_shift, void *to, | |
94405713 | 1661 | void *from, size_t n) |
6dd4ee7c KS |
1662 | { |
1663 | struct ext4_xattr_entry *last = entry; | |
1664 | int new_offs; | |
1665 | ||
94405713 JK |
1666 | /* We always shift xattr headers further thus offsets get lower */ |
1667 | BUG_ON(value_offs_shift > 0); | |
1668 | ||
6dd4ee7c KS |
1669 | /* Adjust the value offsets of the entries */ |
1670 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | |
e50e5129 | 1671 | if (!last->e_value_inum && last->e_value_size) { |
6dd4ee7c KS |
1672 | new_offs = le16_to_cpu(last->e_value_offs) + |
1673 | value_offs_shift; | |
6dd4ee7c KS |
1674 | last->e_value_offs = cpu_to_le16(new_offs); |
1675 | } | |
1676 | } | |
1677 | /* Shift the entries by n bytes */ | |
1678 | memmove(to, from, n); | |
1679 | } | |
1680 | ||
3f2571c1 JK |
1681 | /* |
1682 | * Move xattr pointed to by 'entry' from inode into external xattr block | |
1683 | */ | |
1684 | static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, | |
1685 | struct ext4_inode *raw_inode, | |
1686 | struct ext4_xattr_entry *entry) | |
1687 | { | |
1688 | struct ext4_xattr_ibody_find *is = NULL; | |
1689 | struct ext4_xattr_block_find *bs = NULL; | |
1690 | char *buffer = NULL, *b_entry_name = NULL; | |
f6109100 | 1691 | size_t value_size = le32_to_cpu(entry->e_value_size); |
3f2571c1 JK |
1692 | struct ext4_xattr_info i = { |
1693 | .value = NULL, | |
1694 | .value_len = 0, | |
1695 | .name_index = entry->e_name_index, | |
f6109100 | 1696 | .in_inode = !!entry->e_value_inum, |
3f2571c1 JK |
1697 | }; |
1698 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); | |
1699 | int error; | |
1700 | ||
3f2571c1 JK |
1701 | is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); |
1702 | bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); | |
1703 | buffer = kmalloc(value_size, GFP_NOFS); | |
1704 | b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); | |
1705 | if (!is || !bs || !buffer || !b_entry_name) { | |
1706 | error = -ENOMEM; | |
1707 | goto out; | |
1708 | } | |
1709 | ||
1710 | is->s.not_found = -ENODATA; | |
1711 | bs->s.not_found = -ENODATA; | |
1712 | is->iloc.bh = NULL; | |
1713 | bs->bh = NULL; | |
1714 | ||
1715 | /* Save the entry name and the entry value */ | |
f6109100 TE |
1716 | if (entry->e_value_inum) { |
1717 | error = ext4_xattr_inode_get(inode, | |
1718 | le32_to_cpu(entry->e_value_inum), | |
1719 | buffer, value_size); | |
1720 | if (error) | |
1721 | goto out; | |
1722 | } else { | |
1723 | size_t value_offs = le16_to_cpu(entry->e_value_offs); | |
1724 | memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size); | |
1725 | } | |
1726 | ||
3f2571c1 JK |
1727 | memcpy(b_entry_name, entry->e_name, entry->e_name_len); |
1728 | b_entry_name[entry->e_name_len] = '\0'; | |
1729 | i.name = b_entry_name; | |
1730 | ||
1731 | error = ext4_get_inode_loc(inode, &is->iloc); | |
1732 | if (error) | |
1733 | goto out; | |
1734 | ||
1735 | error = ext4_xattr_ibody_find(inode, &i, is); | |
1736 | if (error) | |
1737 | goto out; | |
1738 | ||
1739 | /* Remove the chosen entry from the inode */ | |
e50e5129 | 1740 | error = ext4_xattr_ibody_set(handle, inode, &i, is); |
3f2571c1 JK |
1741 | if (error) |
1742 | goto out; | |
1743 | ||
3f2571c1 JK |
1744 | i.value = buffer; |
1745 | i.value_len = value_size; | |
1746 | error = ext4_xattr_block_find(inode, &i, bs); | |
1747 | if (error) | |
1748 | goto out; | |
1749 | ||
1750 | /* Add entry which was removed from the inode into the block */ | |
1751 | error = ext4_xattr_block_set(handle, inode, &i, bs); | |
1752 | if (error) | |
1753 | goto out; | |
1754 | error = 0; | |
1755 | out: | |
1756 | kfree(b_entry_name); | |
1757 | kfree(buffer); | |
1758 | if (is) | |
1759 | brelse(is->iloc.bh); | |
1760 | kfree(is); | |
1761 | kfree(bs); | |
1762 | ||
1763 | return error; | |
1764 | } | |
1765 | ||
dfa2064b JK |
1766 | static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode, |
1767 | struct ext4_inode *raw_inode, | |
1768 | int isize_diff, size_t ifree, | |
1769 | size_t bfree, int *total_ino) | |
1770 | { | |
1771 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); | |
1772 | struct ext4_xattr_entry *small_entry; | |
1773 | struct ext4_xattr_entry *entry; | |
1774 | struct ext4_xattr_entry *last; | |
1775 | unsigned int entry_size; /* EA entry size */ | |
1776 | unsigned int total_size; /* EA entry size + value size */ | |
1777 | unsigned int min_total_size; | |
1778 | int error; | |
1779 | ||
1780 | while (isize_diff > ifree) { | |
1781 | entry = NULL; | |
1782 | small_entry = NULL; | |
1783 | min_total_size = ~0U; | |
1784 | last = IFIRST(header); | |
1785 | /* Find the entry best suited to be pushed into EA block */ | |
1786 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | |
9bb21ced TE |
1787 | total_size = EXT4_XATTR_LEN(last->e_name_len); |
1788 | if (!last->e_value_inum) | |
1789 | total_size += EXT4_XATTR_SIZE( | |
1790 | le32_to_cpu(last->e_value_size)); | |
dfa2064b JK |
1791 | if (total_size <= bfree && |
1792 | total_size < min_total_size) { | |
1793 | if (total_size + ifree < isize_diff) { | |
1794 | small_entry = last; | |
1795 | } else { | |
1796 | entry = last; | |
1797 | min_total_size = total_size; | |
1798 | } | |
1799 | } | |
1800 | } | |
1801 | ||
1802 | if (entry == NULL) { | |
1803 | if (small_entry == NULL) | |
1804 | return -ENOSPC; | |
1805 | entry = small_entry; | |
1806 | } | |
1807 | ||
1808 | entry_size = EXT4_XATTR_LEN(entry->e_name_len); | |
9bb21ced TE |
1809 | total_size = entry_size; |
1810 | if (!entry->e_value_inum) | |
1811 | total_size += EXT4_XATTR_SIZE( | |
1812 | le32_to_cpu(entry->e_value_size)); | |
dfa2064b JK |
1813 | error = ext4_xattr_move_to_block(handle, inode, raw_inode, |
1814 | entry); | |
1815 | if (error) | |
1816 | return error; | |
1817 | ||
1818 | *total_ino -= entry_size; | |
1819 | ifree += total_size; | |
1820 | bfree -= total_size; | |
1821 | } | |
1822 | ||
1823 | return 0; | |
1824 | } | |
1825 | ||
6dd4ee7c KS |
1826 | /* |
1827 | * Expand an inode by new_extra_isize bytes when EAs are present. | |
1828 | * Returns 0 on success or negative error number on failure. | |
1829 | */ | |
1830 | int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, | |
1831 | struct ext4_inode *raw_inode, handle_t *handle) | |
1832 | { | |
1833 | struct ext4_xattr_ibody_header *header; | |
6dd4ee7c | 1834 | struct buffer_head *bh = NULL; |
e3014d14 JK |
1835 | size_t min_offs; |
1836 | size_t ifree, bfree; | |
7b1b2c1b | 1837 | int total_ino; |
6e0cd088 | 1838 | void *base, *end; |
d0141191 | 1839 | int error = 0, tried_min_extra_isize = 0; |
ac39849d | 1840 | int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize); |
d0141191 | 1841 | int isize_diff; /* How much do we need to grow i_extra_isize */ |
c755e251 TT |
1842 | int no_expand; |
1843 | ||
1844 | if (ext4_write_trylock_xattr(inode, &no_expand) == 0) | |
1845 | return 0; | |
6dd4ee7c | 1846 | |
6dd4ee7c | 1847 | retry: |
d0141191 | 1848 | isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize; |
2e81a4ee JK |
1849 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
1850 | goto out; | |
6dd4ee7c KS |
1851 | |
1852 | header = IHDR(inode, raw_inode); | |
6dd4ee7c KS |
1853 | |
1854 | /* | |
1855 | * Check if enough free space is available in the inode to shift the | |
1856 | * entries ahead by new_extra_isize. | |
1857 | */ | |
1858 | ||
6e0cd088 | 1859 | base = IFIRST(header); |
6dd4ee7c KS |
1860 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
1861 | min_offs = end - base; | |
6dd4ee7c KS |
1862 | total_ino = sizeof(struct ext4_xattr_ibody_header); |
1863 | ||
9e92f48c TT |
1864 | error = xattr_check_inode(inode, header, end); |
1865 | if (error) | |
1866 | goto cleanup; | |
1867 | ||
6e0cd088 | 1868 | ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino); |
e3014d14 JK |
1869 | if (ifree >= isize_diff) |
1870 | goto shift; | |
6dd4ee7c KS |
1871 | |
1872 | /* | |
1873 | * Enough free space isn't available in the inode, check if | |
1874 | * EA block can hold new_extra_isize bytes. | |
1875 | */ | |
1876 | if (EXT4_I(inode)->i_file_acl) { | |
1877 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); | |
1878 | error = -EIO; | |
1879 | if (!bh) | |
1880 | goto cleanup; | |
cc8e94fd | 1881 | if (ext4_xattr_check_block(inode, bh)) { |
24676da4 TT |
1882 | EXT4_ERROR_INODE(inode, "bad block %llu", |
1883 | EXT4_I(inode)->i_file_acl); | |
6a797d27 | 1884 | error = -EFSCORRUPTED; |
6dd4ee7c KS |
1885 | goto cleanup; |
1886 | } | |
1887 | base = BHDR(bh); | |
6dd4ee7c KS |
1888 | end = bh->b_data + bh->b_size; |
1889 | min_offs = end - base; | |
6e0cd088 JK |
1890 | bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base, |
1891 | NULL); | |
e3014d14 | 1892 | if (bfree + ifree < isize_diff) { |
6dd4ee7c KS |
1893 | if (!tried_min_extra_isize && s_min_extra_isize) { |
1894 | tried_min_extra_isize++; | |
1895 | new_extra_isize = s_min_extra_isize; | |
1896 | brelse(bh); | |
1897 | goto retry; | |
1898 | } | |
dfa2064b | 1899 | error = -ENOSPC; |
6dd4ee7c KS |
1900 | goto cleanup; |
1901 | } | |
1902 | } else { | |
e3014d14 | 1903 | bfree = inode->i_sb->s_blocksize; |
6dd4ee7c KS |
1904 | } |
1905 | ||
dfa2064b JK |
1906 | error = ext4_xattr_make_inode_space(handle, inode, raw_inode, |
1907 | isize_diff, ifree, bfree, | |
1908 | &total_ino); | |
1909 | if (error) { | |
1910 | if (error == -ENOSPC && !tried_min_extra_isize && | |
1911 | s_min_extra_isize) { | |
1912 | tried_min_extra_isize++; | |
1913 | new_extra_isize = s_min_extra_isize; | |
1914 | brelse(bh); | |
1915 | goto retry; | |
6dd4ee7c | 1916 | } |
dfa2064b | 1917 | goto cleanup; |
6dd4ee7c | 1918 | } |
e3014d14 JK |
1919 | shift: |
1920 | /* Adjust the offsets and shift the remaining entries ahead */ | |
6e0cd088 | 1921 | ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize |
e3014d14 JK |
1922 | - new_extra_isize, (void *)raw_inode + |
1923 | EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, | |
94405713 | 1924 | (void *)header, total_ino); |
e3014d14 | 1925 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
6dd4ee7c | 1926 | brelse(bh); |
2e81a4ee | 1927 | out: |
c755e251 | 1928 | ext4_write_unlock_xattr(inode, &no_expand); |
6dd4ee7c KS |
1929 | return 0; |
1930 | ||
1931 | cleanup: | |
6dd4ee7c | 1932 | brelse(bh); |
2e81a4ee | 1933 | /* |
c755e251 | 1934 | * Inode size expansion failed; don't try again |
2e81a4ee | 1935 | */ |
c755e251 TT |
1936 | no_expand = 1; |
1937 | ext4_write_unlock_xattr(inode, &no_expand); | |
6dd4ee7c KS |
1938 | return error; |
1939 | } | |
1940 | ||
1941 | ||
e50e5129 AD |
1942 | #define EIA_INCR 16 /* must be 2^n */ |
1943 | #define EIA_MASK (EIA_INCR - 1) | |
0421a189 TE |
1944 | /* Add the large xattr @inode into @ea_inode_array for later deletion. |
1945 | * If @ea_inode_array is new or full it will be grown and the old | |
e50e5129 AD |
1946 | * contents copied over. |
1947 | */ | |
1948 | static int | |
0421a189 TE |
1949 | ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, |
1950 | struct inode *inode) | |
e50e5129 | 1951 | { |
0421a189 | 1952 | if (*ea_inode_array == NULL) { |
e50e5129 AD |
1953 | /* |
1954 | * Start with 15 inodes, so it fits into a power-of-two size. | |
0421a189 | 1955 | * If *ea_inode_array is NULL, this is essentially offsetof() |
e50e5129 | 1956 | */ |
0421a189 TE |
1957 | (*ea_inode_array) = |
1958 | kmalloc(offsetof(struct ext4_xattr_inode_array, | |
1959 | inodes[EIA_MASK]), | |
e50e5129 | 1960 | GFP_NOFS); |
0421a189 | 1961 | if (*ea_inode_array == NULL) |
e50e5129 | 1962 | return -ENOMEM; |
0421a189 TE |
1963 | (*ea_inode_array)->count = 0; |
1964 | } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) { | |
e50e5129 | 1965 | /* expand the array once all 15 + n * 16 slots are full */ |
0421a189 TE |
1966 | struct ext4_xattr_inode_array *new_array = NULL; |
1967 | int count = (*ea_inode_array)->count; | |
e50e5129 AD |
1968 | |
1969 | /* if new_array is NULL, this is essentially offsetof() */ | |
1970 | new_array = kmalloc( | |
0421a189 TE |
1971 | offsetof(struct ext4_xattr_inode_array, |
1972 | inodes[count + EIA_INCR]), | |
e50e5129 AD |
1973 | GFP_NOFS); |
1974 | if (new_array == NULL) | |
1975 | return -ENOMEM; | |
0421a189 TE |
1976 | memcpy(new_array, *ea_inode_array, |
1977 | offsetof(struct ext4_xattr_inode_array, inodes[count])); | |
1978 | kfree(*ea_inode_array); | |
1979 | *ea_inode_array = new_array; | |
e50e5129 | 1980 | } |
0421a189 | 1981 | (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode; |
e50e5129 AD |
1982 | return 0; |
1983 | } | |
1984 | ||
1985 | /** | |
1986 | * Add xattr inode to orphan list | |
1987 | */ | |
1988 | static int | |
0421a189 TE |
1989 | ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode, int credits, |
1990 | struct ext4_xattr_inode_array *ea_inode_array) | |
e50e5129 | 1991 | { |
e50e5129 | 1992 | int idx = 0, error = 0; |
0421a189 | 1993 | struct inode *ea_inode; |
e50e5129 | 1994 | |
0421a189 | 1995 | if (ea_inode_array == NULL) |
e50e5129 AD |
1996 | return 0; |
1997 | ||
0421a189 | 1998 | for (; idx < ea_inode_array->count; ++idx) { |
e50e5129 AD |
1999 | if (!ext4_handle_has_enough_credits(handle, credits)) { |
2000 | error = ext4_journal_extend(handle, credits); | |
2001 | if (error > 0) | |
2002 | error = ext4_journal_restart(handle, credits); | |
2003 | ||
2004 | if (error != 0) { | |
2005 | ext4_warning(inode->i_sb, | |
2006 | "couldn't extend journal " | |
2007 | "(err %d)", error); | |
2008 | return error; | |
2009 | } | |
2010 | } | |
0421a189 | 2011 | ea_inode = ea_inode_array->inodes[idx]; |
0de5983d | 2012 | inode_lock(ea_inode); |
e50e5129 | 2013 | ext4_orphan_add(handle, ea_inode); |
0de5983d | 2014 | inode_unlock(ea_inode); |
e50e5129 AD |
2015 | /* the inode's i_count will be released by caller */ |
2016 | } | |
2017 | ||
2018 | return 0; | |
2019 | } | |
6dd4ee7c | 2020 | |
ac27a0ec | 2021 | /* |
617ba13b | 2022 | * ext4_xattr_delete_inode() |
ac27a0ec | 2023 | * |
e50e5129 AD |
2024 | * Free extended attribute resources associated with this inode. Traverse |
2025 | * all entries and unlink any xattr inodes associated with this inode. This | |
ac27a0ec | 2026 | * is called immediately before an inode is freed. We have exclusive |
e50e5129 AD |
2027 | * access to the inode. If an orphan inode is deleted it will also delete any |
2028 | * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget() | |
2029 | * to ensure they belong to the parent inode and were not deleted already. | |
ac27a0ec | 2030 | */ |
e50e5129 AD |
2031 | int |
2032 | ext4_xattr_delete_inode(handle_t *handle, struct inode *inode, | |
0421a189 | 2033 | struct ext4_xattr_inode_array **ea_inode_array) |
ac27a0ec DK |
2034 | { |
2035 | struct buffer_head *bh = NULL; | |
e50e5129 AD |
2036 | struct ext4_xattr_ibody_header *header; |
2037 | struct ext4_inode *raw_inode; | |
2038 | struct ext4_iloc iloc; | |
2039 | struct ext4_xattr_entry *entry; | |
0421a189 | 2040 | struct inode *ea_inode; |
990461dd | 2041 | unsigned int ea_ino; |
e50e5129 | 2042 | int credits = 3, error = 0; |
ac27a0ec | 2043 | |
e50e5129 AD |
2044 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
2045 | goto delete_external_ea; | |
2046 | ||
2047 | error = ext4_get_inode_loc(inode, &iloc); | |
2048 | if (error) | |
2049 | goto cleanup; | |
2050 | raw_inode = ext4_raw_inode(&iloc); | |
2051 | header = IHDR(inode, raw_inode); | |
2052 | for (entry = IFIRST(header); !IS_LAST_ENTRY(entry); | |
2053 | entry = EXT4_XATTR_NEXT(entry)) { | |
2054 | if (!entry->e_value_inum) | |
2055 | continue; | |
990461dd | 2056 | ea_ino = le32_to_cpu(entry->e_value_inum); |
0421a189 TE |
2057 | error = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode); |
2058 | if (error) | |
2059 | continue; | |
2060 | error = ext4_expand_inode_array(ea_inode_array, ea_inode); | |
65d30005 | 2061 | if (error) { |
0421a189 | 2062 | iput(ea_inode); |
e50e5129 AD |
2063 | brelse(iloc.bh); |
2064 | goto cleanup; | |
2065 | } | |
2066 | entry->e_value_inum = 0; | |
2067 | } | |
2068 | brelse(iloc.bh); | |
2069 | ||
2070 | delete_external_ea: | |
2071 | if (!EXT4_I(inode)->i_file_acl) { | |
2072 | /* add xattr inode to orphan list */ | |
65d30005 | 2073 | error = ext4_xattr_inode_orphan_add(handle, inode, credits, |
0421a189 | 2074 | *ea_inode_array); |
ac27a0ec | 2075 | goto cleanup; |
e50e5129 | 2076 | } |
617ba13b | 2077 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
ac27a0ec | 2078 | if (!bh) { |
24676da4 TT |
2079 | EXT4_ERROR_INODE(inode, "block %llu read error", |
2080 | EXT4_I(inode)->i_file_acl); | |
65d30005 | 2081 | error = -EIO; |
ac27a0ec DK |
2082 | goto cleanup; |
2083 | } | |
617ba13b | 2084 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
ac27a0ec | 2085 | BHDR(bh)->h_blocks != cpu_to_le32(1)) { |
24676da4 TT |
2086 | EXT4_ERROR_INODE(inode, "bad block %llu", |
2087 | EXT4_I(inode)->i_file_acl); | |
65d30005 | 2088 | error = -EFSCORRUPTED; |
ac27a0ec DK |
2089 | goto cleanup; |
2090 | } | |
e50e5129 AD |
2091 | |
2092 | for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); | |
2093 | entry = EXT4_XATTR_NEXT(entry)) { | |
2094 | if (!entry->e_value_inum) | |
2095 | continue; | |
990461dd | 2096 | ea_ino = le32_to_cpu(entry->e_value_inum); |
0421a189 TE |
2097 | error = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode); |
2098 | if (error) | |
2099 | continue; | |
2100 | error = ext4_expand_inode_array(ea_inode_array, ea_inode); | |
65d30005 | 2101 | if (error) |
e50e5129 AD |
2102 | goto cleanup; |
2103 | entry->e_value_inum = 0; | |
2104 | } | |
2105 | ||
2106 | /* add xattr inode to orphan list */ | |
2107 | error = ext4_xattr_inode_orphan_add(handle, inode, credits, | |
0421a189 | 2108 | *ea_inode_array); |
65d30005 | 2109 | if (error) |
e50e5129 AD |
2110 | goto cleanup; |
2111 | ||
2112 | if (!IS_NOQUOTA(inode)) | |
2113 | credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb); | |
2114 | ||
2115 | if (!ext4_handle_has_enough_credits(handle, credits)) { | |
2116 | error = ext4_journal_extend(handle, credits); | |
2117 | if (error > 0) | |
2118 | error = ext4_journal_restart(handle, credits); | |
65d30005 | 2119 | if (error) { |
e50e5129 AD |
2120 | ext4_warning(inode->i_sb, |
2121 | "couldn't extend journal (err %d)", error); | |
2122 | goto cleanup; | |
2123 | } | |
2124 | } | |
2125 | ||
617ba13b MC |
2126 | ext4_xattr_release_block(handle, inode, bh); |
2127 | EXT4_I(inode)->i_file_acl = 0; | |
ac27a0ec DK |
2128 | |
2129 | cleanup: | |
2130 | brelse(bh); | |
e50e5129 AD |
2131 | |
2132 | return error; | |
2133 | } | |
2134 | ||
0421a189 | 2135 | void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array) |
e50e5129 | 2136 | { |
bab79b04 | 2137 | struct inode *ea_inode; |
e50e5129 | 2138 | int idx = 0; |
e50e5129 | 2139 | |
0421a189 | 2140 | if (ea_inode_array == NULL) |
e50e5129 AD |
2141 | return; |
2142 | ||
0421a189 TE |
2143 | for (; idx < ea_inode_array->count; ++idx) { |
2144 | ea_inode = ea_inode_array->inodes[idx]; | |
e50e5129 AD |
2145 | clear_nlink(ea_inode); |
2146 | iput(ea_inode); | |
2147 | } | |
0421a189 | 2148 | kfree(ea_inode_array); |
ac27a0ec DK |
2149 | } |
2150 | ||
ac27a0ec | 2151 | /* |
47387409 | 2152 | * ext4_xattr_block_cache_insert() |
ac27a0ec | 2153 | * |
47387409 | 2154 | * Create a new entry in the extended attribute block cache, and insert |
ac27a0ec DK |
2155 | * it unless such an entry is already in the cache. |
2156 | * | |
2157 | * Returns 0, or a negative error number on failure. | |
2158 | */ | |
2159 | static void | |
47387409 TE |
2160 | ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache, |
2161 | struct buffer_head *bh) | |
ac27a0ec | 2162 | { |
6048c64b AG |
2163 | struct ext4_xattr_header *header = BHDR(bh); |
2164 | __u32 hash = le32_to_cpu(header->h_hash); | |
2165 | int reusable = le32_to_cpu(header->h_refcount) < | |
2166 | EXT4_XATTR_REFCOUNT_MAX; | |
ac27a0ec DK |
2167 | int error; |
2168 | ||
47387409 | 2169 | error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash, |
6048c64b | 2170 | bh->b_blocknr, reusable); |
ac27a0ec | 2171 | if (error) { |
82939d79 | 2172 | if (error == -EBUSY) |
ac27a0ec | 2173 | ea_bdebug(bh, "already in cache"); |
82939d79 | 2174 | } else |
ac27a0ec | 2175 | ea_bdebug(bh, "inserting [%x]", (int)hash); |
ac27a0ec DK |
2176 | } |
2177 | ||
2178 | /* | |
617ba13b | 2179 | * ext4_xattr_cmp() |
ac27a0ec DK |
2180 | * |
2181 | * Compare two extended attribute blocks for equality. | |
2182 | * | |
2183 | * Returns 0 if the blocks are equal, 1 if they differ, and | |
2184 | * a negative error number on errors. | |
2185 | */ | |
2186 | static int | |
617ba13b MC |
2187 | ext4_xattr_cmp(struct ext4_xattr_header *header1, |
2188 | struct ext4_xattr_header *header2) | |
ac27a0ec | 2189 | { |
617ba13b | 2190 | struct ext4_xattr_entry *entry1, *entry2; |
ac27a0ec DK |
2191 | |
2192 | entry1 = ENTRY(header1+1); | |
2193 | entry2 = ENTRY(header2+1); | |
2194 | while (!IS_LAST_ENTRY(entry1)) { | |
2195 | if (IS_LAST_ENTRY(entry2)) | |
2196 | return 1; | |
2197 | if (entry1->e_hash != entry2->e_hash || | |
2198 | entry1->e_name_index != entry2->e_name_index || | |
2199 | entry1->e_name_len != entry2->e_name_len || | |
2200 | entry1->e_value_size != entry2->e_value_size || | |
e50e5129 | 2201 | entry1->e_value_inum != entry2->e_value_inum || |
ac27a0ec DK |
2202 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) |
2203 | return 1; | |
7cec1918 TE |
2204 | if (!entry1->e_value_inum && |
2205 | memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), | |
ac27a0ec DK |
2206 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), |
2207 | le32_to_cpu(entry1->e_value_size))) | |
2208 | return 1; | |
2209 | ||
617ba13b MC |
2210 | entry1 = EXT4_XATTR_NEXT(entry1); |
2211 | entry2 = EXT4_XATTR_NEXT(entry2); | |
ac27a0ec DK |
2212 | } |
2213 | if (!IS_LAST_ENTRY(entry2)) | |
2214 | return 1; | |
2215 | return 0; | |
2216 | } | |
2217 | ||
2218 | /* | |
47387409 | 2219 | * ext4_xattr_block_cache_find() |
ac27a0ec DK |
2220 | * |
2221 | * Find an identical extended attribute block. | |
2222 | * | |
2223 | * Returns a pointer to the block found, or NULL if such a block was | |
2224 | * not found or an error occurred. | |
2225 | */ | |
2226 | static struct buffer_head * | |
47387409 TE |
2227 | ext4_xattr_block_cache_find(struct inode *inode, |
2228 | struct ext4_xattr_header *header, | |
2229 | struct mb_cache_entry **pce) | |
ac27a0ec DK |
2230 | { |
2231 | __u32 hash = le32_to_cpu(header->h_hash); | |
7a2508e1 | 2232 | struct mb_cache_entry *ce; |
47387409 | 2233 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
ac27a0ec DK |
2234 | |
2235 | if (!header->h_hash) | |
2236 | return NULL; /* never share */ | |
2237 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); | |
47387409 | 2238 | ce = mb_cache_entry_find_first(ea_block_cache, hash); |
ac27a0ec DK |
2239 | while (ce) { |
2240 | struct buffer_head *bh; | |
2241 | ||
c07dfcb4 | 2242 | bh = sb_bread(inode->i_sb, ce->e_value); |
ac27a0ec | 2243 | if (!bh) { |
24676da4 | 2244 | EXT4_ERROR_INODE(inode, "block %lu read error", |
c07dfcb4 | 2245 | (unsigned long)ce->e_value); |
617ba13b | 2246 | } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { |
ac27a0ec DK |
2247 | *pce = ce; |
2248 | return bh; | |
2249 | } | |
2250 | brelse(bh); | |
47387409 | 2251 | ce = mb_cache_entry_find_next(ea_block_cache, ce); |
ac27a0ec DK |
2252 | } |
2253 | return NULL; | |
2254 | } | |
2255 | ||
2256 | #define NAME_HASH_SHIFT 5 | |
2257 | #define VALUE_HASH_SHIFT 16 | |
2258 | ||
2259 | /* | |
617ba13b | 2260 | * ext4_xattr_hash_entry() |
ac27a0ec DK |
2261 | * |
2262 | * Compute the hash of an extended attribute. | |
2263 | */ | |
617ba13b MC |
2264 | static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header, |
2265 | struct ext4_xattr_entry *entry) | |
ac27a0ec DK |
2266 | { |
2267 | __u32 hash = 0; | |
2268 | char *name = entry->e_name; | |
2269 | int n; | |
2270 | ||
2b2d6d01 | 2271 | for (n = 0; n < entry->e_name_len; n++) { |
ac27a0ec DK |
2272 | hash = (hash << NAME_HASH_SHIFT) ^ |
2273 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ | |
2274 | *name++; | |
2275 | } | |
2276 | ||
e50e5129 | 2277 | if (!entry->e_value_inum && entry->e_value_size) { |
ac27a0ec DK |
2278 | __le32 *value = (__le32 *)((char *)header + |
2279 | le16_to_cpu(entry->e_value_offs)); | |
2280 | for (n = (le32_to_cpu(entry->e_value_size) + | |
617ba13b | 2281 | EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) { |
ac27a0ec DK |
2282 | hash = (hash << VALUE_HASH_SHIFT) ^ |
2283 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ | |
2284 | le32_to_cpu(*value++); | |
2285 | } | |
2286 | } | |
2287 | entry->e_hash = cpu_to_le32(hash); | |
2288 | } | |
2289 | ||
2290 | #undef NAME_HASH_SHIFT | |
2291 | #undef VALUE_HASH_SHIFT | |
2292 | ||
2293 | #define BLOCK_HASH_SHIFT 16 | |
2294 | ||
2295 | /* | |
617ba13b | 2296 | * ext4_xattr_rehash() |
ac27a0ec DK |
2297 | * |
2298 | * Re-compute the extended attribute hash value after an entry has changed. | |
2299 | */ | |
617ba13b MC |
2300 | static void ext4_xattr_rehash(struct ext4_xattr_header *header, |
2301 | struct ext4_xattr_entry *entry) | |
ac27a0ec | 2302 | { |
617ba13b | 2303 | struct ext4_xattr_entry *here; |
ac27a0ec DK |
2304 | __u32 hash = 0; |
2305 | ||
617ba13b | 2306 | ext4_xattr_hash_entry(header, entry); |
ac27a0ec DK |
2307 | here = ENTRY(header+1); |
2308 | while (!IS_LAST_ENTRY(here)) { | |
2309 | if (!here->e_hash) { | |
2310 | /* Block is not shared if an entry's hash value == 0 */ | |
2311 | hash = 0; | |
2312 | break; | |
2313 | } | |
2314 | hash = (hash << BLOCK_HASH_SHIFT) ^ | |
2315 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ | |
2316 | le32_to_cpu(here->e_hash); | |
617ba13b | 2317 | here = EXT4_XATTR_NEXT(here); |
ac27a0ec DK |
2318 | } |
2319 | header->h_hash = cpu_to_le32(hash); | |
2320 | } | |
2321 | ||
2322 | #undef BLOCK_HASH_SHIFT | |
2323 | ||
9c191f70 M |
2324 | #define HASH_BUCKET_BITS 10 |
2325 | ||
7a2508e1 | 2326 | struct mb_cache * |
82939d79 | 2327 | ext4_xattr_create_cache(void) |
ac27a0ec | 2328 | { |
7a2508e1 | 2329 | return mb_cache_create(HASH_BUCKET_BITS); |
ac27a0ec DK |
2330 | } |
2331 | ||
7a2508e1 | 2332 | void ext4_xattr_destroy_cache(struct mb_cache *cache) |
ac27a0ec | 2333 | { |
9c191f70 | 2334 | if (cache) |
7a2508e1 | 2335 | mb_cache_destroy(cache); |
ac27a0ec | 2336 | } |
9c191f70 | 2337 |