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);
271 ocfs2_xattr_bucket_relse(bucket);
275 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
276 struct ocfs2_xattr_bucket *bucket,
281 for (i = 0; i < bucket->bu_blocks; i++) {
282 rc = ocfs2_journal_access(handle, bucket->bu_inode,
283 bucket->bu_bhs[i], type);
293 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
294 struct ocfs2_xattr_bucket *bucket)
298 for (i = 0; i < bucket->bu_blocks; i++)
299 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
302 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
303 struct ocfs2_xattr_bucket *src)
306 int blocksize = src->bu_inode->i_sb->s_blocksize;
308 BUG_ON(dest->bu_blocks != src->bu_blocks);
309 BUG_ON(dest->bu_inode != src->bu_inode);
311 for (i = 0; i < src->bu_blocks; i++) {
312 memcpy(bucket_block(dest, i), bucket_block(src, i),
317 static inline const char *ocfs2_xattr_prefix(int name_index)
319 struct xattr_handler *handler = NULL;
321 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
322 handler = ocfs2_xattr_handler_map[name_index];
324 return handler ? handler->prefix : NULL;
327 static u32 ocfs2_xattr_name_hash(struct inode *inode,
331 /* Get hash value of uuid from super block */
332 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
335 /* hash extended attribute name */
336 for (i = 0; i < name_len; i++) {
337 hash = (hash << OCFS2_HASH_SHIFT) ^
338 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
346 * ocfs2_xattr_hash_entry()
348 * Compute the hash of an extended attribute.
350 static void ocfs2_xattr_hash_entry(struct inode *inode,
351 struct ocfs2_xattr_header *header,
352 struct ocfs2_xattr_entry *entry)
355 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
357 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
358 entry->xe_name_hash = cpu_to_le32(hash);
363 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
367 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
368 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
370 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
371 size += sizeof(struct ocfs2_xattr_entry);
376 int ocfs2_calc_security_init(struct inode *dir,
377 struct ocfs2_security_xattr_info *si,
380 struct ocfs2_alloc_context **xattr_ac)
383 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
384 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
388 * The max space of security xattr taken inline is
389 * 256(name) + 80(value) + 16(entry) = 352 bytes,
390 * So reserve one metadata block for it is ok.
392 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
393 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
394 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
399 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
402 /* reserve clusters for xattr value which will be set in B tree*/
403 if (si->value_len > OCFS2_XATTR_INLINE_SIZE)
404 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
409 int ocfs2_calc_xattr_init(struct inode *dir,
410 struct buffer_head *dir_bh,
412 struct ocfs2_security_xattr_info *si,
415 struct ocfs2_alloc_context **xattr_ac)
418 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
424 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
427 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
428 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
429 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
432 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
435 } else if (acl_len != 0 && acl_len != -ENODATA) {
441 if (!(s_size + a_size))
445 * The max space of security xattr taken inline is
446 * 256(name) + 80(value) + 16(entry) = 352 bytes,
447 * The max space of acl xattr taken inline is
448 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
449 * when blocksize = 512, may reserve one more cluser for
450 * xattr bucket, otherwise reserve one metadata block
453 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
454 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
455 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
460 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
463 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
464 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
466 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
469 /* reserve clusters for xattr value which will be set in B tree*/
470 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE)
471 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
473 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
474 acl_len > OCFS2_XATTR_INLINE_SIZE) {
475 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
477 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
484 static int ocfs2_xattr_extend_allocation(struct inode *inode,
486 struct buffer_head *xattr_bh,
487 struct ocfs2_xattr_value_root *xv,
488 struct ocfs2_xattr_set_ctxt *ctxt)
491 handle_t *handle = ctxt->handle;
492 enum ocfs2_alloc_restarted why;
493 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
494 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
495 struct ocfs2_extent_tree et;
497 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
499 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
501 status = ocfs2_journal_access(handle, inode, xattr_bh,
502 OCFS2_JOURNAL_ACCESS_WRITE);
508 prev_clusters = le32_to_cpu(xv->xr_clusters);
509 status = ocfs2_add_clusters_in_btree(osb,
524 status = ocfs2_journal_dirty(handle, xattr_bh);
530 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
533 * We should have already allocated enough space before the transaction,
534 * so no need to restart.
536 BUG_ON(why != RESTART_NONE || clusters_to_add);
543 static int __ocfs2_remove_xattr_range(struct inode *inode,
544 struct buffer_head *root_bh,
545 struct ocfs2_xattr_value_root *xv,
546 u32 cpos, u32 phys_cpos, u32 len,
547 struct ocfs2_xattr_set_ctxt *ctxt)
550 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
551 handle_t *handle = ctxt->handle;
552 struct ocfs2_extent_tree et;
554 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
556 ret = ocfs2_journal_access(handle, inode, root_bh,
557 OCFS2_JOURNAL_ACCESS_WRITE);
563 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
570 le32_add_cpu(&xv->xr_clusters, -len);
572 ret = ocfs2_journal_dirty(handle, root_bh);
578 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
586 static int ocfs2_xattr_shrink_size(struct inode *inode,
589 struct buffer_head *root_bh,
590 struct ocfs2_xattr_value_root *xv,
591 struct ocfs2_xattr_set_ctxt *ctxt)
594 u32 trunc_len, cpos, phys_cpos, alloc_size;
597 if (old_clusters <= new_clusters)
601 trunc_len = old_clusters - new_clusters;
603 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
604 &alloc_size, &xv->xr_list);
610 if (alloc_size > trunc_len)
611 alloc_size = trunc_len;
613 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
614 phys_cpos, alloc_size,
621 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
622 ocfs2_remove_xattr_clusters_from_cache(inode, block,
625 trunc_len -= alloc_size;
632 static int ocfs2_xattr_value_truncate(struct inode *inode,
633 struct buffer_head *root_bh,
634 struct ocfs2_xattr_value_root *xv,
636 struct ocfs2_xattr_set_ctxt *ctxt)
639 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
640 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
642 if (new_clusters == old_clusters)
645 if (new_clusters > old_clusters)
646 ret = ocfs2_xattr_extend_allocation(inode,
647 new_clusters - old_clusters,
650 ret = ocfs2_xattr_shrink_size(inode,
651 old_clusters, new_clusters,
657 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
658 size_t *result, const char *prefix,
659 const char *name, int name_len)
661 char *p = buffer + *result;
662 int prefix_len = strlen(prefix);
663 int total_len = prefix_len + name_len + 1;
665 *result += total_len;
667 /* we are just looking for how big our buffer needs to be */
674 memcpy(p, prefix, prefix_len);
675 memcpy(p + prefix_len, name, name_len);
676 p[prefix_len + name_len] = '\0';
681 static int ocfs2_xattr_list_entries(struct inode *inode,
682 struct ocfs2_xattr_header *header,
683 char *buffer, size_t buffer_size)
687 const char *prefix, *name;
689 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
690 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
691 type = ocfs2_xattr_get_type(entry);
692 prefix = ocfs2_xattr_prefix(type);
695 name = (const char *)header +
696 le16_to_cpu(entry->xe_name_offset);
698 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
699 &result, prefix, name,
709 static int ocfs2_xattr_ibody_list(struct inode *inode,
710 struct ocfs2_dinode *di,
714 struct ocfs2_xattr_header *header = NULL;
715 struct ocfs2_inode_info *oi = OCFS2_I(inode);
718 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
721 header = (struct ocfs2_xattr_header *)
722 ((void *)di + inode->i_sb->s_blocksize -
723 le16_to_cpu(di->i_xattr_inline_size));
725 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
730 static int ocfs2_xattr_block_list(struct inode *inode,
731 struct ocfs2_dinode *di,
735 struct buffer_head *blk_bh = NULL;
736 struct ocfs2_xattr_block *xb;
739 if (!di->i_xattr_loc)
742 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
748 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
749 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
754 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
755 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
756 ret = ocfs2_xattr_list_entries(inode, header,
757 buffer, buffer_size);
759 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
760 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
761 buffer, buffer_size);
769 ssize_t ocfs2_listxattr(struct dentry *dentry,
773 int ret = 0, i_ret = 0, b_ret = 0;
774 struct buffer_head *di_bh = NULL;
775 struct ocfs2_dinode *di = NULL;
776 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
778 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
781 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
784 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
790 di = (struct ocfs2_dinode *)di_bh->b_data;
792 down_read(&oi->ip_xattr_sem);
793 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
801 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
806 up_read(&oi->ip_xattr_sem);
807 ocfs2_inode_unlock(dentry->d_inode, 0);
811 return i_ret + b_ret;
814 static int ocfs2_xattr_find_entry(int name_index,
816 struct ocfs2_xattr_search *xs)
818 struct ocfs2_xattr_entry *entry;
825 name_len = strlen(name);
827 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
828 cmp = name_index - ocfs2_xattr_get_type(entry);
830 cmp = name_len - entry->xe_name_len;
832 cmp = memcmp(name, (xs->base +
833 le16_to_cpu(entry->xe_name_offset)),
841 return cmp ? -ENODATA : 0;
844 static int ocfs2_xattr_get_value_outside(struct inode *inode,
845 struct ocfs2_xattr_value_root *xv,
849 u32 cpos, p_cluster, num_clusters, bpc, clusters;
852 size_t cplen, blocksize;
853 struct buffer_head *bh = NULL;
854 struct ocfs2_extent_list *el;
857 clusters = le32_to_cpu(xv->xr_clusters);
858 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
859 blocksize = inode->i_sb->s_blocksize;
862 while (cpos < clusters) {
863 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
870 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
871 /* Copy ocfs2_xattr_value */
872 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
873 ret = ocfs2_read_block(inode, blkno, &bh);
879 cplen = len >= blocksize ? blocksize : len;
880 memcpy(buffer, bh->b_data, cplen);
889 cpos += num_clusters;
895 static int ocfs2_xattr_ibody_get(struct inode *inode,
900 struct ocfs2_xattr_search *xs)
902 struct ocfs2_inode_info *oi = OCFS2_I(inode);
903 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
904 struct ocfs2_xattr_value_root *xv;
908 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
911 xs->end = (void *)di + inode->i_sb->s_blocksize;
912 xs->header = (struct ocfs2_xattr_header *)
913 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
914 xs->base = (void *)xs->header;
915 xs->here = xs->header->xh_entries;
917 ret = ocfs2_xattr_find_entry(name_index, name, xs);
920 size = le64_to_cpu(xs->here->xe_value_size);
922 if (size > buffer_size)
924 if (ocfs2_xattr_is_local(xs->here)) {
925 memcpy(buffer, (void *)xs->base +
926 le16_to_cpu(xs->here->xe_name_offset) +
927 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
929 xv = (struct ocfs2_xattr_value_root *)
930 (xs->base + le16_to_cpu(
931 xs->here->xe_name_offset) +
932 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
933 ret = ocfs2_xattr_get_value_outside(inode, xv,
945 static int ocfs2_xattr_block_get(struct inode *inode,
950 struct ocfs2_xattr_search *xs)
952 struct ocfs2_xattr_block *xb;
953 struct ocfs2_xattr_value_root *xv;
955 int ret = -ENODATA, name_offset, name_len, block_off, i;
957 xs->bucket = ocfs2_xattr_bucket_new(inode);
964 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
975 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
976 size = le64_to_cpu(xs->here->xe_value_size);
979 if (size > buffer_size)
982 name_offset = le16_to_cpu(xs->here->xe_name_offset);
983 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
984 i = xs->here - xs->header->xh_entries;
986 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
987 ret = ocfs2_xattr_bucket_get_name_value(inode,
988 bucket_xh(xs->bucket),
992 xs->base = bucket_block(xs->bucket, block_off);
994 if (ocfs2_xattr_is_local(xs->here)) {
995 memcpy(buffer, (void *)xs->base +
996 name_offset + name_len, size);
998 xv = (struct ocfs2_xattr_value_root *)
999 (xs->base + name_offset + name_len);
1000 ret = ocfs2_xattr_get_value_outside(inode, xv,
1010 ocfs2_xattr_bucket_free(xs->bucket);
1012 brelse(xs->xattr_bh);
1013 xs->xattr_bh = NULL;
1017 int ocfs2_xattr_get_nolock(struct inode *inode,
1018 struct buffer_head *di_bh,
1025 struct ocfs2_dinode *di = NULL;
1026 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1027 struct ocfs2_xattr_search xis = {
1028 .not_found = -ENODATA,
1030 struct ocfs2_xattr_search xbs = {
1031 .not_found = -ENODATA,
1034 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1037 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1040 xis.inode_bh = xbs.inode_bh = di_bh;
1041 di = (struct ocfs2_dinode *)di_bh->b_data;
1043 down_read(&oi->ip_xattr_sem);
1044 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1046 if (ret == -ENODATA && di->i_xattr_loc)
1047 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1049 up_read(&oi->ip_xattr_sem);
1054 /* ocfs2_xattr_get()
1056 * Copy an extended attribute into the buffer provided.
1057 * Buffer is NULL to compute the size of buffer required.
1059 static int ocfs2_xattr_get(struct inode *inode,
1066 struct buffer_head *di_bh = NULL;
1068 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1073 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1074 name, buffer, buffer_size);
1076 ocfs2_inode_unlock(inode, 0);
1083 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1085 struct ocfs2_xattr_value_root *xv,
1089 int ret = 0, i, cp_len, credits;
1090 u16 blocksize = inode->i_sb->s_blocksize;
1091 u32 p_cluster, num_clusters;
1092 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1093 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1095 struct buffer_head *bh = NULL;
1097 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1100 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1101 * so we don't need to worry about whether ocfs2_extend_trans
1102 * will create a new transactio for us or not.
1104 credits = clusters * bpc;
1105 ret = ocfs2_extend_trans(handle, credits);
1111 while (cpos < clusters) {
1112 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1113 &num_clusters, &xv->xr_list);
1119 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1121 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1122 ret = ocfs2_read_block(inode, blkno, &bh);
1128 ret = ocfs2_journal_access(handle,
1131 OCFS2_JOURNAL_ACCESS_WRITE);
1137 cp_len = value_len > blocksize ? blocksize : value_len;
1138 memcpy(bh->b_data, value, cp_len);
1139 value_len -= cp_len;
1141 if (cp_len < blocksize)
1142 memset(bh->b_data + cp_len, 0,
1143 blocksize - cp_len);
1145 ret = ocfs2_journal_dirty(handle, bh);
1154 * XXX: do we need to empty all the following
1155 * blocks in this cluster?
1160 cpos += num_clusters;
1168 static int ocfs2_xattr_cleanup(struct inode *inode,
1170 struct ocfs2_xattr_info *xi,
1171 struct ocfs2_xattr_search *xs,
1175 size_t name_len = strlen(xi->name);
1176 void *val = xs->base + offs;
1177 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1179 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1180 OCFS2_JOURNAL_ACCESS_WRITE);
1185 /* Decrease xattr count */
1186 le16_add_cpu(&xs->header->xh_count, -1);
1187 /* Remove the xattr entry and tree root which has already be set*/
1188 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1189 memset(val, 0, size);
1191 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1198 static int ocfs2_xattr_update_entry(struct inode *inode,
1200 struct ocfs2_xattr_info *xi,
1201 struct ocfs2_xattr_search *xs,
1206 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1207 OCFS2_JOURNAL_ACCESS_WRITE);
1213 xs->here->xe_name_offset = cpu_to_le16(offs);
1214 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1215 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1216 ocfs2_xattr_set_local(xs->here, 1);
1218 ocfs2_xattr_set_local(xs->here, 0);
1219 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1221 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1229 * ocfs2_xattr_set_value_outside()
1231 * Set large size value in B tree.
1233 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1234 struct ocfs2_xattr_info *xi,
1235 struct ocfs2_xattr_search *xs,
1236 struct ocfs2_xattr_set_ctxt *ctxt,
1239 size_t name_len = strlen(xi->name);
1240 void *val = xs->base + offs;
1241 struct ocfs2_xattr_value_root *xv = NULL;
1242 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1245 memset(val, 0, size);
1246 memcpy(val, xi->name, name_len);
1247 xv = (struct ocfs2_xattr_value_root *)
1248 (val + OCFS2_XATTR_SIZE(name_len));
1249 xv->xr_clusters = 0;
1250 xv->xr_last_eb_blk = 0;
1251 xv->xr_list.l_tree_depth = 0;
1252 xv->xr_list.l_count = cpu_to_le16(1);
1253 xv->xr_list.l_next_free_rec = 0;
1255 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1256 xi->value_len, ctxt);
1261 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
1266 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1267 xi->value, xi->value_len);
1275 * ocfs2_xattr_set_entry_local()
1277 * Set, replace or remove extended attribute in local.
1279 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1280 struct ocfs2_xattr_info *xi,
1281 struct ocfs2_xattr_search *xs,
1282 struct ocfs2_xattr_entry *last,
1285 size_t name_len = strlen(xi->name);
1288 if (xi->value && xs->not_found) {
1289 /* Insert the new xattr entry. */
1290 le16_add_cpu(&xs->header->xh_count, 1);
1291 ocfs2_xattr_set_type(last, xi->name_index);
1292 ocfs2_xattr_set_local(last, 1);
1293 last->xe_name_len = name_len;
1299 first_val = xs->base + min_offs;
1300 offs = le16_to_cpu(xs->here->xe_name_offset);
1301 val = xs->base + offs;
1303 if (le64_to_cpu(xs->here->xe_value_size) >
1304 OCFS2_XATTR_INLINE_SIZE)
1305 size = OCFS2_XATTR_SIZE(name_len) +
1306 OCFS2_XATTR_ROOT_SIZE;
1308 size = OCFS2_XATTR_SIZE(name_len) +
1309 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1311 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1312 OCFS2_XATTR_SIZE(xi->value_len)) {
1313 /* The old and the new value have the
1314 same size. Just replace the value. */
1315 ocfs2_xattr_set_local(xs->here, 1);
1316 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1317 /* Clear value bytes. */
1318 memset(val + OCFS2_XATTR_SIZE(name_len),
1320 OCFS2_XATTR_SIZE(xi->value_len));
1321 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1326 /* Remove the old name+value. */
1327 memmove(first_val + size, first_val, val - first_val);
1328 memset(first_val, 0, size);
1329 xs->here->xe_name_hash = 0;
1330 xs->here->xe_name_offset = 0;
1331 ocfs2_xattr_set_local(xs->here, 1);
1332 xs->here->xe_value_size = 0;
1336 /* Adjust all value offsets. */
1337 last = xs->header->xh_entries;
1338 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1339 size_t o = le16_to_cpu(last->xe_name_offset);
1342 last->xe_name_offset = cpu_to_le16(o + size);
1347 /* Remove the old entry. */
1349 memmove(xs->here, xs->here + 1,
1350 (void *)last - (void *)xs->here);
1351 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1352 le16_add_cpu(&xs->header->xh_count, -1);
1356 /* Insert the new name+value. */
1357 size_t size = OCFS2_XATTR_SIZE(name_len) +
1358 OCFS2_XATTR_SIZE(xi->value_len);
1359 void *val = xs->base + min_offs - size;
1361 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1362 memset(val, 0, size);
1363 memcpy(val, xi->name, name_len);
1364 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1367 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1368 ocfs2_xattr_set_local(xs->here, 1);
1369 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1376 * ocfs2_xattr_set_entry()
1378 * Set extended attribute entry into inode or block.
1380 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1381 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1382 * then set value in B tree with set_value_outside().
1384 static int ocfs2_xattr_set_entry(struct inode *inode,
1385 struct ocfs2_xattr_info *xi,
1386 struct ocfs2_xattr_search *xs,
1387 struct ocfs2_xattr_set_ctxt *ctxt,
1390 struct ocfs2_xattr_entry *last;
1391 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1392 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1393 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1395 handle_t *handle = ctxt->handle;
1397 struct ocfs2_xattr_info xi_l = {
1398 .name_index = xi->name_index,
1401 .value_len = xi->value_len,
1404 /* Compute min_offs, last and free space. */
1405 last = xs->header->xh_entries;
1407 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1408 size_t offs = le16_to_cpu(last->xe_name_offset);
1409 if (offs < min_offs)
1414 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1418 if (!xs->not_found) {
1420 if (ocfs2_xattr_is_local(xs->here))
1421 size = OCFS2_XATTR_SIZE(name_len) +
1422 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1424 size = OCFS2_XATTR_SIZE(name_len) +
1425 OCFS2_XATTR_ROOT_SIZE;
1426 free += (size + sizeof(struct ocfs2_xattr_entry));
1428 /* Check free space in inode or block */
1429 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1430 if (free < sizeof(struct ocfs2_xattr_entry) +
1431 OCFS2_XATTR_SIZE(name_len) +
1432 OCFS2_XATTR_ROOT_SIZE) {
1436 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1437 xi_l.value = (void *)&def_xv;
1438 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1439 } else if (xi->value) {
1440 if (free < sizeof(struct ocfs2_xattr_entry) +
1441 OCFS2_XATTR_SIZE(name_len) +
1442 OCFS2_XATTR_SIZE(xi->value_len)) {
1448 if (!xs->not_found) {
1449 /* For existing extended attribute */
1450 size_t size = OCFS2_XATTR_SIZE(name_len) +
1451 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1452 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1453 void *val = xs->base + offs;
1455 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1456 /* Replace existing local xattr with tree root */
1457 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1462 } else if (!ocfs2_xattr_is_local(xs->here)) {
1463 /* For existing xattr which has value outside */
1464 struct ocfs2_xattr_value_root *xv = NULL;
1465 xv = (struct ocfs2_xattr_value_root *)(val +
1466 OCFS2_XATTR_SIZE(name_len));
1468 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1470 * If new value need set outside also,
1471 * first truncate old value to new value,
1472 * then set new value with set_value_outside().
1474 ret = ocfs2_xattr_value_truncate(inode,
1484 ret = ocfs2_xattr_update_entry(inode,
1494 ret = __ocfs2_xattr_set_value_outside(inode,
1504 * If new value need set in local,
1505 * just trucate old value to zero.
1507 ret = ocfs2_xattr_value_truncate(inode,
1518 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1519 OCFS2_JOURNAL_ACCESS_WRITE);
1525 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1526 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1527 OCFS2_JOURNAL_ACCESS_WRITE);
1535 * Set value in local, include set tree root in local.
1536 * This is the first step for value size >INLINE_SIZE.
1538 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1540 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1541 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1548 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1549 (flag & OCFS2_INLINE_XATTR_FL)) {
1550 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1551 unsigned int xattrsize = osb->s_xattr_inline_size;
1554 * Adjust extent record count or inline data size
1555 * to reserve space for extended attribute.
1557 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1558 struct ocfs2_inline_data *idata = &di->id2.i_data;
1559 le16_add_cpu(&idata->id_count, -xattrsize);
1560 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1561 struct ocfs2_extent_list *el = &di->id2.i_list;
1562 le16_add_cpu(&el->l_count, -(xattrsize /
1563 sizeof(struct ocfs2_extent_rec)));
1565 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1567 /* Update xattr flag */
1568 spin_lock(&oi->ip_lock);
1569 oi->ip_dyn_features |= flag;
1570 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1571 spin_unlock(&oi->ip_lock);
1572 /* Update inode ctime */
1573 inode->i_ctime = CURRENT_TIME;
1574 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1575 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1577 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1581 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1583 * Set value outside in B tree.
1584 * This is the second step for value size > INLINE_SIZE.
1586 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1587 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
1593 * If set value outside failed, we have to clean
1594 * the junk tree root we have already set in local.
1596 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1606 static int ocfs2_remove_value_outside(struct inode*inode,
1607 struct buffer_head *bh,
1608 struct ocfs2_xattr_header *header)
1611 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1612 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1614 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1616 ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
1617 if (IS_ERR(ctxt.handle)) {
1618 ret = PTR_ERR(ctxt.handle);
1623 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1624 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1626 if (!ocfs2_xattr_is_local(entry)) {
1627 struct ocfs2_xattr_value_root *xv;
1630 val = (void *)header +
1631 le16_to_cpu(entry->xe_name_offset);
1632 xv = (struct ocfs2_xattr_value_root *)
1633 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1634 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1643 ocfs2_commit_trans(osb, ctxt.handle);
1644 ocfs2_schedule_truncate_log_flush(osb, 1);
1645 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1650 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1651 struct buffer_head *di_bh)
1654 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1655 struct ocfs2_xattr_header *header;
1658 header = (struct ocfs2_xattr_header *)
1659 ((void *)di + inode->i_sb->s_blocksize -
1660 le16_to_cpu(di->i_xattr_inline_size));
1662 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1667 static int ocfs2_xattr_block_remove(struct inode *inode,
1668 struct buffer_head *blk_bh)
1670 struct ocfs2_xattr_block *xb;
1673 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1674 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1675 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1676 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1678 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1683 static int ocfs2_xattr_free_block(struct inode *inode,
1686 struct inode *xb_alloc_inode;
1687 struct buffer_head *xb_alloc_bh = NULL;
1688 struct buffer_head *blk_bh = NULL;
1689 struct ocfs2_xattr_block *xb;
1690 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1696 ret = ocfs2_read_block(inode, block, &blk_bh);
1702 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1703 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1708 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1714 blk = le64_to_cpu(xb->xb_blkno);
1715 bit = le16_to_cpu(xb->xb_suballoc_bit);
1716 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1718 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1719 EXTENT_ALLOC_SYSTEM_INODE,
1720 le16_to_cpu(xb->xb_suballoc_slot));
1721 if (!xb_alloc_inode) {
1726 mutex_lock(&xb_alloc_inode->i_mutex);
1728 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1734 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1735 if (IS_ERR(handle)) {
1736 ret = PTR_ERR(handle);
1741 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1746 ocfs2_commit_trans(osb, handle);
1748 ocfs2_inode_unlock(xb_alloc_inode, 1);
1749 brelse(xb_alloc_bh);
1751 mutex_unlock(&xb_alloc_inode->i_mutex);
1752 iput(xb_alloc_inode);
1759 * ocfs2_xattr_remove()
1761 * Free extended attribute resources associated with this inode.
1763 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1765 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1766 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1770 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1773 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1776 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1777 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1784 if (di->i_xattr_loc) {
1785 ret = ocfs2_xattr_free_block(inode,
1786 le64_to_cpu(di->i_xattr_loc));
1793 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1794 OCFS2_INODE_UPDATE_CREDITS);
1795 if (IS_ERR(handle)) {
1796 ret = PTR_ERR(handle);
1800 ret = ocfs2_journal_access(handle, inode, di_bh,
1801 OCFS2_JOURNAL_ACCESS_WRITE);
1807 di->i_xattr_loc = 0;
1809 spin_lock(&oi->ip_lock);
1810 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1811 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1812 spin_unlock(&oi->ip_lock);
1814 ret = ocfs2_journal_dirty(handle, di_bh);
1818 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1823 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1824 struct ocfs2_dinode *di)
1826 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1827 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1830 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1833 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1834 struct ocfs2_inline_data *idata = &di->id2.i_data;
1835 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1836 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1837 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1838 le64_to_cpu(di->i_size);
1840 struct ocfs2_extent_list *el = &di->id2.i_list;
1841 free = (le16_to_cpu(el->l_count) -
1842 le16_to_cpu(el->l_next_free_rec)) *
1843 sizeof(struct ocfs2_extent_rec);
1845 if (free >= xattrsize)
1852 * ocfs2_xattr_ibody_find()
1854 * Find extended attribute in inode block and
1855 * fill search info into struct ocfs2_xattr_search.
1857 static int ocfs2_xattr_ibody_find(struct inode *inode,
1860 struct ocfs2_xattr_search *xs)
1862 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1863 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1867 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1870 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1871 down_read(&oi->ip_alloc_sem);
1872 has_space = ocfs2_xattr_has_space_inline(inode, di);
1873 up_read(&oi->ip_alloc_sem);
1878 xs->xattr_bh = xs->inode_bh;
1879 xs->end = (void *)di + inode->i_sb->s_blocksize;
1880 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1881 xs->header = (struct ocfs2_xattr_header *)
1882 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1884 xs->header = (struct ocfs2_xattr_header *)
1885 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1886 xs->base = (void *)xs->header;
1887 xs->here = xs->header->xh_entries;
1889 /* Find the named attribute. */
1890 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1891 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1892 if (ret && ret != -ENODATA)
1894 xs->not_found = ret;
1901 * ocfs2_xattr_ibody_set()
1903 * Set, replace or remove an extended attribute into inode block.
1906 static int ocfs2_xattr_ibody_set(struct inode *inode,
1907 struct ocfs2_xattr_info *xi,
1908 struct ocfs2_xattr_search *xs,
1909 struct ocfs2_xattr_set_ctxt *ctxt)
1911 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1912 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1915 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1918 down_write(&oi->ip_alloc_sem);
1919 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1920 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1926 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
1927 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1929 up_write(&oi->ip_alloc_sem);
1935 * ocfs2_xattr_block_find()
1937 * Find extended attribute in external block and
1938 * fill search info into struct ocfs2_xattr_search.
1940 static int ocfs2_xattr_block_find(struct inode *inode,
1943 struct ocfs2_xattr_search *xs)
1945 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1946 struct buffer_head *blk_bh = NULL;
1947 struct ocfs2_xattr_block *xb;
1950 if (!di->i_xattr_loc)
1953 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1959 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1960 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1965 xs->xattr_bh = blk_bh;
1967 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1968 xs->header = &xb->xb_attrs.xb_header;
1969 xs->base = (void *)xs->header;
1970 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1971 xs->here = xs->header->xh_entries;
1973 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1975 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1979 if (ret && ret != -ENODATA) {
1980 xs->xattr_bh = NULL;
1983 xs->not_found = ret;
1992 * ocfs2_xattr_block_set()
1994 * Set, replace or remove an extended attribute into external block.
1997 static int ocfs2_xattr_block_set(struct inode *inode,
1998 struct ocfs2_xattr_info *xi,
1999 struct ocfs2_xattr_search *xs,
2000 struct ocfs2_xattr_set_ctxt *ctxt)
2002 struct buffer_head *new_bh = NULL;
2003 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2004 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2005 handle_t *handle = ctxt->handle;
2006 struct ocfs2_xattr_block *xblk = NULL;
2007 u16 suballoc_bit_start;
2012 if (!xs->xattr_bh) {
2013 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
2014 OCFS2_JOURNAL_ACCESS_CREATE);
2020 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
2021 &suballoc_bit_start, &num_got,
2028 new_bh = sb_getblk(inode->i_sb, first_blkno);
2029 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2031 ret = ocfs2_journal_access(handle, inode, new_bh,
2032 OCFS2_JOURNAL_ACCESS_CREATE);
2038 /* Initialize ocfs2_xattr_block */
2039 xs->xattr_bh = new_bh;
2040 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2041 memset(xblk, 0, inode->i_sb->s_blocksize);
2042 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2043 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2044 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2045 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2046 xblk->xb_blkno = cpu_to_le64(first_blkno);
2048 xs->header = &xblk->xb_attrs.xb_header;
2049 xs->base = (void *)xs->header;
2050 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2051 xs->here = xs->header->xh_entries;
2053 ret = ocfs2_journal_dirty(handle, new_bh);
2058 di->i_xattr_loc = cpu_to_le64(first_blkno);
2059 ocfs2_journal_dirty(handle, xs->inode_bh);
2061 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2063 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2064 /* Set extended attribute into external block */
2065 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2066 OCFS2_HAS_XATTR_FL);
2067 if (!ret || ret != -ENOSPC)
2070 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2075 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2082 /* Check whether the new xattr can be inserted into the inode. */
2083 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2084 struct ocfs2_xattr_info *xi,
2085 struct ocfs2_xattr_search *xs)
2088 struct ocfs2_xattr_entry *last;
2090 size_t min_offs = xs->end - xs->base;
2095 last = xs->header->xh_entries;
2097 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2098 size_t offs = le16_to_cpu(last->xe_name_offset);
2099 if (offs < min_offs)
2104 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
2108 BUG_ON(!xs->not_found);
2110 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2111 value_size = OCFS2_XATTR_ROOT_SIZE;
2113 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2115 if (free >= sizeof(struct ocfs2_xattr_entry) +
2116 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2122 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2123 struct ocfs2_dinode *di,
2124 struct ocfs2_xattr_info *xi,
2125 struct ocfs2_xattr_search *xis,
2126 struct ocfs2_xattr_search *xbs,
2131 int ret = 0, old_in_xb = 0;
2132 int clusters_add = 0, meta_add = 0, credits = 0;
2133 struct buffer_head *bh = NULL;
2134 struct ocfs2_xattr_block *xb = NULL;
2135 struct ocfs2_xattr_entry *xe = NULL;
2136 struct ocfs2_xattr_value_root *xv = NULL;
2138 int name_offset, name_len = 0;
2139 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2143 if (xis->not_found && xbs->not_found) {
2144 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2146 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2147 clusters_add += new_clusters;
2148 credits += ocfs2_calc_extend_credits(inode->i_sb,
2156 if (!xis->not_found) {
2158 name_offset = le16_to_cpu(xe->xe_name_offset);
2159 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2161 credits += OCFS2_INODE_UPDATE_CREDITS;
2164 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2166 name_offset = le16_to_cpu(xe->xe_name_offset);
2167 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2168 i = xbs->here - xbs->header->xh_entries;
2171 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2172 ret = ocfs2_xattr_bucket_get_name_value(inode,
2173 bucket_xh(xbs->bucket),
2176 base = bucket_block(xbs->bucket, block_off);
2177 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2180 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2185 * delete a xattr doesn't need metadata and cluster allocation.
2186 * so just calculate the credits and return.
2188 * The credits for removing the value tree will be extended
2189 * by ocfs2_remove_extent itself.
2192 if (!ocfs2_xattr_is_local(xe))
2193 credits += OCFS2_REMOVE_EXTENT_CREDITS;
2198 /* do cluster allocation guess first. */
2199 value_size = le64_to_cpu(xe->xe_value_size);
2203 * In xattr set, we always try to set the xe in inode first,
2204 * so if it can be inserted into inode successfully, the old
2205 * one will be removed from the xattr block, and this xattr
2206 * will be inserted into inode as a new xattr in inode.
2208 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2209 clusters_add += new_clusters;
2210 credits += OCFS2_REMOVE_EXTENT_CREDITS +
2211 OCFS2_INODE_UPDATE_CREDITS;
2212 if (!ocfs2_xattr_is_local(xe))
2213 credits += ocfs2_calc_extend_credits(
2221 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2222 /* the new values will be stored outside. */
2223 u32 old_clusters = 0;
2225 if (!ocfs2_xattr_is_local(xe)) {
2226 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2228 xv = (struct ocfs2_xattr_value_root *)
2229 (base + name_offset + name_len);
2233 if (old_clusters >= new_clusters) {
2234 credits += OCFS2_REMOVE_EXTENT_CREDITS;
2237 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2238 clusters_add += new_clusters - old_clusters;
2239 credits += ocfs2_calc_extend_credits(inode->i_sb,
2247 * Now the new value will be stored inside. So if the new
2248 * value is smaller than the size of value root or the old
2249 * value, we don't need any allocation, otherwise we have
2250 * to guess metadata allocation.
2252 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2253 (!ocfs2_xattr_is_local(xe) &&
2254 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2259 /* calculate metadata allocation. */
2260 if (di->i_xattr_loc) {
2261 if (!xbs->xattr_bh) {
2262 ret = ocfs2_read_block(inode,
2263 le64_to_cpu(di->i_xattr_loc),
2270 xb = (struct ocfs2_xattr_block *)bh->b_data;
2272 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2274 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2275 struct ocfs2_extent_list *el =
2276 &xb->xb_attrs.xb_root.xt_list;
2277 meta_add += ocfs2_extend_meta_needed(el);
2278 credits += ocfs2_calc_extend_credits(inode->i_sb,
2283 * This cluster will be used either for new bucket or for
2285 * If the cluster size is the same as the bucket size, one
2286 * more is needed since we may need to extend the bucket
2290 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2291 if (OCFS2_XATTR_BUCKET_SIZE ==
2292 OCFS2_SB(inode->i_sb)->s_clustersize) {
2293 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2298 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2302 *clusters_need = clusters_add;
2304 *meta_need = meta_add;
2306 *credits_need = credits;
2311 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2312 struct ocfs2_dinode *di,
2313 struct ocfs2_xattr_info *xi,
2314 struct ocfs2_xattr_search *xis,
2315 struct ocfs2_xattr_search *xbs,
2316 struct ocfs2_xattr_set_ctxt *ctxt,
2319 int clusters_add, meta_add, ret;
2320 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2322 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2324 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2326 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2327 &clusters_add, &meta_add, credits);
2333 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2334 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2337 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2346 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2352 if (ctxt->meta_ac) {
2353 ocfs2_free_alloc_context(ctxt->meta_ac);
2354 ctxt->meta_ac = NULL;
2358 * We cannot have an error and a non null ctxt->data_ac.
2365 static int __ocfs2_xattr_set_handle(struct inode *inode,
2366 struct ocfs2_dinode *di,
2367 struct ocfs2_xattr_info *xi,
2368 struct ocfs2_xattr_search *xis,
2369 struct ocfs2_xattr_search *xbs,
2370 struct ocfs2_xattr_set_ctxt *ctxt)
2372 int ret = 0, credits;
2375 /* Remove existing extended attribute */
2376 if (!xis->not_found)
2377 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2378 else if (!xbs->not_found)
2379 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2381 /* We always try to set extended attribute into inode first*/
2382 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2383 if (!ret && !xbs->not_found) {
2385 * If succeed and that extended attribute existing in
2386 * external block, then we will remove it.
2391 xis->not_found = -ENODATA;
2392 ret = ocfs2_calc_xattr_set_need(inode,
2405 ret = ocfs2_extend_trans(ctxt->handle, credits +
2406 ctxt->handle->h_buffer_credits);
2411 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2412 } else if (ret == -ENOSPC) {
2413 if (di->i_xattr_loc && !xbs->xattr_bh) {
2414 ret = ocfs2_xattr_block_find(inode,
2420 xis->not_found = -ENODATA;
2421 ret = ocfs2_calc_xattr_set_need(inode,
2434 ret = ocfs2_extend_trans(ctxt->handle, credits +
2435 ctxt->handle->h_buffer_credits);
2442 * If no space in inode, we will set extended attribute
2443 * into external block.
2445 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2448 if (!xis->not_found) {
2450 * If succeed and that extended attribute
2451 * existing in inode, we will remove it.
2455 xbs->not_found = -ENODATA;
2456 ret = ocfs2_calc_xattr_set_need(inode,
2469 ret = ocfs2_extend_trans(ctxt->handle, credits +
2470 ctxt->handle->h_buffer_credits);
2475 ret = ocfs2_xattr_ibody_set(inode, xi,
2486 * This function only called duing creating inode
2487 * for init security/acl xattrs of the new inode.
2488 * The xattrs could be put into ibody or extent block,
2489 * xattr bucket would not be use in this case.
2490 * transanction credits also be reserved in here.
2492 int ocfs2_xattr_set_handle(handle_t *handle,
2493 struct inode *inode,
2494 struct buffer_head *di_bh,
2500 struct ocfs2_alloc_context *meta_ac,
2501 struct ocfs2_alloc_context *data_ac)
2503 struct ocfs2_dinode *di;
2506 struct ocfs2_xattr_info xi = {
2507 .name_index = name_index,
2510 .value_len = value_len,
2513 struct ocfs2_xattr_search xis = {
2514 .not_found = -ENODATA,
2517 struct ocfs2_xattr_search xbs = {
2518 .not_found = -ENODATA,
2521 struct ocfs2_xattr_set_ctxt ctxt = {
2527 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2530 xis.inode_bh = xbs.inode_bh = di_bh;
2531 di = (struct ocfs2_dinode *)di_bh->b_data;
2533 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2535 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2538 if (xis.not_found) {
2539 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2544 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2547 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2548 brelse(xbs.xattr_bh);
2556 * Set, replace or remove an extended attribute for this inode.
2557 * value is NULL to remove an existing extended attribute, else either
2558 * create or replace an extended attribute.
2560 int ocfs2_xattr_set(struct inode *inode,
2567 struct buffer_head *di_bh = NULL;
2568 struct ocfs2_dinode *di;
2570 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2571 struct inode *tl_inode = osb->osb_tl_inode;
2572 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2574 struct ocfs2_xattr_info xi = {
2575 .name_index = name_index,
2578 .value_len = value_len,
2581 struct ocfs2_xattr_search xis = {
2582 .not_found = -ENODATA,
2585 struct ocfs2_xattr_search xbs = {
2586 .not_found = -ENODATA,
2589 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2593 * Only xbs will be used on indexed trees. xis doesn't need a
2596 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2598 mlog_errno(-ENOMEM);
2602 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2605 goto cleanup_nolock;
2607 xis.inode_bh = xbs.inode_bh = di_bh;
2608 di = (struct ocfs2_dinode *)di_bh->b_data;
2610 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2612 * Scan inode and external block to find the same name
2613 * extended attribute and collect search infomation.
2615 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2618 if (xis.not_found) {
2619 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2624 if (xis.not_found && xbs.not_found) {
2626 if (flags & XATTR_REPLACE)
2633 if (flags & XATTR_CREATE)
2638 mutex_lock(&tl_inode->i_mutex);
2640 if (ocfs2_truncate_log_needs_flush(osb)) {
2641 ret = __ocfs2_flush_truncate_log(osb);
2643 mutex_unlock(&tl_inode->i_mutex);
2648 mutex_unlock(&tl_inode->i_mutex);
2650 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2651 &xbs, &ctxt, &credits);
2657 ctxt.handle = ocfs2_start_trans(osb, credits);
2658 if (IS_ERR(ctxt.handle)) {
2659 ret = PTR_ERR(ctxt.handle);
2664 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2666 ocfs2_commit_trans(osb, ctxt.handle);
2669 ocfs2_free_alloc_context(ctxt.data_ac);
2671 ocfs2_free_alloc_context(ctxt.meta_ac);
2672 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2673 ocfs2_schedule_truncate_log_flush(osb, 1);
2674 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2676 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2677 ocfs2_inode_unlock(inode, 1);
2680 brelse(xbs.xattr_bh);
2681 ocfs2_xattr_bucket_free(xbs.bucket);
2687 * Find the xattr extent rec which may contains name_hash.
2688 * e_cpos will be the first name hash of the xattr rec.
2689 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2691 static int ocfs2_xattr_get_rec(struct inode *inode,
2696 struct ocfs2_extent_list *el)
2699 struct buffer_head *eb_bh = NULL;
2700 struct ocfs2_extent_block *eb;
2701 struct ocfs2_extent_rec *rec = NULL;
2704 if (el->l_tree_depth) {
2705 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2711 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2714 if (el->l_tree_depth) {
2715 ocfs2_error(inode->i_sb,
2716 "Inode %lu has non zero tree depth in "
2717 "xattr tree block %llu\n", inode->i_ino,
2718 (unsigned long long)eb_bh->b_blocknr);
2724 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2725 rec = &el->l_recs[i];
2727 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2728 e_blkno = le64_to_cpu(rec->e_blkno);
2734 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2735 "record (%u, %u, 0) in xattr", inode->i_ino,
2736 le32_to_cpu(rec->e_cpos),
2737 ocfs2_rec_clusters(el, rec));
2742 *p_blkno = le64_to_cpu(rec->e_blkno);
2743 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2745 *e_cpos = le32_to_cpu(rec->e_cpos);
2751 typedef int (xattr_bucket_func)(struct inode *inode,
2752 struct ocfs2_xattr_bucket *bucket,
2755 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2756 struct ocfs2_xattr_bucket *bucket,
2763 int i, ret = 0, cmp = 1, block_off, new_offset;
2764 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
2765 size_t name_len = strlen(name);
2766 struct ocfs2_xattr_entry *xe = NULL;
2770 * We don't use binary search in the bucket because there
2771 * may be multiple entries with the same name hash.
2773 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2774 xe = &xh->xh_entries[i];
2776 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2778 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2781 cmp = name_index - ocfs2_xattr_get_type(xe);
2783 cmp = name_len - xe->xe_name_len;
2787 ret = ocfs2_xattr_bucket_get_name_value(inode,
2797 xe_name = bucket_block(bucket, block_off) + new_offset;
2798 if (!memcmp(name, xe_name, name_len)) {
2810 * Find the specified xattr entry in a series of buckets.
2811 * This series start from p_blkno and last for num_clusters.
2812 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2813 * the num of the valid buckets.
2815 * Return the buffer_head this xattr should reside in. And if the xattr's
2816 * hash is in the gap of 2 buckets, return the lower bucket.
2818 static int ocfs2_xattr_bucket_find(struct inode *inode,
2825 struct ocfs2_xattr_search *xs)
2828 struct ocfs2_xattr_header *xh = NULL;
2829 struct ocfs2_xattr_entry *xe = NULL;
2831 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2832 int low_bucket = 0, bucket, high_bucket;
2833 struct ocfs2_xattr_bucket *search;
2835 u64 blkno, lower_blkno = 0;
2837 search = ocfs2_xattr_bucket_new(inode);
2844 ret = ocfs2_read_xattr_bucket(search, p_blkno);
2850 xh = bucket_xh(search);
2851 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2852 while (low_bucket <= high_bucket) {
2853 ocfs2_xattr_bucket_relse(search);
2855 bucket = (low_bucket + high_bucket) / 2;
2856 blkno = p_blkno + bucket * blk_per_bucket;
2857 ret = ocfs2_read_xattr_bucket(search, blkno);
2863 xh = bucket_xh(search);
2864 xe = &xh->xh_entries[0];
2865 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2866 high_bucket = bucket - 1;
2871 * Check whether the hash of the last entry in our
2872 * bucket is larger than the search one. for an empty
2873 * bucket, the last one is also the first one.
2876 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2878 last_hash = le32_to_cpu(xe->xe_name_hash);
2880 /* record lower_blkno which may be the insert place. */
2881 lower_blkno = blkno;
2883 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2884 low_bucket = bucket + 1;
2888 /* the searched xattr should reside in this bucket if exists. */
2889 ret = ocfs2_find_xe_in_bucket(inode, search,
2890 name_index, name, name_hash,
2900 * Record the bucket we have found.
2901 * When the xattr's hash value is in the gap of 2 buckets, we will
2902 * always set it to the previous bucket.
2905 lower_blkno = p_blkno;
2907 /* This should be in cache - we just read it during the search */
2908 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2914 xs->header = bucket_xh(xs->bucket);
2915 xs->base = bucket_block(xs->bucket, 0);
2916 xs->end = xs->base + inode->i_sb->s_blocksize;
2919 xs->here = &xs->header->xh_entries[index];
2920 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2921 (unsigned long long)bucket_blkno(xs->bucket), index);
2926 ocfs2_xattr_bucket_free(search);
2930 static int ocfs2_xattr_index_block_find(struct inode *inode,
2931 struct buffer_head *root_bh,
2934 struct ocfs2_xattr_search *xs)
2937 struct ocfs2_xattr_block *xb =
2938 (struct ocfs2_xattr_block *)root_bh->b_data;
2939 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2940 struct ocfs2_extent_list *el = &xb_root->xt_list;
2942 u32 first_hash, num_clusters = 0;
2943 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2945 if (le16_to_cpu(el->l_next_free_rec) == 0)
2948 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2949 name, name_hash, name_index);
2951 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2958 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2960 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2961 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
2964 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2965 p_blkno, first_hash, num_clusters, xs);
2971 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2974 xattr_bucket_func *func,
2978 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2979 u32 num_buckets = clusters * bpc;
2980 struct ocfs2_xattr_bucket *bucket;
2982 bucket = ocfs2_xattr_bucket_new(inode);
2984 mlog_errno(-ENOMEM);
2988 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2989 clusters, (unsigned long long)blkno);
2991 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
2992 ret = ocfs2_read_xattr_bucket(bucket, blkno);
2999 * The real bucket num in this series of blocks is stored
3000 * in the 1st bucket.
3003 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3005 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3006 (unsigned long long)blkno,
3007 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3009 ret = func(inode, bucket, para);
3012 /* Fall through to bucket_relse() */
3015 ocfs2_xattr_bucket_relse(bucket);
3020 ocfs2_xattr_bucket_free(bucket);
3024 struct ocfs2_xattr_tree_list {
3030 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
3031 struct ocfs2_xattr_header *xh,
3038 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3041 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3043 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
3044 *new_offset = name_offset % inode->i_sb->s_blocksize;
3049 static int ocfs2_list_xattr_bucket(struct inode *inode,
3050 struct ocfs2_xattr_bucket *bucket,
3054 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3055 int i, block_off, new_offset;
3056 const char *prefix, *name;
3058 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3059 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3060 type = ocfs2_xattr_get_type(entry);
3061 prefix = ocfs2_xattr_prefix(type);
3064 ret = ocfs2_xattr_bucket_get_name_value(inode,
3072 name = (const char *)bucket_block(bucket, block_off) +
3074 ret = ocfs2_xattr_list_entry(xl->buffer,
3078 entry->xe_name_len);
3087 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3088 struct ocfs2_xattr_tree_root *xt,
3092 struct ocfs2_extent_list *el = &xt->xt_list;
3094 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3096 struct ocfs2_xattr_tree_list xl = {
3098 .buffer_size = buffer_size,
3102 if (le16_to_cpu(el->l_next_free_rec) == 0)
3105 while (name_hash > 0) {
3106 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3107 &e_cpos, &num_clusters, el);
3113 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3114 ocfs2_list_xattr_bucket,
3124 name_hash = e_cpos - 1;
3132 static int cmp_xe(const void *a, const void *b)
3134 const struct ocfs2_xattr_entry *l = a, *r = b;
3135 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3136 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3138 if (l_hash > r_hash)
3140 if (l_hash < r_hash)
3145 static void swap_xe(void *a, void *b, int size)
3147 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3150 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3151 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3155 * When the ocfs2_xattr_block is filled up, new bucket will be created
3156 * and all the xattr entries will be moved to the new bucket.
3157 * The header goes at the start of the bucket, and the names+values are
3158 * filled from the end. This is why *target starts as the last buffer.
3159 * Note: we need to sort the entries since they are not saved in order
3160 * in the ocfs2_xattr_block.
3162 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3163 struct buffer_head *xb_bh,
3164 struct ocfs2_xattr_bucket *bucket)
3166 int i, blocksize = inode->i_sb->s_blocksize;
3167 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3168 u16 offset, size, off_change;
3169 struct ocfs2_xattr_entry *xe;
3170 struct ocfs2_xattr_block *xb =
3171 (struct ocfs2_xattr_block *)xb_bh->b_data;
3172 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
3173 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3174 u16 count = le16_to_cpu(xb_xh->xh_count);
3175 char *src = xb_bh->b_data;
3176 char *target = bucket_block(bucket, blks - 1);
3178 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3179 (unsigned long long)xb_bh->b_blocknr,
3180 (unsigned long long)bucket_blkno(bucket));
3182 for (i = 0; i < blks; i++)
3183 memset(bucket_block(bucket, i), 0, blocksize);
3186 * Since the xe_name_offset is based on ocfs2_xattr_header,
3187 * there is a offset change corresponding to the change of
3188 * ocfs2_xattr_header's position.
3190 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3191 xe = &xb_xh->xh_entries[count - 1];
3192 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3193 size = blocksize - offset;
3195 /* copy all the names and values. */
3196 memcpy(target + offset, src + offset, size);
3198 /* Init new header now. */
3199 xh->xh_count = xb_xh->xh_count;
3200 xh->xh_num_buckets = cpu_to_le16(1);
3201 xh->xh_name_value_len = cpu_to_le16(size);
3202 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3204 /* copy all the entries. */
3205 target = bucket_block(bucket, 0);
3206 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3207 size = count * sizeof(struct ocfs2_xattr_entry);
3208 memcpy(target + offset, (char *)xb_xh + offset, size);
3210 /* Change the xe offset for all the xe because of the move. */
3211 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3212 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3213 for (i = 0; i < count; i++)
3214 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3216 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3217 offset, size, off_change);
3219 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3224 * After we move xattr from block to index btree, we have to
3225 * update ocfs2_xattr_search to the new xe and base.
3227 * When the entry is in xattr block, xattr_bh indicates the storage place.
3228 * While if the entry is in index b-tree, "bucket" indicates the
3229 * real place of the xattr.
3231 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3232 struct ocfs2_xattr_search *xs,
3233 struct buffer_head *old_bh)
3235 char *buf = old_bh->b_data;
3236 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3237 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
3240 xs->header = bucket_xh(xs->bucket);
3241 xs->base = bucket_block(xs->bucket, 0);
3242 xs->end = xs->base + inode->i_sb->s_blocksize;
3247 i = xs->here - old_xh->xh_entries;
3248 xs->here = &xs->header->xh_entries[i];
3251 static int ocfs2_xattr_create_index_block(struct inode *inode,
3252 struct ocfs2_xattr_search *xs,
3253 struct ocfs2_xattr_set_ctxt *ctxt)
3258 handle_t *handle = ctxt->handle;
3259 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3260 struct ocfs2_inode_info *oi = OCFS2_I(inode);
3261 struct buffer_head *xb_bh = xs->xattr_bh;
3262 struct ocfs2_xattr_block *xb =
3263 (struct ocfs2_xattr_block *)xb_bh->b_data;
3264 struct ocfs2_xattr_tree_root *xr;
3265 u16 xb_flags = le16_to_cpu(xb->xb_flags);
3267 mlog(0, "create xattr index block for %llu\n",
3268 (unsigned long long)xb_bh->b_blocknr);
3270 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
3271 BUG_ON(!xs->bucket);
3275 * We can use this lock for now, and maybe move to a dedicated mutex
3276 * if performance becomes a problem later.
3278 down_write(&oi->ip_alloc_sem);
3280 ret = ocfs2_journal_access(handle, inode, xb_bh,
3281 OCFS2_JOURNAL_ACCESS_WRITE);
3287 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3288 1, 1, &bit_off, &len);
3295 * The bucket may spread in many blocks, and
3296 * we will only touch the 1st block and the last block
3297 * in the whole bucket(one for entry and one for data).
3299 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3301 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3302 (unsigned long long)blkno);
3304 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
3310 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3311 OCFS2_JOURNAL_ACCESS_CREATE);
3317 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3318 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3320 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3322 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3323 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3324 offsetof(struct ocfs2_xattr_block, xb_attrs));
3326 xr = &xb->xb_attrs.xb_root;
3327 xr->xt_clusters = cpu_to_le32(1);
3328 xr->xt_last_eb_blk = 0;
3329 xr->xt_list.l_tree_depth = 0;
3330 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3331 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3333 xr->xt_list.l_recs[0].e_cpos = 0;
3334 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3335 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3337 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3339 ocfs2_journal_dirty(handle, xb_bh);
3342 up_write(&oi->ip_alloc_sem);
3347 static int cmp_xe_offset(const void *a, const void *b)
3349 const struct ocfs2_xattr_entry *l = a, *r = b;
3350 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3351 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3353 if (l_name_offset < r_name_offset)
3355 if (l_name_offset > r_name_offset)
3361 * defrag a xattr bucket if we find that the bucket has some
3362 * holes beteen name/value pairs.
3363 * We will move all the name/value pairs to the end of the bucket
3364 * so that we can spare some space for insertion.
3366 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3368 struct ocfs2_xattr_bucket *bucket)
3371 size_t end, offset, len, value_len;
3372 struct ocfs2_xattr_header *xh;
3373 char *entries, *buf, *bucket_buf = NULL;
3374 u64 blkno = bucket_blkno(bucket);
3376 size_t blocksize = inode->i_sb->s_blocksize;
3377 struct ocfs2_xattr_entry *xe;
3380 * In order to make the operation more efficient and generic,
3381 * we copy all the blocks into a contiguous memory and do the
3382 * defragment there, so if anything is error, we will not touch
3385 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3392 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3393 memcpy(buf, bucket_block(bucket, i), blocksize);
3395 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
3396 OCFS2_JOURNAL_ACCESS_WRITE);
3402 xh = (struct ocfs2_xattr_header *)bucket_buf;
3403 entries = (char *)xh->xh_entries;
3404 xh_free_start = le16_to_cpu(xh->xh_free_start);
3406 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3407 "xh_free_start = %u, xh_name_value_len = %u.\n",
3408 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3409 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
3412 * sort all the entries by their offset.
3413 * the largest will be the first, so that we can
3414 * move them to the end one by one.
3416 sort(entries, le16_to_cpu(xh->xh_count),
3417 sizeof(struct ocfs2_xattr_entry),
3418 cmp_xe_offset, swap_xe);
3420 /* Move all name/values to the end of the bucket. */
3421 xe = xh->xh_entries;
3422 end = OCFS2_XATTR_BUCKET_SIZE;
3423 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3424 offset = le16_to_cpu(xe->xe_name_offset);
3425 if (ocfs2_xattr_is_local(xe))
3426 value_len = OCFS2_XATTR_SIZE(
3427 le64_to_cpu(xe->xe_value_size));
3429 value_len = OCFS2_XATTR_ROOT_SIZE;
3430 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3433 * We must make sure that the name/value pair
3434 * exist in the same block. So adjust end to
3435 * the previous block end if needed.
3437 if (((end - len) / blocksize !=
3438 (end - 1) / blocksize))
3439 end = end - end % blocksize;
3441 if (end > offset + len) {
3442 memmove(bucket_buf + end - len,
3443 bucket_buf + offset, len);
3444 xe->xe_name_offset = cpu_to_le16(end - len);
3447 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3448 "bucket %llu\n", (unsigned long long)blkno);
3453 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3454 "bucket %llu\n", (unsigned long long)blkno);
3456 if (xh_free_start == end)
3459 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3460 xh->xh_free_start = cpu_to_le16(end);
3462 /* sort the entries by their name_hash. */
3463 sort(entries, le16_to_cpu(xh->xh_count),
3464 sizeof(struct ocfs2_xattr_entry),
3468 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3469 memcpy(bucket_block(bucket, i), buf, blocksize);
3470 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
3478 * Move half nums of the xattr bucket in the previous cluster to this new
3479 * cluster. We only touch the last cluster of the previous extend record.
3481 * first_bh is the first buffer_head of a series of bucket in the same
3482 * extent rec and header_bh is the header of one bucket in this cluster.
3483 * They will be updated if we move the data header_bh contains to the new
3484 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3486 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3488 struct buffer_head **first_bh,
3489 struct buffer_head **header_bh,
3495 int i, ret, credits;
3496 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3497 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3498 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3499 int blocksize = inode->i_sb->s_blocksize;
3500 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3501 struct ocfs2_xattr_header *new_xh;
3502 struct ocfs2_xattr_header *xh =
3503 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3505 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3506 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3508 prev_bh = *first_bh;
3510 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3512 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3514 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3515 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
3518 * We need to update the 1st half of the new cluster and
3519 * 1 more for the update of the 1st bucket of the previous
3522 credits = bpc / 2 + 1 + handle->h_buffer_credits;
3523 ret = ocfs2_extend_trans(handle, credits);
3529 ret = ocfs2_journal_access(handle, inode, prev_bh,
3530 OCFS2_JOURNAL_ACCESS_WRITE);
3536 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3537 old_bh = new_bh = NULL;
3538 new_bh = sb_getblk(inode->i_sb, new_blkno);
3545 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3547 ret = ocfs2_journal_access(handle, inode, new_bh,
3548 OCFS2_JOURNAL_ACCESS_CREATE);
3555 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3562 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3565 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3566 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3569 *first_hash = le32_to_cpu(
3570 new_xh->xh_entries[0].xe_name_hash);
3571 new_first_bh = new_bh;
3572 get_bh(new_first_bh);
3575 ocfs2_journal_dirty(handle, new_bh);
3577 if (*header_bh == old_bh) {
3579 *header_bh = new_bh;
3583 *first_bh = new_first_bh;
3590 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3592 ocfs2_journal_dirty(handle, prev_bh);
3595 brelse(new_first_bh);
3600 * Find the suitable pos when we divide a bucket into 2.
3601 * We have to make sure the xattrs with the same hash value exist
3602 * in the same bucket.
3604 * If this ocfs2_xattr_header covers more than one hash value, find a
3605 * place where the hash value changes. Try to find the most even split.
3606 * The most common case is that all entries have different hash values,
3607 * and the first check we make will find a place to split.
3609 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3611 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3612 int count = le16_to_cpu(xh->xh_count);
3613 int delta, middle = count / 2;
3616 * We start at the middle. Each step gets farther away in both
3617 * directions. We therefore hit the change in hash value
3618 * nearest to the middle. Note that this loop does not execute for
3621 for (delta = 0; delta < middle; delta++) {
3622 /* Let's check delta earlier than middle */
3623 if (cmp_xe(&entries[middle - delta - 1],
3624 &entries[middle - delta]))
3625 return middle - delta;
3627 /* For even counts, don't walk off the end */
3628 if ((middle + delta + 1) == count)
3631 /* Now try delta past middle */
3632 if (cmp_xe(&entries[middle + delta],
3633 &entries[middle + delta + 1]))
3634 return middle + delta + 1;
3637 /* Every entry had the same hash */
3642 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3643 * first_hash will record the 1st hash of the new bucket.
3645 * Normally half of the xattrs will be moved. But we have to make
3646 * sure that the xattrs with the same hash value are stored in the
3647 * same bucket. If all the xattrs in this bucket have the same hash
3648 * value, the new bucket will be initialized as an empty one and the
3649 * first_hash will be initialized as (hash_value+1).
3651 static int ocfs2_divide_xattr_bucket(struct inode *inode,
3656 int new_bucket_head)
3659 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
3660 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3661 struct ocfs2_xattr_header *xh;
3662 struct ocfs2_xattr_entry *xe;
3663 int blocksize = inode->i_sb->s_blocksize;
3665 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3666 (unsigned long long)blk, (unsigned long long)new_blk);
3668 s_bucket = ocfs2_xattr_bucket_new(inode);
3669 t_bucket = ocfs2_xattr_bucket_new(inode);
3670 if (!s_bucket || !t_bucket) {
3676 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
3682 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
3683 OCFS2_JOURNAL_ACCESS_WRITE);
3690 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3691 * there's no need to read it.
3693 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
3699 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3701 OCFS2_JOURNAL_ACCESS_CREATE :
3702 OCFS2_JOURNAL_ACCESS_WRITE);
3708 xh = bucket_xh(s_bucket);
3709 count = le16_to_cpu(xh->xh_count);
3710 start = ocfs2_xattr_find_divide_pos(xh);
3712 if (start == count) {
3713 xe = &xh->xh_entries[start-1];
3716 * initialized a new empty bucket here.
3717 * The hash value is set as one larger than
3718 * that of the last entry in the previous bucket.
3720 for (i = 0; i < t_bucket->bu_blocks; i++)
3721 memset(bucket_block(t_bucket, i), 0, blocksize);
3723 xh = bucket_xh(t_bucket);
3724 xh->xh_free_start = cpu_to_le16(blocksize);
3725 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3726 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3728 goto set_num_buckets;
3731 /* copy the whole bucket to the new first. */
3732 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3734 /* update the new bucket. */
3735 xh = bucket_xh(t_bucket);
3738 * Calculate the total name/value len and xh_free_start for
3739 * the old bucket first.
3741 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3743 for (i = 0; i < start; i++) {
3744 xe = &xh->xh_entries[i];
3745 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3746 if (ocfs2_xattr_is_local(xe))
3748 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3750 xe_len += OCFS2_XATTR_ROOT_SIZE;
3751 name_value_len += xe_len;
3752 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3753 name_offset = le16_to_cpu(xe->xe_name_offset);
3757 * Now begin the modification to the new bucket.
3759 * In the new bucket, We just move the xattr entry to the beginning
3760 * and don't touch the name/value. So there will be some holes in the
3761 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3764 xe = &xh->xh_entries[start];
3765 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3766 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3767 (int)((char *)xe - (char *)xh),
3768 (int)((char *)xh->xh_entries - (char *)xh));
3769 memmove((char *)xh->xh_entries, (char *)xe, len);
3770 xe = &xh->xh_entries[count - start];
3771 len = sizeof(struct ocfs2_xattr_entry) * start;
3772 memset((char *)xe, 0, len);
3774 le16_add_cpu(&xh->xh_count, -start);
3775 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3777 /* Calculate xh_free_start for the new bucket. */
3778 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3779 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3780 xe = &xh->xh_entries[i];
3781 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3782 if (ocfs2_xattr_is_local(xe))
3784 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3786 xe_len += OCFS2_XATTR_ROOT_SIZE;
3787 if (le16_to_cpu(xe->xe_name_offset) <
3788 le16_to_cpu(xh->xh_free_start))
3789 xh->xh_free_start = xe->xe_name_offset;
3793 /* set xh->xh_num_buckets for the new xh. */
3794 if (new_bucket_head)
3795 xh->xh_num_buckets = cpu_to_le16(1);
3797 xh->xh_num_buckets = 0;
3799 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3801 /* store the first_hash of the new bucket. */
3803 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3806 * Now only update the 1st block of the old bucket. If we
3807 * just added a new empty bucket, there is no need to modify
3813 xh = bucket_xh(s_bucket);
3814 memset(&xh->xh_entries[start], 0,
3815 sizeof(struct ocfs2_xattr_entry) * (count - start));
3816 xh->xh_count = cpu_to_le16(start);
3817 xh->xh_free_start = cpu_to_le16(name_offset);
3818 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3820 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
3823 ocfs2_xattr_bucket_free(s_bucket);
3824 ocfs2_xattr_bucket_free(t_bucket);
3830 * Copy xattr from one bucket to another bucket.
3832 * The caller must make sure that the journal transaction
3833 * has enough space for journaling.
3835 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3842 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3844 BUG_ON(s_blkno == t_blkno);
3846 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3847 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3850 s_bucket = ocfs2_xattr_bucket_new(inode);
3851 t_bucket = ocfs2_xattr_bucket_new(inode);
3852 if (!s_bucket || !t_bucket) {
3858 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
3863 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3864 * there's no need to read it.
3866 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
3870 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3872 OCFS2_JOURNAL_ACCESS_CREATE :
3873 OCFS2_JOURNAL_ACCESS_WRITE);
3877 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3878 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3881 ocfs2_xattr_bucket_free(t_bucket);
3882 ocfs2_xattr_bucket_free(s_bucket);
3888 * Copy one xattr cluster from src_blk to to_blk.
3889 * The to_blk will become the first bucket header of the cluster, so its
3890 * xh_num_buckets will be initialized as the bucket num in the cluster.
3892 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3894 struct buffer_head *first_bh,
3899 int i, ret, credits;
3900 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3901 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3902 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3903 struct buffer_head *bh = NULL;
3904 struct ocfs2_xattr_header *xh;
3905 u64 to_blk_start = to_blk;
3907 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3908 (unsigned long long)src_blk, (unsigned long long)to_blk);
3911 * We need to update the new cluster and 1 more for the update of
3912 * the 1st bucket of the previous extent rec.
3914 credits = bpc + 1 + handle->h_buffer_credits;
3915 ret = ocfs2_extend_trans(handle, credits);
3921 ret = ocfs2_journal_access(handle, inode, first_bh,
3922 OCFS2_JOURNAL_ACCESS_WRITE);
3928 for (i = 0; i < num_buckets; i++) {
3929 ret = ocfs2_cp_xattr_bucket(inode, handle,
3930 src_blk, to_blk, 1);
3936 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3937 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3940 /* update the old bucket header. */
3941 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3942 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3944 ocfs2_journal_dirty(handle, first_bh);
3946 /* update the new bucket header. */
3947 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3953 ret = ocfs2_journal_access(handle, inode, bh,
3954 OCFS2_JOURNAL_ACCESS_WRITE);
3960 xh = (struct ocfs2_xattr_header *)bh->b_data;
3961 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3963 ocfs2_journal_dirty(handle, bh);
3966 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3973 * Move some xattrs in this cluster to the new cluster.
3974 * This function should only be called when bucket size == cluster size.
3975 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3977 static int ocfs2_divide_xattr_cluster(struct inode *inode,
3983 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3984 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
3986 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3988 ret = ocfs2_extend_trans(handle, credits);
3994 /* Move half of the xattr in start_blk to the next bucket. */
3995 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
3996 new_blk, first_hash, 1);
4000 * Move some xattrs from the old cluster to the new one since they are not
4001 * contiguous in ocfs2 xattr tree.
4003 * new_blk starts a new separate cluster, and we will move some xattrs from
4004 * prev_blk to it. v_start will be set as the first name hash value in this
4005 * new cluster so that it can be used as e_cpos during tree insertion and
4006 * don't collide with our original b-tree operations. first_bh and header_bh
4007 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4008 * to extend the insert bucket.
4010 * The problem is how much xattr should we move to the new one and when should
4011 * we update first_bh and header_bh?
4012 * 1. If cluster size > bucket size, that means the previous cluster has more
4013 * than 1 bucket, so just move half nums of bucket into the new cluster and
4014 * update the first_bh and header_bh if the insert bucket has been moved
4015 * to the new cluster.
4016 * 2. If cluster_size == bucket_size:
4017 * a) If the previous extent rec has more than one cluster and the insert
4018 * place isn't in the last cluster, copy the entire last cluster to the
4019 * new one. This time, we don't need to upate the first_bh and header_bh
4020 * since they will not be moved into the new cluster.
4021 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4022 * the new one. And we set the extend flag to zero if the insert place is
4023 * moved into the new allocated cluster since no extend is needed.
4025 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4027 struct buffer_head **first_bh,
4028 struct buffer_head **header_bh,
4036 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4038 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4039 (unsigned long long)prev_blk, prev_clusters,
4040 (unsigned long long)new_blk);
4042 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
4043 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4052 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
4054 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
4055 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
4059 ret = ocfs2_divide_xattr_cluster(inode, handle,
4063 if ((*header_bh)->b_blocknr == last_blk && extend)
4072 * Add a new cluster for xattr storage.
4074 * If the new cluster is contiguous with the previous one, it will be
4075 * appended to the same extent record, and num_clusters will be updated.
4076 * If not, we will insert a new extent for it and move some xattrs in
4077 * the last cluster into the new allocated one.
4078 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4079 * lose the benefits of hashing because we'll have to search large leaves.
4080 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4083 * first_bh is the first block of the previous extent rec and header_bh
4084 * indicates the bucket we will insert the new xattrs. They will be updated
4085 * when the header_bh is moved into the new cluster.
4087 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4088 struct buffer_head *root_bh,
4089 struct buffer_head **first_bh,
4090 struct buffer_head **header_bh,
4095 struct ocfs2_xattr_set_ctxt *ctxt)
4098 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4099 u32 prev_clusters = *num_clusters;
4100 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4102 handle_t *handle = ctxt->handle;
4103 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4104 struct ocfs2_extent_tree et;
4106 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4107 "previous xattr blkno = %llu\n",
4108 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4109 prev_cpos, (unsigned long long)prev_blkno);
4111 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4113 ret = ocfs2_journal_access(handle, inode, root_bh,
4114 OCFS2_JOURNAL_ACCESS_WRITE);
4120 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
4121 clusters_to_add, &bit_off, &num_bits);
4128 BUG_ON(num_bits > clusters_to_add);
4130 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4131 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4132 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4134 if (prev_blkno + prev_clusters * bpc == block &&
4135 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4136 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4138 * If this cluster is contiguous with the old one and
4139 * adding this new cluster, we don't surpass the limit of
4140 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4141 * initialized and used like other buckets in the previous
4143 * So add it as a contiguous one. The caller will handle
4146 v_start = prev_cpos + prev_clusters;
4147 *num_clusters = prev_clusters + num_bits;
4148 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4151 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4166 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4167 num_bits, (unsigned long long)block, v_start);
4168 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
4169 num_bits, 0, ctxt->meta_ac);
4175 ret = ocfs2_journal_dirty(handle, root_bh);
4184 * Extend a new xattr bucket and move xattrs to the end one by one until
4185 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
4187 static int ocfs2_extend_xattr_bucket(struct inode *inode,
4189 struct buffer_head *first_bh,
4190 struct buffer_head *start_bh,
4194 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4195 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4196 u64 start_blk = start_bh->b_blocknr, end_blk;
4197 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
4198 struct ocfs2_xattr_header *first_xh =
4199 (struct ocfs2_xattr_header *)first_bh->b_data;
4200 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
4202 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4203 "from %llu, len = %u\n", (unsigned long long)start_blk,
4204 (unsigned long long)first_bh->b_blocknr, num_clusters);
4206 BUG_ON(bucket >= num_buckets);
4208 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
4211 * We will touch all the buckets after the start_bh(include it).
4212 * Then we add one more bucket.
4214 credits = end_blk - start_blk + 3 * blk_per_bucket + 1 +
4215 handle->h_buffer_credits;
4216 ret = ocfs2_extend_trans(handle, credits);
4222 ret = ocfs2_journal_access(handle, inode, first_bh,
4223 OCFS2_JOURNAL_ACCESS_WRITE);
4229 while (end_blk != start_blk) {
4230 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4231 end_blk + blk_per_bucket, 0);
4234 end_blk -= blk_per_bucket;
4237 /* Move half of the xattr in start_blk to the next bucket. */
4238 ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
4239 start_blk + blk_per_bucket, NULL, 0);
4241 le16_add_cpu(&first_xh->xh_num_buckets, 1);
4242 ocfs2_journal_dirty(handle, first_bh);
4249 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
4250 * xb_bh is the ocfs2_xattr_block.
4251 * We will move all the buckets starting from header_bh to the next place. As
4252 * for this one, half num of its xattrs will be moved to the next one.
4254 * We will allocate a new cluster if current cluster is full and adjust
4255 * header_bh and first_bh if the insert place is moved to the new cluster.
4257 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4258 struct buffer_head *xb_bh,
4259 struct buffer_head *header_bh,
4260 struct ocfs2_xattr_set_ctxt *ctxt)
4262 struct ocfs2_xattr_header *first_xh = NULL;
4263 struct buffer_head *first_bh = NULL;
4264 struct ocfs2_xattr_block *xb =
4265 (struct ocfs2_xattr_block *)xb_bh->b_data;
4266 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4267 struct ocfs2_extent_list *el = &xb_root->xt_list;
4268 struct ocfs2_xattr_header *xh =
4269 (struct ocfs2_xattr_header *)header_bh->b_data;
4270 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4271 struct super_block *sb = inode->i_sb;
4272 struct ocfs2_super *osb = OCFS2_SB(sb);
4273 int ret, num_buckets, extend = 1;
4275 u32 e_cpos, num_clusters;
4277 mlog(0, "Add new xattr bucket starting form %llu\n",
4278 (unsigned long long)header_bh->b_blocknr);
4281 * Add refrence for header_bh here because it may be
4282 * changed in ocfs2_add_new_xattr_cluster and we need
4283 * to free it in the end.
4287 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4294 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
4300 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4301 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4303 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
4304 ret = ocfs2_add_new_xattr_cluster(inode,
4320 ret = ocfs2_extend_xattr_bucket(inode,
4333 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4334 struct ocfs2_xattr_bucket *bucket,
4337 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4339 offs = offs % inode->i_sb->s_blocksize;
4340 return bucket_block(bucket, block_off) + offs;
4344 * Handle the normal xattr set, including replace, delete and new.
4346 * Note: "local" indicates the real data's locality. So we can't
4347 * just its bucket locality by its length.
4349 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4350 struct ocfs2_xattr_info *xi,
4351 struct ocfs2_xattr_search *xs,
4355 struct ocfs2_xattr_entry *last, *xe;
4356 int name_len = strlen(xi->name);
4357 struct ocfs2_xattr_header *xh = xs->header;
4358 u16 count = le16_to_cpu(xh->xh_count), start;
4359 size_t blocksize = inode->i_sb->s_blocksize;
4361 size_t offs, size, new_size;
4363 last = &xh->xh_entries[count];
4364 if (!xs->not_found) {
4366 offs = le16_to_cpu(xe->xe_name_offset);
4367 if (ocfs2_xattr_is_local(xe))
4368 size = OCFS2_XATTR_SIZE(name_len) +
4369 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4371 size = OCFS2_XATTR_SIZE(name_len) +
4372 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4375 * If the new value will be stored outside, xi->value has been
4376 * initalized as an empty ocfs2_xattr_value_root, and the same
4377 * goes with xi->value_len, so we can set new_size safely here.
4378 * See ocfs2_xattr_set_in_bucket.
4380 new_size = OCFS2_XATTR_SIZE(name_len) +
4381 OCFS2_XATTR_SIZE(xi->value_len);
4383 le16_add_cpu(&xh->xh_name_value_len, -size);
4385 if (new_size > size)
4386 goto set_new_name_value;
4388 /* Now replace the old value with new one. */
4390 xe->xe_value_size = cpu_to_le64(xi->value_len);
4392 xe->xe_value_size = 0;
4394 val = ocfs2_xattr_bucket_get_val(inode,
4396 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4397 size - OCFS2_XATTR_SIZE(name_len));
4398 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4399 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4400 xi->value, xi->value_len);
4402 le16_add_cpu(&xh->xh_name_value_len, new_size);
4403 ocfs2_xattr_set_local(xe, local);
4407 * Remove the old entry if there is more than one.
4408 * We don't remove the last entry so that we can
4409 * use it to indicate the hash value of the empty
4413 le16_add_cpu(&xh->xh_count, -1);
4416 (void *)last - (void *)xe);
4418 sizeof(struct ocfs2_xattr_entry));
4421 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4426 /* find a new entry for insert. */
4427 int low = 0, high = count - 1, tmp;
4428 struct ocfs2_xattr_entry *tmp_xe;
4430 while (low <= high && count) {
4431 tmp = (low + high) / 2;
4432 tmp_xe = &xh->xh_entries[tmp];
4434 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4436 else if (name_hash <
4437 le32_to_cpu(tmp_xe->xe_name_hash))
4445 xe = &xh->xh_entries[low];
4447 memmove(xe + 1, xe, (void *)last - (void *)xe);
4449 le16_add_cpu(&xh->xh_count, 1);
4450 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4451 xe->xe_name_hash = cpu_to_le32(name_hash);
4452 xe->xe_name_len = name_len;
4453 ocfs2_xattr_set_type(xe, xi->name_index);
4457 /* Insert the new name+value. */
4458 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4461 * We must make sure that the name/value pair
4462 * exists in the same block.
4464 offs = le16_to_cpu(xh->xh_free_start);
4465 start = offs - size;
4467 if (start >> inode->i_sb->s_blocksize_bits !=
4468 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4469 offs = offs - offs % blocksize;
4470 xh->xh_free_start = cpu_to_le16(offs);
4473 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
4474 xe->xe_name_offset = cpu_to_le16(offs - size);
4476 memset(val, 0, size);
4477 memcpy(val, xi->name, name_len);
4478 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4480 xe->xe_value_size = cpu_to_le64(xi->value_len);
4481 ocfs2_xattr_set_local(xe, local);
4483 le16_add_cpu(&xh->xh_free_start, -size);
4484 le16_add_cpu(&xh->xh_name_value_len, size);
4490 * Set the xattr entry in the specified bucket.
4491 * The bucket is indicated by xs->bucket and it should have the enough
4492 * space for the xattr insertion.
4494 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4496 struct ocfs2_xattr_info *xi,
4497 struct ocfs2_xattr_search *xs,
4504 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4505 (unsigned long)xi->value_len, xi->name_index,
4506 (unsigned long long)bucket_blkno(xs->bucket));
4508 if (!xs->bucket->bu_bhs[1]) {
4509 blkno = bucket_blkno(xs->bucket);
4510 ocfs2_xattr_bucket_relse(xs->bucket);
4511 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
4518 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4519 OCFS2_JOURNAL_ACCESS_WRITE);
4525 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4526 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4532 static int ocfs2_xattr_value_update_size(struct inode *inode,
4534 struct buffer_head *xe_bh,
4535 struct ocfs2_xattr_entry *xe,
4540 ret = ocfs2_journal_access(handle, inode, xe_bh,
4541 OCFS2_JOURNAL_ACCESS_WRITE);
4547 xe->xe_value_size = cpu_to_le64(new_size);
4549 ret = ocfs2_journal_dirty(handle, xe_bh);
4558 * Truncate the specified xe_off entry in xattr bucket.
4559 * bucket is indicated by header_bh and len is the new length.
4560 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4562 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4564 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4565 struct buffer_head *header_bh,
4568 struct ocfs2_xattr_set_ctxt *ctxt)
4572 struct buffer_head *value_bh = NULL;
4573 struct ocfs2_xattr_value_root *xv;
4574 struct ocfs2_xattr_entry *xe;
4575 struct ocfs2_xattr_header *xh =
4576 (struct ocfs2_xattr_header *)header_bh->b_data;
4577 size_t blocksize = inode->i_sb->s_blocksize;
4579 xe = &xh->xh_entries[xe_off];
4581 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4583 offset = le16_to_cpu(xe->xe_name_offset) +
4584 OCFS2_XATTR_SIZE(xe->xe_name_len);
4586 value_blk = offset / blocksize;
4588 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4589 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4590 value_blk += header_bh->b_blocknr;
4592 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4598 xv = (struct ocfs2_xattr_value_root *)
4599 (value_bh->b_data + offset % blocksize);
4601 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4602 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4603 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
4609 ret = ocfs2_xattr_value_update_size(inode, ctxt->handle,
4610 header_bh, xe, len);
4621 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4622 struct ocfs2_xattr_search *xs,
4624 struct ocfs2_xattr_set_ctxt *ctxt)
4627 struct ocfs2_xattr_entry *xe = xs->here;
4628 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4630 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4632 offset = xe - xh->xh_entries;
4633 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0],
4641 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4643 struct ocfs2_xattr_search *xs,
4648 struct ocfs2_xattr_value_root *xv;
4649 struct ocfs2_xattr_entry *xe = xs->here;
4651 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4653 offset = le16_to_cpu(xe->xe_name_offset) +
4654 OCFS2_XATTR_SIZE(xe->xe_name_len);
4656 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4658 return __ocfs2_xattr_set_value_outside(inode, handle,
4659 xv, val, value_len);
4662 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4663 struct buffer_head *root_bh,
4669 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4670 struct inode *tl_inode = osb->osb_tl_inode;
4672 struct ocfs2_xattr_block *xb =
4673 (struct ocfs2_xattr_block *)root_bh->b_data;
4674 struct ocfs2_alloc_context *meta_ac = NULL;
4675 struct ocfs2_cached_dealloc_ctxt dealloc;
4676 struct ocfs2_extent_tree et;
4678 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4680 ocfs2_init_dealloc_ctxt(&dealloc);
4682 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4683 cpos, len, (unsigned long long)blkno);
4685 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4687 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4693 mutex_lock(&tl_inode->i_mutex);
4695 if (ocfs2_truncate_log_needs_flush(osb)) {
4696 ret = __ocfs2_flush_truncate_log(osb);
4703 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4704 if (IS_ERR(handle)) {
4710 ret = ocfs2_journal_access(handle, inode, root_bh,
4711 OCFS2_JOURNAL_ACCESS_WRITE);
4717 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4724 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4726 ret = ocfs2_journal_dirty(handle, root_bh);
4732 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4737 ocfs2_commit_trans(osb, handle);
4739 ocfs2_schedule_truncate_log_flush(osb, 1);
4741 mutex_unlock(&tl_inode->i_mutex);
4744 ocfs2_free_alloc_context(meta_ac);
4746 ocfs2_run_deallocs(osb, &dealloc);
4751 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4753 struct ocfs2_xattr_search *xs)
4755 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
4756 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4757 le16_to_cpu(xh->xh_count) - 1];
4760 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4761 OCFS2_JOURNAL_ACCESS_WRITE);
4767 /* Remove the old entry. */
4768 memmove(xs->here, xs->here + 1,
4769 (void *)last - (void *)xs->here);
4770 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4771 le16_add_cpu(&xh->xh_count, -1);
4773 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4777 * Set the xattr name/value in the bucket specified in xs.
4779 * As the new value in xi may be stored in the bucket or in an outside cluster,
4780 * we divide the whole process into 3 steps:
4781 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4782 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4783 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4784 * 4. If the clusters for the new outside value can't be allocated, we need
4785 * to free the xattr we allocated in set.
4787 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4788 struct ocfs2_xattr_info *xi,
4789 struct ocfs2_xattr_search *xs,
4790 struct ocfs2_xattr_set_ctxt *ctxt)
4794 char *val = (char *)xi->value;
4795 struct ocfs2_xattr_entry *xe = xs->here;
4796 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4799 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4801 * We need to truncate the xattr storage first.
4803 * If both the old and new value are stored to
4804 * outside block, we only need to truncate
4805 * the storage and then set the value outside.
4807 * If the new value should be stored within block,
4808 * we should free all the outside block first and
4809 * the modification to the xattr block will be done
4810 * by following steps.
4812 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4813 value_len = xi->value_len;
4817 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4824 goto set_value_outside;
4827 value_len = xi->value_len;
4828 /* So we have to handle the inside block change now. */
4829 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4831 * If the new value will be stored outside of block,
4832 * initalize a new empty value root and insert it first.
4835 xi->value = &def_xv;
4836 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4839 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4846 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4849 /* allocate the space now for the outside block storage. */
4850 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4855 if (xs->not_found) {
4857 * We can't allocate enough clusters for outside
4858 * storage and we have allocated xattr already,
4859 * so need to remove it.
4861 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
4867 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4868 xs, val, value_len);
4874 * check whether the xattr bucket is filled up with the same hash value.
4875 * If we want to insert the xattr with the same hash, return -ENOSPC.
4876 * If we want to insert a xattr with different hash value, go ahead
4877 * and ocfs2_divide_xattr_bucket will handle this.
4879 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4880 struct ocfs2_xattr_bucket *bucket,
4883 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4884 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4886 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4889 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4890 xh->xh_entries[0].xe_name_hash) {
4891 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4893 (unsigned long long)bucket_blkno(bucket),
4894 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4901 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4902 struct ocfs2_xattr_info *xi,
4903 struct ocfs2_xattr_search *xs,
4904 struct ocfs2_xattr_set_ctxt *ctxt)
4906 struct ocfs2_xattr_header *xh;
4907 struct ocfs2_xattr_entry *xe;
4908 u16 count, header_size, xh_free_start;
4909 int free, max_free, need, old;
4910 size_t value_size = 0, name_len = strlen(xi->name);
4911 size_t blocksize = inode->i_sb->s_blocksize;
4912 int ret, allocation = 0;
4914 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4918 count = le16_to_cpu(xh->xh_count);
4919 xh_free_start = le16_to_cpu(xh->xh_free_start);
4920 header_size = sizeof(struct ocfs2_xattr_header) +
4921 count * sizeof(struct ocfs2_xattr_entry);
4922 max_free = OCFS2_XATTR_BUCKET_SIZE -
4923 le16_to_cpu(xh->xh_name_value_len) - header_size;
4925 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4926 "of %u which exceed block size\n",
4927 (unsigned long long)bucket_blkno(xs->bucket),
4930 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4931 value_size = OCFS2_XATTR_ROOT_SIZE;
4933 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4936 need = sizeof(struct ocfs2_xattr_entry) +
4937 OCFS2_XATTR_SIZE(name_len) + value_size;
4939 need = value_size + OCFS2_XATTR_SIZE(name_len);
4942 * We only replace the old value if the new length is smaller
4943 * than the old one. Otherwise we will allocate new space in the
4944 * bucket to store it.
4947 if (ocfs2_xattr_is_local(xe))
4948 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4950 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4952 if (old >= value_size)
4956 free = xh_free_start - header_size;
4958 * We need to make sure the new name/value pair
4959 * can exist in the same block.
4961 if (xh_free_start % blocksize < need)
4962 free -= xh_free_start % blocksize;
4964 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4965 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4966 " %u\n", xs->not_found,
4967 (unsigned long long)bucket_blkno(xs->bucket),
4968 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4969 le16_to_cpu(xh->xh_name_value_len));
4973 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
4974 if (need <= max_free &&
4975 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4977 * We can create the space by defragment. Since only the
4978 * name/value will be moved, the xe shouldn't be changed
4981 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
4988 xh_free_start = le16_to_cpu(xh->xh_free_start);
4989 free = xh_free_start - header_size;
4990 if (xh_free_start % blocksize < need)
4991 free -= xh_free_start % blocksize;
4996 mlog(0, "Can't get enough space for xattr insert by "
4997 "defragment. Need %u bytes, but we have %d, so "
4998 "allocate new bucket for it.\n", need, free);
5002 * We have to add new buckets or clusters and one
5003 * allocation should leave us enough space for insert.
5008 * We do not allow for overlapping ranges between buckets. And
5009 * the maximum number of collisions we will allow for then is
5010 * one bucket's worth, so check it here whether we need to
5011 * add a new bucket for the insert.
5013 ret = ocfs2_check_xattr_bucket_collision(inode,
5021 ret = ocfs2_add_new_xattr_bucket(inode,
5023 xs->bucket->bu_bhs[0],
5030 ocfs2_xattr_bucket_relse(xs->bucket);
5032 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5035 if (ret && ret != -ENODATA)
5037 xs->not_found = ret;
5043 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5049 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5050 struct ocfs2_xattr_bucket *bucket,
5054 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5056 struct ocfs2_xattr_entry *xe;
5057 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5058 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5060 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5062 ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
5063 if (IS_ERR(ctxt.handle)) {
5064 ret = PTR_ERR(ctxt.handle);
5069 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5070 xe = &xh->xh_entries[i];
5071 if (ocfs2_xattr_is_local(xe))
5074 ret = ocfs2_xattr_bucket_value_truncate(inode,
5083 ret = ocfs2_commit_trans(osb, ctxt.handle);
5084 ocfs2_schedule_truncate_log_flush(osb, 1);
5085 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5090 static int ocfs2_delete_xattr_index_block(struct inode *inode,
5091 struct buffer_head *xb_bh)
5093 struct ocfs2_xattr_block *xb =
5094 (struct ocfs2_xattr_block *)xb_bh->b_data;
5095 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5097 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5100 if (le16_to_cpu(el->l_next_free_rec) == 0)
5103 while (name_hash > 0) {
5104 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5105 &e_cpos, &num_clusters, el);
5111 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5112 ocfs2_delete_xattr_in_bucket,
5119 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5120 p_blkno, e_cpos, num_clusters);
5129 name_hash = e_cpos - 1;
5137 * 'security' attributes support
5139 static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5140 size_t list_size, const char *name,
5143 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5144 const size_t total_len = prefix_len + name_len + 1;
5146 if (list && total_len <= list_size) {
5147 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5148 memcpy(list + prefix_len, name, name_len);
5149 list[prefix_len + name_len] = '\0';
5154 static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5155 void *buffer, size_t size)
5157 if (strcmp(name, "") == 0)
5159 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5163 static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5164 const void *value, size_t size, int flags)
5166 if (strcmp(name, "") == 0)
5169 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5173 int ocfs2_init_security_get(struct inode *inode,
5175 struct ocfs2_security_xattr_info *si)
5177 return security_inode_init_security(inode, dir, &si->name, &si->value,
5181 int ocfs2_init_security_set(handle_t *handle,
5182 struct inode *inode,
5183 struct buffer_head *di_bh,
5184 struct ocfs2_security_xattr_info *si,
5185 struct ocfs2_alloc_context *xattr_ac,
5186 struct ocfs2_alloc_context *data_ac)
5188 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5189 OCFS2_XATTR_INDEX_SECURITY,
5190 si->name, si->value, si->value_len, 0,
5194 struct xattr_handler ocfs2_xattr_security_handler = {
5195 .prefix = XATTR_SECURITY_PREFIX,
5196 .list = ocfs2_xattr_security_list,
5197 .get = ocfs2_xattr_security_get,
5198 .set = ocfs2_xattr_security_set,
5202 * 'trusted' attributes support
5204 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5205 size_t list_size, const char *name,
5208 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
5209 const size_t total_len = prefix_len + name_len + 1;
5211 if (list && total_len <= list_size) {
5212 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5213 memcpy(list + prefix_len, name, name_len);
5214 list[prefix_len + name_len] = '\0';
5219 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5220 void *buffer, size_t size)
5222 if (strcmp(name, "") == 0)
5224 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5228 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5229 const void *value, size_t size, int flags)
5231 if (strcmp(name, "") == 0)
5234 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5238 struct xattr_handler ocfs2_xattr_trusted_handler = {
5239 .prefix = XATTR_TRUSTED_PREFIX,
5240 .list = ocfs2_xattr_trusted_list,
5241 .get = ocfs2_xattr_trusted_get,
5242 .set = ocfs2_xattr_trusted_set,
5246 * 'user' attributes support
5248 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5249 size_t list_size, const char *name,
5252 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
5253 const size_t total_len = prefix_len + name_len + 1;
5254 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5256 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5259 if (list && total_len <= list_size) {
5260 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5261 memcpy(list + prefix_len, name, name_len);
5262 list[prefix_len + name_len] = '\0';
5267 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5268 void *buffer, size_t size)
5270 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5272 if (strcmp(name, "") == 0)
5274 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5276 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5280 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5281 const void *value, size_t size, int flags)
5283 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5285 if (strcmp(name, "") == 0)
5287 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5290 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5294 struct xattr_handler ocfs2_xattr_user_handler = {
5295 .prefix = XATTR_USER_PREFIX,
5296 .list = ocfs2_xattr_user_list,
5297 .get = ocfs2_xattr_user_get,
5298 .set = ocfs2_xattr_user_set,