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>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
54 #include "buffer_head_io.h"
59 struct ocfs2_xattr_def_value_root {
60 struct ocfs2_xattr_value_root xv;
61 struct ocfs2_extent_rec er;
64 struct ocfs2_xattr_bucket {
65 /* The inode these xattrs are associated with */
66 struct inode *bu_inode;
68 /* The actual buffers that make up the bucket */
69 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
71 /* How many blocks make up one bucket for this filesystem */
75 struct ocfs2_xattr_set_ctxt {
77 struct ocfs2_alloc_context *meta_ac;
78 struct ocfs2_alloc_context *data_ac;
79 struct ocfs2_cached_dealloc_ctxt dealloc;
82 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
83 #define OCFS2_XATTR_INLINE_SIZE 80
84 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
85 - sizeof(struct ocfs2_xattr_header) \
87 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
88 - sizeof(struct ocfs2_xattr_block) \
89 - sizeof(struct ocfs2_xattr_header) \
92 static struct ocfs2_xattr_def_value_root def_xv = {
93 .xv.xr_list.l_count = cpu_to_le16(1),
96 struct xattr_handler *ocfs2_xattr_handlers[] = {
97 &ocfs2_xattr_user_handler,
98 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
99 &ocfs2_xattr_acl_access_handler,
100 &ocfs2_xattr_acl_default_handler,
102 &ocfs2_xattr_trusted_handler,
103 &ocfs2_xattr_security_handler,
107 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
108 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
109 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
115 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
116 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
119 struct ocfs2_xattr_info {
126 struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
134 struct ocfs2_xattr_bucket *bucket;
137 struct ocfs2_xattr_entry *here;
141 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
142 struct ocfs2_xattr_header *xh,
147 static int ocfs2_xattr_block_find(struct inode *inode,
150 struct ocfs2_xattr_search *xs);
151 static int ocfs2_xattr_index_block_find(struct inode *inode,
152 struct buffer_head *root_bh,
155 struct ocfs2_xattr_search *xs);
157 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
158 struct ocfs2_xattr_tree_root *xt,
162 static int ocfs2_xattr_create_index_block(struct inode *inode,
163 struct ocfs2_xattr_search *xs,
164 struct ocfs2_xattr_set_ctxt *ctxt);
166 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
167 struct ocfs2_xattr_info *xi,
168 struct ocfs2_xattr_search *xs,
169 struct ocfs2_xattr_set_ctxt *ctxt);
171 static int ocfs2_delete_xattr_index_block(struct inode *inode,
172 struct buffer_head *xb_bh);
174 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
176 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
179 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
181 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
184 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
186 u16 len = sb->s_blocksize -
187 offsetof(struct ocfs2_xattr_header, xh_entries);
189 return len / sizeof(struct ocfs2_xattr_entry);
192 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
193 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
194 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
196 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
198 struct ocfs2_xattr_bucket *bucket;
199 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
201 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
203 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
205 bucket->bu_inode = inode;
206 bucket->bu_blocks = blks;
212 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
216 for (i = 0; i < bucket->bu_blocks; i++) {
217 brelse(bucket->bu_bhs[i]);
218 bucket->bu_bhs[i] = NULL;
222 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
225 ocfs2_xattr_bucket_relse(bucket);
226 bucket->bu_inode = NULL;
232 * A bucket that has never been written to disk doesn't need to be
233 * read. We just need the buffer_heads. Don't call this for
234 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
237 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
242 for (i = 0; i < bucket->bu_blocks; i++) {
243 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
245 if (!bucket->bu_bhs[i]) {
251 if (!ocfs2_buffer_uptodate(bucket->bu_inode,
253 ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
258 ocfs2_xattr_bucket_relse(bucket);
262 /* Read the xattr bucket at xb_blkno */
263 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
268 rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
269 bucket->bu_blocks, bucket->bu_bhs, 0,
272 ocfs2_xattr_bucket_relse(bucket);
276 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
277 struct ocfs2_xattr_bucket *bucket,
282 for (i = 0; i < bucket->bu_blocks; i++) {
283 rc = ocfs2_journal_access(handle, bucket->bu_inode,
284 bucket->bu_bhs[i], type);
294 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
295 struct ocfs2_xattr_bucket *bucket)
299 for (i = 0; i < bucket->bu_blocks; i++)
300 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
303 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
304 struct ocfs2_xattr_bucket *src)
307 int blocksize = src->bu_inode->i_sb->s_blocksize;
309 BUG_ON(dest->bu_blocks != src->bu_blocks);
310 BUG_ON(dest->bu_inode != src->bu_inode);
312 for (i = 0; i < src->bu_blocks; i++) {
313 memcpy(bucket_block(dest, i), bucket_block(src, i),
318 static int ocfs2_validate_xattr_block(struct super_block *sb,
319 struct buffer_head *bh)
321 struct ocfs2_xattr_block *xb =
322 (struct ocfs2_xattr_block *)bh->b_data;
324 mlog(0, "Validating xattr block %llu\n",
325 (unsigned long long)bh->b_blocknr);
327 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
329 "Extended attribute block #%llu has bad "
331 (unsigned long long)bh->b_blocknr, 7,
336 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
338 "Extended attribute block #%llu has an "
339 "invalid xb_blkno of %llu",
340 (unsigned long long)bh->b_blocknr,
341 (unsigned long long)le64_to_cpu(xb->xb_blkno));
345 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
347 "Extended attribute block #%llu has an invalid "
348 "xb_fs_generation of #%u",
349 (unsigned long long)bh->b_blocknr,
350 le32_to_cpu(xb->xb_fs_generation));
357 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
358 struct buffer_head **bh)
361 struct buffer_head *tmp = *bh;
363 rc = ocfs2_read_block(inode, xb_blkno, &tmp,
364 ocfs2_validate_xattr_block);
366 /* If ocfs2_read_block() got us a new bh, pass it up. */
373 static inline const char *ocfs2_xattr_prefix(int name_index)
375 struct xattr_handler *handler = NULL;
377 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
378 handler = ocfs2_xattr_handler_map[name_index];
380 return handler ? handler->prefix : NULL;
383 static u32 ocfs2_xattr_name_hash(struct inode *inode,
387 /* Get hash value of uuid from super block */
388 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
391 /* hash extended attribute name */
392 for (i = 0; i < name_len; i++) {
393 hash = (hash << OCFS2_HASH_SHIFT) ^
394 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
402 * ocfs2_xattr_hash_entry()
404 * Compute the hash of an extended attribute.
406 static void ocfs2_xattr_hash_entry(struct inode *inode,
407 struct ocfs2_xattr_header *header,
408 struct ocfs2_xattr_entry *entry)
411 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
413 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
414 entry->xe_name_hash = cpu_to_le32(hash);
419 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
423 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
424 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
426 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
427 size += sizeof(struct ocfs2_xattr_entry);
432 int ocfs2_calc_security_init(struct inode *dir,
433 struct ocfs2_security_xattr_info *si,
436 struct ocfs2_alloc_context **xattr_ac)
439 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
440 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
444 * The max space of security xattr taken inline is
445 * 256(name) + 80(value) + 16(entry) = 352 bytes,
446 * So reserve one metadata block for it is ok.
448 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
449 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
450 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
455 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
458 /* reserve clusters for xattr value which will be set in B tree*/
459 if (si->value_len > OCFS2_XATTR_INLINE_SIZE)
460 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
465 int ocfs2_calc_xattr_init(struct inode *dir,
466 struct buffer_head *dir_bh,
468 struct ocfs2_security_xattr_info *si,
471 struct ocfs2_alloc_context **xattr_ac)
474 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
480 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
483 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
484 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
485 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
488 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
491 } else if (acl_len != 0 && acl_len != -ENODATA) {
497 if (!(s_size + a_size))
501 * The max space of security xattr taken inline is
502 * 256(name) + 80(value) + 16(entry) = 352 bytes,
503 * The max space of acl xattr taken inline is
504 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
505 * when blocksize = 512, may reserve one more cluser for
506 * xattr bucket, otherwise reserve one metadata block
509 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
510 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
511 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
516 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
519 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
520 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
522 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
525 /* reserve clusters for xattr value which will be set in B tree*/
526 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE)
527 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
529 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
530 acl_len > OCFS2_XATTR_INLINE_SIZE) {
531 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
533 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
540 static int ocfs2_xattr_extend_allocation(struct inode *inode,
542 struct buffer_head *xattr_bh,
543 struct ocfs2_xattr_value_root *xv,
544 struct ocfs2_xattr_set_ctxt *ctxt)
547 handle_t *handle = ctxt->handle;
548 enum ocfs2_alloc_restarted why;
549 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
550 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
551 struct ocfs2_extent_tree et;
553 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
555 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
557 status = ocfs2_journal_access(handle, inode, xattr_bh,
558 OCFS2_JOURNAL_ACCESS_WRITE);
564 prev_clusters = le32_to_cpu(xv->xr_clusters);
565 status = ocfs2_add_clusters_in_btree(osb,
580 status = ocfs2_journal_dirty(handle, xattr_bh);
586 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
589 * We should have already allocated enough space before the transaction,
590 * so no need to restart.
592 BUG_ON(why != RESTART_NONE || clusters_to_add);
599 static int __ocfs2_remove_xattr_range(struct inode *inode,
600 struct buffer_head *root_bh,
601 struct ocfs2_xattr_value_root *xv,
602 u32 cpos, u32 phys_cpos, u32 len,
603 struct ocfs2_xattr_set_ctxt *ctxt)
606 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
607 handle_t *handle = ctxt->handle;
608 struct ocfs2_extent_tree et;
610 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
612 ret = ocfs2_journal_access(handle, inode, root_bh,
613 OCFS2_JOURNAL_ACCESS_WRITE);
619 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
626 le32_add_cpu(&xv->xr_clusters, -len);
628 ret = ocfs2_journal_dirty(handle, root_bh);
634 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
642 static int ocfs2_xattr_shrink_size(struct inode *inode,
645 struct buffer_head *root_bh,
646 struct ocfs2_xattr_value_root *xv,
647 struct ocfs2_xattr_set_ctxt *ctxt)
650 u32 trunc_len, cpos, phys_cpos, alloc_size;
653 if (old_clusters <= new_clusters)
657 trunc_len = old_clusters - new_clusters;
659 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
660 &alloc_size, &xv->xr_list);
666 if (alloc_size > trunc_len)
667 alloc_size = trunc_len;
669 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
670 phys_cpos, alloc_size,
677 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
678 ocfs2_remove_xattr_clusters_from_cache(inode, block,
681 trunc_len -= alloc_size;
688 static int ocfs2_xattr_value_truncate(struct inode *inode,
689 struct buffer_head *root_bh,
690 struct ocfs2_xattr_value_root *xv,
692 struct ocfs2_xattr_set_ctxt *ctxt)
695 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
696 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
698 if (new_clusters == old_clusters)
701 if (new_clusters > old_clusters)
702 ret = ocfs2_xattr_extend_allocation(inode,
703 new_clusters - old_clusters,
706 ret = ocfs2_xattr_shrink_size(inode,
707 old_clusters, new_clusters,
713 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
714 size_t *result, const char *prefix,
715 const char *name, int name_len)
717 char *p = buffer + *result;
718 int prefix_len = strlen(prefix);
719 int total_len = prefix_len + name_len + 1;
721 *result += total_len;
723 /* we are just looking for how big our buffer needs to be */
730 memcpy(p, prefix, prefix_len);
731 memcpy(p + prefix_len, name, name_len);
732 p[prefix_len + name_len] = '\0';
737 static int ocfs2_xattr_list_entries(struct inode *inode,
738 struct ocfs2_xattr_header *header,
739 char *buffer, size_t buffer_size)
743 const char *prefix, *name;
745 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
746 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
747 type = ocfs2_xattr_get_type(entry);
748 prefix = ocfs2_xattr_prefix(type);
751 name = (const char *)header +
752 le16_to_cpu(entry->xe_name_offset);
754 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
755 &result, prefix, name,
765 static int ocfs2_xattr_ibody_list(struct inode *inode,
766 struct ocfs2_dinode *di,
770 struct ocfs2_xattr_header *header = NULL;
771 struct ocfs2_inode_info *oi = OCFS2_I(inode);
774 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
777 header = (struct ocfs2_xattr_header *)
778 ((void *)di + inode->i_sb->s_blocksize -
779 le16_to_cpu(di->i_xattr_inline_size));
781 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
786 static int ocfs2_xattr_block_list(struct inode *inode,
787 struct ocfs2_dinode *di,
791 struct buffer_head *blk_bh = NULL;
792 struct ocfs2_xattr_block *xb;
795 if (!di->i_xattr_loc)
798 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
805 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
806 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
807 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
808 ret = ocfs2_xattr_list_entries(inode, header,
809 buffer, buffer_size);
811 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
812 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
813 buffer, buffer_size);
821 ssize_t ocfs2_listxattr(struct dentry *dentry,
825 int ret = 0, i_ret = 0, b_ret = 0;
826 struct buffer_head *di_bh = NULL;
827 struct ocfs2_dinode *di = NULL;
828 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
830 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
833 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
836 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
842 di = (struct ocfs2_dinode *)di_bh->b_data;
844 down_read(&oi->ip_xattr_sem);
845 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
853 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
858 up_read(&oi->ip_xattr_sem);
859 ocfs2_inode_unlock(dentry->d_inode, 0);
863 return i_ret + b_ret;
866 static int ocfs2_xattr_find_entry(int name_index,
868 struct ocfs2_xattr_search *xs)
870 struct ocfs2_xattr_entry *entry;
877 name_len = strlen(name);
879 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
880 cmp = name_index - ocfs2_xattr_get_type(entry);
882 cmp = name_len - entry->xe_name_len;
884 cmp = memcmp(name, (xs->base +
885 le16_to_cpu(entry->xe_name_offset)),
893 return cmp ? -ENODATA : 0;
896 static int ocfs2_xattr_get_value_outside(struct inode *inode,
897 struct ocfs2_xattr_value_root *xv,
901 u32 cpos, p_cluster, num_clusters, bpc, clusters;
904 size_t cplen, blocksize;
905 struct buffer_head *bh = NULL;
906 struct ocfs2_extent_list *el;
909 clusters = le32_to_cpu(xv->xr_clusters);
910 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
911 blocksize = inode->i_sb->s_blocksize;
914 while (cpos < clusters) {
915 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
922 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
923 /* Copy ocfs2_xattr_value */
924 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
925 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
931 cplen = len >= blocksize ? blocksize : len;
932 memcpy(buffer, bh->b_data, cplen);
941 cpos += num_clusters;
947 static int ocfs2_xattr_ibody_get(struct inode *inode,
952 struct ocfs2_xattr_search *xs)
954 struct ocfs2_inode_info *oi = OCFS2_I(inode);
955 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
956 struct ocfs2_xattr_value_root *xv;
960 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
963 xs->end = (void *)di + inode->i_sb->s_blocksize;
964 xs->header = (struct ocfs2_xattr_header *)
965 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
966 xs->base = (void *)xs->header;
967 xs->here = xs->header->xh_entries;
969 ret = ocfs2_xattr_find_entry(name_index, name, xs);
972 size = le64_to_cpu(xs->here->xe_value_size);
974 if (size > buffer_size)
976 if (ocfs2_xattr_is_local(xs->here)) {
977 memcpy(buffer, (void *)xs->base +
978 le16_to_cpu(xs->here->xe_name_offset) +
979 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
981 xv = (struct ocfs2_xattr_value_root *)
982 (xs->base + le16_to_cpu(
983 xs->here->xe_name_offset) +
984 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
985 ret = ocfs2_xattr_get_value_outside(inode, xv,
997 static int ocfs2_xattr_block_get(struct inode *inode,
1002 struct ocfs2_xattr_search *xs)
1004 struct ocfs2_xattr_block *xb;
1005 struct ocfs2_xattr_value_root *xv;
1007 int ret = -ENODATA, name_offset, name_len, block_off, i;
1009 xs->bucket = ocfs2_xattr_bucket_new(inode);
1016 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1022 if (xs->not_found) {
1027 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1028 size = le64_to_cpu(xs->here->xe_value_size);
1031 if (size > buffer_size)
1034 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1035 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1036 i = xs->here - xs->header->xh_entries;
1038 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1039 ret = ocfs2_xattr_bucket_get_name_value(inode,
1040 bucket_xh(xs->bucket),
1044 xs->base = bucket_block(xs->bucket, block_off);
1046 if (ocfs2_xattr_is_local(xs->here)) {
1047 memcpy(buffer, (void *)xs->base +
1048 name_offset + name_len, size);
1050 xv = (struct ocfs2_xattr_value_root *)
1051 (xs->base + name_offset + name_len);
1052 ret = ocfs2_xattr_get_value_outside(inode, xv,
1062 ocfs2_xattr_bucket_free(xs->bucket);
1064 brelse(xs->xattr_bh);
1065 xs->xattr_bh = NULL;
1069 int ocfs2_xattr_get_nolock(struct inode *inode,
1070 struct buffer_head *di_bh,
1077 struct ocfs2_dinode *di = NULL;
1078 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1079 struct ocfs2_xattr_search xis = {
1080 .not_found = -ENODATA,
1082 struct ocfs2_xattr_search xbs = {
1083 .not_found = -ENODATA,
1086 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1089 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1092 xis.inode_bh = xbs.inode_bh = di_bh;
1093 di = (struct ocfs2_dinode *)di_bh->b_data;
1095 down_read(&oi->ip_xattr_sem);
1096 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1098 if (ret == -ENODATA && di->i_xattr_loc)
1099 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1101 up_read(&oi->ip_xattr_sem);
1106 /* ocfs2_xattr_get()
1108 * Copy an extended attribute into the buffer provided.
1109 * Buffer is NULL to compute the size of buffer required.
1111 static int ocfs2_xattr_get(struct inode *inode,
1118 struct buffer_head *di_bh = NULL;
1120 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1125 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1126 name, buffer, buffer_size);
1128 ocfs2_inode_unlock(inode, 0);
1135 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1137 struct ocfs2_xattr_value_root *xv,
1141 int ret = 0, i, cp_len, credits;
1142 u16 blocksize = inode->i_sb->s_blocksize;
1143 u32 p_cluster, num_clusters;
1144 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1145 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1147 struct buffer_head *bh = NULL;
1149 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1152 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1153 * so we don't need to worry about whether ocfs2_extend_trans
1154 * will create a new transactio for us or not.
1156 credits = clusters * bpc;
1157 ret = ocfs2_extend_trans(handle, credits);
1163 while (cpos < clusters) {
1164 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1165 &num_clusters, &xv->xr_list);
1171 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1173 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1174 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
1180 ret = ocfs2_journal_access(handle,
1183 OCFS2_JOURNAL_ACCESS_WRITE);
1189 cp_len = value_len > blocksize ? blocksize : value_len;
1190 memcpy(bh->b_data, value, cp_len);
1191 value_len -= cp_len;
1193 if (cp_len < blocksize)
1194 memset(bh->b_data + cp_len, 0,
1195 blocksize - cp_len);
1197 ret = ocfs2_journal_dirty(handle, bh);
1206 * XXX: do we need to empty all the following
1207 * blocks in this cluster?
1212 cpos += num_clusters;
1220 static int ocfs2_xattr_cleanup(struct inode *inode,
1222 struct ocfs2_xattr_info *xi,
1223 struct ocfs2_xattr_search *xs,
1227 size_t name_len = strlen(xi->name);
1228 void *val = xs->base + offs;
1229 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1231 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1232 OCFS2_JOURNAL_ACCESS_WRITE);
1237 /* Decrease xattr count */
1238 le16_add_cpu(&xs->header->xh_count, -1);
1239 /* Remove the xattr entry and tree root which has already be set*/
1240 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1241 memset(val, 0, size);
1243 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1250 static int ocfs2_xattr_update_entry(struct inode *inode,
1252 struct ocfs2_xattr_info *xi,
1253 struct ocfs2_xattr_search *xs,
1258 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1259 OCFS2_JOURNAL_ACCESS_WRITE);
1265 xs->here->xe_name_offset = cpu_to_le16(offs);
1266 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1267 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1268 ocfs2_xattr_set_local(xs->here, 1);
1270 ocfs2_xattr_set_local(xs->here, 0);
1271 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1273 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1281 * ocfs2_xattr_set_value_outside()
1283 * Set large size value in B tree.
1285 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1286 struct ocfs2_xattr_info *xi,
1287 struct ocfs2_xattr_search *xs,
1288 struct ocfs2_xattr_set_ctxt *ctxt,
1291 size_t name_len = strlen(xi->name);
1292 void *val = xs->base + offs;
1293 struct ocfs2_xattr_value_root *xv = NULL;
1294 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1297 memset(val, 0, size);
1298 memcpy(val, xi->name, name_len);
1299 xv = (struct ocfs2_xattr_value_root *)
1300 (val + OCFS2_XATTR_SIZE(name_len));
1301 xv->xr_clusters = 0;
1302 xv->xr_last_eb_blk = 0;
1303 xv->xr_list.l_tree_depth = 0;
1304 xv->xr_list.l_count = cpu_to_le16(1);
1305 xv->xr_list.l_next_free_rec = 0;
1307 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1308 xi->value_len, ctxt);
1313 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
1318 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1319 xi->value, xi->value_len);
1327 * ocfs2_xattr_set_entry_local()
1329 * Set, replace or remove extended attribute in local.
1331 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1332 struct ocfs2_xattr_info *xi,
1333 struct ocfs2_xattr_search *xs,
1334 struct ocfs2_xattr_entry *last,
1337 size_t name_len = strlen(xi->name);
1340 if (xi->value && xs->not_found) {
1341 /* Insert the new xattr entry. */
1342 le16_add_cpu(&xs->header->xh_count, 1);
1343 ocfs2_xattr_set_type(last, xi->name_index);
1344 ocfs2_xattr_set_local(last, 1);
1345 last->xe_name_len = name_len;
1351 first_val = xs->base + min_offs;
1352 offs = le16_to_cpu(xs->here->xe_name_offset);
1353 val = xs->base + offs;
1355 if (le64_to_cpu(xs->here->xe_value_size) >
1356 OCFS2_XATTR_INLINE_SIZE)
1357 size = OCFS2_XATTR_SIZE(name_len) +
1358 OCFS2_XATTR_ROOT_SIZE;
1360 size = OCFS2_XATTR_SIZE(name_len) +
1361 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1363 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1364 OCFS2_XATTR_SIZE(xi->value_len)) {
1365 /* The old and the new value have the
1366 same size. Just replace the value. */
1367 ocfs2_xattr_set_local(xs->here, 1);
1368 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1369 /* Clear value bytes. */
1370 memset(val + OCFS2_XATTR_SIZE(name_len),
1372 OCFS2_XATTR_SIZE(xi->value_len));
1373 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1378 /* Remove the old name+value. */
1379 memmove(first_val + size, first_val, val - first_val);
1380 memset(first_val, 0, size);
1381 xs->here->xe_name_hash = 0;
1382 xs->here->xe_name_offset = 0;
1383 ocfs2_xattr_set_local(xs->here, 1);
1384 xs->here->xe_value_size = 0;
1388 /* Adjust all value offsets. */
1389 last = xs->header->xh_entries;
1390 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1391 size_t o = le16_to_cpu(last->xe_name_offset);
1394 last->xe_name_offset = cpu_to_le16(o + size);
1399 /* Remove the old entry. */
1401 memmove(xs->here, xs->here + 1,
1402 (void *)last - (void *)xs->here);
1403 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1404 le16_add_cpu(&xs->header->xh_count, -1);
1408 /* Insert the new name+value. */
1409 size_t size = OCFS2_XATTR_SIZE(name_len) +
1410 OCFS2_XATTR_SIZE(xi->value_len);
1411 void *val = xs->base + min_offs - size;
1413 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1414 memset(val, 0, size);
1415 memcpy(val, xi->name, name_len);
1416 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1419 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1420 ocfs2_xattr_set_local(xs->here, 1);
1421 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1428 * ocfs2_xattr_set_entry()
1430 * Set extended attribute entry into inode or block.
1432 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1433 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1434 * then set value in B tree with set_value_outside().
1436 static int ocfs2_xattr_set_entry(struct inode *inode,
1437 struct ocfs2_xattr_info *xi,
1438 struct ocfs2_xattr_search *xs,
1439 struct ocfs2_xattr_set_ctxt *ctxt,
1442 struct ocfs2_xattr_entry *last;
1443 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1444 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1445 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1447 handle_t *handle = ctxt->handle;
1449 struct ocfs2_xattr_info xi_l = {
1450 .name_index = xi->name_index,
1453 .value_len = xi->value_len,
1456 /* Compute min_offs, last and free space. */
1457 last = xs->header->xh_entries;
1459 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1460 size_t offs = le16_to_cpu(last->xe_name_offset);
1461 if (offs < min_offs)
1466 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1470 if (!xs->not_found) {
1472 if (ocfs2_xattr_is_local(xs->here))
1473 size = OCFS2_XATTR_SIZE(name_len) +
1474 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1476 size = OCFS2_XATTR_SIZE(name_len) +
1477 OCFS2_XATTR_ROOT_SIZE;
1478 free += (size + sizeof(struct ocfs2_xattr_entry));
1480 /* Check free space in inode or block */
1481 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1482 if (free < sizeof(struct ocfs2_xattr_entry) +
1483 OCFS2_XATTR_SIZE(name_len) +
1484 OCFS2_XATTR_ROOT_SIZE) {
1488 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1489 xi_l.value = (void *)&def_xv;
1490 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1491 } else if (xi->value) {
1492 if (free < sizeof(struct ocfs2_xattr_entry) +
1493 OCFS2_XATTR_SIZE(name_len) +
1494 OCFS2_XATTR_SIZE(xi->value_len)) {
1500 if (!xs->not_found) {
1501 /* For existing extended attribute */
1502 size_t size = OCFS2_XATTR_SIZE(name_len) +
1503 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1504 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1505 void *val = xs->base + offs;
1507 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1508 /* Replace existing local xattr with tree root */
1509 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1514 } else if (!ocfs2_xattr_is_local(xs->here)) {
1515 /* For existing xattr which has value outside */
1516 struct ocfs2_xattr_value_root *xv = NULL;
1517 xv = (struct ocfs2_xattr_value_root *)(val +
1518 OCFS2_XATTR_SIZE(name_len));
1520 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1522 * If new value need set outside also,
1523 * first truncate old value to new value,
1524 * then set new value with set_value_outside().
1526 ret = ocfs2_xattr_value_truncate(inode,
1536 ret = ocfs2_xattr_update_entry(inode,
1546 ret = __ocfs2_xattr_set_value_outside(inode,
1556 * If new value need set in local,
1557 * just trucate old value to zero.
1559 ret = ocfs2_xattr_value_truncate(inode,
1570 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1571 OCFS2_JOURNAL_ACCESS_WRITE);
1577 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1578 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1579 OCFS2_JOURNAL_ACCESS_WRITE);
1587 * Set value in local, include set tree root in local.
1588 * This is the first step for value size >INLINE_SIZE.
1590 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1592 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1593 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1600 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1601 (flag & OCFS2_INLINE_XATTR_FL)) {
1602 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1603 unsigned int xattrsize = osb->s_xattr_inline_size;
1606 * Adjust extent record count or inline data size
1607 * to reserve space for extended attribute.
1609 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1610 struct ocfs2_inline_data *idata = &di->id2.i_data;
1611 le16_add_cpu(&idata->id_count, -xattrsize);
1612 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1613 struct ocfs2_extent_list *el = &di->id2.i_list;
1614 le16_add_cpu(&el->l_count, -(xattrsize /
1615 sizeof(struct ocfs2_extent_rec)));
1617 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1619 /* Update xattr flag */
1620 spin_lock(&oi->ip_lock);
1621 oi->ip_dyn_features |= flag;
1622 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1623 spin_unlock(&oi->ip_lock);
1624 /* Update inode ctime */
1625 inode->i_ctime = CURRENT_TIME;
1626 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1627 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1629 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1633 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1635 * Set value outside in B tree.
1636 * This is the second step for value size > INLINE_SIZE.
1638 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1639 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
1645 * If set value outside failed, we have to clean
1646 * the junk tree root we have already set in local.
1648 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1658 static int ocfs2_remove_value_outside(struct inode*inode,
1659 struct buffer_head *bh,
1660 struct ocfs2_xattr_header *header)
1663 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1664 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1666 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1668 ctxt.handle = ocfs2_start_trans(osb,
1669 ocfs2_remove_extent_credits(osb->sb));
1670 if (IS_ERR(ctxt.handle)) {
1671 ret = PTR_ERR(ctxt.handle);
1676 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1677 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1679 if (!ocfs2_xattr_is_local(entry)) {
1680 struct ocfs2_xattr_value_root *xv;
1683 val = (void *)header +
1684 le16_to_cpu(entry->xe_name_offset);
1685 xv = (struct ocfs2_xattr_value_root *)
1686 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1687 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1696 ocfs2_commit_trans(osb, ctxt.handle);
1697 ocfs2_schedule_truncate_log_flush(osb, 1);
1698 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1703 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1704 struct buffer_head *di_bh)
1707 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1708 struct ocfs2_xattr_header *header;
1711 header = (struct ocfs2_xattr_header *)
1712 ((void *)di + inode->i_sb->s_blocksize -
1713 le16_to_cpu(di->i_xattr_inline_size));
1715 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1720 static int ocfs2_xattr_block_remove(struct inode *inode,
1721 struct buffer_head *blk_bh)
1723 struct ocfs2_xattr_block *xb;
1726 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1727 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1728 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1729 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1731 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1736 static int ocfs2_xattr_free_block(struct inode *inode,
1739 struct inode *xb_alloc_inode;
1740 struct buffer_head *xb_alloc_bh = NULL;
1741 struct buffer_head *blk_bh = NULL;
1742 struct ocfs2_xattr_block *xb;
1743 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1749 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
1755 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1761 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1762 blk = le64_to_cpu(xb->xb_blkno);
1763 bit = le16_to_cpu(xb->xb_suballoc_bit);
1764 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1766 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1767 EXTENT_ALLOC_SYSTEM_INODE,
1768 le16_to_cpu(xb->xb_suballoc_slot));
1769 if (!xb_alloc_inode) {
1774 mutex_lock(&xb_alloc_inode->i_mutex);
1776 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1782 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1783 if (IS_ERR(handle)) {
1784 ret = PTR_ERR(handle);
1789 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1794 ocfs2_commit_trans(osb, handle);
1796 ocfs2_inode_unlock(xb_alloc_inode, 1);
1797 brelse(xb_alloc_bh);
1799 mutex_unlock(&xb_alloc_inode->i_mutex);
1800 iput(xb_alloc_inode);
1807 * ocfs2_xattr_remove()
1809 * Free extended attribute resources associated with this inode.
1811 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1813 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1814 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1818 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1821 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1824 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1825 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1832 if (di->i_xattr_loc) {
1833 ret = ocfs2_xattr_free_block(inode,
1834 le64_to_cpu(di->i_xattr_loc));
1841 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1842 OCFS2_INODE_UPDATE_CREDITS);
1843 if (IS_ERR(handle)) {
1844 ret = PTR_ERR(handle);
1848 ret = ocfs2_journal_access(handle, inode, di_bh,
1849 OCFS2_JOURNAL_ACCESS_WRITE);
1855 di->i_xattr_loc = 0;
1857 spin_lock(&oi->ip_lock);
1858 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1859 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1860 spin_unlock(&oi->ip_lock);
1862 ret = ocfs2_journal_dirty(handle, di_bh);
1866 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1871 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1872 struct ocfs2_dinode *di)
1874 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1875 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1878 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1881 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1882 struct ocfs2_inline_data *idata = &di->id2.i_data;
1883 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1884 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1885 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1886 le64_to_cpu(di->i_size);
1888 struct ocfs2_extent_list *el = &di->id2.i_list;
1889 free = (le16_to_cpu(el->l_count) -
1890 le16_to_cpu(el->l_next_free_rec)) *
1891 sizeof(struct ocfs2_extent_rec);
1893 if (free >= xattrsize)
1900 * ocfs2_xattr_ibody_find()
1902 * Find extended attribute in inode block and
1903 * fill search info into struct ocfs2_xattr_search.
1905 static int ocfs2_xattr_ibody_find(struct inode *inode,
1908 struct ocfs2_xattr_search *xs)
1910 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1911 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1915 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1918 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1919 down_read(&oi->ip_alloc_sem);
1920 has_space = ocfs2_xattr_has_space_inline(inode, di);
1921 up_read(&oi->ip_alloc_sem);
1926 xs->xattr_bh = xs->inode_bh;
1927 xs->end = (void *)di + inode->i_sb->s_blocksize;
1928 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1929 xs->header = (struct ocfs2_xattr_header *)
1930 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1932 xs->header = (struct ocfs2_xattr_header *)
1933 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1934 xs->base = (void *)xs->header;
1935 xs->here = xs->header->xh_entries;
1937 /* Find the named attribute. */
1938 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1939 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1940 if (ret && ret != -ENODATA)
1942 xs->not_found = ret;
1949 * ocfs2_xattr_ibody_set()
1951 * Set, replace or remove an extended attribute into inode block.
1954 static int ocfs2_xattr_ibody_set(struct inode *inode,
1955 struct ocfs2_xattr_info *xi,
1956 struct ocfs2_xattr_search *xs,
1957 struct ocfs2_xattr_set_ctxt *ctxt)
1959 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1960 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1963 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1966 down_write(&oi->ip_alloc_sem);
1967 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1968 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1974 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
1975 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1977 up_write(&oi->ip_alloc_sem);
1983 * ocfs2_xattr_block_find()
1985 * Find extended attribute in external block and
1986 * fill search info into struct ocfs2_xattr_search.
1988 static int ocfs2_xattr_block_find(struct inode *inode,
1991 struct ocfs2_xattr_search *xs)
1993 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1994 struct buffer_head *blk_bh = NULL;
1995 struct ocfs2_xattr_block *xb;
1998 if (!di->i_xattr_loc)
2001 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2008 xs->xattr_bh = blk_bh;
2009 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2011 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2012 xs->header = &xb->xb_attrs.xb_header;
2013 xs->base = (void *)xs->header;
2014 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2015 xs->here = xs->header->xh_entries;
2017 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2019 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2023 if (ret && ret != -ENODATA) {
2024 xs->xattr_bh = NULL;
2027 xs->not_found = ret;
2036 * ocfs2_xattr_block_set()
2038 * Set, replace or remove an extended attribute into external block.
2041 static int ocfs2_xattr_block_set(struct inode *inode,
2042 struct ocfs2_xattr_info *xi,
2043 struct ocfs2_xattr_search *xs,
2044 struct ocfs2_xattr_set_ctxt *ctxt)
2046 struct buffer_head *new_bh = NULL;
2047 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2048 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2049 handle_t *handle = ctxt->handle;
2050 struct ocfs2_xattr_block *xblk = NULL;
2051 u16 suballoc_bit_start;
2056 if (!xs->xattr_bh) {
2057 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
2058 OCFS2_JOURNAL_ACCESS_CREATE);
2064 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
2065 &suballoc_bit_start, &num_got,
2072 new_bh = sb_getblk(inode->i_sb, first_blkno);
2073 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2075 ret = ocfs2_journal_access(handle, inode, new_bh,
2076 OCFS2_JOURNAL_ACCESS_CREATE);
2082 /* Initialize ocfs2_xattr_block */
2083 xs->xattr_bh = new_bh;
2084 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2085 memset(xblk, 0, inode->i_sb->s_blocksize);
2086 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2087 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2088 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2089 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2090 xblk->xb_blkno = cpu_to_le64(first_blkno);
2092 xs->header = &xblk->xb_attrs.xb_header;
2093 xs->base = (void *)xs->header;
2094 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2095 xs->here = xs->header->xh_entries;
2097 ret = ocfs2_journal_dirty(handle, new_bh);
2102 di->i_xattr_loc = cpu_to_le64(first_blkno);
2103 ocfs2_journal_dirty(handle, xs->inode_bh);
2105 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2107 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2108 /* Set extended attribute into external block */
2109 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2110 OCFS2_HAS_XATTR_FL);
2111 if (!ret || ret != -ENOSPC)
2114 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2119 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2126 /* Check whether the new xattr can be inserted into the inode. */
2127 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2128 struct ocfs2_xattr_info *xi,
2129 struct ocfs2_xattr_search *xs)
2132 struct ocfs2_xattr_entry *last;
2134 size_t min_offs = xs->end - xs->base;
2139 last = xs->header->xh_entries;
2141 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2142 size_t offs = le16_to_cpu(last->xe_name_offset);
2143 if (offs < min_offs)
2148 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
2152 BUG_ON(!xs->not_found);
2154 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2155 value_size = OCFS2_XATTR_ROOT_SIZE;
2157 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2159 if (free >= sizeof(struct ocfs2_xattr_entry) +
2160 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2166 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2167 struct ocfs2_dinode *di,
2168 struct ocfs2_xattr_info *xi,
2169 struct ocfs2_xattr_search *xis,
2170 struct ocfs2_xattr_search *xbs,
2175 int ret = 0, old_in_xb = 0;
2176 int clusters_add = 0, meta_add = 0, credits = 0;
2177 struct buffer_head *bh = NULL;
2178 struct ocfs2_xattr_block *xb = NULL;
2179 struct ocfs2_xattr_entry *xe = NULL;
2180 struct ocfs2_xattr_value_root *xv = NULL;
2182 int name_offset, name_len = 0;
2183 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2187 if (xis->not_found && xbs->not_found) {
2188 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2190 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2191 clusters_add += new_clusters;
2192 credits += ocfs2_calc_extend_credits(inode->i_sb,
2200 if (!xis->not_found) {
2202 name_offset = le16_to_cpu(xe->xe_name_offset);
2203 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2205 credits += OCFS2_INODE_UPDATE_CREDITS;
2207 int i, block_off = 0;
2208 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2210 name_offset = le16_to_cpu(xe->xe_name_offset);
2211 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2212 i = xbs->here - xbs->header->xh_entries;
2215 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2216 ret = ocfs2_xattr_bucket_get_name_value(inode,
2217 bucket_xh(xbs->bucket),
2220 base = bucket_block(xbs->bucket, block_off);
2221 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2224 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2229 * delete a xattr doesn't need metadata and cluster allocation.
2230 * so just calculate the credits and return.
2232 * The credits for removing the value tree will be extended
2233 * by ocfs2_remove_extent itself.
2236 if (!ocfs2_xattr_is_local(xe))
2237 credits += ocfs2_remove_extent_credits(inode->i_sb);
2242 /* do cluster allocation guess first. */
2243 value_size = le64_to_cpu(xe->xe_value_size);
2247 * In xattr set, we always try to set the xe in inode first,
2248 * so if it can be inserted into inode successfully, the old
2249 * one will be removed from the xattr block, and this xattr
2250 * will be inserted into inode as a new xattr in inode.
2252 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2253 clusters_add += new_clusters;
2254 credits += ocfs2_remove_extent_credits(inode->i_sb) +
2255 OCFS2_INODE_UPDATE_CREDITS;
2256 if (!ocfs2_xattr_is_local(xe))
2257 credits += ocfs2_calc_extend_credits(
2265 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2266 /* the new values will be stored outside. */
2267 u32 old_clusters = 0;
2269 if (!ocfs2_xattr_is_local(xe)) {
2270 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2272 xv = (struct ocfs2_xattr_value_root *)
2273 (base + name_offset + name_len);
2274 value_size = OCFS2_XATTR_ROOT_SIZE;
2278 if (old_clusters >= new_clusters) {
2279 credits += ocfs2_remove_extent_credits(inode->i_sb);
2282 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2283 clusters_add += new_clusters - old_clusters;
2284 credits += ocfs2_calc_extend_credits(inode->i_sb,
2288 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2293 * Now the new value will be stored inside. So if the new
2294 * value is smaller than the size of value root or the old
2295 * value, we don't need any allocation, otherwise we have
2296 * to guess metadata allocation.
2298 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2299 (!ocfs2_xattr_is_local(xe) &&
2300 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2305 /* calculate metadata allocation. */
2306 if (di->i_xattr_loc) {
2307 if (!xbs->xattr_bh) {
2308 ret = ocfs2_read_xattr_block(inode,
2309 le64_to_cpu(di->i_xattr_loc),
2316 xb = (struct ocfs2_xattr_block *)bh->b_data;
2318 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2320 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2321 struct ocfs2_extent_list *el =
2322 &xb->xb_attrs.xb_root.xt_list;
2323 meta_add += ocfs2_extend_meta_needed(el);
2324 credits += ocfs2_calc_extend_credits(inode->i_sb,
2329 * This cluster will be used either for new bucket or for
2331 * If the cluster size is the same as the bucket size, one
2332 * more is needed since we may need to extend the bucket
2336 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2337 if (OCFS2_XATTR_BUCKET_SIZE ==
2338 OCFS2_SB(inode->i_sb)->s_clustersize) {
2339 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2344 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2348 *clusters_need = clusters_add;
2350 *meta_need = meta_add;
2352 *credits_need = credits;
2357 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2358 struct ocfs2_dinode *di,
2359 struct ocfs2_xattr_info *xi,
2360 struct ocfs2_xattr_search *xis,
2361 struct ocfs2_xattr_search *xbs,
2362 struct ocfs2_xattr_set_ctxt *ctxt,
2365 int clusters_add, meta_add, ret;
2366 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2368 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2370 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2372 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2373 &clusters_add, &meta_add, credits);
2379 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2380 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2383 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2392 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2398 if (ctxt->meta_ac) {
2399 ocfs2_free_alloc_context(ctxt->meta_ac);
2400 ctxt->meta_ac = NULL;
2404 * We cannot have an error and a non null ctxt->data_ac.
2411 static int __ocfs2_xattr_set_handle(struct inode *inode,
2412 struct ocfs2_dinode *di,
2413 struct ocfs2_xattr_info *xi,
2414 struct ocfs2_xattr_search *xis,
2415 struct ocfs2_xattr_search *xbs,
2416 struct ocfs2_xattr_set_ctxt *ctxt)
2418 int ret = 0, credits, old_found;
2421 /* Remove existing extended attribute */
2422 if (!xis->not_found)
2423 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2424 else if (!xbs->not_found)
2425 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2427 /* We always try to set extended attribute into inode first*/
2428 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2429 if (!ret && !xbs->not_found) {
2431 * If succeed and that extended attribute existing in
2432 * external block, then we will remove it.
2437 old_found = xis->not_found;
2438 xis->not_found = -ENODATA;
2439 ret = ocfs2_calc_xattr_set_need(inode,
2447 xis->not_found = old_found;
2453 ret = ocfs2_extend_trans(ctxt->handle, credits +
2454 ctxt->handle->h_buffer_credits);
2459 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2460 } else if (ret == -ENOSPC) {
2461 if (di->i_xattr_loc && !xbs->xattr_bh) {
2462 ret = ocfs2_xattr_block_find(inode,
2468 old_found = xis->not_found;
2469 xis->not_found = -ENODATA;
2470 ret = ocfs2_calc_xattr_set_need(inode,
2478 xis->not_found = old_found;
2484 ret = ocfs2_extend_trans(ctxt->handle, credits +
2485 ctxt->handle->h_buffer_credits);
2492 * If no space in inode, we will set extended attribute
2493 * into external block.
2495 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2498 if (!xis->not_found) {
2500 * If succeed and that extended attribute
2501 * existing in inode, we will remove it.
2505 xbs->not_found = -ENODATA;
2506 ret = ocfs2_calc_xattr_set_need(inode,
2519 ret = ocfs2_extend_trans(ctxt->handle, credits +
2520 ctxt->handle->h_buffer_credits);
2525 ret = ocfs2_xattr_ibody_set(inode, xi,
2536 * This function only called duing creating inode
2537 * for init security/acl xattrs of the new inode.
2538 * The xattrs could be put into ibody or extent block,
2539 * xattr bucket would not be use in this case.
2540 * transanction credits also be reserved in here.
2542 int ocfs2_xattr_set_handle(handle_t *handle,
2543 struct inode *inode,
2544 struct buffer_head *di_bh,
2550 struct ocfs2_alloc_context *meta_ac,
2551 struct ocfs2_alloc_context *data_ac)
2553 struct ocfs2_dinode *di;
2556 struct ocfs2_xattr_info xi = {
2557 .name_index = name_index,
2560 .value_len = value_len,
2563 struct ocfs2_xattr_search xis = {
2564 .not_found = -ENODATA,
2567 struct ocfs2_xattr_search xbs = {
2568 .not_found = -ENODATA,
2571 struct ocfs2_xattr_set_ctxt ctxt = {
2577 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2580 xis.inode_bh = xbs.inode_bh = di_bh;
2581 di = (struct ocfs2_dinode *)di_bh->b_data;
2583 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2585 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2588 if (xis.not_found) {
2589 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2594 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2597 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2598 brelse(xbs.xattr_bh);
2606 * Set, replace or remove an extended attribute for this inode.
2607 * value is NULL to remove an existing extended attribute, else either
2608 * create or replace an extended attribute.
2610 int ocfs2_xattr_set(struct inode *inode,
2617 struct buffer_head *di_bh = NULL;
2618 struct ocfs2_dinode *di;
2620 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2621 struct inode *tl_inode = osb->osb_tl_inode;
2622 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2624 struct ocfs2_xattr_info xi = {
2625 .name_index = name_index,
2628 .value_len = value_len,
2631 struct ocfs2_xattr_search xis = {
2632 .not_found = -ENODATA,
2635 struct ocfs2_xattr_search xbs = {
2636 .not_found = -ENODATA,
2639 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2643 * Only xbs will be used on indexed trees. xis doesn't need a
2646 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2648 mlog_errno(-ENOMEM);
2652 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2655 goto cleanup_nolock;
2657 xis.inode_bh = xbs.inode_bh = di_bh;
2658 di = (struct ocfs2_dinode *)di_bh->b_data;
2660 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2662 * Scan inode and external block to find the same name
2663 * extended attribute and collect search infomation.
2665 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2668 if (xis.not_found) {
2669 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2674 if (xis.not_found && xbs.not_found) {
2676 if (flags & XATTR_REPLACE)
2683 if (flags & XATTR_CREATE)
2688 mutex_lock(&tl_inode->i_mutex);
2690 if (ocfs2_truncate_log_needs_flush(osb)) {
2691 ret = __ocfs2_flush_truncate_log(osb);
2693 mutex_unlock(&tl_inode->i_mutex);
2698 mutex_unlock(&tl_inode->i_mutex);
2700 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2701 &xbs, &ctxt, &credits);
2707 ctxt.handle = ocfs2_start_trans(osb, credits);
2708 if (IS_ERR(ctxt.handle)) {
2709 ret = PTR_ERR(ctxt.handle);
2714 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2716 ocfs2_commit_trans(osb, ctxt.handle);
2719 ocfs2_free_alloc_context(ctxt.data_ac);
2721 ocfs2_free_alloc_context(ctxt.meta_ac);
2722 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2723 ocfs2_schedule_truncate_log_flush(osb, 1);
2724 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2726 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2727 ocfs2_inode_unlock(inode, 1);
2730 brelse(xbs.xattr_bh);
2731 ocfs2_xattr_bucket_free(xbs.bucket);
2737 * Find the xattr extent rec which may contains name_hash.
2738 * e_cpos will be the first name hash of the xattr rec.
2739 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2741 static int ocfs2_xattr_get_rec(struct inode *inode,
2746 struct ocfs2_extent_list *el)
2749 struct buffer_head *eb_bh = NULL;
2750 struct ocfs2_extent_block *eb;
2751 struct ocfs2_extent_rec *rec = NULL;
2754 if (el->l_tree_depth) {
2755 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2761 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2764 if (el->l_tree_depth) {
2765 ocfs2_error(inode->i_sb,
2766 "Inode %lu has non zero tree depth in "
2767 "xattr tree block %llu\n", inode->i_ino,
2768 (unsigned long long)eb_bh->b_blocknr);
2774 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2775 rec = &el->l_recs[i];
2777 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2778 e_blkno = le64_to_cpu(rec->e_blkno);
2784 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2785 "record (%u, %u, 0) in xattr", inode->i_ino,
2786 le32_to_cpu(rec->e_cpos),
2787 ocfs2_rec_clusters(el, rec));
2792 *p_blkno = le64_to_cpu(rec->e_blkno);
2793 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2795 *e_cpos = le32_to_cpu(rec->e_cpos);
2801 typedef int (xattr_bucket_func)(struct inode *inode,
2802 struct ocfs2_xattr_bucket *bucket,
2805 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2806 struct ocfs2_xattr_bucket *bucket,
2813 int i, ret = 0, cmp = 1, block_off, new_offset;
2814 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
2815 size_t name_len = strlen(name);
2816 struct ocfs2_xattr_entry *xe = NULL;
2820 * We don't use binary search in the bucket because there
2821 * may be multiple entries with the same name hash.
2823 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2824 xe = &xh->xh_entries[i];
2826 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2828 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2831 cmp = name_index - ocfs2_xattr_get_type(xe);
2833 cmp = name_len - xe->xe_name_len;
2837 ret = ocfs2_xattr_bucket_get_name_value(inode,
2848 xe_name = bucket_block(bucket, block_off) + new_offset;
2849 if (!memcmp(name, xe_name, name_len)) {
2861 * Find the specified xattr entry in a series of buckets.
2862 * This series start from p_blkno and last for num_clusters.
2863 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2864 * the num of the valid buckets.
2866 * Return the buffer_head this xattr should reside in. And if the xattr's
2867 * hash is in the gap of 2 buckets, return the lower bucket.
2869 static int ocfs2_xattr_bucket_find(struct inode *inode,
2876 struct ocfs2_xattr_search *xs)
2879 struct ocfs2_xattr_header *xh = NULL;
2880 struct ocfs2_xattr_entry *xe = NULL;
2882 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2883 int low_bucket = 0, bucket, high_bucket;
2884 struct ocfs2_xattr_bucket *search;
2886 u64 blkno, lower_blkno = 0;
2888 search = ocfs2_xattr_bucket_new(inode);
2895 ret = ocfs2_read_xattr_bucket(search, p_blkno);
2901 xh = bucket_xh(search);
2902 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2903 while (low_bucket <= high_bucket) {
2904 ocfs2_xattr_bucket_relse(search);
2906 bucket = (low_bucket + high_bucket) / 2;
2907 blkno = p_blkno + bucket * blk_per_bucket;
2908 ret = ocfs2_read_xattr_bucket(search, blkno);
2914 xh = bucket_xh(search);
2915 xe = &xh->xh_entries[0];
2916 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2917 high_bucket = bucket - 1;
2922 * Check whether the hash of the last entry in our
2923 * bucket is larger than the search one. for an empty
2924 * bucket, the last one is also the first one.
2927 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2929 last_hash = le32_to_cpu(xe->xe_name_hash);
2931 /* record lower_blkno which may be the insert place. */
2932 lower_blkno = blkno;
2934 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2935 low_bucket = bucket + 1;
2939 /* the searched xattr should reside in this bucket if exists. */
2940 ret = ocfs2_find_xe_in_bucket(inode, search,
2941 name_index, name, name_hash,
2951 * Record the bucket we have found.
2952 * When the xattr's hash value is in the gap of 2 buckets, we will
2953 * always set it to the previous bucket.
2956 lower_blkno = p_blkno;
2958 /* This should be in cache - we just read it during the search */
2959 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2965 xs->header = bucket_xh(xs->bucket);
2966 xs->base = bucket_block(xs->bucket, 0);
2967 xs->end = xs->base + inode->i_sb->s_blocksize;
2970 xs->here = &xs->header->xh_entries[index];
2971 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2972 (unsigned long long)bucket_blkno(xs->bucket), index);
2977 ocfs2_xattr_bucket_free(search);
2981 static int ocfs2_xattr_index_block_find(struct inode *inode,
2982 struct buffer_head *root_bh,
2985 struct ocfs2_xattr_search *xs)
2988 struct ocfs2_xattr_block *xb =
2989 (struct ocfs2_xattr_block *)root_bh->b_data;
2990 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2991 struct ocfs2_extent_list *el = &xb_root->xt_list;
2993 u32 first_hash, num_clusters = 0;
2994 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2996 if (le16_to_cpu(el->l_next_free_rec) == 0)
2999 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3000 name, name_hash, name_index);
3002 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3009 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3011 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3012 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3015 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3016 p_blkno, first_hash, num_clusters, xs);
3022 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3025 xattr_bucket_func *func,
3029 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3030 u32 num_buckets = clusters * bpc;
3031 struct ocfs2_xattr_bucket *bucket;
3033 bucket = ocfs2_xattr_bucket_new(inode);
3035 mlog_errno(-ENOMEM);
3039 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3040 clusters, (unsigned long long)blkno);
3042 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3043 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3050 * The real bucket num in this series of blocks is stored
3051 * in the 1st bucket.
3054 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3056 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3057 (unsigned long long)blkno,
3058 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3060 ret = func(inode, bucket, para);
3063 /* Fall through to bucket_relse() */
3066 ocfs2_xattr_bucket_relse(bucket);
3071 ocfs2_xattr_bucket_free(bucket);
3075 struct ocfs2_xattr_tree_list {
3081 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
3082 struct ocfs2_xattr_header *xh,
3089 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3092 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3094 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
3095 *new_offset = name_offset % inode->i_sb->s_blocksize;
3100 static int ocfs2_list_xattr_bucket(struct inode *inode,
3101 struct ocfs2_xattr_bucket *bucket,
3105 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3106 int i, block_off, new_offset;
3107 const char *prefix, *name;
3109 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3110 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3111 type = ocfs2_xattr_get_type(entry);
3112 prefix = ocfs2_xattr_prefix(type);
3115 ret = ocfs2_xattr_bucket_get_name_value(inode,
3123 name = (const char *)bucket_block(bucket, block_off) +
3125 ret = ocfs2_xattr_list_entry(xl->buffer,
3129 entry->xe_name_len);
3138 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3139 struct ocfs2_xattr_tree_root *xt,
3143 struct ocfs2_extent_list *el = &xt->xt_list;
3145 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3147 struct ocfs2_xattr_tree_list xl = {
3149 .buffer_size = buffer_size,
3153 if (le16_to_cpu(el->l_next_free_rec) == 0)
3156 while (name_hash > 0) {
3157 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3158 &e_cpos, &num_clusters, el);
3164 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3165 ocfs2_list_xattr_bucket,
3175 name_hash = e_cpos - 1;
3183 static int cmp_xe(const void *a, const void *b)
3185 const struct ocfs2_xattr_entry *l = a, *r = b;
3186 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3187 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3189 if (l_hash > r_hash)
3191 if (l_hash < r_hash)
3196 static void swap_xe(void *a, void *b, int size)
3198 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3201 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3202 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3206 * When the ocfs2_xattr_block is filled up, new bucket will be created
3207 * and all the xattr entries will be moved to the new bucket.
3208 * The header goes at the start of the bucket, and the names+values are
3209 * filled from the end. This is why *target starts as the last buffer.
3210 * Note: we need to sort the entries since they are not saved in order
3211 * in the ocfs2_xattr_block.
3213 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3214 struct buffer_head *xb_bh,
3215 struct ocfs2_xattr_bucket *bucket)
3217 int i, blocksize = inode->i_sb->s_blocksize;
3218 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3219 u16 offset, size, off_change;
3220 struct ocfs2_xattr_entry *xe;
3221 struct ocfs2_xattr_block *xb =
3222 (struct ocfs2_xattr_block *)xb_bh->b_data;
3223 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
3224 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3225 u16 count = le16_to_cpu(xb_xh->xh_count);
3226 char *src = xb_bh->b_data;
3227 char *target = bucket_block(bucket, blks - 1);
3229 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3230 (unsigned long long)xb_bh->b_blocknr,
3231 (unsigned long long)bucket_blkno(bucket));
3233 for (i = 0; i < blks; i++)
3234 memset(bucket_block(bucket, i), 0, blocksize);
3237 * Since the xe_name_offset is based on ocfs2_xattr_header,
3238 * there is a offset change corresponding to the change of
3239 * ocfs2_xattr_header's position.
3241 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3242 xe = &xb_xh->xh_entries[count - 1];
3243 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3244 size = blocksize - offset;
3246 /* copy all the names and values. */
3247 memcpy(target + offset, src + offset, size);
3249 /* Init new header now. */
3250 xh->xh_count = xb_xh->xh_count;
3251 xh->xh_num_buckets = cpu_to_le16(1);
3252 xh->xh_name_value_len = cpu_to_le16(size);
3253 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3255 /* copy all the entries. */
3256 target = bucket_block(bucket, 0);
3257 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3258 size = count * sizeof(struct ocfs2_xattr_entry);
3259 memcpy(target + offset, (char *)xb_xh + offset, size);
3261 /* Change the xe offset for all the xe because of the move. */
3262 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3263 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3264 for (i = 0; i < count; i++)
3265 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3267 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3268 offset, size, off_change);
3270 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3275 * After we move xattr from block to index btree, we have to
3276 * update ocfs2_xattr_search to the new xe and base.
3278 * When the entry is in xattr block, xattr_bh indicates the storage place.
3279 * While if the entry is in index b-tree, "bucket" indicates the
3280 * real place of the xattr.
3282 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3283 struct ocfs2_xattr_search *xs,
3284 struct buffer_head *old_bh)
3286 char *buf = old_bh->b_data;
3287 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3288 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
3291 xs->header = bucket_xh(xs->bucket);
3292 xs->base = bucket_block(xs->bucket, 0);
3293 xs->end = xs->base + inode->i_sb->s_blocksize;
3298 i = xs->here - old_xh->xh_entries;
3299 xs->here = &xs->header->xh_entries[i];
3302 static int ocfs2_xattr_create_index_block(struct inode *inode,
3303 struct ocfs2_xattr_search *xs,
3304 struct ocfs2_xattr_set_ctxt *ctxt)
3309 handle_t *handle = ctxt->handle;
3310 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3311 struct ocfs2_inode_info *oi = OCFS2_I(inode);
3312 struct buffer_head *xb_bh = xs->xattr_bh;
3313 struct ocfs2_xattr_block *xb =
3314 (struct ocfs2_xattr_block *)xb_bh->b_data;
3315 struct ocfs2_xattr_tree_root *xr;
3316 u16 xb_flags = le16_to_cpu(xb->xb_flags);
3318 mlog(0, "create xattr index block for %llu\n",
3319 (unsigned long long)xb_bh->b_blocknr);
3321 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
3322 BUG_ON(!xs->bucket);
3326 * We can use this lock for now, and maybe move to a dedicated mutex
3327 * if performance becomes a problem later.
3329 down_write(&oi->ip_alloc_sem);
3331 ret = ocfs2_journal_access(handle, inode, xb_bh,
3332 OCFS2_JOURNAL_ACCESS_WRITE);
3338 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3339 1, 1, &bit_off, &len);
3346 * The bucket may spread in many blocks, and
3347 * we will only touch the 1st block and the last block
3348 * in the whole bucket(one for entry and one for data).
3350 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3352 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3353 (unsigned long long)blkno);
3355 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
3361 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3362 OCFS2_JOURNAL_ACCESS_CREATE);
3368 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3369 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3371 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3373 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3374 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3375 offsetof(struct ocfs2_xattr_block, xb_attrs));
3377 xr = &xb->xb_attrs.xb_root;
3378 xr->xt_clusters = cpu_to_le32(1);
3379 xr->xt_last_eb_blk = 0;
3380 xr->xt_list.l_tree_depth = 0;
3381 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3382 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3384 xr->xt_list.l_recs[0].e_cpos = 0;
3385 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3386 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3388 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3390 ocfs2_journal_dirty(handle, xb_bh);
3393 up_write(&oi->ip_alloc_sem);
3398 static int cmp_xe_offset(const void *a, const void *b)
3400 const struct ocfs2_xattr_entry *l = a, *r = b;
3401 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3402 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3404 if (l_name_offset < r_name_offset)
3406 if (l_name_offset > r_name_offset)
3412 * defrag a xattr bucket if we find that the bucket has some
3413 * holes beteen name/value pairs.
3414 * We will move all the name/value pairs to the end of the bucket
3415 * so that we can spare some space for insertion.
3417 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3419 struct ocfs2_xattr_bucket *bucket)
3422 size_t end, offset, len, value_len;
3423 struct ocfs2_xattr_header *xh;
3424 char *entries, *buf, *bucket_buf = NULL;
3425 u64 blkno = bucket_blkno(bucket);
3427 size_t blocksize = inode->i_sb->s_blocksize;
3428 struct ocfs2_xattr_entry *xe;
3431 * In order to make the operation more efficient and generic,
3432 * we copy all the blocks into a contiguous memory and do the
3433 * defragment there, so if anything is error, we will not touch
3436 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3443 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3444 memcpy(buf, bucket_block(bucket, i), blocksize);
3446 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
3447 OCFS2_JOURNAL_ACCESS_WRITE);
3453 xh = (struct ocfs2_xattr_header *)bucket_buf;
3454 entries = (char *)xh->xh_entries;
3455 xh_free_start = le16_to_cpu(xh->xh_free_start);
3457 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3458 "xh_free_start = %u, xh_name_value_len = %u.\n",
3459 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3460 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
3463 * sort all the entries by their offset.
3464 * the largest will be the first, so that we can
3465 * move them to the end one by one.
3467 sort(entries, le16_to_cpu(xh->xh_count),
3468 sizeof(struct ocfs2_xattr_entry),
3469 cmp_xe_offset, swap_xe);
3471 /* Move all name/values to the end of the bucket. */
3472 xe = xh->xh_entries;
3473 end = OCFS2_XATTR_BUCKET_SIZE;
3474 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3475 offset = le16_to_cpu(xe->xe_name_offset);
3476 if (ocfs2_xattr_is_local(xe))
3477 value_len = OCFS2_XATTR_SIZE(
3478 le64_to_cpu(xe->xe_value_size));
3480 value_len = OCFS2_XATTR_ROOT_SIZE;
3481 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3484 * We must make sure that the name/value pair
3485 * exist in the same block. So adjust end to
3486 * the previous block end if needed.
3488 if (((end - len) / blocksize !=
3489 (end - 1) / blocksize))
3490 end = end - end % blocksize;
3492 if (end > offset + len) {
3493 memmove(bucket_buf + end - len,
3494 bucket_buf + offset, len);
3495 xe->xe_name_offset = cpu_to_le16(end - len);
3498 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3499 "bucket %llu\n", (unsigned long long)blkno);
3504 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3505 "bucket %llu\n", (unsigned long long)blkno);
3507 if (xh_free_start == end)
3510 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3511 xh->xh_free_start = cpu_to_le16(end);
3513 /* sort the entries by their name_hash. */
3514 sort(entries, le16_to_cpu(xh->xh_count),
3515 sizeof(struct ocfs2_xattr_entry),
3519 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3520 memcpy(bucket_block(bucket, i), buf, blocksize);
3521 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
3529 * Move half nums of the xattr bucket in the previous cluster to this new
3530 * cluster. We only touch the last cluster of the previous extend record.
3532 * first_bh is the first buffer_head of a series of bucket in the same
3533 * extent rec and header_bh is the header of one bucket in this cluster.
3534 * They will be updated if we move the data header_bh contains to the new
3535 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3537 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3539 struct buffer_head **first_bh,
3540 struct buffer_head **header_bh,
3546 int i, ret, credits;
3547 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3548 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3549 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3550 int blocksize = inode->i_sb->s_blocksize;
3551 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3552 struct ocfs2_xattr_header *new_xh;
3553 struct ocfs2_xattr_header *xh =
3554 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3556 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3557 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3559 prev_bh = *first_bh;
3561 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3563 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3565 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3566 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
3569 * We need to update the 1st half of the new cluster and
3570 * 1 more for the update of the 1st bucket of the previous
3573 credits = bpc / 2 + 1 + handle->h_buffer_credits;
3574 ret = ocfs2_extend_trans(handle, credits);
3580 ret = ocfs2_journal_access(handle, inode, prev_bh,
3581 OCFS2_JOURNAL_ACCESS_WRITE);
3587 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3588 old_bh = new_bh = NULL;
3589 new_bh = sb_getblk(inode->i_sb, new_blkno);
3596 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3598 ret = ocfs2_journal_access(handle, inode, new_bh,
3599 OCFS2_JOURNAL_ACCESS_CREATE);
3606 ret = ocfs2_read_block(inode, prev_blkno, &old_bh, NULL);
3613 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3616 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3617 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3620 *first_hash = le32_to_cpu(
3621 new_xh->xh_entries[0].xe_name_hash);
3622 new_first_bh = new_bh;
3623 get_bh(new_first_bh);
3626 ocfs2_journal_dirty(handle, new_bh);
3628 if (*header_bh == old_bh) {
3630 *header_bh = new_bh;
3634 *first_bh = new_first_bh;
3641 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3643 ocfs2_journal_dirty(handle, prev_bh);
3646 brelse(new_first_bh);
3651 * Find the suitable pos when we divide a bucket into 2.
3652 * We have to make sure the xattrs with the same hash value exist
3653 * in the same bucket.
3655 * If this ocfs2_xattr_header covers more than one hash value, find a
3656 * place where the hash value changes. Try to find the most even split.
3657 * The most common case is that all entries have different hash values,
3658 * and the first check we make will find a place to split.
3660 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3662 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3663 int count = le16_to_cpu(xh->xh_count);
3664 int delta, middle = count / 2;
3667 * We start at the middle. Each step gets farther away in both
3668 * directions. We therefore hit the change in hash value
3669 * nearest to the middle. Note that this loop does not execute for
3672 for (delta = 0; delta < middle; delta++) {
3673 /* Let's check delta earlier than middle */
3674 if (cmp_xe(&entries[middle - delta - 1],
3675 &entries[middle - delta]))
3676 return middle - delta;
3678 /* For even counts, don't walk off the end */
3679 if ((middle + delta + 1) == count)
3682 /* Now try delta past middle */
3683 if (cmp_xe(&entries[middle + delta],
3684 &entries[middle + delta + 1]))
3685 return middle + delta + 1;
3688 /* Every entry had the same hash */
3693 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3694 * first_hash will record the 1st hash of the new bucket.
3696 * Normally half of the xattrs will be moved. But we have to make
3697 * sure that the xattrs with the same hash value are stored in the
3698 * same bucket. If all the xattrs in this bucket have the same hash
3699 * value, the new bucket will be initialized as an empty one and the
3700 * first_hash will be initialized as (hash_value+1).
3702 static int ocfs2_divide_xattr_bucket(struct inode *inode,
3707 int new_bucket_head)
3710 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
3711 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3712 struct ocfs2_xattr_header *xh;
3713 struct ocfs2_xattr_entry *xe;
3714 int blocksize = inode->i_sb->s_blocksize;
3716 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3717 (unsigned long long)blk, (unsigned long long)new_blk);
3719 s_bucket = ocfs2_xattr_bucket_new(inode);
3720 t_bucket = ocfs2_xattr_bucket_new(inode);
3721 if (!s_bucket || !t_bucket) {
3727 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
3733 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
3734 OCFS2_JOURNAL_ACCESS_WRITE);
3741 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3742 * there's no need to read it.
3744 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
3750 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3752 OCFS2_JOURNAL_ACCESS_CREATE :
3753 OCFS2_JOURNAL_ACCESS_WRITE);
3759 xh = bucket_xh(s_bucket);
3760 count = le16_to_cpu(xh->xh_count);
3761 start = ocfs2_xattr_find_divide_pos(xh);
3763 if (start == count) {
3764 xe = &xh->xh_entries[start-1];
3767 * initialized a new empty bucket here.
3768 * The hash value is set as one larger than
3769 * that of the last entry in the previous bucket.
3771 for (i = 0; i < t_bucket->bu_blocks; i++)
3772 memset(bucket_block(t_bucket, i), 0, blocksize);
3774 xh = bucket_xh(t_bucket);
3775 xh->xh_free_start = cpu_to_le16(blocksize);
3776 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3777 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3779 goto set_num_buckets;
3782 /* copy the whole bucket to the new first. */
3783 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3785 /* update the new bucket. */
3786 xh = bucket_xh(t_bucket);
3789 * Calculate the total name/value len and xh_free_start for
3790 * the old bucket first.
3792 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3794 for (i = 0; i < start; i++) {
3795 xe = &xh->xh_entries[i];
3796 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3797 if (ocfs2_xattr_is_local(xe))
3799 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3801 xe_len += OCFS2_XATTR_ROOT_SIZE;
3802 name_value_len += xe_len;
3803 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3804 name_offset = le16_to_cpu(xe->xe_name_offset);
3808 * Now begin the modification to the new bucket.
3810 * In the new bucket, We just move the xattr entry to the beginning
3811 * and don't touch the name/value. So there will be some holes in the
3812 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3815 xe = &xh->xh_entries[start];
3816 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3817 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3818 (int)((char *)xe - (char *)xh),
3819 (int)((char *)xh->xh_entries - (char *)xh));
3820 memmove((char *)xh->xh_entries, (char *)xe, len);
3821 xe = &xh->xh_entries[count - start];
3822 len = sizeof(struct ocfs2_xattr_entry) * start;
3823 memset((char *)xe, 0, len);
3825 le16_add_cpu(&xh->xh_count, -start);
3826 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3828 /* Calculate xh_free_start for the new bucket. */
3829 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3830 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3831 xe = &xh->xh_entries[i];
3832 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3833 if (ocfs2_xattr_is_local(xe))
3835 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3837 xe_len += OCFS2_XATTR_ROOT_SIZE;
3838 if (le16_to_cpu(xe->xe_name_offset) <
3839 le16_to_cpu(xh->xh_free_start))
3840 xh->xh_free_start = xe->xe_name_offset;
3844 /* set xh->xh_num_buckets for the new xh. */
3845 if (new_bucket_head)
3846 xh->xh_num_buckets = cpu_to_le16(1);
3848 xh->xh_num_buckets = 0;
3850 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3852 /* store the first_hash of the new bucket. */
3854 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3857 * Now only update the 1st block of the old bucket. If we
3858 * just added a new empty bucket, there is no need to modify
3864 xh = bucket_xh(s_bucket);
3865 memset(&xh->xh_entries[start], 0,
3866 sizeof(struct ocfs2_xattr_entry) * (count - start));
3867 xh->xh_count = cpu_to_le16(start);
3868 xh->xh_free_start = cpu_to_le16(name_offset);
3869 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3871 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
3874 ocfs2_xattr_bucket_free(s_bucket);
3875 ocfs2_xattr_bucket_free(t_bucket);
3881 * Copy xattr from one bucket to another bucket.
3883 * The caller must make sure that the journal transaction
3884 * has enough space for journaling.
3886 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3893 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3895 BUG_ON(s_blkno == t_blkno);
3897 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3898 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3901 s_bucket = ocfs2_xattr_bucket_new(inode);
3902 t_bucket = ocfs2_xattr_bucket_new(inode);
3903 if (!s_bucket || !t_bucket) {
3909 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
3914 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3915 * there's no need to read it.
3917 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
3921 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3923 OCFS2_JOURNAL_ACCESS_CREATE :
3924 OCFS2_JOURNAL_ACCESS_WRITE);
3928 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3929 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3932 ocfs2_xattr_bucket_free(t_bucket);
3933 ocfs2_xattr_bucket_free(s_bucket);
3939 * Copy one xattr cluster from src_blk to to_blk.
3940 * The to_blk will become the first bucket header of the cluster, so its
3941 * xh_num_buckets will be initialized as the bucket num in the cluster.
3943 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3945 struct buffer_head *first_bh,
3950 int i, ret, credits;
3951 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3952 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3953 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3954 struct buffer_head *bh = NULL;
3955 struct ocfs2_xattr_header *xh;
3956 u64 to_blk_start = to_blk;
3958 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3959 (unsigned long long)src_blk, (unsigned long long)to_blk);
3962 * We need to update the new cluster and 1 more for the update of
3963 * the 1st bucket of the previous extent rec.
3965 credits = bpc + 1 + handle->h_buffer_credits;
3966 ret = ocfs2_extend_trans(handle, credits);
3972 ret = ocfs2_journal_access(handle, inode, first_bh,
3973 OCFS2_JOURNAL_ACCESS_WRITE);
3979 for (i = 0; i < num_buckets; i++) {
3980 ret = ocfs2_cp_xattr_bucket(inode, handle,
3981 src_blk, to_blk, 1);
3987 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3988 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3991 /* update the old bucket header. */
3992 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3993 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3995 ocfs2_journal_dirty(handle, first_bh);
3997 /* update the new bucket header. */
3998 ret = ocfs2_read_block(inode, to_blk_start, &bh, NULL);
4004 ret = ocfs2_journal_access(handle, inode, bh,
4005 OCFS2_JOURNAL_ACCESS_WRITE);
4011 xh = (struct ocfs2_xattr_header *)bh->b_data;
4012 xh->xh_num_buckets = cpu_to_le16(num_buckets);
4014 ocfs2_journal_dirty(handle, bh);
4017 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4024 * Move some xattrs in this cluster to the new cluster.
4025 * This function should only be called when bucket size == cluster size.
4026 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4028 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4034 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4035 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4037 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4039 ret = ocfs2_extend_trans(handle, credits);
4045 /* Move half of the xattr in start_blk to the next bucket. */
4046 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4047 new_blk, first_hash, 1);
4051 * Move some xattrs from the old cluster to the new one since they are not
4052 * contiguous in ocfs2 xattr tree.
4054 * new_blk starts a new separate cluster, and we will move some xattrs from
4055 * prev_blk to it. v_start will be set as the first name hash value in this
4056 * new cluster so that it can be used as e_cpos during tree insertion and
4057 * don't collide with our original b-tree operations. first_bh and header_bh
4058 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4059 * to extend the insert bucket.
4061 * The problem is how much xattr should we move to the new one and when should
4062 * we update first_bh and header_bh?
4063 * 1. If cluster size > bucket size, that means the previous cluster has more
4064 * than 1 bucket, so just move half nums of bucket into the new cluster and
4065 * update the first_bh and header_bh if the insert bucket has been moved
4066 * to the new cluster.
4067 * 2. If cluster_size == bucket_size:
4068 * a) If the previous extent rec has more than one cluster and the insert
4069 * place isn't in the last cluster, copy the entire last cluster to the
4070 * new one. This time, we don't need to upate the first_bh and header_bh
4071 * since they will not be moved into the new cluster.
4072 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4073 * the new one. And we set the extend flag to zero if the insert place is
4074 * moved into the new allocated cluster since no extend is needed.
4076 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4078 struct buffer_head **first_bh,
4079 struct buffer_head **header_bh,
4087 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4089 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4090 (unsigned long long)prev_blk, prev_clusters,
4091 (unsigned long long)new_blk);
4093 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
4094 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4103 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
4105 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
4106 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
4110 ret = ocfs2_divide_xattr_cluster(inode, handle,
4114 if ((*header_bh)->b_blocknr == last_blk && extend)
4123 * Add a new cluster for xattr storage.
4125 * If the new cluster is contiguous with the previous one, it will be
4126 * appended to the same extent record, and num_clusters will be updated.
4127 * If not, we will insert a new extent for it and move some xattrs in
4128 * the last cluster into the new allocated one.
4129 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4130 * lose the benefits of hashing because we'll have to search large leaves.
4131 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4134 * first_bh is the first block of the previous extent rec and header_bh
4135 * indicates the bucket we will insert the new xattrs. They will be updated
4136 * when the header_bh is moved into the new cluster.
4138 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4139 struct buffer_head *root_bh,
4140 struct buffer_head **first_bh,
4141 struct buffer_head **header_bh,
4146 struct ocfs2_xattr_set_ctxt *ctxt)
4149 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4150 u32 prev_clusters = *num_clusters;
4151 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4153 handle_t *handle = ctxt->handle;
4154 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4155 struct ocfs2_extent_tree et;
4157 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4158 "previous xattr blkno = %llu\n",
4159 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4160 prev_cpos, (unsigned long long)prev_blkno);
4162 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4164 ret = ocfs2_journal_access(handle, inode, root_bh,
4165 OCFS2_JOURNAL_ACCESS_WRITE);
4171 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
4172 clusters_to_add, &bit_off, &num_bits);
4179 BUG_ON(num_bits > clusters_to_add);
4181 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4182 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4183 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4185 if (prev_blkno + prev_clusters * bpc == block &&
4186 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4187 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4189 * If this cluster is contiguous with the old one and
4190 * adding this new cluster, we don't surpass the limit of
4191 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4192 * initialized and used like other buckets in the previous
4194 * So add it as a contiguous one. The caller will handle
4197 v_start = prev_cpos + prev_clusters;
4198 *num_clusters = prev_clusters + num_bits;
4199 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4202 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4217 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4218 num_bits, (unsigned long long)block, v_start);
4219 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
4220 num_bits, 0, ctxt->meta_ac);
4226 ret = ocfs2_journal_dirty(handle, root_bh);
4235 * We are given an extent. 'first' is the bucket at the very front of
4236 * the extent. The extent has space for an additional bucket past
4237 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4238 * of the target bucket. We wish to shift every bucket past the target
4239 * down one, filling in that additional space. When we get back to the
4240 * target, we split the target between itself and the now-empty bucket
4241 * at target+1 (aka, target_blkno + blks_per_bucket).
4243 static int ocfs2_extend_xattr_bucket(struct inode *inode,
4245 struct ocfs2_xattr_bucket *first,
4250 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4251 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4253 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
4255 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4256 "from %llu, len = %u\n", (unsigned long long)target_blk,
4257 (unsigned long long)bucket_blkno(first), num_clusters);
4259 /* The extent must have room for an additional bucket */
4260 BUG_ON(new_bucket >=
4261 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
4263 /* end_blk points to the last existing bucket */
4264 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
4267 * end_blk is the start of the last existing bucket.
4268 * Thus, (end_blk - target_blk) covers the target bucket and
4269 * every bucket after it up to, but not including, the last
4270 * existing bucket. Then we add the last existing bucket, the
4271 * new bucket, and the first bucket (3 * blk_per_bucket).
4273 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
4274 handle->h_buffer_credits;
4275 ret = ocfs2_extend_trans(handle, credits);
4281 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4282 OCFS2_JOURNAL_ACCESS_WRITE);
4288 while (end_blk != target_blk) {
4289 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4290 end_blk + blk_per_bucket, 0);
4293 end_blk -= blk_per_bucket;
4296 /* Move half of the xattr in target_blkno to the next bucket. */
4297 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4298 target_blk + blk_per_bucket, NULL, 0);
4300 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4301 ocfs2_xattr_bucket_journal_dirty(handle, first);
4308 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
4309 * xb_bh is the ocfs2_xattr_block.
4310 * We will move all the buckets starting from header_bh to the next place. As
4311 * for this one, half num of its xattrs will be moved to the next one.
4313 * We will allocate a new cluster if current cluster is full and adjust
4314 * header_bh and first_bh if the insert place is moved to the new cluster.
4316 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4317 struct buffer_head *xb_bh,
4318 struct buffer_head *header_bh,
4319 struct ocfs2_xattr_set_ctxt *ctxt)
4321 struct ocfs2_xattr_header *first_xh = NULL;
4322 struct buffer_head *first_bh = NULL;
4323 struct ocfs2_xattr_block *xb =
4324 (struct ocfs2_xattr_block *)xb_bh->b_data;
4325 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4326 struct ocfs2_extent_list *el = &xb_root->xt_list;
4327 struct ocfs2_xattr_header *xh =
4328 (struct ocfs2_xattr_header *)header_bh->b_data;
4329 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4330 struct super_block *sb = inode->i_sb;
4331 struct ocfs2_super *osb = OCFS2_SB(sb);
4332 int ret, num_buckets, extend = 1;
4334 u32 e_cpos, num_clusters;
4335 /* The bucket at the front of the extent */
4336 struct ocfs2_xattr_bucket *first;
4338 mlog(0, "Add new xattr bucket starting form %llu\n",
4339 (unsigned long long)header_bh->b_blocknr);
4341 first = ocfs2_xattr_bucket_new(inode);
4349 * Add refrence for header_bh here because it may be
4350 * changed in ocfs2_add_new_xattr_cluster and we need
4351 * to free it in the end.
4355 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4362 ret = ocfs2_read_block(inode, p_blkno, &first_bh, NULL);
4368 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4369 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4371 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
4372 ret = ocfs2_add_new_xattr_cluster(inode,
4388 /* These bucket reads should be cached */
4389 ret = ocfs2_read_xattr_bucket(first, first_bh->b_blocknr);
4394 ret = ocfs2_extend_xattr_bucket(inode,
4396 first, header_bh->b_blocknr,
4405 ocfs2_xattr_bucket_free(first);
4409 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4410 struct ocfs2_xattr_bucket *bucket,
4413 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4415 offs = offs % inode->i_sb->s_blocksize;
4416 return bucket_block(bucket, block_off) + offs;
4420 * Handle the normal xattr set, including replace, delete and new.
4422 * Note: "local" indicates the real data's locality. So we can't
4423 * just its bucket locality by its length.
4425 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4426 struct ocfs2_xattr_info *xi,
4427 struct ocfs2_xattr_search *xs,
4431 struct ocfs2_xattr_entry *last, *xe;
4432 int name_len = strlen(xi->name);
4433 struct ocfs2_xattr_header *xh = xs->header;
4434 u16 count = le16_to_cpu(xh->xh_count), start;
4435 size_t blocksize = inode->i_sb->s_blocksize;
4437 size_t offs, size, new_size;
4439 last = &xh->xh_entries[count];
4440 if (!xs->not_found) {
4442 offs = le16_to_cpu(xe->xe_name_offset);
4443 if (ocfs2_xattr_is_local(xe))
4444 size = OCFS2_XATTR_SIZE(name_len) +
4445 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4447 size = OCFS2_XATTR_SIZE(name_len) +
4448 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4451 * If the new value will be stored outside, xi->value has been
4452 * initalized as an empty ocfs2_xattr_value_root, and the same
4453 * goes with xi->value_len, so we can set new_size safely here.
4454 * See ocfs2_xattr_set_in_bucket.
4456 new_size = OCFS2_XATTR_SIZE(name_len) +
4457 OCFS2_XATTR_SIZE(xi->value_len);
4459 le16_add_cpu(&xh->xh_name_value_len, -size);
4461 if (new_size > size)
4462 goto set_new_name_value;
4464 /* Now replace the old value with new one. */
4466 xe->xe_value_size = cpu_to_le64(xi->value_len);
4468 xe->xe_value_size = 0;
4470 val = ocfs2_xattr_bucket_get_val(inode,
4472 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4473 size - OCFS2_XATTR_SIZE(name_len));
4474 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4475 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4476 xi->value, xi->value_len);
4478 le16_add_cpu(&xh->xh_name_value_len, new_size);
4479 ocfs2_xattr_set_local(xe, local);
4483 * Remove the old entry if there is more than one.
4484 * We don't remove the last entry so that we can
4485 * use it to indicate the hash value of the empty
4489 le16_add_cpu(&xh->xh_count, -1);
4492 (void *)last - (void *)xe);
4494 sizeof(struct ocfs2_xattr_entry));
4497 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4502 /* find a new entry for insert. */
4503 int low = 0, high = count - 1, tmp;
4504 struct ocfs2_xattr_entry *tmp_xe;
4506 while (low <= high && count) {
4507 tmp = (low + high) / 2;
4508 tmp_xe = &xh->xh_entries[tmp];
4510 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4512 else if (name_hash <
4513 le32_to_cpu(tmp_xe->xe_name_hash))
4521 xe = &xh->xh_entries[low];
4523 memmove(xe + 1, xe, (void *)last - (void *)xe);
4525 le16_add_cpu(&xh->xh_count, 1);
4526 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4527 xe->xe_name_hash = cpu_to_le32(name_hash);
4528 xe->xe_name_len = name_len;
4529 ocfs2_xattr_set_type(xe, xi->name_index);
4533 /* Insert the new name+value. */
4534 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4537 * We must make sure that the name/value pair
4538 * exists in the same block.
4540 offs = le16_to_cpu(xh->xh_free_start);
4541 start = offs - size;
4543 if (start >> inode->i_sb->s_blocksize_bits !=
4544 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4545 offs = offs - offs % blocksize;
4546 xh->xh_free_start = cpu_to_le16(offs);
4549 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
4550 xe->xe_name_offset = cpu_to_le16(offs - size);
4552 memset(val, 0, size);
4553 memcpy(val, xi->name, name_len);
4554 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4556 xe->xe_value_size = cpu_to_le64(xi->value_len);
4557 ocfs2_xattr_set_local(xe, local);
4559 le16_add_cpu(&xh->xh_free_start, -size);
4560 le16_add_cpu(&xh->xh_name_value_len, size);
4566 * Set the xattr entry in the specified bucket.
4567 * The bucket is indicated by xs->bucket and it should have the enough
4568 * space for the xattr insertion.
4570 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4572 struct ocfs2_xattr_info *xi,
4573 struct ocfs2_xattr_search *xs,
4580 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4581 (unsigned long)xi->value_len, xi->name_index,
4582 (unsigned long long)bucket_blkno(xs->bucket));
4584 if (!xs->bucket->bu_bhs[1]) {
4585 blkno = bucket_blkno(xs->bucket);
4586 ocfs2_xattr_bucket_relse(xs->bucket);
4587 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
4594 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4595 OCFS2_JOURNAL_ACCESS_WRITE);
4601 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4602 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4609 * Truncate the specified xe_off entry in xattr bucket.
4610 * bucket is indicated by header_bh and len is the new length.
4611 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4613 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4615 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4616 struct ocfs2_xattr_bucket *bucket,
4619 struct ocfs2_xattr_set_ctxt *ctxt)
4623 struct buffer_head *value_bh = NULL;
4624 struct ocfs2_xattr_value_root *xv;
4625 struct ocfs2_xattr_entry *xe;
4626 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4627 size_t blocksize = inode->i_sb->s_blocksize;
4629 xe = &xh->xh_entries[xe_off];
4631 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4633 offset = le16_to_cpu(xe->xe_name_offset) +
4634 OCFS2_XATTR_SIZE(xe->xe_name_len);
4636 value_blk = offset / blocksize;
4638 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4639 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4641 value_bh = bucket->bu_bhs[value_blk];
4644 xv = (struct ocfs2_xattr_value_root *)
4645 (value_bh->b_data + offset % blocksize);
4647 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
4648 OCFS2_JOURNAL_ACCESS_WRITE);
4655 * From here on out we have to dirty the bucket. The generic
4656 * value calls only modify one of the bucket's bhs, but we need
4657 * to send the bucket at once. So if they error, they *could* have
4658 * modified something. We have to assume they did, and dirty
4659 * the whole bucket. This leaves us in a consistent state.
4661 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4662 xe_off, (unsigned long long)bucket_blkno(bucket), len);
4663 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
4669 xe->xe_value_size = cpu_to_le64(len);
4672 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
4678 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4679 struct ocfs2_xattr_search *xs,
4681 struct ocfs2_xattr_set_ctxt *ctxt)
4684 struct ocfs2_xattr_entry *xe = xs->here;
4685 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4687 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4689 offset = xe - xh->xh_entries;
4690 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
4698 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4700 struct ocfs2_xattr_search *xs,
4705 struct ocfs2_xattr_value_root *xv;
4706 struct ocfs2_xattr_entry *xe = xs->here;
4708 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4710 offset = le16_to_cpu(xe->xe_name_offset) +
4711 OCFS2_XATTR_SIZE(xe->xe_name_len);
4713 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4715 return __ocfs2_xattr_set_value_outside(inode, handle,
4716 xv, val, value_len);
4719 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4720 struct buffer_head *root_bh,
4726 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4727 struct inode *tl_inode = osb->osb_tl_inode;
4729 struct ocfs2_xattr_block *xb =
4730 (struct ocfs2_xattr_block *)root_bh->b_data;
4731 struct ocfs2_alloc_context *meta_ac = NULL;
4732 struct ocfs2_cached_dealloc_ctxt dealloc;
4733 struct ocfs2_extent_tree et;
4735 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4737 ocfs2_init_dealloc_ctxt(&dealloc);
4739 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4740 cpos, len, (unsigned long long)blkno);
4742 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4744 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4750 mutex_lock(&tl_inode->i_mutex);
4752 if (ocfs2_truncate_log_needs_flush(osb)) {
4753 ret = __ocfs2_flush_truncate_log(osb);
4760 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
4761 if (IS_ERR(handle)) {
4767 ret = ocfs2_journal_access(handle, inode, root_bh,
4768 OCFS2_JOURNAL_ACCESS_WRITE);
4774 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4781 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4783 ret = ocfs2_journal_dirty(handle, root_bh);
4789 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4794 ocfs2_commit_trans(osb, handle);
4796 ocfs2_schedule_truncate_log_flush(osb, 1);
4798 mutex_unlock(&tl_inode->i_mutex);
4801 ocfs2_free_alloc_context(meta_ac);
4803 ocfs2_run_deallocs(osb, &dealloc);
4808 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4810 struct ocfs2_xattr_search *xs)
4812 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
4813 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4814 le16_to_cpu(xh->xh_count) - 1];
4817 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4818 OCFS2_JOURNAL_ACCESS_WRITE);
4824 /* Remove the old entry. */
4825 memmove(xs->here, xs->here + 1,
4826 (void *)last - (void *)xs->here);
4827 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4828 le16_add_cpu(&xh->xh_count, -1);
4830 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4834 * Set the xattr name/value in the bucket specified in xs.
4836 * As the new value in xi may be stored in the bucket or in an outside cluster,
4837 * we divide the whole process into 3 steps:
4838 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4839 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4840 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4841 * 4. If the clusters for the new outside value can't be allocated, we need
4842 * to free the xattr we allocated in set.
4844 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4845 struct ocfs2_xattr_info *xi,
4846 struct ocfs2_xattr_search *xs,
4847 struct ocfs2_xattr_set_ctxt *ctxt)
4851 char *val = (char *)xi->value;
4852 struct ocfs2_xattr_entry *xe = xs->here;
4853 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4856 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4858 * We need to truncate the xattr storage first.
4860 * If both the old and new value are stored to
4861 * outside block, we only need to truncate
4862 * the storage and then set the value outside.
4864 * If the new value should be stored within block,
4865 * we should free all the outside block first and
4866 * the modification to the xattr block will be done
4867 * by following steps.
4869 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4870 value_len = xi->value_len;
4874 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4881 goto set_value_outside;
4884 value_len = xi->value_len;
4885 /* So we have to handle the inside block change now. */
4886 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4888 * If the new value will be stored outside of block,
4889 * initalize a new empty value root and insert it first.
4892 xi->value = &def_xv;
4893 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4896 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4903 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4906 /* allocate the space now for the outside block storage. */
4907 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4912 if (xs->not_found) {
4914 * We can't allocate enough clusters for outside
4915 * storage and we have allocated xattr already,
4916 * so need to remove it.
4918 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
4924 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4925 xs, val, value_len);
4931 * check whether the xattr bucket is filled up with the same hash value.
4932 * If we want to insert the xattr with the same hash, return -ENOSPC.
4933 * If we want to insert a xattr with different hash value, go ahead
4934 * and ocfs2_divide_xattr_bucket will handle this.
4936 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4937 struct ocfs2_xattr_bucket *bucket,
4940 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4941 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4943 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4946 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4947 xh->xh_entries[0].xe_name_hash) {
4948 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4950 (unsigned long long)bucket_blkno(bucket),
4951 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4958 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4959 struct ocfs2_xattr_info *xi,
4960 struct ocfs2_xattr_search *xs,
4961 struct ocfs2_xattr_set_ctxt *ctxt)
4963 struct ocfs2_xattr_header *xh;
4964 struct ocfs2_xattr_entry *xe;
4965 u16 count, header_size, xh_free_start;
4966 int free, max_free, need, old;
4967 size_t value_size = 0, name_len = strlen(xi->name);
4968 size_t blocksize = inode->i_sb->s_blocksize;
4969 int ret, allocation = 0;
4971 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4975 count = le16_to_cpu(xh->xh_count);
4976 xh_free_start = le16_to_cpu(xh->xh_free_start);
4977 header_size = sizeof(struct ocfs2_xattr_header) +
4978 count * sizeof(struct ocfs2_xattr_entry);
4979 max_free = OCFS2_XATTR_BUCKET_SIZE -
4980 le16_to_cpu(xh->xh_name_value_len) - header_size;
4982 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4983 "of %u which exceed block size\n",
4984 (unsigned long long)bucket_blkno(xs->bucket),
4987 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4988 value_size = OCFS2_XATTR_ROOT_SIZE;
4990 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4993 need = sizeof(struct ocfs2_xattr_entry) +
4994 OCFS2_XATTR_SIZE(name_len) + value_size;
4996 need = value_size + OCFS2_XATTR_SIZE(name_len);
4999 * We only replace the old value if the new length is smaller
5000 * than the old one. Otherwise we will allocate new space in the
5001 * bucket to store it.
5004 if (ocfs2_xattr_is_local(xe))
5005 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5007 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5009 if (old >= value_size)
5013 free = xh_free_start - header_size;
5015 * We need to make sure the new name/value pair
5016 * can exist in the same block.
5018 if (xh_free_start % blocksize < need)
5019 free -= xh_free_start % blocksize;
5021 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5022 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5023 " %u\n", xs->not_found,
5024 (unsigned long long)bucket_blkno(xs->bucket),
5025 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5026 le16_to_cpu(xh->xh_name_value_len));
5030 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5031 if (need <= max_free &&
5032 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5034 * We can create the space by defragment. Since only the
5035 * name/value will be moved, the xe shouldn't be changed
5038 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5045 xh_free_start = le16_to_cpu(xh->xh_free_start);
5046 free = xh_free_start - header_size;
5047 if (xh_free_start % blocksize < need)
5048 free -= xh_free_start % blocksize;
5053 mlog(0, "Can't get enough space for xattr insert by "
5054 "defragment. Need %u bytes, but we have %d, so "
5055 "allocate new bucket for it.\n", need, free);
5059 * We have to add new buckets or clusters and one
5060 * allocation should leave us enough space for insert.
5065 * We do not allow for overlapping ranges between buckets. And
5066 * the maximum number of collisions we will allow for then is
5067 * one bucket's worth, so check it here whether we need to
5068 * add a new bucket for the insert.
5070 ret = ocfs2_check_xattr_bucket_collision(inode,
5078 ret = ocfs2_add_new_xattr_bucket(inode,
5080 xs->bucket->bu_bhs[0],
5087 ocfs2_xattr_bucket_relse(xs->bucket);
5089 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5092 if (ret && ret != -ENODATA)
5094 xs->not_found = ret;
5100 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5106 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5107 struct ocfs2_xattr_bucket *bucket,
5111 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5113 struct ocfs2_xattr_entry *xe;
5114 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5115 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5116 int credits = ocfs2_remove_extent_credits(osb->sb) +
5117 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5120 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5122 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5123 xe = &xh->xh_entries[i];
5124 if (ocfs2_xattr_is_local(xe))
5127 ctxt.handle = ocfs2_start_trans(osb, credits);
5128 if (IS_ERR(ctxt.handle)) {
5129 ret = PTR_ERR(ctxt.handle);
5134 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5137 ocfs2_commit_trans(osb, ctxt.handle);
5144 ocfs2_schedule_truncate_log_flush(osb, 1);
5145 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5149 static int ocfs2_delete_xattr_index_block(struct inode *inode,
5150 struct buffer_head *xb_bh)
5152 struct ocfs2_xattr_block *xb =
5153 (struct ocfs2_xattr_block *)xb_bh->b_data;
5154 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5156 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5159 if (le16_to_cpu(el->l_next_free_rec) == 0)
5162 while (name_hash > 0) {
5163 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5164 &e_cpos, &num_clusters, el);
5170 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5171 ocfs2_delete_xattr_in_bucket,
5178 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5179 p_blkno, e_cpos, num_clusters);
5188 name_hash = e_cpos - 1;
5196 * 'security' attributes support
5198 static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5199 size_t list_size, const char *name,
5202 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5203 const size_t total_len = prefix_len + name_len + 1;
5205 if (list && total_len <= list_size) {
5206 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5207 memcpy(list + prefix_len, name, name_len);
5208 list[prefix_len + name_len] = '\0';
5213 static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5214 void *buffer, size_t size)
5216 if (strcmp(name, "") == 0)
5218 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5222 static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5223 const void *value, size_t size, int flags)
5225 if (strcmp(name, "") == 0)
5228 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5232 int ocfs2_init_security_get(struct inode *inode,
5234 struct ocfs2_security_xattr_info *si)
5236 return security_inode_init_security(inode, dir, &si->name, &si->value,
5240 int ocfs2_init_security_set(handle_t *handle,
5241 struct inode *inode,
5242 struct buffer_head *di_bh,
5243 struct ocfs2_security_xattr_info *si,
5244 struct ocfs2_alloc_context *xattr_ac,
5245 struct ocfs2_alloc_context *data_ac)
5247 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5248 OCFS2_XATTR_INDEX_SECURITY,
5249 si->name, si->value, si->value_len, 0,
5253 struct xattr_handler ocfs2_xattr_security_handler = {
5254 .prefix = XATTR_SECURITY_PREFIX,
5255 .list = ocfs2_xattr_security_list,
5256 .get = ocfs2_xattr_security_get,
5257 .set = ocfs2_xattr_security_set,
5261 * 'trusted' attributes support
5263 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5264 size_t list_size, const char *name,
5267 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
5268 const size_t total_len = prefix_len + name_len + 1;
5270 if (list && total_len <= list_size) {
5271 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5272 memcpy(list + prefix_len, name, name_len);
5273 list[prefix_len + name_len] = '\0';
5278 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5279 void *buffer, size_t size)
5281 if (strcmp(name, "") == 0)
5283 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5287 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5288 const void *value, size_t size, int flags)
5290 if (strcmp(name, "") == 0)
5293 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5297 struct xattr_handler ocfs2_xattr_trusted_handler = {
5298 .prefix = XATTR_TRUSTED_PREFIX,
5299 .list = ocfs2_xattr_trusted_list,
5300 .get = ocfs2_xattr_trusted_get,
5301 .set = ocfs2_xattr_trusted_set,
5305 * 'user' attributes support
5307 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5308 size_t list_size, const char *name,
5311 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
5312 const size_t total_len = prefix_len + name_len + 1;
5313 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5315 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5318 if (list && total_len <= list_size) {
5319 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5320 memcpy(list + prefix_len, name, name_len);
5321 list[prefix_len + name_len] = '\0';
5326 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5327 void *buffer, size_t size)
5329 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5331 if (strcmp(name, "") == 0)
5333 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5335 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5339 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5340 const void *value, size_t size, int flags)
5342 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5344 if (strcmp(name, "") == 0)
5346 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5349 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5353 struct xattr_handler ocfs2_xattr_user_handler = {
5354 .prefix = XATTR_USER_PREFIX,
5355 .list = ocfs2_xattr_user_list,
5356 .get = ocfs2_xattr_user_get,
5357 .set = ocfs2_xattr_user_set,