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