1 diff -ur linux-stage.orig/fs/ext4/ext4.h linux-stage/fs/ext4/ext4.h
2 --- linux-stage.orig/fs/ext4/ext4.h 2012-12-31 15:56:25.000000000 -0500
3 +++ linux-stage/fs/ext4/ext4.h 2012-12-31 15:56:48.000000000 -0500
5 EXT4_FEATURE_INCOMPAT_EXTENTS| \
6 EXT4_FEATURE_INCOMPAT_64BIT| \
7 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
8 + EXT4_FEATURE_INCOMPAT_EA_INODE| \
9 EXT4_FEATURE_INCOMPAT_MMP| \
10 EXT4_FEATURE_INCOMPAT_DIRDATA)
12 @@ -1774,6 +1775,12 @@
16 + * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1Mb
17 + * This limit is arbitrary, but is reasonable for the xattr API.
19 +#define EXT4_XATTR_MAX_LARGE_EA_SIZE (1024 * 1024)
26 extern void ext4_get_inode_flags(struct ext4_inode_info *);
27 extern int ext4_alloc_da_blocks(struct inode *inode);
28 extern void ext4_set_aops(struct inode *inode);
29 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
30 extern int ext4_writepage_trans_blocks(struct inode *);
31 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
32 extern int ext4_block_truncate_page(handle_t *handle,
33 diff -ur linux-stage.orig/fs/ext4/inode.c linux-stage/fs/ext4/inode.c
34 --- linux-stage.orig/fs/ext4/inode.c 2013-01-03 09:31:07.000000000 -0500
35 +++ linux-stage/fs/ext4/inode.c 2013-01-03 09:31:23.000000000 -0500
38 * Also account for superblock, inode, quota and xattr blocks
40 -static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
41 +int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
43 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
45 diff -ur linux-stage.orig/fs/ext4/xattr.c linux-stage/fs/ext4/xattr.c
46 --- linux-stage.orig/fs/ext4/xattr.c 2012-12-31 15:56:25.000000000 -0500
47 +++ linux-stage/fs/ext4/xattr.c 2012-12-31 15:56:48.000000000 -0500
52 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
53 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
54 + struct inode *inode)
56 size_t value_size = le32_to_cpu(entry->e_value_size);
58 - if (entry->e_value_block != 0 || value_size > size ||
59 - le16_to_cpu(entry->e_value_offs) + value_size > size)
60 + if ((entry->e_value_inum == 0) &&
61 + (le16_to_cpu(entry->e_value_offs) + value_size > size))
63 + if (entry->e_value_inum != 0 &&
64 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
65 + le32_to_cpu(entry->e_value_inum) >
66 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
72 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
73 - const char *name, size_t size, int sorted)
74 + const char *name, size_t size, int sorted,
75 + struct inode *inode)
77 struct ext4_xattr_entry *entry;
79 @@ -200,11 +207,103 @@
83 - if (!cmp && ext4_xattr_check_entry(entry, size))
84 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
86 return cmp ? -ENODATA : 0;
90 + * Read the EA value from an inode.
93 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
95 + unsigned long block = 0;
96 + struct buffer_head *bh = NULL;
98 + size_t csize, ret_size = 0;
103 + blocksize = ea_inode->i_sb->s_blocksize;
105 + while (ret_size < *size) {
106 + csize = (*size - ret_size) > blocksize ? blocksize :
108 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
113 + memcpy(buf, bh->b_data, csize);
126 +struct inode *ext4_xattr_inode_iget(struct inode *parent, int ea_ino, int *err)
128 + struct inode *ea_inode = NULL;
130 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
131 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
132 + ext4_error(parent->i_sb, "error while reading EA inode %d",
138 + if (ea_inode->i_xattr_inode_parent != parent->i_ino ||
139 + ea_inode->i_generation != parent->i_generation) {
140 + ext4_error(parent->i_sb, "Backpointer from EA inode %d "
141 + "to parent invalid.", ea_ino);
146 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
147 + ext4_error(parent->i_sb, "EA inode %d does not have "
148 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
162 + * Read the value from the EA inode.
165 +ext4_xattr_inode_get(struct inode *inode, int ea_ino, void *buffer,
168 + struct inode *ea_inode = NULL;
171 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
175 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
182 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
183 void *buffer, size_t buffer_size)
186 ext4_xattr_cache_insert(bh);
188 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
189 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
196 if (size > buffer_size)
198 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
200 + if (entry->e_value_inum != 0) {
201 + error = ext4_xattr_inode_get(inode,
202 + le32_to_cpu(entry->e_value_inum),
207 + memcpy(buffer, bh->b_data +
208 + le16_to_cpu(entry->e_value_offs), size);
216 error = ext4_xattr_find_entry(&entry, name_index, name,
217 - end - (void *)entry, 0);
218 + end - (void *)entry, 0, inode);
221 size = le32_to_cpu(entry->e_value_size);
224 if (size > buffer_size)
226 - memcpy(buffer, (void *)IFIRST(header) +
227 - le16_to_cpu(entry->e_value_offs), size);
228 + if (entry->e_value_inum != 0) {
229 + error = ext4_xattr_inode_get(inode,
230 + le32_to_cpu(entry->e_value_inum),
235 + memcpy(buffer, (void *)IFIRST(header) +
236 + le16_to_cpu(entry->e_value_offs), size);
243 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
244 *total += EXT4_XATTR_LEN(last->e_name_len);
245 - if (!last->e_value_block && last->e_value_size) {
246 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
247 size_t offs = le16_to_cpu(last->e_value_offs);
248 if (offs < *min_offs)
250 @@ -523,11 +639,162 @@
251 return (*min_offs - ((void *)last - base) - sizeof(__u32));
255 + * Write the value of the EA in an inode.
258 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
259 + const void *buf, int bufsize)
261 + struct buffer_head *bh = NULL;
262 + struct ext4_map_blocks map;
263 + unsigned long block = 0;
264 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
265 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
266 + int csize, wsize = 0;
271 + while (ret >= 0 && ret < max_blocks) {
275 + map.m_lblk = block;
276 + map.m_len = max_blocks;
277 + ret = ext4_map_blocks(handle, ea_inode, &map,
278 + EXT4_GET_BLOCKS_CREATE);
280 + ext4_mark_inode_dirty(handle, ea_inode);
281 + if (ret == -ENOSPC &&
282 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
294 + while (wsize < bufsize) {
297 + csize = (bufsize - wsize) > blocksize ? blocksize :
299 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
302 + ret = ext4_journal_get_write_access(handle, bh);
306 + memcpy(bh->b_data, buf, csize);
307 + set_buffer_uptodate(bh);
308 + ext4_journal_dirty_metadata(handle, bh);
315 + i_size_write(ea_inode, wsize);
316 + ext4_update_i_disksize(ea_inode, wsize);
318 + ext4_mark_inode_dirty(handle, ea_inode);
327 + * Create an inode to store the value of a large EA.
329 +static struct inode *
330 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
332 + struct inode *ea_inode = NULL;
335 + * Let the next inode be the goal, so we try and allocate the EA inode
336 + * in the same group, or nearby one.
338 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
339 + S_IFREG|0600, NULL, inode->i_ino + 1);
341 + if (!IS_ERR(ea_inode)) {
342 + ea_inode->i_op = &ext4_file_inode_operations;
343 + ea_inode->i_fop = &ext4_file_operations;
344 + ext4_set_aops(ea_inode);
345 + ea_inode->i_generation = inode->i_generation;
346 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
349 + * A back-pointer from EA inode to parent inode will be useful
352 + ea_inode->i_xattr_inode_parent = inode->i_ino;
353 + unlock_new_inode(ea_inode);
360 + * Unlink the inode storing the value of the EA.
363 +ext4_xattr_inode_unlink(struct inode *inode, int ea_ino)
365 + struct inode *ea_inode = NULL;
368 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
372 + ea_inode->i_nlink = 0;
379 + * Add value of the EA in an inode.
382 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, int *ea_ino,
383 + const void *value, size_t value_len)
385 + struct inode *ea_inode = NULL;
388 + /* Create an inode for the EA value */
389 + ea_inode = ext4_xattr_inode_create(handle, inode);
390 + if (IS_ERR(ea_inode))
393 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
395 + ea_inode->i_nlink = 0;
397 + *ea_ino = ea_inode->i_ino;
404 struct ext4_xattr_info {
413 struct ext4_xattr_search {
414 @@ -539,15 +803,23 @@
418 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
419 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
420 + handle_t *handle, struct inode *inode)
422 struct ext4_xattr_entry *last;
423 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
424 + int in_inode = i->in_inode;
426 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
427 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
428 + (EXT4_XATTR_SIZE(i->value_len) >
429 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
432 /* Compute min_offs and last. */
434 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
435 - if (!last->e_value_block && last->e_value_size) {
436 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
437 size_t offs = le16_to_cpu(last->e_value_offs);
440 @@ -555,16 +827,21 @@
442 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
444 - if (!s->here->e_value_block && s->here->e_value_size) {
445 + if (!in_inode && s->here->e_value_inum == 0 &&
446 + s->here->e_value_size > 0) {
447 size_t size = le32_to_cpu(s->here->e_value_size);
448 free += EXT4_XATTR_SIZE(size);
450 free += EXT4_XATTR_LEN(name_len);
453 - if (free < EXT4_XATTR_SIZE(i->value_len) ||
454 - free < EXT4_XATTR_LEN(name_len) +
455 - EXT4_XATTR_SIZE(i->value_len))
456 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
461 + if (free < value_len ||
462 + free < EXT4_XATTR_LEN(name_len) + value_len)
467 s->here->e_name_len = name_len;
468 memcpy(s->here->e_name, i->name, name_len);
470 - if (!s->here->e_value_block && s->here->e_value_size) {
471 + if (s->here->e_value_offs > 0 && s->here->e_value_inum == 0 &&
472 + s->here->e_value_size > 0) {
473 void *first_val = s->base + min_offs;
474 size_t offs = le16_to_cpu(s->here->e_value_offs);
475 void *val = s->base + offs;
476 @@ -607,13 +885,17 @@
478 while (!IS_LAST_ENTRY(last)) {
479 size_t o = le16_to_cpu(last->e_value_offs);
480 - if (!last->e_value_block &&
481 - last->e_value_size && o < offs)
482 + if (last->e_value_size > 0 && o < offs)
484 cpu_to_le16(o + size);
485 last = EXT4_XATTR_NEXT(last);
488 + if (s->here->e_value_inum != 0) {
489 + ext4_xattr_inode_unlink(inode,
490 + le32_to_cpu(s->here->e_value_inum));
491 + s->here->e_value_inum = 0;
494 /* Remove the old name. */
495 size_t size = EXT4_XATTR_LEN(name_len);
496 @@ -627,10 +909,17 @@
498 /* Insert the new value. */
499 s->here->e_value_size = cpu_to_le32(i->value_len);
500 - if (i->value_len) {
502 + int ea_ino = le32_to_cpu(s->here->e_value_inum);
503 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
505 + s->here->e_value_inum = cpu_to_le32(ea_ino);
506 + s->here->e_value_offs = 0;
507 + } else if (i->value_len) {
508 size_t size = EXT4_XATTR_SIZE(i->value_len);
509 void *val = s->base + min_offs - size;
510 s->here->e_value_offs = cpu_to_le16(min_offs - size);
511 + s->here->e_value_inum = 0;
512 memset(val + size - EXT4_XATTR_PAD, 0,
513 EXT4_XATTR_PAD); /* Clear the pad bytes. */
514 memcpy(val, i->value, i->value_len);
516 bs->s.end = bs->bh->b_data + bs->bh->b_size;
517 bs->s.here = bs->s.first;
518 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
519 - i->name, bs->bh->b_size, 1);
520 + i->name, bs->bh->b_size, 1, inode);
521 if (error && error != -ENODATA)
523 bs->s.not_found = error;
526 #define header(x) ((struct ext4_xattr_header *)(x))
528 - if (i->value && i->value_len > sb->s_blocksize)
531 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
536 ea_bdebug(bs->bh, "modifying in-place");
537 - error = ext4_xattr_set_entry(i, s);
538 + error = ext4_xattr_set_entry(i, s, handle, inode);
540 if (!IS_LAST_ENTRY(s->first))
541 ext4_xattr_rehash(header(s->base),
543 s->end = s->base + sb->s_blocksize;
546 - error = ext4_xattr_set_entry(i, s);
547 + error = ext4_xattr_set_entry(i, s, handle, inode);
552 /* Find the named attribute. */
553 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
555 - (void *)is->s.base, 0);
556 + (void *)is->s.base, 0, inode);
557 if (error && error != -ENODATA)
559 is->s.not_found = error;
562 if (EXT4_I(inode)->i_extra_isize == 0)
564 - error = ext4_xattr_set_entry(i, s);
565 + error = ext4_xattr_set_entry(i, s, handle, inode);
568 header = IHDR(inode, ext4_raw_inode(&is->iloc));
572 .value_len = value_len,
576 struct ext4_xattr_ibody_find is = {
577 .s = { .not_found = -ENODATA, },
578 @@ -1042,6 +1329,15 @@
581 error = ext4_xattr_block_set(handle, inode, &i, &bs);
582 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
583 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
584 + error == -ENOSPC) {
585 + /* xattr not fit to block, store at external
588 + error = ext4_xattr_ibody_set(handle, inode,
593 if (!is.s.not_found) {
594 @@ -1089,10 +1385,25 @@
595 const void *value, size_t value_len, int flags)
598 + struct super_block *sb = inode->i_sb;
599 + int buffer_credits;
600 int error, retries = 0;
602 + buffer_credits = EXT4_DATA_TRANS_BLOCKS(sb);
603 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
604 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
605 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
606 + sb->s_blocksize_bits;
608 + /* For new inode */
609 + buffer_credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
611 + /* For data blocks of EA inode */
612 + buffer_credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
616 - handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
617 + handle = ext4_journal_start(inode, buffer_credits);
618 if (IS_ERR(handle)) {
619 error = PTR_ERR(handle);
621 @@ -1102,7 +1413,7 @@
622 value, value_len, flags);
623 error2 = ext4_journal_stop(handle);
624 if (error == -ENOSPC &&
625 - ext4_should_retry_alloc(inode->i_sb, &retries))
626 + ext4_should_retry_alloc(sb, &retries))
630 @@ -1124,7 +1435,7 @@
632 /* Adjust the value offsets of the entries */
633 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
634 - if (!last->e_value_block && last->e_value_size) {
635 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
636 new_offs = le16_to_cpu(last->e_value_offs) +
638 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
639 @@ -1364,15 +1675,41 @@
641 * ext4_xattr_delete_inode()
643 - * Free extended attribute resources associated with this inode. This
644 + * Free extended attribute resources associated with this inode. Traverse
645 + * all entries and unlink any xattr inodes associated with this inode. This
646 * is called immediately before an inode is freed. We have exclusive
647 - * access to the inode.
648 + * access to the inode. If an orphan inode is deleted it will also delete any
649 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
650 + * to ensure they belong to the parent inode and were not deleted already.
653 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
655 struct buffer_head *bh = NULL;
656 + struct ext4_xattr_ibody_header *header;
657 + struct ext4_inode *raw_inode;
658 + struct ext4_iloc iloc;
659 + struct ext4_xattr_entry *entry;
662 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
663 + goto delete_external_ea;
665 + error = ext4_get_inode_loc(inode, &iloc);
668 + raw_inode = ext4_raw_inode(&iloc);
669 + header = IHDR(inode, raw_inode);
670 + entry = IFIRST(header);
671 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
672 + if (entry->e_value_inum != 0) {
673 + ext4_xattr_inode_unlink(inode,
674 + le32_to_cpu(entry->e_value_inum));
675 + entry->e_value_inum = 0;
680 if (!EXT4_I(inode)->i_file_acl)
682 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
683 @@ -1387,6 +1724,16 @@
684 EXT4_I(inode)->i_file_acl);
688 + entry = BFIRST(bh);
689 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
690 + if (entry->e_value_inum != 0) {
691 + ext4_xattr_inode_unlink(inode,
692 + le32_to_cpu(entry->e_value_inum));
693 + entry->e_value_inum = 0;
697 ext4_xattr_release_block(handle, inode, bh);
698 EXT4_I(inode)->i_file_acl = 0;
700 @@ -1461,10 +1808,9 @@
701 entry1->e_name_index != entry2->e_name_index ||
702 entry1->e_name_len != entry2->e_name_len ||
703 entry1->e_value_size != entry2->e_value_size ||
704 + entry1->e_value_inum != entry2->e_value_inum ||
705 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
707 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
709 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
710 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
711 le32_to_cpu(entry1->e_value_size)))
712 @@ -1548,7 +1894,7 @@
716 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
717 + if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
718 __le32 *value = (__le32 *)((char *)header +
719 le16_to_cpu(entry->e_value_offs));
720 for (n = (le32_to_cpu(entry->e_value_size) +
721 diff -ur linux-stage.orig/fs/ext4/xattr.h linux-stage/fs/ext4/xattr.h
722 --- linux-stage.orig/fs/ext4/xattr.h 2012-12-31 15:56:25.000000000 -0500
723 +++ linux-stage/fs/ext4/xattr.h 2012-12-31 15:56:48.000000000 -0500
725 __u8 e_name_len; /* length of name */
726 __u8 e_name_index; /* attribute name index */
727 __le16 e_value_offs; /* offset in disk block of value */
728 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
729 + __le32 e_value_inum; /* inode in which the value is stored */
730 __le32 e_value_size; /* size of attribute value */
731 __le32 e_hash; /* hash value of name and value */
732 char e_name[0]; /* attribute name */
734 EXT4_I(inode)->i_extra_isize))
735 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
737 +#define i_xattr_inode_parent i_mtime.tv_sec
740 + * The minimum size of EA value when you start storing it in an external inode
741 + * size of block - size of header - size of 1 entry - 4 null bytes
743 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
744 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
746 # ifdef CONFIG_EXT4_FS_XATTR
748 extern const struct xattr_handler ext4_xattr_user_handler;