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>
40 #include <linux/init.h>
41 #include <linux/module.h>
42 #include <linux/string.h>
44 #define MLOG_MASK_PREFIX ML_XATTR
45 #include <cluster/masklog.h>
58 #include "buffer_head_io.h"
63 struct ocfs2_xattr_def_value_root {
64 struct ocfs2_xattr_value_root xv;
65 struct ocfs2_extent_rec er;
68 struct ocfs2_xattr_bucket {
69 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
70 struct ocfs2_xattr_header *xh;
73 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
74 #define OCFS2_XATTR_INLINE_SIZE 80
76 static struct ocfs2_xattr_def_value_root def_xv = {
77 .xv.xr_list.l_count = cpu_to_le16(1),
80 struct xattr_handler *ocfs2_xattr_handlers[] = {
81 &ocfs2_xattr_user_handler,
82 &ocfs2_xattr_trusted_handler,
86 static struct xattr_handler *ocfs2_xattr_handler_map[] = {
87 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
88 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
91 struct ocfs2_xattr_info {
98 struct ocfs2_xattr_search {
99 struct buffer_head *inode_bh;
101 * xattr_bh point to the block buffer head which has extended attribute
102 * when extended attribute in inode, xattr_bh is equal to inode_bh.
104 struct buffer_head *xattr_bh;
105 struct ocfs2_xattr_header *header;
106 struct ocfs2_xattr_bucket bucket;
109 struct ocfs2_xattr_entry *here;
113 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
114 struct ocfs2_xattr_header *xh,
119 static int ocfs2_xattr_index_block_find(struct inode *inode,
120 struct buffer_head *root_bh,
123 struct ocfs2_xattr_search *xs);
125 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
126 struct ocfs2_xattr_tree_root *xt,
130 static int ocfs2_xattr_create_index_block(struct inode *inode,
131 struct ocfs2_xattr_search *xs);
133 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
134 struct ocfs2_xattr_info *xi,
135 struct ocfs2_xattr_search *xs);
137 static int ocfs2_delete_xattr_index_block(struct inode *inode,
138 struct buffer_head *xb_bh);
140 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
142 struct xattr_handler *handler = NULL;
144 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
145 handler = ocfs2_xattr_handler_map[name_index];
150 static u32 ocfs2_xattr_name_hash(struct inode *inode,
154 /* Get hash value of uuid from super block */
155 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
158 /* hash extended attribute name */
159 for (i = 0; i < name_len; i++) {
160 hash = (hash << OCFS2_HASH_SHIFT) ^
161 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
169 * ocfs2_xattr_hash_entry()
171 * Compute the hash of an extended attribute.
173 static void ocfs2_xattr_hash_entry(struct inode *inode,
174 struct ocfs2_xattr_header *header,
175 struct ocfs2_xattr_entry *entry)
178 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
180 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
181 entry->xe_name_hash = cpu_to_le32(hash);
186 static int ocfs2_xattr_extend_allocation(struct inode *inode,
188 struct buffer_head *xattr_bh,
189 struct ocfs2_xattr_value_root *xv)
192 int restart_func = 0;
194 handle_t *handle = NULL;
195 struct ocfs2_alloc_context *data_ac = NULL;
196 struct ocfs2_alloc_context *meta_ac = NULL;
197 enum ocfs2_alloc_restarted why;
198 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
199 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
200 struct ocfs2_extent_tree et;
202 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
204 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
208 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
215 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
217 handle = ocfs2_start_trans(osb, credits);
218 if (IS_ERR(handle)) {
219 status = PTR_ERR(handle);
225 restarted_transaction:
226 status = ocfs2_journal_access(handle, inode, xattr_bh,
227 OCFS2_JOURNAL_ACCESS_WRITE);
233 prev_clusters = le32_to_cpu(xv->xr_clusters);
234 status = ocfs2_add_clusters_in_btree(osb,
244 if ((status < 0) && (status != -EAGAIN)) {
245 if (status != -ENOSPC)
250 status = ocfs2_journal_dirty(handle, xattr_bh);
256 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
258 if (why != RESTART_NONE && clusters_to_add) {
259 if (why == RESTART_META) {
260 mlog(0, "restarting function.\n");
263 BUG_ON(why != RESTART_TRANS);
265 mlog(0, "restarting transaction.\n");
266 /* TODO: This can be more intelligent. */
267 credits = ocfs2_calc_extend_credits(osb->sb,
270 status = ocfs2_extend_trans(handle, credits);
272 /* handle still has to be committed at
278 goto restarted_transaction;
284 ocfs2_commit_trans(osb, handle);
288 ocfs2_free_alloc_context(data_ac);
292 ocfs2_free_alloc_context(meta_ac);
295 if ((!status) && restart_func) {
303 static int __ocfs2_remove_xattr_range(struct inode *inode,
304 struct buffer_head *root_bh,
305 struct ocfs2_xattr_value_root *xv,
306 u32 cpos, u32 phys_cpos, u32 len,
307 struct ocfs2_cached_dealloc_ctxt *dealloc)
310 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
311 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
312 struct inode *tl_inode = osb->osb_tl_inode;
314 struct ocfs2_alloc_context *meta_ac = NULL;
315 struct ocfs2_extent_tree et;
317 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
319 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
325 mutex_lock(&tl_inode->i_mutex);
327 if (ocfs2_truncate_log_needs_flush(osb)) {
328 ret = __ocfs2_flush_truncate_log(osb);
335 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
336 if (IS_ERR(handle)) {
337 ret = PTR_ERR(handle);
342 ret = ocfs2_journal_access(handle, inode, root_bh,
343 OCFS2_JOURNAL_ACCESS_WRITE);
349 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
356 le32_add_cpu(&xv->xr_clusters, -len);
358 ret = ocfs2_journal_dirty(handle, root_bh);
364 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
369 ocfs2_commit_trans(osb, handle);
371 mutex_unlock(&tl_inode->i_mutex);
374 ocfs2_free_alloc_context(meta_ac);
379 static int ocfs2_xattr_shrink_size(struct inode *inode,
382 struct buffer_head *root_bh,
383 struct ocfs2_xattr_value_root *xv)
386 u32 trunc_len, cpos, phys_cpos, alloc_size;
388 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
389 struct ocfs2_cached_dealloc_ctxt dealloc;
391 ocfs2_init_dealloc_ctxt(&dealloc);
393 if (old_clusters <= new_clusters)
397 trunc_len = old_clusters - new_clusters;
399 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
400 &alloc_size, &xv->xr_list);
406 if (alloc_size > trunc_len)
407 alloc_size = trunc_len;
409 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
410 phys_cpos, alloc_size,
417 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
418 ocfs2_remove_xattr_clusters_from_cache(inode, block,
421 trunc_len -= alloc_size;
425 ocfs2_schedule_truncate_log_flush(osb, 1);
426 ocfs2_run_deallocs(osb, &dealloc);
431 static int ocfs2_xattr_value_truncate(struct inode *inode,
432 struct buffer_head *root_bh,
433 struct ocfs2_xattr_value_root *xv,
437 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
438 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
440 if (new_clusters == old_clusters)
443 if (new_clusters > old_clusters)
444 ret = ocfs2_xattr_extend_allocation(inode,
445 new_clusters - old_clusters,
448 ret = ocfs2_xattr_shrink_size(inode,
449 old_clusters, new_clusters,
455 static int ocfs2_xattr_list_entries(struct inode *inode,
456 struct ocfs2_xattr_header *header,
457 char *buffer, size_t buffer_size)
459 size_t rest = buffer_size;
462 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
463 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
464 struct xattr_handler *handler =
465 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
468 size_t size = handler->list(inode, buffer, rest,
470 le16_to_cpu(entry->xe_name_offset)),
481 return buffer_size - rest;
484 static int ocfs2_xattr_ibody_list(struct inode *inode,
485 struct ocfs2_dinode *di,
489 struct ocfs2_xattr_header *header = NULL;
490 struct ocfs2_inode_info *oi = OCFS2_I(inode);
493 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
496 header = (struct ocfs2_xattr_header *)
497 ((void *)di + inode->i_sb->s_blocksize -
498 le16_to_cpu(di->i_xattr_inline_size));
500 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
505 static int ocfs2_xattr_block_list(struct inode *inode,
506 struct ocfs2_dinode *di,
510 struct buffer_head *blk_bh = NULL;
511 struct ocfs2_xattr_block *xb;
514 if (!di->i_xattr_loc)
517 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
518 le64_to_cpu(di->i_xattr_loc),
519 &blk_bh, OCFS2_BH_CACHED, inode);
524 /*Verify the signature of xattr block*/
525 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
526 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
531 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
533 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
534 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
535 ret = ocfs2_xattr_list_entries(inode, header,
536 buffer, buffer_size);
538 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
539 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
540 buffer, buffer_size);
548 ssize_t ocfs2_listxattr(struct dentry *dentry,
552 int ret = 0, i_ret = 0, b_ret = 0;
553 struct buffer_head *di_bh = NULL;
554 struct ocfs2_dinode *di = NULL;
555 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
557 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
560 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
563 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
569 di = (struct ocfs2_dinode *)di_bh->b_data;
571 down_read(&oi->ip_xattr_sem);
572 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
580 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
585 up_read(&oi->ip_xattr_sem);
586 ocfs2_inode_unlock(dentry->d_inode, 0);
590 return i_ret + b_ret;
593 static int ocfs2_xattr_find_entry(int name_index,
595 struct ocfs2_xattr_search *xs)
597 struct ocfs2_xattr_entry *entry;
604 name_len = strlen(name);
606 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
607 cmp = name_index - ocfs2_xattr_get_type(entry);
609 cmp = name_len - entry->xe_name_len;
611 cmp = memcmp(name, (xs->base +
612 le16_to_cpu(entry->xe_name_offset)),
620 return cmp ? -ENODATA : 0;
623 static int ocfs2_xattr_get_value_outside(struct inode *inode,
624 struct ocfs2_xattr_value_root *xv,
628 u32 cpos, p_cluster, num_clusters, bpc, clusters;
631 size_t cplen, blocksize;
632 struct buffer_head *bh = NULL;
633 struct ocfs2_extent_list *el;
636 clusters = le32_to_cpu(xv->xr_clusters);
637 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
638 blocksize = inode->i_sb->s_blocksize;
641 while (cpos < clusters) {
642 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
649 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
650 /* Copy ocfs2_xattr_value */
651 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
652 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
653 &bh, OCFS2_BH_CACHED, inode);
659 cplen = len >= blocksize ? blocksize : len;
660 memcpy(buffer, bh->b_data, cplen);
669 cpos += num_clusters;
675 static int ocfs2_xattr_ibody_get(struct inode *inode,
680 struct ocfs2_xattr_search *xs)
682 struct ocfs2_inode_info *oi = OCFS2_I(inode);
683 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
684 struct ocfs2_xattr_value_root *xv;
688 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
691 xs->end = (void *)di + inode->i_sb->s_blocksize;
692 xs->header = (struct ocfs2_xattr_header *)
693 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
694 xs->base = (void *)xs->header;
695 xs->here = xs->header->xh_entries;
697 ret = ocfs2_xattr_find_entry(name_index, name, xs);
700 size = le64_to_cpu(xs->here->xe_value_size);
702 if (size > buffer_size)
704 if (ocfs2_xattr_is_local(xs->here)) {
705 memcpy(buffer, (void *)xs->base +
706 le16_to_cpu(xs->here->xe_name_offset) +
707 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
709 xv = (struct ocfs2_xattr_value_root *)
710 (xs->base + le16_to_cpu(
711 xs->here->xe_name_offset) +
712 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
713 ret = ocfs2_xattr_get_value_outside(inode, xv,
725 static int ocfs2_xattr_block_get(struct inode *inode,
730 struct ocfs2_xattr_search *xs)
732 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
733 struct buffer_head *blk_bh = NULL;
734 struct ocfs2_xattr_block *xb;
735 struct ocfs2_xattr_value_root *xv;
737 int ret = -ENODATA, name_offset, name_len, block_off, i;
739 if (!di->i_xattr_loc)
742 memset(&xs->bucket, 0, sizeof(xs->bucket));
744 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
745 le64_to_cpu(di->i_xattr_loc),
746 &blk_bh, OCFS2_BH_CACHED, inode);
751 /*Verify the signature of xattr block*/
752 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
753 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
758 xs->xattr_bh = blk_bh;
759 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
761 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
762 xs->header = &xb->xb_attrs.xb_header;
763 xs->base = (void *)xs->header;
764 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
765 xs->here = xs->header->xh_entries;
767 ret = ocfs2_xattr_find_entry(name_index, name, xs);
769 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
775 size = le64_to_cpu(xs->here->xe_value_size);
778 if (size > buffer_size)
781 name_offset = le16_to_cpu(xs->here->xe_name_offset);
782 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
783 i = xs->here - xs->header->xh_entries;
785 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
786 ret = ocfs2_xattr_bucket_get_name_value(inode,
791 xs->base = xs->bucket.bhs[block_off]->b_data;
793 if (ocfs2_xattr_is_local(xs->here)) {
794 memcpy(buffer, (void *)xs->base +
795 name_offset + name_len, size);
797 xv = (struct ocfs2_xattr_value_root *)
798 (xs->base + name_offset + name_len);
799 ret = ocfs2_xattr_get_value_outside(inode, xv,
809 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
810 brelse(xs->bucket.bhs[i]);
811 memset(&xs->bucket, 0, sizeof(xs->bucket));
819 * Copy an extended attribute into the buffer provided.
820 * Buffer is NULL to compute the size of buffer required.
822 int ocfs2_xattr_get(struct inode *inode,
829 struct ocfs2_dinode *di = NULL;
830 struct buffer_head *di_bh = NULL;
831 struct ocfs2_inode_info *oi = OCFS2_I(inode);
832 struct ocfs2_xattr_search xis = {
833 .not_found = -ENODATA,
835 struct ocfs2_xattr_search xbs = {
836 .not_found = -ENODATA,
839 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
842 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
845 ret = ocfs2_inode_lock(inode, &di_bh, 0);
850 xis.inode_bh = xbs.inode_bh = di_bh;
851 di = (struct ocfs2_dinode *)di_bh->b_data;
853 down_read(&oi->ip_xattr_sem);
854 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
857 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
859 up_read(&oi->ip_xattr_sem);
860 ocfs2_inode_unlock(inode, 0);
867 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
868 struct ocfs2_xattr_value_root *xv,
872 int ret = 0, i, cp_len, credits;
873 u16 blocksize = inode->i_sb->s_blocksize;
874 u32 p_cluster, num_clusters;
875 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
876 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
878 struct buffer_head *bh = NULL;
881 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
883 credits = clusters * bpc;
884 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
885 if (IS_ERR(handle)) {
886 ret = PTR_ERR(handle);
891 while (cpos < clusters) {
892 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
893 &num_clusters, &xv->xr_list);
899 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
901 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
902 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
903 &bh, OCFS2_BH_CACHED, inode);
909 ret = ocfs2_journal_access(handle,
912 OCFS2_JOURNAL_ACCESS_WRITE);
918 cp_len = value_len > blocksize ? blocksize : value_len;
919 memcpy(bh->b_data, value, cp_len);
922 if (cp_len < blocksize)
923 memset(bh->b_data + cp_len, 0,
926 ret = ocfs2_journal_dirty(handle, bh);
935 * XXX: do we need to empty all the following
936 * blocks in this cluster?
941 cpos += num_clusters;
944 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
951 static int ocfs2_xattr_cleanup(struct inode *inode,
952 struct ocfs2_xattr_info *xi,
953 struct ocfs2_xattr_search *xs,
956 handle_t *handle = NULL;
958 size_t name_len = strlen(xi->name);
959 void *val = xs->base + offs;
960 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
962 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
963 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
964 if (IS_ERR(handle)) {
965 ret = PTR_ERR(handle);
969 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
970 OCFS2_JOURNAL_ACCESS_WRITE);
975 /* Decrease xattr count */
976 le16_add_cpu(&xs->header->xh_count, -1);
977 /* Remove the xattr entry and tree root which has already be set*/
978 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
979 memset(val, 0, size);
981 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
985 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
990 static int ocfs2_xattr_update_entry(struct inode *inode,
991 struct ocfs2_xattr_info *xi,
992 struct ocfs2_xattr_search *xs,
995 handle_t *handle = NULL;
998 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
999 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1000 if (IS_ERR(handle)) {
1001 ret = PTR_ERR(handle);
1005 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1006 OCFS2_JOURNAL_ACCESS_WRITE);
1012 xs->here->xe_name_offset = cpu_to_le16(offs);
1013 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1014 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1015 ocfs2_xattr_set_local(xs->here, 1);
1017 ocfs2_xattr_set_local(xs->here, 0);
1018 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1020 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1024 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1030 * ocfs2_xattr_set_value_outside()
1032 * Set large size value in B tree.
1034 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1035 struct ocfs2_xattr_info *xi,
1036 struct ocfs2_xattr_search *xs,
1039 size_t name_len = strlen(xi->name);
1040 void *val = xs->base + offs;
1041 struct ocfs2_xattr_value_root *xv = NULL;
1042 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1045 memset(val, 0, size);
1046 memcpy(val, xi->name, name_len);
1047 xv = (struct ocfs2_xattr_value_root *)
1048 (val + OCFS2_XATTR_SIZE(name_len));
1049 xv->xr_clusters = 0;
1050 xv->xr_last_eb_blk = 0;
1051 xv->xr_list.l_tree_depth = 0;
1052 xv->xr_list.l_count = cpu_to_le16(1);
1053 xv->xr_list.l_next_free_rec = 0;
1055 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1061 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1067 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1075 * ocfs2_xattr_set_entry_local()
1077 * Set, replace or remove extended attribute in local.
1079 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1080 struct ocfs2_xattr_info *xi,
1081 struct ocfs2_xattr_search *xs,
1082 struct ocfs2_xattr_entry *last,
1085 size_t name_len = strlen(xi->name);
1088 if (xi->value && xs->not_found) {
1089 /* Insert the new xattr entry. */
1090 le16_add_cpu(&xs->header->xh_count, 1);
1091 ocfs2_xattr_set_type(last, xi->name_index);
1092 ocfs2_xattr_set_local(last, 1);
1093 last->xe_name_len = name_len;
1099 first_val = xs->base + min_offs;
1100 offs = le16_to_cpu(xs->here->xe_name_offset);
1101 val = xs->base + offs;
1103 if (le64_to_cpu(xs->here->xe_value_size) >
1104 OCFS2_XATTR_INLINE_SIZE)
1105 size = OCFS2_XATTR_SIZE(name_len) +
1106 OCFS2_XATTR_ROOT_SIZE;
1108 size = OCFS2_XATTR_SIZE(name_len) +
1109 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1111 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1112 OCFS2_XATTR_SIZE(xi->value_len)) {
1113 /* The old and the new value have the
1114 same size. Just replace the value. */
1115 ocfs2_xattr_set_local(xs->here, 1);
1116 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1117 /* Clear value bytes. */
1118 memset(val + OCFS2_XATTR_SIZE(name_len),
1120 OCFS2_XATTR_SIZE(xi->value_len));
1121 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1126 /* Remove the old name+value. */
1127 memmove(first_val + size, first_val, val - first_val);
1128 memset(first_val, 0, size);
1129 xs->here->xe_name_hash = 0;
1130 xs->here->xe_name_offset = 0;
1131 ocfs2_xattr_set_local(xs->here, 1);
1132 xs->here->xe_value_size = 0;
1136 /* Adjust all value offsets. */
1137 last = xs->header->xh_entries;
1138 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1139 size_t o = le16_to_cpu(last->xe_name_offset);
1142 last->xe_name_offset = cpu_to_le16(o + size);
1147 /* Remove the old entry. */
1149 memmove(xs->here, xs->here + 1,
1150 (void *)last - (void *)xs->here);
1151 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1152 le16_add_cpu(&xs->header->xh_count, -1);
1156 /* Insert the new name+value. */
1157 size_t size = OCFS2_XATTR_SIZE(name_len) +
1158 OCFS2_XATTR_SIZE(xi->value_len);
1159 void *val = xs->base + min_offs - size;
1161 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1162 memset(val, 0, size);
1163 memcpy(val, xi->name, name_len);
1164 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1167 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1168 ocfs2_xattr_set_local(xs->here, 1);
1169 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1176 * ocfs2_xattr_set_entry()
1178 * Set extended attribute entry into inode or block.
1180 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1181 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1182 * then set value in B tree with set_value_outside().
1184 static int ocfs2_xattr_set_entry(struct inode *inode,
1185 struct ocfs2_xattr_info *xi,
1186 struct ocfs2_xattr_search *xs,
1189 struct ocfs2_xattr_entry *last;
1190 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1191 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1192 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1194 handle_t *handle = NULL;
1196 struct ocfs2_xattr_info xi_l = {
1197 .name_index = xi->name_index,
1200 .value_len = xi->value_len,
1203 /* Compute min_offs, last and free space. */
1204 last = xs->header->xh_entries;
1206 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1207 size_t offs = le16_to_cpu(last->xe_name_offset);
1208 if (offs < min_offs)
1213 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1217 if (!xs->not_found) {
1219 if (ocfs2_xattr_is_local(xs->here))
1220 size = OCFS2_XATTR_SIZE(name_len) +
1221 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1223 size = OCFS2_XATTR_SIZE(name_len) +
1224 OCFS2_XATTR_ROOT_SIZE;
1225 free += (size + sizeof(struct ocfs2_xattr_entry));
1227 /* Check free space in inode or block */
1228 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1229 if (free < sizeof(struct ocfs2_xattr_entry) +
1230 OCFS2_XATTR_SIZE(name_len) +
1231 OCFS2_XATTR_ROOT_SIZE) {
1235 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1236 xi_l.value = (void *)&def_xv;
1237 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1238 } else if (xi->value) {
1239 if (free < sizeof(struct ocfs2_xattr_entry) +
1240 OCFS2_XATTR_SIZE(name_len) +
1241 OCFS2_XATTR_SIZE(xi->value_len)) {
1247 if (!xs->not_found) {
1248 /* For existing extended attribute */
1249 size_t size = OCFS2_XATTR_SIZE(name_len) +
1250 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1251 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1252 void *val = xs->base + offs;
1254 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1255 /* Replace existing local xattr with tree root */
1256 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1261 } else if (!ocfs2_xattr_is_local(xs->here)) {
1262 /* For existing xattr which has value outside */
1263 struct ocfs2_xattr_value_root *xv = NULL;
1264 xv = (struct ocfs2_xattr_value_root *)(val +
1265 OCFS2_XATTR_SIZE(name_len));
1267 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1269 * If new value need set outside also,
1270 * first truncate old value to new value,
1271 * then set new value with set_value_outside().
1273 ret = ocfs2_xattr_value_truncate(inode,
1282 ret = __ocfs2_xattr_set_value_outside(inode,
1291 ret = ocfs2_xattr_update_entry(inode,
1300 * If new value need set in local,
1301 * just trucate old value to zero.
1303 ret = ocfs2_xattr_value_truncate(inode,
1313 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1314 OCFS2_INODE_UPDATE_CREDITS);
1315 if (IS_ERR(handle)) {
1316 ret = PTR_ERR(handle);
1321 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1322 OCFS2_JOURNAL_ACCESS_WRITE);
1328 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1329 /* set extended attribute in external block. */
1330 ret = ocfs2_extend_trans(handle,
1331 OCFS2_INODE_UPDATE_CREDITS +
1332 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1337 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1338 OCFS2_JOURNAL_ACCESS_WRITE);
1346 * Set value in local, include set tree root in local.
1347 * This is the first step for value size >INLINE_SIZE.
1349 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1351 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1352 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1359 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1360 (flag & OCFS2_INLINE_XATTR_FL)) {
1361 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1362 unsigned int xattrsize = osb->s_xattr_inline_size;
1365 * Adjust extent record count or inline data size
1366 * to reserve space for extended attribute.
1368 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1369 struct ocfs2_inline_data *idata = &di->id2.i_data;
1370 le16_add_cpu(&idata->id_count, -xattrsize);
1371 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1372 struct ocfs2_extent_list *el = &di->id2.i_list;
1373 le16_add_cpu(&el->l_count, -(xattrsize /
1374 sizeof(struct ocfs2_extent_rec)));
1376 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1378 /* Update xattr flag */
1379 spin_lock(&oi->ip_lock);
1380 oi->ip_dyn_features |= flag;
1381 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1382 spin_unlock(&oi->ip_lock);
1383 /* Update inode ctime */
1384 inode->i_ctime = CURRENT_TIME;
1385 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1386 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1388 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1393 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1395 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1397 * Set value outside in B tree.
1398 * This is the second step for value size > INLINE_SIZE.
1400 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1401 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1407 * If set value outside failed, we have to clean
1408 * the junk tree root we have already set in local.
1410 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1420 static int ocfs2_remove_value_outside(struct inode*inode,
1421 struct buffer_head *bh,
1422 struct ocfs2_xattr_header *header)
1426 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1427 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1429 if (!ocfs2_xattr_is_local(entry)) {
1430 struct ocfs2_xattr_value_root *xv;
1433 val = (void *)header +
1434 le16_to_cpu(entry->xe_name_offset);
1435 xv = (struct ocfs2_xattr_value_root *)
1436 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1437 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1448 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1449 struct buffer_head *di_bh)
1452 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1453 struct ocfs2_xattr_header *header;
1456 header = (struct ocfs2_xattr_header *)
1457 ((void *)di + inode->i_sb->s_blocksize -
1458 le16_to_cpu(di->i_xattr_inline_size));
1460 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1465 static int ocfs2_xattr_block_remove(struct inode *inode,
1466 struct buffer_head *blk_bh)
1468 struct ocfs2_xattr_block *xb;
1471 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1472 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1473 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1474 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1476 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1481 static int ocfs2_xattr_free_block(struct inode *inode,
1484 struct inode *xb_alloc_inode;
1485 struct buffer_head *xb_alloc_bh = NULL;
1486 struct buffer_head *blk_bh = NULL;
1487 struct ocfs2_xattr_block *xb;
1488 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1494 ret = ocfs2_read_block(osb, block, &blk_bh,
1495 OCFS2_BH_CACHED, inode);
1501 /*Verify the signature of xattr block*/
1502 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1503 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1508 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1514 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1515 blk = le64_to_cpu(xb->xb_blkno);
1516 bit = le16_to_cpu(xb->xb_suballoc_bit);
1517 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1519 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1520 EXTENT_ALLOC_SYSTEM_INODE,
1521 le16_to_cpu(xb->xb_suballoc_slot));
1522 if (!xb_alloc_inode) {
1527 mutex_lock(&xb_alloc_inode->i_mutex);
1529 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1535 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1536 if (IS_ERR(handle)) {
1537 ret = PTR_ERR(handle);
1542 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1547 ocfs2_commit_trans(osb, handle);
1549 ocfs2_inode_unlock(xb_alloc_inode, 1);
1550 brelse(xb_alloc_bh);
1552 mutex_unlock(&xb_alloc_inode->i_mutex);
1553 iput(xb_alloc_inode);
1560 * ocfs2_xattr_remove()
1562 * Free extended attribute resources associated with this inode.
1564 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1566 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1567 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1571 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1574 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1577 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1578 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1585 if (di->i_xattr_loc) {
1586 ret = ocfs2_xattr_free_block(inode,
1587 le64_to_cpu(di->i_xattr_loc));
1594 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1595 OCFS2_INODE_UPDATE_CREDITS);
1596 if (IS_ERR(handle)) {
1597 ret = PTR_ERR(handle);
1601 ret = ocfs2_journal_access(handle, inode, di_bh,
1602 OCFS2_JOURNAL_ACCESS_WRITE);
1608 di->i_xattr_loc = 0;
1610 spin_lock(&oi->ip_lock);
1611 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1612 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1613 spin_unlock(&oi->ip_lock);
1615 ret = ocfs2_journal_dirty(handle, di_bh);
1619 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]);
2084 * Find the xattr extent rec which may contains name_hash.
2085 * e_cpos will be the first name hash of the xattr rec.
2086 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2088 static int ocfs2_xattr_get_rec(struct inode *inode,
2093 struct ocfs2_extent_list *el)
2096 struct buffer_head *eb_bh = NULL;
2097 struct ocfs2_extent_block *eb;
2098 struct ocfs2_extent_rec *rec = NULL;
2101 if (el->l_tree_depth) {
2102 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2108 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2111 if (el->l_tree_depth) {
2112 ocfs2_error(inode->i_sb,
2113 "Inode %lu has non zero tree depth in "
2114 "xattr tree block %llu\n", inode->i_ino,
2115 (unsigned long long)eb_bh->b_blocknr);
2121 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2122 rec = &el->l_recs[i];
2124 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2125 e_blkno = le64_to_cpu(rec->e_blkno);
2131 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2132 "record (%u, %u, 0) in xattr", inode->i_ino,
2133 le32_to_cpu(rec->e_cpos),
2134 ocfs2_rec_clusters(el, rec));
2139 *p_blkno = le64_to_cpu(rec->e_blkno);
2140 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2142 *e_cpos = le32_to_cpu(rec->e_cpos);
2148 typedef int (xattr_bucket_func)(struct inode *inode,
2149 struct ocfs2_xattr_bucket *bucket,
2152 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2153 struct buffer_head *header_bh,
2160 int i, ret = 0, cmp = 1, block_off, new_offset;
2161 struct ocfs2_xattr_header *xh =
2162 (struct ocfs2_xattr_header *)header_bh->b_data;
2163 size_t name_len = strlen(name);
2164 struct ocfs2_xattr_entry *xe = NULL;
2165 struct buffer_head *name_bh = NULL;
2169 * We don't use binary search in the bucket because there
2170 * may be multiple entries with the same name hash.
2172 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2173 xe = &xh->xh_entries[i];
2175 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2177 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2180 cmp = name_index - ocfs2_xattr_get_type(xe);
2182 cmp = name_len - xe->xe_name_len;
2186 ret = ocfs2_xattr_bucket_get_name_value(inode,
2196 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2197 header_bh->b_blocknr + block_off,
2198 &name_bh, OCFS2_BH_CACHED, inode);
2203 xe_name = name_bh->b_data + new_offset;
2205 cmp = memcmp(name, xe_name, name_len);
2221 * Find the specified xattr entry in a series of buckets.
2222 * This series start from p_blkno and last for num_clusters.
2223 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2224 * the num of the valid buckets.
2226 * Return the buffer_head this xattr should reside in. And if the xattr's
2227 * hash is in the gap of 2 buckets, return the lower bucket.
2229 static int ocfs2_xattr_bucket_find(struct inode *inode,
2236 struct ocfs2_xattr_search *xs)
2239 struct buffer_head *bh = NULL;
2240 struct buffer_head *lower_bh = NULL;
2241 struct ocfs2_xattr_header *xh = NULL;
2242 struct ocfs2_xattr_entry *xe = NULL;
2244 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2245 int low_bucket = 0, bucket, high_bucket;
2249 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2250 &bh, OCFS2_BH_CACHED, inode);
2256 xh = (struct ocfs2_xattr_header *)bh->b_data;
2257 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2259 while (low_bucket <= high_bucket) {
2262 bucket = (low_bucket + high_bucket) / 2;
2264 blkno = p_blkno + bucket * blk_per_bucket;
2266 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
2267 &bh, OCFS2_BH_CACHED, inode);
2273 xh = (struct ocfs2_xattr_header *)bh->b_data;
2274 xe = &xh->xh_entries[0];
2275 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2276 high_bucket = bucket - 1;
2281 * Check whether the hash of the last entry in our
2282 * bucket is larger than the search one. for an empty
2283 * bucket, the last one is also the first one.
2286 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2288 last_hash = le32_to_cpu(xe->xe_name_hash);
2290 /* record lower_bh which may be the insert place. */
2295 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2296 low_bucket = bucket + 1;
2300 /* the searched xattr should reside in this bucket if exists. */
2301 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2302 name_index, name, name_hash,
2312 * Record the bucket we have found.
2313 * When the xattr's hash value is in the gap of 2 buckets, we will
2314 * always set it to the previous bucket.
2318 * We can't find any bucket whose first name_hash is less
2319 * than the find name_hash.
2321 BUG_ON(bh->b_blocknr != p_blkno);
2325 xs->bucket.bhs[0] = lower_bh;
2326 xs->bucket.xh = (struct ocfs2_xattr_header *)
2327 xs->bucket.bhs[0]->b_data;
2330 xs->header = xs->bucket.xh;
2331 xs->base = xs->bucket.bhs[0]->b_data;
2332 xs->end = xs->base + inode->i_sb->s_blocksize;
2336 * If we have found the xattr enty, read all the blocks in
2339 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2340 xs->bucket.bhs[0]->b_blocknr + 1,
2341 blk_per_bucket - 1, &xs->bucket.bhs[1],
2342 OCFS2_BH_CACHED, inode);
2348 xs->here = &xs->header->xh_entries[index];
2349 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2350 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2360 static int ocfs2_xattr_index_block_find(struct inode *inode,
2361 struct buffer_head *root_bh,
2364 struct ocfs2_xattr_search *xs)
2367 struct ocfs2_xattr_block *xb =
2368 (struct ocfs2_xattr_block *)root_bh->b_data;
2369 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2370 struct ocfs2_extent_list *el = &xb_root->xt_list;
2372 u32 first_hash, num_clusters = 0;
2373 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2375 if (le16_to_cpu(el->l_next_free_rec) == 0)
2378 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2379 name, name_hash, name_index);
2381 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2388 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2390 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2391 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2393 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2394 p_blkno, first_hash, num_clusters, xs);
2400 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2403 xattr_bucket_func *func,
2407 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2408 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2409 u32 num_buckets = clusters * bpc;
2410 struct ocfs2_xattr_bucket bucket;
2412 memset(&bucket, 0, sizeof(bucket));
2414 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2417 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2418 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2419 blkno, blk_per_bucket,
2420 bucket.bhs, OCFS2_BH_CACHED, inode);
2426 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2428 * The real bucket num in this series of blocks is stored
2429 * in the 1st bucket.
2432 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2434 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2435 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
2437 ret = func(inode, &bucket, para);
2444 for (j = 0; j < blk_per_bucket; j++)
2445 brelse(bucket.bhs[j]);
2446 memset(&bucket, 0, sizeof(bucket));
2450 for (j = 0; j < blk_per_bucket; j++)
2451 brelse(bucket.bhs[j]);
2456 struct ocfs2_xattr_tree_list {
2461 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2462 struct ocfs2_xattr_header *xh,
2469 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2472 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2474 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2475 *new_offset = name_offset % inode->i_sb->s_blocksize;
2480 static int ocfs2_list_xattr_bucket(struct inode *inode,
2481 struct ocfs2_xattr_bucket *bucket,
2485 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2487 int i, block_off, new_offset;
2489 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2490 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2491 struct xattr_handler *handler =
2492 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2495 ret = ocfs2_xattr_bucket_get_name_value(inode,
2502 size = handler->list(inode, xl->buffer, xl->buffer_size,
2503 bucket->bhs[block_off]->b_data +
2505 entry->xe_name_len);
2507 if (size > xl->buffer_size)
2511 xl->buffer_size -= size;
2518 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2519 struct ocfs2_xattr_tree_root *xt,
2523 struct ocfs2_extent_list *el = &xt->xt_list;
2525 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2527 struct ocfs2_xattr_tree_list xl = {
2529 .buffer_size = buffer_size,
2532 if (le16_to_cpu(el->l_next_free_rec) == 0)
2535 while (name_hash > 0) {
2536 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2537 &e_cpos, &num_clusters, el);
2543 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2544 ocfs2_list_xattr_bucket,
2554 name_hash = e_cpos - 1;
2557 ret = buffer_size - xl.buffer_size;
2562 static int cmp_xe(const void *a, const void *b)
2564 const struct ocfs2_xattr_entry *l = a, *r = b;
2565 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2566 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2568 if (l_hash > r_hash)
2570 if (l_hash < r_hash)
2575 static void swap_xe(void *a, void *b, int size)
2577 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2580 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2581 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2585 * When the ocfs2_xattr_block is filled up, new bucket will be created
2586 * and all the xattr entries will be moved to the new bucket.
2587 * Note: we need to sort the entries since they are not saved in order
2588 * in the ocfs2_xattr_block.
2590 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2591 struct buffer_head *xb_bh,
2592 struct buffer_head *xh_bh,
2593 struct buffer_head *data_bh)
2595 int i, blocksize = inode->i_sb->s_blocksize;
2596 u16 offset, size, off_change;
2597 struct ocfs2_xattr_entry *xe;
2598 struct ocfs2_xattr_block *xb =
2599 (struct ocfs2_xattr_block *)xb_bh->b_data;
2600 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2601 struct ocfs2_xattr_header *xh =
2602 (struct ocfs2_xattr_header *)xh_bh->b_data;
2603 u16 count = le16_to_cpu(xb_xh->xh_count);
2604 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2606 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2607 (unsigned long long)xb_bh->b_blocknr,
2608 (unsigned long long)xh_bh->b_blocknr);
2610 memset(xh_bh->b_data, 0, blocksize);
2612 memset(data_bh->b_data, 0, blocksize);
2614 * Since the xe_name_offset is based on ocfs2_xattr_header,
2615 * there is a offset change corresponding to the change of
2616 * ocfs2_xattr_header's position.
2618 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2619 xe = &xb_xh->xh_entries[count - 1];
2620 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2621 size = blocksize - offset;
2623 /* copy all the names and values. */
2625 target = data_bh->b_data;
2626 memcpy(target + offset, src + offset, size);
2628 /* Init new header now. */
2629 xh->xh_count = xb_xh->xh_count;
2630 xh->xh_num_buckets = cpu_to_le16(1);
2631 xh->xh_name_value_len = cpu_to_le16(size);
2632 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2634 /* copy all the entries. */
2635 target = xh_bh->b_data;
2636 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2637 size = count * sizeof(struct ocfs2_xattr_entry);
2638 memcpy(target + offset, (char *)xb_xh + offset, size);
2640 /* Change the xe offset for all the xe because of the move. */
2641 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2642 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2643 for (i = 0; i < count; i++)
2644 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2646 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2647 offset, size, off_change);
2649 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2654 * After we move xattr from block to index btree, we have to
2655 * update ocfs2_xattr_search to the new xe and base.
2657 * When the entry is in xattr block, xattr_bh indicates the storage place.
2658 * While if the entry is in index b-tree, "bucket" indicates the
2659 * real place of the xattr.
2661 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2662 struct ocfs2_xattr_search *xs,
2663 struct buffer_head *old_bh,
2664 struct buffer_head *new_bh)
2667 char *buf = old_bh->b_data;
2668 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2669 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2670 int i, blocksize = inode->i_sb->s_blocksize;
2671 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2673 xs->bucket.bhs[0] = new_bh;
2675 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2676 xs->header = xs->bucket.xh;
2678 xs->base = new_bh->b_data;
2679 xs->end = xs->base + inode->i_sb->s_blocksize;
2681 if (!xs->not_found) {
2682 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2683 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2684 xs->bucket.bhs[0]->b_blocknr + 1,
2685 blk_per_bucket - 1, &xs->bucket.bhs[1],
2686 OCFS2_BH_CACHED, inode);
2692 i = xs->here - old_xh->xh_entries;
2693 xs->here = &xs->header->xh_entries[i];
2700 static int ocfs2_xattr_create_index_block(struct inode *inode,
2701 struct ocfs2_xattr_search *xs)
2703 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2707 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2708 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2709 struct ocfs2_alloc_context *data_ac;
2710 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2711 struct buffer_head *xb_bh = xs->xattr_bh;
2712 struct ocfs2_xattr_block *xb =
2713 (struct ocfs2_xattr_block *)xb_bh->b_data;
2714 struct ocfs2_xattr_tree_root *xr;
2715 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2716 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2718 mlog(0, "create xattr index block for %llu\n",
2719 (unsigned long long)xb_bh->b_blocknr);
2721 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2723 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2731 * We can use this lock for now, and maybe move to a dedicated mutex
2732 * if performance becomes a problem later.
2734 down_write(&oi->ip_alloc_sem);
2737 * 3 more credits, one for xattr block update, one for the 1st block
2738 * of the new xattr bucket and one for the value/data.
2741 handle = ocfs2_start_trans(osb, credits);
2742 if (IS_ERR(handle)) {
2743 ret = PTR_ERR(handle);
2748 ret = ocfs2_journal_access(handle, inode, xb_bh,
2749 OCFS2_JOURNAL_ACCESS_WRITE);
2755 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2762 * The bucket may spread in many blocks, and
2763 * we will only touch the 1st block and the last block
2764 * in the whole bucket(one for entry and one for data).
2766 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2768 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2770 xh_bh = sb_getblk(inode->i_sb, blkno);
2777 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2779 ret = ocfs2_journal_access(handle, inode, xh_bh,
2780 OCFS2_JOURNAL_ACCESS_CREATE);
2787 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2794 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2796 ret = ocfs2_journal_access(handle, inode, data_bh,
2797 OCFS2_JOURNAL_ACCESS_CREATE);
2804 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2806 ocfs2_journal_dirty(handle, xh_bh);
2808 ocfs2_journal_dirty(handle, data_bh);
2810 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2812 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2813 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2814 offsetof(struct ocfs2_xattr_block, xb_attrs));
2816 xr = &xb->xb_attrs.xb_root;
2817 xr->xt_clusters = cpu_to_le32(1);
2818 xr->xt_last_eb_blk = 0;
2819 xr->xt_list.l_tree_depth = 0;
2820 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2821 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2823 xr->xt_list.l_recs[0].e_cpos = 0;
2824 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2825 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2827 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2829 ret = ocfs2_journal_dirty(handle, xb_bh);
2836 ocfs2_commit_trans(osb, handle);
2839 up_write(&oi->ip_alloc_sem);
2843 ocfs2_free_alloc_context(data_ac);
2851 static int cmp_xe_offset(const void *a, const void *b)
2853 const struct ocfs2_xattr_entry *l = a, *r = b;
2854 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2855 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2857 if (l_name_offset < r_name_offset)
2859 if (l_name_offset > r_name_offset)
2865 * defrag a xattr bucket if we find that the bucket has some
2866 * holes beteen name/value pairs.
2867 * We will move all the name/value pairs to the end of the bucket
2868 * so that we can spare some space for insertion.
2870 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2871 struct ocfs2_xattr_bucket *bucket)
2874 size_t end, offset, len, value_len;
2875 struct ocfs2_xattr_header *xh;
2876 char *entries, *buf, *bucket_buf = NULL;
2877 u64 blkno = bucket->bhs[0]->b_blocknr;
2878 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2880 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2881 size_t blocksize = inode->i_sb->s_blocksize;
2883 struct buffer_head **bhs;
2884 struct ocfs2_xattr_entry *xe;
2886 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2891 ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
2892 OCFS2_BH_CACHED, inode);
2897 * In order to make the operation more efficient and generic,
2898 * we copy all the blocks into a contiguous memory and do the
2899 * defragment there, so if anything is error, we will not touch
2902 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2909 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2910 memcpy(buf, bhs[i]->b_data, blocksize);
2912 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2913 if (IS_ERR(handle)) {
2914 ret = PTR_ERR(handle);
2920 for (i = 0; i < blk_per_bucket; i++) {
2921 ret = ocfs2_journal_access(handle, inode, bhs[i],
2922 OCFS2_JOURNAL_ACCESS_WRITE);
2929 xh = (struct ocfs2_xattr_header *)bucket_buf;
2930 entries = (char *)xh->xh_entries;
2931 xh_free_start = le16_to_cpu(xh->xh_free_start);
2933 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2934 "xh_free_start = %u, xh_name_value_len = %u.\n",
2935 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2936 le16_to_cpu(xh->xh_name_value_len));
2939 * sort all the entries by their offset.
2940 * the largest will be the first, so that we can
2941 * move them to the end one by one.
2943 sort(entries, le16_to_cpu(xh->xh_count),
2944 sizeof(struct ocfs2_xattr_entry),
2945 cmp_xe_offset, swap_xe);
2947 /* Move all name/values to the end of the bucket. */
2948 xe = xh->xh_entries;
2949 end = OCFS2_XATTR_BUCKET_SIZE;
2950 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2951 offset = le16_to_cpu(xe->xe_name_offset);
2952 if (ocfs2_xattr_is_local(xe))
2953 value_len = OCFS2_XATTR_SIZE(
2954 le64_to_cpu(xe->xe_value_size));
2956 value_len = OCFS2_XATTR_ROOT_SIZE;
2957 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2960 * We must make sure that the name/value pair
2961 * exist in the same block. So adjust end to
2962 * the previous block end if needed.
2964 if (((end - len) / blocksize !=
2965 (end - 1) / blocksize))
2966 end = end - end % blocksize;
2968 if (end > offset + len) {
2969 memmove(bucket_buf + end - len,
2970 bucket_buf + offset, len);
2971 xe->xe_name_offset = cpu_to_le16(end - len);
2974 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2975 "bucket %llu\n", (unsigned long long)blkno);
2980 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2981 "bucket %llu\n", (unsigned long long)blkno);
2983 if (xh_free_start == end)
2986 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2987 xh->xh_free_start = cpu_to_le16(end);
2989 /* sort the entries by their name_hash. */
2990 sort(entries, le16_to_cpu(xh->xh_count),
2991 sizeof(struct ocfs2_xattr_entry),
2995 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
2996 memcpy(bhs[i]->b_data, buf, blocksize);
2997 ocfs2_journal_dirty(handle, bhs[i]);
3001 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3005 for (i = 0; i < blk_per_bucket; i++)
3015 * Move half nums of the xattr bucket in the previous cluster to this new
3016 * cluster. We only touch the last cluster of the previous extend record.
3018 * first_bh is the first buffer_head of a series of bucket in the same
3019 * extent rec and header_bh is the header of one bucket in this cluster.
3020 * They will be updated if we move the data header_bh contains to the new
3021 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3023 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3025 struct buffer_head **first_bh,
3026 struct buffer_head **header_bh,
3032 int i, ret, credits;
3033 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3034 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3035 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3036 int blocksize = inode->i_sb->s_blocksize;
3037 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3038 struct ocfs2_xattr_header *new_xh;
3039 struct ocfs2_xattr_header *xh =
3040 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3042 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3043 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3045 prev_bh = *first_bh;
3047 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3049 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3051 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3052 prev_blkno, new_blkno);
3055 * We need to update the 1st half of the new cluster and
3056 * 1 more for the update of the 1st bucket of the previous
3059 credits = bpc / 2 + 1;
3060 ret = ocfs2_extend_trans(handle, credits);
3066 ret = ocfs2_journal_access(handle, inode, prev_bh,
3067 OCFS2_JOURNAL_ACCESS_WRITE);
3073 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3074 old_bh = new_bh = NULL;
3075 new_bh = sb_getblk(inode->i_sb, new_blkno);
3082 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3084 ret = ocfs2_journal_access(handle, inode, new_bh,
3085 OCFS2_JOURNAL_ACCESS_CREATE);
3092 ret = ocfs2_read_block(osb, prev_blkno,
3093 &old_bh, OCFS2_BH_CACHED, inode);
3100 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3103 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3104 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3107 *first_hash = le32_to_cpu(
3108 new_xh->xh_entries[0].xe_name_hash);
3109 new_first_bh = new_bh;
3110 get_bh(new_first_bh);
3113 ocfs2_journal_dirty(handle, new_bh);
3115 if (*header_bh == old_bh) {
3117 *header_bh = new_bh;
3121 *first_bh = new_first_bh;
3128 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3130 ocfs2_journal_dirty(handle, prev_bh);
3133 brelse(new_first_bh);
3137 static int ocfs2_read_xattr_bucket(struct inode *inode,
3139 struct buffer_head **bhs,
3143 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3146 return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
3147 blk_per_bucket, bhs,
3148 OCFS2_BH_CACHED, inode);
3150 for (i = 0; i < blk_per_bucket; i++) {
3151 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3152 if (bhs[i] == NULL) {
3157 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3164 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3165 * first_hash will record the 1st hash of the new bucket.
3167 static int ocfs2_half_xattr_bucket(struct inode *inode,
3172 int new_bucket_head)
3175 u16 count, start, len, name_value_len, xe_len, name_offset;
3176 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3177 struct buffer_head **s_bhs, **t_bhs = NULL;
3178 struct ocfs2_xattr_header *xh;
3179 struct ocfs2_xattr_entry *xe;
3180 int blocksize = inode->i_sb->s_blocksize;
3182 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3185 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3189 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3195 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3196 OCFS2_JOURNAL_ACCESS_WRITE);
3202 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3208 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3214 for (i = 0; i < blk_per_bucket; i++) {
3215 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3216 OCFS2_JOURNAL_ACCESS_CREATE);
3223 /* copy the whole bucket to the new first. */
3224 for (i = 0; i < blk_per_bucket; i++)
3225 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3227 /* update the new bucket. */
3228 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3229 count = le16_to_cpu(xh->xh_count);
3233 * Calculate the total name/value len and xh_free_start for
3234 * the old bucket first.
3236 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3238 for (i = 0; i < start; i++) {
3239 xe = &xh->xh_entries[i];
3240 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3241 if (ocfs2_xattr_is_local(xe))
3243 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3245 xe_len += OCFS2_XATTR_ROOT_SIZE;
3246 name_value_len += xe_len;
3247 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3248 name_offset = le16_to_cpu(xe->xe_name_offset);
3252 * Now begin the modification to the new bucket.
3254 * In the new bucket, We just move the xattr entry to the beginning
3255 * and don't touch the name/value. So there will be some holes in the
3256 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3259 xe = &xh->xh_entries[start];
3260 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3261 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3262 (int)((char *)xe - (char *)xh),
3263 (int)((char *)xh->xh_entries - (char *)xh));
3264 memmove((char *)xh->xh_entries, (char *)xe, len);
3265 xe = &xh->xh_entries[count - start];
3266 len = sizeof(struct ocfs2_xattr_entry) * start;
3267 memset((char *)xe, 0, len);
3269 le16_add_cpu(&xh->xh_count, -start);
3270 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3272 /* Calculate xh_free_start for the new bucket. */
3273 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3274 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3275 xe = &xh->xh_entries[i];
3276 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3277 if (ocfs2_xattr_is_local(xe))
3279 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3281 xe_len += OCFS2_XATTR_ROOT_SIZE;
3282 if (le16_to_cpu(xe->xe_name_offset) <
3283 le16_to_cpu(xh->xh_free_start))
3284 xh->xh_free_start = xe->xe_name_offset;
3287 /* set xh->xh_num_buckets for the new xh. */
3288 if (new_bucket_head)
3289 xh->xh_num_buckets = cpu_to_le16(1);
3291 xh->xh_num_buckets = 0;
3293 for (i = 0; i < blk_per_bucket; i++) {
3294 ocfs2_journal_dirty(handle, t_bhs[i]);
3299 /* store the first_hash of the new bucket. */
3301 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3304 * Now only update the 1st block of the old bucket.
3305 * Please note that the entry has been sorted already above.
3307 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3308 memset(&xh->xh_entries[start], 0,
3309 sizeof(struct ocfs2_xattr_entry) * (count - start));
3310 xh->xh_count = cpu_to_le16(start);
3311 xh->xh_free_start = cpu_to_le16(name_offset);
3312 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3314 ocfs2_journal_dirty(handle, s_bhs[0]);
3320 for (i = 0; i < blk_per_bucket; i++)
3326 for (i = 0; i < blk_per_bucket; i++)
3335 * Copy xattr from one bucket to another bucket.
3337 * The caller must make sure that the journal transaction
3338 * has enough space for journaling.
3340 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3347 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3348 int blocksize = inode->i_sb->s_blocksize;
3349 struct buffer_head **s_bhs, **t_bhs = NULL;
3351 BUG_ON(s_blkno == t_blkno);
3353 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3354 s_blkno, t_blkno, t_is_new);
3356 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3361 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3365 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3372 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3376 for (i = 0; i < blk_per_bucket; i++) {
3377 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3378 OCFS2_JOURNAL_ACCESS_WRITE);
3383 for (i = 0; i < blk_per_bucket; i++) {
3384 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3385 ocfs2_journal_dirty(handle, t_bhs[i]);
3390 for (i = 0; i < blk_per_bucket; i++)
3396 for (i = 0; i < blk_per_bucket; i++)
3405 * Copy one xattr cluster from src_blk to to_blk.
3406 * The to_blk will become the first bucket header of the cluster, so its
3407 * xh_num_buckets will be initialized as the bucket num in the cluster.
3409 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3411 struct buffer_head *first_bh,
3416 int i, ret, credits;
3417 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3418 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3419 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3420 struct buffer_head *bh = NULL;
3421 struct ocfs2_xattr_header *xh;
3422 u64 to_blk_start = to_blk;
3424 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3427 * We need to update the new cluster and 1 more for the update of
3428 * the 1st bucket of the previous extent rec.
3431 ret = ocfs2_extend_trans(handle, credits);
3437 ret = ocfs2_journal_access(handle, inode, first_bh,
3438 OCFS2_JOURNAL_ACCESS_WRITE);
3444 for (i = 0; i < num_buckets; i++) {
3445 ret = ocfs2_cp_xattr_bucket(inode, handle,
3446 src_blk, to_blk, 1);
3452 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3453 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3456 /* update the old bucket header. */
3457 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3458 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3460 ocfs2_journal_dirty(handle, first_bh);
3462 /* update the new bucket header. */
3463 ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
3469 ret = ocfs2_journal_access(handle, inode, bh,
3470 OCFS2_JOURNAL_ACCESS_WRITE);
3476 xh = (struct ocfs2_xattr_header *)bh->b_data;
3477 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3479 ocfs2_journal_dirty(handle, bh);
3482 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3489 * Move half of the xattrs in this cluster to the new cluster.
3490 * This function should only be called when bucket size == cluster size.
3491 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3493 static int ocfs2_half_xattr_cluster(struct inode *inode,
3499 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3500 int ret, credits = 2 * blk_per_bucket;
3502 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3504 ret = ocfs2_extend_trans(handle, credits);
3510 /* Move half of the xattr in start_blk to the next bucket. */
3511 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3512 new_blk, first_hash, 1);
3516 * Move some xattrs from the old cluster to the new one since they are not
3517 * contiguous in ocfs2 xattr tree.
3519 * new_blk starts a new separate cluster, and we will move some xattrs from
3520 * prev_blk to it. v_start will be set as the first name hash value in this
3521 * new cluster so that it can be used as e_cpos during tree insertion and
3522 * don't collide with our original b-tree operations. first_bh and header_bh
3523 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3524 * to extend the insert bucket.
3526 * The problem is how much xattr should we move to the new one and when should
3527 * we update first_bh and header_bh?
3528 * 1. If cluster size > bucket size, that means the previous cluster has more
3529 * than 1 bucket, so just move half nums of bucket into the new cluster and
3530 * update the first_bh and header_bh if the insert bucket has been moved
3531 * to the new cluster.
3532 * 2. If cluster_size == bucket_size:
3533 * a) If the previous extent rec has more than one cluster and the insert
3534 * place isn't in the last cluster, copy the entire last cluster to the
3535 * new one. This time, we don't need to upate the first_bh and header_bh
3536 * since they will not be moved into the new cluster.
3537 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3538 * the new one. And we set the extend flag to zero if the insert place is
3539 * moved into the new allocated cluster since no extend is needed.
3541 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3543 struct buffer_head **first_bh,
3544 struct buffer_head **header_bh,
3552 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3554 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3555 prev_blk, prev_clusters, new_blk);
3557 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3558 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3567 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3569 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3570 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3574 ret = ocfs2_half_xattr_cluster(inode, handle,
3578 if ((*header_bh)->b_blocknr == last_blk && extend)
3587 * Add a new cluster for xattr storage.
3589 * If the new cluster is contiguous with the previous one, it will be
3590 * appended to the same extent record, and num_clusters will be updated.
3591 * If not, we will insert a new extent for it and move some xattrs in
3592 * the last cluster into the new allocated one.
3593 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3594 * lose the benefits of hashing because we'll have to search large leaves.
3595 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3598 * first_bh is the first block of the previous extent rec and header_bh
3599 * indicates the bucket we will insert the new xattrs. They will be updated
3600 * when the header_bh is moved into the new cluster.
3602 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3603 struct buffer_head *root_bh,
3604 struct buffer_head **first_bh,
3605 struct buffer_head **header_bh,
3612 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3613 u32 prev_clusters = *num_clusters;
3614 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3616 handle_t *handle = NULL;
3617 struct ocfs2_alloc_context *data_ac = NULL;
3618 struct ocfs2_alloc_context *meta_ac = NULL;
3619 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3620 struct ocfs2_extent_tree et;
3622 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3623 "previous xattr blkno = %llu\n",
3624 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3625 prev_cpos, prev_blkno);
3627 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3629 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3630 &data_ac, &meta_ac);
3636 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3638 handle = ocfs2_start_trans(osb, credits);
3639 if (IS_ERR(handle)) {
3640 ret = PTR_ERR(handle);
3646 ret = ocfs2_journal_access(handle, inode, root_bh,
3647 OCFS2_JOURNAL_ACCESS_WRITE);
3653 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3654 clusters_to_add, &bit_off, &num_bits);
3661 BUG_ON(num_bits > clusters_to_add);
3663 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3664 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3665 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3667 if (prev_blkno + prev_clusters * bpc == block &&
3668 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3669 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3671 * If this cluster is contiguous with the old one and
3672 * adding this new cluster, we don't surpass the limit of
3673 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3674 * initialized and used like other buckets in the previous
3676 * So add it as a contiguous one. The caller will handle
3679 v_start = prev_cpos + prev_clusters;
3680 *num_clusters = prev_clusters + num_bits;
3681 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3684 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3699 if (handle->h_buffer_credits < credits) {
3701 * The journal has been restarted before, and don't
3702 * have enough space for the insertion, so extend it
3705 ret = ocfs2_extend_trans(handle, credits);
3711 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3712 num_bits, block, v_start);
3713 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3714 num_bits, 0, meta_ac);
3720 ret = ocfs2_journal_dirty(handle, root_bh);
3728 ocfs2_commit_trans(osb, handle);
3730 ocfs2_free_alloc_context(data_ac);
3732 ocfs2_free_alloc_context(meta_ac);
3738 * Extend a new xattr bucket and move xattrs to the end one by one until
3739 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3741 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3742 struct buffer_head *first_bh,
3743 struct buffer_head *start_bh,
3747 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3748 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3749 u64 start_blk = start_bh->b_blocknr, end_blk;
3750 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3752 struct ocfs2_xattr_header *first_xh =
3753 (struct ocfs2_xattr_header *)first_bh->b_data;
3754 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3756 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3757 "from %llu, len = %u\n", start_blk,
3758 (unsigned long long)first_bh->b_blocknr, num_clusters);
3760 BUG_ON(bucket >= num_buckets);
3762 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3765 * We will touch all the buckets after the start_bh(include it).
3766 * Add one more bucket and modify the first_bh.
3768 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3769 handle = ocfs2_start_trans(osb, credits);
3770 if (IS_ERR(handle)) {
3771 ret = PTR_ERR(handle);
3777 ret = ocfs2_journal_access(handle, inode, first_bh,
3778 OCFS2_JOURNAL_ACCESS_WRITE);
3784 while (end_blk != start_blk) {
3785 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3786 end_blk + blk_per_bucket, 0);
3789 end_blk -= blk_per_bucket;
3792 /* Move half of the xattr in start_blk to the next bucket. */
3793 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3794 start_blk + blk_per_bucket, NULL, 0);
3796 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3797 ocfs2_journal_dirty(handle, first_bh);
3800 ocfs2_commit_trans(osb, handle);
3806 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3807 * xb_bh is the ocfs2_xattr_block.
3808 * We will move all the buckets starting from header_bh to the next place. As
3809 * for this one, half num of its xattrs will be moved to the next one.
3811 * We will allocate a new cluster if current cluster is full and adjust
3812 * header_bh and first_bh if the insert place is moved to the new cluster.
3814 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3815 struct buffer_head *xb_bh,
3816 struct buffer_head *header_bh)
3818 struct ocfs2_xattr_header *first_xh = NULL;
3819 struct buffer_head *first_bh = NULL;
3820 struct ocfs2_xattr_block *xb =
3821 (struct ocfs2_xattr_block *)xb_bh->b_data;
3822 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3823 struct ocfs2_extent_list *el = &xb_root->xt_list;
3824 struct ocfs2_xattr_header *xh =
3825 (struct ocfs2_xattr_header *)header_bh->b_data;
3826 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3827 struct super_block *sb = inode->i_sb;
3828 struct ocfs2_super *osb = OCFS2_SB(sb);
3829 int ret, num_buckets, extend = 1;
3831 u32 e_cpos, num_clusters;
3833 mlog(0, "Add new xattr bucket starting form %llu\n",
3834 (unsigned long long)header_bh->b_blocknr);
3837 * Add refrence for header_bh here because it may be
3838 * changed in ocfs2_add_new_xattr_cluster and we need
3839 * to free it in the end.
3843 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3850 ret = ocfs2_read_block(osb, p_blkno,
3851 &first_bh, OCFS2_BH_CACHED, inode);
3857 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3858 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3860 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3861 ret = ocfs2_add_new_xattr_cluster(inode,
3876 ret = ocfs2_extend_xattr_bucket(inode,
3888 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3889 struct ocfs2_xattr_bucket *bucket,
3892 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3894 offs = offs % inode->i_sb->s_blocksize;
3895 return bucket->bhs[block_off]->b_data + offs;
3899 * Handle the normal xattr set, including replace, delete and new.
3901 * Note: "local" indicates the real data's locality. So we can't
3902 * just its bucket locality by its length.
3904 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3905 struct ocfs2_xattr_info *xi,
3906 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);
3962 * Remove the old entry if there is more than one.
3963 * We don't remove the last entry so that we can
3964 * use it to indicate the hash value of the empty
3968 le16_add_cpu(&xh->xh_count, -1);
3971 (void *)last - (void *)xe);
3973 sizeof(struct ocfs2_xattr_entry));
3976 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3981 /* find a new entry for insert. */
3982 int low = 0, high = count - 1, tmp;
3983 struct ocfs2_xattr_entry *tmp_xe;
3985 while (low <= high && count) {
3986 tmp = (low + high) / 2;
3987 tmp_xe = &xh->xh_entries[tmp];
3989 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
3991 else if (name_hash <
3992 le32_to_cpu(tmp_xe->xe_name_hash))
4000 xe = &xh->xh_entries[low];
4002 memmove(xe + 1, xe, (void *)last - (void *)xe);
4004 le16_add_cpu(&xh->xh_count, 1);
4005 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4006 xe->xe_name_hash = cpu_to_le32(name_hash);
4007 xe->xe_name_len = name_len;
4008 ocfs2_xattr_set_type(xe, xi->name_index);
4012 /* Insert the new name+value. */
4013 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4016 * We must make sure that the name/value pair
4017 * exists in the same block.
4019 offs = le16_to_cpu(xh->xh_free_start);
4020 start = offs - size;
4022 if (start >> inode->i_sb->s_blocksize_bits !=
4023 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4024 offs = offs - offs % blocksize;
4025 xh->xh_free_start = cpu_to_le16(offs);
4028 val = ocfs2_xattr_bucket_get_val(inode,
4029 &xs->bucket, offs - size);
4030 xe->xe_name_offset = cpu_to_le16(offs - size);
4032 memset(val, 0, size);
4033 memcpy(val, xi->name, name_len);
4034 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4036 xe->xe_value_size = cpu_to_le64(xi->value_len);
4037 ocfs2_xattr_set_local(xe, local);
4039 le16_add_cpu(&xh->xh_free_start, -size);
4040 le16_add_cpu(&xh->xh_name_value_len, size);
4045 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4047 struct ocfs2_xattr_search *xs,
4048 struct buffer_head **bhs,
4051 int ret = 0, off, block_off;
4052 struct ocfs2_xattr_entry *xe = xs->here;
4055 * First calculate all the blocks we should journal_access
4056 * and journal_dirty. The first block should always be touched.
4058 ret = ocfs2_journal_dirty(handle, bhs[0]);
4062 /* calc the data. */
4063 off = le16_to_cpu(xe->xe_name_offset);
4064 block_off = off >> inode->i_sb->s_blocksize_bits;
4065 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4073 * Set the xattr entry in the specified bucket.
4074 * The bucket is indicated by xs->bucket and it should have the enough
4075 * space for the xattr insertion.
4077 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4078 struct ocfs2_xattr_info *xi,
4079 struct ocfs2_xattr_search *xs,
4084 handle_t *handle = NULL;
4085 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4086 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4088 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4089 (unsigned long)xi->value_len, xi->name_index,
4090 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4092 if (!xs->bucket.bhs[1]) {
4093 ret = ocfs2_read_blocks(osb,
4094 xs->bucket.bhs[0]->b_blocknr + 1,
4095 blk_per_bucket - 1, &xs->bucket.bhs[1],
4096 OCFS2_BH_CACHED, inode);
4103 handle = ocfs2_start_trans(osb, blk_per_bucket);
4104 if (IS_ERR(handle)) {
4105 ret = PTR_ERR(handle);
4111 for (i = 0; i < blk_per_bucket; i++) {
4112 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4113 OCFS2_JOURNAL_ACCESS_WRITE);
4120 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4122 /*Only dirty the blocks we have touched in set xattr. */
4123 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4124 xs->bucket.bhs, blk_per_bucket);
4128 ocfs2_commit_trans(osb, handle);
4133 static int ocfs2_xattr_value_update_size(struct inode *inode,
4134 struct buffer_head *xe_bh,
4135 struct ocfs2_xattr_entry *xe,
4139 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4140 handle_t *handle = NULL;
4142 handle = ocfs2_start_trans(osb, 1);
4143 if (handle == NULL) {
4149 ret = ocfs2_journal_access(handle, inode, xe_bh,
4150 OCFS2_JOURNAL_ACCESS_WRITE);
4156 xe->xe_value_size = cpu_to_le64(new_size);
4158 ret = ocfs2_journal_dirty(handle, xe_bh);
4163 ocfs2_commit_trans(osb, handle);
4169 * Truncate the specified xe_off entry in xattr bucket.
4170 * bucket is indicated by header_bh and len is the new length.
4171 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4173 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4175 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4176 struct buffer_head *header_bh,
4182 struct buffer_head *value_bh = NULL;
4183 struct ocfs2_xattr_value_root *xv;
4184 struct ocfs2_xattr_entry *xe;
4185 struct ocfs2_xattr_header *xh =
4186 (struct ocfs2_xattr_header *)header_bh->b_data;
4187 size_t blocksize = inode->i_sb->s_blocksize;
4189 xe = &xh->xh_entries[xe_off];
4191 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4193 offset = le16_to_cpu(xe->xe_name_offset) +
4194 OCFS2_XATTR_SIZE(xe->xe_name_len);
4196 value_blk = offset / blocksize;
4198 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4199 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4200 value_blk += header_bh->b_blocknr;
4202 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
4203 &value_bh, OCFS2_BH_CACHED, inode);
4209 xv = (struct ocfs2_xattr_value_root *)
4210 (value_bh->b_data + offset % blocksize);
4212 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4213 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4214 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4220 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4231 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4232 struct ocfs2_xattr_search *xs,
4236 struct ocfs2_xattr_entry *xe = xs->here;
4237 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4239 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4241 offset = xe - xh->xh_entries;
4242 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4250 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4251 struct ocfs2_xattr_search *xs,
4256 struct ocfs2_xattr_value_root *xv;
4257 struct ocfs2_xattr_entry *xe = xs->here;
4259 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4261 offset = le16_to_cpu(xe->xe_name_offset) +
4262 OCFS2_XATTR_SIZE(xe->xe_name_len);
4264 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4266 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4269 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4270 struct buffer_head *root_bh,
4276 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4277 struct inode *tl_inode = osb->osb_tl_inode;
4279 struct ocfs2_xattr_block *xb =
4280 (struct ocfs2_xattr_block *)root_bh->b_data;
4281 struct ocfs2_alloc_context *meta_ac = NULL;
4282 struct ocfs2_cached_dealloc_ctxt dealloc;
4283 struct ocfs2_extent_tree et;
4285 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4287 ocfs2_init_dealloc_ctxt(&dealloc);
4289 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4290 cpos, len, (unsigned long long)blkno);
4292 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4294 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4300 mutex_lock(&tl_inode->i_mutex);
4302 if (ocfs2_truncate_log_needs_flush(osb)) {
4303 ret = __ocfs2_flush_truncate_log(osb);
4310 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4311 if (handle == NULL) {
4317 ret = ocfs2_journal_access(handle, inode, root_bh,
4318 OCFS2_JOURNAL_ACCESS_WRITE);
4324 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4331 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4333 ret = ocfs2_journal_dirty(handle, root_bh);
4339 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4344 ocfs2_commit_trans(osb, handle);
4346 ocfs2_schedule_truncate_log_flush(osb, 1);
4348 mutex_unlock(&tl_inode->i_mutex);
4351 ocfs2_free_alloc_context(meta_ac);
4353 ocfs2_run_deallocs(osb, &dealloc);
4358 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4359 struct ocfs2_xattr_search *xs)
4361 handle_t *handle = NULL;
4362 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4363 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4364 le16_to_cpu(xh->xh_count) - 1];
4367 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4368 if (IS_ERR(handle)) {
4369 ret = PTR_ERR(handle);
4374 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4375 OCFS2_JOURNAL_ACCESS_WRITE);
4381 /* Remove the old entry. */
4382 memmove(xs->here, xs->here + 1,
4383 (void *)last - (void *)xs->here);
4384 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4385 le16_add_cpu(&xh->xh_count, -1);
4387 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4391 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4395 * Set the xattr name/value in the bucket specified in xs.
4397 * As the new value in xi may be stored in the bucket or in an outside cluster,
4398 * we divide the whole process into 3 steps:
4399 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4400 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4401 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4402 * 4. If the clusters for the new outside value can't be allocated, we need
4403 * to free the xattr we allocated in set.
4405 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4406 struct ocfs2_xattr_info *xi,
4407 struct ocfs2_xattr_search *xs)
4411 char *val = (char *)xi->value;
4412 struct ocfs2_xattr_entry *xe = xs->here;
4413 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4416 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4418 * We need to truncate the xattr storage first.
4420 * If both the old and new value are stored to
4421 * outside block, we only need to truncate
4422 * the storage and then set the value outside.
4424 * If the new value should be stored within block,
4425 * we should free all the outside block first and
4426 * the modification to the xattr block will be done
4427 * by following steps.
4429 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4430 value_len = xi->value_len;
4434 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4440 goto set_value_outside;
4443 value_len = xi->value_len;
4444 /* So we have to handle the inside block change now. */
4445 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4447 * If the new value will be stored outside of block,
4448 * initalize a new empty value root and insert it first.
4451 xi->value = &def_xv;
4452 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4455 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4461 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4464 /* allocate the space now for the outside block storage. */
4465 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4470 if (xs->not_found) {
4472 * We can't allocate enough clusters for outside
4473 * storage and we have allocated xattr already,
4474 * so need to remove it.
4476 ocfs2_xattr_bucket_remove_xs(inode, xs);
4482 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4487 /* check whether the xattr bucket is filled up with the same hash value. */
4488 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4489 struct ocfs2_xattr_bucket *bucket)
4491 struct ocfs2_xattr_header *xh = bucket->xh;
4493 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4494 xh->xh_entries[0].xe_name_hash) {
4495 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4497 (unsigned long long)bucket->bhs[0]->b_blocknr,
4498 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4505 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4506 struct ocfs2_xattr_info *xi,
4507 struct ocfs2_xattr_search *xs)
4509 struct ocfs2_xattr_header *xh;
4510 struct ocfs2_xattr_entry *xe;
4511 u16 count, header_size, xh_free_start;
4512 int i, free, max_free, need, old;
4513 size_t value_size = 0, name_len = strlen(xi->name);
4514 size_t blocksize = inode->i_sb->s_blocksize;
4515 int ret, allocation = 0;
4516 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4518 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4522 count = le16_to_cpu(xh->xh_count);
4523 xh_free_start = le16_to_cpu(xh->xh_free_start);
4524 header_size = sizeof(struct ocfs2_xattr_header) +
4525 count * sizeof(struct ocfs2_xattr_entry);
4526 max_free = OCFS2_XATTR_BUCKET_SIZE -
4527 le16_to_cpu(xh->xh_name_value_len) - header_size;
4529 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4530 "of %u which exceed block size\n",
4531 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4534 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4535 value_size = OCFS2_XATTR_ROOT_SIZE;
4537 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4540 need = sizeof(struct ocfs2_xattr_entry) +
4541 OCFS2_XATTR_SIZE(name_len) + value_size;
4543 need = value_size + OCFS2_XATTR_SIZE(name_len);
4546 * We only replace the old value if the new length is smaller
4547 * than the old one. Otherwise we will allocate new space in the
4548 * bucket to store it.
4551 if (ocfs2_xattr_is_local(xe))
4552 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4554 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4556 if (old >= value_size)
4560 free = xh_free_start - header_size;
4562 * We need to make sure the new name/value pair
4563 * can exist in the same block.
4565 if (xh_free_start % blocksize < need)
4566 free -= xh_free_start % blocksize;
4568 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4569 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4570 " %u\n", xs->not_found,
4571 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4572 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4573 le16_to_cpu(xh->xh_name_value_len));
4575 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4576 if (need <= max_free &&
4577 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4579 * We can create the space by defragment. Since only the
4580 * name/value will be moved, the xe shouldn't be changed
4583 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4589 xh_free_start = le16_to_cpu(xh->xh_free_start);
4590 free = xh_free_start - header_size;
4591 if (xh_free_start % blocksize < need)
4592 free -= xh_free_start % blocksize;
4597 mlog(0, "Can't get enough space for xattr insert by "
4598 "defragment. Need %u bytes, but we have %d, so "
4599 "allocate new bucket for it.\n", need, free);
4603 * We have to add new buckets or clusters and one
4604 * allocation should leave us enough space for insert.
4609 * We do not allow for overlapping ranges between buckets. And
4610 * the maximum number of collisions we will allow for then is
4611 * one bucket's worth, so check it here whether we need to
4612 * add a new bucket for the insert.
4614 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4620 ret = ocfs2_add_new_xattr_bucket(inode,
4628 for (i = 0; i < blk_per_bucket; i++)
4629 brelse(xs->bucket.bhs[i]);
4631 memset(&xs->bucket, 0, sizeof(xs->bucket));
4633 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4636 if (ret && ret != -ENODATA)
4638 xs->not_found = ret;
4644 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4650 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4651 struct ocfs2_xattr_bucket *bucket,
4655 struct ocfs2_xattr_header *xh = bucket->xh;
4657 struct ocfs2_xattr_entry *xe;
4659 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4660 xe = &xh->xh_entries[i];
4661 if (ocfs2_xattr_is_local(xe))
4664 ret = ocfs2_xattr_bucket_value_truncate(inode,
4676 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4677 struct buffer_head *xb_bh)
4679 struct ocfs2_xattr_block *xb =
4680 (struct ocfs2_xattr_block *)xb_bh->b_data;
4681 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4683 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4686 if (le16_to_cpu(el->l_next_free_rec) == 0)
4689 while (name_hash > 0) {
4690 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4691 &e_cpos, &num_clusters, el);
4697 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4698 ocfs2_delete_xattr_in_bucket,
4705 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4706 p_blkno, e_cpos, num_clusters);
4715 name_hash = e_cpos - 1;
4723 * 'trusted' attributes support
4726 #define XATTR_TRUSTED_PREFIX "trusted."
4728 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4729 size_t list_size, const char *name,
4732 const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1;
4733 const size_t total_len = prefix_len + name_len + 1;
4735 if (list && total_len <= list_size) {
4736 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4737 memcpy(list + prefix_len, name, name_len);
4738 list[prefix_len + name_len] = '\0';
4743 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4744 void *buffer, size_t size)
4746 if (strcmp(name, "") == 0)
4748 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4752 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4753 const void *value, size_t size, int flags)
4755 if (strcmp(name, "") == 0)
4758 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4762 struct xattr_handler ocfs2_xattr_trusted_handler = {
4763 .prefix = XATTR_TRUSTED_PREFIX,
4764 .list = ocfs2_xattr_trusted_list,
4765 .get = ocfs2_xattr_trusted_get,
4766 .set = ocfs2_xattr_trusted_set,
4771 * 'user' attributes support
4774 #define XATTR_USER_PREFIX "user."
4776 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4777 size_t list_size, const char *name,
4780 const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1;
4781 const size_t total_len = prefix_len + name_len + 1;
4782 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4784 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4787 if (list && total_len <= list_size) {
4788 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4789 memcpy(list + prefix_len, name, name_len);
4790 list[prefix_len + name_len] = '\0';
4795 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4796 void *buffer, size_t size)
4798 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4800 if (strcmp(name, "") == 0)
4802 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4804 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4808 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4809 const void *value, size_t size, int flags)
4811 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4813 if (strcmp(name, "") == 0)
4815 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4818 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4822 struct xattr_handler ocfs2_xattr_user_handler = {
4823 .prefix = XATTR_USER_PREFIX,
4824 .list = ocfs2_xattr_user_list,
4825 .get = ocfs2_xattr_user_get,
4826 .set = ocfs2_xattr_user_set,