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