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