1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
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>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
39 #define MLOG_MASK_PREFIX ML_XATTR
40 #include <cluster/masklog.h>
53 #include "buffer_head_io.h"
58 struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
63 struct ocfs2_xattr_bucket {
64 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
67 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
68 #define OCFS2_XATTR_INLINE_SIZE 80
70 static struct ocfs2_xattr_def_value_root def_xv = {
71 .xv.xr_list.l_count = cpu_to_le16(1),
74 struct xattr_handler *ocfs2_xattr_handlers[] = {
75 &ocfs2_xattr_user_handler,
76 &ocfs2_xattr_trusted_handler,
80 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
81 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
82 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
85 struct ocfs2_xattr_info {
92 struct ocfs2_xattr_search {
93 struct buffer_head *inode_bh;
95 * xattr_bh point to the block buffer head which has extended attribute
96 * when extended attribute in inode, xattr_bh is equal to inode_bh.
98 struct buffer_head *xattr_bh;
99 struct ocfs2_xattr_header *header;
100 struct ocfs2_xattr_bucket bucket;
103 struct ocfs2_xattr_entry *here;
107 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
108 struct ocfs2_xattr_header *xh,
113 static int ocfs2_xattr_block_find(struct inode *inode,
116 struct ocfs2_xattr_search *xs);
117 static int ocfs2_xattr_index_block_find(struct inode *inode,
118 struct buffer_head *root_bh,
121 struct ocfs2_xattr_search *xs);
123 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
124 struct ocfs2_xattr_tree_root *xt,
128 static int ocfs2_xattr_create_index_block(struct inode *inode,
129 struct ocfs2_xattr_search *xs);
131 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
132 struct ocfs2_xattr_info *xi,
133 struct ocfs2_xattr_search *xs);
135 static int ocfs2_delete_xattr_index_block(struct inode *inode,
136 struct buffer_head *xb_bh);
138 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
140 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
143 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
145 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
148 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
150 u16 len = sb->s_blocksize -
151 offsetof(struct ocfs2_xattr_header, xh_entries);
153 return len / sizeof(struct ocfs2_xattr_entry);
156 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
157 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
158 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
160 static void ocfs2_xattr_bucket_relse(struct inode *inode,
161 struct ocfs2_xattr_bucket *bucket)
163 int i, blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
165 for (i = 0; i < blks; i++) {
166 brelse(bucket->bu_bhs[i]);
167 bucket->bu_bhs[i] = NULL;
172 * A bucket that has never been written to disk doesn't need to be
173 * read. We just need the buffer_heads. Don't call this for
174 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
177 static int ocfs2_init_xattr_bucket(struct inode *inode,
178 struct ocfs2_xattr_bucket *bucket,
182 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
184 for (i = 0; i < blks; i++) {
185 bucket->bu_bhs[i] = sb_getblk(inode->i_sb, xb_blkno + i);
186 if (!bucket->bu_bhs[i]) {
192 ocfs2_set_new_buffer_uptodate(inode, bucket->bu_bhs[i]);
196 ocfs2_xattr_bucket_relse(inode, bucket);
200 /* Read the xattr bucket at xb_blkno */
201 static int ocfs2_read_xattr_bucket(struct inode *inode,
202 struct ocfs2_xattr_bucket *bucket,
205 int rc, blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
207 rc = ocfs2_read_blocks(inode, xb_blkno, blks, bucket->bu_bhs, 0);
209 ocfs2_xattr_bucket_relse(inode, bucket);
213 static inline const char *ocfs2_xattr_prefix(int name_index)
215 struct xattr_handler *handler = NULL;
217 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
218 handler = ocfs2_xattr_handler_map[name_index];
220 return handler ? handler->prefix : NULL;
223 static u32 ocfs2_xattr_name_hash(struct inode *inode,
227 /* Get hash value of uuid from super block */
228 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
231 /* hash extended attribute name */
232 for (i = 0; i < name_len; i++) {
233 hash = (hash << OCFS2_HASH_SHIFT) ^
234 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
242 * ocfs2_xattr_hash_entry()
244 * Compute the hash of an extended attribute.
246 static void ocfs2_xattr_hash_entry(struct inode *inode,
247 struct ocfs2_xattr_header *header,
248 struct ocfs2_xattr_entry *entry)
251 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
253 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
254 entry->xe_name_hash = cpu_to_le32(hash);
259 static int ocfs2_xattr_extend_allocation(struct inode *inode,
261 struct buffer_head *xattr_bh,
262 struct ocfs2_xattr_value_root *xv)
265 int restart_func = 0;
267 handle_t *handle = NULL;
268 struct ocfs2_alloc_context *data_ac = NULL;
269 struct ocfs2_alloc_context *meta_ac = NULL;
270 enum ocfs2_alloc_restarted why;
271 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
272 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
273 struct ocfs2_extent_tree et;
275 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
277 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
281 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
288 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
290 handle = ocfs2_start_trans(osb, credits);
291 if (IS_ERR(handle)) {
292 status = PTR_ERR(handle);
298 restarted_transaction:
299 status = ocfs2_journal_access(handle, inode, xattr_bh,
300 OCFS2_JOURNAL_ACCESS_WRITE);
306 prev_clusters = le32_to_cpu(xv->xr_clusters);
307 status = ocfs2_add_clusters_in_btree(osb,
317 if ((status < 0) && (status != -EAGAIN)) {
318 if (status != -ENOSPC)
323 status = ocfs2_journal_dirty(handle, xattr_bh);
329 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
331 if (why != RESTART_NONE && clusters_to_add) {
332 if (why == RESTART_META) {
333 mlog(0, "restarting function.\n");
336 BUG_ON(why != RESTART_TRANS);
338 mlog(0, "restarting transaction.\n");
339 /* TODO: This can be more intelligent. */
340 credits = ocfs2_calc_extend_credits(osb->sb,
343 status = ocfs2_extend_trans(handle, credits);
345 /* handle still has to be committed at
351 goto restarted_transaction;
357 ocfs2_commit_trans(osb, handle);
361 ocfs2_free_alloc_context(data_ac);
365 ocfs2_free_alloc_context(meta_ac);
368 if ((!status) && restart_func) {
376 static int __ocfs2_remove_xattr_range(struct inode *inode,
377 struct buffer_head *root_bh,
378 struct ocfs2_xattr_value_root *xv,
379 u32 cpos, u32 phys_cpos, u32 len,
380 struct ocfs2_cached_dealloc_ctxt *dealloc)
383 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
384 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
385 struct inode *tl_inode = osb->osb_tl_inode;
387 struct ocfs2_alloc_context *meta_ac = NULL;
388 struct ocfs2_extent_tree et;
390 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
392 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
398 mutex_lock(&tl_inode->i_mutex);
400 if (ocfs2_truncate_log_needs_flush(osb)) {
401 ret = __ocfs2_flush_truncate_log(osb);
408 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
409 if (IS_ERR(handle)) {
410 ret = PTR_ERR(handle);
415 ret = ocfs2_journal_access(handle, inode, root_bh,
416 OCFS2_JOURNAL_ACCESS_WRITE);
422 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
429 le32_add_cpu(&xv->xr_clusters, -len);
431 ret = ocfs2_journal_dirty(handle, root_bh);
437 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
442 ocfs2_commit_trans(osb, handle);
444 mutex_unlock(&tl_inode->i_mutex);
447 ocfs2_free_alloc_context(meta_ac);
452 static int ocfs2_xattr_shrink_size(struct inode *inode,
455 struct buffer_head *root_bh,
456 struct ocfs2_xattr_value_root *xv)
459 u32 trunc_len, cpos, phys_cpos, alloc_size;
461 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
462 struct ocfs2_cached_dealloc_ctxt dealloc;
464 ocfs2_init_dealloc_ctxt(&dealloc);
466 if (old_clusters <= new_clusters)
470 trunc_len = old_clusters - new_clusters;
472 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
473 &alloc_size, &xv->xr_list);
479 if (alloc_size > trunc_len)
480 alloc_size = trunc_len;
482 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
483 phys_cpos, alloc_size,
490 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
491 ocfs2_remove_xattr_clusters_from_cache(inode, block,
494 trunc_len -= alloc_size;
498 ocfs2_schedule_truncate_log_flush(osb, 1);
499 ocfs2_run_deallocs(osb, &dealloc);
504 static int ocfs2_xattr_value_truncate(struct inode *inode,
505 struct buffer_head *root_bh,
506 struct ocfs2_xattr_value_root *xv,
510 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
511 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
513 if (new_clusters == old_clusters)
516 if (new_clusters > old_clusters)
517 ret = ocfs2_xattr_extend_allocation(inode,
518 new_clusters - old_clusters,
521 ret = ocfs2_xattr_shrink_size(inode,
522 old_clusters, new_clusters,
528 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
529 size_t *result, const char *prefix,
530 const char *name, int name_len)
532 char *p = buffer + *result;
533 int prefix_len = strlen(prefix);
534 int total_len = prefix_len + name_len + 1;
536 *result += total_len;
538 /* we are just looking for how big our buffer needs to be */
545 memcpy(p, prefix, prefix_len);
546 memcpy(p + prefix_len, name, name_len);
547 p[prefix_len + name_len] = '\0';
552 static int ocfs2_xattr_list_entries(struct inode *inode,
553 struct ocfs2_xattr_header *header,
554 char *buffer, size_t buffer_size)
558 const char *prefix, *name;
560 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
561 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
562 type = ocfs2_xattr_get_type(entry);
563 prefix = ocfs2_xattr_prefix(type);
566 name = (const char *)header +
567 le16_to_cpu(entry->xe_name_offset);
569 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
570 &result, prefix, name,
580 static int ocfs2_xattr_ibody_list(struct inode *inode,
581 struct ocfs2_dinode *di,
585 struct ocfs2_xattr_header *header = NULL;
586 struct ocfs2_inode_info *oi = OCFS2_I(inode);
589 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
592 header = (struct ocfs2_xattr_header *)
593 ((void *)di + inode->i_sb->s_blocksize -
594 le16_to_cpu(di->i_xattr_inline_size));
596 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
601 static int ocfs2_xattr_block_list(struct inode *inode,
602 struct ocfs2_dinode *di,
606 struct buffer_head *blk_bh = NULL;
607 struct ocfs2_xattr_block *xb;
610 if (!di->i_xattr_loc)
613 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
619 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
620 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
625 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
626 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
627 ret = ocfs2_xattr_list_entries(inode, header,
628 buffer, buffer_size);
630 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
631 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
632 buffer, buffer_size);
640 ssize_t ocfs2_listxattr(struct dentry *dentry,
644 int ret = 0, i_ret = 0, b_ret = 0;
645 struct buffer_head *di_bh = NULL;
646 struct ocfs2_dinode *di = NULL;
647 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
649 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
652 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
655 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
661 di = (struct ocfs2_dinode *)di_bh->b_data;
663 down_read(&oi->ip_xattr_sem);
664 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
672 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
677 up_read(&oi->ip_xattr_sem);
678 ocfs2_inode_unlock(dentry->d_inode, 0);
682 return i_ret + b_ret;
685 static int ocfs2_xattr_find_entry(int name_index,
687 struct ocfs2_xattr_search *xs)
689 struct ocfs2_xattr_entry *entry;
696 name_len = strlen(name);
698 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
699 cmp = name_index - ocfs2_xattr_get_type(entry);
701 cmp = name_len - entry->xe_name_len;
703 cmp = memcmp(name, (xs->base +
704 le16_to_cpu(entry->xe_name_offset)),
712 return cmp ? -ENODATA : 0;
715 static int ocfs2_xattr_get_value_outside(struct inode *inode,
716 struct ocfs2_xattr_value_root *xv,
720 u32 cpos, p_cluster, num_clusters, bpc, clusters;
723 size_t cplen, blocksize;
724 struct buffer_head *bh = NULL;
725 struct ocfs2_extent_list *el;
728 clusters = le32_to_cpu(xv->xr_clusters);
729 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
730 blocksize = inode->i_sb->s_blocksize;
733 while (cpos < clusters) {
734 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
741 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
742 /* Copy ocfs2_xattr_value */
743 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
744 ret = ocfs2_read_block(inode, blkno, &bh);
750 cplen = len >= blocksize ? blocksize : len;
751 memcpy(buffer, bh->b_data, cplen);
760 cpos += num_clusters;
766 static int ocfs2_xattr_ibody_get(struct inode *inode,
771 struct ocfs2_xattr_search *xs)
773 struct ocfs2_inode_info *oi = OCFS2_I(inode);
774 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
775 struct ocfs2_xattr_value_root *xv;
779 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
782 xs->end = (void *)di + inode->i_sb->s_blocksize;
783 xs->header = (struct ocfs2_xattr_header *)
784 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
785 xs->base = (void *)xs->header;
786 xs->here = xs->header->xh_entries;
788 ret = ocfs2_xattr_find_entry(name_index, name, xs);
791 size = le64_to_cpu(xs->here->xe_value_size);
793 if (size > buffer_size)
795 if (ocfs2_xattr_is_local(xs->here)) {
796 memcpy(buffer, (void *)xs->base +
797 le16_to_cpu(xs->here->xe_name_offset) +
798 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
800 xv = (struct ocfs2_xattr_value_root *)
801 (xs->base + le16_to_cpu(
802 xs->here->xe_name_offset) +
803 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
804 ret = ocfs2_xattr_get_value_outside(inode, xv,
816 static int ocfs2_xattr_block_get(struct inode *inode,
821 struct ocfs2_xattr_search *xs)
823 struct ocfs2_xattr_block *xb;
824 struct ocfs2_xattr_value_root *xv;
826 int ret = -ENODATA, name_offset, name_len, block_off, i;
828 memset(&xs->bucket, 0, sizeof(xs->bucket));
830 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
841 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
842 size = le64_to_cpu(xs->here->xe_value_size);
845 if (size > buffer_size)
848 name_offset = le16_to_cpu(xs->here->xe_name_offset);
849 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
850 i = xs->here - xs->header->xh_entries;
852 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
853 ret = ocfs2_xattr_bucket_get_name_value(inode,
854 bucket_xh(&xs->bucket),
858 xs->base = bucket_block(&xs->bucket, block_off);
860 if (ocfs2_xattr_is_local(xs->here)) {
861 memcpy(buffer, (void *)xs->base +
862 name_offset + name_len, size);
864 xv = (struct ocfs2_xattr_value_root *)
865 (xs->base + name_offset + name_len);
866 ret = ocfs2_xattr_get_value_outside(inode, xv,
876 ocfs2_xattr_bucket_relse(inode, &xs->bucket);
877 memset(&xs->bucket, 0, sizeof(xs->bucket));
879 brelse(xs->xattr_bh);
886 * Copy an extended attribute into the buffer provided.
887 * Buffer is NULL to compute the size of buffer required.
889 static int ocfs2_xattr_get(struct inode *inode,
896 struct ocfs2_dinode *di = NULL;
897 struct buffer_head *di_bh = NULL;
898 struct ocfs2_inode_info *oi = OCFS2_I(inode);
899 struct ocfs2_xattr_search xis = {
900 .not_found = -ENODATA,
902 struct ocfs2_xattr_search xbs = {
903 .not_found = -ENODATA,
906 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
909 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
912 ret = ocfs2_inode_lock(inode, &di_bh, 0);
917 xis.inode_bh = xbs.inode_bh = di_bh;
918 di = (struct ocfs2_dinode *)di_bh->b_data;
920 down_read(&oi->ip_xattr_sem);
921 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
923 if (ret == -ENODATA && di->i_xattr_loc)
924 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
926 up_read(&oi->ip_xattr_sem);
927 ocfs2_inode_unlock(inode, 0);
934 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
935 struct ocfs2_xattr_value_root *xv,
939 int ret = 0, i, cp_len, credits;
940 u16 blocksize = inode->i_sb->s_blocksize;
941 u32 p_cluster, num_clusters;
942 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
943 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
945 struct buffer_head *bh = NULL;
948 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
950 credits = clusters * bpc;
951 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
952 if (IS_ERR(handle)) {
953 ret = PTR_ERR(handle);
958 while (cpos < clusters) {
959 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
960 &num_clusters, &xv->xr_list);
966 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
968 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
969 ret = ocfs2_read_block(inode, blkno, &bh);
975 ret = ocfs2_journal_access(handle,
978 OCFS2_JOURNAL_ACCESS_WRITE);
984 cp_len = value_len > blocksize ? blocksize : value_len;
985 memcpy(bh->b_data, value, cp_len);
988 if (cp_len < blocksize)
989 memset(bh->b_data + cp_len, 0,
992 ret = ocfs2_journal_dirty(handle, bh);
1001 * XXX: do we need to empty all the following
1002 * blocks in this cluster?
1007 cpos += num_clusters;
1010 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1017 static int ocfs2_xattr_cleanup(struct inode *inode,
1018 struct ocfs2_xattr_info *xi,
1019 struct ocfs2_xattr_search *xs,
1022 handle_t *handle = NULL;
1024 size_t name_len = strlen(xi->name);
1025 void *val = xs->base + offs;
1026 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1028 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1029 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1030 if (IS_ERR(handle)) {
1031 ret = PTR_ERR(handle);
1035 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1036 OCFS2_JOURNAL_ACCESS_WRITE);
1041 /* Decrease xattr count */
1042 le16_add_cpu(&xs->header->xh_count, -1);
1043 /* Remove the xattr entry and tree root which has already be set*/
1044 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1045 memset(val, 0, size);
1047 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1051 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1056 static int ocfs2_xattr_update_entry(struct inode *inode,
1057 struct ocfs2_xattr_info *xi,
1058 struct ocfs2_xattr_search *xs,
1061 handle_t *handle = NULL;
1064 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1065 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1066 if (IS_ERR(handle)) {
1067 ret = PTR_ERR(handle);
1071 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1072 OCFS2_JOURNAL_ACCESS_WRITE);
1078 xs->here->xe_name_offset = cpu_to_le16(offs);
1079 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1080 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1081 ocfs2_xattr_set_local(xs->here, 1);
1083 ocfs2_xattr_set_local(xs->here, 0);
1084 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1086 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1090 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1096 * ocfs2_xattr_set_value_outside()
1098 * Set large size value in B tree.
1100 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1101 struct ocfs2_xattr_info *xi,
1102 struct ocfs2_xattr_search *xs,
1105 size_t name_len = strlen(xi->name);
1106 void *val = xs->base + offs;
1107 struct ocfs2_xattr_value_root *xv = NULL;
1108 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1111 memset(val, 0, size);
1112 memcpy(val, xi->name, name_len);
1113 xv = (struct ocfs2_xattr_value_root *)
1114 (val + OCFS2_XATTR_SIZE(name_len));
1115 xv->xr_clusters = 0;
1116 xv->xr_last_eb_blk = 0;
1117 xv->xr_list.l_tree_depth = 0;
1118 xv->xr_list.l_count = cpu_to_le16(1);
1119 xv->xr_list.l_next_free_rec = 0;
1121 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1127 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1133 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1141 * ocfs2_xattr_set_entry_local()
1143 * Set, replace or remove extended attribute in local.
1145 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1146 struct ocfs2_xattr_info *xi,
1147 struct ocfs2_xattr_search *xs,
1148 struct ocfs2_xattr_entry *last,
1151 size_t name_len = strlen(xi->name);
1154 if (xi->value && xs->not_found) {
1155 /* Insert the new xattr entry. */
1156 le16_add_cpu(&xs->header->xh_count, 1);
1157 ocfs2_xattr_set_type(last, xi->name_index);
1158 ocfs2_xattr_set_local(last, 1);
1159 last->xe_name_len = name_len;
1165 first_val = xs->base + min_offs;
1166 offs = le16_to_cpu(xs->here->xe_name_offset);
1167 val = xs->base + offs;
1169 if (le64_to_cpu(xs->here->xe_value_size) >
1170 OCFS2_XATTR_INLINE_SIZE)
1171 size = OCFS2_XATTR_SIZE(name_len) +
1172 OCFS2_XATTR_ROOT_SIZE;
1174 size = OCFS2_XATTR_SIZE(name_len) +
1175 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1177 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1178 OCFS2_XATTR_SIZE(xi->value_len)) {
1179 /* The old and the new value have the
1180 same size. Just replace the value. */
1181 ocfs2_xattr_set_local(xs->here, 1);
1182 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1183 /* Clear value bytes. */
1184 memset(val + OCFS2_XATTR_SIZE(name_len),
1186 OCFS2_XATTR_SIZE(xi->value_len));
1187 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1192 /* Remove the old name+value. */
1193 memmove(first_val + size, first_val, val - first_val);
1194 memset(first_val, 0, size);
1195 xs->here->xe_name_hash = 0;
1196 xs->here->xe_name_offset = 0;
1197 ocfs2_xattr_set_local(xs->here, 1);
1198 xs->here->xe_value_size = 0;
1202 /* Adjust all value offsets. */
1203 last = xs->header->xh_entries;
1204 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1205 size_t o = le16_to_cpu(last->xe_name_offset);
1208 last->xe_name_offset = cpu_to_le16(o + size);
1213 /* Remove the old entry. */
1215 memmove(xs->here, xs->here + 1,
1216 (void *)last - (void *)xs->here);
1217 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1218 le16_add_cpu(&xs->header->xh_count, -1);
1222 /* Insert the new name+value. */
1223 size_t size = OCFS2_XATTR_SIZE(name_len) +
1224 OCFS2_XATTR_SIZE(xi->value_len);
1225 void *val = xs->base + min_offs - size;
1227 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1228 memset(val, 0, size);
1229 memcpy(val, xi->name, name_len);
1230 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1233 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1234 ocfs2_xattr_set_local(xs->here, 1);
1235 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1242 * ocfs2_xattr_set_entry()
1244 * Set extended attribute entry into inode or block.
1246 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1247 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1248 * then set value in B tree with set_value_outside().
1250 static int ocfs2_xattr_set_entry(struct inode *inode,
1251 struct ocfs2_xattr_info *xi,
1252 struct ocfs2_xattr_search *xs,
1255 struct ocfs2_xattr_entry *last;
1256 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1257 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1258 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1260 handle_t *handle = NULL;
1262 struct ocfs2_xattr_info xi_l = {
1263 .name_index = xi->name_index,
1266 .value_len = xi->value_len,
1269 /* Compute min_offs, last and free space. */
1270 last = xs->header->xh_entries;
1272 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1273 size_t offs = le16_to_cpu(last->xe_name_offset);
1274 if (offs < min_offs)
1279 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1283 if (!xs->not_found) {
1285 if (ocfs2_xattr_is_local(xs->here))
1286 size = OCFS2_XATTR_SIZE(name_len) +
1287 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1289 size = OCFS2_XATTR_SIZE(name_len) +
1290 OCFS2_XATTR_ROOT_SIZE;
1291 free += (size + sizeof(struct ocfs2_xattr_entry));
1293 /* Check free space in inode or block */
1294 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1295 if (free < sizeof(struct ocfs2_xattr_entry) +
1296 OCFS2_XATTR_SIZE(name_len) +
1297 OCFS2_XATTR_ROOT_SIZE) {
1301 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1302 xi_l.value = (void *)&def_xv;
1303 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1304 } else if (xi->value) {
1305 if (free < sizeof(struct ocfs2_xattr_entry) +
1306 OCFS2_XATTR_SIZE(name_len) +
1307 OCFS2_XATTR_SIZE(xi->value_len)) {
1313 if (!xs->not_found) {
1314 /* For existing extended attribute */
1315 size_t size = OCFS2_XATTR_SIZE(name_len) +
1316 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1317 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1318 void *val = xs->base + offs;
1320 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1321 /* Replace existing local xattr with tree root */
1322 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1327 } else if (!ocfs2_xattr_is_local(xs->here)) {
1328 /* For existing xattr which has value outside */
1329 struct ocfs2_xattr_value_root *xv = NULL;
1330 xv = (struct ocfs2_xattr_value_root *)(val +
1331 OCFS2_XATTR_SIZE(name_len));
1333 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1335 * If new value need set outside also,
1336 * first truncate old value to new value,
1337 * then set new value with set_value_outside().
1339 ret = ocfs2_xattr_value_truncate(inode,
1348 ret = __ocfs2_xattr_set_value_outside(inode,
1357 ret = ocfs2_xattr_update_entry(inode,
1366 * If new value need set in local,
1367 * just trucate old value to zero.
1369 ret = ocfs2_xattr_value_truncate(inode,
1379 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1380 OCFS2_INODE_UPDATE_CREDITS);
1381 if (IS_ERR(handle)) {
1382 ret = PTR_ERR(handle);
1387 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1388 OCFS2_JOURNAL_ACCESS_WRITE);
1394 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1395 /* set extended attribute in external block. */
1396 ret = ocfs2_extend_trans(handle,
1397 OCFS2_INODE_UPDATE_CREDITS +
1398 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1403 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1404 OCFS2_JOURNAL_ACCESS_WRITE);
1412 * Set value in local, include set tree root in local.
1413 * This is the first step for value size >INLINE_SIZE.
1415 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1417 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1418 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1425 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1426 (flag & OCFS2_INLINE_XATTR_FL)) {
1427 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1428 unsigned int xattrsize = osb->s_xattr_inline_size;
1431 * Adjust extent record count or inline data size
1432 * to reserve space for extended attribute.
1434 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1435 struct ocfs2_inline_data *idata = &di->id2.i_data;
1436 le16_add_cpu(&idata->id_count, -xattrsize);
1437 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1438 struct ocfs2_extent_list *el = &di->id2.i_list;
1439 le16_add_cpu(&el->l_count, -(xattrsize /
1440 sizeof(struct ocfs2_extent_rec)));
1442 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1444 /* Update xattr flag */
1445 spin_lock(&oi->ip_lock);
1446 oi->ip_dyn_features |= flag;
1447 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1448 spin_unlock(&oi->ip_lock);
1449 /* Update inode ctime */
1450 inode->i_ctime = CURRENT_TIME;
1451 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1452 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1454 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1459 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1461 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1463 * Set value outside in B tree.
1464 * This is the second step for value size > INLINE_SIZE.
1466 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1467 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1473 * If set value outside failed, we have to clean
1474 * the junk tree root we have already set in local.
1476 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1486 static int ocfs2_remove_value_outside(struct inode*inode,
1487 struct buffer_head *bh,
1488 struct ocfs2_xattr_header *header)
1492 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1493 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1495 if (!ocfs2_xattr_is_local(entry)) {
1496 struct ocfs2_xattr_value_root *xv;
1499 val = (void *)header +
1500 le16_to_cpu(entry->xe_name_offset);
1501 xv = (struct ocfs2_xattr_value_root *)
1502 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1503 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1514 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1515 struct buffer_head *di_bh)
1518 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1519 struct ocfs2_xattr_header *header;
1522 header = (struct ocfs2_xattr_header *)
1523 ((void *)di + inode->i_sb->s_blocksize -
1524 le16_to_cpu(di->i_xattr_inline_size));
1526 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1531 static int ocfs2_xattr_block_remove(struct inode *inode,
1532 struct buffer_head *blk_bh)
1534 struct ocfs2_xattr_block *xb;
1537 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1538 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1539 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1540 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1542 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1547 static int ocfs2_xattr_free_block(struct inode *inode,
1550 struct inode *xb_alloc_inode;
1551 struct buffer_head *xb_alloc_bh = NULL;
1552 struct buffer_head *blk_bh = NULL;
1553 struct ocfs2_xattr_block *xb;
1554 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1560 ret = ocfs2_read_block(inode, block, &blk_bh);
1566 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1567 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1572 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1578 blk = le64_to_cpu(xb->xb_blkno);
1579 bit = le16_to_cpu(xb->xb_suballoc_bit);
1580 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1582 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1583 EXTENT_ALLOC_SYSTEM_INODE,
1584 le16_to_cpu(xb->xb_suballoc_slot));
1585 if (!xb_alloc_inode) {
1590 mutex_lock(&xb_alloc_inode->i_mutex);
1592 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1598 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1599 if (IS_ERR(handle)) {
1600 ret = PTR_ERR(handle);
1605 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1610 ocfs2_commit_trans(osb, handle);
1612 ocfs2_inode_unlock(xb_alloc_inode, 1);
1613 brelse(xb_alloc_bh);
1615 mutex_unlock(&xb_alloc_inode->i_mutex);
1616 iput(xb_alloc_inode);
1623 * ocfs2_xattr_remove()
1625 * Free extended attribute resources associated with this inode.
1627 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1629 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1630 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1634 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1637 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1640 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1641 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1648 if (di->i_xattr_loc) {
1649 ret = ocfs2_xattr_free_block(inode,
1650 le64_to_cpu(di->i_xattr_loc));
1657 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1658 OCFS2_INODE_UPDATE_CREDITS);
1659 if (IS_ERR(handle)) {
1660 ret = PTR_ERR(handle);
1664 ret = ocfs2_journal_access(handle, inode, di_bh,
1665 OCFS2_JOURNAL_ACCESS_WRITE);
1671 di->i_xattr_loc = 0;
1673 spin_lock(&oi->ip_lock);
1674 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1675 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1676 spin_unlock(&oi->ip_lock);
1678 ret = ocfs2_journal_dirty(handle, di_bh);
1682 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1687 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1688 struct ocfs2_dinode *di)
1690 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1691 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1694 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1697 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1698 struct ocfs2_inline_data *idata = &di->id2.i_data;
1699 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1700 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1701 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1702 le64_to_cpu(di->i_size);
1704 struct ocfs2_extent_list *el = &di->id2.i_list;
1705 free = (le16_to_cpu(el->l_count) -
1706 le16_to_cpu(el->l_next_free_rec)) *
1707 sizeof(struct ocfs2_extent_rec);
1709 if (free >= xattrsize)
1716 * ocfs2_xattr_ibody_find()
1718 * Find extended attribute in inode block and
1719 * fill search info into struct ocfs2_xattr_search.
1721 static int ocfs2_xattr_ibody_find(struct inode *inode,
1724 struct ocfs2_xattr_search *xs)
1726 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1727 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1731 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1734 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1735 down_read(&oi->ip_alloc_sem);
1736 has_space = ocfs2_xattr_has_space_inline(inode, di);
1737 up_read(&oi->ip_alloc_sem);
1742 xs->xattr_bh = xs->inode_bh;
1743 xs->end = (void *)di + inode->i_sb->s_blocksize;
1744 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1745 xs->header = (struct ocfs2_xattr_header *)
1746 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1748 xs->header = (struct ocfs2_xattr_header *)
1749 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1750 xs->base = (void *)xs->header;
1751 xs->here = xs->header->xh_entries;
1753 /* Find the named attribute. */
1754 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1755 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1756 if (ret && ret != -ENODATA)
1758 xs->not_found = ret;
1765 * ocfs2_xattr_ibody_set()
1767 * Set, replace or remove an extended attribute into inode block.
1770 static int ocfs2_xattr_ibody_set(struct inode *inode,
1771 struct ocfs2_xattr_info *xi,
1772 struct ocfs2_xattr_search *xs)
1774 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1775 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1778 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1781 down_write(&oi->ip_alloc_sem);
1782 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1783 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1789 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1790 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1792 up_write(&oi->ip_alloc_sem);
1798 * ocfs2_xattr_block_find()
1800 * Find extended attribute in external block and
1801 * fill search info into struct ocfs2_xattr_search.
1803 static int ocfs2_xattr_block_find(struct inode *inode,
1806 struct ocfs2_xattr_search *xs)
1808 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1809 struct buffer_head *blk_bh = NULL;
1810 struct ocfs2_xattr_block *xb;
1813 if (!di->i_xattr_loc)
1816 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1822 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1823 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1828 xs->xattr_bh = blk_bh;
1830 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1831 xs->header = &xb->xb_attrs.xb_header;
1832 xs->base = (void *)xs->header;
1833 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1834 xs->here = xs->header->xh_entries;
1836 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1838 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1842 if (ret && ret != -ENODATA) {
1843 xs->xattr_bh = NULL;
1846 xs->not_found = ret;
1855 * ocfs2_xattr_block_set()
1857 * Set, replace or remove an extended attribute into external block.
1860 static int ocfs2_xattr_block_set(struct inode *inode,
1861 struct ocfs2_xattr_info *xi,
1862 struct ocfs2_xattr_search *xs)
1864 struct buffer_head *new_bh = NULL;
1865 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1866 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1867 struct ocfs2_alloc_context *meta_ac = NULL;
1868 handle_t *handle = NULL;
1869 struct ocfs2_xattr_block *xblk = NULL;
1870 u16 suballoc_bit_start;
1875 if (!xs->xattr_bh) {
1877 * Alloc one external block for extended attribute
1880 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1885 handle = ocfs2_start_trans(osb,
1886 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1887 if (IS_ERR(handle)) {
1888 ret = PTR_ERR(handle);
1892 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1893 OCFS2_JOURNAL_ACCESS_CREATE);
1899 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1900 &suballoc_bit_start, &num_got,
1907 new_bh = sb_getblk(inode->i_sb, first_blkno);
1908 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1910 ret = ocfs2_journal_access(handle, inode, new_bh,
1911 OCFS2_JOURNAL_ACCESS_CREATE);
1917 /* Initialize ocfs2_xattr_block */
1918 xs->xattr_bh = new_bh;
1919 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1920 memset(xblk, 0, inode->i_sb->s_blocksize);
1921 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1922 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1923 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1924 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1925 xblk->xb_blkno = cpu_to_le64(first_blkno);
1927 xs->header = &xblk->xb_attrs.xb_header;
1928 xs->base = (void *)xs->header;
1929 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1930 xs->here = xs->header->xh_entries;
1933 ret = ocfs2_journal_dirty(handle, new_bh);
1938 di->i_xattr_loc = cpu_to_le64(first_blkno);
1939 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1943 ocfs2_commit_trans(osb, handle);
1946 ocfs2_free_alloc_context(meta_ac);
1950 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1952 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1953 /* Set extended attribute into external block */
1954 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1955 if (!ret || ret != -ENOSPC)
1958 ret = ocfs2_xattr_create_index_block(inode, xs);
1963 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1973 * Set, replace or remove an extended attribute for this inode.
1974 * value is NULL to remove an existing extended attribute, else either
1975 * create or replace an extended attribute.
1977 int ocfs2_xattr_set(struct inode *inode,
1984 struct buffer_head *di_bh = NULL;
1985 struct ocfs2_dinode *di;
1988 struct ocfs2_xattr_info xi = {
1989 .name_index = name_index,
1992 .value_len = value_len,
1995 struct ocfs2_xattr_search xis = {
1996 .not_found = -ENODATA,
1999 struct ocfs2_xattr_search xbs = {
2000 .not_found = -ENODATA,
2003 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2006 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2011 xis.inode_bh = xbs.inode_bh = di_bh;
2012 di = (struct ocfs2_dinode *)di_bh->b_data;
2014 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2016 * Scan inode and external block to find the same name
2017 * extended attribute and collect search infomation.
2019 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2022 if (xis.not_found) {
2023 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2028 if (xis.not_found && xbs.not_found) {
2030 if (flags & XATTR_REPLACE)
2037 if (flags & XATTR_CREATE)
2042 /* Remove existing extended attribute */
2044 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2045 else if (!xbs.not_found)
2046 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2048 /* We always try to set extended attribute into inode first*/
2049 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2050 if (!ret && !xbs.not_found) {
2052 * If succeed and that extended attribute existing in
2053 * external block, then we will remove it.
2057 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2058 } else if (ret == -ENOSPC) {
2059 if (di->i_xattr_loc && !xbs.xattr_bh) {
2060 ret = ocfs2_xattr_block_find(inode, name_index,
2066 * If no space in inode, we will set extended attribute
2067 * into external block.
2069 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2072 if (!xis.not_found) {
2074 * If succeed and that extended attribute
2075 * existing in inode, we will remove it.
2079 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2084 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2085 ocfs2_inode_unlock(inode, 1);
2087 brelse(xbs.xattr_bh);
2088 ocfs2_xattr_bucket_relse(inode, &xbs.bucket);
2094 * Find the xattr extent rec which may contains name_hash.
2095 * e_cpos will be the first name hash of the xattr rec.
2096 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2098 static int ocfs2_xattr_get_rec(struct inode *inode,
2103 struct ocfs2_extent_list *el)
2106 struct buffer_head *eb_bh = NULL;
2107 struct ocfs2_extent_block *eb;
2108 struct ocfs2_extent_rec *rec = NULL;
2111 if (el->l_tree_depth) {
2112 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2118 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2121 if (el->l_tree_depth) {
2122 ocfs2_error(inode->i_sb,
2123 "Inode %lu has non zero tree depth in "
2124 "xattr tree block %llu\n", inode->i_ino,
2125 (unsigned long long)eb_bh->b_blocknr);
2131 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2132 rec = &el->l_recs[i];
2134 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2135 e_blkno = le64_to_cpu(rec->e_blkno);
2141 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2142 "record (%u, %u, 0) in xattr", inode->i_ino,
2143 le32_to_cpu(rec->e_cpos),
2144 ocfs2_rec_clusters(el, rec));
2149 *p_blkno = le64_to_cpu(rec->e_blkno);
2150 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2152 *e_cpos = le32_to_cpu(rec->e_cpos);
2158 typedef int (xattr_bucket_func)(struct inode *inode,
2159 struct ocfs2_xattr_bucket *bucket,
2162 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2163 struct buffer_head *header_bh,
2170 int i, ret = 0, cmp = 1, block_off, new_offset;
2171 struct ocfs2_xattr_header *xh =
2172 (struct ocfs2_xattr_header *)header_bh->b_data;
2173 size_t name_len = strlen(name);
2174 struct ocfs2_xattr_entry *xe = NULL;
2175 struct buffer_head *name_bh = NULL;
2179 * We don't use binary search in the bucket because there
2180 * may be multiple entries with the same name hash.
2182 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2183 xe = &xh->xh_entries[i];
2185 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2187 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2190 cmp = name_index - ocfs2_xattr_get_type(xe);
2192 cmp = name_len - xe->xe_name_len;
2196 ret = ocfs2_xattr_bucket_get_name_value(inode,
2206 ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
2212 xe_name = name_bh->b_data + new_offset;
2214 cmp = memcmp(name, xe_name, name_len);
2230 * Find the specified xattr entry in a series of buckets.
2231 * This series start from p_blkno and last for num_clusters.
2232 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2233 * the num of the valid buckets.
2235 * Return the buffer_head this xattr should reside in. And if the xattr's
2236 * hash is in the gap of 2 buckets, return the lower bucket.
2238 static int ocfs2_xattr_bucket_find(struct inode *inode,
2245 struct ocfs2_xattr_search *xs)
2248 struct buffer_head *bh = NULL;
2249 struct buffer_head *lower_bh = NULL;
2250 struct ocfs2_xattr_header *xh = NULL;
2251 struct ocfs2_xattr_entry *xe = NULL;
2253 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2254 int low_bucket = 0, bucket, high_bucket;
2258 ret = ocfs2_read_block(inode, p_blkno, &bh);
2264 xh = (struct ocfs2_xattr_header *)bh->b_data;
2265 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2267 while (low_bucket <= high_bucket) {
2270 bucket = (low_bucket + high_bucket) / 2;
2272 blkno = p_blkno + bucket * blk_per_bucket;
2274 ret = ocfs2_read_block(inode, blkno, &bh);
2280 xh = (struct ocfs2_xattr_header *)bh->b_data;
2281 xe = &xh->xh_entries[0];
2282 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2283 high_bucket = bucket - 1;
2288 * Check whether the hash of the last entry in our
2289 * bucket is larger than the search one. for an empty
2290 * bucket, the last one is also the first one.
2293 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2295 last_hash = le32_to_cpu(xe->xe_name_hash);
2297 /* record lower_bh which may be the insert place. */
2302 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2303 low_bucket = bucket + 1;
2307 /* the searched xattr should reside in this bucket if exists. */
2308 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2309 name_index, name, name_hash,
2319 * Record the bucket we have found.
2320 * When the xattr's hash value is in the gap of 2 buckets, we will
2321 * always set it to the previous bucket.
2325 * We can't find any bucket whose first name_hash is less
2326 * than the find name_hash.
2328 BUG_ON(bh->b_blocknr != p_blkno);
2332 xs->bucket.bu_bhs[0] = lower_bh;
2335 xs->header = bucket_xh(&xs->bucket);
2336 xs->base = bucket_block(&xs->bucket, 0);
2337 xs->end = xs->base + inode->i_sb->s_blocksize;
2341 * If we have found the xattr enty, read all the blocks in
2344 ret = ocfs2_read_blocks(inode, bucket_blkno(&xs->bucket) + 1,
2345 blk_per_bucket - 1, &xs->bucket.bu_bhs[1],
2352 xs->here = &xs->header->xh_entries[index];
2353 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2354 (unsigned long long)bucket_blkno(&xs->bucket), index);
2364 static int ocfs2_xattr_index_block_find(struct inode *inode,
2365 struct buffer_head *root_bh,
2368 struct ocfs2_xattr_search *xs)
2371 struct ocfs2_xattr_block *xb =
2372 (struct ocfs2_xattr_block *)root_bh->b_data;
2373 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2374 struct ocfs2_extent_list *el = &xb_root->xt_list;
2376 u32 first_hash, num_clusters = 0;
2377 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2379 if (le16_to_cpu(el->l_next_free_rec) == 0)
2382 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2383 name, name_hash, name_index);
2385 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2392 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2394 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2395 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
2398 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2399 p_blkno, first_hash, num_clusters, xs);
2405 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2408 xattr_bucket_func *func,
2412 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2413 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2414 u32 num_buckets = clusters * bpc;
2415 struct ocfs2_xattr_bucket bucket;
2417 memset(&bucket, 0, sizeof(bucket));
2419 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2420 clusters, (unsigned long long)blkno);
2422 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2423 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
2431 * The real bucket num in this series of blocks is stored
2432 * in the 1st bucket.
2435 num_buckets = le16_to_cpu(bucket_xh(&bucket)->xh_num_buckets);
2437 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2438 (unsigned long long)blkno,
2439 le32_to_cpu(bucket_xh(&bucket)->xh_entries[0].xe_name_hash));
2441 ret = func(inode, &bucket, para);
2448 ocfs2_xattr_bucket_relse(inode, &bucket);
2449 memset(&bucket, 0, sizeof(bucket));
2453 ocfs2_xattr_bucket_relse(inode, &bucket);
2458 struct ocfs2_xattr_tree_list {
2464 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2465 struct ocfs2_xattr_header *xh,
2472 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2475 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2477 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2478 *new_offset = name_offset % inode->i_sb->s_blocksize;
2483 static int ocfs2_list_xattr_bucket(struct inode *inode,
2484 struct ocfs2_xattr_bucket *bucket,
2488 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2489 int i, block_off, new_offset;
2490 const char *prefix, *name;
2492 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
2493 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
2494 type = ocfs2_xattr_get_type(entry);
2495 prefix = ocfs2_xattr_prefix(type);
2498 ret = ocfs2_xattr_bucket_get_name_value(inode,
2506 name = (const char *)bucket_block(bucket, block_off) +
2508 ret = ocfs2_xattr_list_entry(xl->buffer,
2512 entry->xe_name_len);
2521 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2522 struct ocfs2_xattr_tree_root *xt,
2526 struct ocfs2_extent_list *el = &xt->xt_list;
2528 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2530 struct ocfs2_xattr_tree_list xl = {
2532 .buffer_size = buffer_size,
2536 if (le16_to_cpu(el->l_next_free_rec) == 0)
2539 while (name_hash > 0) {
2540 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2541 &e_cpos, &num_clusters, el);
2547 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2548 ocfs2_list_xattr_bucket,
2558 name_hash = e_cpos - 1;
2566 static int cmp_xe(const void *a, const void *b)
2568 const struct ocfs2_xattr_entry *l = a, *r = b;
2569 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2570 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2572 if (l_hash > r_hash)
2574 if (l_hash < r_hash)
2579 static void swap_xe(void *a, void *b, int size)
2581 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2584 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2585 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2589 * When the ocfs2_xattr_block is filled up, new bucket will be created
2590 * and all the xattr entries will be moved to the new bucket.
2591 * Note: we need to sort the entries since they are not saved in order
2592 * in the ocfs2_xattr_block.
2594 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2595 struct buffer_head *xb_bh,
2596 struct buffer_head *xh_bh,
2597 struct buffer_head *data_bh)
2599 int i, blocksize = inode->i_sb->s_blocksize;
2600 u16 offset, size, off_change;
2601 struct ocfs2_xattr_entry *xe;
2602 struct ocfs2_xattr_block *xb =
2603 (struct ocfs2_xattr_block *)xb_bh->b_data;
2604 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2605 struct ocfs2_xattr_header *xh =
2606 (struct ocfs2_xattr_header *)xh_bh->b_data;
2607 u16 count = le16_to_cpu(xb_xh->xh_count);
2608 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2610 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2611 (unsigned long long)xb_bh->b_blocknr,
2612 (unsigned long long)xh_bh->b_blocknr);
2614 memset(xh_bh->b_data, 0, blocksize);
2616 memset(data_bh->b_data, 0, blocksize);
2618 * Since the xe_name_offset is based on ocfs2_xattr_header,
2619 * there is a offset change corresponding to the change of
2620 * ocfs2_xattr_header's position.
2622 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2623 xe = &xb_xh->xh_entries[count - 1];
2624 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2625 size = blocksize - offset;
2627 /* copy all the names and values. */
2629 target = data_bh->b_data;
2630 memcpy(target + offset, src + offset, size);
2632 /* Init new header now. */
2633 xh->xh_count = xb_xh->xh_count;
2634 xh->xh_num_buckets = cpu_to_le16(1);
2635 xh->xh_name_value_len = cpu_to_le16(size);
2636 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2638 /* copy all the entries. */
2639 target = xh_bh->b_data;
2640 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2641 size = count * sizeof(struct ocfs2_xattr_entry);
2642 memcpy(target + offset, (char *)xb_xh + offset, size);
2644 /* Change the xe offset for all the xe because of the move. */
2645 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2646 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2647 for (i = 0; i < count; i++)
2648 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2650 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2651 offset, size, off_change);
2653 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2658 * After we move xattr from block to index btree, we have to
2659 * update ocfs2_xattr_search to the new xe and base.
2661 * When the entry is in xattr block, xattr_bh indicates the storage place.
2662 * While if the entry is in index b-tree, "bucket" indicates the
2663 * real place of the xattr.
2665 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2666 struct ocfs2_xattr_search *xs,
2667 struct buffer_head *old_bh,
2668 struct buffer_head *new_bh)
2671 char *buf = old_bh->b_data;
2672 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2673 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2674 int i, blocksize = inode->i_sb->s_blocksize;
2675 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2677 xs->bucket.bu_bhs[0] = new_bh;
2679 xs->header = bucket_xh(&xs->bucket);
2681 xs->base = new_bh->b_data;
2682 xs->end = xs->base + inode->i_sb->s_blocksize;
2684 if (!xs->not_found) {
2685 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2686 ret = ocfs2_read_blocks(inode,
2687 bucket_blkno(&xs->bucket) + 1,
2688 blk_per_bucket - 1, &xs->bucket.bu_bhs[1],
2696 i = xs->here - old_xh->xh_entries;
2697 xs->here = &xs->header->xh_entries[i];
2703 static int ocfs2_xattr_create_index_block(struct inode *inode,
2704 struct ocfs2_xattr_search *xs)
2706 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2710 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2711 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2712 struct ocfs2_alloc_context *data_ac;
2713 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2714 struct buffer_head *xb_bh = xs->xattr_bh;
2715 struct ocfs2_xattr_block *xb =
2716 (struct ocfs2_xattr_block *)xb_bh->b_data;
2717 struct ocfs2_xattr_tree_root *xr;
2718 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2719 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2721 mlog(0, "create xattr index block for %llu\n",
2722 (unsigned long long)xb_bh->b_blocknr);
2724 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2726 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2734 * We can use this lock for now, and maybe move to a dedicated mutex
2735 * if performance becomes a problem later.
2737 down_write(&oi->ip_alloc_sem);
2740 * 3 more credits, one for xattr block update, one for the 1st block
2741 * of the new xattr bucket and one for the value/data.
2744 handle = ocfs2_start_trans(osb, credits);
2745 if (IS_ERR(handle)) {
2746 ret = PTR_ERR(handle);
2751 ret = ocfs2_journal_access(handle, inode, xb_bh,
2752 OCFS2_JOURNAL_ACCESS_WRITE);
2758 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2765 * The bucket may spread in many blocks, and
2766 * we will only touch the 1st block and the last block
2767 * in the whole bucket(one for entry and one for data).
2769 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2771 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
2772 (unsigned long long)blkno);
2774 xh_bh = sb_getblk(inode->i_sb, blkno);
2781 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2783 ret = ocfs2_journal_access(handle, inode, xh_bh,
2784 OCFS2_JOURNAL_ACCESS_CREATE);
2791 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2798 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2800 ret = ocfs2_journal_access(handle, inode, data_bh,
2801 OCFS2_JOURNAL_ACCESS_CREATE);
2808 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2810 ocfs2_journal_dirty(handle, xh_bh);
2812 ocfs2_journal_dirty(handle, data_bh);
2814 ret = ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2820 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2821 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2822 offsetof(struct ocfs2_xattr_block, xb_attrs));
2824 xr = &xb->xb_attrs.xb_root;
2825 xr->xt_clusters = cpu_to_le32(1);
2826 xr->xt_last_eb_blk = 0;
2827 xr->xt_list.l_tree_depth = 0;
2828 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2829 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2831 xr->xt_list.l_recs[0].e_cpos = 0;
2832 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2833 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2835 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2837 ret = ocfs2_journal_dirty(handle, xb_bh);
2844 ocfs2_commit_trans(osb, handle);
2847 up_write(&oi->ip_alloc_sem);
2851 ocfs2_free_alloc_context(data_ac);
2859 static int cmp_xe_offset(const void *a, const void *b)
2861 const struct ocfs2_xattr_entry *l = a, *r = b;
2862 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2863 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2865 if (l_name_offset < r_name_offset)
2867 if (l_name_offset > r_name_offset)
2873 * defrag a xattr bucket if we find that the bucket has some
2874 * holes beteen name/value pairs.
2875 * We will move all the name/value pairs to the end of the bucket
2876 * so that we can spare some space for insertion.
2878 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2879 struct ocfs2_xattr_bucket *bucket)
2882 size_t end, offset, len, value_len;
2883 struct ocfs2_xattr_header *xh;
2884 char *entries, *buf, *bucket_buf = NULL;
2885 u64 blkno = bucket_blkno(bucket);
2886 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2888 size_t blocksize = inode->i_sb->s_blocksize;
2890 struct buffer_head **bhs;
2891 struct ocfs2_xattr_entry *xe;
2893 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2898 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0);
2903 * In order to make the operation more efficient and generic,
2904 * we copy all the blocks into a contiguous memory and do the
2905 * defragment there, so if anything is error, we will not touch
2908 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2915 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2916 memcpy(buf, bhs[i]->b_data, blocksize);
2918 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2919 if (IS_ERR(handle)) {
2920 ret = PTR_ERR(handle);
2926 for (i = 0; i < blk_per_bucket; i++) {
2927 ret = ocfs2_journal_access(handle, inode, bhs[i],
2928 OCFS2_JOURNAL_ACCESS_WRITE);
2935 xh = (struct ocfs2_xattr_header *)bucket_buf;
2936 entries = (char *)xh->xh_entries;
2937 xh_free_start = le16_to_cpu(xh->xh_free_start);
2939 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2940 "xh_free_start = %u, xh_name_value_len = %u.\n",
2941 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
2942 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
2945 * sort all the entries by their offset.
2946 * the largest will be the first, so that we can
2947 * move them to the end one by one.
2949 sort(entries, le16_to_cpu(xh->xh_count),
2950 sizeof(struct ocfs2_xattr_entry),
2951 cmp_xe_offset, swap_xe);
2953 /* Move all name/values to the end of the bucket. */
2954 xe = xh->xh_entries;
2955 end = OCFS2_XATTR_BUCKET_SIZE;
2956 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2957 offset = le16_to_cpu(xe->xe_name_offset);
2958 if (ocfs2_xattr_is_local(xe))
2959 value_len = OCFS2_XATTR_SIZE(
2960 le64_to_cpu(xe->xe_value_size));
2962 value_len = OCFS2_XATTR_ROOT_SIZE;
2963 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2966 * We must make sure that the name/value pair
2967 * exist in the same block. So adjust end to
2968 * the previous block end if needed.
2970 if (((end - len) / blocksize !=
2971 (end - 1) / blocksize))
2972 end = end - end % blocksize;
2974 if (end > offset + len) {
2975 memmove(bucket_buf + end - len,
2976 bucket_buf + offset, len);
2977 xe->xe_name_offset = cpu_to_le16(end - len);
2980 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2981 "bucket %llu\n", (unsigned long long)blkno);
2986 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2987 "bucket %llu\n", (unsigned long long)blkno);
2989 if (xh_free_start == end)
2992 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2993 xh->xh_free_start = cpu_to_le16(end);
2995 /* sort the entries by their name_hash. */
2996 sort(entries, le16_to_cpu(xh->xh_count),
2997 sizeof(struct ocfs2_xattr_entry),
3001 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3002 memcpy(bhs[i]->b_data, buf, blocksize);
3003 ocfs2_journal_dirty(handle, bhs[i]);
3007 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3011 for (i = 0; i < blk_per_bucket; i++)
3021 * Move half nums of the xattr bucket in the previous cluster to this new
3022 * cluster. We only touch the last cluster of the previous extend record.
3024 * first_bh is the first buffer_head of a series of bucket in the same
3025 * extent rec and header_bh is the header of one bucket in this cluster.
3026 * They will be updated if we move the data header_bh contains to the new
3027 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3029 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3031 struct buffer_head **first_bh,
3032 struct buffer_head **header_bh,
3038 int i, ret, credits;
3039 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3040 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3041 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3042 int blocksize = inode->i_sb->s_blocksize;
3043 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3044 struct ocfs2_xattr_header *new_xh;
3045 struct ocfs2_xattr_header *xh =
3046 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3048 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3049 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3051 prev_bh = *first_bh;
3053 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3055 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3057 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3058 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
3061 * We need to update the 1st half of the new cluster and
3062 * 1 more for the update of the 1st bucket of the previous
3065 credits = bpc / 2 + 1;
3066 ret = ocfs2_extend_trans(handle, credits);
3072 ret = ocfs2_journal_access(handle, inode, prev_bh,
3073 OCFS2_JOURNAL_ACCESS_WRITE);
3079 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3080 old_bh = new_bh = NULL;
3081 new_bh = sb_getblk(inode->i_sb, new_blkno);
3088 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3090 ret = ocfs2_journal_access(handle, inode, new_bh,
3091 OCFS2_JOURNAL_ACCESS_CREATE);
3098 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3105 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3108 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3109 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3112 *first_hash = le32_to_cpu(
3113 new_xh->xh_entries[0].xe_name_hash);
3114 new_first_bh = new_bh;
3115 get_bh(new_first_bh);
3118 ocfs2_journal_dirty(handle, new_bh);
3120 if (*header_bh == old_bh) {
3122 *header_bh = new_bh;
3126 *first_bh = new_first_bh;
3133 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3135 ocfs2_journal_dirty(handle, prev_bh);
3138 brelse(new_first_bh);
3143 * Find the suitable pos when we divide a bucket into 2.
3144 * We have to make sure the xattrs with the same hash value exist
3145 * in the same bucket.
3147 * If this ocfs2_xattr_header covers more than one hash value, find a
3148 * place where the hash value changes. Try to find the most even split.
3149 * The most common case is that all entries have different hash values,
3150 * and the first check we make will find a place to split.
3152 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3154 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3155 int count = le16_to_cpu(xh->xh_count);
3156 int delta, middle = count / 2;
3159 * We start at the middle. Each step gets farther away in both
3160 * directions. We therefore hit the change in hash value
3161 * nearest to the middle. Note that this loop does not execute for
3164 for (delta = 0; delta < middle; delta++) {
3165 /* Let's check delta earlier than middle */
3166 if (cmp_xe(&entries[middle - delta - 1],
3167 &entries[middle - delta]))
3168 return middle - delta;
3170 /* For even counts, don't walk off the end */
3171 if ((middle + delta + 1) == count)
3174 /* Now try delta past middle */
3175 if (cmp_xe(&entries[middle + delta],
3176 &entries[middle + delta + 1]))
3177 return middle + delta + 1;
3180 /* Every entry had the same hash */
3185 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3186 * first_hash will record the 1st hash of the new bucket.
3188 * Normally half of the xattrs will be moved. But we have to make
3189 * sure that the xattrs with the same hash value are stored in the
3190 * same bucket. If all the xattrs in this bucket have the same hash
3191 * value, the new bucket will be initialized as an empty one and the
3192 * first_hash will be initialized as (hash_value+1).
3194 static int ocfs2_divide_xattr_bucket(struct inode *inode,
3199 int new_bucket_head)
3202 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
3203 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3204 struct ocfs2_xattr_bucket s_bucket, t_bucket;
3205 struct ocfs2_xattr_header *xh;
3206 struct ocfs2_xattr_entry *xe;
3207 int blocksize = inode->i_sb->s_blocksize;
3209 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3210 (unsigned long long)blk, (unsigned long long)new_blk);
3212 memset(&s_bucket, 0, sizeof(struct ocfs2_xattr_bucket));
3213 memset(&t_bucket, 0, sizeof(struct ocfs2_xattr_bucket));
3215 ret = ocfs2_read_xattr_bucket(inode, &s_bucket, blk);
3221 ret = ocfs2_journal_access(handle, inode, s_bucket.bu_bhs[0],
3222 OCFS2_JOURNAL_ACCESS_WRITE);
3229 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3230 * there's no need to read it.
3232 ret = ocfs2_init_xattr_bucket(inode, &t_bucket, new_blk);
3238 for (i = 0; i < blk_per_bucket; i++) {
3239 ret = ocfs2_journal_access(handle, inode, t_bucket.bu_bhs[i],
3241 OCFS2_JOURNAL_ACCESS_CREATE :
3242 OCFS2_JOURNAL_ACCESS_WRITE);
3249 xh = bucket_xh(&s_bucket);
3250 count = le16_to_cpu(xh->xh_count);
3251 start = ocfs2_xattr_find_divide_pos(xh);
3253 if (start == count) {
3254 xe = &xh->xh_entries[start-1];
3257 * initialized a new empty bucket here.
3258 * The hash value is set as one larger than
3259 * that of the last entry in the previous bucket.
3261 for (i = 0; i < blk_per_bucket; i++)
3262 memset(bucket_block(&t_bucket, i), 0, blocksize);
3264 xh = bucket_xh(&t_bucket);
3265 xh->xh_free_start = cpu_to_le16(blocksize);
3266 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3267 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3269 goto set_num_buckets;
3272 /* copy the whole bucket to the new first. */
3273 for (i = 0; i < blk_per_bucket; i++)
3274 memcpy(bucket_block(&t_bucket, i), bucket_block(&s_bucket, i),
3277 /* update the new bucket. */
3278 xh = bucket_xh(&t_bucket);
3281 * Calculate the total name/value len and xh_free_start for
3282 * the old bucket first.
3284 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3286 for (i = 0; i < start; i++) {
3287 xe = &xh->xh_entries[i];
3288 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3289 if (ocfs2_xattr_is_local(xe))
3291 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3293 xe_len += OCFS2_XATTR_ROOT_SIZE;
3294 name_value_len += xe_len;
3295 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3296 name_offset = le16_to_cpu(xe->xe_name_offset);
3300 * Now begin the modification to the new bucket.
3302 * In the new bucket, We just move the xattr entry to the beginning
3303 * and don't touch the name/value. So there will be some holes in the
3304 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3307 xe = &xh->xh_entries[start];
3308 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3309 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3310 (int)((char *)xe - (char *)xh),
3311 (int)((char *)xh->xh_entries - (char *)xh));
3312 memmove((char *)xh->xh_entries, (char *)xe, len);
3313 xe = &xh->xh_entries[count - start];
3314 len = sizeof(struct ocfs2_xattr_entry) * start;
3315 memset((char *)xe, 0, len);
3317 le16_add_cpu(&xh->xh_count, -start);
3318 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3320 /* Calculate xh_free_start for the new bucket. */
3321 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3322 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3323 xe = &xh->xh_entries[i];
3324 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3325 if (ocfs2_xattr_is_local(xe))
3327 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3329 xe_len += OCFS2_XATTR_ROOT_SIZE;
3330 if (le16_to_cpu(xe->xe_name_offset) <
3331 le16_to_cpu(xh->xh_free_start))
3332 xh->xh_free_start = xe->xe_name_offset;
3336 /* set xh->xh_num_buckets for the new xh. */
3337 if (new_bucket_head)
3338 xh->xh_num_buckets = cpu_to_le16(1);
3340 xh->xh_num_buckets = 0;
3342 for (i = 0; i < blk_per_bucket; i++) {
3343 ocfs2_journal_dirty(handle, t_bucket.bu_bhs[i]);
3348 /* store the first_hash of the new bucket. */
3350 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3353 * Now only update the 1st block of the old bucket. If we
3354 * just added a new empty bucket, there is no need to modify
3360 xh = bucket_xh(&s_bucket);
3361 memset(&xh->xh_entries[start], 0,
3362 sizeof(struct ocfs2_xattr_entry) * (count - start));
3363 xh->xh_count = cpu_to_le16(start);
3364 xh->xh_free_start = cpu_to_le16(name_offset);
3365 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3367 ocfs2_journal_dirty(handle, s_bucket.bu_bhs[0]);
3372 ocfs2_xattr_bucket_relse(inode, &s_bucket);
3373 ocfs2_xattr_bucket_relse(inode, &t_bucket);
3379 * Copy xattr from one bucket to another bucket.
3381 * The caller must make sure that the journal transaction
3382 * has enough space for journaling.
3384 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3391 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3392 int blocksize = inode->i_sb->s_blocksize;
3393 struct ocfs2_xattr_bucket s_bucket, t_bucket;
3395 BUG_ON(s_blkno == t_blkno);
3397 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3398 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3401 memset(&s_bucket, 0, sizeof(struct ocfs2_xattr_bucket));
3402 memset(&t_bucket, 0, sizeof(struct ocfs2_xattr_bucket));
3404 ret = ocfs2_read_xattr_bucket(inode, &s_bucket, s_blkno);
3409 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3410 * there's no need to read it.
3412 ret = ocfs2_init_xattr_bucket(inode, &t_bucket, t_blkno);
3416 for (i = 0; i < blk_per_bucket; i++) {
3417 ret = ocfs2_journal_access(handle, inode, t_bucket.bu_bhs[i],
3419 OCFS2_JOURNAL_ACCESS_CREATE :
3420 OCFS2_JOURNAL_ACCESS_WRITE);
3425 for (i = 0; i < blk_per_bucket; i++) {
3426 memcpy(bucket_block(&t_bucket, i), bucket_block(&s_bucket, i),
3428 ocfs2_journal_dirty(handle, t_bucket.bu_bhs[i]);
3432 ocfs2_xattr_bucket_relse(inode, &s_bucket);
3433 ocfs2_xattr_bucket_relse(inode, &t_bucket);
3439 * Copy one xattr cluster from src_blk to to_blk.
3440 * The to_blk will become the first bucket header of the cluster, so its
3441 * xh_num_buckets will be initialized as the bucket num in the cluster.
3443 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3445 struct buffer_head *first_bh,
3450 int i, ret, credits;
3451 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3452 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3453 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3454 struct buffer_head *bh = NULL;
3455 struct ocfs2_xattr_header *xh;
3456 u64 to_blk_start = to_blk;
3458 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3459 (unsigned long long)src_blk, (unsigned long long)to_blk);
3462 * We need to update the new cluster and 1 more for the update of
3463 * the 1st bucket of the previous extent rec.
3466 ret = ocfs2_extend_trans(handle, credits);
3472 ret = ocfs2_journal_access(handle, inode, first_bh,
3473 OCFS2_JOURNAL_ACCESS_WRITE);
3479 for (i = 0; i < num_buckets; i++) {
3480 ret = ocfs2_cp_xattr_bucket(inode, handle,
3481 src_blk, to_blk, 1);
3487 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3488 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3491 /* update the old bucket header. */
3492 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3493 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3495 ocfs2_journal_dirty(handle, first_bh);
3497 /* update the new bucket header. */
3498 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3504 ret = ocfs2_journal_access(handle, inode, bh,
3505 OCFS2_JOURNAL_ACCESS_WRITE);
3511 xh = (struct ocfs2_xattr_header *)bh->b_data;
3512 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3514 ocfs2_journal_dirty(handle, bh);
3517 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3524 * Move some xattrs in this cluster to the new cluster.
3525 * This function should only be called when bucket size == cluster size.
3526 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3528 static int ocfs2_divide_xattr_cluster(struct inode *inode,
3534 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3535 int ret, credits = 2 * blk_per_bucket;
3537 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3539 ret = ocfs2_extend_trans(handle, credits);
3545 /* Move half of the xattr in start_blk to the next bucket. */
3546 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
3547 new_blk, first_hash, 1);
3551 * Move some xattrs from the old cluster to the new one since they are not
3552 * contiguous in ocfs2 xattr tree.
3554 * new_blk starts a new separate cluster, and we will move some xattrs from
3555 * prev_blk to it. v_start will be set as the first name hash value in this
3556 * new cluster so that it can be used as e_cpos during tree insertion and
3557 * don't collide with our original b-tree operations. first_bh and header_bh
3558 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3559 * to extend the insert bucket.
3561 * The problem is how much xattr should we move to the new one and when should
3562 * we update first_bh and header_bh?
3563 * 1. If cluster size > bucket size, that means the previous cluster has more
3564 * than 1 bucket, so just move half nums of bucket into the new cluster and
3565 * update the first_bh and header_bh if the insert bucket has been moved
3566 * to the new cluster.
3567 * 2. If cluster_size == bucket_size:
3568 * a) If the previous extent rec has more than one cluster and the insert
3569 * place isn't in the last cluster, copy the entire last cluster to the
3570 * new one. This time, we don't need to upate the first_bh and header_bh
3571 * since they will not be moved into the new cluster.
3572 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3573 * the new one. And we set the extend flag to zero if the insert place is
3574 * moved into the new allocated cluster since no extend is needed.
3576 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3578 struct buffer_head **first_bh,
3579 struct buffer_head **header_bh,
3587 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3589 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3590 (unsigned long long)prev_blk, prev_clusters,
3591 (unsigned long long)new_blk);
3593 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3594 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3603 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3605 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3606 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3610 ret = ocfs2_divide_xattr_cluster(inode, handle,
3614 if ((*header_bh)->b_blocknr == last_blk && extend)
3623 * Add a new cluster for xattr storage.
3625 * If the new cluster is contiguous with the previous one, it will be
3626 * appended to the same extent record, and num_clusters will be updated.
3627 * If not, we will insert a new extent for it and move some xattrs in
3628 * the last cluster into the new allocated one.
3629 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3630 * lose the benefits of hashing because we'll have to search large leaves.
3631 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3634 * first_bh is the first block of the previous extent rec and header_bh
3635 * indicates the bucket we will insert the new xattrs. They will be updated
3636 * when the header_bh is moved into the new cluster.
3638 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3639 struct buffer_head *root_bh,
3640 struct buffer_head **first_bh,
3641 struct buffer_head **header_bh,
3648 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3649 u32 prev_clusters = *num_clusters;
3650 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3652 handle_t *handle = NULL;
3653 struct ocfs2_alloc_context *data_ac = NULL;
3654 struct ocfs2_alloc_context *meta_ac = NULL;
3655 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3656 struct ocfs2_extent_tree et;
3658 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3659 "previous xattr blkno = %llu\n",
3660 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3661 prev_cpos, (unsigned long long)prev_blkno);
3663 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3665 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3666 &data_ac, &meta_ac);
3672 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3674 handle = ocfs2_start_trans(osb, credits);
3675 if (IS_ERR(handle)) {
3676 ret = PTR_ERR(handle);
3682 ret = ocfs2_journal_access(handle, inode, root_bh,
3683 OCFS2_JOURNAL_ACCESS_WRITE);
3689 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3690 clusters_to_add, &bit_off, &num_bits);
3697 BUG_ON(num_bits > clusters_to_add);
3699 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3700 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3701 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3703 if (prev_blkno + prev_clusters * bpc == block &&
3704 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3705 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3707 * If this cluster is contiguous with the old one and
3708 * adding this new cluster, we don't surpass the limit of
3709 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3710 * initialized and used like other buckets in the previous
3712 * So add it as a contiguous one. The caller will handle
3715 v_start = prev_cpos + prev_clusters;
3716 *num_clusters = prev_clusters + num_bits;
3717 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3720 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3735 if (handle->h_buffer_credits < credits) {
3737 * The journal has been restarted before, and don't
3738 * have enough space for the insertion, so extend it
3741 ret = ocfs2_extend_trans(handle, credits);
3747 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3748 num_bits, (unsigned long long)block, v_start);
3749 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3750 num_bits, 0, meta_ac);
3756 ret = ocfs2_journal_dirty(handle, root_bh);
3764 ocfs2_commit_trans(osb, handle);
3766 ocfs2_free_alloc_context(data_ac);
3768 ocfs2_free_alloc_context(meta_ac);
3774 * Extend a new xattr bucket and move xattrs to the end one by one until
3775 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3777 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3778 struct buffer_head *first_bh,
3779 struct buffer_head *start_bh,
3783 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3784 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3785 u64 start_blk = start_bh->b_blocknr, end_blk;
3786 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3788 struct ocfs2_xattr_header *first_xh =
3789 (struct ocfs2_xattr_header *)first_bh->b_data;
3790 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3792 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3793 "from %llu, len = %u\n", (unsigned long long)start_blk,
3794 (unsigned long long)first_bh->b_blocknr, num_clusters);
3796 BUG_ON(bucket >= num_buckets);
3798 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3801 * We will touch all the buckets after the start_bh(include it).
3802 * Add one more bucket and modify the first_bh.
3804 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3805 handle = ocfs2_start_trans(osb, credits);
3806 if (IS_ERR(handle)) {
3807 ret = PTR_ERR(handle);
3813 ret = ocfs2_journal_access(handle, inode, first_bh,
3814 OCFS2_JOURNAL_ACCESS_WRITE);
3820 while (end_blk != start_blk) {
3821 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3822 end_blk + blk_per_bucket, 0);
3825 end_blk -= blk_per_bucket;
3828 /* Move half of the xattr in start_blk to the next bucket. */
3829 ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
3830 start_blk + blk_per_bucket, NULL, 0);
3832 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3833 ocfs2_journal_dirty(handle, first_bh);
3836 ocfs2_commit_trans(osb, handle);
3842 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3843 * xb_bh is the ocfs2_xattr_block.
3844 * We will move all the buckets starting from header_bh to the next place. As
3845 * for this one, half num of its xattrs will be moved to the next one.
3847 * We will allocate a new cluster if current cluster is full and adjust
3848 * header_bh and first_bh if the insert place is moved to the new cluster.
3850 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3851 struct buffer_head *xb_bh,
3852 struct buffer_head *header_bh)
3854 struct ocfs2_xattr_header *first_xh = NULL;
3855 struct buffer_head *first_bh = NULL;
3856 struct ocfs2_xattr_block *xb =
3857 (struct ocfs2_xattr_block *)xb_bh->b_data;
3858 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3859 struct ocfs2_extent_list *el = &xb_root->xt_list;
3860 struct ocfs2_xattr_header *xh =
3861 (struct ocfs2_xattr_header *)header_bh->b_data;
3862 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3863 struct super_block *sb = inode->i_sb;
3864 struct ocfs2_super *osb = OCFS2_SB(sb);
3865 int ret, num_buckets, extend = 1;
3867 u32 e_cpos, num_clusters;
3869 mlog(0, "Add new xattr bucket starting form %llu\n",
3870 (unsigned long long)header_bh->b_blocknr);
3873 * Add refrence for header_bh here because it may be
3874 * changed in ocfs2_add_new_xattr_cluster and we need
3875 * to free it in the end.
3879 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3886 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
3892 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3893 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3895 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3896 ret = ocfs2_add_new_xattr_cluster(inode,
3911 ret = ocfs2_extend_xattr_bucket(inode,
3923 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3924 struct ocfs2_xattr_bucket *bucket,
3927 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3929 offs = offs % inode->i_sb->s_blocksize;
3930 return bucket_block(bucket, block_off) + offs;
3934 * Handle the normal xattr set, including replace, delete and new.
3936 * Note: "local" indicates the real data's locality. So we can't
3937 * just its bucket locality by its length.
3939 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3940 struct ocfs2_xattr_info *xi,
3941 struct ocfs2_xattr_search *xs,
3945 struct ocfs2_xattr_entry *last, *xe;
3946 int name_len = strlen(xi->name);
3947 struct ocfs2_xattr_header *xh = xs->header;
3948 u16 count = le16_to_cpu(xh->xh_count), start;
3949 size_t blocksize = inode->i_sb->s_blocksize;
3951 size_t offs, size, new_size;
3953 last = &xh->xh_entries[count];
3954 if (!xs->not_found) {
3956 offs = le16_to_cpu(xe->xe_name_offset);
3957 if (ocfs2_xattr_is_local(xe))
3958 size = OCFS2_XATTR_SIZE(name_len) +
3959 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3961 size = OCFS2_XATTR_SIZE(name_len) +
3962 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3965 * If the new value will be stored outside, xi->value has been
3966 * initalized as an empty ocfs2_xattr_value_root, and the same
3967 * goes with xi->value_len, so we can set new_size safely here.
3968 * See ocfs2_xattr_set_in_bucket.
3970 new_size = OCFS2_XATTR_SIZE(name_len) +
3971 OCFS2_XATTR_SIZE(xi->value_len);
3973 le16_add_cpu(&xh->xh_name_value_len, -size);
3975 if (new_size > size)
3976 goto set_new_name_value;
3978 /* Now replace the old value with new one. */
3980 xe->xe_value_size = cpu_to_le64(xi->value_len);
3982 xe->xe_value_size = 0;
3984 val = ocfs2_xattr_bucket_get_val(inode,
3986 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3987 size - OCFS2_XATTR_SIZE(name_len));
3988 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3989 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3990 xi->value, xi->value_len);
3992 le16_add_cpu(&xh->xh_name_value_len, new_size);
3993 ocfs2_xattr_set_local(xe, local);
3997 * Remove the old entry if there is more than one.
3998 * We don't remove the last entry so that we can
3999 * use it to indicate the hash value of the empty
4003 le16_add_cpu(&xh->xh_count, -1);
4006 (void *)last - (void *)xe);
4008 sizeof(struct ocfs2_xattr_entry));
4011 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4016 /* find a new entry for insert. */
4017 int low = 0, high = count - 1, tmp;
4018 struct ocfs2_xattr_entry *tmp_xe;
4020 while (low <= high && count) {
4021 tmp = (low + high) / 2;
4022 tmp_xe = &xh->xh_entries[tmp];
4024 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4026 else if (name_hash <
4027 le32_to_cpu(tmp_xe->xe_name_hash))
4035 xe = &xh->xh_entries[low];
4037 memmove(xe + 1, xe, (void *)last - (void *)xe);
4039 le16_add_cpu(&xh->xh_count, 1);
4040 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4041 xe->xe_name_hash = cpu_to_le32(name_hash);
4042 xe->xe_name_len = name_len;
4043 ocfs2_xattr_set_type(xe, xi->name_index);
4047 /* Insert the new name+value. */
4048 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4051 * We must make sure that the name/value pair
4052 * exists in the same block.
4054 offs = le16_to_cpu(xh->xh_free_start);
4055 start = offs - size;
4057 if (start >> inode->i_sb->s_blocksize_bits !=
4058 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4059 offs = offs - offs % blocksize;
4060 xh->xh_free_start = cpu_to_le16(offs);
4063 val = ocfs2_xattr_bucket_get_val(inode,
4064 &xs->bucket, offs - size);
4065 xe->xe_name_offset = cpu_to_le16(offs - size);
4067 memset(val, 0, size);
4068 memcpy(val, xi->name, name_len);
4069 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4071 xe->xe_value_size = cpu_to_le64(xi->value_len);
4072 ocfs2_xattr_set_local(xe, local);
4074 le16_add_cpu(&xh->xh_free_start, -size);
4075 le16_add_cpu(&xh->xh_name_value_len, size);
4080 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4082 struct ocfs2_xattr_search *xs,
4083 struct buffer_head **bhs,
4086 int ret = 0, off, block_off;
4087 struct ocfs2_xattr_entry *xe = xs->here;
4090 * First calculate all the blocks we should journal_access
4091 * and journal_dirty. The first block should always be touched.
4093 ret = ocfs2_journal_dirty(handle, bhs[0]);
4097 /* calc the data. */
4098 off = le16_to_cpu(xe->xe_name_offset);
4099 block_off = off >> inode->i_sb->s_blocksize_bits;
4100 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4108 * Set the xattr entry in the specified bucket.
4109 * The bucket is indicated by xs->bucket and it should have the enough
4110 * space for the xattr insertion.
4112 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4113 struct ocfs2_xattr_info *xi,
4114 struct ocfs2_xattr_search *xs,
4119 handle_t *handle = NULL;
4120 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4121 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4123 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4124 (unsigned long)xi->value_len, xi->name_index,
4125 (unsigned long long)bucket_blkno(&xs->bucket));
4127 if (!xs->bucket.bu_bhs[1]) {
4128 ret = ocfs2_read_blocks(inode,
4129 bucket_blkno(&xs->bucket) + 1,
4130 blk_per_bucket - 1, &xs->bucket.bu_bhs[1],
4138 handle = ocfs2_start_trans(osb, blk_per_bucket);
4139 if (IS_ERR(handle)) {
4140 ret = PTR_ERR(handle);
4146 for (i = 0; i < blk_per_bucket; i++) {
4147 ret = ocfs2_journal_access(handle, inode, xs->bucket.bu_bhs[i],
4148 OCFS2_JOURNAL_ACCESS_WRITE);
4155 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4157 /*Only dirty the blocks we have touched in set xattr. */
4158 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4159 xs->bucket.bu_bhs, blk_per_bucket);
4163 ocfs2_commit_trans(osb, handle);
4168 static int ocfs2_xattr_value_update_size(struct inode *inode,
4169 struct buffer_head *xe_bh,
4170 struct ocfs2_xattr_entry *xe,
4174 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4175 handle_t *handle = NULL;
4177 handle = ocfs2_start_trans(osb, 1);
4178 if (IS_ERR(handle)) {
4184 ret = ocfs2_journal_access(handle, inode, xe_bh,
4185 OCFS2_JOURNAL_ACCESS_WRITE);
4191 xe->xe_value_size = cpu_to_le64(new_size);
4193 ret = ocfs2_journal_dirty(handle, xe_bh);
4198 ocfs2_commit_trans(osb, handle);
4204 * Truncate the specified xe_off entry in xattr bucket.
4205 * bucket is indicated by header_bh and len is the new length.
4206 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4208 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4210 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4211 struct buffer_head *header_bh,
4217 struct buffer_head *value_bh = NULL;
4218 struct ocfs2_xattr_value_root *xv;
4219 struct ocfs2_xattr_entry *xe;
4220 struct ocfs2_xattr_header *xh =
4221 (struct ocfs2_xattr_header *)header_bh->b_data;
4222 size_t blocksize = inode->i_sb->s_blocksize;
4224 xe = &xh->xh_entries[xe_off];
4226 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4228 offset = le16_to_cpu(xe->xe_name_offset) +
4229 OCFS2_XATTR_SIZE(xe->xe_name_len);
4231 value_blk = offset / blocksize;
4233 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4234 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4235 value_blk += header_bh->b_blocknr;
4237 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4243 xv = (struct ocfs2_xattr_value_root *)
4244 (value_bh->b_data + offset % blocksize);
4246 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4247 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4248 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4254 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4265 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4266 struct ocfs2_xattr_search *xs,
4270 struct ocfs2_xattr_entry *xe = xs->here;
4271 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4273 BUG_ON(!xs->bucket.bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4275 offset = xe - xh->xh_entries;
4276 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bu_bhs[0],
4284 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4285 struct ocfs2_xattr_search *xs,
4290 struct ocfs2_xattr_value_root *xv;
4291 struct ocfs2_xattr_entry *xe = xs->here;
4293 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4295 offset = le16_to_cpu(xe->xe_name_offset) +
4296 OCFS2_XATTR_SIZE(xe->xe_name_len);
4298 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4300 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4303 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4304 struct buffer_head *root_bh,
4310 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4311 struct inode *tl_inode = osb->osb_tl_inode;
4313 struct ocfs2_xattr_block *xb =
4314 (struct ocfs2_xattr_block *)root_bh->b_data;
4315 struct ocfs2_alloc_context *meta_ac = NULL;
4316 struct ocfs2_cached_dealloc_ctxt dealloc;
4317 struct ocfs2_extent_tree et;
4319 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4321 ocfs2_init_dealloc_ctxt(&dealloc);
4323 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4324 cpos, len, (unsigned long long)blkno);
4326 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4328 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4334 mutex_lock(&tl_inode->i_mutex);
4336 if (ocfs2_truncate_log_needs_flush(osb)) {
4337 ret = __ocfs2_flush_truncate_log(osb);
4344 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4345 if (IS_ERR(handle)) {
4351 ret = ocfs2_journal_access(handle, inode, root_bh,
4352 OCFS2_JOURNAL_ACCESS_WRITE);
4358 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4365 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4367 ret = ocfs2_journal_dirty(handle, root_bh);
4373 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4378 ocfs2_commit_trans(osb, handle);
4380 ocfs2_schedule_truncate_log_flush(osb, 1);
4382 mutex_unlock(&tl_inode->i_mutex);
4385 ocfs2_free_alloc_context(meta_ac);
4387 ocfs2_run_deallocs(osb, &dealloc);
4392 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4393 struct ocfs2_xattr_search *xs)
4395 handle_t *handle = NULL;
4396 struct ocfs2_xattr_header *xh = bucket_xh(&xs->bucket);
4397 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4398 le16_to_cpu(xh->xh_count) - 1];
4401 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4402 if (IS_ERR(handle)) {
4403 ret = PTR_ERR(handle);
4408 ret = ocfs2_journal_access(handle, inode, xs->bucket.bu_bhs[0],
4409 OCFS2_JOURNAL_ACCESS_WRITE);
4415 /* Remove the old entry. */
4416 memmove(xs->here, xs->here + 1,
4417 (void *)last - (void *)xs->here);
4418 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4419 le16_add_cpu(&xh->xh_count, -1);
4421 ret = ocfs2_journal_dirty(handle, xs->bucket.bu_bhs[0]);
4425 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4429 * Set the xattr name/value in the bucket specified in xs.
4431 * As the new value in xi may be stored in the bucket or in an outside cluster,
4432 * we divide the whole process into 3 steps:
4433 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4434 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4435 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4436 * 4. If the clusters for the new outside value can't be allocated, we need
4437 * to free the xattr we allocated in set.
4439 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4440 struct ocfs2_xattr_info *xi,
4441 struct ocfs2_xattr_search *xs)
4445 char *val = (char *)xi->value;
4446 struct ocfs2_xattr_entry *xe = xs->here;
4447 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4450 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4452 * We need to truncate the xattr storage first.
4454 * If both the old and new value are stored to
4455 * outside block, we only need to truncate
4456 * the storage and then set the value outside.
4458 * If the new value should be stored within block,
4459 * we should free all the outside block first and
4460 * the modification to the xattr block will be done
4461 * by following steps.
4463 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4464 value_len = xi->value_len;
4468 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4474 goto set_value_outside;
4477 value_len = xi->value_len;
4478 /* So we have to handle the inside block change now. */
4479 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4481 * If the new value will be stored outside of block,
4482 * initalize a new empty value root and insert it first.
4485 xi->value = &def_xv;
4486 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4489 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4495 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4498 /* allocate the space now for the outside block storage. */
4499 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4504 if (xs->not_found) {
4506 * We can't allocate enough clusters for outside
4507 * storage and we have allocated xattr already,
4508 * so need to remove it.
4510 ocfs2_xattr_bucket_remove_xs(inode, xs);
4516 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4522 * check whether the xattr bucket is filled up with the same hash value.
4523 * If we want to insert the xattr with the same hash, return -ENOSPC.
4524 * If we want to insert a xattr with different hash value, go ahead
4525 * and ocfs2_divide_xattr_bucket will handle this.
4527 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4528 struct ocfs2_xattr_bucket *bucket,
4531 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4532 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4534 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4537 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4538 xh->xh_entries[0].xe_name_hash) {
4539 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4541 (unsigned long long)bucket_blkno(bucket),
4542 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4549 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4550 struct ocfs2_xattr_info *xi,
4551 struct ocfs2_xattr_search *xs)
4553 struct ocfs2_xattr_header *xh;
4554 struct ocfs2_xattr_entry *xe;
4555 u16 count, header_size, xh_free_start;
4556 int free, max_free, need, old;
4557 size_t value_size = 0, name_len = strlen(xi->name);
4558 size_t blocksize = inode->i_sb->s_blocksize;
4559 int ret, allocation = 0;
4561 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4565 count = le16_to_cpu(xh->xh_count);
4566 xh_free_start = le16_to_cpu(xh->xh_free_start);
4567 header_size = sizeof(struct ocfs2_xattr_header) +
4568 count * sizeof(struct ocfs2_xattr_entry);
4569 max_free = OCFS2_XATTR_BUCKET_SIZE -
4570 le16_to_cpu(xh->xh_name_value_len) - header_size;
4572 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4573 "of %u which exceed block size\n",
4574 (unsigned long long)bucket_blkno(&xs->bucket),
4577 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4578 value_size = OCFS2_XATTR_ROOT_SIZE;
4580 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4583 need = sizeof(struct ocfs2_xattr_entry) +
4584 OCFS2_XATTR_SIZE(name_len) + value_size;
4586 need = value_size + OCFS2_XATTR_SIZE(name_len);
4589 * We only replace the old value if the new length is smaller
4590 * than the old one. Otherwise we will allocate new space in the
4591 * bucket to store it.
4594 if (ocfs2_xattr_is_local(xe))
4595 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4597 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4599 if (old >= value_size)
4603 free = xh_free_start - header_size;
4605 * We need to make sure the new name/value pair
4606 * can exist in the same block.
4608 if (xh_free_start % blocksize < need)
4609 free -= xh_free_start % blocksize;
4611 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4612 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4613 " %u\n", xs->not_found,
4614 (unsigned long long)bucket_blkno(&xs->bucket),
4615 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4616 le16_to_cpu(xh->xh_name_value_len));
4618 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4619 if (need <= max_free &&
4620 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4622 * We can create the space by defragment. Since only the
4623 * name/value will be moved, the xe shouldn't be changed
4626 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4632 xh_free_start = le16_to_cpu(xh->xh_free_start);
4633 free = xh_free_start - header_size;
4634 if (xh_free_start % blocksize < need)
4635 free -= xh_free_start % blocksize;
4640 mlog(0, "Can't get enough space for xattr insert by "
4641 "defragment. Need %u bytes, but we have %d, so "
4642 "allocate new bucket for it.\n", need, free);
4646 * We have to add new buckets or clusters and one
4647 * allocation should leave us enough space for insert.
4652 * We do not allow for overlapping ranges between buckets. And
4653 * the maximum number of collisions we will allow for then is
4654 * one bucket's worth, so check it here whether we need to
4655 * add a new bucket for the insert.
4657 ret = ocfs2_check_xattr_bucket_collision(inode,
4665 ret = ocfs2_add_new_xattr_bucket(inode,
4667 xs->bucket.bu_bhs[0]);
4673 ocfs2_xattr_bucket_relse(inode, &xs->bucket);
4674 memset(&xs->bucket, 0, sizeof(xs->bucket));
4676 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4679 if (ret && ret != -ENODATA)
4681 xs->not_found = ret;
4687 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4693 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4694 struct ocfs2_xattr_bucket *bucket,
4698 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4700 struct ocfs2_xattr_entry *xe;
4702 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4703 xe = &xh->xh_entries[i];
4704 if (ocfs2_xattr_is_local(xe))
4707 ret = ocfs2_xattr_bucket_value_truncate(inode,
4719 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4720 struct buffer_head *xb_bh)
4722 struct ocfs2_xattr_block *xb =
4723 (struct ocfs2_xattr_block *)xb_bh->b_data;
4724 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4726 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4729 if (le16_to_cpu(el->l_next_free_rec) == 0)
4732 while (name_hash > 0) {
4733 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4734 &e_cpos, &num_clusters, el);
4740 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4741 ocfs2_delete_xattr_in_bucket,
4748 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4749 p_blkno, e_cpos, num_clusters);
4758 name_hash = e_cpos - 1;
4766 * 'trusted' attributes support
4768 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4769 size_t list_size, const char *name,
4772 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
4773 const size_t total_len = prefix_len + name_len + 1;
4775 if (list && total_len <= list_size) {
4776 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4777 memcpy(list + prefix_len, name, name_len);
4778 list[prefix_len + name_len] = '\0';
4783 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4784 void *buffer, size_t size)
4786 if (strcmp(name, "") == 0)
4788 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4792 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4793 const void *value, size_t size, int flags)
4795 if (strcmp(name, "") == 0)
4798 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4802 struct xattr_handler ocfs2_xattr_trusted_handler = {
4803 .prefix = XATTR_TRUSTED_PREFIX,
4804 .list = ocfs2_xattr_trusted_list,
4805 .get = ocfs2_xattr_trusted_get,
4806 .set = ocfs2_xattr_trusted_set,
4810 * 'user' attributes support
4812 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4813 size_t list_size, const char *name,
4816 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
4817 const size_t total_len = prefix_len + name_len + 1;
4818 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4820 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4823 if (list && total_len <= list_size) {
4824 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4825 memcpy(list + prefix_len, name, name_len);
4826 list[prefix_len + name_len] = '\0';
4831 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4832 void *buffer, size_t size)
4834 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4836 if (strcmp(name, "") == 0)
4838 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4840 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4844 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4845 const void *value, size_t size, int flags)
4847 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4849 if (strcmp(name, "") == 0)
4851 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4854 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4858 struct xattr_handler ocfs2_xattr_user_handler = {
4859 .prefix = XATTR_USER_PREFIX,
4860 .list = ocfs2_xattr_user_list,
4861 .get = ocfs2_xattr_user_get,
4862 .set = ocfs2_xattr_user_set,