1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2008 Oracle. All rights reserved.
9 * Lots of code in this file is taken from ext3.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
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.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
27 #include <linux/capability.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/highmem.h>
32 #include <linux/pagemap.h>
33 #include <linux/uio.h>
34 #include <linux/sched.h>
35 #include <linux/splice.h>
36 #include <linux/mount.h>
37 #include <linux/writeback.h>
38 #include <linux/falloc.h>
39 #include <linux/sort.h>
41 #define MLOG_MASK_PREFIX ML_XATTR
42 #include <cluster/masklog.h>
55 #include "buffer_head_io.h"
60 struct ocfs2_xattr_def_value_root {
61 struct ocfs2_xattr_value_root xv;
62 struct ocfs2_extent_rec er;
65 struct ocfs2_xattr_bucket {
66 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
67 struct ocfs2_xattr_header *xh;
70 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
71 #define OCFS2_XATTR_INLINE_SIZE 80
73 static struct ocfs2_xattr_def_value_root def_xv = {
74 .xv.xr_list.l_count = cpu_to_le16(1),
77 struct xattr_handler *ocfs2_xattr_handlers[] = {
78 &ocfs2_xattr_user_handler,
79 &ocfs2_xattr_trusted_handler,
83 static struct xattr_handler *ocfs2_xattr_handler_map[] = {
84 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
85 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
88 struct ocfs2_xattr_info {
95 struct ocfs2_xattr_search {
96 struct buffer_head *inode_bh;
98 * xattr_bh point to the block buffer head which has extended attribute
99 * when extended attribute in inode, xattr_bh is equal to inode_bh.
101 struct buffer_head *xattr_bh;
102 struct ocfs2_xattr_header *header;
103 struct ocfs2_xattr_bucket bucket;
106 struct ocfs2_xattr_entry *here;
110 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
111 struct ocfs2_xattr_header *xh,
116 static int ocfs2_xattr_index_block_find(struct inode *inode,
117 struct buffer_head *root_bh,
120 struct ocfs2_xattr_search *xs);
122 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
123 struct ocfs2_xattr_tree_root *xt,
127 static int ocfs2_xattr_create_index_block(struct inode *inode,
128 struct ocfs2_xattr_search *xs);
130 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
131 struct ocfs2_xattr_info *xi,
132 struct ocfs2_xattr_search *xs);
134 static int ocfs2_delete_xattr_index_block(struct inode *inode,
135 struct buffer_head *xb_bh);
137 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
139 struct xattr_handler *handler = NULL;
141 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
142 handler = ocfs2_xattr_handler_map[name_index];
147 static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
153 /* Get hash value of uuid from super block */
154 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
157 /* hash extended attribute prefix */
158 for (i = 0; i < prefix_len; i++) {
159 hash = (hash << OCFS2_HASH_SHIFT) ^
160 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
163 /* hash extended attribute name */
164 for (i = 0; i < name_len; i++) {
165 hash = (hash << OCFS2_HASH_SHIFT) ^
166 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
174 * ocfs2_xattr_hash_entry()
176 * Compute the hash of an extended attribute.
178 static void ocfs2_xattr_hash_entry(struct inode *inode,
179 struct ocfs2_xattr_header *header,
180 struct ocfs2_xattr_entry *entry)
183 struct xattr_handler *handler =
184 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
185 char *prefix = handler->prefix;
186 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
187 int prefix_len = strlen(handler->prefix);
189 hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
191 entry->xe_name_hash = cpu_to_le32(hash);
196 static int ocfs2_xattr_extend_allocation(struct inode *inode,
198 struct buffer_head *xattr_bh,
199 struct ocfs2_xattr_value_root *xv)
202 int restart_func = 0;
204 handle_t *handle = NULL;
205 struct ocfs2_alloc_context *data_ac = NULL;
206 struct ocfs2_alloc_context *meta_ac = NULL;
207 enum ocfs2_alloc_restarted why;
208 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
209 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
210 struct ocfs2_extent_tree et;
212 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
214 ocfs2_get_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
218 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
225 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
227 handle = ocfs2_start_trans(osb, credits);
228 if (IS_ERR(handle)) {
229 status = PTR_ERR(handle);
235 restarted_transaction:
236 status = ocfs2_journal_access(handle, inode, xattr_bh,
237 OCFS2_JOURNAL_ACCESS_WRITE);
243 prev_clusters = le32_to_cpu(xv->xr_clusters);
244 status = ocfs2_add_clusters_in_btree(osb,
254 if ((status < 0) && (status != -EAGAIN)) {
255 if (status != -ENOSPC)
260 status = ocfs2_journal_dirty(handle, xattr_bh);
266 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
268 if (why != RESTART_NONE && clusters_to_add) {
269 if (why == RESTART_META) {
270 mlog(0, "restarting function.\n");
273 BUG_ON(why != RESTART_TRANS);
275 mlog(0, "restarting transaction.\n");
276 /* TODO: This can be more intelligent. */
277 credits = ocfs2_calc_extend_credits(osb->sb,
280 status = ocfs2_extend_trans(handle, credits);
282 /* handle still has to be committed at
288 goto restarted_transaction;
294 ocfs2_commit_trans(osb, handle);
298 ocfs2_free_alloc_context(data_ac);
302 ocfs2_free_alloc_context(meta_ac);
305 if ((!status) && restart_func) {
310 ocfs2_put_extent_tree(&et);
314 static int __ocfs2_remove_xattr_range(struct inode *inode,
315 struct buffer_head *root_bh,
316 struct ocfs2_xattr_value_root *xv,
317 u32 cpos, u32 phys_cpos, u32 len,
318 struct ocfs2_cached_dealloc_ctxt *dealloc)
321 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
322 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
323 struct inode *tl_inode = osb->osb_tl_inode;
325 struct ocfs2_alloc_context *meta_ac = NULL;
326 struct ocfs2_extent_tree et;
328 ocfs2_get_xattr_value_extent_tree(&et, inode, root_bh, xv);
330 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
332 ocfs2_put_extent_tree(&et);
337 mutex_lock(&tl_inode->i_mutex);
339 if (ocfs2_truncate_log_needs_flush(osb)) {
340 ret = __ocfs2_flush_truncate_log(osb);
347 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
348 if (IS_ERR(handle)) {
349 ret = PTR_ERR(handle);
354 ret = ocfs2_journal_access(handle, inode, root_bh,
355 OCFS2_JOURNAL_ACCESS_WRITE);
361 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
368 le32_add_cpu(&xv->xr_clusters, -len);
370 ret = ocfs2_journal_dirty(handle, root_bh);
376 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
381 ocfs2_commit_trans(osb, handle);
383 mutex_unlock(&tl_inode->i_mutex);
386 ocfs2_free_alloc_context(meta_ac);
388 ocfs2_put_extent_tree(&et);
392 static int ocfs2_xattr_shrink_size(struct inode *inode,
395 struct buffer_head *root_bh,
396 struct ocfs2_xattr_value_root *xv)
399 u32 trunc_len, cpos, phys_cpos, alloc_size;
401 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
402 struct ocfs2_cached_dealloc_ctxt dealloc;
404 ocfs2_init_dealloc_ctxt(&dealloc);
406 if (old_clusters <= new_clusters)
410 trunc_len = old_clusters - new_clusters;
412 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
413 &alloc_size, &xv->xr_list);
419 if (alloc_size > trunc_len)
420 alloc_size = trunc_len;
422 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
423 phys_cpos, alloc_size,
430 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
431 ocfs2_remove_xattr_clusters_from_cache(inode, block,
434 trunc_len -= alloc_size;
438 ocfs2_schedule_truncate_log_flush(osb, 1);
439 ocfs2_run_deallocs(osb, &dealloc);
444 static int ocfs2_xattr_value_truncate(struct inode *inode,
445 struct buffer_head *root_bh,
446 struct ocfs2_xattr_value_root *xv,
450 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
451 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
453 if (new_clusters == old_clusters)
456 if (new_clusters > old_clusters)
457 ret = ocfs2_xattr_extend_allocation(inode,
458 new_clusters - old_clusters,
461 ret = ocfs2_xattr_shrink_size(inode,
462 old_clusters, new_clusters,
468 static int ocfs2_xattr_list_entries(struct inode *inode,
469 struct ocfs2_xattr_header *header,
470 char *buffer, size_t buffer_size)
472 size_t rest = buffer_size;
475 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
476 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
477 struct xattr_handler *handler =
478 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
481 size_t size = handler->list(inode, buffer, rest,
483 le16_to_cpu(entry->xe_name_offset)),
494 return buffer_size - rest;
497 static int ocfs2_xattr_ibody_list(struct inode *inode,
498 struct ocfs2_dinode *di,
502 struct ocfs2_xattr_header *header = NULL;
503 struct ocfs2_inode_info *oi = OCFS2_I(inode);
506 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
509 header = (struct ocfs2_xattr_header *)
510 ((void *)di + inode->i_sb->s_blocksize -
511 le16_to_cpu(di->i_xattr_inline_size));
513 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
518 static int ocfs2_xattr_block_list(struct inode *inode,
519 struct ocfs2_dinode *di,
523 struct buffer_head *blk_bh = NULL;
524 struct ocfs2_xattr_block *xb;
527 if (!di->i_xattr_loc)
530 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
531 le64_to_cpu(di->i_xattr_loc),
532 &blk_bh, OCFS2_BH_CACHED, inode);
537 /*Verify the signature of xattr block*/
538 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
539 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
544 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
546 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
547 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
548 ret = ocfs2_xattr_list_entries(inode, header,
549 buffer, buffer_size);
551 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
552 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
553 buffer, buffer_size);
561 ssize_t ocfs2_listxattr(struct dentry *dentry,
565 int ret = 0, i_ret = 0, b_ret = 0;
566 struct buffer_head *di_bh = NULL;
567 struct ocfs2_dinode *di = NULL;
568 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
570 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
573 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
576 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
582 di = (struct ocfs2_dinode *)di_bh->b_data;
584 down_read(&oi->ip_xattr_sem);
585 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
593 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
598 up_read(&oi->ip_xattr_sem);
599 ocfs2_inode_unlock(dentry->d_inode, 0);
603 return i_ret + b_ret;
606 static int ocfs2_xattr_find_entry(int name_index,
608 struct ocfs2_xattr_search *xs)
610 struct ocfs2_xattr_entry *entry;
617 name_len = strlen(name);
619 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
620 cmp = name_index - ocfs2_xattr_get_type(entry);
622 cmp = name_len - entry->xe_name_len;
624 cmp = memcmp(name, (xs->base +
625 le16_to_cpu(entry->xe_name_offset)),
633 return cmp ? -ENODATA : 0;
636 static int ocfs2_xattr_get_value_outside(struct inode *inode,
637 struct ocfs2_xattr_value_root *xv,
641 u32 cpos, p_cluster, num_clusters, bpc, clusters;
644 size_t cplen, blocksize;
645 struct buffer_head *bh = NULL;
646 struct ocfs2_extent_list *el;
649 clusters = le32_to_cpu(xv->xr_clusters);
650 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
651 blocksize = inode->i_sb->s_blocksize;
654 while (cpos < clusters) {
655 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
662 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
663 /* Copy ocfs2_xattr_value */
664 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
665 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
666 &bh, OCFS2_BH_CACHED, inode);
672 cplen = len >= blocksize ? blocksize : len;
673 memcpy(buffer, bh->b_data, cplen);
682 cpos += num_clusters;
688 static int ocfs2_xattr_ibody_get(struct inode *inode,
693 struct ocfs2_xattr_search *xs)
695 struct ocfs2_inode_info *oi = OCFS2_I(inode);
696 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
697 struct ocfs2_xattr_value_root *xv;
701 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
704 xs->end = (void *)di + inode->i_sb->s_blocksize;
705 xs->header = (struct ocfs2_xattr_header *)
706 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
707 xs->base = (void *)xs->header;
708 xs->here = xs->header->xh_entries;
710 ret = ocfs2_xattr_find_entry(name_index, name, xs);
713 size = le64_to_cpu(xs->here->xe_value_size);
715 if (size > buffer_size)
717 if (ocfs2_xattr_is_local(xs->here)) {
718 memcpy(buffer, (void *)xs->base +
719 le16_to_cpu(xs->here->xe_name_offset) +
720 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
722 xv = (struct ocfs2_xattr_value_root *)
723 (xs->base + le16_to_cpu(
724 xs->here->xe_name_offset) +
725 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
726 ret = ocfs2_xattr_get_value_outside(inode, xv,
738 static int ocfs2_xattr_block_get(struct inode *inode,
743 struct ocfs2_xattr_search *xs)
745 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
746 struct buffer_head *blk_bh = NULL;
747 struct ocfs2_xattr_block *xb;
748 struct ocfs2_xattr_value_root *xv;
750 int ret = -ENODATA, name_offset, name_len, block_off, i;
752 if (!di->i_xattr_loc)
755 memset(&xs->bucket, 0, sizeof(xs->bucket));
757 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
758 le64_to_cpu(di->i_xattr_loc),
759 &blk_bh, OCFS2_BH_CACHED, inode);
764 /*Verify the signature of xattr block*/
765 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
766 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
771 xs->xattr_bh = blk_bh;
772 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
774 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
775 xs->header = &xb->xb_attrs.xb_header;
776 xs->base = (void *)xs->header;
777 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
778 xs->here = xs->header->xh_entries;
780 ret = ocfs2_xattr_find_entry(name_index, name, xs);
782 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
788 size = le64_to_cpu(xs->here->xe_value_size);
791 if (size > buffer_size)
794 name_offset = le16_to_cpu(xs->here->xe_name_offset);
795 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
796 i = xs->here - xs->header->xh_entries;
798 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
799 ret = ocfs2_xattr_bucket_get_name_value(inode,
804 xs->base = xs->bucket.bhs[block_off]->b_data;
806 if (ocfs2_xattr_is_local(xs->here)) {
807 memcpy(buffer, (void *)xs->base +
808 name_offset + name_len, size);
810 xv = (struct ocfs2_xattr_value_root *)
811 (xs->base + name_offset + name_len);
812 ret = ocfs2_xattr_get_value_outside(inode, xv,
822 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
823 brelse(xs->bucket.bhs[i]);
824 memset(&xs->bucket, 0, sizeof(xs->bucket));
832 * Copy an extended attribute into the buffer provided.
833 * Buffer is NULL to compute the size of buffer required.
835 int ocfs2_xattr_get(struct inode *inode,
842 struct ocfs2_dinode *di = NULL;
843 struct buffer_head *di_bh = NULL;
844 struct ocfs2_inode_info *oi = OCFS2_I(inode);
845 struct ocfs2_xattr_search xis = {
846 .not_found = -ENODATA,
848 struct ocfs2_xattr_search xbs = {
849 .not_found = -ENODATA,
852 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
855 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
858 ret = ocfs2_inode_lock(inode, &di_bh, 0);
863 xis.inode_bh = xbs.inode_bh = di_bh;
864 di = (struct ocfs2_dinode *)di_bh->b_data;
866 down_read(&oi->ip_xattr_sem);
867 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
870 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
872 up_read(&oi->ip_xattr_sem);
873 ocfs2_inode_unlock(inode, 0);
880 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
881 struct ocfs2_xattr_value_root *xv,
885 int ret = 0, i, cp_len, credits;
886 u16 blocksize = inode->i_sb->s_blocksize;
887 u32 p_cluster, num_clusters;
888 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
889 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
891 struct buffer_head *bh = NULL;
894 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
896 credits = clusters * bpc;
897 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
898 if (IS_ERR(handle)) {
899 ret = PTR_ERR(handle);
904 while (cpos < clusters) {
905 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
906 &num_clusters, &xv->xr_list);
912 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
914 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
915 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
916 &bh, OCFS2_BH_CACHED, inode);
922 ret = ocfs2_journal_access(handle,
925 OCFS2_JOURNAL_ACCESS_WRITE);
931 cp_len = value_len > blocksize ? blocksize : value_len;
932 memcpy(bh->b_data, value, cp_len);
935 if (cp_len < blocksize)
936 memset(bh->b_data + cp_len, 0,
939 ret = ocfs2_journal_dirty(handle, bh);
948 * XXX: do we need to empty all the following
949 * blocks in this cluster?
954 cpos += num_clusters;
957 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
964 static int ocfs2_xattr_cleanup(struct inode *inode,
965 struct ocfs2_xattr_info *xi,
966 struct ocfs2_xattr_search *xs,
969 handle_t *handle = NULL;
971 size_t name_len = strlen(xi->name);
972 void *val = xs->base + offs;
973 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
975 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
976 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
977 if (IS_ERR(handle)) {
978 ret = PTR_ERR(handle);
982 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
983 OCFS2_JOURNAL_ACCESS_WRITE);
988 /* Decrease xattr count */
989 le16_add_cpu(&xs->header->xh_count, -1);
990 /* Remove the xattr entry and tree root which has already be set*/
991 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
992 memset(val, 0, size);
994 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
998 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1003 static int ocfs2_xattr_update_entry(struct inode *inode,
1004 struct ocfs2_xattr_info *xi,
1005 struct ocfs2_xattr_search *xs,
1008 handle_t *handle = NULL;
1011 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1012 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1013 if (IS_ERR(handle)) {
1014 ret = PTR_ERR(handle);
1018 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1019 OCFS2_JOURNAL_ACCESS_WRITE);
1025 xs->here->xe_name_offset = cpu_to_le16(offs);
1026 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1027 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1028 ocfs2_xattr_set_local(xs->here, 1);
1030 ocfs2_xattr_set_local(xs->here, 0);
1031 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1033 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1037 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1043 * ocfs2_xattr_set_value_outside()
1045 * Set large size value in B tree.
1047 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1048 struct ocfs2_xattr_info *xi,
1049 struct ocfs2_xattr_search *xs,
1052 size_t name_len = strlen(xi->name);
1053 void *val = xs->base + offs;
1054 struct ocfs2_xattr_value_root *xv = NULL;
1055 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1058 memset(val, 0, size);
1059 memcpy(val, xi->name, name_len);
1060 xv = (struct ocfs2_xattr_value_root *)
1061 (val + OCFS2_XATTR_SIZE(name_len));
1062 xv->xr_clusters = 0;
1063 xv->xr_last_eb_blk = 0;
1064 xv->xr_list.l_tree_depth = 0;
1065 xv->xr_list.l_count = cpu_to_le16(1);
1066 xv->xr_list.l_next_free_rec = 0;
1068 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1074 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1080 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1088 * ocfs2_xattr_set_entry_local()
1090 * Set, replace or remove extended attribute in local.
1092 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1093 struct ocfs2_xattr_info *xi,
1094 struct ocfs2_xattr_search *xs,
1095 struct ocfs2_xattr_entry *last,
1098 size_t name_len = strlen(xi->name);
1101 if (xi->value && xs->not_found) {
1102 /* Insert the new xattr entry. */
1103 le16_add_cpu(&xs->header->xh_count, 1);
1104 ocfs2_xattr_set_type(last, xi->name_index);
1105 ocfs2_xattr_set_local(last, 1);
1106 last->xe_name_len = name_len;
1112 first_val = xs->base + min_offs;
1113 offs = le16_to_cpu(xs->here->xe_name_offset);
1114 val = xs->base + offs;
1116 if (le64_to_cpu(xs->here->xe_value_size) >
1117 OCFS2_XATTR_INLINE_SIZE)
1118 size = OCFS2_XATTR_SIZE(name_len) +
1119 OCFS2_XATTR_ROOT_SIZE;
1121 size = OCFS2_XATTR_SIZE(name_len) +
1122 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1124 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1125 OCFS2_XATTR_SIZE(xi->value_len)) {
1126 /* The old and the new value have the
1127 same size. Just replace the value. */
1128 ocfs2_xattr_set_local(xs->here, 1);
1129 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1130 /* Clear value bytes. */
1131 memset(val + OCFS2_XATTR_SIZE(name_len),
1133 OCFS2_XATTR_SIZE(xi->value_len));
1134 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1139 /* Remove the old name+value. */
1140 memmove(first_val + size, first_val, val - first_val);
1141 memset(first_val, 0, size);
1142 xs->here->xe_name_hash = 0;
1143 xs->here->xe_name_offset = 0;
1144 ocfs2_xattr_set_local(xs->here, 1);
1145 xs->here->xe_value_size = 0;
1149 /* Adjust all value offsets. */
1150 last = xs->header->xh_entries;
1151 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1152 size_t o = le16_to_cpu(last->xe_name_offset);
1155 last->xe_name_offset = cpu_to_le16(o + size);
1160 /* Remove the old entry. */
1162 memmove(xs->here, xs->here + 1,
1163 (void *)last - (void *)xs->here);
1164 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1165 le16_add_cpu(&xs->header->xh_count, -1);
1169 /* Insert the new name+value. */
1170 size_t size = OCFS2_XATTR_SIZE(name_len) +
1171 OCFS2_XATTR_SIZE(xi->value_len);
1172 void *val = xs->base + min_offs - size;
1174 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1175 memset(val, 0, size);
1176 memcpy(val, xi->name, name_len);
1177 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1180 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1181 ocfs2_xattr_set_local(xs->here, 1);
1182 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1189 * ocfs2_xattr_set_entry()
1191 * Set extended attribute entry into inode or block.
1193 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1194 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1195 * then set value in B tree with set_value_outside().
1197 static int ocfs2_xattr_set_entry(struct inode *inode,
1198 struct ocfs2_xattr_info *xi,
1199 struct ocfs2_xattr_search *xs,
1202 struct ocfs2_xattr_entry *last;
1203 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1204 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1205 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1207 handle_t *handle = NULL;
1209 struct ocfs2_xattr_info xi_l = {
1210 .name_index = xi->name_index,
1213 .value_len = xi->value_len,
1216 /* Compute min_offs, last and free space. */
1217 last = xs->header->xh_entries;
1219 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1220 size_t offs = le16_to_cpu(last->xe_name_offset);
1221 if (offs < min_offs)
1226 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1230 if (!xs->not_found) {
1232 if (ocfs2_xattr_is_local(xs->here))
1233 size = OCFS2_XATTR_SIZE(name_len) +
1234 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1236 size = OCFS2_XATTR_SIZE(name_len) +
1237 OCFS2_XATTR_ROOT_SIZE;
1238 free += (size + sizeof(struct ocfs2_xattr_entry));
1240 /* Check free space in inode or block */
1241 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1242 if (free < sizeof(struct ocfs2_xattr_entry) +
1243 OCFS2_XATTR_SIZE(name_len) +
1244 OCFS2_XATTR_ROOT_SIZE) {
1248 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1249 xi_l.value = (void *)&def_xv;
1250 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1251 } else if (xi->value) {
1252 if (free < sizeof(struct ocfs2_xattr_entry) +
1253 OCFS2_XATTR_SIZE(name_len) +
1254 OCFS2_XATTR_SIZE(xi->value_len)) {
1260 if (!xs->not_found) {
1261 /* For existing extended attribute */
1262 size_t size = OCFS2_XATTR_SIZE(name_len) +
1263 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1264 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1265 void *val = xs->base + offs;
1267 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1268 /* Replace existing local xattr with tree root */
1269 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1274 } else if (!ocfs2_xattr_is_local(xs->here)) {
1275 /* For existing xattr which has value outside */
1276 struct ocfs2_xattr_value_root *xv = NULL;
1277 xv = (struct ocfs2_xattr_value_root *)(val +
1278 OCFS2_XATTR_SIZE(name_len));
1280 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1282 * If new value need set outside also,
1283 * first truncate old value to new value,
1284 * then set new value with set_value_outside().
1286 ret = ocfs2_xattr_value_truncate(inode,
1295 ret = __ocfs2_xattr_set_value_outside(inode,
1304 ret = ocfs2_xattr_update_entry(inode,
1313 * If new value need set in local,
1314 * just trucate old value to zero.
1316 ret = ocfs2_xattr_value_truncate(inode,
1326 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1327 OCFS2_INODE_UPDATE_CREDITS);
1328 if (IS_ERR(handle)) {
1329 ret = PTR_ERR(handle);
1334 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1335 OCFS2_JOURNAL_ACCESS_WRITE);
1341 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1342 /*set extended attribue in external blcok*/
1343 ret = ocfs2_extend_trans(handle,
1344 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1349 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1350 OCFS2_JOURNAL_ACCESS_WRITE);
1358 * Set value in local, include set tree root in local.
1359 * This is the first step for value size >INLINE_SIZE.
1361 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1363 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1364 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1371 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1372 (flag & OCFS2_INLINE_XATTR_FL)) {
1373 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1374 unsigned int xattrsize = osb->s_xattr_inline_size;
1377 * Adjust extent record count or inline data size
1378 * to reserve space for extended attribute.
1380 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1381 struct ocfs2_inline_data *idata = &di->id2.i_data;
1382 le16_add_cpu(&idata->id_count, -xattrsize);
1383 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1384 struct ocfs2_extent_list *el = &di->id2.i_list;
1385 le16_add_cpu(&el->l_count, -(xattrsize /
1386 sizeof(struct ocfs2_extent_rec)));
1388 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1390 /* Update xattr flag */
1391 spin_lock(&oi->ip_lock);
1392 oi->ip_dyn_features |= flag;
1393 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1394 spin_unlock(&oi->ip_lock);
1395 /* Update inode ctime */
1396 inode->i_ctime = CURRENT_TIME;
1397 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1398 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1400 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1405 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1407 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1409 * Set value outside in B tree.
1410 * This is the second step for value size > INLINE_SIZE.
1412 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1413 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1419 * If set value outside failed, we have to clean
1420 * the junk tree root we have already set in local.
1422 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1432 static int ocfs2_xattr_free_block(handle_t *handle,
1433 struct ocfs2_super *osb,
1434 struct ocfs2_xattr_block *xb)
1436 struct inode *xb_alloc_inode;
1437 struct buffer_head *xb_alloc_bh = NULL;
1438 u64 blk = le64_to_cpu(xb->xb_blkno);
1439 u16 bit = le16_to_cpu(xb->xb_suballoc_bit);
1440 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1443 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1444 EXTENT_ALLOC_SYSTEM_INODE,
1445 le16_to_cpu(xb->xb_suballoc_slot));
1446 if (!xb_alloc_inode) {
1451 mutex_lock(&xb_alloc_inode->i_mutex);
1453 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1458 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
1463 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1468 ocfs2_inode_unlock(xb_alloc_inode, 1);
1469 brelse(xb_alloc_bh);
1471 mutex_unlock(&xb_alloc_inode->i_mutex);
1472 iput(xb_alloc_inode);
1477 static int ocfs2_remove_value_outside(struct inode*inode,
1478 struct buffer_head *bh,
1479 struct ocfs2_xattr_header *header)
1483 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1484 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1486 if (!ocfs2_xattr_is_local(entry)) {
1487 struct ocfs2_xattr_value_root *xv;
1490 val = (void *)header +
1491 le16_to_cpu(entry->xe_name_offset);
1492 xv = (struct ocfs2_xattr_value_root *)
1493 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1494 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1505 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1506 struct buffer_head *di_bh)
1509 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1510 struct ocfs2_xattr_header *header;
1513 header = (struct ocfs2_xattr_header *)
1514 ((void *)di + inode->i_sb->s_blocksize -
1515 le16_to_cpu(di->i_xattr_inline_size));
1517 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1522 static int ocfs2_xattr_block_remove(struct inode *inode,
1523 struct buffer_head *blk_bh)
1525 struct ocfs2_xattr_block *xb;
1528 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1529 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1530 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1531 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1533 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1539 * ocfs2_xattr_remove()
1541 * Free extended attribute resources associated with this inode.
1543 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1545 struct ocfs2_xattr_block *xb;
1546 struct buffer_head *blk_bh = NULL;
1547 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1548 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1549 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1553 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1556 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1559 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1560 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1566 if (di->i_xattr_loc) {
1567 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1568 le64_to_cpu(di->i_xattr_loc),
1569 &blk_bh, OCFS2_BH_CACHED, inode);
1574 /*Verify the signature of xattr block*/
1575 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1576 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1581 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1588 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1589 OCFS2_INODE_UPDATE_CREDITS);
1590 if (IS_ERR(handle)) {
1591 ret = PTR_ERR(handle);
1595 ret = ocfs2_journal_access(handle, inode, di_bh,
1596 OCFS2_JOURNAL_ACCESS_WRITE);
1602 if (di->i_xattr_loc) {
1603 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1604 ocfs2_xattr_free_block(handle, osb, xb);
1605 di->i_xattr_loc = cpu_to_le64(0);
1608 spin_lock(&oi->ip_lock);
1609 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1610 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1611 spin_unlock(&oi->ip_lock);
1613 ret = ocfs2_journal_dirty(handle, di_bh);
1617 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1624 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1625 struct ocfs2_dinode *di)
1627 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1628 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1631 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1634 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1635 struct ocfs2_inline_data *idata = &di->id2.i_data;
1636 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1637 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1638 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1639 le64_to_cpu(di->i_size);
1641 struct ocfs2_extent_list *el = &di->id2.i_list;
1642 free = (le16_to_cpu(el->l_count) -
1643 le16_to_cpu(el->l_next_free_rec)) *
1644 sizeof(struct ocfs2_extent_rec);
1646 if (free >= xattrsize)
1653 * ocfs2_xattr_ibody_find()
1655 * Find extended attribute in inode block and
1656 * fill search info into struct ocfs2_xattr_search.
1658 static int ocfs2_xattr_ibody_find(struct inode *inode,
1661 struct ocfs2_xattr_search *xs)
1663 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1664 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1668 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1671 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1672 down_read(&oi->ip_alloc_sem);
1673 has_space = ocfs2_xattr_has_space_inline(inode, di);
1674 up_read(&oi->ip_alloc_sem);
1679 xs->xattr_bh = xs->inode_bh;
1680 xs->end = (void *)di + inode->i_sb->s_blocksize;
1681 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1682 xs->header = (struct ocfs2_xattr_header *)
1683 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1685 xs->header = (struct ocfs2_xattr_header *)
1686 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1687 xs->base = (void *)xs->header;
1688 xs->here = xs->header->xh_entries;
1690 /* Find the named attribute. */
1691 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1692 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1693 if (ret && ret != -ENODATA)
1695 xs->not_found = ret;
1702 * ocfs2_xattr_ibody_set()
1704 * Set, replace or remove an extended attribute into inode block.
1707 static int ocfs2_xattr_ibody_set(struct inode *inode,
1708 struct ocfs2_xattr_info *xi,
1709 struct ocfs2_xattr_search *xs)
1711 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1712 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1715 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1718 down_write(&oi->ip_alloc_sem);
1719 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1720 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1726 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1727 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1729 up_write(&oi->ip_alloc_sem);
1735 * ocfs2_xattr_block_find()
1737 * Find extended attribute in external block and
1738 * fill search info into struct ocfs2_xattr_search.
1740 static int ocfs2_xattr_block_find(struct inode *inode,
1743 struct ocfs2_xattr_search *xs)
1745 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1746 struct buffer_head *blk_bh = NULL;
1747 struct ocfs2_xattr_block *xb;
1750 if (!di->i_xattr_loc)
1753 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1754 le64_to_cpu(di->i_xattr_loc),
1755 &blk_bh, OCFS2_BH_CACHED, inode);
1760 /*Verify the signature of xattr block*/
1761 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1762 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1767 xs->xattr_bh = blk_bh;
1768 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1770 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1771 xs->header = &xb->xb_attrs.xb_header;
1772 xs->base = (void *)xs->header;
1773 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1774 xs->here = xs->header->xh_entries;
1776 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1778 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1782 if (ret && ret != -ENODATA) {
1783 xs->xattr_bh = NULL;
1786 xs->not_found = ret;
1795 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1796 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1799 static int ocfs2_restore_xattr_block(struct inode *inode,
1800 struct ocfs2_xattr_search *xs)
1804 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1805 struct ocfs2_xattr_block *xb =
1806 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1807 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1808 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1810 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1811 le16_to_cpu(el->l_next_free_rec) != 0);
1813 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1814 if (IS_ERR(handle)) {
1815 ret = PTR_ERR(handle);
1820 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1821 OCFS2_JOURNAL_ACCESS_WRITE);
1827 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1828 offsetof(struct ocfs2_xattr_block, xb_attrs));
1830 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1832 ocfs2_journal_dirty(handle, xs->xattr_bh);
1835 ocfs2_commit_trans(osb, handle);
1841 * ocfs2_xattr_block_set()
1843 * Set, replace or remove an extended attribute into external block.
1846 static int ocfs2_xattr_block_set(struct inode *inode,
1847 struct ocfs2_xattr_info *xi,
1848 struct ocfs2_xattr_search *xs)
1850 struct buffer_head *new_bh = NULL;
1851 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1852 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1853 struct ocfs2_alloc_context *meta_ac = NULL;
1854 handle_t *handle = NULL;
1855 struct ocfs2_xattr_block *xblk = NULL;
1856 u16 suballoc_bit_start;
1861 if (!xs->xattr_bh) {
1863 * Alloc one external block for extended attribute
1866 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1871 handle = ocfs2_start_trans(osb,
1872 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1873 if (IS_ERR(handle)) {
1874 ret = PTR_ERR(handle);
1878 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1879 OCFS2_JOURNAL_ACCESS_CREATE);
1885 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1886 &suballoc_bit_start, &num_got,
1893 new_bh = sb_getblk(inode->i_sb, first_blkno);
1894 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1896 ret = ocfs2_journal_access(handle, inode, new_bh,
1897 OCFS2_JOURNAL_ACCESS_CREATE);
1903 /* Initialize ocfs2_xattr_block */
1904 xs->xattr_bh = new_bh;
1905 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1906 memset(xblk, 0, inode->i_sb->s_blocksize);
1907 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1908 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1909 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1910 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1911 xblk->xb_blkno = cpu_to_le64(first_blkno);
1913 xs->header = &xblk->xb_attrs.xb_header;
1914 xs->base = (void *)xs->header;
1915 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1916 xs->here = xs->header->xh_entries;
1919 ret = ocfs2_journal_dirty(handle, new_bh);
1924 di->i_xattr_loc = cpu_to_le64(first_blkno);
1925 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1929 ocfs2_commit_trans(osb, handle);
1932 ocfs2_free_alloc_context(meta_ac);
1936 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1938 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1939 /* Set extended attribute into external block */
1940 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1941 if (!ret || ret != -ENOSPC)
1944 ret = ocfs2_xattr_create_index_block(inode, xs);
1949 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1950 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1951 ret = ocfs2_restore_xattr_block(inode, xs);
1961 * Set, replace or remove an extended attribute for this inode.
1962 * value is NULL to remove an existing extended attribute, else either
1963 * create or replace an extended attribute.
1965 int ocfs2_xattr_set(struct inode *inode,
1972 struct buffer_head *di_bh = NULL;
1973 struct ocfs2_dinode *di;
1975 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1977 struct ocfs2_xattr_info xi = {
1978 .name_index = name_index,
1981 .value_len = value_len,
1984 struct ocfs2_xattr_search xis = {
1985 .not_found = -ENODATA,
1988 struct ocfs2_xattr_search xbs = {
1989 .not_found = -ENODATA,
1992 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1995 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2000 xis.inode_bh = xbs.inode_bh = di_bh;
2001 di = (struct ocfs2_dinode *)di_bh->b_data;
2003 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2005 * Scan inode and external block to find the same name
2006 * extended attribute and collect search infomation.
2008 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2011 if (xis.not_found) {
2012 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2017 if (xis.not_found && xbs.not_found) {
2019 if (flags & XATTR_REPLACE)
2026 if (flags & XATTR_CREATE)
2031 /* Remove existing extended attribute */
2033 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2034 else if (!xbs.not_found)
2035 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2037 /* We always try to set extended attribute into inode first*/
2038 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2039 if (!ret && !xbs.not_found) {
2041 * If succeed and that extended attribute existing in
2042 * external block, then we will remove it.
2046 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2047 } else if (ret == -ENOSPC) {
2048 if (di->i_xattr_loc && !xbs.xattr_bh) {
2049 ret = ocfs2_xattr_block_find(inode, name_index,
2055 * If no space in inode, we will set extended attribute
2056 * into external block.
2058 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2061 if (!xis.not_found) {
2063 * If succeed and that extended attribute
2064 * existing in inode, we will remove it.
2068 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2073 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2074 ocfs2_inode_unlock(inode, 1);
2076 brelse(xbs.xattr_bh);
2077 for (i = 0; i < blk_per_bucket; i++)
2078 brelse(xbs.bucket.bhs[i]);
2083 static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode,
2085 const char *suffix_name)
2087 struct xattr_handler *handler = ocfs2_xattr_handler(name_index);
2088 char *prefix = handler->prefix;
2089 int prefix_len = strlen(handler->prefix);
2091 return ocfs2_xattr_name_hash(inode, prefix, prefix_len,
2092 (char *)suffix_name, strlen(suffix_name));
2096 * Find the xattr extent rec which may contains name_hash.
2097 * e_cpos will be the first name hash of the xattr rec.
2098 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2100 static int ocfs2_xattr_get_rec(struct inode *inode,
2105 struct ocfs2_extent_list *el)
2108 struct buffer_head *eb_bh = NULL;
2109 struct ocfs2_extent_block *eb;
2110 struct ocfs2_extent_rec *rec = NULL;
2113 if (el->l_tree_depth) {
2114 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2120 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2123 if (el->l_tree_depth) {
2124 ocfs2_error(inode->i_sb,
2125 "Inode %lu has non zero tree depth in "
2126 "xattr tree block %llu\n", inode->i_ino,
2127 (unsigned long long)eb_bh->b_blocknr);
2133 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2134 rec = &el->l_recs[i];
2136 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2137 e_blkno = le64_to_cpu(rec->e_blkno);
2143 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2144 "record (%u, %u, 0) in xattr", inode->i_ino,
2145 le32_to_cpu(rec->e_cpos),
2146 ocfs2_rec_clusters(el, rec));
2151 *p_blkno = le64_to_cpu(rec->e_blkno);
2152 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2154 *e_cpos = le32_to_cpu(rec->e_cpos);
2160 typedef int (xattr_bucket_func)(struct inode *inode,
2161 struct ocfs2_xattr_bucket *bucket,
2164 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2165 struct buffer_head *header_bh,
2172 int i, ret = 0, cmp = 1, block_off, new_offset;
2173 struct ocfs2_xattr_header *xh =
2174 (struct ocfs2_xattr_header *)header_bh->b_data;
2175 size_t name_len = strlen(name);
2176 struct ocfs2_xattr_entry *xe = NULL;
2177 struct buffer_head *name_bh = NULL;
2181 * We don't use binary search in the bucket because there
2182 * may be multiple entries with the same name hash.
2184 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2185 xe = &xh->xh_entries[i];
2187 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2189 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2192 cmp = name_index - ocfs2_xattr_get_type(xe);
2194 cmp = name_len - xe->xe_name_len;
2198 ret = ocfs2_xattr_bucket_get_name_value(inode,
2208 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2209 header_bh->b_blocknr + block_off,
2210 &name_bh, OCFS2_BH_CACHED, inode);
2215 xe_name = name_bh->b_data + new_offset;
2217 cmp = memcmp(name, xe_name, name_len);
2233 * Find the specified xattr entry in a series of buckets.
2234 * This series start from p_blkno and last for num_clusters.
2235 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2236 * the num of the valid buckets.
2238 * Return the buffer_head this xattr should reside in. And if the xattr's
2239 * hash is in the gap of 2 buckets, return the lower bucket.
2241 static int ocfs2_xattr_bucket_find(struct inode *inode,
2248 struct ocfs2_xattr_search *xs)
2251 struct buffer_head *bh = NULL;
2252 struct buffer_head *lower_bh = NULL;
2253 struct ocfs2_xattr_header *xh = NULL;
2254 struct ocfs2_xattr_entry *xe = NULL;
2256 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2257 int low_bucket = 0, bucket, high_bucket;
2261 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2262 &bh, OCFS2_BH_CACHED, inode);
2268 xh = (struct ocfs2_xattr_header *)bh->b_data;
2269 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2271 while (low_bucket <= high_bucket) {
2274 bucket = (low_bucket + high_bucket) / 2;
2276 blkno = p_blkno + bucket * blk_per_bucket;
2278 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
2279 &bh, OCFS2_BH_CACHED, inode);
2285 xh = (struct ocfs2_xattr_header *)bh->b_data;
2286 xe = &xh->xh_entries[0];
2287 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2288 high_bucket = bucket - 1;
2293 * Check whether the hash of the last entry in our
2294 * bucket is larger than the search one.
2296 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2297 last_hash = le32_to_cpu(xe->xe_name_hash);
2299 /* record lower_bh which may be the insert place. */
2304 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2305 low_bucket = bucket + 1;
2309 /* the searched xattr should reside in this bucket if exists. */
2310 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2311 name_index, name, name_hash,
2321 * Record the bucket we have found.
2322 * When the xattr's hash value is in the gap of 2 buckets, we will
2323 * always set it to the previous bucket.
2327 * We can't find any bucket whose first name_hash is less
2328 * than the find name_hash.
2330 BUG_ON(bh->b_blocknr != p_blkno);
2334 xs->bucket.bhs[0] = lower_bh;
2335 xs->bucket.xh = (struct ocfs2_xattr_header *)
2336 xs->bucket.bhs[0]->b_data;
2339 xs->header = xs->bucket.xh;
2340 xs->base = xs->bucket.bhs[0]->b_data;
2341 xs->end = xs->base + inode->i_sb->s_blocksize;
2345 * If we have found the xattr enty, read all the blocks in
2348 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2349 xs->bucket.bhs[0]->b_blocknr + 1,
2350 blk_per_bucket - 1, &xs->bucket.bhs[1],
2351 OCFS2_BH_CACHED, inode);
2357 xs->here = &xs->header->xh_entries[index];
2358 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2359 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2369 static int ocfs2_xattr_index_block_find(struct inode *inode,
2370 struct buffer_head *root_bh,
2373 struct ocfs2_xattr_search *xs)
2376 struct ocfs2_xattr_block *xb =
2377 (struct ocfs2_xattr_block *)root_bh->b_data;
2378 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2379 struct ocfs2_extent_list *el = &xb_root->xt_list;
2381 u32 first_hash, num_clusters = 0;
2382 u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name);
2384 if (le16_to_cpu(el->l_next_free_rec) == 0)
2387 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2388 name, name_hash, name_index);
2390 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2397 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2399 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2400 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2402 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2403 p_blkno, first_hash, num_clusters, xs);
2409 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2412 xattr_bucket_func *func,
2416 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2417 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2418 u32 num_buckets = clusters * bpc;
2419 struct ocfs2_xattr_bucket bucket;
2421 memset(&bucket, 0, sizeof(bucket));
2423 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2426 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2427 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2428 blkno, blk_per_bucket,
2429 bucket.bhs, OCFS2_BH_CACHED, inode);
2435 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2437 * The real bucket num in this series of blocks is stored
2438 * in the 1st bucket.
2441 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2443 mlog(0, "iterating xattr bucket %llu\n", blkno);
2445 ret = func(inode, &bucket, para);
2452 for (j = 0; j < blk_per_bucket; j++)
2453 brelse(bucket.bhs[j]);
2454 memset(&bucket, 0, sizeof(bucket));
2458 for (j = 0; j < blk_per_bucket; j++)
2459 brelse(bucket.bhs[j]);
2464 struct ocfs2_xattr_tree_list {
2469 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2470 struct ocfs2_xattr_header *xh,
2477 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2480 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2482 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2483 *new_offset = name_offset % inode->i_sb->s_blocksize;
2488 static int ocfs2_list_xattr_bucket(struct inode *inode,
2489 struct ocfs2_xattr_bucket *bucket,
2493 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2495 int i, block_off, new_offset;
2497 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2498 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2499 struct xattr_handler *handler =
2500 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2503 ret = ocfs2_xattr_bucket_get_name_value(inode,
2510 size = handler->list(inode, xl->buffer, xl->buffer_size,
2511 bucket->bhs[block_off]->b_data +
2513 entry->xe_name_len);
2515 if (size > xl->buffer_size)
2519 xl->buffer_size -= size;
2526 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2527 struct ocfs2_xattr_tree_root *xt,
2531 struct ocfs2_extent_list *el = &xt->xt_list;
2533 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2535 struct ocfs2_xattr_tree_list xl = {
2537 .buffer_size = buffer_size,
2540 if (le16_to_cpu(el->l_next_free_rec) == 0)
2543 while (name_hash > 0) {
2544 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2545 &e_cpos, &num_clusters, el);
2551 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2552 ocfs2_list_xattr_bucket,
2562 name_hash = e_cpos - 1;
2565 ret = buffer_size - xl.buffer_size;
2570 static int cmp_xe(const void *a, const void *b)
2572 const struct ocfs2_xattr_entry *l = a, *r = b;
2573 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2574 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2576 if (l_hash > r_hash)
2578 if (l_hash < r_hash)
2583 static void swap_xe(void *a, void *b, int size)
2585 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2588 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2589 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2593 * When the ocfs2_xattr_block is filled up, new bucket will be created
2594 * and all the xattr entries will be moved to the new bucket.
2595 * Note: we need to sort the entries since they are not saved in order
2596 * in the ocfs2_xattr_block.
2598 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2599 struct buffer_head *xb_bh,
2600 struct buffer_head *xh_bh,
2601 struct buffer_head *data_bh)
2603 int i, blocksize = inode->i_sb->s_blocksize;
2604 u16 offset, size, off_change;
2605 struct ocfs2_xattr_entry *xe;
2606 struct ocfs2_xattr_block *xb =
2607 (struct ocfs2_xattr_block *)xb_bh->b_data;
2608 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2609 struct ocfs2_xattr_header *xh =
2610 (struct ocfs2_xattr_header *)xh_bh->b_data;
2611 u16 count = le16_to_cpu(xb_xh->xh_count);
2612 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2614 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2615 (unsigned long long)xb_bh->b_blocknr,
2616 (unsigned long long)xh_bh->b_blocknr);
2618 memset(xh_bh->b_data, 0, blocksize);
2620 memset(data_bh->b_data, 0, blocksize);
2622 * Since the xe_name_offset is based on ocfs2_xattr_header,
2623 * there is a offset change corresponding to the change of
2624 * ocfs2_xattr_header's position.
2626 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2627 xe = &xb_xh->xh_entries[count - 1];
2628 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2629 size = blocksize - offset;
2631 /* copy all the names and values. */
2633 target = data_bh->b_data;
2634 memcpy(target + offset, src + offset, size);
2636 /* Init new header now. */
2637 xh->xh_count = xb_xh->xh_count;
2638 xh->xh_num_buckets = cpu_to_le16(1);
2639 xh->xh_name_value_len = cpu_to_le16(size);
2640 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2642 /* copy all the entries. */
2643 target = xh_bh->b_data;
2644 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2645 size = count * sizeof(struct ocfs2_xattr_entry);
2646 memcpy(target + offset, (char *)xb_xh + offset, size);
2648 /* Change the xe offset for all the xe because of the move. */
2649 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2650 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2651 for (i = 0; i < count; i++)
2652 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2654 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2655 offset, size, off_change);
2657 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2662 * After we move xattr from block to index btree, we have to
2663 * update ocfs2_xattr_search to the new xe and base.
2665 * When the entry is in xattr block, xattr_bh indicates the storage place.
2666 * While if the entry is in index b-tree, "bucket" indicates the
2667 * real place of the xattr.
2669 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2670 struct ocfs2_xattr_search *xs,
2671 struct buffer_head *old_bh,
2672 struct buffer_head *new_bh)
2675 char *buf = old_bh->b_data;
2676 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2677 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2678 int i, blocksize = inode->i_sb->s_blocksize;
2679 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2681 xs->bucket.bhs[0] = new_bh;
2683 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2684 xs->header = xs->bucket.xh;
2686 xs->base = new_bh->b_data;
2687 xs->end = xs->base + inode->i_sb->s_blocksize;
2689 if (!xs->not_found) {
2690 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2691 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2692 xs->bucket.bhs[0]->b_blocknr + 1,
2693 blk_per_bucket - 1, &xs->bucket.bhs[1],
2694 OCFS2_BH_CACHED, inode);
2700 i = xs->here - old_xh->xh_entries;
2701 xs->here = &xs->header->xh_entries[i];
2708 static int ocfs2_xattr_create_index_block(struct inode *inode,
2709 struct ocfs2_xattr_search *xs)
2711 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2715 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2716 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2717 struct ocfs2_alloc_context *data_ac;
2718 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2719 struct buffer_head *xb_bh = xs->xattr_bh;
2720 struct ocfs2_xattr_block *xb =
2721 (struct ocfs2_xattr_block *)xb_bh->b_data;
2722 struct ocfs2_xattr_tree_root *xr;
2723 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2724 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2726 mlog(0, "create xattr index block for %llu\n",
2727 (unsigned long long)xb_bh->b_blocknr);
2729 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2731 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2739 * We can use this lock for now, and maybe move to a dedicated mutex
2740 * if performance becomes a problem later.
2742 down_write(&oi->ip_alloc_sem);
2745 * 3 more credits, one for xattr block update, one for the 1st block
2746 * of the new xattr bucket and one for the value/data.
2749 handle = ocfs2_start_trans(osb, credits);
2750 if (IS_ERR(handle)) {
2751 ret = PTR_ERR(handle);
2756 ret = ocfs2_journal_access(handle, inode, xb_bh,
2757 OCFS2_JOURNAL_ACCESS_WRITE);
2763 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2770 * The bucket may spread in many blocks, and
2771 * we will only touch the 1st block and the last block
2772 * in the whole bucket(one for entry and one for data).
2774 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2776 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2778 xh_bh = sb_getblk(inode->i_sb, blkno);
2785 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2787 ret = ocfs2_journal_access(handle, inode, xh_bh,
2788 OCFS2_JOURNAL_ACCESS_CREATE);
2795 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2802 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2804 ret = ocfs2_journal_access(handle, inode, data_bh,
2805 OCFS2_JOURNAL_ACCESS_CREATE);
2812 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2814 ocfs2_journal_dirty(handle, xh_bh);
2816 ocfs2_journal_dirty(handle, data_bh);
2818 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->bhs[0]->b_blocknr;
2886 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2888 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2889 size_t blocksize = inode->i_sb->s_blocksize;
2891 struct buffer_head **bhs;
2892 struct ocfs2_xattr_entry *xe;
2894 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2899 ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
2900 OCFS2_BH_CACHED, inode);
2905 * In order to make the operation more efficient and generic,
2906 * we copy all the blocks into a contiguous memory and do the
2907 * defragment there, so if anything is error, we will not touch
2910 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2917 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2918 memcpy(buf, bhs[i]->b_data, blocksize);
2920 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2921 if (IS_ERR(handle)) {
2922 ret = PTR_ERR(handle);
2928 for (i = 0; i < blk_per_bucket; i++) {
2929 ret = ocfs2_journal_access(handle, inode, bhs[i],
2930 OCFS2_JOURNAL_ACCESS_WRITE);
2937 xh = (struct ocfs2_xattr_header *)bucket_buf;
2938 entries = (char *)xh->xh_entries;
2939 xh_free_start = le16_to_cpu(xh->xh_free_start);
2941 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2942 "xh_free_start = %u, xh_name_value_len = %u.\n",
2943 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2944 le16_to_cpu(xh->xh_name_value_len));
2947 * sort all the entries by their offset.
2948 * the largest will be the first, so that we can
2949 * move them to the end one by one.
2951 sort(entries, le16_to_cpu(xh->xh_count),
2952 sizeof(struct ocfs2_xattr_entry),
2953 cmp_xe_offset, swap_xe);
2955 /* Move all name/values to the end of the bucket. */
2956 xe = xh->xh_entries;
2957 end = OCFS2_XATTR_BUCKET_SIZE;
2958 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2959 offset = le16_to_cpu(xe->xe_name_offset);
2960 if (ocfs2_xattr_is_local(xe))
2961 value_len = OCFS2_XATTR_SIZE(
2962 le64_to_cpu(xe->xe_value_size));
2964 value_len = OCFS2_XATTR_ROOT_SIZE;
2965 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2968 * We must make sure that the name/value pair
2969 * exist in the same block. So adjust end to
2970 * the previous block end if needed.
2972 if (((end - len) / blocksize !=
2973 (end - 1) / blocksize))
2974 end = end - end % blocksize;
2976 if (end > offset + len) {
2977 memmove(bucket_buf + end - len,
2978 bucket_buf + offset, len);
2979 xe->xe_name_offset = cpu_to_le16(end - len);
2982 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2983 "bucket %llu\n", (unsigned long long)blkno);
2988 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2989 "bucket %llu\n", (unsigned long long)blkno);
2991 if (xh_free_start == end)
2994 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2995 xh->xh_free_start = cpu_to_le16(end);
2997 /* sort the entries by their name_hash. */
2998 sort(entries, le16_to_cpu(xh->xh_count),
2999 sizeof(struct ocfs2_xattr_entry),
3003 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3004 memcpy(bhs[i]->b_data, buf, blocksize);
3005 ocfs2_journal_dirty(handle, bhs[i]);
3009 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3013 for (i = 0; i < blk_per_bucket; i++)
3023 * Move half nums of the xattr bucket in the previous cluster to this new
3024 * cluster. We only touch the last cluster of the previous extend record.
3026 * first_bh is the first buffer_head of a series of bucket in the same
3027 * extent rec and header_bh is the header of one bucket in this cluster.
3028 * They will be updated if we move the data header_bh contains to the new
3029 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3031 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3033 struct buffer_head **first_bh,
3034 struct buffer_head **header_bh,
3040 int i, ret, credits;
3041 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3042 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3043 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3044 int blocksize = inode->i_sb->s_blocksize;
3045 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3046 struct ocfs2_xattr_header *new_xh;
3047 struct ocfs2_xattr_header *xh =
3048 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3050 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3051 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3053 prev_bh = *first_bh;
3055 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3057 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3059 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3060 prev_blkno, new_blkno);
3063 * We need to update the 1st half of the new cluster and
3064 * 1 more for the update of the 1st bucket of the previous
3067 credits = bpc / 2 + 1;
3068 ret = ocfs2_extend_trans(handle, credits);
3074 ret = ocfs2_journal_access(handle, inode, prev_bh,
3075 OCFS2_JOURNAL_ACCESS_WRITE);
3081 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3082 old_bh = new_bh = NULL;
3083 new_bh = sb_getblk(inode->i_sb, new_blkno);
3090 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3092 ret = ocfs2_journal_access(handle, inode, new_bh,
3093 OCFS2_JOURNAL_ACCESS_CREATE);
3100 ret = ocfs2_read_block(osb, prev_blkno,
3101 &old_bh, OCFS2_BH_CACHED, inode);
3108 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3111 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3112 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3115 *first_hash = le32_to_cpu(
3116 new_xh->xh_entries[0].xe_name_hash);
3117 new_first_bh = new_bh;
3118 get_bh(new_first_bh);
3121 ocfs2_journal_dirty(handle, new_bh);
3123 if (*header_bh == old_bh) {
3125 *header_bh = new_bh;
3129 *first_bh = new_first_bh;
3136 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3138 ocfs2_journal_dirty(handle, prev_bh);
3141 brelse(new_first_bh);
3145 static int ocfs2_read_xattr_bucket(struct inode *inode,
3147 struct buffer_head **bhs,
3151 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3154 return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
3155 blk_per_bucket, bhs,
3156 OCFS2_BH_CACHED, inode);
3158 for (i = 0; i < blk_per_bucket; i++) {
3159 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3160 if (bhs[i] == NULL) {
3165 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3172 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3173 * first_hash will record the 1st hash of the new bucket.
3175 static int ocfs2_half_xattr_bucket(struct inode *inode,
3180 int new_bucket_head)
3183 u16 count, start, len, name_value_len, xe_len, name_offset;
3184 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3185 struct buffer_head **s_bhs, **t_bhs = NULL;
3186 struct ocfs2_xattr_header *xh;
3187 struct ocfs2_xattr_entry *xe;
3188 int blocksize = inode->i_sb->s_blocksize;
3190 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3193 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3197 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3203 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3204 OCFS2_JOURNAL_ACCESS_WRITE);
3210 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3216 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3222 for (i = 0; i < blk_per_bucket; i++) {
3223 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3224 OCFS2_JOURNAL_ACCESS_CREATE);
3231 /* copy the whole bucket to the new first. */
3232 for (i = 0; i < blk_per_bucket; i++)
3233 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3235 /* update the new bucket. */
3236 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3237 count = le16_to_cpu(xh->xh_count);
3241 * Calculate the total name/value len and xh_free_start for
3242 * the old bucket first.
3244 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3246 for (i = 0; i < start; i++) {
3247 xe = &xh->xh_entries[i];
3248 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3249 if (ocfs2_xattr_is_local(xe))
3251 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3253 xe_len += OCFS2_XATTR_ROOT_SIZE;
3254 name_value_len += xe_len;
3255 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3256 name_offset = le16_to_cpu(xe->xe_name_offset);
3260 * Now begin the modification to the new bucket.
3262 * In the new bucket, We just move the xattr entry to the beginning
3263 * and don't touch the name/value. So there will be some holes in the
3264 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3267 xe = &xh->xh_entries[start];
3268 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3269 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3270 (int)((char *)xe - (char *)xh),
3271 (int)((char *)xh->xh_entries - (char *)xh));
3272 memmove((char *)xh->xh_entries, (char *)xe, len);
3273 xe = &xh->xh_entries[count - start];
3274 len = sizeof(struct ocfs2_xattr_entry) * start;
3275 memset((char *)xe, 0, len);
3277 le16_add_cpu(&xh->xh_count, -start);
3278 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3280 /* Calculate xh_free_start for the new bucket. */
3281 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3282 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3283 xe = &xh->xh_entries[i];
3284 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3285 if (ocfs2_xattr_is_local(xe))
3287 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3289 xe_len += OCFS2_XATTR_ROOT_SIZE;
3290 if (le16_to_cpu(xe->xe_name_offset) <
3291 le16_to_cpu(xh->xh_free_start))
3292 xh->xh_free_start = xe->xe_name_offset;
3295 /* set xh->xh_num_buckets for the new xh. */
3296 if (new_bucket_head)
3297 xh->xh_num_buckets = cpu_to_le16(1);
3299 xh->xh_num_buckets = 0;
3301 for (i = 0; i < blk_per_bucket; i++) {
3302 ocfs2_journal_dirty(handle, t_bhs[i]);
3307 /* store the first_hash of the new bucket. */
3309 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3312 * Now only update the 1st block of the old bucket.
3313 * Please note that the entry has been sorted already above.
3315 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3316 memset(&xh->xh_entries[start], 0,
3317 sizeof(struct ocfs2_xattr_entry) * (count - start));
3318 xh->xh_count = cpu_to_le16(start);
3319 xh->xh_free_start = cpu_to_le16(name_offset);
3320 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3322 ocfs2_journal_dirty(handle, s_bhs[0]);
3328 for (i = 0; i < blk_per_bucket; i++)
3334 for (i = 0; i < blk_per_bucket; i++)
3343 * Copy xattr from one bucket to another bucket.
3345 * The caller must make sure that the journal transaction
3346 * has enough space for journaling.
3348 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3355 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3356 int blocksize = inode->i_sb->s_blocksize;
3357 struct buffer_head **s_bhs, **t_bhs = NULL;
3359 BUG_ON(s_blkno == t_blkno);
3361 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3362 s_blkno, t_blkno, t_is_new);
3364 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3369 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3373 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3380 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3384 for (i = 0; i < blk_per_bucket; i++) {
3385 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3386 OCFS2_JOURNAL_ACCESS_WRITE);
3391 for (i = 0; i < blk_per_bucket; i++) {
3392 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3393 ocfs2_journal_dirty(handle, t_bhs[i]);
3398 for (i = 0; i < blk_per_bucket; i++)
3404 for (i = 0; i < blk_per_bucket; i++)
3413 * Copy one xattr cluster from src_blk to to_blk.
3414 * The to_blk will become the first bucket header of the cluster, so its
3415 * xh_num_buckets will be initialized as the bucket num in the cluster.
3417 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3419 struct buffer_head *first_bh,
3424 int i, ret, credits;
3425 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3426 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3427 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3428 struct buffer_head *bh = NULL;
3429 struct ocfs2_xattr_header *xh;
3430 u64 to_blk_start = to_blk;
3432 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3435 * We need to update the new cluster and 1 more for the update of
3436 * the 1st bucket of the previous extent rec.
3439 ret = ocfs2_extend_trans(handle, credits);
3445 ret = ocfs2_journal_access(handle, inode, first_bh,
3446 OCFS2_JOURNAL_ACCESS_WRITE);
3452 for (i = 0; i < num_buckets; i++) {
3453 ret = ocfs2_cp_xattr_bucket(inode, handle,
3454 src_blk, to_blk, 1);
3460 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3461 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3464 /* update the old bucket header. */
3465 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3466 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3468 ocfs2_journal_dirty(handle, first_bh);
3470 /* update the new bucket header. */
3471 ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
3477 ret = ocfs2_journal_access(handle, inode, bh,
3478 OCFS2_JOURNAL_ACCESS_WRITE);
3484 xh = (struct ocfs2_xattr_header *)bh->b_data;
3485 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3487 ocfs2_journal_dirty(handle, bh);
3490 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3497 * Move half of the xattrs in this cluster to the new cluster.
3498 * This function should only be called when bucket size == cluster size.
3499 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3501 static int ocfs2_half_xattr_cluster(struct inode *inode,
3507 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3508 int ret, credits = 2 * blk_per_bucket;
3510 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3512 ret = ocfs2_extend_trans(handle, credits);
3518 /* Move half of the xattr in start_blk to the next bucket. */
3519 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3520 new_blk, first_hash, 1);
3524 * Move some xattrs from the old cluster to the new one since they are not
3525 * contiguous in ocfs2 xattr tree.
3527 * new_blk starts a new separate cluster, and we will move some xattrs from
3528 * prev_blk to it. v_start will be set as the first name hash value in this
3529 * new cluster so that it can be used as e_cpos during tree insertion and
3530 * don't collide with our original b-tree operations. first_bh and header_bh
3531 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3532 * to extend the insert bucket.
3534 * The problem is how much xattr should we move to the new one and when should
3535 * we update first_bh and header_bh?
3536 * 1. If cluster size > bucket size, that means the previous cluster has more
3537 * than 1 bucket, so just move half nums of bucket into the new cluster and
3538 * update the first_bh and header_bh if the insert bucket has been moved
3539 * to the new cluster.
3540 * 2. If cluster_size == bucket_size:
3541 * a) If the previous extent rec has more than one cluster and the insert
3542 * place isn't in the last cluster, copy the entire last cluster to the
3543 * new one. This time, we don't need to upate the first_bh and header_bh
3544 * since they will not be moved into the new cluster.
3545 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3546 * the new one. And we set the extend flag to zero if the insert place is
3547 * moved into the new allocated cluster since no extend is needed.
3549 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3551 struct buffer_head **first_bh,
3552 struct buffer_head **header_bh,
3560 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3562 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3563 prev_blk, prev_clusters, new_blk);
3565 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3566 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3575 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3577 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3578 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3582 ret = ocfs2_half_xattr_cluster(inode, handle,
3586 if ((*header_bh)->b_blocknr == last_blk && extend)
3595 * Add a new cluster for xattr storage.
3597 * If the new cluster is contiguous with the previous one, it will be
3598 * appended to the same extent record, and num_clusters will be updated.
3599 * If not, we will insert a new extent for it and move some xattrs in
3600 * the last cluster into the new allocated one.
3601 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3602 * lose the benefits of hashing because we'll have to search large leaves.
3603 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3606 * first_bh is the first block of the previous extent rec and header_bh
3607 * indicates the bucket we will insert the new xattrs. They will be updated
3608 * when the header_bh is moved into the new cluster.
3610 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3611 struct buffer_head *root_bh,
3612 struct buffer_head **first_bh,
3613 struct buffer_head **header_bh,
3620 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3621 u32 prev_clusters = *num_clusters;
3622 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3624 handle_t *handle = NULL;
3625 struct ocfs2_alloc_context *data_ac = NULL;
3626 struct ocfs2_alloc_context *meta_ac = NULL;
3627 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3628 struct ocfs2_extent_tree et;
3630 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3631 "previous xattr blkno = %llu\n",
3632 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3633 prev_cpos, prev_blkno);
3635 ocfs2_get_xattr_tree_extent_tree(&et, inode, root_bh);
3637 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3638 &data_ac, &meta_ac);
3644 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3646 handle = ocfs2_start_trans(osb, credits);
3647 if (IS_ERR(handle)) {
3648 ret = PTR_ERR(handle);
3654 ret = ocfs2_journal_access(handle, inode, root_bh,
3655 OCFS2_JOURNAL_ACCESS_WRITE);
3661 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3662 clusters_to_add, &bit_off, &num_bits);
3669 BUG_ON(num_bits > clusters_to_add);
3671 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3672 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3673 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3675 if (prev_blkno + prev_clusters * bpc == block &&
3676 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3677 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3679 * If this cluster is contiguous with the old one and
3680 * adding this new cluster, we don't surpass the limit of
3681 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3682 * initialized and used like other buckets in the previous
3684 * So add it as a contiguous one. The caller will handle
3687 v_start = prev_cpos + prev_clusters;
3688 *num_clusters = prev_clusters + num_bits;
3689 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3692 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3707 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3708 num_bits, block, v_start);
3709 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3710 num_bits, 0, meta_ac);
3716 ret = ocfs2_journal_dirty(handle, root_bh);
3724 ocfs2_commit_trans(osb, handle);
3726 ocfs2_free_alloc_context(data_ac);
3728 ocfs2_free_alloc_context(meta_ac);
3730 ocfs2_put_extent_tree(&et);
3735 * Extend a new xattr bucket and move xattrs to the end one by one until
3736 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3738 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3739 struct buffer_head *first_bh,
3740 struct buffer_head *start_bh,
3744 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3745 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3746 u64 start_blk = start_bh->b_blocknr, end_blk;
3747 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3749 struct ocfs2_xattr_header *first_xh =
3750 (struct ocfs2_xattr_header *)first_bh->b_data;
3751 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3753 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3754 "from %llu, len = %u\n", start_blk,
3755 (unsigned long long)first_bh->b_blocknr, num_clusters);
3757 BUG_ON(bucket >= num_buckets);
3759 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3762 * We will touch all the buckets after the start_bh(include it).
3763 * Add one more bucket and modify the first_bh.
3765 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3766 handle = ocfs2_start_trans(osb, credits);
3767 if (IS_ERR(handle)) {
3768 ret = PTR_ERR(handle);
3774 ret = ocfs2_journal_access(handle, inode, first_bh,
3775 OCFS2_JOURNAL_ACCESS_WRITE);
3781 while (end_blk != start_blk) {
3782 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3783 end_blk + blk_per_bucket, 0);
3786 end_blk -= blk_per_bucket;
3789 /* Move half of the xattr in start_blk to the next bucket. */
3790 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3791 start_blk + blk_per_bucket, NULL, 0);
3793 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3794 ocfs2_journal_dirty(handle, first_bh);
3797 ocfs2_commit_trans(osb, handle);
3803 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3804 * xb_bh is the ocfs2_xattr_block.
3805 * We will move all the buckets starting from header_bh to the next place. As
3806 * for this one, half num of its xattrs will be moved to the next one.
3808 * We will allocate a new cluster if current cluster is full and adjust
3809 * header_bh and first_bh if the insert place is moved to the new cluster.
3811 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3812 struct buffer_head *xb_bh,
3813 struct buffer_head *header_bh)
3815 struct ocfs2_xattr_header *first_xh = NULL;
3816 struct buffer_head *first_bh = NULL;
3817 struct ocfs2_xattr_block *xb =
3818 (struct ocfs2_xattr_block *)xb_bh->b_data;
3819 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3820 struct ocfs2_extent_list *el = &xb_root->xt_list;
3821 struct ocfs2_xattr_header *xh =
3822 (struct ocfs2_xattr_header *)header_bh->b_data;
3823 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3824 struct super_block *sb = inode->i_sb;
3825 struct ocfs2_super *osb = OCFS2_SB(sb);
3826 int ret, num_buckets, extend = 1;
3828 u32 e_cpos, num_clusters;
3830 mlog(0, "Add new xattr bucket starting form %llu\n",
3831 (unsigned long long)header_bh->b_blocknr);
3834 * Add refrence for header_bh here because it may be
3835 * changed in ocfs2_add_new_xattr_cluster and we need
3836 * to free it in the end.
3840 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3847 ret = ocfs2_read_block(osb, p_blkno,
3848 &first_bh, OCFS2_BH_CACHED, inode);
3854 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3855 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3857 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3858 ret = ocfs2_add_new_xattr_cluster(inode,
3873 ret = ocfs2_extend_xattr_bucket(inode,
3885 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3886 struct ocfs2_xattr_bucket *bucket,
3889 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3891 offs = offs % inode->i_sb->s_blocksize;
3892 return bucket->bhs[block_off]->b_data + offs;
3896 * Handle the normal xattr set, including replace, delete and new.
3897 * When the bucket is empty, "is_empty" is set and the caller can
3900 * Note: "local" indicates the real data's locality. So we can't
3901 * just its bucket locality by its length.
3903 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3904 struct ocfs2_xattr_info *xi,
3905 struct ocfs2_xattr_search *xs,
3910 struct ocfs2_xattr_entry *last, *xe;
3911 int name_len = strlen(xi->name);
3912 struct ocfs2_xattr_header *xh = xs->header;
3913 u16 count = le16_to_cpu(xh->xh_count), start;
3914 size_t blocksize = inode->i_sb->s_blocksize;
3916 size_t offs, size, new_size;
3918 last = &xh->xh_entries[count];
3919 if (!xs->not_found) {
3921 offs = le16_to_cpu(xe->xe_name_offset);
3922 if (ocfs2_xattr_is_local(xe))
3923 size = OCFS2_XATTR_SIZE(name_len) +
3924 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3926 size = OCFS2_XATTR_SIZE(name_len) +
3927 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3930 * If the new value will be stored outside, xi->value has been
3931 * initalized as an empty ocfs2_xattr_value_root, and the same
3932 * goes with xi->value_len, so we can set new_size safely here.
3933 * See ocfs2_xattr_set_in_bucket.
3935 new_size = OCFS2_XATTR_SIZE(name_len) +
3936 OCFS2_XATTR_SIZE(xi->value_len);
3938 le16_add_cpu(&xh->xh_name_value_len, -size);
3940 if (new_size > size)
3941 goto set_new_name_value;
3943 /* Now replace the old value with new one. */
3945 xe->xe_value_size = cpu_to_le64(xi->value_len);
3947 xe->xe_value_size = 0;
3949 val = ocfs2_xattr_bucket_get_val(inode,
3951 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3952 size - OCFS2_XATTR_SIZE(name_len));
3953 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3954 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3955 xi->value, xi->value_len);
3957 le16_add_cpu(&xh->xh_name_value_len, new_size);
3958 ocfs2_xattr_set_local(xe, local);
3961 /* Remove the old entry. */
3964 (void *)last - (void *)xe);
3965 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
3966 le16_add_cpu(&xh->xh_count, -1);
3967 if (xh->xh_count == 0 && is_empty)
3972 /* find a new entry for insert. */
3973 int low = 0, high = count - 1, tmp;
3974 struct ocfs2_xattr_entry *tmp_xe;
3976 while (low <= high) {
3977 tmp = (low + high) / 2;
3978 tmp_xe = &xh->xh_entries[tmp];
3980 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
3982 else if (name_hash <
3983 le32_to_cpu(tmp_xe->xe_name_hash))
3989 xe = &xh->xh_entries[low];
3991 memmove(xe + 1, xe, (void *)last - (void *)xe);
3993 le16_add_cpu(&xh->xh_count, 1);
3994 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
3995 xe->xe_name_hash = cpu_to_le32(name_hash);
3996 xe->xe_name_len = name_len;
3997 ocfs2_xattr_set_type(xe, xi->name_index);
4001 /* Insert the new name+value. */
4002 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4005 * We must make sure that the name/value pair
4006 * exists in the same block.
4008 offs = le16_to_cpu(xh->xh_free_start);
4009 start = offs - size;
4011 if (start >> inode->i_sb->s_blocksize_bits !=
4012 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4013 offs = offs - offs % blocksize;
4014 xh->xh_free_start = cpu_to_le16(offs);
4017 val = ocfs2_xattr_bucket_get_val(inode,
4018 &xs->bucket, offs - size);
4019 xe->xe_name_offset = cpu_to_le16(offs - size);
4021 memset(val, 0, size);
4022 memcpy(val, xi->name, name_len);
4023 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4025 xe->xe_value_size = cpu_to_le64(xi->value_len);
4026 ocfs2_xattr_set_local(xe, local);
4028 le16_add_cpu(&xh->xh_free_start, -size);
4029 le16_add_cpu(&xh->xh_name_value_len, size);
4034 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4036 struct ocfs2_xattr_search *xs,
4037 struct buffer_head **bhs,
4040 int ret = 0, off, block_off;
4041 struct ocfs2_xattr_entry *xe = xs->here;
4044 * First calculate all the blocks we should journal_access
4045 * and journal_dirty. The first block should always be touched.
4047 ret = ocfs2_journal_dirty(handle, bhs[0]);
4051 /* calc the data. */
4052 off = le16_to_cpu(xe->xe_name_offset);
4053 block_off = off >> inode->i_sb->s_blocksize_bits;
4054 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4062 * Set the xattr entry in the specified bucket.
4063 * The bucket is indicated by xs->bucket and it should have the enough
4064 * space for the xattr insertion.
4066 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4067 struct ocfs2_xattr_info *xi,
4068 struct ocfs2_xattr_search *xs,
4074 handle_t *handle = NULL;
4075 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4076 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4078 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4079 (unsigned long)xi->value_len, xi->name_index,
4080 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4082 if (!xs->bucket.bhs[1]) {
4083 ret = ocfs2_read_blocks(osb,
4084 xs->bucket.bhs[0]->b_blocknr + 1,
4085 blk_per_bucket - 1, &xs->bucket.bhs[1],
4086 OCFS2_BH_CACHED, inode);
4093 handle = ocfs2_start_trans(osb, blk_per_bucket);
4094 if (IS_ERR(handle)) {
4095 ret = PTR_ERR(handle);
4101 for (i = 0; i < blk_per_bucket; i++) {
4102 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4103 OCFS2_JOURNAL_ACCESS_WRITE);
4110 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash,
4111 local, bucket_empty);
4113 /*Only dirty the blocks we have touched in set xattr. */
4114 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4115 xs->bucket.bhs, blk_per_bucket);
4119 ocfs2_commit_trans(osb, handle);
4124 static int ocfs2_xattr_value_update_size(struct inode *inode,
4125 struct buffer_head *xe_bh,
4126 struct ocfs2_xattr_entry *xe,
4130 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4131 handle_t *handle = NULL;
4133 handle = ocfs2_start_trans(osb, 1);
4134 if (handle == NULL) {
4140 ret = ocfs2_journal_access(handle, inode, xe_bh,
4141 OCFS2_JOURNAL_ACCESS_WRITE);
4147 xe->xe_value_size = cpu_to_le64(new_size);
4149 ret = ocfs2_journal_dirty(handle, xe_bh);
4154 ocfs2_commit_trans(osb, handle);
4160 * Truncate the specified xe_off entry in xattr bucket.
4161 * bucket is indicated by header_bh and len is the new length.
4162 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4164 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4166 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4167 struct buffer_head *header_bh,
4173 struct buffer_head *value_bh = NULL;
4174 struct ocfs2_xattr_value_root *xv;
4175 struct ocfs2_xattr_entry *xe;
4176 struct ocfs2_xattr_header *xh =
4177 (struct ocfs2_xattr_header *)header_bh->b_data;
4178 size_t blocksize = inode->i_sb->s_blocksize;
4180 xe = &xh->xh_entries[xe_off];
4182 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4184 offset = le16_to_cpu(xe->xe_name_offset) +
4185 OCFS2_XATTR_SIZE(xe->xe_name_len);
4187 value_blk = offset / blocksize;
4189 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4190 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4191 value_blk += header_bh->b_blocknr;
4193 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
4194 &value_bh, OCFS2_BH_CACHED, inode);
4200 xv = (struct ocfs2_xattr_value_root *)
4201 (value_bh->b_data + offset % blocksize);
4203 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4204 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4205 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4211 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4222 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4223 struct ocfs2_xattr_search *xs,
4227 struct ocfs2_xattr_entry *xe = xs->here;
4228 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4230 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4232 offset = xe - xh->xh_entries;
4233 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4241 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4242 struct ocfs2_xattr_search *xs,
4247 struct ocfs2_xattr_value_root *xv;
4248 struct ocfs2_xattr_entry *xe = xs->here;
4250 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4252 offset = le16_to_cpu(xe->xe_name_offset) +
4253 OCFS2_XATTR_SIZE(xe->xe_name_len);
4255 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4257 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4261 * Remove the xattr bucket pointed by bucket_bh.
4262 * All the buckets after it in the same xattr extent rec will be
4263 * move forward one by one.
4265 static int ocfs2_rm_xattr_bucket(struct inode *inode,
4266 struct buffer_head *first_bh,
4267 struct ocfs2_xattr_bucket *bucket)
4269 int ret = 0, credits;
4270 struct ocfs2_xattr_header *xh =
4271 (struct ocfs2_xattr_header *)first_bh->b_data;
4272 u16 bucket_num = le16_to_cpu(xh->xh_num_buckets);
4273 u64 end, start = bucket->bhs[0]->b_blocknr;
4274 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4276 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4278 end = first_bh->b_blocknr + (bucket_num - 1) * blk_per_bucket;
4280 mlog(0, "rm xattr bucket %llu\n", start);
4282 * We need to update the first xattr_header and all the buckets starting
4283 * from start in this xattr rec.
4285 * XXX: Should we empty the old last bucket here?
4287 credits = 1 + end - start;
4288 handle = ocfs2_start_trans(osb, credits);
4289 if (IS_ERR(handle)) {
4290 ret = PTR_ERR(handle);
4295 ret = ocfs2_journal_access(handle, inode, first_bh,
4296 OCFS2_JOURNAL_ACCESS_WRITE);
4303 while (start < end) {
4304 ret = ocfs2_cp_xattr_bucket(inode, handle,
4305 start + blk_per_bucket,
4311 start += blk_per_bucket;
4314 /* update the first_bh. */
4315 xh->xh_num_buckets = cpu_to_le16(bucket_num - 1);
4316 ocfs2_journal_dirty(handle, first_bh);
4319 ocfs2_commit_trans(osb, handle);
4323 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4324 struct buffer_head *root_bh,
4330 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4331 struct inode *tl_inode = osb->osb_tl_inode;
4333 struct ocfs2_xattr_block *xb =
4334 (struct ocfs2_xattr_block *)root_bh->b_data;
4335 struct ocfs2_alloc_context *meta_ac = NULL;
4336 struct ocfs2_cached_dealloc_ctxt dealloc;
4337 struct ocfs2_extent_tree et;
4339 ocfs2_get_xattr_tree_extent_tree(&et, inode, root_bh);
4341 ocfs2_init_dealloc_ctxt(&dealloc);
4343 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4344 cpos, len, (unsigned long long)blkno);
4346 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4348 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4350 ocfs2_put_extent_tree(&et);
4355 mutex_lock(&tl_inode->i_mutex);
4357 if (ocfs2_truncate_log_needs_flush(osb)) {
4358 ret = __ocfs2_flush_truncate_log(osb);
4365 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4366 if (handle == NULL) {
4372 ret = ocfs2_journal_access(handle, inode, root_bh,
4373 OCFS2_JOURNAL_ACCESS_WRITE);
4379 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4386 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4388 ret = ocfs2_journal_dirty(handle, root_bh);
4394 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4399 ocfs2_commit_trans(osb, handle);
4401 ocfs2_schedule_truncate_log_flush(osb, 1);
4403 mutex_unlock(&tl_inode->i_mutex);
4406 ocfs2_free_alloc_context(meta_ac);
4408 ocfs2_run_deallocs(osb, &dealloc);
4410 ocfs2_put_extent_tree(&et);
4415 * Free the xattr bucket indicated by xs->bucket and if all the buckets
4416 * in the clusters is free, free the clusters also.
4418 static int ocfs2_xattr_bucket_shrink(struct inode *inode,
4419 struct ocfs2_xattr_info *xi,
4420 struct ocfs2_xattr_search *xs,
4424 u32 e_cpos, num_clusters;
4426 struct buffer_head *first_bh = NULL;
4427 struct ocfs2_xattr_header *first_xh;
4428 struct ocfs2_xattr_block *xb =
4429 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
4431 BUG_ON(xs->header->xh_count != 0);
4433 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4434 &e_cpos, &num_clusters,
4435 &xb->xb_attrs.xb_root.xt_list);
4441 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
4442 &first_bh, OCFS2_BH_CACHED, inode);
4448 ret = ocfs2_rm_xattr_bucket(inode, first_bh, &xs->bucket);
4454 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4455 if (first_xh->xh_num_buckets == 0)
4456 ret = ocfs2_rm_xattr_cluster(inode, xs->xattr_bh,
4465 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4466 struct ocfs2_xattr_search *xs)
4468 handle_t *handle = NULL;
4469 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4470 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4471 le16_to_cpu(xh->xh_count) - 1];
4474 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4475 if (IS_ERR(handle)) {
4476 ret = PTR_ERR(handle);
4481 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4482 OCFS2_JOURNAL_ACCESS_WRITE);
4488 /* Remove the old entry. */
4489 memmove(xs->here, xs->here + 1,
4490 (void *)last - (void *)xs->here);
4491 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4492 le16_add_cpu(&xh->xh_count, -1);
4494 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4498 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4502 * Set the xattr name/value in the bucket specified in xs.
4504 * As the new value in xi may be stored in the bucket or in an outside cluster,
4505 * we divide the whole process into 3 steps:
4506 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4507 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4508 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4509 * 4. If the clusters for the new outside value can't be allocated, we need
4510 * to free the xattr we allocated in set.
4512 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4513 struct ocfs2_xattr_info *xi,
4514 struct ocfs2_xattr_search *xs)
4516 int ret, local = 1, bucket_empty = 0;
4518 char *val = (char *)xi->value;
4519 struct ocfs2_xattr_entry *xe = xs->here;
4520 u32 name_hash = ocfs2_xattr_hash_by_name(inode,
4521 xi->name_index, xi->name);
4523 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4525 * We need to truncate the xattr storage first.
4527 * If both the old and new value are stored to
4528 * outside block, we only need to truncate
4529 * the storage and then set the value outside.
4531 * If the new value should be stored within block,
4532 * we should free all the outside block first and
4533 * the modification to the xattr block will be done
4534 * by following steps.
4536 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4537 value_len = xi->value_len;
4541 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4547 goto set_value_outside;
4550 value_len = xi->value_len;
4551 /* So we have to handle the inside block change now. */
4552 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4554 * If the new value will be stored outside of block,
4555 * initalize a new empty value root and insert it first.
4558 xi->value = &def_xv;
4559 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4562 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash,
4563 local, &bucket_empty);
4569 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4570 /* allocate the space now for the outside block storage. */
4571 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4576 if (xs->not_found) {
4578 * We can't allocate enough clusters for outside
4579 * storage and we have allocated xattr already,
4580 * so need to remove it.
4582 ocfs2_xattr_bucket_remove_xs(inode, xs);
4588 ret = ocfs2_xattr_bucket_shrink(inode, xi,
4594 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4599 /* check whether the xattr bucket is filled up with the same hash value. */
4600 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4601 struct ocfs2_xattr_bucket *bucket)
4603 struct ocfs2_xattr_header *xh = bucket->xh;
4605 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4606 xh->xh_entries[0].xe_name_hash) {
4607 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4609 (unsigned long long)bucket->bhs[0]->b_blocknr,
4610 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4617 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4618 struct ocfs2_xattr_info *xi,
4619 struct ocfs2_xattr_search *xs)
4621 struct ocfs2_xattr_header *xh;
4622 struct ocfs2_xattr_entry *xe;
4623 u16 count, header_size, xh_free_start;
4624 int i, free, max_free, need, old;
4625 size_t value_size = 0, name_len = strlen(xi->name);
4626 size_t blocksize = inode->i_sb->s_blocksize;
4627 int ret, allocation = 0;
4628 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4630 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4634 count = le16_to_cpu(xh->xh_count);
4635 xh_free_start = le16_to_cpu(xh->xh_free_start);
4636 header_size = sizeof(struct ocfs2_xattr_header) +
4637 count * sizeof(struct ocfs2_xattr_entry);
4638 max_free = OCFS2_XATTR_BUCKET_SIZE -
4639 le16_to_cpu(xh->xh_name_value_len) - header_size;
4641 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4642 "of %u which exceed block size\n",
4643 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4646 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4647 value_size = OCFS2_XATTR_ROOT_SIZE;
4649 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4652 need = sizeof(struct ocfs2_xattr_entry) +
4653 OCFS2_XATTR_SIZE(name_len) + value_size;
4655 need = value_size + OCFS2_XATTR_SIZE(name_len);
4658 * We only replace the old value if the new length is smaller
4659 * than the old one. Otherwise we will allocate new space in the
4660 * bucket to store it.
4663 if (ocfs2_xattr_is_local(xe))
4664 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4666 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4668 if (old >= value_size)
4672 free = xh_free_start - header_size;
4674 * We need to make sure the new name/value pair
4675 * can exist in the same block.
4677 if (xh_free_start % blocksize < need)
4678 free -= xh_free_start % blocksize;
4680 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4681 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4682 " %u\n", xs->not_found,
4683 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4684 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4685 le16_to_cpu(xh->xh_name_value_len));
4687 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4688 if (need <= max_free &&
4689 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4691 * We can create the space by defragment. Since only the
4692 * name/value will be moved, the xe shouldn't be changed
4695 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4701 xh_free_start = le16_to_cpu(xh->xh_free_start);
4702 free = xh_free_start - header_size;
4703 if (xh_free_start % blocksize < need)
4704 free -= xh_free_start % blocksize;
4709 mlog(0, "Can't get enough space for xattr insert by "
4710 "defragment. Need %u bytes, but we have %d, so "
4711 "allocate new bucket for it.\n", need, free);
4715 * We have to add new buckets or clusters and one
4716 * allocation should leave us enough space for insert.
4721 * We do not allow for overlapping ranges between buckets. And
4722 * the maximum number of collisions we will allow for then is
4723 * one bucket's worth, so check it here whether we need to
4724 * add a new bucket for the insert.
4726 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4732 ret = ocfs2_add_new_xattr_bucket(inode,
4740 for (i = 0; i < blk_per_bucket; i++)
4741 brelse(xs->bucket.bhs[i]);
4743 memset(&xs->bucket, 0, sizeof(xs->bucket));
4745 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4748 if (ret && ret != -ENODATA)
4750 xs->not_found = ret;
4756 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4762 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4763 struct ocfs2_xattr_bucket *bucket,
4767 struct ocfs2_xattr_header *xh = bucket->xh;
4769 struct ocfs2_xattr_entry *xe;
4771 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4772 xe = &xh->xh_entries[i];
4773 if (ocfs2_xattr_is_local(xe))
4776 ret = ocfs2_xattr_bucket_value_truncate(inode,
4788 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4789 struct buffer_head *xb_bh)
4791 struct ocfs2_xattr_block *xb =
4792 (struct ocfs2_xattr_block *)xb_bh->b_data;
4793 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4795 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4798 if (le16_to_cpu(el->l_next_free_rec) == 0)
4801 while (name_hash > 0) {
4802 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4803 &e_cpos, &num_clusters, el);
4809 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4810 ocfs2_delete_xattr_in_bucket,
4817 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4818 p_blkno, e_cpos, num_clusters);
4827 name_hash = e_cpos - 1;