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 *); | |
431547b3 | 81 | static int ext4_xattr_list(struct dentry *dentry, char *buffer, |
d3a95d47 | 82 | size_t buffer_size); |
ac27a0ec | 83 | |
11e27528 | 84 | static const struct xattr_handler *ext4_xattr_handler_map[] = { |
617ba13b | 85 | [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler, |
03010a33 | 86 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
64e178a7 CH |
87 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, |
88 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, | |
ac27a0ec | 89 | #endif |
617ba13b | 90 | [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler, |
03010a33 | 91 | #ifdef CONFIG_EXT4_FS_SECURITY |
617ba13b | 92 | [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler, |
ac27a0ec DK |
93 | #endif |
94 | }; | |
95 | ||
11e27528 | 96 | const struct xattr_handler *ext4_xattr_handlers[] = { |
617ba13b MC |
97 | &ext4_xattr_user_handler, |
98 | &ext4_xattr_trusted_handler, | |
03010a33 | 99 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
64e178a7 CH |
100 | &posix_acl_access_xattr_handler, |
101 | &posix_acl_default_xattr_handler, | |
ac27a0ec | 102 | #endif |
03010a33 | 103 | #ifdef CONFIG_EXT4_FS_SECURITY |
617ba13b | 104 | &ext4_xattr_security_handler, |
ac27a0ec DK |
105 | #endif |
106 | NULL | |
107 | }; | |
108 | ||
9c191f70 M |
109 | #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \ |
110 | inode->i_sb->s_fs_info)->s_mb_cache) | |
111 | ||
cc8e94fd DW |
112 | static __le32 ext4_xattr_block_csum(struct inode *inode, |
113 | sector_t block_nr, | |
114 | struct ext4_xattr_header *hdr) | |
115 | { | |
116 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
d6a77105 | 117 | __u32 csum; |
d6a77105 | 118 | __le64 dsk_block_nr = cpu_to_le64(block_nr); |
b47820ed DJ |
119 | __u32 dummy_csum = 0; |
120 | int offset = offsetof(struct ext4_xattr_header, h_checksum); | |
cc8e94fd | 121 | |
d6a77105 TT |
122 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr, |
123 | sizeof(dsk_block_nr)); | |
b47820ed DJ |
124 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset); |
125 | csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); | |
126 | offset += sizeof(dummy_csum); | |
127 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset, | |
128 | EXT4_BLOCK_SIZE(inode->i_sb) - offset); | |
41eb70dd | 129 | |
cc8e94fd DW |
130 | return cpu_to_le32(csum); |
131 | } | |
132 | ||
133 | static int ext4_xattr_block_csum_verify(struct inode *inode, | |
134 | sector_t block_nr, | |
135 | struct ext4_xattr_header *hdr) | |
136 | { | |
9aa5d32b | 137 | if (ext4_has_metadata_csum(inode->i_sb) && |
cc8e94fd DW |
138 | (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr))) |
139 | return 0; | |
140 | return 1; | |
141 | } | |
142 | ||
143 | static void ext4_xattr_block_csum_set(struct inode *inode, | |
144 | sector_t block_nr, | |
145 | struct ext4_xattr_header *hdr) | |
146 | { | |
9aa5d32b | 147 | if (!ext4_has_metadata_csum(inode->i_sb)) |
cc8e94fd DW |
148 | return; |
149 | ||
150 | hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr); | |
151 | } | |
152 | ||
153 | static inline int ext4_handle_dirty_xattr_block(handle_t *handle, | |
154 | struct inode *inode, | |
155 | struct buffer_head *bh) | |
156 | { | |
157 | ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh)); | |
158 | return ext4_handle_dirty_metadata(handle, inode, bh); | |
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 | ||
171 | /* | |
172 | * Inode operation listxattr() | |
173 | * | |
2b0143b5 | 174 | * d_inode(dentry)->i_mutex: don't care |
ac27a0ec DK |
175 | */ |
176 | ssize_t | |
617ba13b | 177 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t size) |
ac27a0ec | 178 | { |
431547b3 | 179 | return ext4_xattr_list(dentry, buffer, size); |
ac27a0ec DK |
180 | } |
181 | ||
182 | static int | |
a0626e75 DW |
183 | ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end, |
184 | void *value_start) | |
ac27a0ec | 185 | { |
a0626e75 DW |
186 | struct ext4_xattr_entry *e = entry; |
187 | ||
188 | while (!IS_LAST_ENTRY(e)) { | |
189 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); | |
ac27a0ec | 190 | if ((void *)next >= end) |
6a797d27 | 191 | return -EFSCORRUPTED; |
a0626e75 | 192 | e = next; |
ac27a0ec | 193 | } |
a0626e75 DW |
194 | |
195 | while (!IS_LAST_ENTRY(entry)) { | |
2de58f11 JK |
196 | if (entry->e_value_block != 0) |
197 | return -EFSCORRUPTED; | |
a0626e75 DW |
198 | if (entry->e_value_size != 0 && |
199 | (value_start + le16_to_cpu(entry->e_value_offs) < | |
200 | (void *)e + sizeof(__u32) || | |
201 | value_start + le16_to_cpu(entry->e_value_offs) + | |
202 | le32_to_cpu(entry->e_value_size) > end)) | |
6a797d27 | 203 | return -EFSCORRUPTED; |
a0626e75 DW |
204 | entry = EXT4_XATTR_NEXT(entry); |
205 | } | |
206 | ||
ac27a0ec DK |
207 | return 0; |
208 | } | |
209 | ||
210 | static inline int | |
cc8e94fd | 211 | ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh) |
ac27a0ec | 212 | { |
cc8e94fd DW |
213 | int error; |
214 | ||
215 | if (buffer_verified(bh)) | |
216 | return 0; | |
217 | ||
617ba13b | 218 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
ac27a0ec | 219 | BHDR(bh)->h_blocks != cpu_to_le32(1)) |
6a797d27 | 220 | return -EFSCORRUPTED; |
cc8e94fd | 221 | if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh))) |
6a797d27 | 222 | return -EFSBADCRC; |
a0626e75 DW |
223 | error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size, |
224 | bh->b_data); | |
cc8e94fd DW |
225 | if (!error) |
226 | set_buffer_verified(bh); | |
227 | return error; | |
ac27a0ec DK |
228 | } |
229 | ||
9e92f48c TT |
230 | static int |
231 | __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header, | |
232 | void *end, const char *function, unsigned int line) | |
233 | { | |
234 | struct ext4_xattr_entry *entry = IFIRST(header); | |
235 | int error = -EFSCORRUPTED; | |
236 | ||
237 | if (((void *) header >= end) || | |
19962509 | 238 | (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC))) |
9e92f48c TT |
239 | goto errout; |
240 | error = ext4_xattr_check_names(entry, end, entry); | |
241 | errout: | |
242 | if (error) | |
243 | __ext4_error_inode(inode, function, line, 0, | |
244 | "corrupted in-inode xattr"); | |
245 | return error; | |
246 | } | |
247 | ||
248 | #define xattr_check_inode(inode, header, end) \ | |
249 | __xattr_check_inode((inode), (header), (end), __func__, __LINE__) | |
250 | ||
ac27a0ec | 251 | static inline int |
617ba13b | 252 | ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size) |
ac27a0ec DK |
253 | { |
254 | size_t value_size = le32_to_cpu(entry->e_value_size); | |
255 | ||
256 | if (entry->e_value_block != 0 || value_size > size || | |
257 | le16_to_cpu(entry->e_value_offs) + value_size > size) | |
6a797d27 | 258 | return -EFSCORRUPTED; |
ac27a0ec DK |
259 | return 0; |
260 | } | |
261 | ||
262 | static int | |
617ba13b | 263 | ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index, |
ac27a0ec DK |
264 | const char *name, size_t size, int sorted) |
265 | { | |
617ba13b | 266 | struct ext4_xattr_entry *entry; |
ac27a0ec DK |
267 | size_t name_len; |
268 | int cmp = 1; | |
269 | ||
270 | if (name == NULL) | |
271 | return -EINVAL; | |
272 | name_len = strlen(name); | |
273 | entry = *pentry; | |
617ba13b | 274 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
ac27a0ec DK |
275 | cmp = name_index - entry->e_name_index; |
276 | if (!cmp) | |
277 | cmp = name_len - entry->e_name_len; | |
278 | if (!cmp) | |
279 | cmp = memcmp(name, entry->e_name, name_len); | |
280 | if (cmp <= 0 && (sorted || cmp == 0)) | |
281 | break; | |
282 | } | |
283 | *pentry = entry; | |
617ba13b | 284 | if (!cmp && ext4_xattr_check_entry(entry, size)) |
6a797d27 | 285 | return -EFSCORRUPTED; |
ac27a0ec DK |
286 | return cmp ? -ENODATA : 0; |
287 | } | |
288 | ||
289 | static int | |
617ba13b | 290 | ext4_xattr_block_get(struct inode *inode, int name_index, const char *name, |
ac27a0ec DK |
291 | void *buffer, size_t buffer_size) |
292 | { | |
293 | struct buffer_head *bh = NULL; | |
617ba13b | 294 | struct ext4_xattr_entry *entry; |
ac27a0ec DK |
295 | size_t size; |
296 | int error; | |
7a2508e1 | 297 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
ac27a0ec DK |
298 | |
299 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", | |
300 | name_index, name, buffer, (long)buffer_size); | |
301 | ||
302 | error = -ENODATA; | |
617ba13b | 303 | if (!EXT4_I(inode)->i_file_acl) |
ac27a0ec | 304 | goto cleanup; |
ace36ad4 JP |
305 | ea_idebug(inode, "reading block %llu", |
306 | (unsigned long long)EXT4_I(inode)->i_file_acl); | |
617ba13b | 307 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
ac27a0ec DK |
308 | if (!bh) |
309 | goto cleanup; | |
310 | ea_bdebug(bh, "b_count=%d, refcount=%d", | |
311 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); | |
cc8e94fd | 312 | if (ext4_xattr_check_block(inode, bh)) { |
12062ddd | 313 | bad_block: |
24676da4 TT |
314 | EXT4_ERROR_INODE(inode, "bad block %llu", |
315 | EXT4_I(inode)->i_file_acl); | |
6a797d27 | 316 | error = -EFSCORRUPTED; |
ac27a0ec DK |
317 | goto cleanup; |
318 | } | |
9c191f70 | 319 | ext4_xattr_cache_insert(ext4_mb_cache, bh); |
ac27a0ec | 320 | entry = BFIRST(bh); |
617ba13b | 321 | error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1); |
6a797d27 | 322 | if (error == -EFSCORRUPTED) |
ac27a0ec DK |
323 | goto bad_block; |
324 | if (error) | |
325 | goto cleanup; | |
326 | size = le32_to_cpu(entry->e_value_size); | |
327 | if (buffer) { | |
328 | error = -ERANGE; | |
329 | if (size > buffer_size) | |
330 | goto cleanup; | |
331 | memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs), | |
332 | size); | |
333 | } | |
334 | error = size; | |
335 | ||
336 | cleanup: | |
337 | brelse(bh); | |
338 | return error; | |
339 | } | |
340 | ||
879b3825 | 341 | int |
617ba13b | 342 | ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, |
ac27a0ec DK |
343 | void *buffer, size_t buffer_size) |
344 | { | |
617ba13b MC |
345 | struct ext4_xattr_ibody_header *header; |
346 | struct ext4_xattr_entry *entry; | |
347 | struct ext4_inode *raw_inode; | |
348 | struct ext4_iloc iloc; | |
ac27a0ec DK |
349 | size_t size; |
350 | void *end; | |
351 | int error; | |
352 | ||
19f5fb7a | 353 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
ac27a0ec | 354 | return -ENODATA; |
617ba13b | 355 | error = ext4_get_inode_loc(inode, &iloc); |
ac27a0ec DK |
356 | if (error) |
357 | return error; | |
617ba13b | 358 | raw_inode = ext4_raw_inode(&iloc); |
ac27a0ec DK |
359 | header = IHDR(inode, raw_inode); |
360 | entry = IFIRST(header); | |
617ba13b | 361 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
9e92f48c | 362 | error = xattr_check_inode(inode, header, end); |
ac27a0ec DK |
363 | if (error) |
364 | goto cleanup; | |
617ba13b | 365 | error = ext4_xattr_find_entry(&entry, name_index, name, |
ac27a0ec DK |
366 | end - (void *)entry, 0); |
367 | if (error) | |
368 | goto cleanup; | |
369 | size = le32_to_cpu(entry->e_value_size); | |
370 | if (buffer) { | |
371 | error = -ERANGE; | |
372 | if (size > buffer_size) | |
373 | goto cleanup; | |
374 | memcpy(buffer, (void *)IFIRST(header) + | |
375 | le16_to_cpu(entry->e_value_offs), size); | |
376 | } | |
377 | error = size; | |
378 | ||
379 | cleanup: | |
380 | brelse(iloc.bh); | |
381 | return error; | |
382 | } | |
383 | ||
384 | /* | |
617ba13b | 385 | * ext4_xattr_get() |
ac27a0ec DK |
386 | * |
387 | * Copy an extended attribute into the buffer | |
388 | * provided, or compute the buffer size required. | |
389 | * Buffer is NULL to compute the size of the buffer required. | |
390 | * | |
391 | * Returns a negative error number on failure, or the number of bytes | |
392 | * used / required on success. | |
393 | */ | |
394 | int | |
617ba13b | 395 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, |
ac27a0ec DK |
396 | void *buffer, size_t buffer_size) |
397 | { | |
398 | int error; | |
399 | ||
230b8c1a ZZ |
400 | if (strlen(name) > 255) |
401 | return -ERANGE; | |
402 | ||
617ba13b MC |
403 | down_read(&EXT4_I(inode)->xattr_sem); |
404 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, | |
ac27a0ec DK |
405 | buffer_size); |
406 | if (error == -ENODATA) | |
617ba13b | 407 | error = ext4_xattr_block_get(inode, name_index, name, buffer, |
ac27a0ec | 408 | buffer_size); |
617ba13b | 409 | up_read(&EXT4_I(inode)->xattr_sem); |
ac27a0ec DK |
410 | return error; |
411 | } | |
412 | ||
413 | static int | |
431547b3 | 414 | ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, |
ac27a0ec DK |
415 | char *buffer, size_t buffer_size) |
416 | { | |
417 | size_t rest = buffer_size; | |
418 | ||
617ba13b | 419 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
11e27528 | 420 | const struct xattr_handler *handler = |
617ba13b | 421 | ext4_xattr_handler(entry->e_name_index); |
ac27a0ec | 422 | |
764a5c6b AG |
423 | if (handler && (!handler->list || handler->list(dentry))) { |
424 | const char *prefix = handler->prefix ?: handler->name; | |
425 | size_t prefix_len = strlen(prefix); | |
426 | size_t size = prefix_len + entry->e_name_len + 1; | |
427 | ||
ac27a0ec DK |
428 | if (buffer) { |
429 | if (size > rest) | |
430 | return -ERANGE; | |
764a5c6b AG |
431 | memcpy(buffer, prefix, prefix_len); |
432 | buffer += prefix_len; | |
433 | memcpy(buffer, entry->e_name, entry->e_name_len); | |
434 | buffer += entry->e_name_len; | |
435 | *buffer++ = 0; | |
ac27a0ec DK |
436 | } |
437 | rest -= size; | |
438 | } | |
439 | } | |
764a5c6b | 440 | return buffer_size - rest; /* total size */ |
ac27a0ec DK |
441 | } |
442 | ||
443 | static int | |
431547b3 | 444 | ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
ac27a0ec | 445 | { |
2b0143b5 | 446 | struct inode *inode = d_inode(dentry); |
ac27a0ec DK |
447 | struct buffer_head *bh = NULL; |
448 | int error; | |
7a2508e1 | 449 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
ac27a0ec DK |
450 | |
451 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", | |
452 | buffer, (long)buffer_size); | |
453 | ||
454 | error = 0; | |
617ba13b | 455 | if (!EXT4_I(inode)->i_file_acl) |
ac27a0ec | 456 | goto cleanup; |
ace36ad4 JP |
457 | ea_idebug(inode, "reading block %llu", |
458 | (unsigned long long)EXT4_I(inode)->i_file_acl); | |
617ba13b | 459 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
ac27a0ec DK |
460 | error = -EIO; |
461 | if (!bh) | |
462 | goto cleanup; | |
463 | ea_bdebug(bh, "b_count=%d, refcount=%d", | |
464 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); | |
cc8e94fd | 465 | if (ext4_xattr_check_block(inode, bh)) { |
24676da4 TT |
466 | EXT4_ERROR_INODE(inode, "bad block %llu", |
467 | EXT4_I(inode)->i_file_acl); | |
6a797d27 | 468 | error = -EFSCORRUPTED; |
ac27a0ec DK |
469 | goto cleanup; |
470 | } | |
9c191f70 | 471 | ext4_xattr_cache_insert(ext4_mb_cache, bh); |
431547b3 | 472 | error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size); |
ac27a0ec DK |
473 | |
474 | cleanup: | |
475 | brelse(bh); | |
476 | ||
477 | return error; | |
478 | } | |
479 | ||
480 | static int | |
431547b3 | 481 | ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
ac27a0ec | 482 | { |
2b0143b5 | 483 | struct inode *inode = d_inode(dentry); |
617ba13b MC |
484 | struct ext4_xattr_ibody_header *header; |
485 | struct ext4_inode *raw_inode; | |
486 | struct ext4_iloc iloc; | |
ac27a0ec DK |
487 | void *end; |
488 | int error; | |
489 | ||
19f5fb7a | 490 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
ac27a0ec | 491 | return 0; |
617ba13b | 492 | error = ext4_get_inode_loc(inode, &iloc); |
ac27a0ec DK |
493 | if (error) |
494 | return error; | |
617ba13b | 495 | raw_inode = ext4_raw_inode(&iloc); |
ac27a0ec | 496 | header = IHDR(inode, raw_inode); |
617ba13b | 497 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
9e92f48c | 498 | error = xattr_check_inode(inode, header, end); |
ac27a0ec DK |
499 | if (error) |
500 | goto cleanup; | |
431547b3 | 501 | error = ext4_xattr_list_entries(dentry, IFIRST(header), |
ac27a0ec DK |
502 | buffer, buffer_size); |
503 | ||
504 | cleanup: | |
505 | brelse(iloc.bh); | |
506 | return error; | |
507 | } | |
508 | ||
509 | /* | |
617ba13b | 510 | * ext4_xattr_list() |
ac27a0ec DK |
511 | * |
512 | * Copy a list of attribute names into the buffer | |
513 | * provided, or compute the buffer size required. | |
514 | * Buffer is NULL to compute the size of the buffer required. | |
515 | * | |
516 | * Returns a negative error number on failure, or the number of bytes | |
517 | * used / required on success. | |
518 | */ | |
d3a95d47 | 519 | static int |
431547b3 | 520 | ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
ac27a0ec | 521 | { |
eaeef867 | 522 | int ret, ret2; |
ac27a0ec | 523 | |
2b0143b5 | 524 | down_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
eaeef867 TT |
525 | ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); |
526 | if (ret < 0) | |
527 | goto errout; | |
528 | if (buffer) { | |
529 | buffer += ret; | |
530 | buffer_size -= ret; | |
ac27a0ec | 531 | } |
eaeef867 TT |
532 | ret = ext4_xattr_block_list(dentry, buffer, buffer_size); |
533 | if (ret < 0) | |
534 | goto errout; | |
535 | ret += ret2; | |
536 | errout: | |
2b0143b5 | 537 | up_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
eaeef867 | 538 | return ret; |
ac27a0ec DK |
539 | } |
540 | ||
541 | /* | |
617ba13b | 542 | * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is |
ac27a0ec DK |
543 | * not set, set it. |
544 | */ | |
617ba13b | 545 | static void ext4_xattr_update_super_block(handle_t *handle, |
ac27a0ec DK |
546 | struct super_block *sb) |
547 | { | |
e2b911c5 | 548 | if (ext4_has_feature_xattr(sb)) |
ac27a0ec DK |
549 | return; |
550 | ||
5d601255 | 551 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); |
617ba13b | 552 | if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) { |
e2b911c5 | 553 | ext4_set_feature_xattr(sb); |
a0375156 | 554 | ext4_handle_dirty_super(handle, sb); |
ac27a0ec | 555 | } |
ac27a0ec DK |
556 | } |
557 | ||
558 | /* | |
ec4cb1aa JK |
559 | * Release the xattr block BH: If the reference count is > 1, decrement it; |
560 | * otherwise free the block. | |
ac27a0ec DK |
561 | */ |
562 | static void | |
617ba13b | 563 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, |
ac27a0ec DK |
564 | struct buffer_head *bh) |
565 | { | |
6048c64b AG |
566 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
567 | u32 hash, ref; | |
8a2bfdcb | 568 | int error = 0; |
ac27a0ec | 569 | |
5d601255 | 570 | BUFFER_TRACE(bh, "get_write_access"); |
8a2bfdcb MC |
571 | error = ext4_journal_get_write_access(handle, bh); |
572 | if (error) | |
573 | goto out; | |
574 | ||
575 | lock_buffer(bh); | |
6048c64b AG |
576 | hash = le32_to_cpu(BHDR(bh)->h_hash); |
577 | ref = le32_to_cpu(BHDR(bh)->h_refcount); | |
578 | if (ref == 1) { | |
ac27a0ec | 579 | ea_bdebug(bh, "refcount now=0; freeing"); |
82939d79 JK |
580 | /* |
581 | * This must happen under buffer lock for | |
582 | * ext4_xattr_block_set() to reliably detect freed block | |
583 | */ | |
6048c64b | 584 | mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr); |
ac27a0ec | 585 | get_bh(bh); |
ec4cb1aa | 586 | unlock_buffer(bh); |
e6362609 TT |
587 | ext4_free_blocks(handle, inode, bh, 0, 1, |
588 | EXT4_FREE_BLOCKS_METADATA | | |
589 | EXT4_FREE_BLOCKS_FORGET); | |
ac27a0ec | 590 | } else { |
6048c64b AG |
591 | ref--; |
592 | BHDR(bh)->h_refcount = cpu_to_le32(ref); | |
593 | if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) { | |
594 | struct mb_cache_entry *ce; | |
595 | ||
596 | ce = mb_cache_entry_get(ext4_mb_cache, hash, | |
597 | bh->b_blocknr); | |
598 | if (ce) { | |
599 | ce->e_reusable = 1; | |
600 | mb_cache_entry_put(ext4_mb_cache, ce); | |
601 | } | |
602 | } | |
603 | ||
ec4cb1aa JK |
604 | /* |
605 | * Beware of this ugliness: Releasing of xattr block references | |
606 | * from different inodes can race and so we have to protect | |
607 | * from a race where someone else frees the block (and releases | |
608 | * its journal_head) before we are done dirtying the buffer. In | |
609 | * nojournal mode this race is harmless and we actually cannot | |
610 | * call ext4_handle_dirty_xattr_block() with locked buffer as | |
611 | * that function can call sync_dirty_buffer() so for that case | |
612 | * we handle the dirtying after unlocking the buffer. | |
613 | */ | |
614 | if (ext4_handle_valid(handle)) | |
615 | error = ext4_handle_dirty_xattr_block(handle, inode, | |
616 | bh); | |
c1bb05a6 | 617 | unlock_buffer(bh); |
ec4cb1aa JK |
618 | if (!ext4_handle_valid(handle)) |
619 | error = ext4_handle_dirty_xattr_block(handle, inode, | |
620 | bh); | |
8a2bfdcb | 621 | if (IS_SYNC(inode)) |
0390131b | 622 | ext4_handle_sync(handle); |
1231b3a1 | 623 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1)); |
8a2bfdcb MC |
624 | ea_bdebug(bh, "refcount now=%d; releasing", |
625 | le32_to_cpu(BHDR(bh)->h_refcount)); | |
ac27a0ec | 626 | } |
8a2bfdcb MC |
627 | out: |
628 | ext4_std_error(inode->i_sb, error); | |
629 | return; | |
ac27a0ec DK |
630 | } |
631 | ||
6dd4ee7c KS |
632 | /* |
633 | * Find the available free space for EAs. This also returns the total number of | |
634 | * bytes used by EA entries. | |
635 | */ | |
636 | static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, | |
637 | size_t *min_offs, void *base, int *total) | |
638 | { | |
639 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | |
1cba4237 | 640 | if (last->e_value_size) { |
6dd4ee7c KS |
641 | size_t offs = le16_to_cpu(last->e_value_offs); |
642 | if (offs < *min_offs) | |
643 | *min_offs = offs; | |
644 | } | |
7b1b2c1b TT |
645 | if (total) |
646 | *total += EXT4_XATTR_LEN(last->e_name_len); | |
6dd4ee7c KS |
647 | } |
648 | return (*min_offs - ((void *)last - base) - sizeof(__u32)); | |
649 | } | |
650 | ||
ac27a0ec | 651 | static int |
617ba13b | 652 | ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s) |
ac27a0ec | 653 | { |
617ba13b | 654 | struct ext4_xattr_entry *last; |
ac27a0ec DK |
655 | size_t free, min_offs = s->end - s->base, name_len = strlen(i->name); |
656 | ||
657 | /* Compute min_offs and last. */ | |
658 | last = s->first; | |
617ba13b | 659 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
1cba4237 | 660 | if (last->e_value_size) { |
ac27a0ec DK |
661 | size_t offs = le16_to_cpu(last->e_value_offs); |
662 | if (offs < min_offs) | |
663 | min_offs = offs; | |
664 | } | |
665 | } | |
666 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); | |
667 | if (!s->not_found) { | |
1cba4237 | 668 | if (s->here->e_value_size) { |
ac27a0ec | 669 | size_t size = le32_to_cpu(s->here->e_value_size); |
617ba13b | 670 | free += EXT4_XATTR_SIZE(size); |
ac27a0ec | 671 | } |
617ba13b | 672 | free += EXT4_XATTR_LEN(name_len); |
ac27a0ec DK |
673 | } |
674 | if (i->value) { | |
5f80f62a | 675 | if (free < EXT4_XATTR_LEN(name_len) + |
617ba13b | 676 | EXT4_XATTR_SIZE(i->value_len)) |
ac27a0ec DK |
677 | return -ENOSPC; |
678 | } | |
679 | ||
680 | if (i->value && s->not_found) { | |
681 | /* Insert the new name. */ | |
617ba13b | 682 | size_t size = EXT4_XATTR_LEN(name_len); |
ac27a0ec DK |
683 | size_t rest = (void *)last - (void *)s->here + sizeof(__u32); |
684 | memmove((void *)s->here + size, s->here, rest); | |
685 | memset(s->here, 0, size); | |
686 | s->here->e_name_index = i->name_index; | |
687 | s->here->e_name_len = name_len; | |
688 | memcpy(s->here->e_name, i->name, name_len); | |
689 | } else { | |
1cba4237 | 690 | if (s->here->e_value_size) { |
ac27a0ec DK |
691 | void *first_val = s->base + min_offs; |
692 | size_t offs = le16_to_cpu(s->here->e_value_offs); | |
693 | void *val = s->base + offs; | |
617ba13b | 694 | size_t size = EXT4_XATTR_SIZE( |
ac27a0ec DK |
695 | le32_to_cpu(s->here->e_value_size)); |
696 | ||
617ba13b | 697 | if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) { |
ac27a0ec DK |
698 | /* The old and the new value have the same |
699 | size. Just replace. */ | |
700 | s->here->e_value_size = | |
701 | cpu_to_le32(i->value_len); | |
bd9926e8 TT |
702 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
703 | memset(val, 0, size); | |
704 | } else { | |
705 | /* Clear pad bytes first. */ | |
706 | memset(val + size - EXT4_XATTR_PAD, 0, | |
707 | EXT4_XATTR_PAD); | |
708 | memcpy(val, i->value, i->value_len); | |
709 | } | |
ac27a0ec DK |
710 | return 0; |
711 | } | |
712 | ||
713 | /* Remove the old value. */ | |
714 | memmove(first_val + size, first_val, val - first_val); | |
715 | memset(first_val, 0, size); | |
716 | s->here->e_value_size = 0; | |
717 | s->here->e_value_offs = 0; | |
718 | min_offs += size; | |
719 | ||
720 | /* Adjust all value offsets. */ | |
721 | last = s->first; | |
722 | while (!IS_LAST_ENTRY(last)) { | |
723 | size_t o = le16_to_cpu(last->e_value_offs); | |
1cba4237 | 724 | if (last->e_value_size && o < offs) |
ac27a0ec DK |
725 | last->e_value_offs = |
726 | cpu_to_le16(o + size); | |
617ba13b | 727 | last = EXT4_XATTR_NEXT(last); |
ac27a0ec DK |
728 | } |
729 | } | |
730 | if (!i->value) { | |
731 | /* Remove the old name. */ | |
617ba13b | 732 | size_t size = EXT4_XATTR_LEN(name_len); |
ac27a0ec DK |
733 | last = ENTRY((void *)last - size); |
734 | memmove(s->here, (void *)s->here + size, | |
735 | (void *)last - (void *)s->here + sizeof(__u32)); | |
736 | memset(last, 0, size); | |
737 | } | |
738 | } | |
739 | ||
740 | if (i->value) { | |
741 | /* Insert the new value. */ | |
742 | s->here->e_value_size = cpu_to_le32(i->value_len); | |
743 | if (i->value_len) { | |
617ba13b | 744 | size_t size = EXT4_XATTR_SIZE(i->value_len); |
ac27a0ec DK |
745 | void *val = s->base + min_offs - size; |
746 | s->here->e_value_offs = cpu_to_le16(min_offs - size); | |
bd9926e8 TT |
747 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
748 | memset(val, 0, size); | |
749 | } else { | |
750 | /* Clear the pad bytes first. */ | |
751 | memset(val + size - EXT4_XATTR_PAD, 0, | |
752 | EXT4_XATTR_PAD); | |
753 | memcpy(val, i->value, i->value_len); | |
754 | } | |
ac27a0ec DK |
755 | } |
756 | } | |
757 | return 0; | |
758 | } | |
759 | ||
617ba13b MC |
760 | struct ext4_xattr_block_find { |
761 | struct ext4_xattr_search s; | |
ac27a0ec DK |
762 | struct buffer_head *bh; |
763 | }; | |
764 | ||
765 | static int | |
617ba13b MC |
766 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, |
767 | struct ext4_xattr_block_find *bs) | |
ac27a0ec DK |
768 | { |
769 | struct super_block *sb = inode->i_sb; | |
770 | int error; | |
771 | ||
772 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", | |
773 | i->name_index, i->name, i->value, (long)i->value_len); | |
774 | ||
617ba13b | 775 | if (EXT4_I(inode)->i_file_acl) { |
ac27a0ec | 776 | /* The inode already has an extended attribute block. */ |
617ba13b | 777 | bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl); |
ac27a0ec DK |
778 | error = -EIO; |
779 | if (!bs->bh) | |
780 | goto cleanup; | |
781 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", | |
782 | atomic_read(&(bs->bh->b_count)), | |
783 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); | |
cc8e94fd | 784 | if (ext4_xattr_check_block(inode, bs->bh)) { |
24676da4 TT |
785 | EXT4_ERROR_INODE(inode, "bad block %llu", |
786 | EXT4_I(inode)->i_file_acl); | |
6a797d27 | 787 | error = -EFSCORRUPTED; |
ac27a0ec DK |
788 | goto cleanup; |
789 | } | |
790 | /* Find the named attribute. */ | |
791 | bs->s.base = BHDR(bs->bh); | |
792 | bs->s.first = BFIRST(bs->bh); | |
793 | bs->s.end = bs->bh->b_data + bs->bh->b_size; | |
794 | bs->s.here = bs->s.first; | |
617ba13b | 795 | error = ext4_xattr_find_entry(&bs->s.here, i->name_index, |
ac27a0ec DK |
796 | i->name, bs->bh->b_size, 1); |
797 | if (error && error != -ENODATA) | |
798 | goto cleanup; | |
799 | bs->s.not_found = error; | |
800 | } | |
801 | error = 0; | |
802 | ||
803 | cleanup: | |
804 | return error; | |
805 | } | |
806 | ||
807 | static int | |
617ba13b MC |
808 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, |
809 | struct ext4_xattr_info *i, | |
810 | struct ext4_xattr_block_find *bs) | |
ac27a0ec DK |
811 | { |
812 | struct super_block *sb = inode->i_sb; | |
813 | struct buffer_head *new_bh = NULL; | |
617ba13b | 814 | struct ext4_xattr_search *s = &bs->s; |
7a2508e1 | 815 | struct mb_cache_entry *ce = NULL; |
8a2bfdcb | 816 | int error = 0; |
7a2508e1 | 817 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
ac27a0ec | 818 | |
617ba13b | 819 | #define header(x) ((struct ext4_xattr_header *)(x)) |
ac27a0ec DK |
820 | |
821 | if (i->value && i->value_len > sb->s_blocksize) | |
822 | return -ENOSPC; | |
823 | if (s->base) { | |
5d601255 | 824 | BUFFER_TRACE(bs->bh, "get_write_access"); |
8a2bfdcb MC |
825 | error = ext4_journal_get_write_access(handle, bs->bh); |
826 | if (error) | |
827 | goto cleanup; | |
828 | lock_buffer(bs->bh); | |
829 | ||
ac27a0ec | 830 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { |
82939d79 JK |
831 | __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash); |
832 | ||
833 | /* | |
834 | * This must happen under buffer lock for | |
835 | * ext4_xattr_block_set() to reliably detect modified | |
836 | * block | |
837 | */ | |
7a2508e1 JK |
838 | mb_cache_entry_delete_block(ext4_mb_cache, hash, |
839 | bs->bh->b_blocknr); | |
ac27a0ec | 840 | ea_bdebug(bs->bh, "modifying in-place"); |
617ba13b | 841 | error = ext4_xattr_set_entry(i, s); |
ac27a0ec DK |
842 | if (!error) { |
843 | if (!IS_LAST_ENTRY(s->first)) | |
617ba13b | 844 | ext4_xattr_rehash(header(s->base), |
ac27a0ec | 845 | s->here); |
9c191f70 M |
846 | ext4_xattr_cache_insert(ext4_mb_cache, |
847 | bs->bh); | |
ac27a0ec DK |
848 | } |
849 | unlock_buffer(bs->bh); | |
6a797d27 | 850 | if (error == -EFSCORRUPTED) |
ac27a0ec DK |
851 | goto bad_block; |
852 | if (!error) | |
cc8e94fd DW |
853 | error = ext4_handle_dirty_xattr_block(handle, |
854 | inode, | |
855 | bs->bh); | |
ac27a0ec DK |
856 | if (error) |
857 | goto cleanup; | |
858 | goto inserted; | |
859 | } else { | |
860 | int offset = (char *)s->here - bs->bh->b_data; | |
861 | ||
8a2bfdcb | 862 | unlock_buffer(bs->bh); |
ac27a0ec | 863 | ea_bdebug(bs->bh, "cloning"); |
216553c4 | 864 | s->base = kmalloc(bs->bh->b_size, GFP_NOFS); |
ac27a0ec DK |
865 | error = -ENOMEM; |
866 | if (s->base == NULL) | |
867 | goto cleanup; | |
868 | memcpy(s->base, BHDR(bs->bh), bs->bh->b_size); | |
869 | s->first = ENTRY(header(s->base)+1); | |
870 | header(s->base)->h_refcount = cpu_to_le32(1); | |
871 | s->here = ENTRY(s->base + offset); | |
872 | s->end = s->base + bs->bh->b_size; | |
873 | } | |
874 | } else { | |
875 | /* Allocate a buffer where we construct the new block. */ | |
216553c4 | 876 | s->base = kzalloc(sb->s_blocksize, GFP_NOFS); |
ac27a0ec DK |
877 | /* assert(header == s->base) */ |
878 | error = -ENOMEM; | |
879 | if (s->base == NULL) | |
880 | goto cleanup; | |
617ba13b | 881 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
ac27a0ec DK |
882 | header(s->base)->h_blocks = cpu_to_le32(1); |
883 | header(s->base)->h_refcount = cpu_to_le32(1); | |
884 | s->first = ENTRY(header(s->base)+1); | |
885 | s->here = ENTRY(header(s->base)+1); | |
886 | s->end = s->base + sb->s_blocksize; | |
887 | } | |
888 | ||
617ba13b | 889 | error = ext4_xattr_set_entry(i, s); |
6a797d27 | 890 | if (error == -EFSCORRUPTED) |
ac27a0ec DK |
891 | goto bad_block; |
892 | if (error) | |
893 | goto cleanup; | |
894 | if (!IS_LAST_ENTRY(s->first)) | |
617ba13b | 895 | ext4_xattr_rehash(header(s->base), s->here); |
ac27a0ec DK |
896 | |
897 | inserted: | |
898 | if (!IS_LAST_ENTRY(s->first)) { | |
617ba13b | 899 | new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce); |
ac27a0ec DK |
900 | if (new_bh) { |
901 | /* We found an identical block in the cache. */ | |
902 | if (new_bh == bs->bh) | |
903 | ea_bdebug(new_bh, "keeping"); | |
904 | else { | |
6048c64b AG |
905 | u32 ref; |
906 | ||
ac27a0ec DK |
907 | /* The old block is released after updating |
908 | the inode. */ | |
1231b3a1 LC |
909 | error = dquot_alloc_block(inode, |
910 | EXT4_C2B(EXT4_SB(sb), 1)); | |
5dd4056d | 911 | if (error) |
ac27a0ec | 912 | goto cleanup; |
5d601255 | 913 | BUFFER_TRACE(new_bh, "get_write_access"); |
617ba13b | 914 | error = ext4_journal_get_write_access(handle, |
ac27a0ec DK |
915 | new_bh); |
916 | if (error) | |
917 | goto cleanup_dquot; | |
918 | lock_buffer(new_bh); | |
82939d79 JK |
919 | /* |
920 | * We have to be careful about races with | |
6048c64b AG |
921 | * freeing, rehashing or adding references to |
922 | * xattr block. Once we hold buffer lock xattr | |
923 | * block's state is stable so we can check | |
924 | * whether the block got freed / rehashed or | |
925 | * not. Since we unhash mbcache entry under | |
926 | * buffer lock when freeing / rehashing xattr | |
927 | * block, checking whether entry is still | |
928 | * hashed is reliable. Same rules hold for | |
929 | * e_reusable handling. | |
82939d79 | 930 | */ |
6048c64b AG |
931 | if (hlist_bl_unhashed(&ce->e_hash_list) || |
932 | !ce->e_reusable) { | |
82939d79 JK |
933 | /* |
934 | * Undo everything and check mbcache | |
935 | * again. | |
936 | */ | |
937 | unlock_buffer(new_bh); | |
938 | dquot_free_block(inode, | |
939 | EXT4_C2B(EXT4_SB(sb), | |
940 | 1)); | |
941 | brelse(new_bh); | |
7a2508e1 | 942 | mb_cache_entry_put(ext4_mb_cache, ce); |
82939d79 JK |
943 | ce = NULL; |
944 | new_bh = NULL; | |
945 | goto inserted; | |
946 | } | |
6048c64b AG |
947 | ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1; |
948 | BHDR(new_bh)->h_refcount = cpu_to_le32(ref); | |
949 | if (ref >= EXT4_XATTR_REFCOUNT_MAX) | |
950 | ce->e_reusable = 0; | |
ac27a0ec | 951 | ea_bdebug(new_bh, "reusing; refcount now=%d", |
6048c64b | 952 | ref); |
ac27a0ec | 953 | unlock_buffer(new_bh); |
cc8e94fd DW |
954 | error = ext4_handle_dirty_xattr_block(handle, |
955 | inode, | |
956 | new_bh); | |
ac27a0ec DK |
957 | if (error) |
958 | goto cleanup_dquot; | |
959 | } | |
7a2508e1 JK |
960 | mb_cache_entry_touch(ext4_mb_cache, ce); |
961 | mb_cache_entry_put(ext4_mb_cache, ce); | |
ac27a0ec DK |
962 | ce = NULL; |
963 | } else if (bs->bh && s->base == bs->bh->b_data) { | |
964 | /* We were modifying this block in-place. */ | |
965 | ea_bdebug(bs->bh, "keeping this block"); | |
966 | new_bh = bs->bh; | |
967 | get_bh(new_bh); | |
968 | } else { | |
969 | /* We need to allocate a new block */ | |
fb0a387d ES |
970 | ext4_fsblk_t goal, block; |
971 | ||
972 | goal = ext4_group_first_block_no(sb, | |
d00a6d7b | 973 | EXT4_I(inode)->i_block_group); |
fb0a387d ES |
974 | |
975 | /* non-extent files can't have physical blocks past 2^32 */ | |
12e9b892 | 976 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
fb0a387d ES |
977 | goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; |
978 | ||
55f020db AH |
979 | block = ext4_new_meta_blocks(handle, inode, goal, 0, |
980 | NULL, &error); | |
ac27a0ec DK |
981 | if (error) |
982 | goto cleanup; | |
fb0a387d | 983 | |
12e9b892 | 984 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
fb0a387d ES |
985 | BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS); |
986 | ||
ace36ad4 JP |
987 | ea_idebug(inode, "creating block %llu", |
988 | (unsigned long long)block); | |
ac27a0ec DK |
989 | |
990 | new_bh = sb_getblk(sb, block); | |
aebf0243 | 991 | if (unlikely(!new_bh)) { |
860d21e2 | 992 | error = -ENOMEM; |
ac27a0ec | 993 | getblk_failed: |
7dc57615 | 994 | ext4_free_blocks(handle, inode, NULL, block, 1, |
e6362609 | 995 | EXT4_FREE_BLOCKS_METADATA); |
ac27a0ec DK |
996 | goto cleanup; |
997 | } | |
998 | lock_buffer(new_bh); | |
617ba13b | 999 | error = ext4_journal_get_create_access(handle, new_bh); |
ac27a0ec DK |
1000 | if (error) { |
1001 | unlock_buffer(new_bh); | |
860d21e2 | 1002 | error = -EIO; |
ac27a0ec DK |
1003 | goto getblk_failed; |
1004 | } | |
1005 | memcpy(new_bh->b_data, s->base, new_bh->b_size); | |
1006 | set_buffer_uptodate(new_bh); | |
1007 | unlock_buffer(new_bh); | |
9c191f70 | 1008 | ext4_xattr_cache_insert(ext4_mb_cache, new_bh); |
cc8e94fd DW |
1009 | error = ext4_handle_dirty_xattr_block(handle, |
1010 | inode, new_bh); | |
ac27a0ec DK |
1011 | if (error) |
1012 | goto cleanup; | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | /* Update the inode. */ | |
617ba13b | 1017 | EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0; |
ac27a0ec DK |
1018 | |
1019 | /* Drop the previous xattr block. */ | |
1020 | if (bs->bh && bs->bh != new_bh) | |
617ba13b | 1021 | ext4_xattr_release_block(handle, inode, bs->bh); |
ac27a0ec DK |
1022 | error = 0; |
1023 | ||
1024 | cleanup: | |
1025 | if (ce) | |
7a2508e1 | 1026 | mb_cache_entry_put(ext4_mb_cache, ce); |
ac27a0ec DK |
1027 | brelse(new_bh); |
1028 | if (!(bs->bh && s->base == bs->bh->b_data)) | |
1029 | kfree(s->base); | |
1030 | ||
1031 | return error; | |
1032 | ||
1033 | cleanup_dquot: | |
1231b3a1 | 1034 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1)); |
ac27a0ec DK |
1035 | goto cleanup; |
1036 | ||
1037 | bad_block: | |
24676da4 TT |
1038 | EXT4_ERROR_INODE(inode, "bad block %llu", |
1039 | EXT4_I(inode)->i_file_acl); | |
ac27a0ec DK |
1040 | goto cleanup; |
1041 | ||
1042 | #undef header | |
1043 | } | |
1044 | ||
879b3825 TM |
1045 | int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, |
1046 | struct ext4_xattr_ibody_find *is) | |
ac27a0ec | 1047 | { |
617ba13b MC |
1048 | struct ext4_xattr_ibody_header *header; |
1049 | struct ext4_inode *raw_inode; | |
ac27a0ec DK |
1050 | int error; |
1051 | ||
617ba13b | 1052 | if (EXT4_I(inode)->i_extra_isize == 0) |
ac27a0ec | 1053 | return 0; |
617ba13b | 1054 | raw_inode = ext4_raw_inode(&is->iloc); |
ac27a0ec DK |
1055 | header = IHDR(inode, raw_inode); |
1056 | is->s.base = is->s.first = IFIRST(header); | |
1057 | is->s.here = is->s.first; | |
617ba13b | 1058 | is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
19f5fb7a | 1059 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
9e92f48c | 1060 | error = xattr_check_inode(inode, header, is->s.end); |
ac27a0ec DK |
1061 | if (error) |
1062 | return error; | |
1063 | /* Find the named attribute. */ | |
617ba13b | 1064 | error = ext4_xattr_find_entry(&is->s.here, i->name_index, |
ac27a0ec DK |
1065 | i->name, is->s.end - |
1066 | (void *)is->s.base, 0); | |
1067 | if (error && error != -ENODATA) | |
1068 | return error; | |
1069 | is->s.not_found = error; | |
1070 | } | |
1071 | return 0; | |
1072 | } | |
1073 | ||
0d812f77 TM |
1074 | int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode, |
1075 | struct ext4_xattr_info *i, | |
1076 | struct ext4_xattr_ibody_find *is) | |
1077 | { | |
1078 | struct ext4_xattr_ibody_header *header; | |
1079 | struct ext4_xattr_search *s = &is->s; | |
1080 | int error; | |
1081 | ||
1082 | if (EXT4_I(inode)->i_extra_isize == 0) | |
1083 | return -ENOSPC; | |
1084 | error = ext4_xattr_set_entry(i, s); | |
1085 | if (error) { | |
1086 | if (error == -ENOSPC && | |
1087 | ext4_has_inline_data(inode)) { | |
1088 | error = ext4_try_to_evict_inline_data(handle, inode, | |
1089 | EXT4_XATTR_LEN(strlen(i->name) + | |
1090 | EXT4_XATTR_SIZE(i->value_len))); | |
1091 | if (error) | |
1092 | return error; | |
1093 | error = ext4_xattr_ibody_find(inode, i, is); | |
1094 | if (error) | |
1095 | return error; | |
1096 | error = ext4_xattr_set_entry(i, s); | |
1097 | } | |
1098 | if (error) | |
1099 | return error; | |
1100 | } | |
1101 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); | |
1102 | if (!IS_LAST_ENTRY(s->first)) { | |
1103 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); | |
1104 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); | |
1105 | } else { | |
1106 | header->h_magic = cpu_to_le32(0); | |
1107 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); | |
1108 | } | |
1109 | return 0; | |
1110 | } | |
1111 | ||
d5c8dab6 | 1112 | static int ext4_xattr_ibody_set(struct inode *inode, |
0d812f77 TM |
1113 | struct ext4_xattr_info *i, |
1114 | struct ext4_xattr_ibody_find *is) | |
ac27a0ec | 1115 | { |
617ba13b MC |
1116 | struct ext4_xattr_ibody_header *header; |
1117 | struct ext4_xattr_search *s = &is->s; | |
ac27a0ec DK |
1118 | int error; |
1119 | ||
617ba13b | 1120 | if (EXT4_I(inode)->i_extra_isize == 0) |
ac27a0ec | 1121 | return -ENOSPC; |
617ba13b | 1122 | error = ext4_xattr_set_entry(i, s); |
ac27a0ec DK |
1123 | if (error) |
1124 | return error; | |
617ba13b | 1125 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
ac27a0ec | 1126 | if (!IS_LAST_ENTRY(s->first)) { |
617ba13b | 1127 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
19f5fb7a | 1128 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
ac27a0ec DK |
1129 | } else { |
1130 | header->h_magic = cpu_to_le32(0); | |
19f5fb7a | 1131 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
ac27a0ec DK |
1132 | } |
1133 | return 0; | |
1134 | } | |
1135 | ||
3fd16462 JK |
1136 | static int ext4_xattr_value_same(struct ext4_xattr_search *s, |
1137 | struct ext4_xattr_info *i) | |
1138 | { | |
1139 | void *value; | |
1140 | ||
1141 | if (le32_to_cpu(s->here->e_value_size) != i->value_len) | |
1142 | return 0; | |
1143 | value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs); | |
1144 | return !memcmp(value, i->value, i->value_len); | |
1145 | } | |
1146 | ||
ac27a0ec | 1147 | /* |
617ba13b | 1148 | * ext4_xattr_set_handle() |
ac27a0ec | 1149 | * |
6e9510b0 | 1150 | * Create, replace or remove an extended attribute for this inode. Value |
ac27a0ec DK |
1151 | * is NULL to remove an existing extended attribute, and non-NULL to |
1152 | * either replace an existing extended attribute, or create a new extended | |
1153 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE | |
1154 | * specify that an extended attribute must exist and must not exist | |
1155 | * previous to the call, respectively. | |
1156 | * | |
1157 | * Returns 0, or a negative error number on failure. | |
1158 | */ | |
1159 | int | |
617ba13b | 1160 | ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, |
ac27a0ec DK |
1161 | const char *name, const void *value, size_t value_len, |
1162 | int flags) | |
1163 | { | |
617ba13b | 1164 | struct ext4_xattr_info i = { |
ac27a0ec DK |
1165 | .name_index = name_index, |
1166 | .name = name, | |
1167 | .value = value, | |
1168 | .value_len = value_len, | |
1169 | ||
1170 | }; | |
617ba13b | 1171 | struct ext4_xattr_ibody_find is = { |
ac27a0ec DK |
1172 | .s = { .not_found = -ENODATA, }, |
1173 | }; | |
617ba13b | 1174 | struct ext4_xattr_block_find bs = { |
ac27a0ec DK |
1175 | .s = { .not_found = -ENODATA, }, |
1176 | }; | |
4d20c685 | 1177 | unsigned long no_expand; |
ac27a0ec DK |
1178 | int error; |
1179 | ||
1180 | if (!name) | |
1181 | return -EINVAL; | |
1182 | if (strlen(name) > 255) | |
1183 | return -ERANGE; | |
617ba13b | 1184 | down_write(&EXT4_I(inode)->xattr_sem); |
19f5fb7a TT |
1185 | no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND); |
1186 | ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND); | |
4d20c685 | 1187 | |
66543617 | 1188 | error = ext4_reserve_inode_write(handle, inode, &is.iloc); |
86ebfd08 ES |
1189 | if (error) |
1190 | goto cleanup; | |
1191 | ||
19f5fb7a | 1192 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) { |
617ba13b MC |
1193 | struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); |
1194 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); | |
19f5fb7a | 1195 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); |
ac27a0ec DK |
1196 | } |
1197 | ||
617ba13b | 1198 | error = ext4_xattr_ibody_find(inode, &i, &is); |
ac27a0ec DK |
1199 | if (error) |
1200 | goto cleanup; | |
1201 | if (is.s.not_found) | |
617ba13b | 1202 | error = ext4_xattr_block_find(inode, &i, &bs); |
ac27a0ec DK |
1203 | if (error) |
1204 | goto cleanup; | |
1205 | if (is.s.not_found && bs.s.not_found) { | |
1206 | error = -ENODATA; | |
1207 | if (flags & XATTR_REPLACE) | |
1208 | goto cleanup; | |
1209 | error = 0; | |
1210 | if (!value) | |
1211 | goto cleanup; | |
1212 | } else { | |
1213 | error = -EEXIST; | |
1214 | if (flags & XATTR_CREATE) | |
1215 | goto cleanup; | |
1216 | } | |
ac27a0ec DK |
1217 | if (!value) { |
1218 | if (!is.s.not_found) | |
d5c8dab6 | 1219 | error = ext4_xattr_ibody_set(inode, &i, &is); |
ac27a0ec | 1220 | else if (!bs.s.not_found) |
617ba13b | 1221 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
ac27a0ec | 1222 | } else { |
3fd16462 JK |
1223 | error = 0; |
1224 | /* Xattr value did not change? Save us some work and bail out */ | |
1225 | if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i)) | |
1226 | goto cleanup; | |
1227 | if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i)) | |
1228 | goto cleanup; | |
1229 | ||
d5c8dab6 | 1230 | error = ext4_xattr_ibody_set(inode, &i, &is); |
ac27a0ec DK |
1231 | if (!error && !bs.s.not_found) { |
1232 | i.value = NULL; | |
617ba13b | 1233 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
ac27a0ec | 1234 | } else if (error == -ENOSPC) { |
7e01c8e5 TY |
1235 | if (EXT4_I(inode)->i_file_acl && !bs.s.base) { |
1236 | error = ext4_xattr_block_find(inode, &i, &bs); | |
1237 | if (error) | |
1238 | goto cleanup; | |
1239 | } | |
617ba13b | 1240 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
ac27a0ec DK |
1241 | if (error) |
1242 | goto cleanup; | |
1243 | if (!is.s.not_found) { | |
1244 | i.value = NULL; | |
d5c8dab6 | 1245 | error = ext4_xattr_ibody_set(inode, &i, &is); |
ac27a0ec DK |
1246 | } |
1247 | } | |
1248 | } | |
1249 | if (!error) { | |
617ba13b | 1250 | ext4_xattr_update_super_block(handle, inode->i_sb); |
eeca7ea1 | 1251 | inode->i_ctime = current_time(inode); |
6dd4ee7c | 1252 | if (!value) |
19f5fb7a | 1253 | ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND); |
617ba13b | 1254 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
ac27a0ec | 1255 | /* |
617ba13b | 1256 | * The bh is consumed by ext4_mark_iloc_dirty, even with |
ac27a0ec DK |
1257 | * error != 0. |
1258 | */ | |
1259 | is.iloc.bh = NULL; | |
1260 | if (IS_SYNC(inode)) | |
0390131b | 1261 | ext4_handle_sync(handle); |
ac27a0ec DK |
1262 | } |
1263 | ||
1264 | cleanup: | |
1265 | brelse(is.iloc.bh); | |
1266 | brelse(bs.bh); | |
4d20c685 | 1267 | if (no_expand == 0) |
19f5fb7a | 1268 | ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND); |
617ba13b | 1269 | up_write(&EXT4_I(inode)->xattr_sem); |
ac27a0ec DK |
1270 | return error; |
1271 | } | |
1272 | ||
1273 | /* | |
617ba13b | 1274 | * ext4_xattr_set() |
ac27a0ec | 1275 | * |
617ba13b | 1276 | * Like ext4_xattr_set_handle, but start from an inode. This extended |
ac27a0ec DK |
1277 | * attribute modification is a filesystem transaction by itself. |
1278 | * | |
1279 | * Returns 0, or a negative error number on failure. | |
1280 | */ | |
1281 | int | |
617ba13b | 1282 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, |
ac27a0ec DK |
1283 | const void *value, size_t value_len, int flags) |
1284 | { | |
1285 | handle_t *handle; | |
1286 | int error, retries = 0; | |
95eaefbd | 1287 | int credits = ext4_jbd2_credits_xattr(inode); |
ac27a0ec DK |
1288 | |
1289 | retry: | |
9924a92a | 1290 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
ac27a0ec DK |
1291 | if (IS_ERR(handle)) { |
1292 | error = PTR_ERR(handle); | |
1293 | } else { | |
1294 | int error2; | |
1295 | ||
617ba13b | 1296 | error = ext4_xattr_set_handle(handle, inode, name_index, name, |
ac27a0ec | 1297 | value, value_len, flags); |
617ba13b | 1298 | error2 = ext4_journal_stop(handle); |
ac27a0ec | 1299 | if (error == -ENOSPC && |
617ba13b | 1300 | ext4_should_retry_alloc(inode->i_sb, &retries)) |
ac27a0ec DK |
1301 | goto retry; |
1302 | if (error == 0) | |
1303 | error = error2; | |
1304 | } | |
1305 | ||
1306 | return error; | |
1307 | } | |
1308 | ||
6dd4ee7c KS |
1309 | /* |
1310 | * Shift the EA entries in the inode to create space for the increased | |
1311 | * i_extra_isize. | |
1312 | */ | |
1313 | static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, | |
1314 | int value_offs_shift, void *to, | |
94405713 | 1315 | void *from, size_t n) |
6dd4ee7c KS |
1316 | { |
1317 | struct ext4_xattr_entry *last = entry; | |
1318 | int new_offs; | |
1319 | ||
94405713 JK |
1320 | /* We always shift xattr headers further thus offsets get lower */ |
1321 | BUG_ON(value_offs_shift > 0); | |
1322 | ||
6dd4ee7c KS |
1323 | /* Adjust the value offsets of the entries */ |
1324 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | |
1cba4237 | 1325 | if (last->e_value_size) { |
6dd4ee7c KS |
1326 | new_offs = le16_to_cpu(last->e_value_offs) + |
1327 | value_offs_shift; | |
6dd4ee7c KS |
1328 | last->e_value_offs = cpu_to_le16(new_offs); |
1329 | } | |
1330 | } | |
1331 | /* Shift the entries by n bytes */ | |
1332 | memmove(to, from, n); | |
1333 | } | |
1334 | ||
3f2571c1 JK |
1335 | /* |
1336 | * Move xattr pointed to by 'entry' from inode into external xattr block | |
1337 | */ | |
1338 | static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, | |
1339 | struct ext4_inode *raw_inode, | |
1340 | struct ext4_xattr_entry *entry) | |
1341 | { | |
1342 | struct ext4_xattr_ibody_find *is = NULL; | |
1343 | struct ext4_xattr_block_find *bs = NULL; | |
1344 | char *buffer = NULL, *b_entry_name = NULL; | |
1345 | size_t value_offs, value_size; | |
1346 | struct ext4_xattr_info i = { | |
1347 | .value = NULL, | |
1348 | .value_len = 0, | |
1349 | .name_index = entry->e_name_index, | |
1350 | }; | |
1351 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); | |
1352 | int error; | |
1353 | ||
1354 | value_offs = le16_to_cpu(entry->e_value_offs); | |
1355 | value_size = le32_to_cpu(entry->e_value_size); | |
1356 | ||
1357 | is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); | |
1358 | bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); | |
1359 | buffer = kmalloc(value_size, GFP_NOFS); | |
1360 | b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); | |
1361 | if (!is || !bs || !buffer || !b_entry_name) { | |
1362 | error = -ENOMEM; | |
1363 | goto out; | |
1364 | } | |
1365 | ||
1366 | is->s.not_found = -ENODATA; | |
1367 | bs->s.not_found = -ENODATA; | |
1368 | is->iloc.bh = NULL; | |
1369 | bs->bh = NULL; | |
1370 | ||
1371 | /* Save the entry name and the entry value */ | |
1372 | memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size); | |
1373 | memcpy(b_entry_name, entry->e_name, entry->e_name_len); | |
1374 | b_entry_name[entry->e_name_len] = '\0'; | |
1375 | i.name = b_entry_name; | |
1376 | ||
1377 | error = ext4_get_inode_loc(inode, &is->iloc); | |
1378 | if (error) | |
1379 | goto out; | |
1380 | ||
1381 | error = ext4_xattr_ibody_find(inode, &i, is); | |
1382 | if (error) | |
1383 | goto out; | |
1384 | ||
1385 | /* Remove the chosen entry from the inode */ | |
d5c8dab6 | 1386 | error = ext4_xattr_ibody_set(inode, &i, is); |
3f2571c1 JK |
1387 | if (error) |
1388 | goto out; | |
1389 | ||
1390 | i.name = b_entry_name; | |
1391 | i.value = buffer; | |
1392 | i.value_len = value_size; | |
1393 | error = ext4_xattr_block_find(inode, &i, bs); | |
1394 | if (error) | |
1395 | goto out; | |
1396 | ||
1397 | /* Add entry which was removed from the inode into the block */ | |
1398 | error = ext4_xattr_block_set(handle, inode, &i, bs); | |
1399 | if (error) | |
1400 | goto out; | |
1401 | error = 0; | |
1402 | out: | |
1403 | kfree(b_entry_name); | |
1404 | kfree(buffer); | |
1405 | if (is) | |
1406 | brelse(is->iloc.bh); | |
1407 | kfree(is); | |
1408 | kfree(bs); | |
1409 | ||
1410 | return error; | |
1411 | } | |
1412 | ||
dfa2064b JK |
1413 | static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode, |
1414 | struct ext4_inode *raw_inode, | |
1415 | int isize_diff, size_t ifree, | |
1416 | size_t bfree, int *total_ino) | |
1417 | { | |
1418 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); | |
1419 | struct ext4_xattr_entry *small_entry; | |
1420 | struct ext4_xattr_entry *entry; | |
1421 | struct ext4_xattr_entry *last; | |
1422 | unsigned int entry_size; /* EA entry size */ | |
1423 | unsigned int total_size; /* EA entry size + value size */ | |
1424 | unsigned int min_total_size; | |
1425 | int error; | |
1426 | ||
1427 | while (isize_diff > ifree) { | |
1428 | entry = NULL; | |
1429 | small_entry = NULL; | |
1430 | min_total_size = ~0U; | |
1431 | last = IFIRST(header); | |
1432 | /* Find the entry best suited to be pushed into EA block */ | |
1433 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | |
1434 | total_size = | |
1435 | EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) + | |
1436 | EXT4_XATTR_LEN(last->e_name_len); | |
1437 | if (total_size <= bfree && | |
1438 | total_size < min_total_size) { | |
1439 | if (total_size + ifree < isize_diff) { | |
1440 | small_entry = last; | |
1441 | } else { | |
1442 | entry = last; | |
1443 | min_total_size = total_size; | |
1444 | } | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | if (entry == NULL) { | |
1449 | if (small_entry == NULL) | |
1450 | return -ENOSPC; | |
1451 | entry = small_entry; | |
1452 | } | |
1453 | ||
1454 | entry_size = EXT4_XATTR_LEN(entry->e_name_len); | |
1455 | total_size = entry_size + | |
1456 | EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)); | |
1457 | error = ext4_xattr_move_to_block(handle, inode, raw_inode, | |
1458 | entry); | |
1459 | if (error) | |
1460 | return error; | |
1461 | ||
1462 | *total_ino -= entry_size; | |
1463 | ifree += total_size; | |
1464 | bfree -= total_size; | |
1465 | } | |
1466 | ||
1467 | return 0; | |
1468 | } | |
1469 | ||
6dd4ee7c KS |
1470 | /* |
1471 | * Expand an inode by new_extra_isize bytes when EAs are present. | |
1472 | * Returns 0 on success or negative error number on failure. | |
1473 | */ | |
1474 | int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, | |
1475 | struct ext4_inode *raw_inode, handle_t *handle) | |
1476 | { | |
1477 | struct ext4_xattr_ibody_header *header; | |
6dd4ee7c | 1478 | struct buffer_head *bh = NULL; |
e3014d14 JK |
1479 | size_t min_offs; |
1480 | size_t ifree, bfree; | |
7b1b2c1b | 1481 | int total_ino; |
6e0cd088 | 1482 | void *base, *end; |
d0141191 | 1483 | int error = 0, tried_min_extra_isize = 0; |
ac39849d | 1484 | int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize); |
d0141191 | 1485 | int isize_diff; /* How much do we need to grow i_extra_isize */ |
6dd4ee7c KS |
1486 | |
1487 | down_write(&EXT4_I(inode)->xattr_sem); | |
2e81a4ee JK |
1488 | /* |
1489 | * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty | |
1490 | */ | |
1491 | ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND); | |
6dd4ee7c | 1492 | retry: |
d0141191 | 1493 | isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize; |
2e81a4ee JK |
1494 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
1495 | goto out; | |
6dd4ee7c KS |
1496 | |
1497 | header = IHDR(inode, raw_inode); | |
6dd4ee7c KS |
1498 | |
1499 | /* | |
1500 | * Check if enough free space is available in the inode to shift the | |
1501 | * entries ahead by new_extra_isize. | |
1502 | */ | |
1503 | ||
6e0cd088 | 1504 | base = IFIRST(header); |
6dd4ee7c KS |
1505 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
1506 | min_offs = end - base; | |
6dd4ee7c KS |
1507 | total_ino = sizeof(struct ext4_xattr_ibody_header); |
1508 | ||
9e92f48c TT |
1509 | error = xattr_check_inode(inode, header, end); |
1510 | if (error) | |
1511 | goto cleanup; | |
1512 | ||
6e0cd088 | 1513 | ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino); |
e3014d14 JK |
1514 | if (ifree >= isize_diff) |
1515 | goto shift; | |
6dd4ee7c KS |
1516 | |
1517 | /* | |
1518 | * Enough free space isn't available in the inode, check if | |
1519 | * EA block can hold new_extra_isize bytes. | |
1520 | */ | |
1521 | if (EXT4_I(inode)->i_file_acl) { | |
1522 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); | |
1523 | error = -EIO; | |
1524 | if (!bh) | |
1525 | goto cleanup; | |
cc8e94fd | 1526 | if (ext4_xattr_check_block(inode, bh)) { |
24676da4 TT |
1527 | EXT4_ERROR_INODE(inode, "bad block %llu", |
1528 | EXT4_I(inode)->i_file_acl); | |
6a797d27 | 1529 | error = -EFSCORRUPTED; |
6dd4ee7c KS |
1530 | goto cleanup; |
1531 | } | |
1532 | base = BHDR(bh); | |
6dd4ee7c KS |
1533 | end = bh->b_data + bh->b_size; |
1534 | min_offs = end - base; | |
6e0cd088 JK |
1535 | bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base, |
1536 | NULL); | |
e3014d14 | 1537 | if (bfree + ifree < isize_diff) { |
6dd4ee7c KS |
1538 | if (!tried_min_extra_isize && s_min_extra_isize) { |
1539 | tried_min_extra_isize++; | |
1540 | new_extra_isize = s_min_extra_isize; | |
1541 | brelse(bh); | |
1542 | goto retry; | |
1543 | } | |
dfa2064b | 1544 | error = -ENOSPC; |
6dd4ee7c KS |
1545 | goto cleanup; |
1546 | } | |
1547 | } else { | |
e3014d14 | 1548 | bfree = inode->i_sb->s_blocksize; |
6dd4ee7c KS |
1549 | } |
1550 | ||
dfa2064b JK |
1551 | error = ext4_xattr_make_inode_space(handle, inode, raw_inode, |
1552 | isize_diff, ifree, bfree, | |
1553 | &total_ino); | |
1554 | if (error) { | |
1555 | if (error == -ENOSPC && !tried_min_extra_isize && | |
1556 | s_min_extra_isize) { | |
1557 | tried_min_extra_isize++; | |
1558 | new_extra_isize = s_min_extra_isize; | |
1559 | brelse(bh); | |
1560 | goto retry; | |
6dd4ee7c | 1561 | } |
dfa2064b | 1562 | goto cleanup; |
6dd4ee7c | 1563 | } |
e3014d14 JK |
1564 | shift: |
1565 | /* Adjust the offsets and shift the remaining entries ahead */ | |
6e0cd088 | 1566 | ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize |
e3014d14 JK |
1567 | - new_extra_isize, (void *)raw_inode + |
1568 | EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, | |
94405713 | 1569 | (void *)header, total_ino); |
e3014d14 | 1570 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
6dd4ee7c | 1571 | brelse(bh); |
2e81a4ee JK |
1572 | out: |
1573 | ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND); | |
6dd4ee7c KS |
1574 | up_write(&EXT4_I(inode)->xattr_sem); |
1575 | return 0; | |
1576 | ||
1577 | cleanup: | |
6dd4ee7c | 1578 | brelse(bh); |
2e81a4ee JK |
1579 | /* |
1580 | * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode | |
1581 | * size expansion failed. | |
1582 | */ | |
6dd4ee7c KS |
1583 | up_write(&EXT4_I(inode)->xattr_sem); |
1584 | return error; | |
1585 | } | |
1586 | ||
1587 | ||
1588 | ||
ac27a0ec | 1589 | /* |
617ba13b | 1590 | * ext4_xattr_delete_inode() |
ac27a0ec DK |
1591 | * |
1592 | * Free extended attribute resources associated with this inode. This | |
1593 | * is called immediately before an inode is freed. We have exclusive | |
1594 | * access to the inode. | |
1595 | */ | |
1596 | void | |
617ba13b | 1597 | ext4_xattr_delete_inode(handle_t *handle, struct inode *inode) |
ac27a0ec DK |
1598 | { |
1599 | struct buffer_head *bh = NULL; | |
1600 | ||
617ba13b | 1601 | if (!EXT4_I(inode)->i_file_acl) |
ac27a0ec | 1602 | goto cleanup; |
617ba13b | 1603 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
ac27a0ec | 1604 | if (!bh) { |
24676da4 TT |
1605 | EXT4_ERROR_INODE(inode, "block %llu read error", |
1606 | EXT4_I(inode)->i_file_acl); | |
ac27a0ec DK |
1607 | goto cleanup; |
1608 | } | |
617ba13b | 1609 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
ac27a0ec | 1610 | BHDR(bh)->h_blocks != cpu_to_le32(1)) { |
24676da4 TT |
1611 | EXT4_ERROR_INODE(inode, "bad block %llu", |
1612 | EXT4_I(inode)->i_file_acl); | |
ac27a0ec DK |
1613 | goto cleanup; |
1614 | } | |
617ba13b MC |
1615 | ext4_xattr_release_block(handle, inode, bh); |
1616 | EXT4_I(inode)->i_file_acl = 0; | |
ac27a0ec DK |
1617 | |
1618 | cleanup: | |
1619 | brelse(bh); | |
1620 | } | |
1621 | ||
ac27a0ec | 1622 | /* |
617ba13b | 1623 | * ext4_xattr_cache_insert() |
ac27a0ec DK |
1624 | * |
1625 | * Create a new entry in the extended attribute cache, and insert | |
1626 | * it unless such an entry is already in the cache. | |
1627 | * | |
1628 | * Returns 0, or a negative error number on failure. | |
1629 | */ | |
1630 | static void | |
7a2508e1 | 1631 | ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh) |
ac27a0ec | 1632 | { |
6048c64b AG |
1633 | struct ext4_xattr_header *header = BHDR(bh); |
1634 | __u32 hash = le32_to_cpu(header->h_hash); | |
1635 | int reusable = le32_to_cpu(header->h_refcount) < | |
1636 | EXT4_XATTR_REFCOUNT_MAX; | |
ac27a0ec DK |
1637 | int error; |
1638 | ||
7a2508e1 | 1639 | error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash, |
6048c64b | 1640 | bh->b_blocknr, reusable); |
ac27a0ec | 1641 | if (error) { |
82939d79 | 1642 | if (error == -EBUSY) |
ac27a0ec | 1643 | ea_bdebug(bh, "already in cache"); |
82939d79 | 1644 | } else |
ac27a0ec | 1645 | ea_bdebug(bh, "inserting [%x]", (int)hash); |
ac27a0ec DK |
1646 | } |
1647 | ||
1648 | /* | |
617ba13b | 1649 | * ext4_xattr_cmp() |
ac27a0ec DK |
1650 | * |
1651 | * Compare two extended attribute blocks for equality. | |
1652 | * | |
1653 | * Returns 0 if the blocks are equal, 1 if they differ, and | |
1654 | * a negative error number on errors. | |
1655 | */ | |
1656 | static int | |
617ba13b MC |
1657 | ext4_xattr_cmp(struct ext4_xattr_header *header1, |
1658 | struct ext4_xattr_header *header2) | |
ac27a0ec | 1659 | { |
617ba13b | 1660 | struct ext4_xattr_entry *entry1, *entry2; |
ac27a0ec DK |
1661 | |
1662 | entry1 = ENTRY(header1+1); | |
1663 | entry2 = ENTRY(header2+1); | |
1664 | while (!IS_LAST_ENTRY(entry1)) { | |
1665 | if (IS_LAST_ENTRY(entry2)) | |
1666 | return 1; | |
1667 | if (entry1->e_hash != entry2->e_hash || | |
1668 | entry1->e_name_index != entry2->e_name_index || | |
1669 | entry1->e_name_len != entry2->e_name_len || | |
1670 | entry1->e_value_size != entry2->e_value_size || | |
1671 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) | |
1672 | return 1; | |
1673 | if (entry1->e_value_block != 0 || entry2->e_value_block != 0) | |
6a797d27 | 1674 | return -EFSCORRUPTED; |
ac27a0ec DK |
1675 | if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), |
1676 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), | |
1677 | le32_to_cpu(entry1->e_value_size))) | |
1678 | return 1; | |
1679 | ||
617ba13b MC |
1680 | entry1 = EXT4_XATTR_NEXT(entry1); |
1681 | entry2 = EXT4_XATTR_NEXT(entry2); | |
ac27a0ec DK |
1682 | } |
1683 | if (!IS_LAST_ENTRY(entry2)) | |
1684 | return 1; | |
1685 | return 0; | |
1686 | } | |
1687 | ||
1688 | /* | |
617ba13b | 1689 | * ext4_xattr_cache_find() |
ac27a0ec DK |
1690 | * |
1691 | * Find an identical extended attribute block. | |
1692 | * | |
1693 | * Returns a pointer to the block found, or NULL if such a block was | |
1694 | * not found or an error occurred. | |
1695 | */ | |
1696 | static struct buffer_head * | |
617ba13b | 1697 | ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header, |
7a2508e1 | 1698 | struct mb_cache_entry **pce) |
ac27a0ec DK |
1699 | { |
1700 | __u32 hash = le32_to_cpu(header->h_hash); | |
7a2508e1 JK |
1701 | struct mb_cache_entry *ce; |
1702 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); | |
ac27a0ec DK |
1703 | |
1704 | if (!header->h_hash) | |
1705 | return NULL; /* never share */ | |
1706 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); | |
7a2508e1 | 1707 | ce = mb_cache_entry_find_first(ext4_mb_cache, hash); |
ac27a0ec DK |
1708 | while (ce) { |
1709 | struct buffer_head *bh; | |
1710 | ||
ac27a0ec DK |
1711 | bh = sb_bread(inode->i_sb, ce->e_block); |
1712 | if (!bh) { | |
24676da4 TT |
1713 | EXT4_ERROR_INODE(inode, "block %lu read error", |
1714 | (unsigned long) ce->e_block); | |
617ba13b | 1715 | } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { |
ac27a0ec DK |
1716 | *pce = ce; |
1717 | return bh; | |
1718 | } | |
1719 | brelse(bh); | |
7a2508e1 | 1720 | ce = mb_cache_entry_find_next(ext4_mb_cache, ce); |
ac27a0ec DK |
1721 | } |
1722 | return NULL; | |
1723 | } | |
1724 | ||
1725 | #define NAME_HASH_SHIFT 5 | |
1726 | #define VALUE_HASH_SHIFT 16 | |
1727 | ||
1728 | /* | |
617ba13b | 1729 | * ext4_xattr_hash_entry() |
ac27a0ec DK |
1730 | * |
1731 | * Compute the hash of an extended attribute. | |
1732 | */ | |
617ba13b MC |
1733 | static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header, |
1734 | struct ext4_xattr_entry *entry) | |
ac27a0ec DK |
1735 | { |
1736 | __u32 hash = 0; | |
1737 | char *name = entry->e_name; | |
1738 | int n; | |
1739 | ||
2b2d6d01 | 1740 | for (n = 0; n < entry->e_name_len; n++) { |
ac27a0ec DK |
1741 | hash = (hash << NAME_HASH_SHIFT) ^ |
1742 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ | |
1743 | *name++; | |
1744 | } | |
1745 | ||
1cba4237 | 1746 | if (entry->e_value_size != 0) { |
ac27a0ec DK |
1747 | __le32 *value = (__le32 *)((char *)header + |
1748 | le16_to_cpu(entry->e_value_offs)); | |
1749 | for (n = (le32_to_cpu(entry->e_value_size) + | |
617ba13b | 1750 | EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) { |
ac27a0ec DK |
1751 | hash = (hash << VALUE_HASH_SHIFT) ^ |
1752 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ | |
1753 | le32_to_cpu(*value++); | |
1754 | } | |
1755 | } | |
1756 | entry->e_hash = cpu_to_le32(hash); | |
1757 | } | |
1758 | ||
1759 | #undef NAME_HASH_SHIFT | |
1760 | #undef VALUE_HASH_SHIFT | |
1761 | ||
1762 | #define BLOCK_HASH_SHIFT 16 | |
1763 | ||
1764 | /* | |
617ba13b | 1765 | * ext4_xattr_rehash() |
ac27a0ec DK |
1766 | * |
1767 | * Re-compute the extended attribute hash value after an entry has changed. | |
1768 | */ | |
617ba13b MC |
1769 | static void ext4_xattr_rehash(struct ext4_xattr_header *header, |
1770 | struct ext4_xattr_entry *entry) | |
ac27a0ec | 1771 | { |
617ba13b | 1772 | struct ext4_xattr_entry *here; |
ac27a0ec DK |
1773 | __u32 hash = 0; |
1774 | ||
617ba13b | 1775 | ext4_xattr_hash_entry(header, entry); |
ac27a0ec DK |
1776 | here = ENTRY(header+1); |
1777 | while (!IS_LAST_ENTRY(here)) { | |
1778 | if (!here->e_hash) { | |
1779 | /* Block is not shared if an entry's hash value == 0 */ | |
1780 | hash = 0; | |
1781 | break; | |
1782 | } | |
1783 | hash = (hash << BLOCK_HASH_SHIFT) ^ | |
1784 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ | |
1785 | le32_to_cpu(here->e_hash); | |
617ba13b | 1786 | here = EXT4_XATTR_NEXT(here); |
ac27a0ec DK |
1787 | } |
1788 | header->h_hash = cpu_to_le32(hash); | |
1789 | } | |
1790 | ||
1791 | #undef BLOCK_HASH_SHIFT | |
1792 | ||
9c191f70 M |
1793 | #define HASH_BUCKET_BITS 10 |
1794 | ||
7a2508e1 | 1795 | struct mb_cache * |
82939d79 | 1796 | ext4_xattr_create_cache(void) |
ac27a0ec | 1797 | { |
7a2508e1 | 1798 | return mb_cache_create(HASH_BUCKET_BITS); |
ac27a0ec DK |
1799 | } |
1800 | ||
7a2508e1 | 1801 | void ext4_xattr_destroy_cache(struct mb_cache *cache) |
ac27a0ec | 1802 | { |
9c191f70 | 1803 | if (cache) |
7a2508e1 | 1804 | mb_cache_destroy(cache); |
ac27a0ec | 1805 | } |
9c191f70 | 1806 |