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