22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
33 #include <linux/falloc.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
85 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
86 #define OCFS2_XATTR_INLINE_SIZE 80
87 #define OCFS2_XATTR_HEADER_GAP 4
88 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
89 - sizeof(struct ocfs2_xattr_header) \
90 - OCFS2_XATTR_HEADER_GAP)
91 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
92 - sizeof(struct ocfs2_xattr_block) \
93 - sizeof(struct ocfs2_xattr_header) \
94 - OCFS2_XATTR_HEADER_GAP)
246 ocfs2_xattr_is_local(xe));
251 static int ocfs2_xattr_bucket_get_name_value(
struct super_block *
sb,
257 static int ocfs2_xattr_block_find(
struct inode *
inode,
261 static int ocfs2_xattr_index_block_find(
struct inode *
inode,
262 struct buffer_head *root_bh,
267 static int ocfs2_xattr_tree_list_index_block(
struct inode *
inode,
268 struct buffer_head *blk_bh,
272 static int ocfs2_xattr_create_index_block(
struct inode *
inode,
276 static int ocfs2_xattr_set_entry_index_block(
struct inode *
inode,
282 struct buffer_head *root_bh,
284 static int ocfs2_iterate_xattr_index_block(
struct inode *inode,
285 struct buffer_head *root_bh,
288 static int ocfs2_delete_xattr_in_bucket(
struct inode *inode,
291 static int ocfs2_rm_xattr_cluster(
struct inode *inode,
292 struct buffer_head *root_bh,
298 static int ocfs2_mv_xattr_buckets(
struct inode *inode, handle_t *
handle,
300 unsigned int start_bucket,
302 static int ocfs2_prepare_refcount_xattr(
struct inode *inode,
310 static int ocfs2_get_xattr_tree_value_root(
struct super_block *
sb,
314 struct buffer_head **bh);
316 static inline u16 ocfs2_xattr_buckets_per_cluster(
struct ocfs2_super *osb)
326 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
327 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
328 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
333 int blks = ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
350 for (i = 0; i < bucket->
bu_blocks; i++) {
351 brelse(bucket->
bu_bhs[i]);
359 ocfs2_xattr_bucket_relse(bucket);
376 for (i = 0; i < bucket->
bu_blocks; i++) {
392 ocfs2_xattr_bucket_relse(bucket);
417 ocfs2_xattr_bucket_relse(bucket);
421 static int ocfs2_xattr_bucket_journal_access(handle_t *
handle,
427 for (i = 0; i < bucket->
bu_blocks; i++) {
440 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
459 int blocksize = src->
bu_inode->i_sb->s_blocksize;
470 static int ocfs2_validate_xattr_block(
struct super_block *
sb,
471 struct buffer_head *bh)
477 trace_ocfs2_validate_xattr_block((
unsigned long long)bh->b_blocknr);
479 BUG_ON(!buffer_uptodate(bh));
496 "Extended attribute block #%llu has bad "
498 (
unsigned long long)bh->b_blocknr, 7,
505 "Extended attribute block #%llu has an "
506 "invalid xb_blkno of %llu",
507 (
unsigned long long)bh->b_blocknr,
514 "Extended attribute block #%llu has an invalid "
515 "xb_fs_generation of #%u",
516 (
unsigned long long)bh->b_blocknr,
524 static int ocfs2_read_xattr_block(
struct inode *inode,
u64 xb_blkno,
525 struct buffer_head **bh)
528 struct buffer_head *
tmp = *bh;
530 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
531 ocfs2_validate_xattr_block);
540 static inline const char *ocfs2_xattr_prefix(
int name_index)
545 handler = ocfs2_xattr_handler_map[name_index];
550 static u32 ocfs2_xattr_name_hash(
struct inode *inode,
568 static int ocfs2_xattr_entry_real_size(
int name_len,
size_t value_len)
570 return namevalue_size(name_len, value_len) +
576 return namevalue_size_xi(xi) +
582 return namevalue_size_xe(xe) +
614 int new_clusters = ocfs2_clusters_for_bytes(dir->
i_sb,
617 *xattr_credits += ocfs2_clusters_to_blocks(dir->
i_sb,
619 *want_clusters += new_clusters;
625 struct buffer_head *dir_bh,
634 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
637 s_size = ocfs2_xattr_entry_real_size(
strlen(si->
name),
645 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
648 }
else if (acl_len != 0 && acl_len != -
ENODATA) {
654 if (!(s_size + a_size))
670 (
S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
672 *want_meta = *want_meta + 1;
679 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->
i_sb);
687 new_clusters = ocfs2_clusters_for_bytes(dir->
i_sb,
689 *xattr_credits += ocfs2_clusters_to_blocks(dir->
i_sb,
691 *want_clusters += new_clusters;
696 new_clusters = (
S_ISDIR(mode) ? 2 : 1) *
697 ocfs2_clusters_for_bytes(dir->
i_sb, acl_len);
698 *xattr_credits += ocfs2_clusters_to_blocks(dir->
i_sb,
700 *want_clusters += new_clusters;
706 static int ocfs2_xattr_extend_allocation(
struct inode *inode,
712 handle_t *handle = ctxt->
handle;
719 while (clusters_to_add) {
720 trace_ocfs2_xattr_extend_allocation(clusters_to_add);
738 if ((status < 0) && (status != -
EAGAIN)) {
756 credits = ocfs2_calc_extend_credits(inode->
i_sb,
771 static int __ocfs2_remove_xattr_range(
struct inode *inode,
778 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->
i_sb, phys_cpos);
779 handle_t *handle = ctxt->
handle;
798 le32_add_cpu(&vb->
vb_xv->xr_clusters, -len);
803 ocfs2_blocks_to_clusters(inode->
i_sb,
816 static int ocfs2_xattr_shrink_size(
struct inode *inode,
824 u32 trunc_len, cpos, phys_cpos, alloc_size;
827 if (old_clusters <= new_clusters)
831 trunc_len = old_clusters - new_clusters;
835 &vb->
vb_xv->xr_list, &ext_flags);
841 if (alloc_size > trunc_len)
842 alloc_size = trunc_len;
844 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
845 phys_cpos, alloc_size,
852 block = ocfs2_clusters_to_blocks(inode->
i_sb, phys_cpos);
856 trunc_len -= alloc_size;
863 static int ocfs2_xattr_value_truncate(
struct inode *inode,
869 u32 new_clusters = ocfs2_clusters_for_bytes(inode->
i_sb, len);
872 if (new_clusters == old_clusters)
875 if (new_clusters > old_clusters)
876 ret = ocfs2_xattr_extend_allocation(inode,
877 new_clusters - old_clusters,
880 ret = ocfs2_xattr_shrink_size(inode,
881 old_clusters, new_clusters,
887 static int ocfs2_xattr_list_entry(
char *
buffer,
size_t size,
889 const char *name,
int name_len)
892 int prefix_len =
strlen(prefix);
893 int total_len = prefix_len + name_len + 1;
904 memcpy(p, prefix, prefix_len);
905 memcpy(p + prefix_len, name, name_len);
911 static int ocfs2_xattr_list_entries(
struct inode *inode,
913 char *buffer,
size_t buffer_size)
921 type = ocfs2_xattr_get_type(entry);
922 prefix = ocfs2_xattr_prefix(type);
925 name = (
const char *)header +
928 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
929 &result, prefix, name,
946 ((
void *)di + inode->
i_sb->s_blocksize -
950 if (!ocfs2_xattr_is_local(&xh->
xh_entries[i]))
956 static int ocfs2_xattr_ibody_list(
struct inode *inode,
969 ((
void *)di + inode->
i_sb->s_blocksize -
972 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
977 static int ocfs2_xattr_block_list(
struct inode *inode,
982 struct buffer_head *blk_bh =
NULL;
999 ret = ocfs2_xattr_list_entries(inode, header,
1000 buffer, buffer_size);
1002 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1003 buffer, buffer_size);
1014 int ret = 0, i_ret = 0, b_ret = 0;
1015 struct buffer_head *di_bh =
NULL;
1034 i_ret = ocfs2_xattr_ibody_list(dentry->
d_inode, di, buffer, size);
1042 b_ret = ocfs2_xattr_block_list(dentry->
d_inode, di,
1052 return i_ret + b_ret;
1055 static int ocfs2_xattr_find_entry(
int name_index,
1069 cmp = name_index - ocfs2_xattr_get_type(entry);
1085 static int ocfs2_xattr_get_value_outside(
struct inode *inode,
1090 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1093 size_t cplen, blocksize;
1094 struct buffer_head *bh =
NULL;
1099 bpc = ocfs2_clusters_to_blocks(inode->
i_sb, 1);
1100 blocksize = inode->
i_sb->s_blocksize;
1103 while (cpos < clusters) {
1105 &num_clusters, el,
NULL);
1111 blkno = ocfs2_clusters_to_blocks(inode->
i_sb, p_cluster);
1113 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1114 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1121 cplen = len >= blocksize ? blocksize : len;
1122 memcpy(buffer, bh->b_data, cplen);
1131 cpos += num_clusters;
1137 static int ocfs2_xattr_ibody_get(
struct inode *inode,
1153 xs->
end = (
void *)di + inode->
i_sb->s_blocksize;
1159 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1164 if (size > buffer_size)
1166 if (ocfs2_xattr_is_local(xs->
here)) {
1173 xs->
here->xe_name_offset) +
1175 ret = ocfs2_xattr_get_value_outside(inode, xv,
1187 static int ocfs2_xattr_block_get(
struct inode *inode,
1200 xs->
bucket = ocfs2_xattr_bucket_new(inode);
1207 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1222 if (size > buffer_size)
1230 ret = ocfs2_xattr_bucket_get_name_value(inode->
i_sb,
1237 if (ocfs2_xattr_is_local(xs->
here)) {
1239 name_offset + name_len, size);
1242 (xs->
base + name_offset + name_len);
1243 ret = ocfs2_xattr_get_value_outside(inode, xv,
1253 ocfs2_xattr_bucket_free(xs->
bucket);
1261 struct buffer_head *di_bh,
1286 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1289 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1300 static int ocfs2_xattr_get(
struct inode *inode,
1307 struct buffer_head *di_bh =
NULL;
1314 down_read(&OCFS2_I(inode)->ip_xattr_sem);
1316 name, buffer, buffer_size);
1317 up_read(&OCFS2_I(inode)->ip_xattr_sem);
1326 static int __ocfs2_xattr_set_value_outside(
struct inode *inode,
1332 int ret = 0,
i, cp_len;
1333 u16 blocksize = inode->
i_sb->s_blocksize;
1334 u32 p_cluster, num_clusters;
1335 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->
i_sb, 1);
1336 u32 clusters = ocfs2_clusters_for_bytes(inode->
i_sb, value_len);
1338 struct buffer_head *bh =
NULL;
1344 while (cpos < clusters) {
1353 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1355 blkno = ocfs2_clusters_to_blocks(inode->
i_sb, p_cluster);
1357 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1358 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1374 cp_len = value_len > blocksize ? blocksize :
value_len;
1375 memcpy(bh->b_data, value, cp_len);
1376 value_len -= cp_len;
1378 if (cp_len < blocksize)
1379 memset(bh->b_data + cp_len, 0,
1380 blocksize - cp_len);
1393 cpos += num_clusters;
1401 static int ocfs2_xa_check_space_helper(
int needed_space,
int free_start,
1409 free_space = free_start -
1415 if (free_space < needed_space)
1421 static int ocfs2_xa_journal_access(handle_t *handle,
struct ocfs2_xa_loc *
loc,
1424 return loc->
xl_ops->xlo_journal_access(handle, loc, type);
1427 static void ocfs2_xa_journal_dirty(handle_t *handle,
struct ocfs2_xa_loc *loc)
1429 loc->
xl_ops->xlo_journal_dirty(handle, loc);
1436 return loc->
xl_ops->xlo_offset_pointer(loc, offset);
1444 static void ocfs2_xa_wipe_namevalue(
struct ocfs2_xa_loc *loc)
1446 loc->
xl_ops->xlo_wipe_namevalue(loc);
1453 static int ocfs2_xa_get_free_start(
struct ocfs2_xa_loc *loc)
1455 return loc->
xl_ops->xlo_get_free_start(loc);
1459 static int ocfs2_xa_can_reuse_entry(
struct ocfs2_xa_loc *loc,
1462 return loc->
xl_ops->xlo_can_reuse(loc, xi);
1466 static int ocfs2_xa_check_space(
struct ocfs2_xa_loc *loc,
1469 return loc->
xl_ops->xlo_check_space(loc, xi);
1472 static void ocfs2_xa_add_entry(
struct ocfs2_xa_loc *loc,
u32 name_hash)
1474 loc->
xl_ops->xlo_add_entry(loc, name_hash);
1484 static void ocfs2_xa_add_namevalue(
struct ocfs2_xa_loc *loc,
1487 int size = namevalue_size_xi(xi);
1491 loc->
xl_ops->xlo_add_namevalue(loc, size);
1495 ocfs2_xattr_set_local(loc->
xl_entry,
1499 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1500 memset(nameval_buf, 0, size);
1504 static void ocfs2_xa_fill_value_buf(
struct ocfs2_xa_loc *loc,
1515 loc->
xl_ops->xlo_fill_value_buf(loc, vb);
1522 static int ocfs2_xa_block_journal_access(handle_t *handle,
1528 if (loc->
xl_size == (bh->b_size -
1537 static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1545 static void *ocfs2_xa_block_offset_pointer(
struct ocfs2_xa_loc *loc,
1551 static int ocfs2_xa_block_can_reuse(
struct ocfs2_xa_loc *loc,
1558 return namevalue_size_xe(loc->
xl_entry) ==
1559 namevalue_size_xi(xi);
1562 static int ocfs2_xa_block_get_free_start(
struct ocfs2_xa_loc *loc)
1568 for (i = 0; i <
count; i++) {
1570 if (offset < free_start)
1577 static int ocfs2_xa_block_check_space(
struct ocfs2_xa_loc *loc,
1581 int free_start = ocfs2_xa_get_free_start(loc);
1582 int needed_space = ocfs2_xi_entry_usage(xi);
1591 if (ocfs2_xa_can_reuse_entry(loc, xi))
1594 needed_space -= ocfs2_xe_entry_usage(loc->
xl_entry);
1596 if (needed_space < 0)
1598 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1605 static void ocfs2_xa_block_wipe_namevalue(
struct ocfs2_xa_loc *loc)
1608 int namevalue_offset, first_namevalue_offset, namevalue_size;
1614 namevalue_size = namevalue_size_xe(entry);
1615 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1618 memmove((
char *)xh + first_namevalue_offset + namevalue_size,
1619 (
char *)xh + first_namevalue_offset,
1620 namevalue_offset - first_namevalue_offset);
1621 memset((
char *)xh + first_namevalue_offset, 0, namevalue_size);
1624 for (i = 0; i <
count; i++) {
1626 if (offset <= namevalue_offset)
1627 le16_add_cpu(&xh->
xh_entries[i].xe_name_offset,
1637 static void ocfs2_xa_block_add_entry(
struct ocfs2_xa_loc *loc,
u32 name_hash)
1641 le16_add_cpu(&loc->
xl_header->xh_count, 1);
1645 static void ocfs2_xa_block_add_namevalue(
struct ocfs2_xa_loc *loc,
int size)
1647 int free_start = ocfs2_xa_get_free_start(loc);
1652 static void ocfs2_xa_block_fill_value_buf(
struct ocfs2_xa_loc *loc,
1657 if (loc->
xl_size == (bh->b_size -
1671 .xlo_journal_access = ocfs2_xa_block_journal_access,
1672 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
1673 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1674 .xlo_check_space = ocfs2_xa_block_check_space,
1675 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1676 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1677 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1678 .xlo_add_entry = ocfs2_xa_block_add_entry,
1679 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1680 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1683 static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1688 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1691 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1696 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1699 static void *ocfs2_xa_bucket_offset_pointer(
struct ocfs2_xa_loc *loc,
1706 block = offset >> loc->
xl_inode->i_sb->s_blocksize_bits;
1707 block_offset = offset % loc->
xl_inode->i_sb->s_blocksize;
1712 static int ocfs2_xa_bucket_can_reuse(
struct ocfs2_xa_loc *loc,
1715 return namevalue_size_xe(loc->
xl_entry) >=
1716 namevalue_size_xi(xi);
1719 static int ocfs2_xa_bucket_get_free_start(
struct ocfs2_xa_loc *loc)
1725 static int ocfs2_bucket_align_free_start(
struct super_block *sb,
1726 int free_start,
int size)
1739 static int ocfs2_xa_bucket_check_space(
struct ocfs2_xa_loc *loc,
1744 int free_start = ocfs2_xa_get_free_start(loc);
1745 int needed_space = ocfs2_xi_entry_usage(xi);
1746 int size = namevalue_size_xi(xi);
1757 if (ocfs2_xa_can_reuse_entry(loc, xi))
1762 BUG_ON(needed_space < 0);
1764 if (free_start < size) {
1774 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1778 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1781 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1784 static void ocfs2_xa_bucket_wipe_namevalue(
struct ocfs2_xa_loc *loc)
1786 le16_add_cpu(&loc->
xl_header->xh_name_value_len,
1787 -namevalue_size_xe(loc->
xl_entry));
1790 static void ocfs2_xa_bucket_add_entry(
struct ocfs2_xa_loc *loc,
u32 name_hash)
1801 while (low <=
high && count) {
1802 tmp = (low +
high) / 2;
1825 static void ocfs2_xa_bucket_add_namevalue(
struct ocfs2_xa_loc *loc,
int size)
1827 int free_start = ocfs2_xa_get_free_start(loc);
1832 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1833 nameval_offset = free_start -
size;
1840 static void ocfs2_xa_bucket_fill_value_buf(
struct ocfs2_xa_loc *loc,
1846 int size = namevalue_size_xe(loc->
xl_entry);
1861 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1862 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
1863 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1864 .xlo_check_space = ocfs2_xa_bucket_check_space,
1865 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1866 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1867 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1868 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1869 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1870 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1873 static unsigned int ocfs2_xa_value_clusters(
struct ocfs2_xa_loc *loc)
1877 if (ocfs2_xattr_is_local(loc->
xl_entry))
1880 ocfs2_xa_fill_value_buf(loc, &vb);
1887 int trunc_rc, access_rc;
1890 ocfs2_xa_fill_value_buf(loc, &vb);
1891 trunc_rc = ocfs2_xattr_value_truncate(loc->
xl_inode, &vb, bytes,
1904 access_rc = ocfs2_xa_journal_access(ctxt->
handle, loc,
1908 return trunc_rc ? trunc_rc : access_rc;
1911 static void ocfs2_xa_remove_entry(
struct ocfs2_xa_loc *loc)
1917 ocfs2_xa_wipe_namevalue(loc);
1929 index = ((
char *)entry - (
char *)&xh->
xh_entries) /
1962 static void ocfs2_xa_cleanup_value_truncate(
struct ocfs2_xa_loc *loc,
1964 unsigned int orig_clusters)
1966 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1967 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1970 if (new_clusters < orig_clusters) {
1972 "Partial truncate while %s xattr %.*s. Leaking "
1973 "%u clusters and removing the entry\n",
1974 what, loc->
xl_entry->xe_name_len, nameval_buf,
1975 orig_clusters - new_clusters);
1976 ocfs2_xa_remove_entry(loc);
1977 }
else if (!orig_clusters) {
1979 "Unable to allocate an external value for xattr "
1980 "%.*s safely. Leaking %u clusters and removing the "
1982 loc->
xl_entry->xe_name_len, nameval_buf,
1983 new_clusters - orig_clusters);
1984 ocfs2_xa_remove_entry(loc);
1985 }
else if (new_clusters > orig_clusters)
1987 "Unable to grow xattr %.*s safely. %u new clusters "
1988 "have been added, but the value will not be "
1990 loc->
xl_entry->xe_name_len, nameval_buf,
1991 new_clusters - orig_clusters);
1998 unsigned int orig_clusters;
2000 if (!ocfs2_xattr_is_local(loc->
xl_entry)) {
2001 orig_clusters = ocfs2_xa_value_clusters(loc);
2002 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2011 if (orig_clusters != ocfs2_xa_value_clusters(loc))
2013 ocfs2_xa_cleanup_value_truncate(loc,
"removing",
2020 ocfs2_xa_remove_entry(loc);
2026 static void ocfs2_xa_install_value_root(
struct ocfs2_xa_loc *loc)
2031 nameval_buf = ocfs2_xa_offset_pointer(loc,
2041 static int ocfs2_xa_reuse_entry(
struct ocfs2_xa_loc *loc,
2047 unsigned int orig_clusters;
2049 int xe_local = ocfs2_xattr_is_local(loc->
xl_entry);
2055 nameval_buf = ocfs2_xa_offset_pointer(loc,
2058 memset(nameval_buf + name_size, 0,
2059 namevalue_size_xe(loc->
xl_entry) - name_size);
2061 ocfs2_xa_install_value_root(loc);
2063 orig_clusters = ocfs2_xa_value_clusters(loc);
2065 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2069 memset(nameval_buf + name_size, 0,
2081 ocfs2_xa_cleanup_value_truncate(loc,
"reusing",
2088 ocfs2_xattr_set_local(loc->
xl_entry, xi_local);
2102 static int ocfs2_xa_prepare_entry(
struct ocfs2_xa_loc *loc,
2108 unsigned int orig_clusters;
2109 __le64 orig_value_size = 0;
2111 rc = ocfs2_xa_check_space(loc, xi);
2116 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2117 orig_value_size = loc->
xl_entry->xe_value_size;
2118 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2124 if (!ocfs2_xattr_is_local(loc->
xl_entry)) {
2125 orig_clusters = ocfs2_xa_value_clusters(loc);
2126 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2129 ocfs2_xa_cleanup_value_truncate(loc,
2135 ocfs2_xa_wipe_namevalue(loc);
2137 ocfs2_xa_add_entry(loc, name_hash);
2143 ocfs2_xa_add_namevalue(loc, xi);
2145 ocfs2_xa_install_value_root(loc);
2149 orig_clusters = ocfs2_xa_value_clusters(loc);
2150 rc = ocfs2_xa_value_truncate(loc, xi->
xi_value_len, ctxt);
2153 ocfs2_xa_cleanup_value_truncate(loc,
"growing",
2162 BUG_ON(!orig_value_size);
2163 loc->
xl_entry->xe_value_size = orig_value_size;
2178 static int ocfs2_xa_store_value(
struct ocfs2_xa_loc *loc,
2188 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2190 ocfs2_xa_fill_value_buf(loc, &vb);
2191 rc = __ocfs2_xattr_set_value_outside(loc->
xl_inode,
2209 ret = ocfs2_xa_journal_access(ctxt->
handle, loc,
2224 ret = ocfs2_xa_remove(loc, ctxt);
2228 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2235 ret = ocfs2_xa_store_value(loc, xi, ctxt);
2240 ocfs2_xa_journal_dirty(ctxt->
handle, loc);
2246 static void ocfs2_init_dinode_xa_loc(
struct ocfs2_xa_loc *loc,
2247 struct inode *inode,
2248 struct buffer_head *bh,
2256 loc->
xl_ops = &ocfs2_xa_block_loc_ops;
2265 static void ocfs2_init_xattr_block_xa_loc(
struct ocfs2_xa_loc *loc,
2266 struct inode *inode,
2267 struct buffer_head *bh,
2276 loc->
xl_ops = &ocfs2_xa_block_loc_ops;
2284 static void ocfs2_init_xattr_bucket_xa_loc(
struct ocfs2_xa_loc *loc,
2289 loc->
xl_ops = &ocfs2_xa_bucket_loc_ops;
2300 static int ocfs2_lock_xattr_remove_allocators(
struct inode *inode,
2303 struct buffer_head *ref_root_bh,
2307 int ret, meta_add = 0;
2308 u32 p_cluster, num_clusters;
2321 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2326 &meta_add, ref_credits);
2341 static int ocfs2_remove_value_outside(
struct inode*inode,
2345 struct buffer_head *ref_root_bh)
2347 int ret = 0,
i, ref_credits;
2352 ocfs2_init_dealloc_ctxt(&ctxt.
dealloc);
2357 if (ocfs2_xattr_is_local(entry))
2360 val = (
void *)header +
2365 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->
vb_xv,
2366 ref_ci, ref_root_bh,
2371 ocfs2_remove_extent_credits(osb->
sb));
2372 if (IS_ERR(ctxt.
handle)) {
2373 ret = PTR_ERR(ctxt.
handle);
2378 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2400 static int ocfs2_xattr_ibody_remove(
struct inode *inode,
2401 struct buffer_head *di_bh,
2403 struct buffer_head *ref_root_bh)
2415 ((
void *)di + inode->
i_sb->s_blocksize -
2418 ret = ocfs2_remove_value_outside(inode, &vb, header,
2419 ref_ci, ref_root_bh);
2429 static int ocfs2_xattr_block_remove(
struct inode *inode,
2430 struct buffer_head *blk_bh,
2432 struct buffer_head *ref_root_bh)
2448 ret = ocfs2_remove_value_outside(inode, &vb, header,
2449 ref_ci, ref_root_bh);
2451 ret = ocfs2_iterate_xattr_index_block(inode,
2453 ocfs2_rm_xattr_cluster,
2459 static int ocfs2_xattr_free_block(
struct inode *inode,
2462 struct buffer_head *ref_root_bh)
2464 struct inode *xb_alloc_inode;
2465 struct buffer_head *xb_alloc_bh =
NULL;
2466 struct buffer_head *blk_bh =
NULL;
2474 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2480 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2492 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2497 if (!xb_alloc_inode) {
2511 if (IS_ERR(handle)) {
2512 ret = PTR_ERR(handle);
2525 brelse(xb_alloc_bh);
2528 iput(xb_alloc_inode);
2544 struct buffer_head *ref_root_bh =
NULL;
2558 1, &ref_tree, &ref_root_bh);
2563 ref_ci = &ref_tree->rf_ci;
2568 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2569 ref_ci, ref_root_bh);
2577 ret = ocfs2_xattr_free_block(inode,
2579 ref_ci, ref_root_bh);
2588 if (IS_ERR(handle)) {
2589 ret = PTR_ERR(handle);
2613 brelse(ref_root_bh);
2617 static int ocfs2_xattr_has_space_inline(
struct inode *inode,
2621 unsigned int xattrsize =
OCFS2_SB(inode->
i_sb)->s_xattr_inline_size;
2630 }
else if (ocfs2_inode_is_fast_symlink(inode)) {
2631 free = ocfs2_fast_symlink_chars(inode->
i_sb) -
2639 if (free >= xattrsize)
2651 static int ocfs2_xattr_ibody_find(
struct inode *inode,
2666 has_space = ocfs2_xattr_has_space_inline(inode, di);
2673 xs->
end = (
void *)di + inode->
i_sb->s_blocksize;
2685 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2694 static int ocfs2_xattr_ibody_init(
struct inode *inode,
2695 struct buffer_head *di_bh,
2704 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2722 le16_add_cpu(&idata->
id_count, -xattrsize);
2723 }
else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2725 le16_add_cpu(&el->
l_count, -(xattrsize /
2747 static int ocfs2_xattr_ibody_set(
struct inode *inode,
2762 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2769 ret = ocfs2_xattr_ibody_init(inode, xs->
inode_bh, ctxt);
2777 ocfs2_init_dinode_xa_loc(&loc, inode, xs->
inode_bh,
2779 ret = ocfs2_xa_set(&loc, xi, ctxt);
2799 static int ocfs2_xattr_block_find(
struct inode *inode,
2805 struct buffer_head *blk_bh =
NULL;
2825 xs->
end = (
void *)(blk_bh->b_data) + blk_bh->b_size;
2828 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2830 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2846 static int ocfs2_create_xattr_block(
struct inode *inode,
2847 struct buffer_head *inode_bh,
2850 struct buffer_head **ret_bh)
2853 u16 suballoc_bit_start;
2855 u64 suballoc_loc, first_blkno;
2857 struct buffer_head *new_bh =
NULL;
2868 &suballoc_loc, &suballoc_bit_start,
2869 &num_got, &first_blkno);
2875 new_bh = sb_getblk(inode->
i_sb, first_blkno);
2891 xblk->xb_suballoc_loc =
cpu_to_le64(suballoc_loc);
2892 xblk->xb_suballoc_bit =
cpu_to_le16(suballoc_bit_start);
2893 xblk->xb_fs_generation =
2902 ocfs2_xattr_recs_per_xb(inode->
i_sb));
2911 spin_lock(&OCFS2_I(inode)->ip_lock);
2914 spin_unlock(&OCFS2_I(inode)->ip_lock);
2932 static int ocfs2_xattr_block_set(
struct inode *inode,
2937 struct buffer_head *new_bh =
NULL;
2943 ret = ocfs2_create_xattr_block(inode, xs->
inode_bh, ctxt,
2954 xs->
end = (
void *)xblk + inode->
i_sb->s_blocksize;
2960 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->
xattr_bh,
2963 ret = ocfs2_xa_set(&loc, xi, ctxt);
2969 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2976 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2983 static int ocfs2_xattr_can_be_in_inode(
struct inode *inode,
2989 size_t min_offs = xs->
end - xs->
base;
2994 last = xs->
header->xh_entries;
2998 if (offs < min_offs)
3015 static int ocfs2_calc_xattr_set_need(
struct inode *inode,
3024 int ret = 0, old_in_xb = 0;
3025 int clusters_add = 0, meta_add = 0, credits = 0;
3026 struct buffer_head *bh =
NULL;
3031 int name_offset, name_len = 0;
3032 u32 new_clusters = ocfs2_clusters_for_bytes(inode->
i_sb,
3042 credits += new_clusters *
3043 ocfs2_clusters_to_blocks(inode->
i_sb, 1);
3046 credits += ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
3049 clusters_add += new_clusters;
3050 credits += ocfs2_calc_extend_credits(inode->
i_sb,
3065 int i, block_off = 0;
3074 ret = ocfs2_xattr_bucket_get_name_value(inode->
i_sb,
3079 credits += ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
3094 if (!ocfs2_xattr_is_local(xe))
3095 credits += ocfs2_remove_extent_credits(inode->
i_sb);
3110 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3111 clusters_add += new_clusters;
3112 credits += ocfs2_remove_extent_credits(inode->
i_sb) +
3114 if (!ocfs2_xattr_is_local(xe))
3115 credits += ocfs2_calc_extend_credits(
3125 u32 old_clusters = 0;
3127 if (!ocfs2_xattr_is_local(xe)) {
3128 old_clusters = ocfs2_clusters_for_bytes(inode->
i_sb,
3131 (base + name_offset + name_len);
3136 if (old_clusters >= new_clusters) {
3137 credits += ocfs2_remove_extent_credits(inode->
i_sb);
3140 meta_add += ocfs2_extend_meta_needed(&xv->
xr_list);
3141 clusters_add += new_clusters - old_clusters;
3142 credits += ocfs2_calc_extend_credits(inode->
i_sb,
3156 if ((ocfs2_xattr_is_local(xe) &&
3158 (!ocfs2_xattr_is_local(xe) &&
3167 ret = ocfs2_read_xattr_block(inode,
3189 meta_add += ocfs2_extend_meta_needed(el);
3190 credits += ocfs2_calc_extend_credits(inode->
i_sb,
3203 credits += ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
3206 credits += ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
3215 *clusters_need = clusters_add;
3217 *meta_need = meta_add;
3219 *credits_need = credits;
3224 static int ocfs2_init_xattr_set_ctxt(
struct inode *inode,
3233 int clusters_add, meta_add,
ret;
3238 ocfs2_init_dealloc_ctxt(&ctxt->
dealloc);
3240 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3241 &clusters_add, &meta_add, credits);
3247 meta_add += extra_meta;
3248 trace_ocfs2_init_xattr_set_ctxt(xi->
xi_name, meta_add,
3249 clusters_add, *credits);
3280 static int __ocfs2_xattr_set_handle(
struct inode *inode,
3287 int ret = 0, credits, old_found;
3292 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3294 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3297 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3308 ret = ocfs2_calc_xattr_set_need(inode,
3327 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3330 ret = ocfs2_xattr_block_find(inode,
3338 ret = ocfs2_calc_xattr_set_need(inode,
3362 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3373 ret = ocfs2_calc_xattr_set_need(inode,
3391 ret = ocfs2_xattr_ibody_set(inode, xi,
3422 struct inode *inode,
3423 struct buffer_head *di_bh,
3438 .xi_name_len =
strlen(name),
3466 xbs.
bucket = ocfs2_xattr_bucket_new(inode);
3478 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3482 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3487 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3490 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3492 ocfs2_xattr_bucket_free(xbs.
bucket);
3511 struct buffer_head *di_bh =
NULL;
3513 int ret, credits, ref_meta = 0, ref_credits = 0;
3522 .xi_name_len =
strlen(name),
3542 xbs.
bucket = ocfs2_xattr_bucket_new(inode);
3551 goto cleanup_nolock;
3561 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3565 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3586 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3587 &xis, &xbs, &ref_tree,
3588 &ref_meta, &ref_credits);
3607 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3608 &xbs, &ctxt, ref_meta, &credits);
3617 if (IS_ERR(ctxt.
handle)) {
3618 ret = PTR_ERR(ctxt.
handle);
3623 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3631 if (ocfs2_dealloc_has_cluster(&ctxt.
dealloc))
3638 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3639 if (!value && !ret) {
3648 ocfs2_xattr_bucket_free(xbs.
bucket);
3658 static int ocfs2_xattr_get_rec(
struct inode *inode,
3666 struct buffer_head *eb_bh =
NULL;
3684 "Inode %lu has non zero tree depth in "
3685 "xattr tree block %llu\n", inode->
i_ino,
3686 (
unsigned long long)eb_bh->b_blocknr);
3703 "record (%u, %u, 0) in xattr", inode->
i_ino,
3705 ocfs2_rec_clusters(el, rec));
3723 static int ocfs2_find_xe_in_bucket(
struct inode *inode,
3731 int i, ret = 0, cmp = 1, block_off, new_offset;
3733 size_t name_len =
strlen(name);
3749 cmp = name_index - ocfs2_xattr_get_type(xe);
3755 ret = ocfs2_xattr_bucket_get_name_value(inode->
i_sb,
3766 xe_name =
bucket_block(bucket, block_off) + new_offset;
3767 if (!
memcmp(name, xe_name, name_len)) {
3787 static int ocfs2_xattr_bucket_find(
struct inode *inode,
3800 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
3801 int low_bucket = 0, bucket, high_bucket;
3804 u64 blkno, lower_blkno = 0;
3806 search = ocfs2_xattr_bucket_new(inode);
3813 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3821 while (low_bucket <= high_bucket) {
3822 ocfs2_xattr_bucket_relse(search);
3824 bucket = (low_bucket + high_bucket) / 2;
3825 blkno = p_blkno + bucket * blk_per_bucket;
3826 ret = ocfs2_read_xattr_bucket(search, blkno);
3835 high_bucket = bucket - 1;
3850 lower_blkno = blkno;
3853 low_bucket = bucket + 1;
3858 ret = ocfs2_find_xe_in_bucket(inode, search,
3859 name_index, name, name_hash,
3874 lower_blkno = p_blkno;
3877 ret = ocfs2_read_xattr_bucket(xs->
bucket, lower_blkno);
3889 trace_ocfs2_xattr_bucket_find(OCFS2_I(inode)->ip_blkno,
3890 name, name_index, name_hash,
3897 ocfs2_xattr_bucket_free(search);
3901 static int ocfs2_xattr_index_block_find(
struct inode *inode,
3902 struct buffer_head *root_bh,
3913 u32 first_hash, num_clusters = 0;
3914 u32 name_hash = ocfs2_xattr_name_hash(inode, name,
strlen(name));
3919 trace_ocfs2_xattr_index_block_find(OCFS2_I(inode)->ip_blkno,
3920 name, name_index, name_hash,
3921 (
unsigned long long)root_bh->b_blocknr,
3924 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3931 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3933 trace_ocfs2_xattr_index_block_find_rec(OCFS2_I(inode)->ip_blkno,
3934 name, name_index, first_hash,
3935 (
unsigned long long)p_blkno,
3938 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3939 p_blkno, first_hash, num_clusters, xs);
3945 static int ocfs2_iterate_xattr_buckets(
struct inode *inode,
3953 u32 num_buckets = clusters * bpc;
3956 bucket = ocfs2_xattr_bucket_new(inode);
3962 trace_ocfs2_iterate_xattr_buckets(
3963 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
3964 (
unsigned long long)blkno, clusters);
3966 for (i = 0; i < num_buckets; i++, blkno += bucket->
bu_blocks) {
3967 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3980 trace_ocfs2_iterate_xattr_bucket((
unsigned long long)blkno,
3983 ret =
func(inode, bucket, para);
3984 if (ret && ret != -
ERANGE)
3989 ocfs2_xattr_bucket_relse(bucket);
3994 ocfs2_xattr_bucket_free(bucket);
4004 static int ocfs2_xattr_bucket_get_name_value(
struct super_block *sb,
4023 static int ocfs2_list_xattr_bucket(
struct inode *inode,
4029 int i, block_off, new_offset;
4034 type = ocfs2_xattr_get_type(entry);
4035 prefix = ocfs2_xattr_prefix(type);
4038 ret = ocfs2_xattr_bucket_get_name_value(inode->
i_sb,
4048 ret = ocfs2_xattr_list_entry(xl->
buffer,
4061 static int ocfs2_iterate_xattr_index_block(
struct inode *inode,
4062 struct buffer_head *blk_bh,
4070 u32 name_hash =
UINT_MAX, e_cpos = 0, num_clusters = 0;
4076 while (name_hash > 0) {
4077 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4078 &e_cpos, &num_clusters, el);
4084 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4085 num_clusters, para);
4095 name_hash = e_cpos - 1;
4102 static int ocfs2_list_xattr_tree_rec(
struct inode *inode,
4103 struct buffer_head *root_bh,
4104 u64 blkno,
u32 cpos,
u32 len,
void *para)
4106 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4107 ocfs2_list_xattr_bucket, para);
4110 static int ocfs2_xattr_tree_list_index_block(
struct inode *inode,
4111 struct buffer_head *blk_bh,
4122 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4123 ocfs2_list_xattr_tree_rec, &xl);
4134 static int cmp_xe(
const void *
a,
const void *
b)
4140 if (l_hash > r_hash)
4142 if (l_hash < r_hash)
4147 static void swap_xe(
void *a,
void *b,
int size)
4164 static void ocfs2_cp_xattr_block_to_bucket(
struct inode *inode,
4165 struct buffer_head *xb_bh,
4168 int i, blocksize = inode->
i_sb->s_blocksize;
4169 int blks = ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
4177 char *src = xb_bh->b_data;
4180 trace_ocfs2_cp_xattr_block_to_bucket_begin(
4181 (
unsigned long long)xb_bh->b_blocknr,
4184 for (i = 0; i < blks; i++)
4193 xe = &xb_xh->xh_entries[count - 1];
4195 size = blocksize -
offset;
4198 memcpy(target + offset, src + offset, size);
4210 memcpy(target + offset, (
char *)xb_xh + offset, size);
4215 for (i = 0; i <
count; i++)
4216 le16_add_cpu(&xh->
xh_entries[i].xe_name_offset, off_change);
4218 trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
4232 static void ocfs2_xattr_update_xattr_search(
struct inode *inode,
4234 struct buffer_head *old_bh)
4236 char *
buf = old_bh->b_data;
4252 static int ocfs2_xattr_create_index_block(
struct inode *inode,
4259 handle_t *handle = ctxt->
handle;
4261 struct buffer_head *xb_bh = xs->
xattr_bh;
4267 trace_ocfs2_xattr_create_index_block_begin(
4268 (
unsigned long long)xb_bh->b_blocknr);
4288 1, 1, &bit_off, &len);
4299 blkno = ocfs2_clusters_to_blocks(inode->
i_sb, bit_off);
4301 trace_ocfs2_xattr_create_index_block((
unsigned long long)blkno);
4303 ret = ocfs2_init_xattr_bucket(xs->
bucket, blkno);
4309 ret = ocfs2_xattr_bucket_journal_access(handle, xs->
bucket,
4316 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->
bucket);
4317 ocfs2_xattr_bucket_journal_dirty(handle, xs->
bucket);
4319 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4332 xr->
xt_list.l_recs[0].e_cpos = 0;
4346 static int cmp_xe_offset(
const void *a,
const void *b)
4352 if (l_name_offset < r_name_offset)
4354 if (l_name_offset > r_name_offset)
4365 static int ocfs2_defrag_xattr_bucket(
struct inode *inode,
4375 size_t blocksize = inode->
i_sb->s_blocksize;
4391 for (i = 0; i < bucket->
bu_blocks; i++, buf += blocksize)
4394 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4405 trace_ocfs2_defrag_xattr_bucket(
4416 cmp_xe_offset, swap_xe);
4423 len = namevalue_size_xe(xe);
4430 if (((end - len) / blocksize !=
4431 (end - 1) / blocksize))
4432 end = end - end % blocksize;
4434 if (end > offset + len) {
4435 memmove(bucket_buf + end - len,
4436 bucket_buf + offset, len);
4441 "bucket %llu\n", (
unsigned long long)blkno);
4447 "bucket %llu\n", (
unsigned long long)blkno);
4449 if (xh_free_start == end)
4452 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4461 for (i = 0; i < bucket->
bu_blocks; i++, buf += blocksize)
4463 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4487 static int ocfs2_mv_xattr_bucket_cross_cluster(
struct inode *inode,
4497 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4498 int num_buckets = ocfs2_xattr_buckets_per_cluster(
OCFS2_SB(sb));
4499 int to_move = num_buckets / 2;
4502 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4507 trace_ocfs2_mv_xattr_bucket_cross_cluster(
4508 (
unsigned long long)last_cluster_blkno,
4509 (
unsigned long long)new_blkno);
4511 ret = ocfs2_mv_xattr_buckets(inode, handle,
bucket_blkno(first),
4512 last_cluster_blkno, new_blkno,
4513 to_move, first_hash);
4520 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4528 src_blkno = new_blkno +
4531 ocfs2_xattr_bucket_relse(first);
4532 ocfs2_xattr_bucket_relse(target);
4538 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4543 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4567 int delta, middle = count / 2;
4575 for (delta = 0; delta < middle; delta++) {
4577 if (cmp_xe(&entries[middle - delta - 1],
4578 &entries[middle - delta]))
4579 return middle -
delta;
4582 if ((middle + delta + 1) == count)
4586 if (cmp_xe(&entries[middle + delta],
4587 &entries[middle + delta + 1]))
4588 return middle + delta + 1;
4605 static int ocfs2_divide_xattr_bucket(
struct inode *inode,
4610 int new_bucket_head)
4613 int count,
start, len, name_value_len = 0, name_offset = 0;
4617 int blocksize = inode->
i_sb->s_blocksize;
4619 trace_ocfs2_divide_xattr_bucket_begin((
unsigned long long)blk,
4620 (
unsigned long long)new_blk);
4622 s_bucket = ocfs2_xattr_bucket_new(inode);
4623 t_bucket = ocfs2_xattr_bucket_new(inode);
4624 if (!s_bucket || !t_bucket) {
4630 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4636 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4647 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4658 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4669 start = ocfs2_xattr_find_divide_pos(xh);
4671 if (start == count) {
4679 for (i = 0; i < t_bucket->bu_blocks; i++)
4685 le32_add_cpu(&xh->
xh_entries[0].xe_name_hash, 1);
4687 goto set_num_buckets;
4691 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4702 for (i = 0; i <
start; i++) {
4704 name_value_len += namevalue_size_xe(xe);
4719 trace_ocfs2_divide_xattr_bucket_move(len,
4720 (
int)((
char *)xe - (
char *)xh),
4725 memset((
char *)xe, 0, len);
4727 le16_add_cpu(&xh->
xh_count, -start);
4741 if (new_bucket_head)
4746 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4767 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4770 ocfs2_xattr_bucket_free(s_bucket);
4771 ocfs2_xattr_bucket_free(t_bucket);
4782 static int ocfs2_cp_xattr_bucket(
struct inode *inode,
4791 BUG_ON(s_blkno == t_blkno);
4793 trace_ocfs2_cp_xattr_bucket((
unsigned long long)s_blkno,
4794 (
unsigned long long)t_blkno,
4797 s_bucket = ocfs2_xattr_bucket_new(inode);
4798 t_bucket = ocfs2_xattr_bucket_new(inode);
4799 if (!s_bucket || !t_bucket) {
4805 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4813 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4830 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4837 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4838 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4841 ocfs2_xattr_bucket_free(t_bucket);
4842 ocfs2_xattr_bucket_free(s_bucket);
4856 static int ocfs2_mv_xattr_buckets(
struct inode *inode, handle_t *handle,
4858 unsigned int start_bucket,
4861 int i,
ret, credits;
4863 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
4864 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4867 trace_ocfs2_mv_xattr_buckets((
unsigned long long)last_blk,
4868 (
unsigned long long)to_blk);
4870 BUG_ON(start_bucket >= num_buckets);
4872 num_buckets -= start_bucket;
4873 last_blk += (start_bucket * blks_per_bucket);
4877 old_first = ocfs2_xattr_bucket_new(inode);
4879 new_first = ocfs2_xattr_bucket_new(inode);
4880 if (!old_first || !new_first) {
4886 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4896 credits = ((num_buckets + 1) * blks_per_bucket);
4903 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4910 for (i = 0; i < num_buckets; i++) {
4911 ret = ocfs2_cp_xattr_bucket(inode, handle,
4912 last_blk + (i * blks_per_bucket),
4913 to_blk + (i * blks_per_bucket),
4926 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4931 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4939 le16_add_cpu(&
bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4940 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4943 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4949 ocfs2_xattr_bucket_free(new_first);
4950 ocfs2_xattr_bucket_free(old_first);
4959 static int ocfs2_divide_xattr_cluster(
struct inode *inode,
4965 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
4966 int ret, credits = 2 * blk_per_bucket;
4977 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4978 new_blk, first_hash, 1);
5007 static int ocfs2_adjust_xattr_cross_cluster(
struct inode *inode,
5018 trace_ocfs2_adjust_xattr_cross_cluster(
5020 (
unsigned long long)new_blk, prev_clusters);
5022 if (ocfs2_xattr_buckets_per_cluster(
OCFS2_SB(inode->
i_sb)) > 1) {
5023 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5034 ((prev_clusters - 1) *
5035 ocfs2_clusters_to_blocks(inode->
i_sb, 1));
5037 if (prev_clusters > 1 &&
bucket_blkno(target) != last_blk) {
5038 ret = ocfs2_mv_xattr_buckets(inode, handle,
5040 last_blk, new_blk, 0,
5045 ret = ocfs2_divide_xattr_cluster(inode, handle,
5075 static int ocfs2_add_new_xattr_cluster(
struct inode *inode,
5076 struct buffer_head *root_bh,
5085 u16 bpc = ocfs2_clusters_to_blocks(inode->
i_sb, 1);
5086 u32 prev_clusters = *num_clusters;
5087 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5089 handle_t *handle = ctxt->
handle;
5093 trace_ocfs2_add_new_xattr_cluster_begin(
5094 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
5096 prev_cpos, prev_clusters);
5108 clusters_to_add, &bit_off, &num_bits);
5115 BUG_ON(num_bits > clusters_to_add);
5117 block = ocfs2_clusters_to_blocks(osb->
sb, bit_off);
5118 trace_ocfs2_add_new_xattr_cluster((
unsigned long long)block, num_bits);
5120 if (
bucket_blkno(first) + (prev_clusters * bpc) == block &&
5132 v_start = prev_cpos + prev_clusters;
5133 *num_clusters = prev_clusters + num_bits;
5135 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5149 trace_ocfs2_add_new_xattr_cluster_insert((
unsigned long long)block,
5173 static int ocfs2_extend_xattr_bucket(
struct inode *inode,
5181 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
5185 trace_ocfs2_extend_xattr_bucket((
unsigned long long)target_blk,
5187 num_clusters, new_bucket);
5191 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5194 end_blk =
bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5203 credits = (end_blk - target_blk) + (3 * blk_per_bucket);
5210 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5217 while (end_blk != target_blk) {
5218 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5219 end_blk + blk_per_bucket, 0);
5222 end_blk -= blk_per_bucket;
5226 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5227 target_blk + blk_per_bucket,
NULL, 0);
5229 le16_add_cpu(&
bucket_xh(first)->xh_num_buckets, 1);
5230 ocfs2_xattr_bucket_journal_dirty(handle, first);
5249 static int ocfs2_add_new_xattr_bucket(
struct inode *inode,
5250 struct buffer_head *xb_bh,
5261 int ret, num_buckets, extend = 1;
5263 u32 e_cpos, num_clusters;
5267 trace_ocfs2_add_new_xattr_bucket(
5271 first = ocfs2_xattr_bucket_new(inode);
5278 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5285 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5291 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5297 ret = ocfs2_add_new_xattr_cluster(inode,
5312 ret = ocfs2_extend_xattr_bucket(inode,
5322 ocfs2_xattr_bucket_free(first);
5327 static inline char *ocfs2_xattr_bucket_get_val(
struct inode *inode,
5331 int block_off = offs >> inode->
i_sb->s_blocksize_bits;
5333 offs = offs % inode->
i_sb->s_blocksize;
5344 static int ocfs2_xattr_bucket_value_truncate(
struct inode *inode,
5354 size_t blocksize = inode->
i_sb->s_blocksize;
5361 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5366 value_blk = offset / blocksize;
5375 (vb.
vb_bh->b_data + offset % blocksize);
5384 trace_ocfs2_xattr_bucket_value_truncate(
5385 (
unsigned long long)
bucket_blkno(bucket), xe_off, len);
5386 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5392 ret = ocfs2_xattr_bucket_journal_access(ctxt->
handle, bucket,
5401 ocfs2_xattr_bucket_journal_dirty(ctxt->
handle, bucket);
5407 static int ocfs2_rm_xattr_cluster(
struct inode *inode,
5408 struct buffer_head *root_bh,
5424 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5425 ocfs2_delete_xattr_in_bucket, para);
5433 ocfs2_init_dealloc_ctxt(&dealloc);
5435 trace_ocfs2_rm_xattr_cluster(
5436 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
5437 (
unsigned long long)blkno, cpos, len);
5459 if (IS_ERR(handle)) {
5507 static int ocfs2_check_xattr_bucket_collision(
struct inode *inode,
5512 u32 name_hash = ocfs2_xattr_name_hash(inode, name,
strlen(name));
5519 mlog(
ML_ERROR,
"Too much hash collision in xattr bucket %llu, "
5533 static int ocfs2_xattr_set_entry_bucket(
struct inode *inode,
5541 trace_ocfs2_xattr_set_entry_bucket(xi->
xi_name);
5543 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->
bucket,
5545 ret = ocfs2_xa_set(&loc, xi, ctxt);
5556 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->
handle,
5563 ret = ocfs2_xa_set(&loc, xi, ctxt);
5576 static int ocfs2_xattr_set_entry_index_block(
struct inode *inode,
5583 trace_ocfs2_xattr_set_entry_index_block(xi->
xi_name);
5585 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5601 ret = ocfs2_check_xattr_bucket_collision(inode,
5609 ret = ocfs2_add_new_xattr_bucket(inode,
5625 ocfs2_xattr_bucket_relse(xs->
bucket);
5626 ret = ocfs2_xattr_index_block_find(inode, xs->
xattr_bh,
5634 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5635 if (ret && (ret != -
ENOSPC))
5642 static int ocfs2_delete_xattr_in_bucket(
struct inode *inode,
5646 int ret = 0, ref_credits;
5652 int credits = ocfs2_remove_extent_credits(osb->
sb) +
5653 ocfs2_blocks_per_xattr_bucket(inode->
i_sb);
5658 ocfs2_init_dealloc_ctxt(&ctxt.
dealloc);
5662 if (ocfs2_xattr_is_local(xe))
5665 ret = ocfs2_get_xattr_tree_value_root(inode->
i_sb, bucket,
5668 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5675 if (IS_ERR(ctxt.
handle)) {
5676 ret = PTR_ERR(ctxt.
handle);
5681 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5710 static int ocfs2_xattr_bucket_post_refcount(
struct inode *inode,
5718 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5725 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5744 static int ocfs2_prepare_refcount_xattr(
struct inode *inode,
5757 u32 p_cluster, num_clusters;
5765 struct buffer_head *ref_root_bh =
NULL;
5775 int i, block_off = 0;
5783 ret = ocfs2_xattr_bucket_get_name_value(inode->
i_sb,
5795 if (ocfs2_meta_ecc(osb)) {
5801 ocfs2_xattr_bucket_post_refcount;
5811 if (ocfs2_xattr_is_local(xe))
5815 (base + name_offset + name_len);
5818 &num_clusters, &vb.
vb_xv->xr_list,
5830 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5834 1, ref_tree, &ref_root_bh);
5851 &(*ref_tree)->rf_ci,
5852 ref_root_bh, vb.
vb_xv,
5860 *ref_tree, ref_root_bh, 0,
5866 brelse(ref_root_bh);
5874 static int ocfs2_xattr_value_attach_refcount(
struct inode *inode,
5878 struct buffer_head *ref_root_bh,
5884 u32 cpos, p_cluster, num_clusters;
5889 while (cpos < clusters) {
5891 &num_clusters, el, &ext_flags);
5893 cpos += num_clusters;
5894 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5900 ref_ci, ref_root_bh,
5901 cpos - num_clusters,
5902 p_cluster, num_clusters,
5918 static int ocfs2_xattr_attach_refcount_normal(
struct inode *inode,
5922 struct buffer_head *ref_root_bh,
5934 if (ocfs2_xattr_is_local(xe))
5944 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &
et,
5945 ref_ci, ref_root_bh,
5956 static int ocfs2_xattr_inline_attach_refcount(
struct inode *inode,
5957 struct buffer_head *fe_bh,
5959 struct buffer_head *ref_root_bh,
5964 (fe_bh->b_data + inode->
i_sb->s_blocksize -
5971 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5972 ref_ci, ref_root_bh, dealloc);
5981 static int ocfs2_get_xattr_tree_value_root(
struct super_block *sb,
5985 struct buffer_head **bh)
5987 int ret, block_off, name_offset;
5992 ret = ocfs2_xattr_bucket_get_name_value(sb,
6008 *bh = bucket->
bu_bhs[block_off];
6017 static int ocfs2_xattr_bucket_value_refcount(
struct inode *inode,
6034 .func = ocfs2_xattr_bucket_post_refcount,
6042 trace_ocfs2_xattr_bucket_value_refcount(
6048 if (ocfs2_xattr_is_local(xe))
6051 ret = ocfs2_get_xattr_tree_value_root(inode->
i_sb, bucket, i,
6059 INODE_CACHE(inode), &vb);
6061 ret = ocfs2_xattr_value_attach_refcount(inode, vb.
vb_xv,
6075 static int ocfs2_refcount_xattr_tree_rec(
struct inode *inode,
6076 struct buffer_head *root_bh,
6077 u64 blkno,
u32 cpos,
u32 len,
void *para)
6079 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6080 ocfs2_xattr_bucket_value_refcount,
6084 static int ocfs2_xattr_block_attach_refcount(
struct inode *inode,
6085 struct buffer_head *blk_bh,
6087 struct buffer_head *ref_root_bh,
6101 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6102 ref_ci, ref_root_bh,
6111 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6112 ocfs2_refcount_xattr_tree_rec,
6120 struct buffer_head *fe_bh,
6122 struct buffer_head *ref_root_bh,
6128 struct buffer_head *blk_bh =
NULL;
6131 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6132 ref_ci, ref_root_bh,
6150 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6151 ref_root_bh, dealloc);
6183 struct buffer_head *bh,
6187 struct buffer_head **ret_bh,
6195 static int ocfs2_value_metas_in_xattr_header(
struct super_block *sb,
6196 struct buffer_head *bh,
6198 int *metas,
int *credits,
6209 if (ocfs2_xattr_is_local(xe))
6212 ret =
func(sb, bh, xh, i, &xv,
NULL, para);
6221 *credits += ocfs2_calc_extend_credits(sb,
6229 if (!xv->
xr_list.l_tree_depth)
6232 *num_recs += ocfs2_clusters_for_bytes(sb,
6240 static int ocfs2_get_xattr_value_root(
struct super_block *sb,
6241 struct buffer_head *bh,
6245 struct buffer_head **ret_bh,
6264 static int ocfs2_reflink_lock_xattr_allocators(
struct ocfs2_super *osb,
6266 struct buffer_head *ref_root_bh,
6270 int ret, meta_add = 0, num_recs = 0;
6276 ret = ocfs2_value_metas_in_xattr_header(osb->
sb,
NULL, xh,
6277 &meta_add, credits, &num_recs,
6278 ocfs2_get_xattr_value_root,
6292 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->
sb) * 2;
6293 meta_add += num_recs;
6320 static int ocfs2_reflink_xattr_header(handle_t *handle,
6322 struct buffer_head *old_bh,
6324 struct buffer_head *new_bh,
6333 struct buffer_head *value_bh;
6337 u32 clusters, cpos, p_cluster, num_clusters;
6338 unsigned int ext_flags = 0;
6340 trace_ocfs2_reflink_xattr_header((
unsigned long long)old_bh->b_blocknr,
6350 le16_add_cpu(&new_xh->
xh_count, -1);
6353 (
void *)last - (
void *)xe);
6366 if (ocfs2_xattr_is_local(xe))
6369 ret =
func(sb, old_bh, xh, i, &xv,
NULL, para);
6375 ret =
func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6391 if (xv->
xr_list.l_tree_depth) {
6392 memcpy(new_xv, &def_xv,
sizeof(def_xv));
6394 vb->
vb_bh = value_bh;
6401 while (cpos < clusters) {
6415 if (xv->
xr_list.l_tree_depth) {
6418 ocfs2_clusters_to_blocks(
6421 num_clusters, ext_flags,
6431 p_cluster, num_clusters,
6438 cpos += num_clusters;
6448 int ret = 0, credits = 0;
6453 int header_off = osb->
sb->s_blocksize - inline_size;
6455 (args->
old_bh->b_data + header_off);
6457 (args->
new_bh->b_data + header_off);
6466 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->
ref_root_bh,
6467 &credits, &meta_ac);
6474 if (IS_ERR(handle)) {
6475 ret = PTR_ERR(handle);
6488 args->
old_bh->b_data + header_off, inline_size);
6493 ret = ocfs2_reflink_xattr_header(handle, args, args->
old_bh, xh,
6494 args->
new_bh, new_xh, &vb, meta_ac,
6495 ocfs2_get_xattr_value_root,
NULL);
6505 spin_unlock(&new_oi->
ip_lock);
6518 static int ocfs2_create_empty_xattr_block(
struct inode *inode,
6519 struct buffer_head *fe_bh,
6520 struct buffer_head **ret_bh,
6527 memset(&ctxt, 0,
sizeof(ctxt));
6535 if (IS_ERR(ctxt.
handle)) {
6536 ret = PTR_ERR(ctxt.
handle);
6541 trace_ocfs2_create_empty_xattr_block(
6542 (
unsigned long long)fe_bh->b_blocknr, indexed);
6543 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6555 struct buffer_head *blk_bh,
6556 struct buffer_head *new_blk_bh)
6558 int ret = 0, credits = 0;
6572 .
vb_bh = new_blk_bh,
6576 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->
ref_root_bh,
6577 &credits, &meta_ac);
6585 if (IS_ERR(handle)) {
6586 ret = PTR_ERR(handle);
6609 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6610 osb->
sb->s_blocksize - header_off);
6612 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6613 new_blk_bh, new_xh, &vb, meta_ac,
6614 ocfs2_get_xattr_value_root,
NULL);
6627 spin_unlock(&new_oi->
ip_lock);
6654 static int ocfs2_get_reflink_xattr_value_root(
struct super_block *sb,
6655 struct buffer_head *bh,
6659 struct buffer_head **ret_bh,
6671 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6681 static int ocfs2_value_tree_metas_in_bucket(
struct super_block *sb,
6682 struct buffer_head *bh,
6686 struct buffer_head **ret_bh,
6692 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6696 static int ocfs2_calc_value_tree_metas(
struct inode *inode,
6707 return ocfs2_value_metas_in_xattr_header(inode->
i_sb, bucket->
bu_bhs[0],
6710 ocfs2_value_tree_metas_in_bucket,
6719 static int ocfs2_lock_reflink_xattr_rec_allocators(
6722 u64 blkno,
u32 len,
int *credits,
6726 int ret, num_free_extents;
6731 memset(&metas, 0,
sizeof(metas));
6733 ret = ocfs2_iterate_xattr_buckets(args->
reflink->old_inode, blkno, len,
6734 ocfs2_calc_value_tree_metas, &metas);
6755 (metas.
num_recs + ocfs2_refcount_recs_per_rb(osb->
sb) - 1) /
6756 ocfs2_refcount_recs_per_rb(osb->
sb) * 2;
6768 if (num_free_extents < 0) {
6769 ret = num_free_extents;
6774 if (num_free_extents < len)
6777 *credits += ocfs2_calc_extend_credits(osb->
sb,
6805 static int ocfs2_reflink_xattr_bucket(handle_t *handle,
6807 u32 *cpos,
int num_buckets,
6819 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6820 ret = ocfs2_read_xattr_bucket(args->
old_bucket, blkno);
6826 ret = ocfs2_init_xattr_bucket(args->
new_bucket, new_blkno);
6832 ret = ocfs2_xattr_bucket_journal_access(handle,
6840 for (j = 0; j < bpb; j++)
6852 xh_entries[0].xe_name_hash);
6857 ocfs2_xattr_bucket_journal_dirty(handle, args->
new_bucket);
6859 ret = ocfs2_reflink_xattr_header(handle, args->
reflink,
6865 ocfs2_get_reflink_xattr_value_root,
6877 ret = ocfs2_xattr_bucket_journal_access(handle,
6885 ocfs2_xattr_bucket_journal_dirty(handle, args->
new_bucket);
6896 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6897 struct inode *inode,
6904 int ret, first_inserted = 0;
6905 u32 p_cluster, num_clusters, reflink_cpos = 0;
6907 unsigned int num_buckets, reflink_buckets;
6909 ocfs2_xattr_buckets_per_cluster(
OCFS2_SB(inode->
i_sb));
6911 ret = ocfs2_read_xattr_bucket(args->
old_bucket, blkno);
6919 while (len && num_buckets) {
6921 1, &p_cluster, &num_clusters);
6927 new_blkno = ocfs2_clusters_to_blocks(inode->
i_sb, p_cluster);
6928 reflink_buckets =
min(num_buckets, bpc * num_clusters);
6930 ret = ocfs2_reflink_xattr_bucket(handle, blkno,
6931 new_blkno, num_clusters,
6932 &reflink_cpos, reflink_buckets,
6933 meta_ac, data_ac, args);
6944 if (!first_inserted) {
6945 reflink_cpos = cpos;
6949 num_clusters, 0, meta_ac);
6953 trace_ocfs2_reflink_xattr_buckets((
unsigned long long)new_blkno,
6954 num_clusters, reflink_cpos);
6956 len -= num_clusters;
6957 blkno += ocfs2_clusters_to_blocks(inode->
i_sb, num_clusters);
6958 num_buckets -= reflink_buckets;
6967 static int ocfs2_reflink_xattr_rec(
struct inode *inode,
6968 struct buffer_head *root_bh,
6974 int ret, credits = 0;
6983 trace_ocfs2_reflink_xattr_rec((
unsigned long long)blkno, len);
6986 INODE_CACHE(args->
reflink->new_inode),
6989 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6991 &meta_ac, &data_ac);
6998 if (IS_ERR(handle)) {
6999 ret = PTR_ERR(handle);
7004 ret = ocfs2_reflink_xattr_buckets(handle, inode, args, &et,
7026 struct buffer_head *blk_bh,
7027 struct buffer_head *new_blk_bh)
7032 memset(¶, 0,
sizeof(para));
7033 para.reflink = args;
7034 para.old_blk_bh = blk_bh;
7037 para.old_bucket = ocfs2_xattr_bucket_new(args->
old_inode);
7038 if (!para.old_bucket) {
7043 para.new_bucket = ocfs2_xattr_bucket_new(args->
new_inode);
7044 if (!para.new_bucket) {
7050 ret = ocfs2_iterate_xattr_index_block(args->
old_inode, blk_bh,
7051 ocfs2_reflink_xattr_rec,
7057 ocfs2_xattr_bucket_free(para.old_bucket);
7058 ocfs2_xattr_bucket_free(para.new_bucket);
7063 struct buffer_head *blk_bh)
7065 int ret, indexed = 0;
7066 struct buffer_head *new_blk_bh =
NULL;
7075 &new_blk_bh, indexed);
7082 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7084 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7095 int type = ocfs2_xattr_get_type(xe);
7103 struct buffer_head *old_bh,
7105 struct buffer_head *new_bh,
7106 bool preserve_security)
7112 struct buffer_head *blk_bh =
NULL;
7115 struct buffer_head *ref_root_bh =
NULL;
7119 1, &ref_tree, &ref_root_bh);
7125 ocfs2_init_dealloc_ctxt(&dealloc);
7134 if (preserve_security)
7140 ret = ocfs2_reflink_xattr_inline(&args);
7157 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7166 brelse(ref_root_bh);
7168 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7185 struct inode *inode,
7189 struct buffer_head *dir_bh =
NULL;
7216 size_t list_size,
const char *name,
7217 size_t name_len,
int type)
7220 const size_t total_len = prefix_len + name_len + 1;
7222 if (list && total_len <= list_size) {
7224 memcpy(list + prefix_len, name, name_len);
7225 list[prefix_len +
name_len] =
'\0';
7230 static int ocfs2_xattr_security_get(
struct dentry *dentry,
const char *name,
7231 void *buffer,
size_t size,
int type)
7233 if (
strcmp(name,
"") == 0)
7236 name, buffer, size);
7239 static int ocfs2_xattr_security_set(
struct dentry *dentry,
const char *name,
7240 const void *value,
size_t size,
int flags,
int type)
7242 if (
strcmp(name,
"") == 0)
7246 name, value, size, flags);
7255 for (xattr = xattr_array; xattr->
name !=
NULL; xattr++) {
7283 struct inode *inode,
7284 struct buffer_head *di_bh,
7297 .list = ocfs2_xattr_security_list,
7298 .get = ocfs2_xattr_security_get,
7299 .set = ocfs2_xattr_security_set,
7305 static size_t ocfs2_xattr_trusted_list(
struct dentry *dentry,
char *list,
7306 size_t list_size,
const char *name,
7307 size_t name_len,
int type)
7310 const size_t total_len = prefix_len + name_len + 1;
7312 if (list && total_len <= list_size) {
7314 memcpy(list + prefix_len, name, name_len);
7315 list[prefix_len +
name_len] =
'\0';
7320 static int ocfs2_xattr_trusted_get(
struct dentry *dentry,
const char *name,
7321 void *buffer,
size_t size,
int type)
7323 if (
strcmp(name,
"") == 0)
7326 name, buffer, size);
7329 static int ocfs2_xattr_trusted_set(
struct dentry *dentry,
const char *name,
7330 const void *value,
size_t size,
int flags,
int type)
7332 if (
strcmp(name,
"") == 0)
7336 name, value, size, flags);
7341 .list = ocfs2_xattr_trusted_list,
7342 .get = ocfs2_xattr_trusted_get,
7343 .set = ocfs2_xattr_trusted_set,
7349 static size_t ocfs2_xattr_user_list(
struct dentry *dentry,
char *list,
7350 size_t list_size,
const char *name,
7351 size_t name_len,
int type)
7354 const size_t total_len = prefix_len + name_len + 1;
7360 if (list && total_len <= list_size) {
7362 memcpy(list + prefix_len, name, name_len);
7363 list[prefix_len +
name_len] =
'\0';
7368 static int ocfs2_xattr_user_get(
struct dentry *dentry,
const char *name,
7369 void *buffer,
size_t size,
int type)
7373 if (
strcmp(name,
"") == 0)
7381 static int ocfs2_xattr_user_set(
struct dentry *dentry,
const char *name,
7382 const void *value,
size_t size,
int flags,
int type)
7386 if (
strcmp(name,
"") == 0)
7392 name, value, size, flags);
7397 .list = ocfs2_xattr_user_list,
7398 .get = ocfs2_xattr_user_get,
7399 .set = ocfs2_xattr_user_set,