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