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 const char *ocfs2_xattr_prefix(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];
147 return handler ? handler->prefix : NULL;
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_entry(char *buffer, size_t size,
456 size_t *result, const char *prefix,
457 const char *name, int name_len)
459 char *p = buffer + *result;
460 int prefix_len = strlen(prefix);
461 int total_len = prefix_len + name_len + 1;
463 *result += total_len;
465 /* we are just looking for how big our buffer needs to be */
472 memcpy(p, prefix, prefix_len);
473 memcpy(p + prefix_len, name, name_len);
474 p[prefix_len + name_len] = '\0';
479 static int ocfs2_xattr_list_entries(struct inode *inode,
480 struct ocfs2_xattr_header *header,
481 char *buffer, size_t buffer_size)
485 const char *prefix, *name;
487 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
488 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
489 type = ocfs2_xattr_get_type(entry);
490 prefix = ocfs2_xattr_prefix(type);
493 name = (const char *)header +
494 le16_to_cpu(entry->xe_name_offset);
496 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
497 &result, prefix, name,
507 static int ocfs2_xattr_ibody_list(struct inode *inode,
508 struct ocfs2_dinode *di,
512 struct ocfs2_xattr_header *header = NULL;
513 struct ocfs2_inode_info *oi = OCFS2_I(inode);
516 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
519 header = (struct ocfs2_xattr_header *)
520 ((void *)di + inode->i_sb->s_blocksize -
521 le16_to_cpu(di->i_xattr_inline_size));
523 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
528 static int ocfs2_xattr_block_list(struct inode *inode,
529 struct ocfs2_dinode *di,
533 struct buffer_head *blk_bh = NULL;
534 struct ocfs2_xattr_block *xb;
537 if (!di->i_xattr_loc)
540 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
541 le64_to_cpu(di->i_xattr_loc),
542 &blk_bh, OCFS2_BH_CACHED, inode);
547 /*Verify the signature of xattr block*/
548 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
549 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
554 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
556 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
557 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
558 ret = ocfs2_xattr_list_entries(inode, header,
559 buffer, buffer_size);
561 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
562 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
563 buffer, buffer_size);
571 ssize_t ocfs2_listxattr(struct dentry *dentry,
575 int ret = 0, i_ret = 0, b_ret = 0;
576 struct buffer_head *di_bh = NULL;
577 struct ocfs2_dinode *di = NULL;
578 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
580 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
583 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
586 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
592 di = (struct ocfs2_dinode *)di_bh->b_data;
594 down_read(&oi->ip_xattr_sem);
595 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
603 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
608 up_read(&oi->ip_xattr_sem);
609 ocfs2_inode_unlock(dentry->d_inode, 0);
613 return i_ret + b_ret;
616 static int ocfs2_xattr_find_entry(int name_index,
618 struct ocfs2_xattr_search *xs)
620 struct ocfs2_xattr_entry *entry;
627 name_len = strlen(name);
629 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
630 cmp = name_index - ocfs2_xattr_get_type(entry);
632 cmp = name_len - entry->xe_name_len;
634 cmp = memcmp(name, (xs->base +
635 le16_to_cpu(entry->xe_name_offset)),
643 return cmp ? -ENODATA : 0;
646 static int ocfs2_xattr_get_value_outside(struct inode *inode,
647 struct ocfs2_xattr_value_root *xv,
651 u32 cpos, p_cluster, num_clusters, bpc, clusters;
654 size_t cplen, blocksize;
655 struct buffer_head *bh = NULL;
656 struct ocfs2_extent_list *el;
659 clusters = le32_to_cpu(xv->xr_clusters);
660 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
661 blocksize = inode->i_sb->s_blocksize;
664 while (cpos < clusters) {
665 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
672 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
673 /* Copy ocfs2_xattr_value */
674 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
675 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
676 &bh, OCFS2_BH_CACHED, inode);
682 cplen = len >= blocksize ? blocksize : len;
683 memcpy(buffer, bh->b_data, cplen);
692 cpos += num_clusters;
698 static int ocfs2_xattr_ibody_get(struct inode *inode,
703 struct ocfs2_xattr_search *xs)
705 struct ocfs2_inode_info *oi = OCFS2_I(inode);
706 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
707 struct ocfs2_xattr_value_root *xv;
711 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
714 xs->end = (void *)di + inode->i_sb->s_blocksize;
715 xs->header = (struct ocfs2_xattr_header *)
716 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
717 xs->base = (void *)xs->header;
718 xs->here = xs->header->xh_entries;
720 ret = ocfs2_xattr_find_entry(name_index, name, xs);
723 size = le64_to_cpu(xs->here->xe_value_size);
725 if (size > buffer_size)
727 if (ocfs2_xattr_is_local(xs->here)) {
728 memcpy(buffer, (void *)xs->base +
729 le16_to_cpu(xs->here->xe_name_offset) +
730 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
732 xv = (struct ocfs2_xattr_value_root *)
733 (xs->base + le16_to_cpu(
734 xs->here->xe_name_offset) +
735 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
736 ret = ocfs2_xattr_get_value_outside(inode, xv,
748 static int ocfs2_xattr_block_get(struct inode *inode,
753 struct ocfs2_xattr_search *xs)
755 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
756 struct buffer_head *blk_bh = NULL;
757 struct ocfs2_xattr_block *xb;
758 struct ocfs2_xattr_value_root *xv;
760 int ret = -ENODATA, name_offset, name_len, block_off, i;
762 if (!di->i_xattr_loc)
765 memset(&xs->bucket, 0, sizeof(xs->bucket));
767 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
768 le64_to_cpu(di->i_xattr_loc),
769 &blk_bh, OCFS2_BH_CACHED, inode);
774 /*Verify the signature of xattr block*/
775 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
776 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
781 xs->xattr_bh = blk_bh;
782 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
784 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
785 xs->header = &xb->xb_attrs.xb_header;
786 xs->base = (void *)xs->header;
787 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
788 xs->here = xs->header->xh_entries;
790 ret = ocfs2_xattr_find_entry(name_index, name, xs);
792 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
798 size = le64_to_cpu(xs->here->xe_value_size);
801 if (size > buffer_size)
804 name_offset = le16_to_cpu(xs->here->xe_name_offset);
805 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
806 i = xs->here - xs->header->xh_entries;
808 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
809 ret = ocfs2_xattr_bucket_get_name_value(inode,
814 xs->base = xs->bucket.bhs[block_off]->b_data;
816 if (ocfs2_xattr_is_local(xs->here)) {
817 memcpy(buffer, (void *)xs->base +
818 name_offset + name_len, size);
820 xv = (struct ocfs2_xattr_value_root *)
821 (xs->base + name_offset + name_len);
822 ret = ocfs2_xattr_get_value_outside(inode, xv,
832 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
833 brelse(xs->bucket.bhs[i]);
834 memset(&xs->bucket, 0, sizeof(xs->bucket));
842 * Copy an extended attribute into the buffer provided.
843 * Buffer is NULL to compute the size of buffer required.
845 int ocfs2_xattr_get(struct inode *inode,
852 struct ocfs2_dinode *di = NULL;
853 struct buffer_head *di_bh = NULL;
854 struct ocfs2_inode_info *oi = OCFS2_I(inode);
855 struct ocfs2_xattr_search xis = {
856 .not_found = -ENODATA,
858 struct ocfs2_xattr_search xbs = {
859 .not_found = -ENODATA,
862 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
865 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
868 ret = ocfs2_inode_lock(inode, &di_bh, 0);
873 xis.inode_bh = xbs.inode_bh = di_bh;
874 di = (struct ocfs2_dinode *)di_bh->b_data;
876 down_read(&oi->ip_xattr_sem);
877 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
880 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
882 up_read(&oi->ip_xattr_sem);
883 ocfs2_inode_unlock(inode, 0);
890 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
891 struct ocfs2_xattr_value_root *xv,
895 int ret = 0, i, cp_len, credits;
896 u16 blocksize = inode->i_sb->s_blocksize;
897 u32 p_cluster, num_clusters;
898 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
899 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
901 struct buffer_head *bh = NULL;
904 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
906 credits = clusters * bpc;
907 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
908 if (IS_ERR(handle)) {
909 ret = PTR_ERR(handle);
914 while (cpos < clusters) {
915 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
916 &num_clusters, &xv->xr_list);
922 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
924 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
925 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
926 &bh, OCFS2_BH_CACHED, inode);
932 ret = ocfs2_journal_access(handle,
935 OCFS2_JOURNAL_ACCESS_WRITE);
941 cp_len = value_len > blocksize ? blocksize : value_len;
942 memcpy(bh->b_data, value, cp_len);
945 if (cp_len < blocksize)
946 memset(bh->b_data + cp_len, 0,
949 ret = ocfs2_journal_dirty(handle, bh);
958 * XXX: do we need to empty all the following
959 * blocks in this cluster?
964 cpos += num_clusters;
967 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
974 static int ocfs2_xattr_cleanup(struct inode *inode,
975 struct ocfs2_xattr_info *xi,
976 struct ocfs2_xattr_search *xs,
979 handle_t *handle = NULL;
981 size_t name_len = strlen(xi->name);
982 void *val = xs->base + offs;
983 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
985 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
986 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
987 if (IS_ERR(handle)) {
988 ret = PTR_ERR(handle);
992 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
993 OCFS2_JOURNAL_ACCESS_WRITE);
998 /* Decrease xattr count */
999 le16_add_cpu(&xs->header->xh_count, -1);
1000 /* Remove the xattr entry and tree root which has already be set*/
1001 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1002 memset(val, 0, size);
1004 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1008 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1013 static int ocfs2_xattr_update_entry(struct inode *inode,
1014 struct ocfs2_xattr_info *xi,
1015 struct ocfs2_xattr_search *xs,
1018 handle_t *handle = NULL;
1021 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1022 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1023 if (IS_ERR(handle)) {
1024 ret = PTR_ERR(handle);
1028 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1029 OCFS2_JOURNAL_ACCESS_WRITE);
1035 xs->here->xe_name_offset = cpu_to_le16(offs);
1036 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1037 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1038 ocfs2_xattr_set_local(xs->here, 1);
1040 ocfs2_xattr_set_local(xs->here, 0);
1041 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1043 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1047 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1053 * ocfs2_xattr_set_value_outside()
1055 * Set large size value in B tree.
1057 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1058 struct ocfs2_xattr_info *xi,
1059 struct ocfs2_xattr_search *xs,
1062 size_t name_len = strlen(xi->name);
1063 void *val = xs->base + offs;
1064 struct ocfs2_xattr_value_root *xv = NULL;
1065 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1068 memset(val, 0, size);
1069 memcpy(val, xi->name, name_len);
1070 xv = (struct ocfs2_xattr_value_root *)
1071 (val + OCFS2_XATTR_SIZE(name_len));
1072 xv->xr_clusters = 0;
1073 xv->xr_last_eb_blk = 0;
1074 xv->xr_list.l_tree_depth = 0;
1075 xv->xr_list.l_count = cpu_to_le16(1);
1076 xv->xr_list.l_next_free_rec = 0;
1078 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1084 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1090 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1098 * ocfs2_xattr_set_entry_local()
1100 * Set, replace or remove extended attribute in local.
1102 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1103 struct ocfs2_xattr_info *xi,
1104 struct ocfs2_xattr_search *xs,
1105 struct ocfs2_xattr_entry *last,
1108 size_t name_len = strlen(xi->name);
1111 if (xi->value && xs->not_found) {
1112 /* Insert the new xattr entry. */
1113 le16_add_cpu(&xs->header->xh_count, 1);
1114 ocfs2_xattr_set_type(last, xi->name_index);
1115 ocfs2_xattr_set_local(last, 1);
1116 last->xe_name_len = name_len;
1122 first_val = xs->base + min_offs;
1123 offs = le16_to_cpu(xs->here->xe_name_offset);
1124 val = xs->base + offs;
1126 if (le64_to_cpu(xs->here->xe_value_size) >
1127 OCFS2_XATTR_INLINE_SIZE)
1128 size = OCFS2_XATTR_SIZE(name_len) +
1129 OCFS2_XATTR_ROOT_SIZE;
1131 size = OCFS2_XATTR_SIZE(name_len) +
1132 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1134 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1135 OCFS2_XATTR_SIZE(xi->value_len)) {
1136 /* The old and the new value have the
1137 same size. Just replace the value. */
1138 ocfs2_xattr_set_local(xs->here, 1);
1139 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1140 /* Clear value bytes. */
1141 memset(val + OCFS2_XATTR_SIZE(name_len),
1143 OCFS2_XATTR_SIZE(xi->value_len));
1144 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1149 /* Remove the old name+value. */
1150 memmove(first_val + size, first_val, val - first_val);
1151 memset(first_val, 0, size);
1152 xs->here->xe_name_hash = 0;
1153 xs->here->xe_name_offset = 0;
1154 ocfs2_xattr_set_local(xs->here, 1);
1155 xs->here->xe_value_size = 0;
1159 /* Adjust all value offsets. */
1160 last = xs->header->xh_entries;
1161 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1162 size_t o = le16_to_cpu(last->xe_name_offset);
1165 last->xe_name_offset = cpu_to_le16(o + size);
1170 /* Remove the old entry. */
1172 memmove(xs->here, xs->here + 1,
1173 (void *)last - (void *)xs->here);
1174 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1175 le16_add_cpu(&xs->header->xh_count, -1);
1179 /* Insert the new name+value. */
1180 size_t size = OCFS2_XATTR_SIZE(name_len) +
1181 OCFS2_XATTR_SIZE(xi->value_len);
1182 void *val = xs->base + min_offs - size;
1184 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1185 memset(val, 0, size);
1186 memcpy(val, xi->name, name_len);
1187 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1190 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1191 ocfs2_xattr_set_local(xs->here, 1);
1192 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1199 * ocfs2_xattr_set_entry()
1201 * Set extended attribute entry into inode or block.
1203 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1204 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1205 * then set value in B tree with set_value_outside().
1207 static int ocfs2_xattr_set_entry(struct inode *inode,
1208 struct ocfs2_xattr_info *xi,
1209 struct ocfs2_xattr_search *xs,
1212 struct ocfs2_xattr_entry *last;
1213 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1214 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1215 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1217 handle_t *handle = NULL;
1219 struct ocfs2_xattr_info xi_l = {
1220 .name_index = xi->name_index,
1223 .value_len = xi->value_len,
1226 /* Compute min_offs, last and free space. */
1227 last = xs->header->xh_entries;
1229 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1230 size_t offs = le16_to_cpu(last->xe_name_offset);
1231 if (offs < min_offs)
1236 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1240 if (!xs->not_found) {
1242 if (ocfs2_xattr_is_local(xs->here))
1243 size = OCFS2_XATTR_SIZE(name_len) +
1244 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1246 size = OCFS2_XATTR_SIZE(name_len) +
1247 OCFS2_XATTR_ROOT_SIZE;
1248 free += (size + sizeof(struct ocfs2_xattr_entry));
1250 /* Check free space in inode or block */
1251 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1252 if (free < sizeof(struct ocfs2_xattr_entry) +
1253 OCFS2_XATTR_SIZE(name_len) +
1254 OCFS2_XATTR_ROOT_SIZE) {
1258 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1259 xi_l.value = (void *)&def_xv;
1260 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1261 } else if (xi->value) {
1262 if (free < sizeof(struct ocfs2_xattr_entry) +
1263 OCFS2_XATTR_SIZE(name_len) +
1264 OCFS2_XATTR_SIZE(xi->value_len)) {
1270 if (!xs->not_found) {
1271 /* For existing extended attribute */
1272 size_t size = OCFS2_XATTR_SIZE(name_len) +
1273 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1274 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1275 void *val = xs->base + offs;
1277 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1278 /* Replace existing local xattr with tree root */
1279 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1284 } else if (!ocfs2_xattr_is_local(xs->here)) {
1285 /* For existing xattr which has value outside */
1286 struct ocfs2_xattr_value_root *xv = NULL;
1287 xv = (struct ocfs2_xattr_value_root *)(val +
1288 OCFS2_XATTR_SIZE(name_len));
1290 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1292 * If new value need set outside also,
1293 * first truncate old value to new value,
1294 * then set new value with set_value_outside().
1296 ret = ocfs2_xattr_value_truncate(inode,
1305 ret = __ocfs2_xattr_set_value_outside(inode,
1314 ret = ocfs2_xattr_update_entry(inode,
1323 * If new value need set in local,
1324 * just trucate old value to zero.
1326 ret = ocfs2_xattr_value_truncate(inode,
1336 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1337 OCFS2_INODE_UPDATE_CREDITS);
1338 if (IS_ERR(handle)) {
1339 ret = PTR_ERR(handle);
1344 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1345 OCFS2_JOURNAL_ACCESS_WRITE);
1351 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1352 /* set extended attribute in external block. */
1353 ret = ocfs2_extend_trans(handle,
1354 OCFS2_INODE_UPDATE_CREDITS +
1355 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1360 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1361 OCFS2_JOURNAL_ACCESS_WRITE);
1369 * Set value in local, include set tree root in local.
1370 * This is the first step for value size >INLINE_SIZE.
1372 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1374 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1375 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1382 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1383 (flag & OCFS2_INLINE_XATTR_FL)) {
1384 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1385 unsigned int xattrsize = osb->s_xattr_inline_size;
1388 * Adjust extent record count or inline data size
1389 * to reserve space for extended attribute.
1391 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1392 struct ocfs2_inline_data *idata = &di->id2.i_data;
1393 le16_add_cpu(&idata->id_count, -xattrsize);
1394 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1395 struct ocfs2_extent_list *el = &di->id2.i_list;
1396 le16_add_cpu(&el->l_count, -(xattrsize /
1397 sizeof(struct ocfs2_extent_rec)));
1399 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1401 /* Update xattr flag */
1402 spin_lock(&oi->ip_lock);
1403 oi->ip_dyn_features |= flag;
1404 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1405 spin_unlock(&oi->ip_lock);
1406 /* Update inode ctime */
1407 inode->i_ctime = CURRENT_TIME;
1408 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1409 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1411 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1416 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1418 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1420 * Set value outside in B tree.
1421 * This is the second step for value size > INLINE_SIZE.
1423 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1424 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1430 * If set value outside failed, we have to clean
1431 * the junk tree root we have already set in local.
1433 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1443 static int ocfs2_remove_value_outside(struct inode*inode,
1444 struct buffer_head *bh,
1445 struct ocfs2_xattr_header *header)
1449 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1450 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1452 if (!ocfs2_xattr_is_local(entry)) {
1453 struct ocfs2_xattr_value_root *xv;
1456 val = (void *)header +
1457 le16_to_cpu(entry->xe_name_offset);
1458 xv = (struct ocfs2_xattr_value_root *)
1459 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1460 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1471 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1472 struct buffer_head *di_bh)
1475 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1476 struct ocfs2_xattr_header *header;
1479 header = (struct ocfs2_xattr_header *)
1480 ((void *)di + inode->i_sb->s_blocksize -
1481 le16_to_cpu(di->i_xattr_inline_size));
1483 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1488 static int ocfs2_xattr_block_remove(struct inode *inode,
1489 struct buffer_head *blk_bh)
1491 struct ocfs2_xattr_block *xb;
1494 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1495 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1496 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1497 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1499 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1504 static int ocfs2_xattr_free_block(struct inode *inode,
1507 struct inode *xb_alloc_inode;
1508 struct buffer_head *xb_alloc_bh = NULL;
1509 struct buffer_head *blk_bh = NULL;
1510 struct ocfs2_xattr_block *xb;
1511 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1517 ret = ocfs2_read_block(osb, block, &blk_bh,
1518 OCFS2_BH_CACHED, inode);
1524 /*Verify the signature of xattr block*/
1525 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1526 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1531 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1537 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1538 blk = le64_to_cpu(xb->xb_blkno);
1539 bit = le16_to_cpu(xb->xb_suballoc_bit);
1540 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1542 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1543 EXTENT_ALLOC_SYSTEM_INODE,
1544 le16_to_cpu(xb->xb_suballoc_slot));
1545 if (!xb_alloc_inode) {
1550 mutex_lock(&xb_alloc_inode->i_mutex);
1552 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1558 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1559 if (IS_ERR(handle)) {
1560 ret = PTR_ERR(handle);
1565 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1570 ocfs2_commit_trans(osb, handle);
1572 ocfs2_inode_unlock(xb_alloc_inode, 1);
1573 brelse(xb_alloc_bh);
1575 mutex_unlock(&xb_alloc_inode->i_mutex);
1576 iput(xb_alloc_inode);
1583 * ocfs2_xattr_remove()
1585 * Free extended attribute resources associated with this inode.
1587 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1589 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1590 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1594 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1597 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1600 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1601 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1608 if (di->i_xattr_loc) {
1609 ret = ocfs2_xattr_free_block(inode,
1610 le64_to_cpu(di->i_xattr_loc));
1617 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1618 OCFS2_INODE_UPDATE_CREDITS);
1619 if (IS_ERR(handle)) {
1620 ret = PTR_ERR(handle);
1624 ret = ocfs2_journal_access(handle, inode, di_bh,
1625 OCFS2_JOURNAL_ACCESS_WRITE);
1631 di->i_xattr_loc = 0;
1633 spin_lock(&oi->ip_lock);
1634 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1635 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1636 spin_unlock(&oi->ip_lock);
1638 ret = ocfs2_journal_dirty(handle, di_bh);
1642 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1647 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1648 struct ocfs2_dinode *di)
1650 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1651 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1654 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1657 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1658 struct ocfs2_inline_data *idata = &di->id2.i_data;
1659 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1660 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1661 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1662 le64_to_cpu(di->i_size);
1664 struct ocfs2_extent_list *el = &di->id2.i_list;
1665 free = (le16_to_cpu(el->l_count) -
1666 le16_to_cpu(el->l_next_free_rec)) *
1667 sizeof(struct ocfs2_extent_rec);
1669 if (free >= xattrsize)
1676 * ocfs2_xattr_ibody_find()
1678 * Find extended attribute in inode block and
1679 * fill search info into struct ocfs2_xattr_search.
1681 static int ocfs2_xattr_ibody_find(struct inode *inode,
1684 struct ocfs2_xattr_search *xs)
1686 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1687 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1691 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1694 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1695 down_read(&oi->ip_alloc_sem);
1696 has_space = ocfs2_xattr_has_space_inline(inode, di);
1697 up_read(&oi->ip_alloc_sem);
1702 xs->xattr_bh = xs->inode_bh;
1703 xs->end = (void *)di + inode->i_sb->s_blocksize;
1704 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1705 xs->header = (struct ocfs2_xattr_header *)
1706 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1708 xs->header = (struct ocfs2_xattr_header *)
1709 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1710 xs->base = (void *)xs->header;
1711 xs->here = xs->header->xh_entries;
1713 /* Find the named attribute. */
1714 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1715 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1716 if (ret && ret != -ENODATA)
1718 xs->not_found = ret;
1725 * ocfs2_xattr_ibody_set()
1727 * Set, replace or remove an extended attribute into inode block.
1730 static int ocfs2_xattr_ibody_set(struct inode *inode,
1731 struct ocfs2_xattr_info *xi,
1732 struct ocfs2_xattr_search *xs)
1734 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1735 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1738 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1741 down_write(&oi->ip_alloc_sem);
1742 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1743 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1749 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1750 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1752 up_write(&oi->ip_alloc_sem);
1758 * ocfs2_xattr_block_find()
1760 * Find extended attribute in external block and
1761 * fill search info into struct ocfs2_xattr_search.
1763 static int ocfs2_xattr_block_find(struct inode *inode,
1766 struct ocfs2_xattr_search *xs)
1768 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1769 struct buffer_head *blk_bh = NULL;
1770 struct ocfs2_xattr_block *xb;
1773 if (!di->i_xattr_loc)
1776 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1777 le64_to_cpu(di->i_xattr_loc),
1778 &blk_bh, OCFS2_BH_CACHED, inode);
1783 /*Verify the signature of xattr block*/
1784 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1785 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1790 xs->xattr_bh = blk_bh;
1791 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1793 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1794 xs->header = &xb->xb_attrs.xb_header;
1795 xs->base = (void *)xs->header;
1796 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1797 xs->here = xs->header->xh_entries;
1799 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1801 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1805 if (ret && ret != -ENODATA) {
1806 xs->xattr_bh = NULL;
1809 xs->not_found = ret;
1818 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1819 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1822 static int ocfs2_restore_xattr_block(struct inode *inode,
1823 struct ocfs2_xattr_search *xs)
1827 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1828 struct ocfs2_xattr_block *xb =
1829 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1830 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1831 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1833 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1834 le16_to_cpu(el->l_next_free_rec) != 0);
1836 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1837 if (IS_ERR(handle)) {
1838 ret = PTR_ERR(handle);
1843 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1844 OCFS2_JOURNAL_ACCESS_WRITE);
1850 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1851 offsetof(struct ocfs2_xattr_block, xb_attrs));
1853 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1855 ocfs2_journal_dirty(handle, xs->xattr_bh);
1858 ocfs2_commit_trans(osb, handle);
1864 * ocfs2_xattr_block_set()
1866 * Set, replace or remove an extended attribute into external block.
1869 static int ocfs2_xattr_block_set(struct inode *inode,
1870 struct ocfs2_xattr_info *xi,
1871 struct ocfs2_xattr_search *xs)
1873 struct buffer_head *new_bh = NULL;
1874 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1875 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1876 struct ocfs2_alloc_context *meta_ac = NULL;
1877 handle_t *handle = NULL;
1878 struct ocfs2_xattr_block *xblk = NULL;
1879 u16 suballoc_bit_start;
1884 if (!xs->xattr_bh) {
1886 * Alloc one external block for extended attribute
1889 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1894 handle = ocfs2_start_trans(osb,
1895 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1896 if (IS_ERR(handle)) {
1897 ret = PTR_ERR(handle);
1901 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1902 OCFS2_JOURNAL_ACCESS_CREATE);
1908 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1909 &suballoc_bit_start, &num_got,
1916 new_bh = sb_getblk(inode->i_sb, first_blkno);
1917 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1919 ret = ocfs2_journal_access(handle, inode, new_bh,
1920 OCFS2_JOURNAL_ACCESS_CREATE);
1926 /* Initialize ocfs2_xattr_block */
1927 xs->xattr_bh = new_bh;
1928 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1929 memset(xblk, 0, inode->i_sb->s_blocksize);
1930 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1931 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1932 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1933 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1934 xblk->xb_blkno = cpu_to_le64(first_blkno);
1936 xs->header = &xblk->xb_attrs.xb_header;
1937 xs->base = (void *)xs->header;
1938 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1939 xs->here = xs->header->xh_entries;
1942 ret = ocfs2_journal_dirty(handle, new_bh);
1947 di->i_xattr_loc = cpu_to_le64(first_blkno);
1948 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1952 ocfs2_commit_trans(osb, handle);
1955 ocfs2_free_alloc_context(meta_ac);
1959 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1961 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1962 /* Set extended attribute into external block */
1963 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1964 if (!ret || ret != -ENOSPC)
1967 ret = ocfs2_xattr_create_index_block(inode, xs);
1972 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1973 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1974 ret = ocfs2_restore_xattr_block(inode, xs);
1984 * Set, replace or remove an extended attribute for this inode.
1985 * value is NULL to remove an existing extended attribute, else either
1986 * create or replace an extended attribute.
1988 int ocfs2_xattr_set(struct inode *inode,
1995 struct buffer_head *di_bh = NULL;
1996 struct ocfs2_dinode *di;
1998 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2000 struct ocfs2_xattr_info xi = {
2001 .name_index = name_index,
2004 .value_len = value_len,
2007 struct ocfs2_xattr_search xis = {
2008 .not_found = -ENODATA,
2011 struct ocfs2_xattr_search xbs = {
2012 .not_found = -ENODATA,
2015 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2018 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2023 xis.inode_bh = xbs.inode_bh = di_bh;
2024 di = (struct ocfs2_dinode *)di_bh->b_data;
2026 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2028 * Scan inode and external block to find the same name
2029 * extended attribute and collect search infomation.
2031 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2034 if (xis.not_found) {
2035 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2040 if (xis.not_found && xbs.not_found) {
2042 if (flags & XATTR_REPLACE)
2049 if (flags & XATTR_CREATE)
2054 /* Remove existing extended attribute */
2056 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2057 else if (!xbs.not_found)
2058 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2060 /* We always try to set extended attribute into inode first*/
2061 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2062 if (!ret && !xbs.not_found) {
2064 * If succeed and that extended attribute existing in
2065 * external block, then we will remove it.
2069 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2070 } else if (ret == -ENOSPC) {
2071 if (di->i_xattr_loc && !xbs.xattr_bh) {
2072 ret = ocfs2_xattr_block_find(inode, name_index,
2078 * If no space in inode, we will set extended attribute
2079 * into external block.
2081 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2084 if (!xis.not_found) {
2086 * If succeed and that extended attribute
2087 * existing in inode, we will remove it.
2091 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2096 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2097 ocfs2_inode_unlock(inode, 1);
2099 brelse(xbs.xattr_bh);
2100 for (i = 0; i < blk_per_bucket; i++)
2101 brelse(xbs.bucket.bhs[i]);
2107 * Find the xattr extent rec which may contains name_hash.
2108 * e_cpos will be the first name hash of the xattr rec.
2109 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2111 static int ocfs2_xattr_get_rec(struct inode *inode,
2116 struct ocfs2_extent_list *el)
2119 struct buffer_head *eb_bh = NULL;
2120 struct ocfs2_extent_block *eb;
2121 struct ocfs2_extent_rec *rec = NULL;
2124 if (el->l_tree_depth) {
2125 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2131 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2134 if (el->l_tree_depth) {
2135 ocfs2_error(inode->i_sb,
2136 "Inode %lu has non zero tree depth in "
2137 "xattr tree block %llu\n", inode->i_ino,
2138 (unsigned long long)eb_bh->b_blocknr);
2144 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2145 rec = &el->l_recs[i];
2147 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2148 e_blkno = le64_to_cpu(rec->e_blkno);
2154 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2155 "record (%u, %u, 0) in xattr", inode->i_ino,
2156 le32_to_cpu(rec->e_cpos),
2157 ocfs2_rec_clusters(el, rec));
2162 *p_blkno = le64_to_cpu(rec->e_blkno);
2163 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2165 *e_cpos = le32_to_cpu(rec->e_cpos);
2171 typedef int (xattr_bucket_func)(struct inode *inode,
2172 struct ocfs2_xattr_bucket *bucket,
2175 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2176 struct buffer_head *header_bh,
2183 int i, ret = 0, cmp = 1, block_off, new_offset;
2184 struct ocfs2_xattr_header *xh =
2185 (struct ocfs2_xattr_header *)header_bh->b_data;
2186 size_t name_len = strlen(name);
2187 struct ocfs2_xattr_entry *xe = NULL;
2188 struct buffer_head *name_bh = NULL;
2192 * We don't use binary search in the bucket because there
2193 * may be multiple entries with the same name hash.
2195 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2196 xe = &xh->xh_entries[i];
2198 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2200 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2203 cmp = name_index - ocfs2_xattr_get_type(xe);
2205 cmp = name_len - xe->xe_name_len;
2209 ret = ocfs2_xattr_bucket_get_name_value(inode,
2219 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2220 header_bh->b_blocknr + block_off,
2221 &name_bh, OCFS2_BH_CACHED, inode);
2226 xe_name = name_bh->b_data + new_offset;
2228 cmp = memcmp(name, xe_name, name_len);
2244 * Find the specified xattr entry in a series of buckets.
2245 * This series start from p_blkno and last for num_clusters.
2246 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2247 * the num of the valid buckets.
2249 * Return the buffer_head this xattr should reside in. And if the xattr's
2250 * hash is in the gap of 2 buckets, return the lower bucket.
2252 static int ocfs2_xattr_bucket_find(struct inode *inode,
2259 struct ocfs2_xattr_search *xs)
2262 struct buffer_head *bh = NULL;
2263 struct buffer_head *lower_bh = NULL;
2264 struct ocfs2_xattr_header *xh = NULL;
2265 struct ocfs2_xattr_entry *xe = NULL;
2267 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2268 int low_bucket = 0, bucket, high_bucket;
2272 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2273 &bh, OCFS2_BH_CACHED, inode);
2279 xh = (struct ocfs2_xattr_header *)bh->b_data;
2280 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2282 while (low_bucket <= high_bucket) {
2285 bucket = (low_bucket + high_bucket) / 2;
2287 blkno = p_blkno + bucket * blk_per_bucket;
2289 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
2290 &bh, OCFS2_BH_CACHED, inode);
2296 xh = (struct ocfs2_xattr_header *)bh->b_data;
2297 xe = &xh->xh_entries[0];
2298 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2299 high_bucket = bucket - 1;
2304 * Check whether the hash of the last entry in our
2305 * bucket is larger than the search one. for an empty
2306 * bucket, the last one is also the first one.
2309 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2311 last_hash = le32_to_cpu(xe->xe_name_hash);
2313 /* record lower_bh which may be the insert place. */
2318 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2319 low_bucket = bucket + 1;
2323 /* the searched xattr should reside in this bucket if exists. */
2324 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2325 name_index, name, name_hash,
2335 * Record the bucket we have found.
2336 * When the xattr's hash value is in the gap of 2 buckets, we will
2337 * always set it to the previous bucket.
2341 * We can't find any bucket whose first name_hash is less
2342 * than the find name_hash.
2344 BUG_ON(bh->b_blocknr != p_blkno);
2348 xs->bucket.bhs[0] = lower_bh;
2349 xs->bucket.xh = (struct ocfs2_xattr_header *)
2350 xs->bucket.bhs[0]->b_data;
2353 xs->header = xs->bucket.xh;
2354 xs->base = xs->bucket.bhs[0]->b_data;
2355 xs->end = xs->base + inode->i_sb->s_blocksize;
2359 * If we have found the xattr enty, read all the blocks in
2362 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2363 xs->bucket.bhs[0]->b_blocknr + 1,
2364 blk_per_bucket - 1, &xs->bucket.bhs[1],
2365 OCFS2_BH_CACHED, inode);
2371 xs->here = &xs->header->xh_entries[index];
2372 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2373 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2383 static int ocfs2_xattr_index_block_find(struct inode *inode,
2384 struct buffer_head *root_bh,
2387 struct ocfs2_xattr_search *xs)
2390 struct ocfs2_xattr_block *xb =
2391 (struct ocfs2_xattr_block *)root_bh->b_data;
2392 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2393 struct ocfs2_extent_list *el = &xb_root->xt_list;
2395 u32 first_hash, num_clusters = 0;
2396 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2398 if (le16_to_cpu(el->l_next_free_rec) == 0)
2401 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2402 name, name_hash, name_index);
2404 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2411 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2413 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2414 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2416 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2417 p_blkno, first_hash, num_clusters, xs);
2423 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2426 xattr_bucket_func *func,
2430 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2431 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2432 u32 num_buckets = clusters * bpc;
2433 struct ocfs2_xattr_bucket bucket;
2435 memset(&bucket, 0, sizeof(bucket));
2437 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2440 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2441 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2442 blkno, blk_per_bucket,
2443 bucket.bhs, OCFS2_BH_CACHED, inode);
2449 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2451 * The real bucket num in this series of blocks is stored
2452 * in the 1st bucket.
2455 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2457 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2458 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
2460 ret = func(inode, &bucket, para);
2467 for (j = 0; j < blk_per_bucket; j++)
2468 brelse(bucket.bhs[j]);
2469 memset(&bucket, 0, sizeof(bucket));
2473 for (j = 0; j < blk_per_bucket; j++)
2474 brelse(bucket.bhs[j]);
2479 struct ocfs2_xattr_tree_list {
2485 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2486 struct ocfs2_xattr_header *xh,
2493 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2496 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2498 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2499 *new_offset = name_offset % inode->i_sb->s_blocksize;
2504 static int ocfs2_list_xattr_bucket(struct inode *inode,
2505 struct ocfs2_xattr_bucket *bucket,
2509 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2510 int i, block_off, new_offset;
2511 const char *prefix, *name;
2513 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2514 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2515 type = ocfs2_xattr_get_type(entry);
2516 prefix = ocfs2_xattr_prefix(type);
2519 ret = ocfs2_xattr_bucket_get_name_value(inode,
2527 name = (const char *)bucket->bhs[block_off]->b_data +
2529 ret = ocfs2_xattr_list_entry(xl->buffer,
2533 entry->xe_name_len);
2542 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2543 struct ocfs2_xattr_tree_root *xt,
2547 struct ocfs2_extent_list *el = &xt->xt_list;
2549 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2551 struct ocfs2_xattr_tree_list xl = {
2553 .buffer_size = buffer_size,
2557 if (le16_to_cpu(el->l_next_free_rec) == 0)
2560 while (name_hash > 0) {
2561 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2562 &e_cpos, &num_clusters, el);
2568 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2569 ocfs2_list_xattr_bucket,
2579 name_hash = e_cpos - 1;
2587 static int cmp_xe(const void *a, const void *b)
2589 const struct ocfs2_xattr_entry *l = a, *r = b;
2590 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2591 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2593 if (l_hash > r_hash)
2595 if (l_hash < r_hash)
2600 static void swap_xe(void *a, void *b, int size)
2602 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2605 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2606 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2610 * When the ocfs2_xattr_block is filled up, new bucket will be created
2611 * and all the xattr entries will be moved to the new bucket.
2612 * Note: we need to sort the entries since they are not saved in order
2613 * in the ocfs2_xattr_block.
2615 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2616 struct buffer_head *xb_bh,
2617 struct buffer_head *xh_bh,
2618 struct buffer_head *data_bh)
2620 int i, blocksize = inode->i_sb->s_blocksize;
2621 u16 offset, size, off_change;
2622 struct ocfs2_xattr_entry *xe;
2623 struct ocfs2_xattr_block *xb =
2624 (struct ocfs2_xattr_block *)xb_bh->b_data;
2625 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2626 struct ocfs2_xattr_header *xh =
2627 (struct ocfs2_xattr_header *)xh_bh->b_data;
2628 u16 count = le16_to_cpu(xb_xh->xh_count);
2629 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2631 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2632 (unsigned long long)xb_bh->b_blocknr,
2633 (unsigned long long)xh_bh->b_blocknr);
2635 memset(xh_bh->b_data, 0, blocksize);
2637 memset(data_bh->b_data, 0, blocksize);
2639 * Since the xe_name_offset is based on ocfs2_xattr_header,
2640 * there is a offset change corresponding to the change of
2641 * ocfs2_xattr_header's position.
2643 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2644 xe = &xb_xh->xh_entries[count - 1];
2645 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2646 size = blocksize - offset;
2648 /* copy all the names and values. */
2650 target = data_bh->b_data;
2651 memcpy(target + offset, src + offset, size);
2653 /* Init new header now. */
2654 xh->xh_count = xb_xh->xh_count;
2655 xh->xh_num_buckets = cpu_to_le16(1);
2656 xh->xh_name_value_len = cpu_to_le16(size);
2657 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2659 /* copy all the entries. */
2660 target = xh_bh->b_data;
2661 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2662 size = count * sizeof(struct ocfs2_xattr_entry);
2663 memcpy(target + offset, (char *)xb_xh + offset, size);
2665 /* Change the xe offset for all the xe because of the move. */
2666 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2667 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2668 for (i = 0; i < count; i++)
2669 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2671 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2672 offset, size, off_change);
2674 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2679 * After we move xattr from block to index btree, we have to
2680 * update ocfs2_xattr_search to the new xe and base.
2682 * When the entry is in xattr block, xattr_bh indicates the storage place.
2683 * While if the entry is in index b-tree, "bucket" indicates the
2684 * real place of the xattr.
2686 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2687 struct ocfs2_xattr_search *xs,
2688 struct buffer_head *old_bh,
2689 struct buffer_head *new_bh)
2692 char *buf = old_bh->b_data;
2693 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2694 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2695 int i, blocksize = inode->i_sb->s_blocksize;
2696 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2698 xs->bucket.bhs[0] = new_bh;
2700 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2701 xs->header = xs->bucket.xh;
2703 xs->base = new_bh->b_data;
2704 xs->end = xs->base + inode->i_sb->s_blocksize;
2706 if (!xs->not_found) {
2707 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2708 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2709 xs->bucket.bhs[0]->b_blocknr + 1,
2710 blk_per_bucket - 1, &xs->bucket.bhs[1],
2711 OCFS2_BH_CACHED, inode);
2717 i = xs->here - old_xh->xh_entries;
2718 xs->here = &xs->header->xh_entries[i];
2725 static int ocfs2_xattr_create_index_block(struct inode *inode,
2726 struct ocfs2_xattr_search *xs)
2728 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2732 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2733 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2734 struct ocfs2_alloc_context *data_ac;
2735 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2736 struct buffer_head *xb_bh = xs->xattr_bh;
2737 struct ocfs2_xattr_block *xb =
2738 (struct ocfs2_xattr_block *)xb_bh->b_data;
2739 struct ocfs2_xattr_tree_root *xr;
2740 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2741 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2743 mlog(0, "create xattr index block for %llu\n",
2744 (unsigned long long)xb_bh->b_blocknr);
2746 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2748 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2756 * We can use this lock for now, and maybe move to a dedicated mutex
2757 * if performance becomes a problem later.
2759 down_write(&oi->ip_alloc_sem);
2762 * 3 more credits, one for xattr block update, one for the 1st block
2763 * of the new xattr bucket and one for the value/data.
2766 handle = ocfs2_start_trans(osb, credits);
2767 if (IS_ERR(handle)) {
2768 ret = PTR_ERR(handle);
2773 ret = ocfs2_journal_access(handle, inode, xb_bh,
2774 OCFS2_JOURNAL_ACCESS_WRITE);
2780 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2787 * The bucket may spread in many blocks, and
2788 * we will only touch the 1st block and the last block
2789 * in the whole bucket(one for entry and one for data).
2791 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2793 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2795 xh_bh = sb_getblk(inode->i_sb, blkno);
2802 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2804 ret = ocfs2_journal_access(handle, inode, xh_bh,
2805 OCFS2_JOURNAL_ACCESS_CREATE);
2812 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2819 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2821 ret = ocfs2_journal_access(handle, inode, data_bh,
2822 OCFS2_JOURNAL_ACCESS_CREATE);
2829 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2831 ocfs2_journal_dirty(handle, xh_bh);
2833 ocfs2_journal_dirty(handle, data_bh);
2835 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2837 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2838 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2839 offsetof(struct ocfs2_xattr_block, xb_attrs));
2841 xr = &xb->xb_attrs.xb_root;
2842 xr->xt_clusters = cpu_to_le32(1);
2843 xr->xt_last_eb_blk = 0;
2844 xr->xt_list.l_tree_depth = 0;
2845 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2846 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2848 xr->xt_list.l_recs[0].e_cpos = 0;
2849 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2850 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2852 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2854 ret = ocfs2_journal_dirty(handle, xb_bh);
2861 ocfs2_commit_trans(osb, handle);
2864 up_write(&oi->ip_alloc_sem);
2868 ocfs2_free_alloc_context(data_ac);
2876 static int cmp_xe_offset(const void *a, const void *b)
2878 const struct ocfs2_xattr_entry *l = a, *r = b;
2879 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2880 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2882 if (l_name_offset < r_name_offset)
2884 if (l_name_offset > r_name_offset)
2890 * defrag a xattr bucket if we find that the bucket has some
2891 * holes beteen name/value pairs.
2892 * We will move all the name/value pairs to the end of the bucket
2893 * so that we can spare some space for insertion.
2895 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2896 struct ocfs2_xattr_bucket *bucket)
2899 size_t end, offset, len, value_len;
2900 struct ocfs2_xattr_header *xh;
2901 char *entries, *buf, *bucket_buf = NULL;
2902 u64 blkno = bucket->bhs[0]->b_blocknr;
2903 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2905 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2906 size_t blocksize = inode->i_sb->s_blocksize;
2908 struct buffer_head **bhs;
2909 struct ocfs2_xattr_entry *xe;
2911 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2916 ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
2917 OCFS2_BH_CACHED, inode);
2922 * In order to make the operation more efficient and generic,
2923 * we copy all the blocks into a contiguous memory and do the
2924 * defragment there, so if anything is error, we will not touch
2927 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2934 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2935 memcpy(buf, bhs[i]->b_data, blocksize);
2937 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2938 if (IS_ERR(handle)) {
2939 ret = PTR_ERR(handle);
2945 for (i = 0; i < blk_per_bucket; i++) {
2946 ret = ocfs2_journal_access(handle, inode, bhs[i],
2947 OCFS2_JOURNAL_ACCESS_WRITE);
2954 xh = (struct ocfs2_xattr_header *)bucket_buf;
2955 entries = (char *)xh->xh_entries;
2956 xh_free_start = le16_to_cpu(xh->xh_free_start);
2958 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2959 "xh_free_start = %u, xh_name_value_len = %u.\n",
2960 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2961 le16_to_cpu(xh->xh_name_value_len));
2964 * sort all the entries by their offset.
2965 * the largest will be the first, so that we can
2966 * move them to the end one by one.
2968 sort(entries, le16_to_cpu(xh->xh_count),
2969 sizeof(struct ocfs2_xattr_entry),
2970 cmp_xe_offset, swap_xe);
2972 /* Move all name/values to the end of the bucket. */
2973 xe = xh->xh_entries;
2974 end = OCFS2_XATTR_BUCKET_SIZE;
2975 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2976 offset = le16_to_cpu(xe->xe_name_offset);
2977 if (ocfs2_xattr_is_local(xe))
2978 value_len = OCFS2_XATTR_SIZE(
2979 le64_to_cpu(xe->xe_value_size));
2981 value_len = OCFS2_XATTR_ROOT_SIZE;
2982 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2985 * We must make sure that the name/value pair
2986 * exist in the same block. So adjust end to
2987 * the previous block end if needed.
2989 if (((end - len) / blocksize !=
2990 (end - 1) / blocksize))
2991 end = end - end % blocksize;
2993 if (end > offset + len) {
2994 memmove(bucket_buf + end - len,
2995 bucket_buf + offset, len);
2996 xe->xe_name_offset = cpu_to_le16(end - len);
2999 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3000 "bucket %llu\n", (unsigned long long)blkno);
3005 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3006 "bucket %llu\n", (unsigned long long)blkno);
3008 if (xh_free_start == end)
3011 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3012 xh->xh_free_start = cpu_to_le16(end);
3014 /* sort the entries by their name_hash. */
3015 sort(entries, le16_to_cpu(xh->xh_count),
3016 sizeof(struct ocfs2_xattr_entry),
3020 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3021 memcpy(bhs[i]->b_data, buf, blocksize);
3022 ocfs2_journal_dirty(handle, bhs[i]);
3026 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3030 for (i = 0; i < blk_per_bucket; i++)
3040 * Move half nums of the xattr bucket in the previous cluster to this new
3041 * cluster. We only touch the last cluster of the previous extend record.
3043 * first_bh is the first buffer_head of a series of bucket in the same
3044 * extent rec and header_bh is the header of one bucket in this cluster.
3045 * They will be updated if we move the data header_bh contains to the new
3046 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3048 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3050 struct buffer_head **first_bh,
3051 struct buffer_head **header_bh,
3057 int i, ret, credits;
3058 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3059 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3060 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3061 int blocksize = inode->i_sb->s_blocksize;
3062 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3063 struct ocfs2_xattr_header *new_xh;
3064 struct ocfs2_xattr_header *xh =
3065 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3067 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3068 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3070 prev_bh = *first_bh;
3072 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3074 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3076 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3077 prev_blkno, new_blkno);
3080 * We need to update the 1st half of the new cluster and
3081 * 1 more for the update of the 1st bucket of the previous
3084 credits = bpc / 2 + 1;
3085 ret = ocfs2_extend_trans(handle, credits);
3091 ret = ocfs2_journal_access(handle, inode, prev_bh,
3092 OCFS2_JOURNAL_ACCESS_WRITE);
3098 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3099 old_bh = new_bh = NULL;
3100 new_bh = sb_getblk(inode->i_sb, new_blkno);
3107 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3109 ret = ocfs2_journal_access(handle, inode, new_bh,
3110 OCFS2_JOURNAL_ACCESS_CREATE);
3117 ret = ocfs2_read_block(osb, prev_blkno,
3118 &old_bh, OCFS2_BH_CACHED, inode);
3125 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3128 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3129 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3132 *first_hash = le32_to_cpu(
3133 new_xh->xh_entries[0].xe_name_hash);
3134 new_first_bh = new_bh;
3135 get_bh(new_first_bh);
3138 ocfs2_journal_dirty(handle, new_bh);
3140 if (*header_bh == old_bh) {
3142 *header_bh = new_bh;
3146 *first_bh = new_first_bh;
3153 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3155 ocfs2_journal_dirty(handle, prev_bh);
3158 brelse(new_first_bh);
3162 static int ocfs2_read_xattr_bucket(struct inode *inode,
3164 struct buffer_head **bhs,
3168 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3171 return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
3172 blk_per_bucket, bhs,
3173 OCFS2_BH_CACHED, inode);
3175 for (i = 0; i < blk_per_bucket; i++) {
3176 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3177 if (bhs[i] == NULL) {
3182 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3189 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3190 * first_hash will record the 1st hash of the new bucket.
3192 static int ocfs2_half_xattr_bucket(struct inode *inode,
3197 int new_bucket_head)
3200 u16 count, start, len, name_value_len, xe_len, name_offset;
3201 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3202 struct buffer_head **s_bhs, **t_bhs = NULL;
3203 struct ocfs2_xattr_header *xh;
3204 struct ocfs2_xattr_entry *xe;
3205 int blocksize = inode->i_sb->s_blocksize;
3207 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3210 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3214 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3220 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3221 OCFS2_JOURNAL_ACCESS_WRITE);
3227 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3233 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3239 for (i = 0; i < blk_per_bucket; i++) {
3240 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3241 OCFS2_JOURNAL_ACCESS_CREATE);
3248 /* copy the whole bucket to the new first. */
3249 for (i = 0; i < blk_per_bucket; i++)
3250 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3252 /* update the new bucket. */
3253 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3254 count = le16_to_cpu(xh->xh_count);
3258 * Calculate the total name/value len and xh_free_start for
3259 * the old bucket first.
3261 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3263 for (i = 0; i < start; i++) {
3264 xe = &xh->xh_entries[i];
3265 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3266 if (ocfs2_xattr_is_local(xe))
3268 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3270 xe_len += OCFS2_XATTR_ROOT_SIZE;
3271 name_value_len += xe_len;
3272 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3273 name_offset = le16_to_cpu(xe->xe_name_offset);
3277 * Now begin the modification to the new bucket.
3279 * In the new bucket, We just move the xattr entry to the beginning
3280 * and don't touch the name/value. So there will be some holes in the
3281 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3284 xe = &xh->xh_entries[start];
3285 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3286 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3287 (int)((char *)xe - (char *)xh),
3288 (int)((char *)xh->xh_entries - (char *)xh));
3289 memmove((char *)xh->xh_entries, (char *)xe, len);
3290 xe = &xh->xh_entries[count - start];
3291 len = sizeof(struct ocfs2_xattr_entry) * start;
3292 memset((char *)xe, 0, len);
3294 le16_add_cpu(&xh->xh_count, -start);
3295 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3297 /* Calculate xh_free_start for the new bucket. */
3298 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3299 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3300 xe = &xh->xh_entries[i];
3301 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3302 if (ocfs2_xattr_is_local(xe))
3304 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3306 xe_len += OCFS2_XATTR_ROOT_SIZE;
3307 if (le16_to_cpu(xe->xe_name_offset) <
3308 le16_to_cpu(xh->xh_free_start))
3309 xh->xh_free_start = xe->xe_name_offset;
3312 /* set xh->xh_num_buckets for the new xh. */
3313 if (new_bucket_head)
3314 xh->xh_num_buckets = cpu_to_le16(1);
3316 xh->xh_num_buckets = 0;
3318 for (i = 0; i < blk_per_bucket; i++) {
3319 ocfs2_journal_dirty(handle, t_bhs[i]);
3324 /* store the first_hash of the new bucket. */
3326 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3329 * Now only update the 1st block of the old bucket.
3330 * Please note that the entry has been sorted already above.
3332 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3333 memset(&xh->xh_entries[start], 0,
3334 sizeof(struct ocfs2_xattr_entry) * (count - start));
3335 xh->xh_count = cpu_to_le16(start);
3336 xh->xh_free_start = cpu_to_le16(name_offset);
3337 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3339 ocfs2_journal_dirty(handle, s_bhs[0]);
3345 for (i = 0; i < blk_per_bucket; i++)
3351 for (i = 0; i < blk_per_bucket; i++)
3360 * Copy xattr from one bucket to another bucket.
3362 * The caller must make sure that the journal transaction
3363 * has enough space for journaling.
3365 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3372 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3373 int blocksize = inode->i_sb->s_blocksize;
3374 struct buffer_head **s_bhs, **t_bhs = NULL;
3376 BUG_ON(s_blkno == t_blkno);
3378 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3379 s_blkno, t_blkno, t_is_new);
3381 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3386 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3390 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3397 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3401 for (i = 0; i < blk_per_bucket; i++) {
3402 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3403 OCFS2_JOURNAL_ACCESS_WRITE);
3408 for (i = 0; i < blk_per_bucket; i++) {
3409 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3410 ocfs2_journal_dirty(handle, t_bhs[i]);
3415 for (i = 0; i < blk_per_bucket; i++)
3421 for (i = 0; i < blk_per_bucket; i++)
3430 * Copy one xattr cluster from src_blk to to_blk.
3431 * The to_blk will become the first bucket header of the cluster, so its
3432 * xh_num_buckets will be initialized as the bucket num in the cluster.
3434 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3436 struct buffer_head *first_bh,
3441 int i, ret, credits;
3442 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3443 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3444 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3445 struct buffer_head *bh = NULL;
3446 struct ocfs2_xattr_header *xh;
3447 u64 to_blk_start = to_blk;
3449 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3452 * We need to update the new cluster and 1 more for the update of
3453 * the 1st bucket of the previous extent rec.
3456 ret = ocfs2_extend_trans(handle, credits);
3462 ret = ocfs2_journal_access(handle, inode, first_bh,
3463 OCFS2_JOURNAL_ACCESS_WRITE);
3469 for (i = 0; i < num_buckets; i++) {
3470 ret = ocfs2_cp_xattr_bucket(inode, handle,
3471 src_blk, to_blk, 1);
3477 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3478 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3481 /* update the old bucket header. */
3482 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3483 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3485 ocfs2_journal_dirty(handle, first_bh);
3487 /* update the new bucket header. */
3488 ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
3494 ret = ocfs2_journal_access(handle, inode, bh,
3495 OCFS2_JOURNAL_ACCESS_WRITE);
3501 xh = (struct ocfs2_xattr_header *)bh->b_data;
3502 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3504 ocfs2_journal_dirty(handle, bh);
3507 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3514 * Move half of the xattrs in this cluster to the new cluster.
3515 * This function should only be called when bucket size == cluster size.
3516 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3518 static int ocfs2_half_xattr_cluster(struct inode *inode,
3524 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3525 int ret, credits = 2 * blk_per_bucket;
3527 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3529 ret = ocfs2_extend_trans(handle, credits);
3535 /* Move half of the xattr in start_blk to the next bucket. */
3536 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3537 new_blk, first_hash, 1);
3541 * Move some xattrs from the old cluster to the new one since they are not
3542 * contiguous in ocfs2 xattr tree.
3544 * new_blk starts a new separate cluster, and we will move some xattrs from
3545 * prev_blk to it. v_start will be set as the first name hash value in this
3546 * new cluster so that it can be used as e_cpos during tree insertion and
3547 * don't collide with our original b-tree operations. first_bh and header_bh
3548 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3549 * to extend the insert bucket.
3551 * The problem is how much xattr should we move to the new one and when should
3552 * we update first_bh and header_bh?
3553 * 1. If cluster size > bucket size, that means the previous cluster has more
3554 * than 1 bucket, so just move half nums of bucket into the new cluster and
3555 * update the first_bh and header_bh if the insert bucket has been moved
3556 * to the new cluster.
3557 * 2. If cluster_size == bucket_size:
3558 * a) If the previous extent rec has more than one cluster and the insert
3559 * place isn't in the last cluster, copy the entire last cluster to the
3560 * new one. This time, we don't need to upate the first_bh and header_bh
3561 * since they will not be moved into the new cluster.
3562 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3563 * the new one. And we set the extend flag to zero if the insert place is
3564 * moved into the new allocated cluster since no extend is needed.
3566 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3568 struct buffer_head **first_bh,
3569 struct buffer_head **header_bh,
3577 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3579 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3580 prev_blk, prev_clusters, new_blk);
3582 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3583 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3592 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3594 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3595 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3599 ret = ocfs2_half_xattr_cluster(inode, handle,
3603 if ((*header_bh)->b_blocknr == last_blk && extend)
3612 * Add a new cluster for xattr storage.
3614 * If the new cluster is contiguous with the previous one, it will be
3615 * appended to the same extent record, and num_clusters will be updated.
3616 * If not, we will insert a new extent for it and move some xattrs in
3617 * the last cluster into the new allocated one.
3618 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3619 * lose the benefits of hashing because we'll have to search large leaves.
3620 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3623 * first_bh is the first block of the previous extent rec and header_bh
3624 * indicates the bucket we will insert the new xattrs. They will be updated
3625 * when the header_bh is moved into the new cluster.
3627 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3628 struct buffer_head *root_bh,
3629 struct buffer_head **first_bh,
3630 struct buffer_head **header_bh,
3637 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3638 u32 prev_clusters = *num_clusters;
3639 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3641 handle_t *handle = NULL;
3642 struct ocfs2_alloc_context *data_ac = NULL;
3643 struct ocfs2_alloc_context *meta_ac = NULL;
3644 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3645 struct ocfs2_extent_tree et;
3647 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3648 "previous xattr blkno = %llu\n",
3649 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3650 prev_cpos, prev_blkno);
3652 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3654 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3655 &data_ac, &meta_ac);
3661 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3663 handle = ocfs2_start_trans(osb, credits);
3664 if (IS_ERR(handle)) {
3665 ret = PTR_ERR(handle);
3671 ret = ocfs2_journal_access(handle, inode, root_bh,
3672 OCFS2_JOURNAL_ACCESS_WRITE);
3678 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3679 clusters_to_add, &bit_off, &num_bits);
3686 BUG_ON(num_bits > clusters_to_add);
3688 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3689 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3690 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3692 if (prev_blkno + prev_clusters * bpc == block &&
3693 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3694 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3696 * If this cluster is contiguous with the old one and
3697 * adding this new cluster, we don't surpass the limit of
3698 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3699 * initialized and used like other buckets in the previous
3701 * So add it as a contiguous one. The caller will handle
3704 v_start = prev_cpos + prev_clusters;
3705 *num_clusters = prev_clusters + num_bits;
3706 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3709 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3724 if (handle->h_buffer_credits < credits) {
3726 * The journal has been restarted before, and don't
3727 * have enough space for the insertion, so extend it
3730 ret = ocfs2_extend_trans(handle, credits);
3736 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3737 num_bits, block, v_start);
3738 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3739 num_bits, 0, meta_ac);
3745 ret = ocfs2_journal_dirty(handle, root_bh);
3753 ocfs2_commit_trans(osb, handle);
3755 ocfs2_free_alloc_context(data_ac);
3757 ocfs2_free_alloc_context(meta_ac);
3763 * Extend a new xattr bucket and move xattrs to the end one by one until
3764 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3766 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3767 struct buffer_head *first_bh,
3768 struct buffer_head *start_bh,
3772 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3773 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3774 u64 start_blk = start_bh->b_blocknr, end_blk;
3775 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3777 struct ocfs2_xattr_header *first_xh =
3778 (struct ocfs2_xattr_header *)first_bh->b_data;
3779 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3781 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3782 "from %llu, len = %u\n", start_blk,
3783 (unsigned long long)first_bh->b_blocknr, num_clusters);
3785 BUG_ON(bucket >= num_buckets);
3787 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3790 * We will touch all the buckets after the start_bh(include it).
3791 * Add one more bucket and modify the first_bh.
3793 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3794 handle = ocfs2_start_trans(osb, credits);
3795 if (IS_ERR(handle)) {
3796 ret = PTR_ERR(handle);
3802 ret = ocfs2_journal_access(handle, inode, first_bh,
3803 OCFS2_JOURNAL_ACCESS_WRITE);
3809 while (end_blk != start_blk) {
3810 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3811 end_blk + blk_per_bucket, 0);
3814 end_blk -= blk_per_bucket;
3817 /* Move half of the xattr in start_blk to the next bucket. */
3818 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3819 start_blk + blk_per_bucket, NULL, 0);
3821 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3822 ocfs2_journal_dirty(handle, first_bh);
3825 ocfs2_commit_trans(osb, handle);
3831 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3832 * xb_bh is the ocfs2_xattr_block.
3833 * We will move all the buckets starting from header_bh to the next place. As
3834 * for this one, half num of its xattrs will be moved to the next one.
3836 * We will allocate a new cluster if current cluster is full and adjust
3837 * header_bh and first_bh if the insert place is moved to the new cluster.
3839 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3840 struct buffer_head *xb_bh,
3841 struct buffer_head *header_bh)
3843 struct ocfs2_xattr_header *first_xh = NULL;
3844 struct buffer_head *first_bh = NULL;
3845 struct ocfs2_xattr_block *xb =
3846 (struct ocfs2_xattr_block *)xb_bh->b_data;
3847 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3848 struct ocfs2_extent_list *el = &xb_root->xt_list;
3849 struct ocfs2_xattr_header *xh =
3850 (struct ocfs2_xattr_header *)header_bh->b_data;
3851 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3852 struct super_block *sb = inode->i_sb;
3853 struct ocfs2_super *osb = OCFS2_SB(sb);
3854 int ret, num_buckets, extend = 1;
3856 u32 e_cpos, num_clusters;
3858 mlog(0, "Add new xattr bucket starting form %llu\n",
3859 (unsigned long long)header_bh->b_blocknr);
3862 * Add refrence for header_bh here because it may be
3863 * changed in ocfs2_add_new_xattr_cluster and we need
3864 * to free it in the end.
3868 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3875 ret = ocfs2_read_block(osb, p_blkno,
3876 &first_bh, OCFS2_BH_CACHED, inode);
3882 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3883 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3885 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3886 ret = ocfs2_add_new_xattr_cluster(inode,
3901 ret = ocfs2_extend_xattr_bucket(inode,
3913 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3914 struct ocfs2_xattr_bucket *bucket,
3917 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3919 offs = offs % inode->i_sb->s_blocksize;
3920 return bucket->bhs[block_off]->b_data + offs;
3924 * Handle the normal xattr set, including replace, delete and new.
3926 * Note: "local" indicates the real data's locality. So we can't
3927 * just its bucket locality by its length.
3929 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3930 struct ocfs2_xattr_info *xi,
3931 struct ocfs2_xattr_search *xs,
3935 struct ocfs2_xattr_entry *last, *xe;
3936 int name_len = strlen(xi->name);
3937 struct ocfs2_xattr_header *xh = xs->header;
3938 u16 count = le16_to_cpu(xh->xh_count), start;
3939 size_t blocksize = inode->i_sb->s_blocksize;
3941 size_t offs, size, new_size;
3943 last = &xh->xh_entries[count];
3944 if (!xs->not_found) {
3946 offs = le16_to_cpu(xe->xe_name_offset);
3947 if (ocfs2_xattr_is_local(xe))
3948 size = OCFS2_XATTR_SIZE(name_len) +
3949 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3951 size = OCFS2_XATTR_SIZE(name_len) +
3952 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3955 * If the new value will be stored outside, xi->value has been
3956 * initalized as an empty ocfs2_xattr_value_root, and the same
3957 * goes with xi->value_len, so we can set new_size safely here.
3958 * See ocfs2_xattr_set_in_bucket.
3960 new_size = OCFS2_XATTR_SIZE(name_len) +
3961 OCFS2_XATTR_SIZE(xi->value_len);
3963 le16_add_cpu(&xh->xh_name_value_len, -size);
3965 if (new_size > size)
3966 goto set_new_name_value;
3968 /* Now replace the old value with new one. */
3970 xe->xe_value_size = cpu_to_le64(xi->value_len);
3972 xe->xe_value_size = 0;
3974 val = ocfs2_xattr_bucket_get_val(inode,
3976 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3977 size - OCFS2_XATTR_SIZE(name_len));
3978 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3979 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3980 xi->value, xi->value_len);
3982 le16_add_cpu(&xh->xh_name_value_len, new_size);
3983 ocfs2_xattr_set_local(xe, local);
3987 * Remove the old entry if there is more than one.
3988 * We don't remove the last entry so that we can
3989 * use it to indicate the hash value of the empty
3993 le16_add_cpu(&xh->xh_count, -1);
3996 (void *)last - (void *)xe);
3998 sizeof(struct ocfs2_xattr_entry));
4001 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4006 /* find a new entry for insert. */
4007 int low = 0, high = count - 1, tmp;
4008 struct ocfs2_xattr_entry *tmp_xe;
4010 while (low <= high && count) {
4011 tmp = (low + high) / 2;
4012 tmp_xe = &xh->xh_entries[tmp];
4014 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4016 else if (name_hash <
4017 le32_to_cpu(tmp_xe->xe_name_hash))
4025 xe = &xh->xh_entries[low];
4027 memmove(xe + 1, xe, (void *)last - (void *)xe);
4029 le16_add_cpu(&xh->xh_count, 1);
4030 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4031 xe->xe_name_hash = cpu_to_le32(name_hash);
4032 xe->xe_name_len = name_len;
4033 ocfs2_xattr_set_type(xe, xi->name_index);
4037 /* Insert the new name+value. */
4038 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4041 * We must make sure that the name/value pair
4042 * exists in the same block.
4044 offs = le16_to_cpu(xh->xh_free_start);
4045 start = offs - size;
4047 if (start >> inode->i_sb->s_blocksize_bits !=
4048 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4049 offs = offs - offs % blocksize;
4050 xh->xh_free_start = cpu_to_le16(offs);
4053 val = ocfs2_xattr_bucket_get_val(inode,
4054 &xs->bucket, offs - size);
4055 xe->xe_name_offset = cpu_to_le16(offs - size);
4057 memset(val, 0, size);
4058 memcpy(val, xi->name, name_len);
4059 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4061 xe->xe_value_size = cpu_to_le64(xi->value_len);
4062 ocfs2_xattr_set_local(xe, local);
4064 le16_add_cpu(&xh->xh_free_start, -size);
4065 le16_add_cpu(&xh->xh_name_value_len, size);
4070 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4072 struct ocfs2_xattr_search *xs,
4073 struct buffer_head **bhs,
4076 int ret = 0, off, block_off;
4077 struct ocfs2_xattr_entry *xe = xs->here;
4080 * First calculate all the blocks we should journal_access
4081 * and journal_dirty. The first block should always be touched.
4083 ret = ocfs2_journal_dirty(handle, bhs[0]);
4087 /* calc the data. */
4088 off = le16_to_cpu(xe->xe_name_offset);
4089 block_off = off >> inode->i_sb->s_blocksize_bits;
4090 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4098 * Set the xattr entry in the specified bucket.
4099 * The bucket is indicated by xs->bucket and it should have the enough
4100 * space for the xattr insertion.
4102 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4103 struct ocfs2_xattr_info *xi,
4104 struct ocfs2_xattr_search *xs,
4109 handle_t *handle = NULL;
4110 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4111 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4113 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4114 (unsigned long)xi->value_len, xi->name_index,
4115 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4117 if (!xs->bucket.bhs[1]) {
4118 ret = ocfs2_read_blocks(osb,
4119 xs->bucket.bhs[0]->b_blocknr + 1,
4120 blk_per_bucket - 1, &xs->bucket.bhs[1],
4121 OCFS2_BH_CACHED, inode);
4128 handle = ocfs2_start_trans(osb, blk_per_bucket);
4129 if (IS_ERR(handle)) {
4130 ret = PTR_ERR(handle);
4136 for (i = 0; i < blk_per_bucket; i++) {
4137 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4138 OCFS2_JOURNAL_ACCESS_WRITE);
4145 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4147 /*Only dirty the blocks we have touched in set xattr. */
4148 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4149 xs->bucket.bhs, blk_per_bucket);
4153 ocfs2_commit_trans(osb, handle);
4158 static int ocfs2_xattr_value_update_size(struct inode *inode,
4159 struct buffer_head *xe_bh,
4160 struct ocfs2_xattr_entry *xe,
4164 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4165 handle_t *handle = NULL;
4167 handle = ocfs2_start_trans(osb, 1);
4168 if (handle == NULL) {
4174 ret = ocfs2_journal_access(handle, inode, xe_bh,
4175 OCFS2_JOURNAL_ACCESS_WRITE);
4181 xe->xe_value_size = cpu_to_le64(new_size);
4183 ret = ocfs2_journal_dirty(handle, xe_bh);
4188 ocfs2_commit_trans(osb, handle);
4194 * Truncate the specified xe_off entry in xattr bucket.
4195 * bucket is indicated by header_bh and len is the new length.
4196 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4198 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4200 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4201 struct buffer_head *header_bh,
4207 struct buffer_head *value_bh = NULL;
4208 struct ocfs2_xattr_value_root *xv;
4209 struct ocfs2_xattr_entry *xe;
4210 struct ocfs2_xattr_header *xh =
4211 (struct ocfs2_xattr_header *)header_bh->b_data;
4212 size_t blocksize = inode->i_sb->s_blocksize;
4214 xe = &xh->xh_entries[xe_off];
4216 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4218 offset = le16_to_cpu(xe->xe_name_offset) +
4219 OCFS2_XATTR_SIZE(xe->xe_name_len);
4221 value_blk = offset / blocksize;
4223 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4224 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4225 value_blk += header_bh->b_blocknr;
4227 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
4228 &value_bh, OCFS2_BH_CACHED, inode);
4234 xv = (struct ocfs2_xattr_value_root *)
4235 (value_bh->b_data + offset % blocksize);
4237 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4238 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4239 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4245 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4256 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4257 struct ocfs2_xattr_search *xs,
4261 struct ocfs2_xattr_entry *xe = xs->here;
4262 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4264 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4266 offset = xe - xh->xh_entries;
4267 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4275 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4276 struct ocfs2_xattr_search *xs,
4281 struct ocfs2_xattr_value_root *xv;
4282 struct ocfs2_xattr_entry *xe = xs->here;
4284 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4286 offset = le16_to_cpu(xe->xe_name_offset) +
4287 OCFS2_XATTR_SIZE(xe->xe_name_len);
4289 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4291 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4294 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4295 struct buffer_head *root_bh,
4301 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4302 struct inode *tl_inode = osb->osb_tl_inode;
4304 struct ocfs2_xattr_block *xb =
4305 (struct ocfs2_xattr_block *)root_bh->b_data;
4306 struct ocfs2_alloc_context *meta_ac = NULL;
4307 struct ocfs2_cached_dealloc_ctxt dealloc;
4308 struct ocfs2_extent_tree et;
4310 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4312 ocfs2_init_dealloc_ctxt(&dealloc);
4314 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4315 cpos, len, (unsigned long long)blkno);
4317 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4319 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4325 mutex_lock(&tl_inode->i_mutex);
4327 if (ocfs2_truncate_log_needs_flush(osb)) {
4328 ret = __ocfs2_flush_truncate_log(osb);
4335 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4336 if (handle == NULL) {
4342 ret = ocfs2_journal_access(handle, inode, root_bh,
4343 OCFS2_JOURNAL_ACCESS_WRITE);
4349 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4356 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4358 ret = ocfs2_journal_dirty(handle, root_bh);
4364 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4369 ocfs2_commit_trans(osb, handle);
4371 ocfs2_schedule_truncate_log_flush(osb, 1);
4373 mutex_unlock(&tl_inode->i_mutex);
4376 ocfs2_free_alloc_context(meta_ac);
4378 ocfs2_run_deallocs(osb, &dealloc);
4383 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4384 struct ocfs2_xattr_search *xs)
4386 handle_t *handle = NULL;
4387 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4388 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4389 le16_to_cpu(xh->xh_count) - 1];
4392 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4393 if (IS_ERR(handle)) {
4394 ret = PTR_ERR(handle);
4399 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4400 OCFS2_JOURNAL_ACCESS_WRITE);
4406 /* Remove the old entry. */
4407 memmove(xs->here, xs->here + 1,
4408 (void *)last - (void *)xs->here);
4409 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4410 le16_add_cpu(&xh->xh_count, -1);
4412 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4416 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4420 * Set the xattr name/value in the bucket specified in xs.
4422 * As the new value in xi may be stored in the bucket or in an outside cluster,
4423 * we divide the whole process into 3 steps:
4424 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4425 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4426 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4427 * 4. If the clusters for the new outside value can't be allocated, we need
4428 * to free the xattr we allocated in set.
4430 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4431 struct ocfs2_xattr_info *xi,
4432 struct ocfs2_xattr_search *xs)
4436 char *val = (char *)xi->value;
4437 struct ocfs2_xattr_entry *xe = xs->here;
4438 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4441 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4443 * We need to truncate the xattr storage first.
4445 * If both the old and new value are stored to
4446 * outside block, we only need to truncate
4447 * the storage and then set the value outside.
4449 * If the new value should be stored within block,
4450 * we should free all the outside block first and
4451 * the modification to the xattr block will be done
4452 * by following steps.
4454 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4455 value_len = xi->value_len;
4459 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4465 goto set_value_outside;
4468 value_len = xi->value_len;
4469 /* So we have to handle the inside block change now. */
4470 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4472 * If the new value will be stored outside of block,
4473 * initalize a new empty value root and insert it first.
4476 xi->value = &def_xv;
4477 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4480 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4486 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4489 /* allocate the space now for the outside block storage. */
4490 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4495 if (xs->not_found) {
4497 * We can't allocate enough clusters for outside
4498 * storage and we have allocated xattr already,
4499 * so need to remove it.
4501 ocfs2_xattr_bucket_remove_xs(inode, xs);
4507 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4512 /* check whether the xattr bucket is filled up with the same hash value. */
4513 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4514 struct ocfs2_xattr_bucket *bucket)
4516 struct ocfs2_xattr_header *xh = bucket->xh;
4518 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4519 xh->xh_entries[0].xe_name_hash) {
4520 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4522 (unsigned long long)bucket->bhs[0]->b_blocknr,
4523 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4530 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4531 struct ocfs2_xattr_info *xi,
4532 struct ocfs2_xattr_search *xs)
4534 struct ocfs2_xattr_header *xh;
4535 struct ocfs2_xattr_entry *xe;
4536 u16 count, header_size, xh_free_start;
4537 int i, free, max_free, need, old;
4538 size_t value_size = 0, name_len = strlen(xi->name);
4539 size_t blocksize = inode->i_sb->s_blocksize;
4540 int ret, allocation = 0;
4541 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4543 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4547 count = le16_to_cpu(xh->xh_count);
4548 xh_free_start = le16_to_cpu(xh->xh_free_start);
4549 header_size = sizeof(struct ocfs2_xattr_header) +
4550 count * sizeof(struct ocfs2_xattr_entry);
4551 max_free = OCFS2_XATTR_BUCKET_SIZE -
4552 le16_to_cpu(xh->xh_name_value_len) - header_size;
4554 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4555 "of %u which exceed block size\n",
4556 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4559 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4560 value_size = OCFS2_XATTR_ROOT_SIZE;
4562 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4565 need = sizeof(struct ocfs2_xattr_entry) +
4566 OCFS2_XATTR_SIZE(name_len) + value_size;
4568 need = value_size + OCFS2_XATTR_SIZE(name_len);
4571 * We only replace the old value if the new length is smaller
4572 * than the old one. Otherwise we will allocate new space in the
4573 * bucket to store it.
4576 if (ocfs2_xattr_is_local(xe))
4577 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4579 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4581 if (old >= value_size)
4585 free = xh_free_start - header_size;
4587 * We need to make sure the new name/value pair
4588 * can exist in the same block.
4590 if (xh_free_start % blocksize < need)
4591 free -= xh_free_start % blocksize;
4593 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4594 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4595 " %u\n", xs->not_found,
4596 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4597 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4598 le16_to_cpu(xh->xh_name_value_len));
4600 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4601 if (need <= max_free &&
4602 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4604 * We can create the space by defragment. Since only the
4605 * name/value will be moved, the xe shouldn't be changed
4608 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4614 xh_free_start = le16_to_cpu(xh->xh_free_start);
4615 free = xh_free_start - header_size;
4616 if (xh_free_start % blocksize < need)
4617 free -= xh_free_start % blocksize;
4622 mlog(0, "Can't get enough space for xattr insert by "
4623 "defragment. Need %u bytes, but we have %d, so "
4624 "allocate new bucket for it.\n", need, free);
4628 * We have to add new buckets or clusters and one
4629 * allocation should leave us enough space for insert.
4634 * We do not allow for overlapping ranges between buckets. And
4635 * the maximum number of collisions we will allow for then is
4636 * one bucket's worth, so check it here whether we need to
4637 * add a new bucket for the insert.
4639 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4645 ret = ocfs2_add_new_xattr_bucket(inode,
4653 for (i = 0; i < blk_per_bucket; i++)
4654 brelse(xs->bucket.bhs[i]);
4656 memset(&xs->bucket, 0, sizeof(xs->bucket));
4658 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4661 if (ret && ret != -ENODATA)
4663 xs->not_found = ret;
4669 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4675 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4676 struct ocfs2_xattr_bucket *bucket,
4680 struct ocfs2_xattr_header *xh = bucket->xh;
4682 struct ocfs2_xattr_entry *xe;
4684 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4685 xe = &xh->xh_entries[i];
4686 if (ocfs2_xattr_is_local(xe))
4689 ret = ocfs2_xattr_bucket_value_truncate(inode,
4701 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4702 struct buffer_head *xb_bh)
4704 struct ocfs2_xattr_block *xb =
4705 (struct ocfs2_xattr_block *)xb_bh->b_data;
4706 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4708 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4711 if (le16_to_cpu(el->l_next_free_rec) == 0)
4714 while (name_hash > 0) {
4715 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4716 &e_cpos, &num_clusters, el);
4722 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4723 ocfs2_delete_xattr_in_bucket,
4730 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4731 p_blkno, e_cpos, num_clusters);
4740 name_hash = e_cpos - 1;
4748 * 'trusted' attributes support
4751 #define XATTR_TRUSTED_PREFIX "trusted."
4753 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4754 size_t list_size, const char *name,
4757 const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1;
4758 const size_t total_len = prefix_len + name_len + 1;
4760 if (list && total_len <= list_size) {
4761 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4762 memcpy(list + prefix_len, name, name_len);
4763 list[prefix_len + name_len] = '\0';
4768 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4769 void *buffer, size_t size)
4771 if (strcmp(name, "") == 0)
4773 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4777 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4778 const void *value, size_t size, int flags)
4780 if (strcmp(name, "") == 0)
4783 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4787 struct xattr_handler ocfs2_xattr_trusted_handler = {
4788 .prefix = XATTR_TRUSTED_PREFIX,
4789 .list = ocfs2_xattr_trusted_list,
4790 .get = ocfs2_xattr_trusted_get,
4791 .set = ocfs2_xattr_trusted_set,
4796 * 'user' attributes support
4799 #define XATTR_USER_PREFIX "user."
4801 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4802 size_t list_size, const char *name,
4805 const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1;
4806 const size_t total_len = prefix_len + name_len + 1;
4807 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4809 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4812 if (list && total_len <= list_size) {
4813 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4814 memcpy(list + prefix_len, name, name_len);
4815 list[prefix_len + name_len] = '\0';
4820 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4821 void *buffer, size_t size)
4823 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4825 if (strcmp(name, "") == 0)
4827 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4829 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4833 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4834 const void *value, size_t size, int flags)
4836 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4838 if (strcmp(name, "") == 0)
4840 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4843 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4847 struct xattr_handler ocfs2_xattr_user_handler = {
4848 .prefix = XATTR_USER_PREFIX,
4849 .list = ocfs2_xattr_user_list,
4850 .get = ocfs2_xattr_user_get,
4851 .set = ocfs2_xattr_user_set,