1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
39 #define MLOG_MASK_PREFIX ML_XATTR
40 #include <cluster/masklog.h>
53 #include "buffer_head_io.h"
58 struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
63 struct ocfs2_xattr_bucket {
64 /* The inode these xattrs are associated with */
65 struct inode *bu_inode;
67 /* The actual buffers that make up the bucket */
68 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
70 /* How many blocks make up one bucket for this filesystem */
74 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
75 #define OCFS2_XATTR_INLINE_SIZE 80
77 static struct ocfs2_xattr_def_value_root def_xv = {
78 .xv.xr_list.l_count = cpu_to_le16(1),
81 struct xattr_handler *ocfs2_xattr_handlers[] = {
82 &ocfs2_xattr_user_handler,
83 &ocfs2_xattr_trusted_handler,
87 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
88 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
89 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
92 struct ocfs2_xattr_info {
99 struct ocfs2_xattr_search {
100 struct buffer_head *inode_bh;
102 * xattr_bh point to the block buffer head which has extended attribute
103 * when extended attribute in inode, xattr_bh is equal to inode_bh.
105 struct buffer_head *xattr_bh;
106 struct ocfs2_xattr_header *header;
107 struct ocfs2_xattr_bucket *bucket;
110 struct ocfs2_xattr_entry *here;
114 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
115 struct ocfs2_xattr_header *xh,
120 static int ocfs2_xattr_block_find(struct inode *inode,
123 struct ocfs2_xattr_search *xs);
124 static int ocfs2_xattr_index_block_find(struct inode *inode,
125 struct buffer_head *root_bh,
128 struct ocfs2_xattr_search *xs);
130 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
131 struct ocfs2_xattr_tree_root *xt,
135 static int ocfs2_xattr_create_index_block(struct inode *inode,
136 struct ocfs2_xattr_search *xs);
138 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
139 struct ocfs2_xattr_info *xi,
140 struct ocfs2_xattr_search *xs);
142 static int ocfs2_delete_xattr_index_block(struct inode *inode,
143 struct buffer_head *xb_bh);
145 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
147 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
150 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
152 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
155 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
157 u16 len = sb->s_blocksize -
158 offsetof(struct ocfs2_xattr_header, xh_entries);
160 return len / sizeof(struct ocfs2_xattr_entry);
163 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
164 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
165 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
167 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
169 struct ocfs2_xattr_bucket *bucket;
170 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
172 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
174 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
176 bucket->bu_inode = inode;
177 bucket->bu_blocks = blks;
183 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
187 for (i = 0; i < bucket->bu_blocks; i++) {
188 brelse(bucket->bu_bhs[i]);
189 bucket->bu_bhs[i] = NULL;
193 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
196 ocfs2_xattr_bucket_relse(bucket);
197 bucket->bu_inode = NULL;
203 * A bucket that has never been written to disk doesn't need to be
204 * read. We just need the buffer_heads. Don't call this for
205 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
208 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
213 for (i = 0; i < bucket->bu_blocks; i++) {
214 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
216 if (!bucket->bu_bhs[i]) {
222 if (!ocfs2_buffer_uptodate(bucket->bu_inode,
224 ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
229 ocfs2_xattr_bucket_relse(bucket);
233 /* Read the xattr bucket at xb_blkno */
234 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
239 rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
240 bucket->bu_blocks, bucket->bu_bhs, 0);
242 ocfs2_xattr_bucket_relse(bucket);
246 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
247 struct ocfs2_xattr_bucket *bucket,
252 for (i = 0; i < bucket->bu_blocks; i++) {
253 rc = ocfs2_journal_access(handle, bucket->bu_inode,
254 bucket->bu_bhs[i], type);
264 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
265 struct ocfs2_xattr_bucket *bucket)
269 for (i = 0; i < bucket->bu_blocks; i++)
270 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
273 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
274 struct ocfs2_xattr_bucket *src)
277 int blocksize = src->bu_inode->i_sb->s_blocksize;
279 BUG_ON(dest->bu_blocks != src->bu_blocks);
280 BUG_ON(dest->bu_inode != src->bu_inode);
282 for (i = 0; i < src->bu_blocks; i++) {
283 memcpy(bucket_block(dest, i), bucket_block(src, i),
288 static inline const char *ocfs2_xattr_prefix(int name_index)
290 struct xattr_handler *handler = NULL;
292 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
293 handler = ocfs2_xattr_handler_map[name_index];
295 return handler ? handler->prefix : NULL;
298 static u32 ocfs2_xattr_name_hash(struct inode *inode,
302 /* Get hash value of uuid from super block */
303 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
306 /* hash extended attribute name */
307 for (i = 0; i < name_len; i++) {
308 hash = (hash << OCFS2_HASH_SHIFT) ^
309 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
317 * ocfs2_xattr_hash_entry()
319 * Compute the hash of an extended attribute.
321 static void ocfs2_xattr_hash_entry(struct inode *inode,
322 struct ocfs2_xattr_header *header,
323 struct ocfs2_xattr_entry *entry)
326 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
328 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
329 entry->xe_name_hash = cpu_to_le32(hash);
334 static int ocfs2_xattr_extend_allocation(struct inode *inode,
336 struct buffer_head *xattr_bh,
337 struct ocfs2_xattr_value_root *xv)
340 int restart_func = 0;
342 handle_t *handle = NULL;
343 struct ocfs2_alloc_context *data_ac = NULL;
344 struct ocfs2_alloc_context *meta_ac = NULL;
345 enum ocfs2_alloc_restarted why;
346 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
347 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
348 struct ocfs2_extent_tree et;
350 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
352 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
356 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
363 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
365 handle = ocfs2_start_trans(osb, credits);
366 if (IS_ERR(handle)) {
367 status = PTR_ERR(handle);
373 restarted_transaction:
374 status = ocfs2_journal_access(handle, inode, xattr_bh,
375 OCFS2_JOURNAL_ACCESS_WRITE);
381 prev_clusters = le32_to_cpu(xv->xr_clusters);
382 status = ocfs2_add_clusters_in_btree(osb,
392 if ((status < 0) && (status != -EAGAIN)) {
393 if (status != -ENOSPC)
398 status = ocfs2_journal_dirty(handle, xattr_bh);
404 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
406 if (why != RESTART_NONE && clusters_to_add) {
407 if (why == RESTART_META) {
408 mlog(0, "restarting function.\n");
411 BUG_ON(why != RESTART_TRANS);
413 mlog(0, "restarting transaction.\n");
414 /* TODO: This can be more intelligent. */
415 credits = ocfs2_calc_extend_credits(osb->sb,
418 status = ocfs2_extend_trans(handle, credits);
420 /* handle still has to be committed at
426 goto restarted_transaction;
432 ocfs2_commit_trans(osb, handle);
436 ocfs2_free_alloc_context(data_ac);
440 ocfs2_free_alloc_context(meta_ac);
443 if ((!status) && restart_func) {
451 static int __ocfs2_remove_xattr_range(struct inode *inode,
452 struct buffer_head *root_bh,
453 struct ocfs2_xattr_value_root *xv,
454 u32 cpos, u32 phys_cpos, u32 len,
455 struct ocfs2_cached_dealloc_ctxt *dealloc)
458 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
459 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
460 struct inode *tl_inode = osb->osb_tl_inode;
462 struct ocfs2_alloc_context *meta_ac = NULL;
463 struct ocfs2_extent_tree et;
465 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
467 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
473 mutex_lock(&tl_inode->i_mutex);
475 if (ocfs2_truncate_log_needs_flush(osb)) {
476 ret = __ocfs2_flush_truncate_log(osb);
483 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
484 if (IS_ERR(handle)) {
485 ret = PTR_ERR(handle);
490 ret = ocfs2_journal_access(handle, inode, root_bh,
491 OCFS2_JOURNAL_ACCESS_WRITE);
497 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
504 le32_add_cpu(&xv->xr_clusters, -len);
506 ret = ocfs2_journal_dirty(handle, root_bh);
512 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
517 ocfs2_commit_trans(osb, handle);
519 mutex_unlock(&tl_inode->i_mutex);
522 ocfs2_free_alloc_context(meta_ac);
527 static int ocfs2_xattr_shrink_size(struct inode *inode,
530 struct buffer_head *root_bh,
531 struct ocfs2_xattr_value_root *xv)
534 u32 trunc_len, cpos, phys_cpos, alloc_size;
536 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
537 struct ocfs2_cached_dealloc_ctxt dealloc;
539 ocfs2_init_dealloc_ctxt(&dealloc);
541 if (old_clusters <= new_clusters)
545 trunc_len = old_clusters - new_clusters;
547 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
548 &alloc_size, &xv->xr_list);
554 if (alloc_size > trunc_len)
555 alloc_size = trunc_len;
557 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
558 phys_cpos, alloc_size,
565 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
566 ocfs2_remove_xattr_clusters_from_cache(inode, block,
569 trunc_len -= alloc_size;
573 ocfs2_schedule_truncate_log_flush(osb, 1);
574 ocfs2_run_deallocs(osb, &dealloc);
579 static int ocfs2_xattr_value_truncate(struct inode *inode,
580 struct buffer_head *root_bh,
581 struct ocfs2_xattr_value_root *xv,
585 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
586 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
588 if (new_clusters == old_clusters)
591 if (new_clusters > old_clusters)
592 ret = ocfs2_xattr_extend_allocation(inode,
593 new_clusters - old_clusters,
596 ret = ocfs2_xattr_shrink_size(inode,
597 old_clusters, new_clusters,
603 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
604 size_t *result, const char *prefix,
605 const char *name, int name_len)
607 char *p = buffer + *result;
608 int prefix_len = strlen(prefix);
609 int total_len = prefix_len + name_len + 1;
611 *result += total_len;
613 /* we are just looking for how big our buffer needs to be */
620 memcpy(p, prefix, prefix_len);
621 memcpy(p + prefix_len, name, name_len);
622 p[prefix_len + name_len] = '\0';
627 static int ocfs2_xattr_list_entries(struct inode *inode,
628 struct ocfs2_xattr_header *header,
629 char *buffer, size_t buffer_size)
633 const char *prefix, *name;
635 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
636 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
637 type = ocfs2_xattr_get_type(entry);
638 prefix = ocfs2_xattr_prefix(type);
641 name = (const char *)header +
642 le16_to_cpu(entry->xe_name_offset);
644 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
645 &result, prefix, name,
655 static int ocfs2_xattr_ibody_list(struct inode *inode,
656 struct ocfs2_dinode *di,
660 struct ocfs2_xattr_header *header = NULL;
661 struct ocfs2_inode_info *oi = OCFS2_I(inode);
664 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
667 header = (struct ocfs2_xattr_header *)
668 ((void *)di + inode->i_sb->s_blocksize -
669 le16_to_cpu(di->i_xattr_inline_size));
671 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
676 static int ocfs2_xattr_block_list(struct inode *inode,
677 struct ocfs2_dinode *di,
681 struct buffer_head *blk_bh = NULL;
682 struct ocfs2_xattr_block *xb;
685 if (!di->i_xattr_loc)
688 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
694 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
695 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
700 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
701 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
702 ret = ocfs2_xattr_list_entries(inode, header,
703 buffer, buffer_size);
705 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
706 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
707 buffer, buffer_size);
715 ssize_t ocfs2_listxattr(struct dentry *dentry,
719 int ret = 0, i_ret = 0, b_ret = 0;
720 struct buffer_head *di_bh = NULL;
721 struct ocfs2_dinode *di = NULL;
722 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
724 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
727 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
730 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
736 di = (struct ocfs2_dinode *)di_bh->b_data;
738 down_read(&oi->ip_xattr_sem);
739 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
747 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
752 up_read(&oi->ip_xattr_sem);
753 ocfs2_inode_unlock(dentry->d_inode, 0);
757 return i_ret + b_ret;
760 static int ocfs2_xattr_find_entry(int name_index,
762 struct ocfs2_xattr_search *xs)
764 struct ocfs2_xattr_entry *entry;
771 name_len = strlen(name);
773 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
774 cmp = name_index - ocfs2_xattr_get_type(entry);
776 cmp = name_len - entry->xe_name_len;
778 cmp = memcmp(name, (xs->base +
779 le16_to_cpu(entry->xe_name_offset)),
787 return cmp ? -ENODATA : 0;
790 static int ocfs2_xattr_get_value_outside(struct inode *inode,
791 struct ocfs2_xattr_value_root *xv,
795 u32 cpos, p_cluster, num_clusters, bpc, clusters;
798 size_t cplen, blocksize;
799 struct buffer_head *bh = NULL;
800 struct ocfs2_extent_list *el;
803 clusters = le32_to_cpu(xv->xr_clusters);
804 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
805 blocksize = inode->i_sb->s_blocksize;
808 while (cpos < clusters) {
809 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
816 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
817 /* Copy ocfs2_xattr_value */
818 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
819 ret = ocfs2_read_block(inode, blkno, &bh);
825 cplen = len >= blocksize ? blocksize : len;
826 memcpy(buffer, bh->b_data, cplen);
835 cpos += num_clusters;
841 static int ocfs2_xattr_ibody_get(struct inode *inode,
846 struct ocfs2_xattr_search *xs)
848 struct ocfs2_inode_info *oi = OCFS2_I(inode);
849 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
850 struct ocfs2_xattr_value_root *xv;
854 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
857 xs->end = (void *)di + inode->i_sb->s_blocksize;
858 xs->header = (struct ocfs2_xattr_header *)
859 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
860 xs->base = (void *)xs->header;
861 xs->here = xs->header->xh_entries;
863 ret = ocfs2_xattr_find_entry(name_index, name, xs);
866 size = le64_to_cpu(xs->here->xe_value_size);
868 if (size > buffer_size)
870 if (ocfs2_xattr_is_local(xs->here)) {
871 memcpy(buffer, (void *)xs->base +
872 le16_to_cpu(xs->here->xe_name_offset) +
873 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
875 xv = (struct ocfs2_xattr_value_root *)
876 (xs->base + le16_to_cpu(
877 xs->here->xe_name_offset) +
878 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
879 ret = ocfs2_xattr_get_value_outside(inode, xv,
891 static int ocfs2_xattr_block_get(struct inode *inode,
896 struct ocfs2_xattr_search *xs)
898 struct ocfs2_xattr_block *xb;
899 struct ocfs2_xattr_value_root *xv;
901 int ret = -ENODATA, name_offset, name_len, block_off, i;
903 xs->bucket = ocfs2_xattr_bucket_new(inode);
910 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
921 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
922 size = le64_to_cpu(xs->here->xe_value_size);
925 if (size > buffer_size)
928 name_offset = le16_to_cpu(xs->here->xe_name_offset);
929 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
930 i = xs->here - xs->header->xh_entries;
932 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
933 ret = ocfs2_xattr_bucket_get_name_value(inode,
934 bucket_xh(xs->bucket),
938 xs->base = bucket_block(xs->bucket, block_off);
940 if (ocfs2_xattr_is_local(xs->here)) {
941 memcpy(buffer, (void *)xs->base +
942 name_offset + name_len, size);
944 xv = (struct ocfs2_xattr_value_root *)
945 (xs->base + name_offset + name_len);
946 ret = ocfs2_xattr_get_value_outside(inode, xv,
956 ocfs2_xattr_bucket_free(xs->bucket);
958 brelse(xs->xattr_bh);
965 * Copy an extended attribute into the buffer provided.
966 * Buffer is NULL to compute the size of buffer required.
968 static int ocfs2_xattr_get(struct inode *inode,
975 struct ocfs2_dinode *di = NULL;
976 struct buffer_head *di_bh = NULL;
977 struct ocfs2_inode_info *oi = OCFS2_I(inode);
978 struct ocfs2_xattr_search xis = {
979 .not_found = -ENODATA,
981 struct ocfs2_xattr_search xbs = {
982 .not_found = -ENODATA,
985 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
988 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
991 ret = ocfs2_inode_lock(inode, &di_bh, 0);
996 xis.inode_bh = xbs.inode_bh = di_bh;
997 di = (struct ocfs2_dinode *)di_bh->b_data;
999 down_read(&oi->ip_xattr_sem);
1000 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1002 if (ret == -ENODATA && di->i_xattr_loc)
1003 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1005 up_read(&oi->ip_xattr_sem);
1006 ocfs2_inode_unlock(inode, 0);
1013 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1014 struct ocfs2_xattr_value_root *xv,
1018 int ret = 0, i, cp_len, credits;
1019 u16 blocksize = inode->i_sb->s_blocksize;
1020 u32 p_cluster, num_clusters;
1021 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1022 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1024 struct buffer_head *bh = NULL;
1027 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1029 credits = clusters * bpc;
1030 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
1031 if (IS_ERR(handle)) {
1032 ret = PTR_ERR(handle);
1037 while (cpos < clusters) {
1038 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1039 &num_clusters, &xv->xr_list);
1045 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1047 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1048 ret = ocfs2_read_block(inode, blkno, &bh);
1054 ret = ocfs2_journal_access(handle,
1057 OCFS2_JOURNAL_ACCESS_WRITE);
1063 cp_len = value_len > blocksize ? blocksize : value_len;
1064 memcpy(bh->b_data, value, cp_len);
1065 value_len -= cp_len;
1067 if (cp_len < blocksize)
1068 memset(bh->b_data + cp_len, 0,
1069 blocksize - cp_len);
1071 ret = ocfs2_journal_dirty(handle, bh);
1080 * XXX: do we need to empty all the following
1081 * blocks in this cluster?
1086 cpos += num_clusters;
1089 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1096 static int ocfs2_xattr_cleanup(struct inode *inode,
1097 struct ocfs2_xattr_info *xi,
1098 struct ocfs2_xattr_search *xs,
1101 handle_t *handle = NULL;
1103 size_t name_len = strlen(xi->name);
1104 void *val = xs->base + offs;
1105 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1107 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1108 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1109 if (IS_ERR(handle)) {
1110 ret = PTR_ERR(handle);
1114 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1115 OCFS2_JOURNAL_ACCESS_WRITE);
1120 /* Decrease xattr count */
1121 le16_add_cpu(&xs->header->xh_count, -1);
1122 /* Remove the xattr entry and tree root which has already be set*/
1123 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1124 memset(val, 0, size);
1126 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1130 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1135 static int ocfs2_xattr_update_entry(struct inode *inode,
1136 struct ocfs2_xattr_info *xi,
1137 struct ocfs2_xattr_search *xs,
1140 handle_t *handle = NULL;
1143 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1144 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1145 if (IS_ERR(handle)) {
1146 ret = PTR_ERR(handle);
1150 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1151 OCFS2_JOURNAL_ACCESS_WRITE);
1157 xs->here->xe_name_offset = cpu_to_le16(offs);
1158 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1159 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1160 ocfs2_xattr_set_local(xs->here, 1);
1162 ocfs2_xattr_set_local(xs->here, 0);
1163 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1165 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1169 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1175 * ocfs2_xattr_set_value_outside()
1177 * Set large size value in B tree.
1179 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1180 struct ocfs2_xattr_info *xi,
1181 struct ocfs2_xattr_search *xs,
1184 size_t name_len = strlen(xi->name);
1185 void *val = xs->base + offs;
1186 struct ocfs2_xattr_value_root *xv = NULL;
1187 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1190 memset(val, 0, size);
1191 memcpy(val, xi->name, name_len);
1192 xv = (struct ocfs2_xattr_value_root *)
1193 (val + OCFS2_XATTR_SIZE(name_len));
1194 xv->xr_clusters = 0;
1195 xv->xr_last_eb_blk = 0;
1196 xv->xr_list.l_tree_depth = 0;
1197 xv->xr_list.l_count = cpu_to_le16(1);
1198 xv->xr_list.l_next_free_rec = 0;
1200 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1206 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1212 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1220 * ocfs2_xattr_set_entry_local()
1222 * Set, replace or remove extended attribute in local.
1224 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1225 struct ocfs2_xattr_info *xi,
1226 struct ocfs2_xattr_search *xs,
1227 struct ocfs2_xattr_entry *last,
1230 size_t name_len = strlen(xi->name);
1233 if (xi->value && xs->not_found) {
1234 /* Insert the new xattr entry. */
1235 le16_add_cpu(&xs->header->xh_count, 1);
1236 ocfs2_xattr_set_type(last, xi->name_index);
1237 ocfs2_xattr_set_local(last, 1);
1238 last->xe_name_len = name_len;
1244 first_val = xs->base + min_offs;
1245 offs = le16_to_cpu(xs->here->xe_name_offset);
1246 val = xs->base + offs;
1248 if (le64_to_cpu(xs->here->xe_value_size) >
1249 OCFS2_XATTR_INLINE_SIZE)
1250 size = OCFS2_XATTR_SIZE(name_len) +
1251 OCFS2_XATTR_ROOT_SIZE;
1253 size = OCFS2_XATTR_SIZE(name_len) +
1254 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1256 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1257 OCFS2_XATTR_SIZE(xi->value_len)) {
1258 /* The old and the new value have the
1259 same size. Just replace the value. */
1260 ocfs2_xattr_set_local(xs->here, 1);
1261 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1262 /* Clear value bytes. */
1263 memset(val + OCFS2_XATTR_SIZE(name_len),
1265 OCFS2_XATTR_SIZE(xi->value_len));
1266 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1271 /* Remove the old name+value. */
1272 memmove(first_val + size, first_val, val - first_val);
1273 memset(first_val, 0, size);
1274 xs->here->xe_name_hash = 0;
1275 xs->here->xe_name_offset = 0;
1276 ocfs2_xattr_set_local(xs->here, 1);
1277 xs->here->xe_value_size = 0;
1281 /* Adjust all value offsets. */
1282 last = xs->header->xh_entries;
1283 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1284 size_t o = le16_to_cpu(last->xe_name_offset);
1287 last->xe_name_offset = cpu_to_le16(o + size);
1292 /* Remove the old entry. */
1294 memmove(xs->here, xs->here + 1,
1295 (void *)last - (void *)xs->here);
1296 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1297 le16_add_cpu(&xs->header->xh_count, -1);
1301 /* Insert the new name+value. */
1302 size_t size = OCFS2_XATTR_SIZE(name_len) +
1303 OCFS2_XATTR_SIZE(xi->value_len);
1304 void *val = xs->base + min_offs - size;
1306 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1307 memset(val, 0, size);
1308 memcpy(val, xi->name, name_len);
1309 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1312 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1313 ocfs2_xattr_set_local(xs->here, 1);
1314 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1321 * ocfs2_xattr_set_entry()
1323 * Set extended attribute entry into inode or block.
1325 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1326 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1327 * then set value in B tree with set_value_outside().
1329 static int ocfs2_xattr_set_entry(struct inode *inode,
1330 struct ocfs2_xattr_info *xi,
1331 struct ocfs2_xattr_search *xs,
1334 struct ocfs2_xattr_entry *last;
1335 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1336 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1337 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1339 handle_t *handle = NULL;
1341 struct ocfs2_xattr_info xi_l = {
1342 .name_index = xi->name_index,
1345 .value_len = xi->value_len,
1348 /* Compute min_offs, last and free space. */
1349 last = xs->header->xh_entries;
1351 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1352 size_t offs = le16_to_cpu(last->xe_name_offset);
1353 if (offs < min_offs)
1358 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1362 if (!xs->not_found) {
1364 if (ocfs2_xattr_is_local(xs->here))
1365 size = OCFS2_XATTR_SIZE(name_len) +
1366 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1368 size = OCFS2_XATTR_SIZE(name_len) +
1369 OCFS2_XATTR_ROOT_SIZE;
1370 free += (size + sizeof(struct ocfs2_xattr_entry));
1372 /* Check free space in inode or block */
1373 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1374 if (free < sizeof(struct ocfs2_xattr_entry) +
1375 OCFS2_XATTR_SIZE(name_len) +
1376 OCFS2_XATTR_ROOT_SIZE) {
1380 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1381 xi_l.value = (void *)&def_xv;
1382 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1383 } else if (xi->value) {
1384 if (free < sizeof(struct ocfs2_xattr_entry) +
1385 OCFS2_XATTR_SIZE(name_len) +
1386 OCFS2_XATTR_SIZE(xi->value_len)) {
1392 if (!xs->not_found) {
1393 /* For existing extended attribute */
1394 size_t size = OCFS2_XATTR_SIZE(name_len) +
1395 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1396 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1397 void *val = xs->base + offs;
1399 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1400 /* Replace existing local xattr with tree root */
1401 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1406 } else if (!ocfs2_xattr_is_local(xs->here)) {
1407 /* For existing xattr which has value outside */
1408 struct ocfs2_xattr_value_root *xv = NULL;
1409 xv = (struct ocfs2_xattr_value_root *)(val +
1410 OCFS2_XATTR_SIZE(name_len));
1412 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1414 * If new value need set outside also,
1415 * first truncate old value to new value,
1416 * then set new value with set_value_outside().
1418 ret = ocfs2_xattr_value_truncate(inode,
1427 ret = __ocfs2_xattr_set_value_outside(inode,
1436 ret = ocfs2_xattr_update_entry(inode,
1445 * If new value need set in local,
1446 * just trucate old value to zero.
1448 ret = ocfs2_xattr_value_truncate(inode,
1458 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1459 OCFS2_INODE_UPDATE_CREDITS);
1460 if (IS_ERR(handle)) {
1461 ret = PTR_ERR(handle);
1466 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1467 OCFS2_JOURNAL_ACCESS_WRITE);
1473 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1474 /* set extended attribute in external block. */
1475 ret = ocfs2_extend_trans(handle,
1476 OCFS2_INODE_UPDATE_CREDITS +
1477 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1482 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1483 OCFS2_JOURNAL_ACCESS_WRITE);
1491 * Set value in local, include set tree root in local.
1492 * This is the first step for value size >INLINE_SIZE.
1494 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1496 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1497 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1504 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1505 (flag & OCFS2_INLINE_XATTR_FL)) {
1506 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1507 unsigned int xattrsize = osb->s_xattr_inline_size;
1510 * Adjust extent record count or inline data size
1511 * to reserve space for extended attribute.
1513 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1514 struct ocfs2_inline_data *idata = &di->id2.i_data;
1515 le16_add_cpu(&idata->id_count, -xattrsize);
1516 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1517 struct ocfs2_extent_list *el = &di->id2.i_list;
1518 le16_add_cpu(&el->l_count, -(xattrsize /
1519 sizeof(struct ocfs2_extent_rec)));
1521 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1523 /* Update xattr flag */
1524 spin_lock(&oi->ip_lock);
1525 oi->ip_dyn_features |= flag;
1526 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1527 spin_unlock(&oi->ip_lock);
1528 /* Update inode ctime */
1529 inode->i_ctime = CURRENT_TIME;
1530 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1531 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1533 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1538 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1540 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1542 * Set value outside in B tree.
1543 * This is the second step for value size > INLINE_SIZE.
1545 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1546 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1552 * If set value outside failed, we have to clean
1553 * the junk tree root we have already set in local.
1555 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1565 static int ocfs2_remove_value_outside(struct inode*inode,
1566 struct buffer_head *bh,
1567 struct ocfs2_xattr_header *header)
1571 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1572 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1574 if (!ocfs2_xattr_is_local(entry)) {
1575 struct ocfs2_xattr_value_root *xv;
1578 val = (void *)header +
1579 le16_to_cpu(entry->xe_name_offset);
1580 xv = (struct ocfs2_xattr_value_root *)
1581 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1582 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1593 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1594 struct buffer_head *di_bh)
1597 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1598 struct ocfs2_xattr_header *header;
1601 header = (struct ocfs2_xattr_header *)
1602 ((void *)di + inode->i_sb->s_blocksize -
1603 le16_to_cpu(di->i_xattr_inline_size));
1605 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1610 static int ocfs2_xattr_block_remove(struct inode *inode,
1611 struct buffer_head *blk_bh)
1613 struct ocfs2_xattr_block *xb;
1616 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1617 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1618 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1619 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1621 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1626 static int ocfs2_xattr_free_block(struct inode *inode,
1629 struct inode *xb_alloc_inode;
1630 struct buffer_head *xb_alloc_bh = NULL;
1631 struct buffer_head *blk_bh = NULL;
1632 struct ocfs2_xattr_block *xb;
1633 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1639 ret = ocfs2_read_block(inode, block, &blk_bh);
1645 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1646 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1651 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1657 blk = le64_to_cpu(xb->xb_blkno);
1658 bit = le16_to_cpu(xb->xb_suballoc_bit);
1659 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1661 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1662 EXTENT_ALLOC_SYSTEM_INODE,
1663 le16_to_cpu(xb->xb_suballoc_slot));
1664 if (!xb_alloc_inode) {
1669 mutex_lock(&xb_alloc_inode->i_mutex);
1671 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1677 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1678 if (IS_ERR(handle)) {
1679 ret = PTR_ERR(handle);
1684 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1689 ocfs2_commit_trans(osb, handle);
1691 ocfs2_inode_unlock(xb_alloc_inode, 1);
1692 brelse(xb_alloc_bh);
1694 mutex_unlock(&xb_alloc_inode->i_mutex);
1695 iput(xb_alloc_inode);
1702 * ocfs2_xattr_remove()
1704 * Free extended attribute resources associated with this inode.
1706 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1708 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1709 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1713 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1716 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1719 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1720 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1727 if (di->i_xattr_loc) {
1728 ret = ocfs2_xattr_free_block(inode,
1729 le64_to_cpu(di->i_xattr_loc));
1736 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1737 OCFS2_INODE_UPDATE_CREDITS);
1738 if (IS_ERR(handle)) {
1739 ret = PTR_ERR(handle);
1743 ret = ocfs2_journal_access(handle, inode, di_bh,
1744 OCFS2_JOURNAL_ACCESS_WRITE);
1750 di->i_xattr_loc = 0;
1752 spin_lock(&oi->ip_lock);
1753 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1754 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1755 spin_unlock(&oi->ip_lock);
1757 ret = ocfs2_journal_dirty(handle, di_bh);
1761 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1766 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1767 struct ocfs2_dinode *di)
1769 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1770 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1773 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1776 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1777 struct ocfs2_inline_data *idata = &di->id2.i_data;
1778 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1779 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1780 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1781 le64_to_cpu(di->i_size);
1783 struct ocfs2_extent_list *el = &di->id2.i_list;
1784 free = (le16_to_cpu(el->l_count) -
1785 le16_to_cpu(el->l_next_free_rec)) *
1786 sizeof(struct ocfs2_extent_rec);
1788 if (free >= xattrsize)
1795 * ocfs2_xattr_ibody_find()
1797 * Find extended attribute in inode block and
1798 * fill search info into struct ocfs2_xattr_search.
1800 static int ocfs2_xattr_ibody_find(struct inode *inode,
1803 struct ocfs2_xattr_search *xs)
1805 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1806 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1810 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1813 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1814 down_read(&oi->ip_alloc_sem);
1815 has_space = ocfs2_xattr_has_space_inline(inode, di);
1816 up_read(&oi->ip_alloc_sem);
1821 xs->xattr_bh = xs->inode_bh;
1822 xs->end = (void *)di + inode->i_sb->s_blocksize;
1823 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1824 xs->header = (struct ocfs2_xattr_header *)
1825 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1827 xs->header = (struct ocfs2_xattr_header *)
1828 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1829 xs->base = (void *)xs->header;
1830 xs->here = xs->header->xh_entries;
1832 /* Find the named attribute. */
1833 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1834 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1835 if (ret && ret != -ENODATA)
1837 xs->not_found = ret;
1844 * ocfs2_xattr_ibody_set()
1846 * Set, replace or remove an extended attribute into inode block.
1849 static int ocfs2_xattr_ibody_set(struct inode *inode,
1850 struct ocfs2_xattr_info *xi,
1851 struct ocfs2_xattr_search *xs)
1853 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1854 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1857 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1860 down_write(&oi->ip_alloc_sem);
1861 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1862 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1868 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1869 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1871 up_write(&oi->ip_alloc_sem);
1877 * ocfs2_xattr_block_find()
1879 * Find extended attribute in external block and
1880 * fill search info into struct ocfs2_xattr_search.
1882 static int ocfs2_xattr_block_find(struct inode *inode,
1885 struct ocfs2_xattr_search *xs)
1887 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1888 struct buffer_head *blk_bh = NULL;
1889 struct ocfs2_xattr_block *xb;
1892 if (!di->i_xattr_loc)
1895 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1901 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1902 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1907 xs->xattr_bh = blk_bh;
1909 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1910 xs->header = &xb->xb_attrs.xb_header;
1911 xs->base = (void *)xs->header;
1912 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1913 xs->here = xs->header->xh_entries;
1915 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1917 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1921 if (ret && ret != -ENODATA) {
1922 xs->xattr_bh = NULL;
1925 xs->not_found = ret;
1934 * ocfs2_xattr_block_set()
1936 * Set, replace or remove an extended attribute into external block.
1939 static int ocfs2_xattr_block_set(struct inode *inode,
1940 struct ocfs2_xattr_info *xi,
1941 struct ocfs2_xattr_search *xs)
1943 struct buffer_head *new_bh = NULL;
1944 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1945 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1946 struct ocfs2_alloc_context *meta_ac = NULL;
1947 handle_t *handle = NULL;
1948 struct ocfs2_xattr_block *xblk = NULL;
1949 u16 suballoc_bit_start;
1954 if (!xs->xattr_bh) {
1956 * Alloc one external block for extended attribute
1959 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1964 handle = ocfs2_start_trans(osb,
1965 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1966 if (IS_ERR(handle)) {
1967 ret = PTR_ERR(handle);
1971 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1972 OCFS2_JOURNAL_ACCESS_CREATE);
1978 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1979 &suballoc_bit_start, &num_got,
1986 new_bh = sb_getblk(inode->i_sb, first_blkno);
1987 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1989 ret = ocfs2_journal_access(handle, inode, new_bh,
1990 OCFS2_JOURNAL_ACCESS_CREATE);
1996 /* Initialize ocfs2_xattr_block */
1997 xs->xattr_bh = new_bh;
1998 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1999 memset(xblk, 0, inode->i_sb->s_blocksize);
2000 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2001 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2002 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2003 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2004 xblk->xb_blkno = cpu_to_le64(first_blkno);
2006 xs->header = &xblk->xb_attrs.xb_header;
2007 xs->base = (void *)xs->header;
2008 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2009 xs->here = xs->header->xh_entries;
2012 ret = ocfs2_journal_dirty(handle, new_bh);
2017 di->i_xattr_loc = cpu_to_le64(first_blkno);
2018 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
2022 ocfs2_commit_trans(osb, handle);
2025 ocfs2_free_alloc_context(meta_ac);
2029 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2031 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2032 /* Set extended attribute into external block */
2033 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
2034 if (!ret || ret != -ENOSPC)
2037 ret = ocfs2_xattr_create_index_block(inode, xs);
2042 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
2052 * Set, replace or remove an extended attribute for this inode.
2053 * value is NULL to remove an existing extended attribute, else either
2054 * create or replace an extended attribute.
2056 int ocfs2_xattr_set(struct inode *inode,
2063 struct buffer_head *di_bh = NULL;
2064 struct ocfs2_dinode *di;
2067 struct ocfs2_xattr_info xi = {
2068 .name_index = name_index,
2071 .value_len = value_len,
2074 struct ocfs2_xattr_search xis = {
2075 .not_found = -ENODATA,
2078 struct ocfs2_xattr_search xbs = {
2079 .not_found = -ENODATA,
2082 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2086 * Only xbs will be used on indexed trees. xis doesn't need a
2089 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2091 mlog_errno(-ENOMEM);
2095 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2098 goto cleanup_nolock;
2100 xis.inode_bh = xbs.inode_bh = di_bh;
2101 di = (struct ocfs2_dinode *)di_bh->b_data;
2103 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2105 * Scan inode and external block to find the same name
2106 * extended attribute and collect search infomation.
2108 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2111 if (xis.not_found) {
2112 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2117 if (xis.not_found && xbs.not_found) {
2119 if (flags & XATTR_REPLACE)
2126 if (flags & XATTR_CREATE)
2131 /* Remove existing extended attribute */
2133 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2134 else if (!xbs.not_found)
2135 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2137 /* We always try to set extended attribute into inode first*/
2138 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2139 if (!ret && !xbs.not_found) {
2141 * If succeed and that extended attribute existing in
2142 * external block, then we will remove it.
2146 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2147 } else if (ret == -ENOSPC) {
2148 if (di->i_xattr_loc && !xbs.xattr_bh) {
2149 ret = ocfs2_xattr_block_find(inode, name_index,
2155 * If no space in inode, we will set extended attribute
2156 * into external block.
2158 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2161 if (!xis.not_found) {
2163 * If succeed and that extended attribute
2164 * existing in inode, we will remove it.
2168 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2173 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2174 ocfs2_inode_unlock(inode, 1);
2177 brelse(xbs.xattr_bh);
2178 ocfs2_xattr_bucket_free(xbs.bucket);
2184 * Find the xattr extent rec which may contains name_hash.
2185 * e_cpos will be the first name hash of the xattr rec.
2186 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2188 static int ocfs2_xattr_get_rec(struct inode *inode,
2193 struct ocfs2_extent_list *el)
2196 struct buffer_head *eb_bh = NULL;
2197 struct ocfs2_extent_block *eb;
2198 struct ocfs2_extent_rec *rec = NULL;
2201 if (el->l_tree_depth) {
2202 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2208 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2211 if (el->l_tree_depth) {
2212 ocfs2_error(inode->i_sb,
2213 "Inode %lu has non zero tree depth in "
2214 "xattr tree block %llu\n", inode->i_ino,
2215 (unsigned long long)eb_bh->b_blocknr);
2221 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2222 rec = &el->l_recs[i];
2224 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2225 e_blkno = le64_to_cpu(rec->e_blkno);
2231 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2232 "record (%u, %u, 0) in xattr", inode->i_ino,
2233 le32_to_cpu(rec->e_cpos),
2234 ocfs2_rec_clusters(el, rec));
2239 *p_blkno = le64_to_cpu(rec->e_blkno);
2240 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2242 *e_cpos = le32_to_cpu(rec->e_cpos);
2248 typedef int (xattr_bucket_func)(struct inode *inode,
2249 struct ocfs2_xattr_bucket *bucket,
2252 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2253 struct ocfs2_xattr_bucket *bucket,
2260 int i, ret = 0, cmp = 1, block_off, new_offset;
2261 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
2262 size_t name_len = strlen(name);
2263 struct ocfs2_xattr_entry *xe = NULL;
2267 * We don't use binary search in the bucket because there
2268 * may be multiple entries with the same name hash.
2270 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2271 xe = &xh->xh_entries[i];
2273 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2275 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2278 cmp = name_index - ocfs2_xattr_get_type(xe);
2280 cmp = name_len - xe->xe_name_len;
2284 ret = ocfs2_xattr_bucket_get_name_value(inode,
2294 xe_name = bucket_block(bucket, block_off) + new_offset;
2295 if (!memcmp(name, xe_name, name_len)) {
2307 * Find the specified xattr entry in a series of buckets.
2308 * This series start from p_blkno and last for num_clusters.
2309 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2310 * the num of the valid buckets.
2312 * Return the buffer_head this xattr should reside in. And if the xattr's
2313 * hash is in the gap of 2 buckets, return the lower bucket.
2315 static int ocfs2_xattr_bucket_find(struct inode *inode,
2322 struct ocfs2_xattr_search *xs)
2325 struct ocfs2_xattr_header *xh = NULL;
2326 struct ocfs2_xattr_entry *xe = NULL;
2328 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2329 int low_bucket = 0, bucket, high_bucket;
2330 struct ocfs2_xattr_bucket *search;
2332 u64 blkno, lower_blkno = 0;
2334 search = ocfs2_xattr_bucket_new(inode);
2341 ret = ocfs2_read_xattr_bucket(search, p_blkno);
2347 xh = bucket_xh(search);
2348 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2349 while (low_bucket <= high_bucket) {
2350 ocfs2_xattr_bucket_relse(search);
2352 bucket = (low_bucket + high_bucket) / 2;
2353 blkno = p_blkno + bucket * blk_per_bucket;
2354 ret = ocfs2_read_xattr_bucket(search, blkno);
2360 xh = bucket_xh(search);
2361 xe = &xh->xh_entries[0];
2362 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2363 high_bucket = bucket - 1;
2368 * Check whether the hash of the last entry in our
2369 * bucket is larger than the search one. for an empty
2370 * bucket, the last one is also the first one.
2373 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2375 last_hash = le32_to_cpu(xe->xe_name_hash);
2377 /* record lower_blkno which may be the insert place. */
2378 lower_blkno = blkno;
2380 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2381 low_bucket = bucket + 1;
2385 /* the searched xattr should reside in this bucket if exists. */
2386 ret = ocfs2_find_xe_in_bucket(inode, search,
2387 name_index, name, name_hash,
2397 * Record the bucket we have found.
2398 * When the xattr's hash value is in the gap of 2 buckets, we will
2399 * always set it to the previous bucket.
2402 lower_blkno = p_blkno;
2404 /* This should be in cache - we just read it during the search */
2405 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2411 xs->header = bucket_xh(xs->bucket);
2412 xs->base = bucket_block(xs->bucket, 0);
2413 xs->end = xs->base + inode->i_sb->s_blocksize;
2416 xs->here = &xs->header->xh_entries[index];
2417 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2418 (unsigned long long)bucket_blkno(xs->bucket), index);
2423 ocfs2_xattr_bucket_free(search);
2427 static int ocfs2_xattr_index_block_find(struct inode *inode,
2428 struct buffer_head *root_bh,
2431 struct ocfs2_xattr_search *xs)
2434 struct ocfs2_xattr_block *xb =
2435 (struct ocfs2_xattr_block *)root_bh->b_data;
2436 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2437 struct ocfs2_extent_list *el = &xb_root->xt_list;
2439 u32 first_hash, num_clusters = 0;
2440 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2442 if (le16_to_cpu(el->l_next_free_rec) == 0)
2445 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2446 name, name_hash, name_index);
2448 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2455 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2457 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2458 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
2461 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2462 p_blkno, first_hash, num_clusters, xs);
2468 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2471 xattr_bucket_func *func,
2475 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2476 u32 num_buckets = clusters * bpc;
2477 struct ocfs2_xattr_bucket *bucket;
2479 bucket = ocfs2_xattr_bucket_new(inode);
2481 mlog_errno(-ENOMEM);
2485 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2486 clusters, (unsigned long long)blkno);
2488 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
2489 ret = ocfs2_read_xattr_bucket(bucket, blkno);
2496 * The real bucket num in this series of blocks is stored
2497 * in the 1st bucket.
2500 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
2502 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2503 (unsigned long long)blkno,
2504 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
2506 ret = func(inode, bucket, para);
2509 /* Fall through to bucket_relse() */
2512 ocfs2_xattr_bucket_relse(bucket);
2517 ocfs2_xattr_bucket_free(bucket);
2521 struct ocfs2_xattr_tree_list {
2527 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2528 struct ocfs2_xattr_header *xh,
2535 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2538 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2540 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2541 *new_offset = name_offset % inode->i_sb->s_blocksize;
2546 static int ocfs2_list_xattr_bucket(struct inode *inode,
2547 struct ocfs2_xattr_bucket *bucket,
2551 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2552 int i, block_off, new_offset;
2553 const char *prefix, *name;
2555 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
2556 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
2557 type = ocfs2_xattr_get_type(entry);
2558 prefix = ocfs2_xattr_prefix(type);
2561 ret = ocfs2_xattr_bucket_get_name_value(inode,
2569 name = (const char *)bucket_block(bucket, block_off) +
2571 ret = ocfs2_xattr_list_entry(xl->buffer,
2575 entry->xe_name_len);
2584 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2585 struct ocfs2_xattr_tree_root *xt,
2589 struct ocfs2_extent_list *el = &xt->xt_list;
2591 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2593 struct ocfs2_xattr_tree_list xl = {
2595 .buffer_size = buffer_size,
2599 if (le16_to_cpu(el->l_next_free_rec) == 0)
2602 while (name_hash > 0) {
2603 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2604 &e_cpos, &num_clusters, el);
2610 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2611 ocfs2_list_xattr_bucket,
2621 name_hash = e_cpos - 1;
2629 static int cmp_xe(const void *a, const void *b)
2631 const struct ocfs2_xattr_entry *l = a, *r = b;
2632 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2633 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2635 if (l_hash > r_hash)
2637 if (l_hash < r_hash)
2642 static void swap_xe(void *a, void *b, int size)
2644 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2647 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2648 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2652 * When the ocfs2_xattr_block is filled up, new bucket will be created
2653 * and all the xattr entries will be moved to the new bucket.
2654 * The header goes at the start of the bucket, and the names+values are
2655 * filled from the end. This is why *target starts as the last buffer.
2656 * Note: we need to sort the entries since they are not saved in order
2657 * in the ocfs2_xattr_block.
2659 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2660 struct buffer_head *xb_bh,
2661 struct ocfs2_xattr_bucket *bucket)
2663 int i, blocksize = inode->i_sb->s_blocksize;
2664 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2665 u16 offset, size, off_change;
2666 struct ocfs2_xattr_entry *xe;
2667 struct ocfs2_xattr_block *xb =
2668 (struct ocfs2_xattr_block *)xb_bh->b_data;
2669 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2670 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
2671 u16 count = le16_to_cpu(xb_xh->xh_count);
2672 char *src = xb_bh->b_data;
2673 char *target = bucket_block(bucket, blks - 1);
2675 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2676 (unsigned long long)xb_bh->b_blocknr,
2677 (unsigned long long)bucket_blkno(bucket));
2679 for (i = 0; i < blks; i++)
2680 memset(bucket_block(bucket, i), 0, blocksize);
2683 * Since the xe_name_offset is based on ocfs2_xattr_header,
2684 * there is a offset change corresponding to the change of
2685 * ocfs2_xattr_header's position.
2687 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2688 xe = &xb_xh->xh_entries[count - 1];
2689 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2690 size = blocksize - offset;
2692 /* copy all the names and values. */
2693 memcpy(target + offset, src + offset, size);
2695 /* Init new header now. */
2696 xh->xh_count = xb_xh->xh_count;
2697 xh->xh_num_buckets = cpu_to_le16(1);
2698 xh->xh_name_value_len = cpu_to_le16(size);
2699 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2701 /* copy all the entries. */
2702 target = bucket_block(bucket, 0);
2703 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2704 size = count * sizeof(struct ocfs2_xattr_entry);
2705 memcpy(target + offset, (char *)xb_xh + offset, size);
2707 /* Change the xe offset for all the xe because of the move. */
2708 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2709 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2710 for (i = 0; i < count; i++)
2711 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2713 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2714 offset, size, off_change);
2716 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2721 * After we move xattr from block to index btree, we have to
2722 * update ocfs2_xattr_search to the new xe and base.
2724 * When the entry is in xattr block, xattr_bh indicates the storage place.
2725 * While if the entry is in index b-tree, "bucket" indicates the
2726 * real place of the xattr.
2728 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
2729 struct ocfs2_xattr_search *xs,
2730 struct buffer_head *old_bh)
2732 char *buf = old_bh->b_data;
2733 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2734 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2737 xs->header = bucket_xh(xs->bucket);
2738 xs->base = bucket_block(xs->bucket, 0);
2739 xs->end = xs->base + inode->i_sb->s_blocksize;
2744 i = xs->here - old_xh->xh_entries;
2745 xs->here = &xs->header->xh_entries[i];
2748 static int ocfs2_xattr_create_index_block(struct inode *inode,
2749 struct ocfs2_xattr_search *xs)
2751 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2755 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2756 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2757 struct ocfs2_alloc_context *data_ac;
2758 struct buffer_head *xb_bh = xs->xattr_bh;
2759 struct ocfs2_xattr_block *xb =
2760 (struct ocfs2_xattr_block *)xb_bh->b_data;
2761 struct ocfs2_xattr_tree_root *xr;
2762 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2764 mlog(0, "create xattr index block for %llu\n",
2765 (unsigned long long)xb_bh->b_blocknr);
2767 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2768 BUG_ON(!xs->bucket);
2770 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2778 * We can use this lock for now, and maybe move to a dedicated mutex
2779 * if performance becomes a problem later.
2781 down_write(&oi->ip_alloc_sem);
2784 * We need more credits. One for the xattr block update and one
2785 * for each block of the new xattr bucket.
2787 credits += 1 + ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2788 handle = ocfs2_start_trans(osb, credits);
2789 if (IS_ERR(handle)) {
2790 ret = PTR_ERR(handle);
2795 ret = ocfs2_journal_access(handle, inode, xb_bh,
2796 OCFS2_JOURNAL_ACCESS_WRITE);
2802 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2809 * The bucket may spread in many blocks, and
2810 * we will only touch the 1st block and the last block
2811 * in the whole bucket(one for entry and one for data).
2813 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2815 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
2816 (unsigned long long)blkno);
2818 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
2824 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
2825 OCFS2_JOURNAL_ACCESS_CREATE);
2831 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
2832 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
2834 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
2836 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2837 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2838 offsetof(struct ocfs2_xattr_block, xb_attrs));
2840 xr = &xb->xb_attrs.xb_root;
2841 xr->xt_clusters = cpu_to_le32(1);
2842 xr->xt_last_eb_blk = 0;
2843 xr->xt_list.l_tree_depth = 0;
2844 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2845 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2847 xr->xt_list.l_recs[0].e_cpos = 0;
2848 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2849 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2851 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2853 ret = ocfs2_journal_dirty(handle, xb_bh);
2860 ocfs2_commit_trans(osb, handle);
2863 up_write(&oi->ip_alloc_sem);
2867 ocfs2_free_alloc_context(data_ac);
2872 static int cmp_xe_offset(const void *a, const void *b)
2874 const struct ocfs2_xattr_entry *l = a, *r = b;
2875 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2876 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2878 if (l_name_offset < r_name_offset)
2880 if (l_name_offset > r_name_offset)
2886 * defrag a xattr bucket if we find that the bucket has some
2887 * holes beteen name/value pairs.
2888 * We will move all the name/value pairs to the end of the bucket
2889 * so that we can spare some space for insertion.
2891 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2892 struct ocfs2_xattr_bucket *bucket)
2895 size_t end, offset, len, value_len;
2896 struct ocfs2_xattr_header *xh;
2897 char *entries, *buf, *bucket_buf = NULL;
2898 u64 blkno = bucket_blkno(bucket);
2900 size_t blocksize = inode->i_sb->s_blocksize;
2902 struct ocfs2_xattr_entry *xe;
2905 * In order to make the operation more efficient and generic,
2906 * we copy all the blocks into a contiguous memory and do the
2907 * defragment there, so if anything is error, we will not touch
2910 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2917 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
2918 memcpy(buf, bucket_block(bucket, i), blocksize);
2920 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), bucket->bu_blocks);
2921 if (IS_ERR(handle)) {
2922 ret = PTR_ERR(handle);
2928 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
2929 OCFS2_JOURNAL_ACCESS_WRITE);
2935 xh = (struct ocfs2_xattr_header *)bucket_buf;
2936 entries = (char *)xh->xh_entries;
2937 xh_free_start = le16_to_cpu(xh->xh_free_start);
2939 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2940 "xh_free_start = %u, xh_name_value_len = %u.\n",
2941 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
2942 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
2945 * sort all the entries by their offset.
2946 * the largest will be the first, so that we can
2947 * move them to the end one by one.
2949 sort(entries, le16_to_cpu(xh->xh_count),
2950 sizeof(struct ocfs2_xattr_entry),
2951 cmp_xe_offset, swap_xe);
2953 /* Move all name/values to the end of the bucket. */
2954 xe = xh->xh_entries;
2955 end = OCFS2_XATTR_BUCKET_SIZE;
2956 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2957 offset = le16_to_cpu(xe->xe_name_offset);
2958 if (ocfs2_xattr_is_local(xe))
2959 value_len = OCFS2_XATTR_SIZE(
2960 le64_to_cpu(xe->xe_value_size));
2962 value_len = OCFS2_XATTR_ROOT_SIZE;
2963 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2966 * We must make sure that the name/value pair
2967 * exist in the same block. So adjust end to
2968 * the previous block end if needed.
2970 if (((end - len) / blocksize !=
2971 (end - 1) / blocksize))
2972 end = end - end % blocksize;
2974 if (end > offset + len) {
2975 memmove(bucket_buf + end - len,
2976 bucket_buf + offset, len);
2977 xe->xe_name_offset = cpu_to_le16(end - len);
2980 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2981 "bucket %llu\n", (unsigned long long)blkno);
2986 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2987 "bucket %llu\n", (unsigned long long)blkno);
2989 if (xh_free_start == end)
2992 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2993 xh->xh_free_start = cpu_to_le16(end);
2995 /* sort the entries by their name_hash. */
2996 sort(entries, le16_to_cpu(xh->xh_count),
2997 sizeof(struct ocfs2_xattr_entry),
3001 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3002 memcpy(bucket_block(bucket, i), buf, blocksize);
3003 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
3006 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3013 * Move half nums of the xattr bucket in the previous cluster to this new
3014 * cluster. We only touch the last cluster of the previous extend record.
3016 * first_bh is the first buffer_head of a series of bucket in the same
3017 * extent rec and header_bh is the header of one bucket in this cluster.
3018 * They will be updated if we move the data header_bh contains to the new
3019 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3021 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3023 struct buffer_head **first_bh,
3024 struct buffer_head **header_bh,
3030 int i, ret, credits;
3031 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3032 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3033 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3034 int blocksize = inode->i_sb->s_blocksize;
3035 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3036 struct ocfs2_xattr_header *new_xh;
3037 struct ocfs2_xattr_header *xh =
3038 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3040 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3041 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3043 prev_bh = *first_bh;
3045 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3047 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3049 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3050 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
3053 * We need to update the 1st half of the new cluster and
3054 * 1 more for the update of the 1st bucket of the previous
3057 credits = bpc / 2 + 1;
3058 ret = ocfs2_extend_trans(handle, credits);
3064 ret = ocfs2_journal_access(handle, inode, prev_bh,
3065 OCFS2_JOURNAL_ACCESS_WRITE);
3071 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3072 old_bh = new_bh = NULL;
3073 new_bh = sb_getblk(inode->i_sb, new_blkno);
3080 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3082 ret = ocfs2_journal_access(handle, inode, new_bh,
3083 OCFS2_JOURNAL_ACCESS_CREATE);
3090 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3097 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3100 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3101 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3104 *first_hash = le32_to_cpu(
3105 new_xh->xh_entries[0].xe_name_hash);
3106 new_first_bh = new_bh;
3107 get_bh(new_first_bh);
3110 ocfs2_journal_dirty(handle, new_bh);
3112 if (*header_bh == old_bh) {
3114 *header_bh = new_bh;
3118 *first_bh = new_first_bh;
3125 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3127 ocfs2_journal_dirty(handle, prev_bh);
3130 brelse(new_first_bh);
3135 * Find the suitable pos when we divide a bucket into 2.
3136 * We have to make sure the xattrs with the same hash value exist
3137 * in the same bucket.
3139 * If this ocfs2_xattr_header covers more than one hash value, find a
3140 * place where the hash value changes. Try to find the most even split.
3141 * The most common case is that all entries have different hash values,
3142 * and the first check we make will find a place to split.
3144 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3146 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3147 int count = le16_to_cpu(xh->xh_count);
3148 int delta, middle = count / 2;
3151 * We start at the middle. Each step gets farther away in both
3152 * directions. We therefore hit the change in hash value
3153 * nearest to the middle. Note that this loop does not execute for
3156 for (delta = 0; delta < middle; delta++) {
3157 /* Let's check delta earlier than middle */
3158 if (cmp_xe(&entries[middle - delta - 1],
3159 &entries[middle - delta]))
3160 return middle - delta;
3162 /* For even counts, don't walk off the end */
3163 if ((middle + delta + 1) == count)
3166 /* Now try delta past middle */
3167 if (cmp_xe(&entries[middle + delta],
3168 &entries[middle + delta + 1]))
3169 return middle + delta + 1;
3172 /* Every entry had the same hash */
3177 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3178 * first_hash will record the 1st hash of the new bucket.
3180 * Normally half of the xattrs will be moved. But we have to make
3181 * sure that the xattrs with the same hash value are stored in the
3182 * same bucket. If all the xattrs in this bucket have the same hash
3183 * value, the new bucket will be initialized as an empty one and the
3184 * first_hash will be initialized as (hash_value+1).
3186 static int ocfs2_divide_xattr_bucket(struct inode *inode,
3191 int new_bucket_head)
3194 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
3195 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3196 struct ocfs2_xattr_header *xh;
3197 struct ocfs2_xattr_entry *xe;
3198 int blocksize = inode->i_sb->s_blocksize;
3200 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3201 (unsigned long long)blk, (unsigned long long)new_blk);
3203 s_bucket = ocfs2_xattr_bucket_new(inode);
3204 t_bucket = ocfs2_xattr_bucket_new(inode);
3205 if (!s_bucket || !t_bucket) {
3211 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
3217 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
3218 OCFS2_JOURNAL_ACCESS_WRITE);
3225 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3226 * there's no need to read it.
3228 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
3234 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3236 OCFS2_JOURNAL_ACCESS_CREATE :
3237 OCFS2_JOURNAL_ACCESS_WRITE);
3243 xh = bucket_xh(s_bucket);
3244 count = le16_to_cpu(xh->xh_count);
3245 start = ocfs2_xattr_find_divide_pos(xh);
3247 if (start == count) {
3248 xe = &xh->xh_entries[start-1];
3251 * initialized a new empty bucket here.
3252 * The hash value is set as one larger than
3253 * that of the last entry in the previous bucket.
3255 for (i = 0; i < t_bucket->bu_blocks; i++)
3256 memset(bucket_block(t_bucket, i), 0, blocksize);
3258 xh = bucket_xh(t_bucket);
3259 xh->xh_free_start = cpu_to_le16(blocksize);
3260 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3261 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3263 goto set_num_buckets;
3266 /* copy the whole bucket to the new first. */
3267 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3269 /* update the new bucket. */
3270 xh = bucket_xh(t_bucket);
3273 * Calculate the total name/value len and xh_free_start for
3274 * the old bucket first.
3276 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3278 for (i = 0; i < start; i++) {
3279 xe = &xh->xh_entries[i];
3280 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3281 if (ocfs2_xattr_is_local(xe))
3283 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3285 xe_len += OCFS2_XATTR_ROOT_SIZE;
3286 name_value_len += xe_len;
3287 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3288 name_offset = le16_to_cpu(xe->xe_name_offset);
3292 * Now begin the modification to the new bucket.
3294 * In the new bucket, We just move the xattr entry to the beginning
3295 * and don't touch the name/value. So there will be some holes in the
3296 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3299 xe = &xh->xh_entries[start];
3300 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3301 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3302 (int)((char *)xe - (char *)xh),
3303 (int)((char *)xh->xh_entries - (char *)xh));
3304 memmove((char *)xh->xh_entries, (char *)xe, len);
3305 xe = &xh->xh_entries[count - start];
3306 len = sizeof(struct ocfs2_xattr_entry) * start;
3307 memset((char *)xe, 0, len);
3309 le16_add_cpu(&xh->xh_count, -start);
3310 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3312 /* Calculate xh_free_start for the new bucket. */
3313 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3314 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3315 xe = &xh->xh_entries[i];
3316 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3317 if (ocfs2_xattr_is_local(xe))
3319 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3321 xe_len += OCFS2_XATTR_ROOT_SIZE;
3322 if (le16_to_cpu(xe->xe_name_offset) <
3323 le16_to_cpu(xh->xh_free_start))
3324 xh->xh_free_start = xe->xe_name_offset;
3328 /* set xh->xh_num_buckets for the new xh. */
3329 if (new_bucket_head)
3330 xh->xh_num_buckets = cpu_to_le16(1);
3332 xh->xh_num_buckets = 0;
3334 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3336 /* store the first_hash of the new bucket. */
3338 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3341 * Now only update the 1st block of the old bucket. If we
3342 * just added a new empty bucket, there is no need to modify
3348 xh = bucket_xh(s_bucket);
3349 memset(&xh->xh_entries[start], 0,
3350 sizeof(struct ocfs2_xattr_entry) * (count - start));
3351 xh->xh_count = cpu_to_le16(start);
3352 xh->xh_free_start = cpu_to_le16(name_offset);
3353 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3355 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
3358 ocfs2_xattr_bucket_free(s_bucket);
3359 ocfs2_xattr_bucket_free(t_bucket);
3365 * Copy xattr from one bucket to another bucket.
3367 * The caller must make sure that the journal transaction
3368 * has enough space for journaling.
3370 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3377 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3379 BUG_ON(s_blkno == t_blkno);
3381 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3382 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3385 s_bucket = ocfs2_xattr_bucket_new(inode);
3386 t_bucket = ocfs2_xattr_bucket_new(inode);
3387 if (!s_bucket || !t_bucket) {
3393 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
3398 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3399 * there's no need to read it.
3401 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
3405 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3407 OCFS2_JOURNAL_ACCESS_CREATE :
3408 OCFS2_JOURNAL_ACCESS_WRITE);
3412 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3413 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3416 ocfs2_xattr_bucket_free(t_bucket);
3417 ocfs2_xattr_bucket_free(s_bucket);
3423 * Copy one xattr cluster from src_blk to to_blk.
3424 * The to_blk will become the first bucket header of the cluster, so its
3425 * xh_num_buckets will be initialized as the bucket num in the cluster.
3427 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3429 struct buffer_head *first_bh,
3434 int i, ret, credits;
3435 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3436 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3437 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3438 struct buffer_head *bh = NULL;
3439 struct ocfs2_xattr_header *xh;
3440 u64 to_blk_start = to_blk;
3442 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3443 (unsigned long long)src_blk, (unsigned long long)to_blk);
3446 * We need to update the new cluster and 1 more for the update of
3447 * the 1st bucket of the previous extent rec.
3450 ret = ocfs2_extend_trans(handle, credits);
3456 ret = ocfs2_journal_access(handle, inode, first_bh,
3457 OCFS2_JOURNAL_ACCESS_WRITE);
3463 for (i = 0; i < num_buckets; i++) {
3464 ret = ocfs2_cp_xattr_bucket(inode, handle,
3465 src_blk, to_blk, 1);
3471 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3472 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3475 /* update the old bucket header. */
3476 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3477 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3479 ocfs2_journal_dirty(handle, first_bh);
3481 /* update the new bucket header. */
3482 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3488 ret = ocfs2_journal_access(handle, inode, bh,
3489 OCFS2_JOURNAL_ACCESS_WRITE);
3495 xh = (struct ocfs2_xattr_header *)bh->b_data;
3496 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3498 ocfs2_journal_dirty(handle, bh);
3501 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3508 * Move some xattrs in this cluster to the new cluster.
3509 * This function should only be called when bucket size == cluster size.
3510 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3512 static int ocfs2_divide_xattr_cluster(struct inode *inode,
3518 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3519 int ret, credits = 2 * blk_per_bucket;
3521 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3523 ret = ocfs2_extend_trans(handle, credits);
3529 /* Move half of the xattr in start_blk to the next bucket. */
3530 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
3531 new_blk, first_hash, 1);
3535 * Move some xattrs from the old cluster to the new one since they are not
3536 * contiguous in ocfs2 xattr tree.
3538 * new_blk starts a new separate cluster, and we will move some xattrs from
3539 * prev_blk to it. v_start will be set as the first name hash value in this
3540 * new cluster so that it can be used as e_cpos during tree insertion and
3541 * don't collide with our original b-tree operations. first_bh and header_bh
3542 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3543 * to extend the insert bucket.
3545 * The problem is how much xattr should we move to the new one and when should
3546 * we update first_bh and header_bh?
3547 * 1. If cluster size > bucket size, that means the previous cluster has more
3548 * than 1 bucket, so just move half nums of bucket into the new cluster and
3549 * update the first_bh and header_bh if the insert bucket has been moved
3550 * to the new cluster.
3551 * 2. If cluster_size == bucket_size:
3552 * a) If the previous extent rec has more than one cluster and the insert
3553 * place isn't in the last cluster, copy the entire last cluster to the
3554 * new one. This time, we don't need to upate the first_bh and header_bh
3555 * since they will not be moved into the new cluster.
3556 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3557 * the new one. And we set the extend flag to zero if the insert place is
3558 * moved into the new allocated cluster since no extend is needed.
3560 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3562 struct buffer_head **first_bh,
3563 struct buffer_head **header_bh,
3571 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3573 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3574 (unsigned long long)prev_blk, prev_clusters,
3575 (unsigned long long)new_blk);
3577 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3578 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3587 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3589 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3590 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3594 ret = ocfs2_divide_xattr_cluster(inode, handle,
3598 if ((*header_bh)->b_blocknr == last_blk && extend)
3607 * Add a new cluster for xattr storage.
3609 * If the new cluster is contiguous with the previous one, it will be
3610 * appended to the same extent record, and num_clusters will be updated.
3611 * If not, we will insert a new extent for it and move some xattrs in
3612 * the last cluster into the new allocated one.
3613 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3614 * lose the benefits of hashing because we'll have to search large leaves.
3615 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3618 * first_bh is the first block of the previous extent rec and header_bh
3619 * indicates the bucket we will insert the new xattrs. They will be updated
3620 * when the header_bh is moved into the new cluster.
3622 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3623 struct buffer_head *root_bh,
3624 struct buffer_head **first_bh,
3625 struct buffer_head **header_bh,
3632 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3633 u32 prev_clusters = *num_clusters;
3634 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3636 handle_t *handle = NULL;
3637 struct ocfs2_alloc_context *data_ac = NULL;
3638 struct ocfs2_alloc_context *meta_ac = NULL;
3639 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3640 struct ocfs2_extent_tree et;
3642 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3643 "previous xattr blkno = %llu\n",
3644 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3645 prev_cpos, (unsigned long long)prev_blkno);
3647 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3649 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3650 &data_ac, &meta_ac);
3656 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3658 handle = ocfs2_start_trans(osb, credits);
3659 if (IS_ERR(handle)) {
3660 ret = PTR_ERR(handle);
3666 ret = ocfs2_journal_access(handle, inode, root_bh,
3667 OCFS2_JOURNAL_ACCESS_WRITE);
3673 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3674 clusters_to_add, &bit_off, &num_bits);
3681 BUG_ON(num_bits > clusters_to_add);
3683 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3684 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3685 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3687 if (prev_blkno + prev_clusters * bpc == block &&
3688 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3689 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3691 * If this cluster is contiguous with the old one and
3692 * adding this new cluster, we don't surpass the limit of
3693 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3694 * initialized and used like other buckets in the previous
3696 * So add it as a contiguous one. The caller will handle
3699 v_start = prev_cpos + prev_clusters;
3700 *num_clusters = prev_clusters + num_bits;
3701 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3704 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3719 if (handle->h_buffer_credits < credits) {
3721 * The journal has been restarted before, and don't
3722 * have enough space for the insertion, so extend it
3725 ret = ocfs2_extend_trans(handle, credits);
3731 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3732 num_bits, (unsigned long long)block, v_start);
3733 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3734 num_bits, 0, meta_ac);
3740 ret = ocfs2_journal_dirty(handle, root_bh);
3748 ocfs2_commit_trans(osb, handle);
3750 ocfs2_free_alloc_context(data_ac);
3752 ocfs2_free_alloc_context(meta_ac);
3758 * Extend a new xattr bucket and move xattrs to the end one by one until
3759 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3761 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3762 struct buffer_head *first_bh,
3763 struct buffer_head *start_bh,
3767 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3768 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3769 u64 start_blk = start_bh->b_blocknr, end_blk;
3770 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3772 struct ocfs2_xattr_header *first_xh =
3773 (struct ocfs2_xattr_header *)first_bh->b_data;
3774 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3776 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3777 "from %llu, len = %u\n", (unsigned long long)start_blk,
3778 (unsigned long long)first_bh->b_blocknr, num_clusters);
3780 BUG_ON(bucket >= num_buckets);
3782 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3785 * We will touch all the buckets after the start_bh(include it).
3786 * Then we add one more bucket.
3788 credits = end_blk - start_blk + 3 * blk_per_bucket + 1;
3789 handle = ocfs2_start_trans(osb, credits);
3790 if (IS_ERR(handle)) {
3791 ret = PTR_ERR(handle);
3797 ret = ocfs2_journal_access(handle, inode, first_bh,
3798 OCFS2_JOURNAL_ACCESS_WRITE);
3804 while (end_blk != start_blk) {
3805 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3806 end_blk + blk_per_bucket, 0);
3809 end_blk -= blk_per_bucket;
3812 /* Move half of the xattr in start_blk to the next bucket. */
3813 ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
3814 start_blk + blk_per_bucket, NULL, 0);
3816 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3817 ocfs2_journal_dirty(handle, first_bh);
3820 ocfs2_commit_trans(osb, handle);
3826 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3827 * xb_bh is the ocfs2_xattr_block.
3828 * We will move all the buckets starting from header_bh to the next place. As
3829 * for this one, half num of its xattrs will be moved to the next one.
3831 * We will allocate a new cluster if current cluster is full and adjust
3832 * header_bh and first_bh if the insert place is moved to the new cluster.
3834 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3835 struct buffer_head *xb_bh,
3836 struct buffer_head *header_bh)
3838 struct ocfs2_xattr_header *first_xh = NULL;
3839 struct buffer_head *first_bh = NULL;
3840 struct ocfs2_xattr_block *xb =
3841 (struct ocfs2_xattr_block *)xb_bh->b_data;
3842 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3843 struct ocfs2_extent_list *el = &xb_root->xt_list;
3844 struct ocfs2_xattr_header *xh =
3845 (struct ocfs2_xattr_header *)header_bh->b_data;
3846 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3847 struct super_block *sb = inode->i_sb;
3848 struct ocfs2_super *osb = OCFS2_SB(sb);
3849 int ret, num_buckets, extend = 1;
3851 u32 e_cpos, num_clusters;
3853 mlog(0, "Add new xattr bucket starting form %llu\n",
3854 (unsigned long long)header_bh->b_blocknr);
3857 * Add refrence for header_bh here because it may be
3858 * changed in ocfs2_add_new_xattr_cluster and we need
3859 * to free it in the end.
3863 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3870 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
3876 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3877 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3879 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3880 ret = ocfs2_add_new_xattr_cluster(inode,
3895 ret = ocfs2_extend_xattr_bucket(inode,
3907 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3908 struct ocfs2_xattr_bucket *bucket,
3911 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3913 offs = offs % inode->i_sb->s_blocksize;
3914 return bucket_block(bucket, block_off) + offs;
3918 * Handle the normal xattr set, including replace, delete and new.
3920 * Note: "local" indicates the real data's locality. So we can't
3921 * just its bucket locality by its length.
3923 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3924 struct ocfs2_xattr_info *xi,
3925 struct ocfs2_xattr_search *xs,
3929 struct ocfs2_xattr_entry *last, *xe;
3930 int name_len = strlen(xi->name);
3931 struct ocfs2_xattr_header *xh = xs->header;
3932 u16 count = le16_to_cpu(xh->xh_count), start;
3933 size_t blocksize = inode->i_sb->s_blocksize;
3935 size_t offs, size, new_size;
3937 last = &xh->xh_entries[count];
3938 if (!xs->not_found) {
3940 offs = le16_to_cpu(xe->xe_name_offset);
3941 if (ocfs2_xattr_is_local(xe))
3942 size = OCFS2_XATTR_SIZE(name_len) +
3943 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3945 size = OCFS2_XATTR_SIZE(name_len) +
3946 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3949 * If the new value will be stored outside, xi->value has been
3950 * initalized as an empty ocfs2_xattr_value_root, and the same
3951 * goes with xi->value_len, so we can set new_size safely here.
3952 * See ocfs2_xattr_set_in_bucket.
3954 new_size = OCFS2_XATTR_SIZE(name_len) +
3955 OCFS2_XATTR_SIZE(xi->value_len);
3957 le16_add_cpu(&xh->xh_name_value_len, -size);
3959 if (new_size > size)
3960 goto set_new_name_value;
3962 /* Now replace the old value with new one. */
3964 xe->xe_value_size = cpu_to_le64(xi->value_len);
3966 xe->xe_value_size = 0;
3968 val = ocfs2_xattr_bucket_get_val(inode,
3970 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3971 size - OCFS2_XATTR_SIZE(name_len));
3972 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3973 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3974 xi->value, xi->value_len);
3976 le16_add_cpu(&xh->xh_name_value_len, new_size);
3977 ocfs2_xattr_set_local(xe, local);
3981 * Remove the old entry if there is more than one.
3982 * We don't remove the last entry so that we can
3983 * use it to indicate the hash value of the empty
3987 le16_add_cpu(&xh->xh_count, -1);
3990 (void *)last - (void *)xe);
3992 sizeof(struct ocfs2_xattr_entry));
3995 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4000 /* find a new entry for insert. */
4001 int low = 0, high = count - 1, tmp;
4002 struct ocfs2_xattr_entry *tmp_xe;
4004 while (low <= high && count) {
4005 tmp = (low + high) / 2;
4006 tmp_xe = &xh->xh_entries[tmp];
4008 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4010 else if (name_hash <
4011 le32_to_cpu(tmp_xe->xe_name_hash))
4019 xe = &xh->xh_entries[low];
4021 memmove(xe + 1, xe, (void *)last - (void *)xe);
4023 le16_add_cpu(&xh->xh_count, 1);
4024 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4025 xe->xe_name_hash = cpu_to_le32(name_hash);
4026 xe->xe_name_len = name_len;
4027 ocfs2_xattr_set_type(xe, xi->name_index);
4031 /* Insert the new name+value. */
4032 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4035 * We must make sure that the name/value pair
4036 * exists in the same block.
4038 offs = le16_to_cpu(xh->xh_free_start);
4039 start = offs - size;
4041 if (start >> inode->i_sb->s_blocksize_bits !=
4042 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4043 offs = offs - offs % blocksize;
4044 xh->xh_free_start = cpu_to_le16(offs);
4047 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
4048 xe->xe_name_offset = cpu_to_le16(offs - size);
4050 memset(val, 0, size);
4051 memcpy(val, xi->name, name_len);
4052 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4054 xe->xe_value_size = cpu_to_le64(xi->value_len);
4055 ocfs2_xattr_set_local(xe, local);
4057 le16_add_cpu(&xh->xh_free_start, -size);
4058 le16_add_cpu(&xh->xh_name_value_len, size);
4064 * Set the xattr entry in the specified bucket.
4065 * The bucket is indicated by xs->bucket and it should have the enough
4066 * space for the xattr insertion.
4068 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4069 struct ocfs2_xattr_info *xi,
4070 struct ocfs2_xattr_search *xs,
4075 handle_t *handle = NULL;
4076 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4079 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4080 (unsigned long)xi->value_len, xi->name_index,
4081 (unsigned long long)bucket_blkno(xs->bucket));
4083 if (!xs->bucket->bu_bhs[1]) {
4084 blkno = bucket_blkno(xs->bucket);
4085 ocfs2_xattr_bucket_relse(xs->bucket);
4086 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
4093 handle = ocfs2_start_trans(osb, xs->bucket->bu_blocks);
4094 if (IS_ERR(handle)) {
4095 ret = PTR_ERR(handle);
4101 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4102 OCFS2_JOURNAL_ACCESS_WRITE);
4108 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4109 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4112 ocfs2_commit_trans(osb, handle);
4117 static int ocfs2_xattr_value_update_size(struct inode *inode,
4118 struct buffer_head *xe_bh,
4119 struct ocfs2_xattr_entry *xe,
4123 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4124 handle_t *handle = NULL;
4126 handle = ocfs2_start_trans(osb, 1);
4127 if (IS_ERR(handle)) {
4133 ret = ocfs2_journal_access(handle, inode, xe_bh,
4134 OCFS2_JOURNAL_ACCESS_WRITE);
4140 xe->xe_value_size = cpu_to_le64(new_size);
4142 ret = ocfs2_journal_dirty(handle, xe_bh);
4147 ocfs2_commit_trans(osb, handle);
4153 * Truncate the specified xe_off entry in xattr bucket.
4154 * bucket is indicated by header_bh and len is the new length.
4155 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4157 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4159 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4160 struct buffer_head *header_bh,
4166 struct buffer_head *value_bh = NULL;
4167 struct ocfs2_xattr_value_root *xv;
4168 struct ocfs2_xattr_entry *xe;
4169 struct ocfs2_xattr_header *xh =
4170 (struct ocfs2_xattr_header *)header_bh->b_data;
4171 size_t blocksize = inode->i_sb->s_blocksize;
4173 xe = &xh->xh_entries[xe_off];
4175 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4177 offset = le16_to_cpu(xe->xe_name_offset) +
4178 OCFS2_XATTR_SIZE(xe->xe_name_len);
4180 value_blk = offset / blocksize;
4182 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4183 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4184 value_blk += header_bh->b_blocknr;
4186 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4192 xv = (struct ocfs2_xattr_value_root *)
4193 (value_bh->b_data + offset % blocksize);
4195 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4196 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4197 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4203 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4214 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4215 struct ocfs2_xattr_search *xs,
4219 struct ocfs2_xattr_entry *xe = xs->here;
4220 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4222 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4224 offset = xe - xh->xh_entries;
4225 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0],
4233 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4234 struct ocfs2_xattr_search *xs,
4239 struct ocfs2_xattr_value_root *xv;
4240 struct ocfs2_xattr_entry *xe = xs->here;
4242 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4244 offset = le16_to_cpu(xe->xe_name_offset) +
4245 OCFS2_XATTR_SIZE(xe->xe_name_len);
4247 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4249 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4252 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4253 struct buffer_head *root_bh,
4259 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4260 struct inode *tl_inode = osb->osb_tl_inode;
4262 struct ocfs2_xattr_block *xb =
4263 (struct ocfs2_xattr_block *)root_bh->b_data;
4264 struct ocfs2_alloc_context *meta_ac = NULL;
4265 struct ocfs2_cached_dealloc_ctxt dealloc;
4266 struct ocfs2_extent_tree et;
4268 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4270 ocfs2_init_dealloc_ctxt(&dealloc);
4272 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4273 cpos, len, (unsigned long long)blkno);
4275 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4277 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4283 mutex_lock(&tl_inode->i_mutex);
4285 if (ocfs2_truncate_log_needs_flush(osb)) {
4286 ret = __ocfs2_flush_truncate_log(osb);
4293 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4294 if (IS_ERR(handle)) {
4300 ret = ocfs2_journal_access(handle, inode, root_bh,
4301 OCFS2_JOURNAL_ACCESS_WRITE);
4307 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4314 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4316 ret = ocfs2_journal_dirty(handle, root_bh);
4322 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4327 ocfs2_commit_trans(osb, handle);
4329 ocfs2_schedule_truncate_log_flush(osb, 1);
4331 mutex_unlock(&tl_inode->i_mutex);
4334 ocfs2_free_alloc_context(meta_ac);
4336 ocfs2_run_deallocs(osb, &dealloc);
4341 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4342 struct ocfs2_xattr_search *xs)
4344 handle_t *handle = NULL;
4345 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
4346 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4347 le16_to_cpu(xh->xh_count) - 1];
4350 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
4351 ocfs2_blocks_per_xattr_bucket(inode->i_sb));
4352 if (IS_ERR(handle)) {
4353 ret = PTR_ERR(handle);
4358 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4359 OCFS2_JOURNAL_ACCESS_WRITE);
4365 /* Remove the old entry. */
4366 memmove(xs->here, xs->here + 1,
4367 (void *)last - (void *)xs->here);
4368 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4369 le16_add_cpu(&xh->xh_count, -1);
4371 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4374 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4378 * Set the xattr name/value in the bucket specified in xs.
4380 * As the new value in xi may be stored in the bucket or in an outside cluster,
4381 * we divide the whole process into 3 steps:
4382 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4383 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4384 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4385 * 4. If the clusters for the new outside value can't be allocated, we need
4386 * to free the xattr we allocated in set.
4388 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4389 struct ocfs2_xattr_info *xi,
4390 struct ocfs2_xattr_search *xs)
4394 char *val = (char *)xi->value;
4395 struct ocfs2_xattr_entry *xe = xs->here;
4396 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4399 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4401 * We need to truncate the xattr storage first.
4403 * If both the old and new value are stored to
4404 * outside block, we only need to truncate
4405 * the storage and then set the value outside.
4407 * If the new value should be stored within block,
4408 * we should free all the outside block first and
4409 * the modification to the xattr block will be done
4410 * by following steps.
4412 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4413 value_len = xi->value_len;
4417 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4423 goto set_value_outside;
4426 value_len = xi->value_len;
4427 /* So we have to handle the inside block change now. */
4428 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4430 * If the new value will be stored outside of block,
4431 * initalize a new empty value root and insert it first.
4434 xi->value = &def_xv;
4435 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4438 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4444 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4447 /* allocate the space now for the outside block storage. */
4448 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4453 if (xs->not_found) {
4455 * We can't allocate enough clusters for outside
4456 * storage and we have allocated xattr already,
4457 * so need to remove it.
4459 ocfs2_xattr_bucket_remove_xs(inode, xs);
4465 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4471 * check whether the xattr bucket is filled up with the same hash value.
4472 * If we want to insert the xattr with the same hash, return -ENOSPC.
4473 * If we want to insert a xattr with different hash value, go ahead
4474 * and ocfs2_divide_xattr_bucket will handle this.
4476 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4477 struct ocfs2_xattr_bucket *bucket,
4480 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4481 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4483 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4486 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4487 xh->xh_entries[0].xe_name_hash) {
4488 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4490 (unsigned long long)bucket_blkno(bucket),
4491 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4498 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4499 struct ocfs2_xattr_info *xi,
4500 struct ocfs2_xattr_search *xs)
4502 struct ocfs2_xattr_header *xh;
4503 struct ocfs2_xattr_entry *xe;
4504 u16 count, header_size, xh_free_start;
4505 int free, max_free, need, old;
4506 size_t value_size = 0, name_len = strlen(xi->name);
4507 size_t blocksize = inode->i_sb->s_blocksize;
4508 int ret, allocation = 0;
4510 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4514 count = le16_to_cpu(xh->xh_count);
4515 xh_free_start = le16_to_cpu(xh->xh_free_start);
4516 header_size = sizeof(struct ocfs2_xattr_header) +
4517 count * sizeof(struct ocfs2_xattr_entry);
4518 max_free = OCFS2_XATTR_BUCKET_SIZE -
4519 le16_to_cpu(xh->xh_name_value_len) - header_size;
4521 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4522 "of %u which exceed block size\n",
4523 (unsigned long long)bucket_blkno(xs->bucket),
4526 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4527 value_size = OCFS2_XATTR_ROOT_SIZE;
4529 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4532 need = sizeof(struct ocfs2_xattr_entry) +
4533 OCFS2_XATTR_SIZE(name_len) + value_size;
4535 need = value_size + OCFS2_XATTR_SIZE(name_len);
4538 * We only replace the old value if the new length is smaller
4539 * than the old one. Otherwise we will allocate new space in the
4540 * bucket to store it.
4543 if (ocfs2_xattr_is_local(xe))
4544 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4546 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4548 if (old >= value_size)
4552 free = xh_free_start - header_size;
4554 * We need to make sure the new name/value pair
4555 * can exist in the same block.
4557 if (xh_free_start % blocksize < need)
4558 free -= xh_free_start % blocksize;
4560 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4561 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4562 " %u\n", xs->not_found,
4563 (unsigned long long)bucket_blkno(xs->bucket),
4564 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4565 le16_to_cpu(xh->xh_name_value_len));
4567 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4568 if (need <= max_free &&
4569 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4571 * We can create the space by defragment. Since only the
4572 * name/value will be moved, the xe shouldn't be changed
4575 ret = ocfs2_defrag_xattr_bucket(inode, xs->bucket);
4581 xh_free_start = le16_to_cpu(xh->xh_free_start);
4582 free = xh_free_start - header_size;
4583 if (xh_free_start % blocksize < need)
4584 free -= xh_free_start % blocksize;
4589 mlog(0, "Can't get enough space for xattr insert by "
4590 "defragment. Need %u bytes, but we have %d, so "
4591 "allocate new bucket for it.\n", need, free);
4595 * We have to add new buckets or clusters and one
4596 * allocation should leave us enough space for insert.
4601 * We do not allow for overlapping ranges between buckets. And
4602 * the maximum number of collisions we will allow for then is
4603 * one bucket's worth, so check it here whether we need to
4604 * add a new bucket for the insert.
4606 ret = ocfs2_check_xattr_bucket_collision(inode,
4614 ret = ocfs2_add_new_xattr_bucket(inode,
4616 xs->bucket->bu_bhs[0]);
4622 ocfs2_xattr_bucket_relse(xs->bucket);
4624 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4627 if (ret && ret != -ENODATA)
4629 xs->not_found = ret;
4635 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4641 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4642 struct ocfs2_xattr_bucket *bucket,
4646 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4648 struct ocfs2_xattr_entry *xe;
4650 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4651 xe = &xh->xh_entries[i];
4652 if (ocfs2_xattr_is_local(xe))
4655 ret = ocfs2_xattr_bucket_value_truncate(inode,
4667 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4668 struct buffer_head *xb_bh)
4670 struct ocfs2_xattr_block *xb =
4671 (struct ocfs2_xattr_block *)xb_bh->b_data;
4672 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4674 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4677 if (le16_to_cpu(el->l_next_free_rec) == 0)
4680 while (name_hash > 0) {
4681 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4682 &e_cpos, &num_clusters, el);
4688 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4689 ocfs2_delete_xattr_in_bucket,
4696 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4697 p_blkno, e_cpos, num_clusters);
4706 name_hash = e_cpos - 1;
4714 * 'trusted' attributes support
4716 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4717 size_t list_size, const char *name,
4720 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
4721 const size_t total_len = prefix_len + name_len + 1;
4723 if (list && total_len <= list_size) {
4724 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4725 memcpy(list + prefix_len, name, name_len);
4726 list[prefix_len + name_len] = '\0';
4731 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4732 void *buffer, size_t size)
4734 if (strcmp(name, "") == 0)
4736 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4740 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4741 const void *value, size_t size, int flags)
4743 if (strcmp(name, "") == 0)
4746 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4750 struct xattr_handler ocfs2_xattr_trusted_handler = {
4751 .prefix = XATTR_TRUSTED_PREFIX,
4752 .list = ocfs2_xattr_trusted_list,
4753 .get = ocfs2_xattr_trusted_get,
4754 .set = ocfs2_xattr_trusted_set,
4758 * 'user' attributes support
4760 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4761 size_t list_size, const char *name,
4764 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
4765 const size_t total_len = prefix_len + name_len + 1;
4766 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4768 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4771 if (list && total_len <= list_size) {
4772 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4773 memcpy(list + prefix_len, name, name_len);
4774 list[prefix_len + name_len] = '\0';
4779 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4780 void *buffer, size_t size)
4782 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4784 if (strcmp(name, "") == 0)
4786 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4788 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4792 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4793 const void *value, size_t size, int flags)
4795 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4797 if (strcmp(name, "") == 0)
4799 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4802 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4806 struct xattr_handler ocfs2_xattr_user_handler = {
4807 .prefix = XATTR_USER_PREFIX,
4808 .list = ocfs2_xattr_user_list,
4809 .get = ocfs2_xattr_user_get,
4810 .set = ocfs2_xattr_user_set,