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