Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfasheh/ocfs2

* 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfasheh/ocfs2: (21 commits)
ocfs2: Check search result in ocfs2_xattr_block_get()
ocfs2: fix printk related build warnings in xattr.c
ocfs2: truncate outstanding block after direct io failure
ocfs2/xattr: Proper hash collision handle in bucket division
ocfs2: return 0 in page_mkwrite to let VFS retry.
ocfs2: Set journal descriptor to NULL after journal shutdown
ocfs2: Fix check of return value of ocfs2_start_trans() in xattr.c.
ocfs2: Let inode be really deleted when ocfs2_mknod_locked() fails
ocfs2: Fix checking of return value of new_inode()
ocfs2: Fix check of return value of ocfs2_start_trans()
ocfs2: Fix some typos in xattr annotations.
ocfs2: Remove unused ocfs2_restore_xattr_block().
ocfs2: Don't repeat ocfs2_xattr_block_find()
ocfs2: Specify appropriate journal access for new xattr buckets.
ocfs2: Check errors from ocfs2_xattr_update_xattr_search()
ocfs2: Don't return -EFAULT from a corrupt xattr entry.
ocfs2: Check xattr block signatures properly.
ocfs2: add handler_map array bounds checking
ocfs2: remove duplicate definition in xattr
ocfs2: fix function declaration and definition in xattr
...

+257 -223
+17 -10
fs/ocfs2/file.c
··· 247 247 mlog_entry_void(); 248 248 249 249 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 250 - if (handle == NULL) { 251 - ret = -ENOMEM; 250 + if (IS_ERR(handle)) { 251 + ret = PTR_ERR(handle); 252 252 mlog_errno(ret); 253 253 goto out; 254 254 } ··· 312 312 handle_t *handle = NULL; 313 313 314 314 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 315 - if (handle == NULL) { 316 - ret = -ENOMEM; 315 + if (IS_ERR(handle)) { 316 + ret = PTR_ERR(handle); 317 317 mlog_errno(ret); 318 318 goto out; 319 319 } ··· 1055 1055 (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode); 1056 1056 1057 1057 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 1058 - if (handle == NULL) { 1059 - ret = -ENOMEM; 1058 + if (IS_ERR(handle)) { 1059 + ret = PTR_ERR(handle); 1060 1060 mlog_errno(ret); 1061 1061 goto out; 1062 1062 } ··· 1259 1259 } 1260 1260 1261 1261 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); 1262 - if (handle == NULL) { 1263 - ret = -ENOMEM; 1262 + if (IS_ERR(handle)) { 1263 + ret = PTR_ERR(handle); 1264 1264 mlog_errno(ret); 1265 1265 goto out; 1266 1266 } ··· 1352 1352 goto out; 1353 1353 1354 1354 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 1355 - if (handle == NULL) { 1356 - ret = -ENOMEM; 1355 + if (IS_ERR(handle)) { 1356 + ret = PTR_ERR(handle); 1357 1357 mlog_errno(ret); 1358 1358 goto out; 1359 1359 } ··· 1866 1866 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos, 1867 1867 ppos, count, ocount); 1868 1868 if (written < 0) { 1869 + /* 1870 + * direct write may have instantiated a few 1871 + * blocks outside i_size. Trim these off again. 1872 + * Don't need i_size_read because we hold i_mutex. 1873 + */ 1874 + if (*ppos + count > inode->i_size) 1875 + vmtruncate(inode, inode->i_size); 1869 1876 ret = written; 1870 1877 goto out_dio; 1871 1878 }
+6
fs/ocfs2/inode.c
··· 1106 1106 oi->ip_last_trans = 0; 1107 1107 oi->ip_dir_start_lookup = 0; 1108 1108 oi->ip_blkno = 0ULL; 1109 + 1110 + /* 1111 + * ip_jinode is used to track txns against this inode. We ensure that 1112 + * the journal is flushed before journal shutdown. Thus it is safe to 1113 + * have inodes get cleaned up after journal shutdown. 1114 + */ 1109 1115 jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal, 1110 1116 &oi->ip_jinode); 1111 1117
+1
fs/ocfs2/journal.c
··· 690 690 691 691 /* Shutdown the kernel journal system */ 692 692 jbd2_journal_destroy(journal->j_journal); 693 + journal->j_journal = NULL; 693 694 694 695 OCFS2_I(inode)->ip_open_count--; 695 696
+5 -1
fs/ocfs2/mmap.c
··· 113 113 * ocfs2_write_begin_nolock(). 114 114 */ 115 115 if (!PageUptodate(page) || page->mapping != inode->i_mapping) { 116 - ret = -EINVAL; 116 + /* 117 + * the page has been umapped in ocfs2_data_downconvert_worker. 118 + * So return 0 here and let VFS retry. 119 + */ 120 + ret = 0; 117 121 goto out; 118 122 } 119 123
+5 -3
fs/ocfs2/namei.c
··· 378 378 } 379 379 380 380 inode = new_inode(dir->i_sb); 381 - if (IS_ERR(inode)) { 382 - status = PTR_ERR(inode); 381 + if (!inode) { 382 + status = -ENOMEM; 383 383 mlog(ML_ERROR, "new_inode failed!\n"); 384 384 goto leave; 385 385 } ··· 491 491 brelse(*new_fe_bh); 492 492 *new_fe_bh = NULL; 493 493 } 494 - if (inode) 494 + if (inode) { 495 + clear_nlink(inode); 495 496 iput(inode); 497 + } 496 498 } 497 499 498 500 mlog_exit(status);
+3
fs/ocfs2/ocfs2.h
··· 473 473 (____gd)->bg_signature); \ 474 474 } while (0) 475 475 476 + #define OCFS2_IS_VALID_XATTR_BLOCK(ptr) \ 477 + (!strcmp((ptr)->xb_signature, OCFS2_XATTR_BLOCK_SIGNATURE)) 478 + 476 479 static inline unsigned long ino_from_blkno(struct super_block *sb, 477 480 u64 blkno) 478 481 {
+9 -8
fs/ocfs2/ocfs2_fs.h
··· 742 742 */ 743 743 struct ocfs2_xattr_entry { 744 744 __le32 xe_name_hash; /* hash value of xattr prefix+suffix. */ 745 - __le16 xe_name_offset; /* byte offset from the 1st etnry in the local 745 + __le16 xe_name_offset; /* byte offset from the 1st entry in the 746 746 local xattr storage(inode, xattr block or 747 747 xattr bucket). */ 748 748 __u8 xe_name_len; /* xattr name len, does't include prefix. */ 749 - __u8 xe_type; /* the low 7 bits indicates the name prefix's 750 - * type and the highest 1 bits indicate whether 749 + __u8 xe_type; /* the low 7 bits indicate the name prefix 750 + * type and the highest bit indicates whether 751 751 * the EA is stored in the local storage. */ 752 752 __le64 xe_value_size; /* real xattr value length. */ 753 753 }; ··· 766 766 xattr. */ 767 767 __le16 xh_name_value_len; /* total length of name/value 768 768 length in this bucket. */ 769 - __le16 xh_num_buckets; /* bucket nums in one extent 770 - record, only valid in the 771 - first bucket. */ 769 + __le16 xh_num_buckets; /* Number of xattr buckets 770 + in this extent record, 771 + only valid in the first 772 + bucket. */ 772 773 __le64 xh_csum; 773 774 struct ocfs2_xattr_entry xh_entries[0]; /* xattr entry list. */ 774 775 }; ··· 777 776 /* 778 777 * On disk structure for xattr value root. 779 778 * 780 - * It is used when one extended attribute's size is larger, and we will save it 781 - * in an outside cluster. It will stored in a b-tree like file content. 779 + * When an xattr's value is large enough, it is stored in an external 780 + * b-tree like file data. The xattr value root points to this structure. 782 781 */ 783 782 struct ocfs2_xattr_value_root { 784 783 /*00*/ __le32 xr_clusters; /* clusters covered by xattr value. */
+205 -169
fs/ocfs2/xattr.c
··· 3 3 * 4 4 * xattr.c 5 5 * 6 - * Copyright (C) 2008 Oracle. All rights reserved. 6 + * Copyright (C) 2004, 2008 Oracle. All rights reserved. 7 7 * 8 8 * CREDITS: 9 - * Lots of code in this file is taken from ext3. 9 + * Lots of code in this file is copy from linux/fs/ext3/xattr.c. 10 + * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> 10 11 * 11 12 * This program is free software; you can redistribute it and/or 12 13 * modify it under the terms of the GNU General Public 13 - * License as published by the Free Software Foundation; either 14 - * version 2 of the License, or (at your option) any later version. 14 + * License version 2 as published by the Free Software Foundation. 15 15 * 16 16 * This program is distributed in the hope that it will be useful, 17 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 19 * General Public License for more details. 20 - * 21 - * You should have received a copy of the GNU General Public 22 - * License along with this program; if not, write to the 23 - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 24 - * Boston, MA 021110-1307, USA. 25 20 */ 26 21 27 22 #include <linux/capability.h> ··· 78 83 NULL 79 84 }; 80 85 81 - static struct xattr_handler *ocfs2_xattr_handler_map[] = { 86 + static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = { 82 87 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler, 83 88 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler, 84 89 }; ··· 111 116 int *block_off, 112 117 int *new_offset); 113 118 119 + static int ocfs2_xattr_block_find(struct inode *inode, 120 + int name_index, 121 + const char *name, 122 + struct ocfs2_xattr_search *xs); 114 123 static int ocfs2_xattr_index_block_find(struct inode *inode, 115 124 struct buffer_head *root_bh, 116 125 int name_index, ··· 135 136 136 137 static int ocfs2_delete_xattr_index_block(struct inode *inode, 137 138 struct buffer_head *xb_bh); 139 + 140 + static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb) 141 + { 142 + return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE; 143 + } 144 + 145 + static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb) 146 + { 147 + return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits); 148 + } 149 + 150 + static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb) 151 + { 152 + u16 len = sb->s_blocksize - 153 + offsetof(struct ocfs2_xattr_header, xh_entries); 154 + 155 + return len / sizeof(struct ocfs2_xattr_entry); 156 + } 138 157 139 158 static inline const char *ocfs2_xattr_prefix(int name_index) 140 159 { ··· 559 542 mlog_errno(ret); 560 543 return ret; 561 544 } 562 - /*Verify the signature of xattr block*/ 563 - if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE, 564 - strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) { 565 - ret = -EFAULT; 566 - goto cleanup; 567 - } 568 545 569 546 xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 547 + if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { 548 + ret = -EIO; 549 + goto cleanup; 550 + } 570 551 571 552 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { 572 553 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header; ··· 764 749 size_t buffer_size, 765 750 struct ocfs2_xattr_search *xs) 766 751 { 767 - struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; 768 - struct buffer_head *blk_bh = NULL; 769 752 struct ocfs2_xattr_block *xb; 770 753 struct ocfs2_xattr_value_root *xv; 771 754 size_t size; 772 755 int ret = -ENODATA, name_offset, name_len, block_off, i; 773 756 774 - if (!di->i_xattr_loc) 775 - return ret; 776 - 777 757 memset(&xs->bucket, 0, sizeof(xs->bucket)); 778 758 779 - ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh); 780 - if (ret < 0) { 759 + ret = ocfs2_xattr_block_find(inode, name_index, name, xs); 760 + if (ret) { 781 761 mlog_errno(ret); 782 - return ret; 783 - } 784 - /*Verify the signature of xattr block*/ 785 - if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE, 786 - strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) { 787 - ret = -EFAULT; 788 762 goto cleanup; 789 763 } 790 764 791 - xs->xattr_bh = blk_bh; 792 - xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 793 - 794 - if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { 795 - xs->header = &xb->xb_attrs.xb_header; 796 - xs->base = (void *)xs->header; 797 - xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size; 798 - xs->here = xs->header->xh_entries; 799 - 800 - ret = ocfs2_xattr_find_entry(name_index, name, xs); 801 - } else 802 - ret = ocfs2_xattr_index_block_find(inode, blk_bh, 803 - name_index, 804 - name, xs); 805 - 806 - if (ret) 765 + if (xs->not_found) { 766 + ret = -ENODATA; 807 767 goto cleanup; 768 + } 769 + 770 + xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; 808 771 size = le64_to_cpu(xs->here->xe_value_size); 809 772 if (buffer) { 810 773 ret = -ERANGE; ··· 821 828 brelse(xs->bucket.bhs[i]); 822 829 memset(&xs->bucket, 0, sizeof(xs->bucket)); 823 830 824 - brelse(blk_bh); 831 + brelse(xs->xattr_bh); 832 + xs->xattr_bh = NULL; 825 833 return ret; 826 834 } 827 835 ··· 831 837 * Copy an extended attribute into the buffer provided. 832 838 * Buffer is NULL to compute the size of buffer required. 833 839 */ 834 - int ocfs2_xattr_get(struct inode *inode, 835 - int name_index, 836 - const char *name, 837 - void *buffer, 838 - size_t buffer_size) 840 + static int ocfs2_xattr_get(struct inode *inode, 841 + int name_index, 842 + const char *name, 843 + void *buffer, 844 + size_t buffer_size) 839 845 { 840 846 int ret; 841 847 struct ocfs2_dinode *di = NULL; ··· 865 871 down_read(&oi->ip_xattr_sem); 866 872 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer, 867 873 buffer_size, &xis); 868 - if (ret == -ENODATA) 874 + if (ret == -ENODATA && di->i_xattr_loc) 869 875 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer, 870 876 buffer_size, &xbs); 871 877 up_read(&oi->ip_xattr_sem); ··· 1223 1229 1224 1230 free = min_offs - ((void *)last - xs->base) - sizeof(__u32); 1225 1231 if (free < 0) 1226 - return -EFAULT; 1232 + return -EIO; 1227 1233 1228 1234 if (!xs->not_found) { 1229 1235 size_t size = 0; ··· 1508 1514 goto out; 1509 1515 } 1510 1516 1511 - /*Verify the signature of xattr block*/ 1512 - if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE, 1513 - strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) { 1514 - ret = -EFAULT; 1517 + xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 1518 + if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { 1519 + ret = -EIO; 1515 1520 goto out; 1516 1521 } 1517 1522 ··· 1520 1527 goto out; 1521 1528 } 1522 1529 1523 - xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 1524 1530 blk = le64_to_cpu(xb->xb_blkno); 1525 1531 bit = le16_to_cpu(xb->xb_suballoc_bit); 1526 1532 bg_blkno = ocfs2_which_suballoc_group(blk, bit); ··· 1763 1771 mlog_errno(ret); 1764 1772 return ret; 1765 1773 } 1766 - /*Verify the signature of xattr block*/ 1767 - if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE, 1768 - strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) { 1769 - ret = -EFAULT; 1770 - goto cleanup; 1774 + 1775 + xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 1776 + if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { 1777 + ret = -EIO; 1778 + goto cleanup; 1771 1779 } 1772 1780 1773 1781 xs->xattr_bh = blk_bh; 1774 - xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 1775 1782 1776 1783 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { 1777 1784 xs->header = &xb->xb_attrs.xb_header; ··· 1793 1802 cleanup: 1794 1803 brelse(blk_bh); 1795 1804 1796 - return ret; 1797 - } 1798 - 1799 - /* 1800 - * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree 1801 - * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header 1802 - * re-initialized. 1803 - */ 1804 - static int ocfs2_restore_xattr_block(struct inode *inode, 1805 - struct ocfs2_xattr_search *xs) 1806 - { 1807 - int ret; 1808 - handle_t *handle; 1809 - struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1810 - struct ocfs2_xattr_block *xb = 1811 - (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; 1812 - struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list; 1813 - u16 xb_flags = le16_to_cpu(xb->xb_flags); 1814 - 1815 - BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) || 1816 - le16_to_cpu(el->l_next_free_rec) != 0); 1817 - 1818 - handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS); 1819 - if (IS_ERR(handle)) { 1820 - ret = PTR_ERR(handle); 1821 - handle = NULL; 1822 - goto out; 1823 - } 1824 - 1825 - ret = ocfs2_journal_access(handle, inode, xs->xattr_bh, 1826 - OCFS2_JOURNAL_ACCESS_WRITE); 1827 - if (ret < 0) { 1828 - mlog_errno(ret); 1829 - goto out_commit; 1830 - } 1831 - 1832 - memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize - 1833 - offsetof(struct ocfs2_xattr_block, xb_attrs)); 1834 - 1835 - xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED); 1836 - 1837 - ocfs2_journal_dirty(handle, xs->xattr_bh); 1838 - 1839 - out_commit: 1840 - ocfs2_commit_trans(osb, handle); 1841 - out: 1842 1805 return ret; 1843 1806 } 1844 1807 ··· 1906 1961 } 1907 1962 1908 1963 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs); 1909 - if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0) 1910 - ret = ocfs2_restore_xattr_block(inode, xs); 1911 1964 1912 1965 end: 1913 1966 ··· 2341 2398 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash); 2342 2399 2343 2400 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash " 2344 - "in the rec is %u\n", num_clusters, p_blkno, first_hash); 2401 + "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno, 2402 + first_hash); 2345 2403 2346 2404 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash, 2347 2405 p_blkno, first_hash, num_clusters, xs); ··· 2366 2422 memset(&bucket, 0, sizeof(bucket)); 2367 2423 2368 2424 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n", 2369 - clusters, blkno); 2425 + clusters, (unsigned long long)blkno); 2370 2426 2371 2427 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) { 2372 2428 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, ··· 2384 2440 if (i == 0) 2385 2441 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets); 2386 2442 2387 - mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno, 2443 + mlog(0, "iterating xattr bucket %llu, first hash %u\n", 2444 + (unsigned long long)blkno, 2388 2445 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash)); 2389 2446 if (func) { 2390 2447 ret = func(inode, &bucket, para); ··· 2721 2776 */ 2722 2777 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); 2723 2778 2724 - mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno); 2779 + mlog(0, "allocate 1 cluster from %llu to xattr block\n", 2780 + (unsigned long long)blkno); 2725 2781 2726 2782 xh_bh = sb_getblk(inode->i_sb, blkno); 2727 2783 if (!xh_bh) { ··· 2764 2818 if (data_bh) 2765 2819 ocfs2_journal_dirty(handle, data_bh); 2766 2820 2767 - ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh); 2821 + ret = ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh); 2822 + if (ret) { 2823 + mlog_errno(ret); 2824 + goto out_commit; 2825 + } 2768 2826 2769 2827 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */ 2770 2828 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize - ··· 2891 2941 2892 2942 mlog(0, "adjust xattr bucket in %llu, count = %u, " 2893 2943 "xh_free_start = %u, xh_name_value_len = %u.\n", 2894 - blkno, le16_to_cpu(xh->xh_count), xh_free_start, 2895 - le16_to_cpu(xh->xh_name_value_len)); 2944 + (unsigned long long)blkno, le16_to_cpu(xh->xh_count), 2945 + xh_free_start, le16_to_cpu(xh->xh_name_value_len)); 2896 2946 2897 2947 /* 2898 2948 * sort all the entries by their offset. ··· 3008 3058 prev_blkno += (num_clusters - 1) * bpc + bpc / 2; 3009 3059 3010 3060 mlog(0, "move half of xattrs in cluster %llu to %llu\n", 3011 - prev_blkno, new_blkno); 3061 + (unsigned long long)prev_blkno, (unsigned long long)new_blkno); 3012 3062 3013 3063 /* 3014 3064 * We need to update the 1st half of the new cluster and ··· 3118 3168 } 3119 3169 3120 3170 /* 3121 - * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk). 3122 - * first_hash will record the 1st hash of the new bucket. 3171 + * Find the suitable pos when we divide a bucket into 2. 3172 + * We have to make sure the xattrs with the same hash value exist 3173 + * in the same bucket. 3174 + * 3175 + * If this ocfs2_xattr_header covers more than one hash value, find a 3176 + * place where the hash value changes. Try to find the most even split. 3177 + * The most common case is that all entries have different hash values, 3178 + * and the first check we make will find a place to split. 3123 3179 */ 3124 - static int ocfs2_half_xattr_bucket(struct inode *inode, 3125 - handle_t *handle, 3126 - u64 blk, 3127 - u64 new_blk, 3128 - u32 *first_hash, 3129 - int new_bucket_head) 3180 + static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh) 3181 + { 3182 + struct ocfs2_xattr_entry *entries = xh->xh_entries; 3183 + int count = le16_to_cpu(xh->xh_count); 3184 + int delta, middle = count / 2; 3185 + 3186 + /* 3187 + * We start at the middle. Each step gets farther away in both 3188 + * directions. We therefore hit the change in hash value 3189 + * nearest to the middle. Note that this loop does not execute for 3190 + * count < 2. 3191 + */ 3192 + for (delta = 0; delta < middle; delta++) { 3193 + /* Let's check delta earlier than middle */ 3194 + if (cmp_xe(&entries[middle - delta - 1], 3195 + &entries[middle - delta])) 3196 + return middle - delta; 3197 + 3198 + /* For even counts, don't walk off the end */ 3199 + if ((middle + delta + 1) == count) 3200 + continue; 3201 + 3202 + /* Now try delta past middle */ 3203 + if (cmp_xe(&entries[middle + delta], 3204 + &entries[middle + delta + 1])) 3205 + return middle + delta + 1; 3206 + } 3207 + 3208 + /* Every entry had the same hash */ 3209 + return count; 3210 + } 3211 + 3212 + /* 3213 + * Move some xattrs in old bucket(blk) to new bucket(new_blk). 3214 + * first_hash will record the 1st hash of the new bucket. 3215 + * 3216 + * Normally half of the xattrs will be moved. But we have to make 3217 + * sure that the xattrs with the same hash value are stored in the 3218 + * same bucket. If all the xattrs in this bucket have the same hash 3219 + * value, the new bucket will be initialized as an empty one and the 3220 + * first_hash will be initialized as (hash_value+1). 3221 + */ 3222 + static int ocfs2_divide_xattr_bucket(struct inode *inode, 3223 + handle_t *handle, 3224 + u64 blk, 3225 + u64 new_blk, 3226 + u32 *first_hash, 3227 + int new_bucket_head) 3130 3228 { 3131 3229 int ret, i; 3132 - u16 count, start, len, name_value_len, xe_len, name_offset; 3230 + int count, start, len, name_value_len = 0, xe_len, name_offset = 0; 3133 3231 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); 3134 3232 struct buffer_head **s_bhs, **t_bhs = NULL; 3135 3233 struct ocfs2_xattr_header *xh; 3136 3234 struct ocfs2_xattr_entry *xe; 3137 3235 int blocksize = inode->i_sb->s_blocksize; 3138 3236 3139 - mlog(0, "move half of xattrs from bucket %llu to %llu\n", 3140 - blk, new_blk); 3237 + mlog(0, "move some of xattrs from bucket %llu to %llu\n", 3238 + (unsigned long long)blk, (unsigned long long)new_blk); 3141 3239 3142 3240 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS); 3143 3241 if (!s_bhs) ··· 3218 3220 3219 3221 for (i = 0; i < blk_per_bucket; i++) { 3220 3222 ret = ocfs2_journal_access(handle, inode, t_bhs[i], 3221 - OCFS2_JOURNAL_ACCESS_CREATE); 3223 + new_bucket_head ? 3224 + OCFS2_JOURNAL_ACCESS_CREATE : 3225 + OCFS2_JOURNAL_ACCESS_WRITE); 3222 3226 if (ret) { 3223 3227 mlog_errno(ret); 3224 3228 goto out; 3225 3229 } 3230 + } 3231 + 3232 + xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data; 3233 + count = le16_to_cpu(xh->xh_count); 3234 + start = ocfs2_xattr_find_divide_pos(xh); 3235 + 3236 + if (start == count) { 3237 + xe = &xh->xh_entries[start-1]; 3238 + 3239 + /* 3240 + * initialized a new empty bucket here. 3241 + * The hash value is set as one larger than 3242 + * that of the last entry in the previous bucket. 3243 + */ 3244 + for (i = 0; i < blk_per_bucket; i++) 3245 + memset(t_bhs[i]->b_data, 0, blocksize); 3246 + 3247 + xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data; 3248 + xh->xh_free_start = cpu_to_le16(blocksize); 3249 + xh->xh_entries[0].xe_name_hash = xe->xe_name_hash; 3250 + le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1); 3251 + 3252 + goto set_num_buckets; 3226 3253 } 3227 3254 3228 3255 /* copy the whole bucket to the new first. */ ··· 3256 3233 3257 3234 /* update the new bucket. */ 3258 3235 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data; 3259 - count = le16_to_cpu(xh->xh_count); 3260 - start = count / 2; 3261 3236 3262 3237 /* 3263 3238 * Calculate the total name/value len and xh_free_start for ··· 3312 3291 xh->xh_free_start = xe->xe_name_offset; 3313 3292 } 3314 3293 3294 + set_num_buckets: 3315 3295 /* set xh->xh_num_buckets for the new xh. */ 3316 3296 if (new_bucket_head) 3317 3297 xh->xh_num_buckets = cpu_to_le16(1); ··· 3330 3308 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); 3331 3309 3332 3310 /* 3333 - * Now only update the 1st block of the old bucket. 3334 - * Please note that the entry has been sorted already above. 3311 + * Now only update the 1st block of the old bucket. If we 3312 + * just added a new empty bucket, there is no need to modify 3313 + * it. 3335 3314 */ 3315 + if (start == count) 3316 + goto out; 3317 + 3336 3318 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data; 3337 3319 memset(&xh->xh_entries[start], 0, 3338 3320 sizeof(struct ocfs2_xattr_entry) * (count - start)); ··· 3384 3358 BUG_ON(s_blkno == t_blkno); 3385 3359 3386 3360 mlog(0, "cp bucket %llu to %llu, target is %d\n", 3387 - s_blkno, t_blkno, t_is_new); 3361 + (unsigned long long)s_blkno, (unsigned long long)t_blkno, 3362 + t_is_new); 3388 3363 3389 3364 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket, 3390 3365 GFP_NOFS); ··· 3409 3382 3410 3383 for (i = 0; i < blk_per_bucket; i++) { 3411 3384 ret = ocfs2_journal_access(handle, inode, t_bhs[i], 3385 + t_is_new ? 3386 + OCFS2_JOURNAL_ACCESS_CREATE : 3412 3387 OCFS2_JOURNAL_ACCESS_WRITE); 3413 3388 if (ret) 3414 3389 goto out; ··· 3457 3428 struct ocfs2_xattr_header *xh; 3458 3429 u64 to_blk_start = to_blk; 3459 3430 3460 - mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk); 3431 + mlog(0, "cp xattrs from cluster %llu to %llu\n", 3432 + (unsigned long long)src_blk, (unsigned long long)to_blk); 3461 3433 3462 3434 /* 3463 3435 * We need to update the new cluster and 1 more for the update of ··· 3523 3493 } 3524 3494 3525 3495 /* 3526 - * Move half of the xattrs in this cluster to the new cluster. 3496 + * Move some xattrs in this cluster to the new cluster. 3527 3497 * This function should only be called when bucket size == cluster size. 3528 3498 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead. 3529 3499 */ 3530 - static int ocfs2_half_xattr_cluster(struct inode *inode, 3531 - handle_t *handle, 3532 - u64 prev_blk, 3533 - u64 new_blk, 3534 - u32 *first_hash) 3500 + static int ocfs2_divide_xattr_cluster(struct inode *inode, 3501 + handle_t *handle, 3502 + u64 prev_blk, 3503 + u64 new_blk, 3504 + u32 *first_hash) 3535 3505 { 3536 3506 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); 3537 3507 int ret, credits = 2 * blk_per_bucket; ··· 3545 3515 } 3546 3516 3547 3517 /* Move half of the xattr in start_blk to the next bucket. */ 3548 - return ocfs2_half_xattr_bucket(inode, handle, prev_blk, 3549 - new_blk, first_hash, 1); 3518 + return ocfs2_divide_xattr_bucket(inode, handle, prev_blk, 3519 + new_blk, first_hash, 1); 3550 3520 } 3551 3521 3552 3522 /* ··· 3589 3559 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); 3590 3560 3591 3561 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n", 3592 - prev_blk, prev_clusters, new_blk); 3562 + (unsigned long long)prev_blk, prev_clusters, 3563 + (unsigned long long)new_blk); 3593 3564 3594 3565 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) 3595 3566 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode, ··· 3609 3578 last_blk, new_blk, 3610 3579 v_start); 3611 3580 else { 3612 - ret = ocfs2_half_xattr_cluster(inode, handle, 3613 - last_blk, new_blk, 3614 - v_start); 3581 + ret = ocfs2_divide_xattr_cluster(inode, handle, 3582 + last_blk, new_blk, 3583 + v_start); 3615 3584 3616 3585 if ((*header_bh)->b_blocknr == last_blk && extend) 3617 3586 *extend = 0; ··· 3660 3629 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, " 3661 3630 "previous xattr blkno = %llu\n", 3662 3631 (unsigned long long)OCFS2_I(inode)->ip_blkno, 3663 - prev_cpos, prev_blkno); 3632 + prev_cpos, (unsigned long long)prev_blkno); 3664 3633 3665 3634 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh); 3666 3635 ··· 3747 3716 } 3748 3717 } 3749 3718 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n", 3750 - num_bits, block, v_start); 3719 + num_bits, (unsigned long long)block, v_start); 3751 3720 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block, 3752 3721 num_bits, 0, meta_ac); 3753 3722 if (ret < 0) { ··· 3792 3761 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets); 3793 3762 3794 3763 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting " 3795 - "from %llu, len = %u\n", start_blk, 3764 + "from %llu, len = %u\n", (unsigned long long)start_blk, 3796 3765 (unsigned long long)first_bh->b_blocknr, num_clusters); 3797 3766 3798 3767 BUG_ON(bucket >= num_buckets); ··· 3828 3797 } 3829 3798 3830 3799 /* Move half of the xattr in start_blk to the next bucket. */ 3831 - ret = ocfs2_half_xattr_bucket(inode, handle, start_blk, 3832 - start_blk + blk_per_bucket, NULL, 0); 3800 + ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk, 3801 + start_blk + blk_per_bucket, NULL, 0); 3833 3802 3834 3803 le16_add_cpu(&first_xh->xh_num_buckets, 1); 3835 3804 ocfs2_journal_dirty(handle, first_bh); ··· 4177 4146 handle_t *handle = NULL; 4178 4147 4179 4148 handle = ocfs2_start_trans(osb, 1); 4180 - if (handle == NULL) { 4149 + if (IS_ERR(handle)) { 4181 4150 ret = -ENOMEM; 4182 4151 mlog_errno(ret); 4183 4152 goto out; ··· 4344 4313 } 4345 4314 4346 4315 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); 4347 - if (handle == NULL) { 4316 + if (IS_ERR(handle)) { 4348 4317 ret = -ENOMEM; 4349 4318 mlog_errno(ret); 4350 4319 goto out; ··· 4520 4489 return ret; 4521 4490 } 4522 4491 4523 - /* check whether the xattr bucket is filled up with the same hash value. */ 4492 + /* 4493 + * check whether the xattr bucket is filled up with the same hash value. 4494 + * If we want to insert the xattr with the same hash, return -ENOSPC. 4495 + * If we want to insert a xattr with different hash value, go ahead 4496 + * and ocfs2_divide_xattr_bucket will handle this. 4497 + */ 4524 4498 static int ocfs2_check_xattr_bucket_collision(struct inode *inode, 4525 - struct ocfs2_xattr_bucket *bucket) 4499 + struct ocfs2_xattr_bucket *bucket, 4500 + const char *name) 4526 4501 { 4527 4502 struct ocfs2_xattr_header *xh = bucket->xh; 4503 + u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); 4504 + 4505 + if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash)) 4506 + return 0; 4528 4507 4529 4508 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash == 4530 4509 xh->xh_entries[0].xe_name_hash) { ··· 4657 4616 * one bucket's worth, so check it here whether we need to 4658 4617 * add a new bucket for the insert. 4659 4618 */ 4660 - ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket); 4619 + ret = ocfs2_check_xattr_bucket_collision(inode, 4620 + &xs->bucket, 4621 + xi->name); 4661 4622 if (ret) { 4662 4623 mlog_errno(ret); 4663 4624 goto out; ··· 4770 4727 /* 4771 4728 * 'trusted' attributes support 4772 4729 */ 4773 - 4774 - #define XATTR_TRUSTED_PREFIX "trusted." 4775 - 4776 4730 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list, 4777 4731 size_t list_size, const char *name, 4778 4732 size_t name_len) 4779 4733 { 4780 - const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1; 4734 + const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN; 4781 4735 const size_t total_len = prefix_len + name_len + 1; 4782 4736 4783 4737 if (list && total_len <= list_size) { ··· 4811 4771 .set = ocfs2_xattr_trusted_set, 4812 4772 }; 4813 4773 4814 - 4815 4774 /* 4816 4775 * 'user' attributes support 4817 4776 */ 4818 - 4819 - #define XATTR_USER_PREFIX "user." 4820 - 4821 4777 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list, 4822 4778 size_t list_size, const char *name, 4823 4779 size_t name_len) 4824 4780 { 4825 - const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1; 4781 + const size_t prefix_len = XATTR_USER_PREFIX_LEN; 4826 4782 const size_t total_len = prefix_len + name_len + 1; 4827 4783 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 4828 4784
+6 -32
fs/ocfs2/xattr.h
··· 3 3 * 4 4 * xattr.h 5 5 * 6 - * Function prototypes 7 - * 8 - * Copyright (C) 2008 Oracle. All rights reserved. 6 + * Copyright (C) 2004, 2008 Oracle. All rights reserved. 9 7 * 10 8 * This program is free software; you can redistribute it and/or 11 9 * modify it under the terms of the GNU General Public 12 - * License as published by the Free Software Foundation; either 13 - * version 2 of the License, or (at your option) any later version. 10 + * License version 2 as published by the Free Software Foundation. 14 11 * 15 12 * This program is distributed in the hope that it will be useful, 16 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 15 * General Public License for more details. 19 - * 20 - * You should have received a copy of the GNU General Public 21 - * License along with this program; if not, write to the 22 - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 - * Boston, MA 021110-1307, USA. 24 16 */ 25 17 26 18 #ifndef OCFS2_XATTR_H ··· 32 40 33 41 extern struct xattr_handler ocfs2_xattr_user_handler; 34 42 extern struct xattr_handler ocfs2_xattr_trusted_handler; 35 - 36 - extern ssize_t ocfs2_listxattr(struct dentry *, char *, size_t); 37 - extern int ocfs2_xattr_get(struct inode *, int, const char *, void *, size_t); 38 - extern int ocfs2_xattr_set(struct inode *, int, const char *, const void *, 39 - size_t, int); 40 - extern int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh); 41 43 extern struct xattr_handler *ocfs2_xattr_handlers[]; 42 44 43 - static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb) 44 - { 45 - return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE; 46 - } 45 + ssize_t ocfs2_listxattr(struct dentry *, char *, size_t); 46 + int ocfs2_xattr_set(struct inode *, int, const char *, const void *, 47 + size_t, int); 48 + int ocfs2_xattr_remove(struct inode *, struct buffer_head *); 47 49 48 - static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb) 49 - { 50 - return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits); 51 - } 52 - 53 - static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb) 54 - { 55 - u16 len = sb->s_blocksize - 56 - offsetof(struct ocfs2_xattr_header, xh_entries); 57 - 58 - return len / sizeof(struct ocfs2_xattr_entry); 59 - } 60 50 #endif /* OCFS2_XATTR_H */