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