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>
45 #include "blockcheck.h"
55 #include "buffer_head_io.h"
60 struct ocfs2_xattr_def_value_root {
61 struct ocfs2_xattr_value_root xv;
62 struct ocfs2_extent_rec er;
65 struct ocfs2_xattr_bucket {
66 /* The inode these xattrs are associated with */
67 struct inode *bu_inode;
69 /* The actual buffers that make up the bucket */
70 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
72 /* How many blocks make up one bucket for this filesystem */
76 struct ocfs2_xattr_set_ctxt {
78 struct ocfs2_alloc_context *meta_ac;
79 struct ocfs2_alloc_context *data_ac;
80 struct ocfs2_cached_dealloc_ctxt dealloc;
83 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
84 #define OCFS2_XATTR_INLINE_SIZE 80
85 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
86 - sizeof(struct ocfs2_xattr_header) \
88 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
89 - sizeof(struct ocfs2_xattr_block) \
90 - sizeof(struct ocfs2_xattr_header) \
93 static struct ocfs2_xattr_def_value_root def_xv = {
94 .xv.xr_list.l_count = cpu_to_le16(1),
97 struct xattr_handler *ocfs2_xattr_handlers[] = {
98 &ocfs2_xattr_user_handler,
99 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
100 &ocfs2_xattr_acl_access_handler,
101 &ocfs2_xattr_acl_default_handler,
103 &ocfs2_xattr_trusted_handler,
104 &ocfs2_xattr_security_handler,
108 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
110 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
111 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
112 = &ocfs2_xattr_acl_access_handler,
113 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
114 = &ocfs2_xattr_acl_default_handler,
116 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
117 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
120 struct ocfs2_xattr_info {
127 struct ocfs2_xattr_search {
128 struct buffer_head *inode_bh;
130 * xattr_bh point to the block buffer head which has extended attribute
131 * when extended attribute in inode, xattr_bh is equal to inode_bh.
133 struct buffer_head *xattr_bh;
134 struct ocfs2_xattr_header *header;
135 struct ocfs2_xattr_bucket *bucket;
138 struct ocfs2_xattr_entry *here;
142 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
143 struct ocfs2_xattr_header *xh,
148 static int ocfs2_xattr_block_find(struct inode *inode,
151 struct ocfs2_xattr_search *xs);
152 static int ocfs2_xattr_index_block_find(struct inode *inode,
153 struct buffer_head *root_bh,
156 struct ocfs2_xattr_search *xs);
158 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
159 struct ocfs2_xattr_tree_root *xt,
163 static int ocfs2_xattr_create_index_block(struct inode *inode,
164 struct ocfs2_xattr_search *xs,
165 struct ocfs2_xattr_set_ctxt *ctxt);
167 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
168 struct ocfs2_xattr_info *xi,
169 struct ocfs2_xattr_search *xs,
170 struct ocfs2_xattr_set_ctxt *ctxt);
172 static int ocfs2_delete_xattr_index_block(struct inode *inode,
173 struct buffer_head *xb_bh);
174 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
175 u64 src_blk, u64 last_blk, u64 to_blk,
176 unsigned int start_bucket,
179 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
181 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
184 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
186 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
189 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
191 u16 len = sb->s_blocksize -
192 offsetof(struct ocfs2_xattr_header, xh_entries);
194 return len / sizeof(struct ocfs2_xattr_entry);
197 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
198 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
199 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
201 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
203 struct ocfs2_xattr_bucket *bucket;
204 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
206 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
208 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
210 bucket->bu_inode = inode;
211 bucket->bu_blocks = blks;
217 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
221 for (i = 0; i < bucket->bu_blocks; i++) {
222 brelse(bucket->bu_bhs[i]);
223 bucket->bu_bhs[i] = NULL;
227 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
230 ocfs2_xattr_bucket_relse(bucket);
231 bucket->bu_inode = NULL;
237 * A bucket that has never been written to disk doesn't need to be
238 * read. We just need the buffer_heads. Don't call this for
239 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
242 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
247 for (i = 0; i < bucket->bu_blocks; i++) {
248 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
250 if (!bucket->bu_bhs[i]) {
256 if (!ocfs2_buffer_uptodate(bucket->bu_inode,
258 ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
263 ocfs2_xattr_bucket_relse(bucket);
267 /* Read the xattr bucket at xb_blkno */
268 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
273 rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
274 bucket->bu_blocks, bucket->bu_bhs, 0,
277 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
280 &bucket_xh(bucket)->xh_check);
286 ocfs2_xattr_bucket_relse(bucket);
290 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
291 struct ocfs2_xattr_bucket *bucket,
296 for (i = 0; i < bucket->bu_blocks; i++) {
297 rc = ocfs2_journal_access(handle, bucket->bu_inode,
298 bucket->bu_bhs[i], type);
308 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
309 struct ocfs2_xattr_bucket *bucket)
313 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
314 bucket->bu_bhs, bucket->bu_blocks,
315 &bucket_xh(bucket)->xh_check);
317 for (i = 0; i < bucket->bu_blocks; i++)
318 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
321 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
322 struct ocfs2_xattr_bucket *src)
325 int blocksize = src->bu_inode->i_sb->s_blocksize;
327 BUG_ON(dest->bu_blocks != src->bu_blocks);
328 BUG_ON(dest->bu_inode != src->bu_inode);
330 for (i = 0; i < src->bu_blocks; i++) {
331 memcpy(bucket_block(dest, i), bucket_block(src, i),
336 static int ocfs2_validate_xattr_block(struct super_block *sb,
337 struct buffer_head *bh)
340 struct ocfs2_xattr_block *xb =
341 (struct ocfs2_xattr_block *)bh->b_data;
343 mlog(0, "Validating xattr block %llu\n",
344 (unsigned long long)bh->b_blocknr);
346 BUG_ON(!buffer_uptodate(bh));
349 * If the ecc fails, we return the error but otherwise
350 * leave the filesystem running. We know any error is
351 * local to this block.
353 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
358 * Errors after here are fatal
361 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
363 "Extended attribute block #%llu has bad "
365 (unsigned long long)bh->b_blocknr, 7,
370 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
372 "Extended attribute block #%llu has an "
373 "invalid xb_blkno of %llu",
374 (unsigned long long)bh->b_blocknr,
375 (unsigned long long)le64_to_cpu(xb->xb_blkno));
379 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
381 "Extended attribute block #%llu has an invalid "
382 "xb_fs_generation of #%u",
383 (unsigned long long)bh->b_blocknr,
384 le32_to_cpu(xb->xb_fs_generation));
391 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
392 struct buffer_head **bh)
395 struct buffer_head *tmp = *bh;
397 rc = ocfs2_read_block(inode, xb_blkno, &tmp,
398 ocfs2_validate_xattr_block);
400 /* If ocfs2_read_block() got us a new bh, pass it up. */
407 static inline const char *ocfs2_xattr_prefix(int name_index)
409 struct xattr_handler *handler = NULL;
411 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
412 handler = ocfs2_xattr_handler_map[name_index];
414 return handler ? handler->prefix : NULL;
417 static u32 ocfs2_xattr_name_hash(struct inode *inode,
421 /* Get hash value of uuid from super block */
422 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
425 /* hash extended attribute name */
426 for (i = 0; i < name_len; i++) {
427 hash = (hash << OCFS2_HASH_SHIFT) ^
428 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
436 * ocfs2_xattr_hash_entry()
438 * Compute the hash of an extended attribute.
440 static void ocfs2_xattr_hash_entry(struct inode *inode,
441 struct ocfs2_xattr_header *header,
442 struct ocfs2_xattr_entry *entry)
445 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
447 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
448 entry->xe_name_hash = cpu_to_le32(hash);
453 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
457 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
458 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
460 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
461 size += sizeof(struct ocfs2_xattr_entry);
466 int ocfs2_calc_security_init(struct inode *dir,
467 struct ocfs2_security_xattr_info *si,
470 struct ocfs2_alloc_context **xattr_ac)
473 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
474 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
478 * The max space of security xattr taken inline is
479 * 256(name) + 80(value) + 16(entry) = 352 bytes,
480 * So reserve one metadata block for it is ok.
482 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
483 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
484 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
489 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
492 /* reserve clusters for xattr value which will be set in B tree*/
493 if (si->value_len > OCFS2_XATTR_INLINE_SIZE)
494 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
499 int ocfs2_calc_xattr_init(struct inode *dir,
500 struct buffer_head *dir_bh,
502 struct ocfs2_security_xattr_info *si,
505 struct ocfs2_alloc_context **xattr_ac)
508 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
514 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
517 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
518 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
519 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
522 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
525 } else if (acl_len != 0 && acl_len != -ENODATA) {
531 if (!(s_size + a_size))
535 * The max space of security xattr taken inline is
536 * 256(name) + 80(value) + 16(entry) = 352 bytes,
537 * The max space of acl xattr taken inline is
538 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
539 * when blocksize = 512, may reserve one more cluser for
540 * xattr bucket, otherwise reserve one metadata block
543 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
544 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
545 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
550 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
553 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
554 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
556 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
559 /* reserve clusters for xattr value which will be set in B tree*/
560 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE)
561 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
563 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
564 acl_len > OCFS2_XATTR_INLINE_SIZE) {
565 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
567 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
574 static int ocfs2_xattr_extend_allocation(struct inode *inode,
576 struct buffer_head *xattr_bh,
577 struct ocfs2_xattr_value_root *xv,
578 struct ocfs2_xattr_set_ctxt *ctxt)
581 handle_t *handle = ctxt->handle;
582 enum ocfs2_alloc_restarted why;
583 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
584 struct ocfs2_xattr_value_buf vb = {
587 .vb_access = ocfs2_journal_access,
589 u32 prev_clusters, logical_start = le32_to_cpu(vb.vb_xv->xr_clusters);
590 struct ocfs2_extent_tree et;
592 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
594 ocfs2_init_xattr_value_extent_tree(&et, inode, &vb);
596 status = vb.vb_access(handle, inode, vb.vb_bh,
597 OCFS2_JOURNAL_ACCESS_WRITE);
603 prev_clusters = le32_to_cpu(vb.vb_xv->xr_clusters);
604 status = ocfs2_add_clusters_in_btree(osb,
619 status = ocfs2_journal_dirty(handle, vb.vb_bh);
625 clusters_to_add -= le32_to_cpu(vb.vb_xv->xr_clusters) - prev_clusters;
628 * We should have already allocated enough space before the transaction,
629 * so no need to restart.
631 BUG_ON(why != RESTART_NONE || clusters_to_add);
638 static int __ocfs2_remove_xattr_range(struct inode *inode,
639 struct buffer_head *root_bh,
640 struct ocfs2_xattr_value_root *xv,
641 u32 cpos, u32 phys_cpos, u32 len,
642 struct ocfs2_xattr_set_ctxt *ctxt)
645 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
646 handle_t *handle = ctxt->handle;
647 struct ocfs2_extent_tree et;
648 struct ocfs2_xattr_value_buf vb = {
651 .vb_access = ocfs2_journal_access,
654 ocfs2_init_xattr_value_extent_tree(&et, inode, &vb);
656 ret = vb.vb_access(handle, inode, vb.vb_bh,
657 OCFS2_JOURNAL_ACCESS_WRITE);
663 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
670 le32_add_cpu(&vb.vb_xv->xr_clusters, -len);
672 ret = ocfs2_journal_dirty(handle, vb.vb_bh);
678 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
686 static int ocfs2_xattr_shrink_size(struct inode *inode,
689 struct buffer_head *root_bh,
690 struct ocfs2_xattr_value_root *xv,
691 struct ocfs2_xattr_set_ctxt *ctxt)
694 u32 trunc_len, cpos, phys_cpos, alloc_size;
697 if (old_clusters <= new_clusters)
701 trunc_len = old_clusters - new_clusters;
703 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
704 &alloc_size, &xv->xr_list);
710 if (alloc_size > trunc_len)
711 alloc_size = trunc_len;
713 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
714 phys_cpos, alloc_size,
721 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
722 ocfs2_remove_xattr_clusters_from_cache(inode, block,
725 trunc_len -= alloc_size;
732 static int ocfs2_xattr_value_truncate(struct inode *inode,
733 struct buffer_head *root_bh,
734 struct ocfs2_xattr_value_root *xv,
736 struct ocfs2_xattr_set_ctxt *ctxt)
739 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
740 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
742 if (new_clusters == old_clusters)
745 if (new_clusters > old_clusters)
746 ret = ocfs2_xattr_extend_allocation(inode,
747 new_clusters - old_clusters,
750 ret = ocfs2_xattr_shrink_size(inode,
751 old_clusters, new_clusters,
757 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
758 size_t *result, const char *prefix,
759 const char *name, int name_len)
761 char *p = buffer + *result;
762 int prefix_len = strlen(prefix);
763 int total_len = prefix_len + name_len + 1;
765 *result += total_len;
767 /* we are just looking for how big our buffer needs to be */
774 memcpy(p, prefix, prefix_len);
775 memcpy(p + prefix_len, name, name_len);
776 p[prefix_len + name_len] = '\0';
781 static int ocfs2_xattr_list_entries(struct inode *inode,
782 struct ocfs2_xattr_header *header,
783 char *buffer, size_t buffer_size)
787 const char *prefix, *name;
789 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
790 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
791 type = ocfs2_xattr_get_type(entry);
792 prefix = ocfs2_xattr_prefix(type);
795 name = (const char *)header +
796 le16_to_cpu(entry->xe_name_offset);
798 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
799 &result, prefix, name,
809 static int ocfs2_xattr_ibody_list(struct inode *inode,
810 struct ocfs2_dinode *di,
814 struct ocfs2_xattr_header *header = NULL;
815 struct ocfs2_inode_info *oi = OCFS2_I(inode);
818 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
821 header = (struct ocfs2_xattr_header *)
822 ((void *)di + inode->i_sb->s_blocksize -
823 le16_to_cpu(di->i_xattr_inline_size));
825 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
830 static int ocfs2_xattr_block_list(struct inode *inode,
831 struct ocfs2_dinode *di,
835 struct buffer_head *blk_bh = NULL;
836 struct ocfs2_xattr_block *xb;
839 if (!di->i_xattr_loc)
842 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
849 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
850 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
851 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
852 ret = ocfs2_xattr_list_entries(inode, header,
853 buffer, buffer_size);
855 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
856 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
857 buffer, buffer_size);
865 ssize_t ocfs2_listxattr(struct dentry *dentry,
869 int ret = 0, i_ret = 0, b_ret = 0;
870 struct buffer_head *di_bh = NULL;
871 struct ocfs2_dinode *di = NULL;
872 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
874 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
877 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
880 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
886 di = (struct ocfs2_dinode *)di_bh->b_data;
888 down_read(&oi->ip_xattr_sem);
889 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
897 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
902 up_read(&oi->ip_xattr_sem);
903 ocfs2_inode_unlock(dentry->d_inode, 0);
907 return i_ret + b_ret;
910 static int ocfs2_xattr_find_entry(int name_index,
912 struct ocfs2_xattr_search *xs)
914 struct ocfs2_xattr_entry *entry;
921 name_len = strlen(name);
923 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
924 cmp = name_index - ocfs2_xattr_get_type(entry);
926 cmp = name_len - entry->xe_name_len;
928 cmp = memcmp(name, (xs->base +
929 le16_to_cpu(entry->xe_name_offset)),
937 return cmp ? -ENODATA : 0;
940 static int ocfs2_xattr_get_value_outside(struct inode *inode,
941 struct ocfs2_xattr_value_root *xv,
945 u32 cpos, p_cluster, num_clusters, bpc, clusters;
948 size_t cplen, blocksize;
949 struct buffer_head *bh = NULL;
950 struct ocfs2_extent_list *el;
953 clusters = le32_to_cpu(xv->xr_clusters);
954 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
955 blocksize = inode->i_sb->s_blocksize;
958 while (cpos < clusters) {
959 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
966 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
967 /* Copy ocfs2_xattr_value */
968 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
969 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
975 cplen = len >= blocksize ? blocksize : len;
976 memcpy(buffer, bh->b_data, cplen);
985 cpos += num_clusters;
991 static int ocfs2_xattr_ibody_get(struct inode *inode,
996 struct ocfs2_xattr_search *xs)
998 struct ocfs2_inode_info *oi = OCFS2_I(inode);
999 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1000 struct ocfs2_xattr_value_root *xv;
1004 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1007 xs->end = (void *)di + inode->i_sb->s_blocksize;
1008 xs->header = (struct ocfs2_xattr_header *)
1009 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1010 xs->base = (void *)xs->header;
1011 xs->here = xs->header->xh_entries;
1013 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1016 size = le64_to_cpu(xs->here->xe_value_size);
1018 if (size > buffer_size)
1020 if (ocfs2_xattr_is_local(xs->here)) {
1021 memcpy(buffer, (void *)xs->base +
1022 le16_to_cpu(xs->here->xe_name_offset) +
1023 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1025 xv = (struct ocfs2_xattr_value_root *)
1026 (xs->base + le16_to_cpu(
1027 xs->here->xe_name_offset) +
1028 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1029 ret = ocfs2_xattr_get_value_outside(inode, xv,
1041 static int ocfs2_xattr_block_get(struct inode *inode,
1046 struct ocfs2_xattr_search *xs)
1048 struct ocfs2_xattr_block *xb;
1049 struct ocfs2_xattr_value_root *xv;
1051 int ret = -ENODATA, name_offset, name_len, block_off, i;
1053 xs->bucket = ocfs2_xattr_bucket_new(inode);
1060 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1066 if (xs->not_found) {
1071 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1072 size = le64_to_cpu(xs->here->xe_value_size);
1075 if (size > buffer_size)
1078 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1079 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1080 i = xs->here - xs->header->xh_entries;
1082 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1083 ret = ocfs2_xattr_bucket_get_name_value(inode,
1084 bucket_xh(xs->bucket),
1088 xs->base = bucket_block(xs->bucket, block_off);
1090 if (ocfs2_xattr_is_local(xs->here)) {
1091 memcpy(buffer, (void *)xs->base +
1092 name_offset + name_len, size);
1094 xv = (struct ocfs2_xattr_value_root *)
1095 (xs->base + name_offset + name_len);
1096 ret = ocfs2_xattr_get_value_outside(inode, xv,
1106 ocfs2_xattr_bucket_free(xs->bucket);
1108 brelse(xs->xattr_bh);
1109 xs->xattr_bh = NULL;
1113 int ocfs2_xattr_get_nolock(struct inode *inode,
1114 struct buffer_head *di_bh,
1121 struct ocfs2_dinode *di = NULL;
1122 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1123 struct ocfs2_xattr_search xis = {
1124 .not_found = -ENODATA,
1126 struct ocfs2_xattr_search xbs = {
1127 .not_found = -ENODATA,
1130 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1133 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1136 xis.inode_bh = xbs.inode_bh = di_bh;
1137 di = (struct ocfs2_dinode *)di_bh->b_data;
1139 down_read(&oi->ip_xattr_sem);
1140 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1142 if (ret == -ENODATA && di->i_xattr_loc)
1143 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1145 up_read(&oi->ip_xattr_sem);
1150 /* ocfs2_xattr_get()
1152 * Copy an extended attribute into the buffer provided.
1153 * Buffer is NULL to compute the size of buffer required.
1155 static int ocfs2_xattr_get(struct inode *inode,
1162 struct buffer_head *di_bh = NULL;
1164 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1169 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1170 name, buffer, buffer_size);
1172 ocfs2_inode_unlock(inode, 0);
1179 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1181 struct ocfs2_xattr_value_root *xv,
1185 int ret = 0, i, cp_len, credits;
1186 u16 blocksize = inode->i_sb->s_blocksize;
1187 u32 p_cluster, num_clusters;
1188 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1189 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1191 struct buffer_head *bh = NULL;
1193 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1196 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1197 * so we don't need to worry about whether ocfs2_extend_trans
1198 * will create a new transactio for us or not.
1200 credits = clusters * bpc;
1201 ret = ocfs2_extend_trans(handle, credits);
1207 while (cpos < clusters) {
1208 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1209 &num_clusters, &xv->xr_list);
1215 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1217 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1218 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
1224 ret = ocfs2_journal_access(handle,
1227 OCFS2_JOURNAL_ACCESS_WRITE);
1233 cp_len = value_len > blocksize ? blocksize : value_len;
1234 memcpy(bh->b_data, value, cp_len);
1235 value_len -= cp_len;
1237 if (cp_len < blocksize)
1238 memset(bh->b_data + cp_len, 0,
1239 blocksize - cp_len);
1241 ret = ocfs2_journal_dirty(handle, bh);
1250 * XXX: do we need to empty all the following
1251 * blocks in this cluster?
1256 cpos += num_clusters;
1264 static int ocfs2_xattr_cleanup(struct inode *inode,
1266 struct ocfs2_xattr_info *xi,
1267 struct ocfs2_xattr_search *xs,
1271 size_t name_len = strlen(xi->name);
1272 void *val = xs->base + offs;
1273 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1275 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1276 OCFS2_JOURNAL_ACCESS_WRITE);
1281 /* Decrease xattr count */
1282 le16_add_cpu(&xs->header->xh_count, -1);
1283 /* Remove the xattr entry and tree root which has already be set*/
1284 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1285 memset(val, 0, size);
1287 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1294 static int ocfs2_xattr_update_entry(struct inode *inode,
1296 struct ocfs2_xattr_info *xi,
1297 struct ocfs2_xattr_search *xs,
1302 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1303 OCFS2_JOURNAL_ACCESS_WRITE);
1309 xs->here->xe_name_offset = cpu_to_le16(offs);
1310 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1311 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1312 ocfs2_xattr_set_local(xs->here, 1);
1314 ocfs2_xattr_set_local(xs->here, 0);
1315 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1317 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1325 * ocfs2_xattr_set_value_outside()
1327 * Set large size value in B tree.
1329 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1330 struct ocfs2_xattr_info *xi,
1331 struct ocfs2_xattr_search *xs,
1332 struct ocfs2_xattr_set_ctxt *ctxt,
1335 size_t name_len = strlen(xi->name);
1336 void *val = xs->base + offs;
1337 struct ocfs2_xattr_value_root *xv = NULL;
1338 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1341 memset(val, 0, size);
1342 memcpy(val, xi->name, name_len);
1343 xv = (struct ocfs2_xattr_value_root *)
1344 (val + OCFS2_XATTR_SIZE(name_len));
1345 xv->xr_clusters = 0;
1346 xv->xr_last_eb_blk = 0;
1347 xv->xr_list.l_tree_depth = 0;
1348 xv->xr_list.l_count = cpu_to_le16(1);
1349 xv->xr_list.l_next_free_rec = 0;
1351 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1352 xi->value_len, ctxt);
1357 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
1362 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1363 xi->value, xi->value_len);
1371 * ocfs2_xattr_set_entry_local()
1373 * Set, replace or remove extended attribute in local.
1375 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1376 struct ocfs2_xattr_info *xi,
1377 struct ocfs2_xattr_search *xs,
1378 struct ocfs2_xattr_entry *last,
1381 size_t name_len = strlen(xi->name);
1384 if (xi->value && xs->not_found) {
1385 /* Insert the new xattr entry. */
1386 le16_add_cpu(&xs->header->xh_count, 1);
1387 ocfs2_xattr_set_type(last, xi->name_index);
1388 ocfs2_xattr_set_local(last, 1);
1389 last->xe_name_len = name_len;
1395 first_val = xs->base + min_offs;
1396 offs = le16_to_cpu(xs->here->xe_name_offset);
1397 val = xs->base + offs;
1399 if (le64_to_cpu(xs->here->xe_value_size) >
1400 OCFS2_XATTR_INLINE_SIZE)
1401 size = OCFS2_XATTR_SIZE(name_len) +
1402 OCFS2_XATTR_ROOT_SIZE;
1404 size = OCFS2_XATTR_SIZE(name_len) +
1405 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1407 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1408 OCFS2_XATTR_SIZE(xi->value_len)) {
1409 /* The old and the new value have the
1410 same size. Just replace the value. */
1411 ocfs2_xattr_set_local(xs->here, 1);
1412 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1413 /* Clear value bytes. */
1414 memset(val + OCFS2_XATTR_SIZE(name_len),
1416 OCFS2_XATTR_SIZE(xi->value_len));
1417 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1422 /* Remove the old name+value. */
1423 memmove(first_val + size, first_val, val - first_val);
1424 memset(first_val, 0, size);
1425 xs->here->xe_name_hash = 0;
1426 xs->here->xe_name_offset = 0;
1427 ocfs2_xattr_set_local(xs->here, 1);
1428 xs->here->xe_value_size = 0;
1432 /* Adjust all value offsets. */
1433 last = xs->header->xh_entries;
1434 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1435 size_t o = le16_to_cpu(last->xe_name_offset);
1438 last->xe_name_offset = cpu_to_le16(o + size);
1443 /* Remove the old entry. */
1445 memmove(xs->here, xs->here + 1,
1446 (void *)last - (void *)xs->here);
1447 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1448 le16_add_cpu(&xs->header->xh_count, -1);
1452 /* Insert the new name+value. */
1453 size_t size = OCFS2_XATTR_SIZE(name_len) +
1454 OCFS2_XATTR_SIZE(xi->value_len);
1455 void *val = xs->base + min_offs - size;
1457 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1458 memset(val, 0, size);
1459 memcpy(val, xi->name, name_len);
1460 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1463 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1464 ocfs2_xattr_set_local(xs->here, 1);
1465 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1472 * ocfs2_xattr_set_entry()
1474 * Set extended attribute entry into inode or block.
1476 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1477 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1478 * then set value in B tree with set_value_outside().
1480 static int ocfs2_xattr_set_entry(struct inode *inode,
1481 struct ocfs2_xattr_info *xi,
1482 struct ocfs2_xattr_search *xs,
1483 struct ocfs2_xattr_set_ctxt *ctxt,
1486 struct ocfs2_xattr_entry *last;
1487 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1488 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1489 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1491 handle_t *handle = ctxt->handle;
1493 struct ocfs2_xattr_info xi_l = {
1494 .name_index = xi->name_index,
1497 .value_len = xi->value_len,
1500 /* Compute min_offs, last and free space. */
1501 last = xs->header->xh_entries;
1503 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1504 size_t offs = le16_to_cpu(last->xe_name_offset);
1505 if (offs < min_offs)
1510 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1514 if (!xs->not_found) {
1516 if (ocfs2_xattr_is_local(xs->here))
1517 size = OCFS2_XATTR_SIZE(name_len) +
1518 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1520 size = OCFS2_XATTR_SIZE(name_len) +
1521 OCFS2_XATTR_ROOT_SIZE;
1522 free += (size + sizeof(struct ocfs2_xattr_entry));
1524 /* Check free space in inode or block */
1525 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1526 if (free < sizeof(struct ocfs2_xattr_entry) +
1527 OCFS2_XATTR_SIZE(name_len) +
1528 OCFS2_XATTR_ROOT_SIZE) {
1532 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1533 xi_l.value = (void *)&def_xv;
1534 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1535 } else if (xi->value) {
1536 if (free < sizeof(struct ocfs2_xattr_entry) +
1537 OCFS2_XATTR_SIZE(name_len) +
1538 OCFS2_XATTR_SIZE(xi->value_len)) {
1544 if (!xs->not_found) {
1545 /* For existing extended attribute */
1546 size_t size = OCFS2_XATTR_SIZE(name_len) +
1547 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1548 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1549 void *val = xs->base + offs;
1551 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1552 /* Replace existing local xattr with tree root */
1553 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1558 } else if (!ocfs2_xattr_is_local(xs->here)) {
1559 /* For existing xattr which has value outside */
1560 struct ocfs2_xattr_value_root *xv = NULL;
1561 xv = (struct ocfs2_xattr_value_root *)(val +
1562 OCFS2_XATTR_SIZE(name_len));
1564 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1566 * If new value need set outside also,
1567 * first truncate old value to new value,
1568 * then set new value with set_value_outside().
1570 ret = ocfs2_xattr_value_truncate(inode,
1580 ret = ocfs2_xattr_update_entry(inode,
1590 ret = __ocfs2_xattr_set_value_outside(inode,
1600 * If new value need set in local,
1601 * just trucate old value to zero.
1603 ret = ocfs2_xattr_value_truncate(inode,
1614 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1615 OCFS2_JOURNAL_ACCESS_WRITE);
1621 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1622 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1623 OCFS2_JOURNAL_ACCESS_WRITE);
1631 * Set value in local, include set tree root in local.
1632 * This is the first step for value size >INLINE_SIZE.
1634 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1636 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1637 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1644 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1645 (flag & OCFS2_INLINE_XATTR_FL)) {
1646 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1647 unsigned int xattrsize = osb->s_xattr_inline_size;
1650 * Adjust extent record count or inline data size
1651 * to reserve space for extended attribute.
1653 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1654 struct ocfs2_inline_data *idata = &di->id2.i_data;
1655 le16_add_cpu(&idata->id_count, -xattrsize);
1656 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1657 struct ocfs2_extent_list *el = &di->id2.i_list;
1658 le16_add_cpu(&el->l_count, -(xattrsize /
1659 sizeof(struct ocfs2_extent_rec)));
1661 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1663 /* Update xattr flag */
1664 spin_lock(&oi->ip_lock);
1665 oi->ip_dyn_features |= flag;
1666 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1667 spin_unlock(&oi->ip_lock);
1668 /* Update inode ctime */
1669 inode->i_ctime = CURRENT_TIME;
1670 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1671 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1673 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1677 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1679 * Set value outside in B tree.
1680 * This is the second step for value size > INLINE_SIZE.
1682 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1683 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
1689 * If set value outside failed, we have to clean
1690 * the junk tree root we have already set in local.
1692 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1702 static int ocfs2_remove_value_outside(struct inode*inode,
1703 struct buffer_head *bh,
1704 struct ocfs2_xattr_header *header)
1707 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1708 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1710 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1712 ctxt.handle = ocfs2_start_trans(osb,
1713 ocfs2_remove_extent_credits(osb->sb));
1714 if (IS_ERR(ctxt.handle)) {
1715 ret = PTR_ERR(ctxt.handle);
1720 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1721 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1723 if (!ocfs2_xattr_is_local(entry)) {
1724 struct ocfs2_xattr_value_root *xv;
1727 val = (void *)header +
1728 le16_to_cpu(entry->xe_name_offset);
1729 xv = (struct ocfs2_xattr_value_root *)
1730 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1731 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1740 ocfs2_commit_trans(osb, ctxt.handle);
1741 ocfs2_schedule_truncate_log_flush(osb, 1);
1742 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1747 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1748 struct buffer_head *di_bh)
1751 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1752 struct ocfs2_xattr_header *header;
1755 header = (struct ocfs2_xattr_header *)
1756 ((void *)di + inode->i_sb->s_blocksize -
1757 le16_to_cpu(di->i_xattr_inline_size));
1759 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1764 static int ocfs2_xattr_block_remove(struct inode *inode,
1765 struct buffer_head *blk_bh)
1767 struct ocfs2_xattr_block *xb;
1770 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1771 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1772 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1773 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1775 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1780 static int ocfs2_xattr_free_block(struct inode *inode,
1783 struct inode *xb_alloc_inode;
1784 struct buffer_head *xb_alloc_bh = NULL;
1785 struct buffer_head *blk_bh = NULL;
1786 struct ocfs2_xattr_block *xb;
1787 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1793 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
1799 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1805 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1806 blk = le64_to_cpu(xb->xb_blkno);
1807 bit = le16_to_cpu(xb->xb_suballoc_bit);
1808 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1810 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1811 EXTENT_ALLOC_SYSTEM_INODE,
1812 le16_to_cpu(xb->xb_suballoc_slot));
1813 if (!xb_alloc_inode) {
1818 mutex_lock(&xb_alloc_inode->i_mutex);
1820 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1826 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1827 if (IS_ERR(handle)) {
1828 ret = PTR_ERR(handle);
1833 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1838 ocfs2_commit_trans(osb, handle);
1840 ocfs2_inode_unlock(xb_alloc_inode, 1);
1841 brelse(xb_alloc_bh);
1843 mutex_unlock(&xb_alloc_inode->i_mutex);
1844 iput(xb_alloc_inode);
1851 * ocfs2_xattr_remove()
1853 * Free extended attribute resources associated with this inode.
1855 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1857 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1858 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1862 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1865 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1868 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1869 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1876 if (di->i_xattr_loc) {
1877 ret = ocfs2_xattr_free_block(inode,
1878 le64_to_cpu(di->i_xattr_loc));
1885 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1886 OCFS2_INODE_UPDATE_CREDITS);
1887 if (IS_ERR(handle)) {
1888 ret = PTR_ERR(handle);
1892 ret = ocfs2_journal_access(handle, inode, di_bh,
1893 OCFS2_JOURNAL_ACCESS_WRITE);
1899 di->i_xattr_loc = 0;
1901 spin_lock(&oi->ip_lock);
1902 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1903 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1904 spin_unlock(&oi->ip_lock);
1906 ret = ocfs2_journal_dirty(handle, di_bh);
1910 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1915 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1916 struct ocfs2_dinode *di)
1918 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1919 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1922 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1925 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1926 struct ocfs2_inline_data *idata = &di->id2.i_data;
1927 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1928 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1929 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1930 le64_to_cpu(di->i_size);
1932 struct ocfs2_extent_list *el = &di->id2.i_list;
1933 free = (le16_to_cpu(el->l_count) -
1934 le16_to_cpu(el->l_next_free_rec)) *
1935 sizeof(struct ocfs2_extent_rec);
1937 if (free >= xattrsize)
1944 * ocfs2_xattr_ibody_find()
1946 * Find extended attribute in inode block and
1947 * fill search info into struct ocfs2_xattr_search.
1949 static int ocfs2_xattr_ibody_find(struct inode *inode,
1952 struct ocfs2_xattr_search *xs)
1954 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1955 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1959 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1962 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1963 down_read(&oi->ip_alloc_sem);
1964 has_space = ocfs2_xattr_has_space_inline(inode, di);
1965 up_read(&oi->ip_alloc_sem);
1970 xs->xattr_bh = xs->inode_bh;
1971 xs->end = (void *)di + inode->i_sb->s_blocksize;
1972 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1973 xs->header = (struct ocfs2_xattr_header *)
1974 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1976 xs->header = (struct ocfs2_xattr_header *)
1977 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1978 xs->base = (void *)xs->header;
1979 xs->here = xs->header->xh_entries;
1981 /* Find the named attribute. */
1982 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1983 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1984 if (ret && ret != -ENODATA)
1986 xs->not_found = ret;
1993 * ocfs2_xattr_ibody_set()
1995 * Set, replace or remove an extended attribute into inode block.
1998 static int ocfs2_xattr_ibody_set(struct inode *inode,
1999 struct ocfs2_xattr_info *xi,
2000 struct ocfs2_xattr_search *xs,
2001 struct ocfs2_xattr_set_ctxt *ctxt)
2003 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2004 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2007 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2010 down_write(&oi->ip_alloc_sem);
2011 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2012 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2018 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2019 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2021 up_write(&oi->ip_alloc_sem);
2027 * ocfs2_xattr_block_find()
2029 * Find extended attribute in external block and
2030 * fill search info into struct ocfs2_xattr_search.
2032 static int ocfs2_xattr_block_find(struct inode *inode,
2035 struct ocfs2_xattr_search *xs)
2037 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2038 struct buffer_head *blk_bh = NULL;
2039 struct ocfs2_xattr_block *xb;
2042 if (!di->i_xattr_loc)
2045 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2052 xs->xattr_bh = blk_bh;
2053 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2055 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2056 xs->header = &xb->xb_attrs.xb_header;
2057 xs->base = (void *)xs->header;
2058 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2059 xs->here = xs->header->xh_entries;
2061 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2063 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2067 if (ret && ret != -ENODATA) {
2068 xs->xattr_bh = NULL;
2071 xs->not_found = ret;
2080 * ocfs2_xattr_block_set()
2082 * Set, replace or remove an extended attribute into external block.
2085 static int ocfs2_xattr_block_set(struct inode *inode,
2086 struct ocfs2_xattr_info *xi,
2087 struct ocfs2_xattr_search *xs,
2088 struct ocfs2_xattr_set_ctxt *ctxt)
2090 struct buffer_head *new_bh = NULL;
2091 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2092 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2093 handle_t *handle = ctxt->handle;
2094 struct ocfs2_xattr_block *xblk = NULL;
2095 u16 suballoc_bit_start;
2100 if (!xs->xattr_bh) {
2101 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
2102 OCFS2_JOURNAL_ACCESS_CREATE);
2108 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
2109 &suballoc_bit_start, &num_got,
2116 new_bh = sb_getblk(inode->i_sb, first_blkno);
2117 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2119 ret = ocfs2_journal_access(handle, inode, new_bh,
2120 OCFS2_JOURNAL_ACCESS_CREATE);
2126 /* Initialize ocfs2_xattr_block */
2127 xs->xattr_bh = new_bh;
2128 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2129 memset(xblk, 0, inode->i_sb->s_blocksize);
2130 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2131 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2132 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2133 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2134 xblk->xb_blkno = cpu_to_le64(first_blkno);
2136 xs->header = &xblk->xb_attrs.xb_header;
2137 xs->base = (void *)xs->header;
2138 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2139 xs->here = xs->header->xh_entries;
2141 ret = ocfs2_journal_dirty(handle, new_bh);
2146 di->i_xattr_loc = cpu_to_le64(first_blkno);
2147 ocfs2_journal_dirty(handle, xs->inode_bh);
2149 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2151 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2152 /* Set extended attribute into external block */
2153 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2154 OCFS2_HAS_XATTR_FL);
2155 if (!ret || ret != -ENOSPC)
2158 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2163 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2170 /* Check whether the new xattr can be inserted into the inode. */
2171 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2172 struct ocfs2_xattr_info *xi,
2173 struct ocfs2_xattr_search *xs)
2176 struct ocfs2_xattr_entry *last;
2178 size_t min_offs = xs->end - xs->base;
2183 last = xs->header->xh_entries;
2185 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2186 size_t offs = le16_to_cpu(last->xe_name_offset);
2187 if (offs < min_offs)
2192 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
2196 BUG_ON(!xs->not_found);
2198 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2199 value_size = OCFS2_XATTR_ROOT_SIZE;
2201 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2203 if (free >= sizeof(struct ocfs2_xattr_entry) +
2204 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2210 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2211 struct ocfs2_dinode *di,
2212 struct ocfs2_xattr_info *xi,
2213 struct ocfs2_xattr_search *xis,
2214 struct ocfs2_xattr_search *xbs,
2219 int ret = 0, old_in_xb = 0;
2220 int clusters_add = 0, meta_add = 0, credits = 0;
2221 struct buffer_head *bh = NULL;
2222 struct ocfs2_xattr_block *xb = NULL;
2223 struct ocfs2_xattr_entry *xe = NULL;
2224 struct ocfs2_xattr_value_root *xv = NULL;
2226 int name_offset, name_len = 0;
2227 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2231 if (xis->not_found && xbs->not_found) {
2232 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2234 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2235 clusters_add += new_clusters;
2236 credits += ocfs2_calc_extend_credits(inode->i_sb,
2244 if (!xis->not_found) {
2246 name_offset = le16_to_cpu(xe->xe_name_offset);
2247 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2249 credits += OCFS2_INODE_UPDATE_CREDITS;
2251 int i, block_off = 0;
2252 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2254 name_offset = le16_to_cpu(xe->xe_name_offset);
2255 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2256 i = xbs->here - xbs->header->xh_entries;
2259 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2260 ret = ocfs2_xattr_bucket_get_name_value(inode,
2261 bucket_xh(xbs->bucket),
2264 base = bucket_block(xbs->bucket, block_off);
2265 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2268 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2273 * delete a xattr doesn't need metadata and cluster allocation.
2274 * so just calculate the credits and return.
2276 * The credits for removing the value tree will be extended
2277 * by ocfs2_remove_extent itself.
2280 if (!ocfs2_xattr_is_local(xe))
2281 credits += ocfs2_remove_extent_credits(inode->i_sb);
2286 /* do cluster allocation guess first. */
2287 value_size = le64_to_cpu(xe->xe_value_size);
2291 * In xattr set, we always try to set the xe in inode first,
2292 * so if it can be inserted into inode successfully, the old
2293 * one will be removed from the xattr block, and this xattr
2294 * will be inserted into inode as a new xattr in inode.
2296 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2297 clusters_add += new_clusters;
2298 credits += ocfs2_remove_extent_credits(inode->i_sb) +
2299 OCFS2_INODE_UPDATE_CREDITS;
2300 if (!ocfs2_xattr_is_local(xe))
2301 credits += ocfs2_calc_extend_credits(
2309 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2310 /* the new values will be stored outside. */
2311 u32 old_clusters = 0;
2313 if (!ocfs2_xattr_is_local(xe)) {
2314 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2316 xv = (struct ocfs2_xattr_value_root *)
2317 (base + name_offset + name_len);
2318 value_size = OCFS2_XATTR_ROOT_SIZE;
2322 if (old_clusters >= new_clusters) {
2323 credits += ocfs2_remove_extent_credits(inode->i_sb);
2326 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2327 clusters_add += new_clusters - old_clusters;
2328 credits += ocfs2_calc_extend_credits(inode->i_sb,
2332 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2337 * Now the new value will be stored inside. So if the new
2338 * value is smaller than the size of value root or the old
2339 * value, we don't need any allocation, otherwise we have
2340 * to guess metadata allocation.
2342 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2343 (!ocfs2_xattr_is_local(xe) &&
2344 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2349 /* calculate metadata allocation. */
2350 if (di->i_xattr_loc) {
2351 if (!xbs->xattr_bh) {
2352 ret = ocfs2_read_xattr_block(inode,
2353 le64_to_cpu(di->i_xattr_loc),
2360 xb = (struct ocfs2_xattr_block *)bh->b_data;
2362 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2364 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2365 struct ocfs2_extent_list *el =
2366 &xb->xb_attrs.xb_root.xt_list;
2367 meta_add += ocfs2_extend_meta_needed(el);
2368 credits += ocfs2_calc_extend_credits(inode->i_sb,
2373 * This cluster will be used either for new bucket or for
2375 * If the cluster size is the same as the bucket size, one
2376 * more is needed since we may need to extend the bucket
2380 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2381 if (OCFS2_XATTR_BUCKET_SIZE ==
2382 OCFS2_SB(inode->i_sb)->s_clustersize) {
2383 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2388 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2392 *clusters_need = clusters_add;
2394 *meta_need = meta_add;
2396 *credits_need = credits;
2401 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2402 struct ocfs2_dinode *di,
2403 struct ocfs2_xattr_info *xi,
2404 struct ocfs2_xattr_search *xis,
2405 struct ocfs2_xattr_search *xbs,
2406 struct ocfs2_xattr_set_ctxt *ctxt,
2409 int clusters_add, meta_add, ret;
2410 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2412 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2414 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2416 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2417 &clusters_add, &meta_add, credits);
2423 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2424 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2427 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2436 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2442 if (ctxt->meta_ac) {
2443 ocfs2_free_alloc_context(ctxt->meta_ac);
2444 ctxt->meta_ac = NULL;
2448 * We cannot have an error and a non null ctxt->data_ac.
2455 static int __ocfs2_xattr_set_handle(struct inode *inode,
2456 struct ocfs2_dinode *di,
2457 struct ocfs2_xattr_info *xi,
2458 struct ocfs2_xattr_search *xis,
2459 struct ocfs2_xattr_search *xbs,
2460 struct ocfs2_xattr_set_ctxt *ctxt)
2462 int ret = 0, credits, old_found;
2465 /* Remove existing extended attribute */
2466 if (!xis->not_found)
2467 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2468 else if (!xbs->not_found)
2469 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2471 /* We always try to set extended attribute into inode first*/
2472 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2473 if (!ret && !xbs->not_found) {
2475 * If succeed and that extended attribute existing in
2476 * external block, then we will remove it.
2481 old_found = xis->not_found;
2482 xis->not_found = -ENODATA;
2483 ret = ocfs2_calc_xattr_set_need(inode,
2491 xis->not_found = old_found;
2497 ret = ocfs2_extend_trans(ctxt->handle, credits +
2498 ctxt->handle->h_buffer_credits);
2503 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2504 } else if (ret == -ENOSPC) {
2505 if (di->i_xattr_loc && !xbs->xattr_bh) {
2506 ret = ocfs2_xattr_block_find(inode,
2512 old_found = xis->not_found;
2513 xis->not_found = -ENODATA;
2514 ret = ocfs2_calc_xattr_set_need(inode,
2522 xis->not_found = old_found;
2528 ret = ocfs2_extend_trans(ctxt->handle, credits +
2529 ctxt->handle->h_buffer_credits);
2536 * If no space in inode, we will set extended attribute
2537 * into external block.
2539 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2542 if (!xis->not_found) {
2544 * If succeed and that extended attribute
2545 * existing in inode, we will remove it.
2549 xbs->not_found = -ENODATA;
2550 ret = ocfs2_calc_xattr_set_need(inode,
2563 ret = ocfs2_extend_trans(ctxt->handle, credits +
2564 ctxt->handle->h_buffer_credits);
2569 ret = ocfs2_xattr_ibody_set(inode, xi,
2580 * This function only called duing creating inode
2581 * for init security/acl xattrs of the new inode.
2582 * The xattrs could be put into ibody or extent block,
2583 * xattr bucket would not be use in this case.
2584 * transanction credits also be reserved in here.
2586 int ocfs2_xattr_set_handle(handle_t *handle,
2587 struct inode *inode,
2588 struct buffer_head *di_bh,
2594 struct ocfs2_alloc_context *meta_ac,
2595 struct ocfs2_alloc_context *data_ac)
2597 struct ocfs2_dinode *di;
2600 struct ocfs2_xattr_info xi = {
2601 .name_index = name_index,
2604 .value_len = value_len,
2607 struct ocfs2_xattr_search xis = {
2608 .not_found = -ENODATA,
2611 struct ocfs2_xattr_search xbs = {
2612 .not_found = -ENODATA,
2615 struct ocfs2_xattr_set_ctxt ctxt = {
2621 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2624 xis.inode_bh = xbs.inode_bh = di_bh;
2625 di = (struct ocfs2_dinode *)di_bh->b_data;
2627 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2629 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2632 if (xis.not_found) {
2633 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2638 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2641 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2642 brelse(xbs.xattr_bh);
2650 * Set, replace or remove an extended attribute for this inode.
2651 * value is NULL to remove an existing extended attribute, else either
2652 * create or replace an extended attribute.
2654 int ocfs2_xattr_set(struct inode *inode,
2661 struct buffer_head *di_bh = NULL;
2662 struct ocfs2_dinode *di;
2664 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2665 struct inode *tl_inode = osb->osb_tl_inode;
2666 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2668 struct ocfs2_xattr_info xi = {
2669 .name_index = name_index,
2672 .value_len = value_len,
2675 struct ocfs2_xattr_search xis = {
2676 .not_found = -ENODATA,
2679 struct ocfs2_xattr_search xbs = {
2680 .not_found = -ENODATA,
2683 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2687 * Only xbs will be used on indexed trees. xis doesn't need a
2690 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2692 mlog_errno(-ENOMEM);
2696 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2699 goto cleanup_nolock;
2701 xis.inode_bh = xbs.inode_bh = di_bh;
2702 di = (struct ocfs2_dinode *)di_bh->b_data;
2704 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2706 * Scan inode and external block to find the same name
2707 * extended attribute and collect search infomation.
2709 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2712 if (xis.not_found) {
2713 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2718 if (xis.not_found && xbs.not_found) {
2720 if (flags & XATTR_REPLACE)
2727 if (flags & XATTR_CREATE)
2732 mutex_lock(&tl_inode->i_mutex);
2734 if (ocfs2_truncate_log_needs_flush(osb)) {
2735 ret = __ocfs2_flush_truncate_log(osb);
2737 mutex_unlock(&tl_inode->i_mutex);
2742 mutex_unlock(&tl_inode->i_mutex);
2744 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2745 &xbs, &ctxt, &credits);
2751 ctxt.handle = ocfs2_start_trans(osb, credits);
2752 if (IS_ERR(ctxt.handle)) {
2753 ret = PTR_ERR(ctxt.handle);
2758 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2760 ocfs2_commit_trans(osb, ctxt.handle);
2763 ocfs2_free_alloc_context(ctxt.data_ac);
2765 ocfs2_free_alloc_context(ctxt.meta_ac);
2766 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2767 ocfs2_schedule_truncate_log_flush(osb, 1);
2768 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2770 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2771 ocfs2_inode_unlock(inode, 1);
2774 brelse(xbs.xattr_bh);
2775 ocfs2_xattr_bucket_free(xbs.bucket);
2781 * Find the xattr extent rec which may contains name_hash.
2782 * e_cpos will be the first name hash of the xattr rec.
2783 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2785 static int ocfs2_xattr_get_rec(struct inode *inode,
2790 struct ocfs2_extent_list *el)
2793 struct buffer_head *eb_bh = NULL;
2794 struct ocfs2_extent_block *eb;
2795 struct ocfs2_extent_rec *rec = NULL;
2798 if (el->l_tree_depth) {
2799 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2805 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2808 if (el->l_tree_depth) {
2809 ocfs2_error(inode->i_sb,
2810 "Inode %lu has non zero tree depth in "
2811 "xattr tree block %llu\n", inode->i_ino,
2812 (unsigned long long)eb_bh->b_blocknr);
2818 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2819 rec = &el->l_recs[i];
2821 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2822 e_blkno = le64_to_cpu(rec->e_blkno);
2828 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2829 "record (%u, %u, 0) in xattr", inode->i_ino,
2830 le32_to_cpu(rec->e_cpos),
2831 ocfs2_rec_clusters(el, rec));
2836 *p_blkno = le64_to_cpu(rec->e_blkno);
2837 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2839 *e_cpos = le32_to_cpu(rec->e_cpos);
2845 typedef int (xattr_bucket_func)(struct inode *inode,
2846 struct ocfs2_xattr_bucket *bucket,
2849 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2850 struct ocfs2_xattr_bucket *bucket,
2857 int i, ret = 0, cmp = 1, block_off, new_offset;
2858 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
2859 size_t name_len = strlen(name);
2860 struct ocfs2_xattr_entry *xe = NULL;
2864 * We don't use binary search in the bucket because there
2865 * may be multiple entries with the same name hash.
2867 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2868 xe = &xh->xh_entries[i];
2870 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2872 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2875 cmp = name_index - ocfs2_xattr_get_type(xe);
2877 cmp = name_len - xe->xe_name_len;
2881 ret = ocfs2_xattr_bucket_get_name_value(inode,
2892 xe_name = bucket_block(bucket, block_off) + new_offset;
2893 if (!memcmp(name, xe_name, name_len)) {
2905 * Find the specified xattr entry in a series of buckets.
2906 * This series start from p_blkno and last for num_clusters.
2907 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2908 * the num of the valid buckets.
2910 * Return the buffer_head this xattr should reside in. And if the xattr's
2911 * hash is in the gap of 2 buckets, return the lower bucket.
2913 static int ocfs2_xattr_bucket_find(struct inode *inode,
2920 struct ocfs2_xattr_search *xs)
2923 struct ocfs2_xattr_header *xh = NULL;
2924 struct ocfs2_xattr_entry *xe = NULL;
2926 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2927 int low_bucket = 0, bucket, high_bucket;
2928 struct ocfs2_xattr_bucket *search;
2930 u64 blkno, lower_blkno = 0;
2932 search = ocfs2_xattr_bucket_new(inode);
2939 ret = ocfs2_read_xattr_bucket(search, p_blkno);
2945 xh = bucket_xh(search);
2946 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2947 while (low_bucket <= high_bucket) {
2948 ocfs2_xattr_bucket_relse(search);
2950 bucket = (low_bucket + high_bucket) / 2;
2951 blkno = p_blkno + bucket * blk_per_bucket;
2952 ret = ocfs2_read_xattr_bucket(search, blkno);
2958 xh = bucket_xh(search);
2959 xe = &xh->xh_entries[0];
2960 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2961 high_bucket = bucket - 1;
2966 * Check whether the hash of the last entry in our
2967 * bucket is larger than the search one. for an empty
2968 * bucket, the last one is also the first one.
2971 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2973 last_hash = le32_to_cpu(xe->xe_name_hash);
2975 /* record lower_blkno which may be the insert place. */
2976 lower_blkno = blkno;
2978 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2979 low_bucket = bucket + 1;
2983 /* the searched xattr should reside in this bucket if exists. */
2984 ret = ocfs2_find_xe_in_bucket(inode, search,
2985 name_index, name, name_hash,
2995 * Record the bucket we have found.
2996 * When the xattr's hash value is in the gap of 2 buckets, we will
2997 * always set it to the previous bucket.
3000 lower_blkno = p_blkno;
3002 /* This should be in cache - we just read it during the search */
3003 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3009 xs->header = bucket_xh(xs->bucket);
3010 xs->base = bucket_block(xs->bucket, 0);
3011 xs->end = xs->base + inode->i_sb->s_blocksize;
3014 xs->here = &xs->header->xh_entries[index];
3015 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3016 (unsigned long long)bucket_blkno(xs->bucket), index);
3021 ocfs2_xattr_bucket_free(search);
3025 static int ocfs2_xattr_index_block_find(struct inode *inode,
3026 struct buffer_head *root_bh,
3029 struct ocfs2_xattr_search *xs)
3032 struct ocfs2_xattr_block *xb =
3033 (struct ocfs2_xattr_block *)root_bh->b_data;
3034 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3035 struct ocfs2_extent_list *el = &xb_root->xt_list;
3037 u32 first_hash, num_clusters = 0;
3038 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3040 if (le16_to_cpu(el->l_next_free_rec) == 0)
3043 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3044 name, name_hash, name_index);
3046 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3053 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3055 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3056 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3059 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3060 p_blkno, first_hash, num_clusters, xs);
3066 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3069 xattr_bucket_func *func,
3073 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3074 u32 num_buckets = clusters * bpc;
3075 struct ocfs2_xattr_bucket *bucket;
3077 bucket = ocfs2_xattr_bucket_new(inode);
3079 mlog_errno(-ENOMEM);
3083 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3084 clusters, (unsigned long long)blkno);
3086 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3087 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3094 * The real bucket num in this series of blocks is stored
3095 * in the 1st bucket.
3098 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3100 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3101 (unsigned long long)blkno,
3102 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3104 ret = func(inode, bucket, para);
3107 /* Fall through to bucket_relse() */
3110 ocfs2_xattr_bucket_relse(bucket);
3115 ocfs2_xattr_bucket_free(bucket);
3119 struct ocfs2_xattr_tree_list {
3125 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
3126 struct ocfs2_xattr_header *xh,
3133 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3136 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3138 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
3139 *new_offset = name_offset % inode->i_sb->s_blocksize;
3144 static int ocfs2_list_xattr_bucket(struct inode *inode,
3145 struct ocfs2_xattr_bucket *bucket,
3149 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3150 int i, block_off, new_offset;
3151 const char *prefix, *name;
3153 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3154 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3155 type = ocfs2_xattr_get_type(entry);
3156 prefix = ocfs2_xattr_prefix(type);
3159 ret = ocfs2_xattr_bucket_get_name_value(inode,
3167 name = (const char *)bucket_block(bucket, block_off) +
3169 ret = ocfs2_xattr_list_entry(xl->buffer,
3173 entry->xe_name_len);
3182 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3183 struct ocfs2_xattr_tree_root *xt,
3187 struct ocfs2_extent_list *el = &xt->xt_list;
3189 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3191 struct ocfs2_xattr_tree_list xl = {
3193 .buffer_size = buffer_size,
3197 if (le16_to_cpu(el->l_next_free_rec) == 0)
3200 while (name_hash > 0) {
3201 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3202 &e_cpos, &num_clusters, el);
3208 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3209 ocfs2_list_xattr_bucket,
3219 name_hash = e_cpos - 1;
3227 static int cmp_xe(const void *a, const void *b)
3229 const struct ocfs2_xattr_entry *l = a, *r = b;
3230 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3231 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3233 if (l_hash > r_hash)
3235 if (l_hash < r_hash)
3240 static void swap_xe(void *a, void *b, int size)
3242 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3245 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3246 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3250 * When the ocfs2_xattr_block is filled up, new bucket will be created
3251 * and all the xattr entries will be moved to the new bucket.
3252 * The header goes at the start of the bucket, and the names+values are
3253 * filled from the end. This is why *target starts as the last buffer.
3254 * Note: we need to sort the entries since they are not saved in order
3255 * in the ocfs2_xattr_block.
3257 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3258 struct buffer_head *xb_bh,
3259 struct ocfs2_xattr_bucket *bucket)
3261 int i, blocksize = inode->i_sb->s_blocksize;
3262 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3263 u16 offset, size, off_change;
3264 struct ocfs2_xattr_entry *xe;
3265 struct ocfs2_xattr_block *xb =
3266 (struct ocfs2_xattr_block *)xb_bh->b_data;
3267 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
3268 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3269 u16 count = le16_to_cpu(xb_xh->xh_count);
3270 char *src = xb_bh->b_data;
3271 char *target = bucket_block(bucket, blks - 1);
3273 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3274 (unsigned long long)xb_bh->b_blocknr,
3275 (unsigned long long)bucket_blkno(bucket));
3277 for (i = 0; i < blks; i++)
3278 memset(bucket_block(bucket, i), 0, blocksize);
3281 * Since the xe_name_offset is based on ocfs2_xattr_header,
3282 * there is a offset change corresponding to the change of
3283 * ocfs2_xattr_header's position.
3285 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3286 xe = &xb_xh->xh_entries[count - 1];
3287 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3288 size = blocksize - offset;
3290 /* copy all the names and values. */
3291 memcpy(target + offset, src + offset, size);
3293 /* Init new header now. */
3294 xh->xh_count = xb_xh->xh_count;
3295 xh->xh_num_buckets = cpu_to_le16(1);
3296 xh->xh_name_value_len = cpu_to_le16(size);
3297 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3299 /* copy all the entries. */
3300 target = bucket_block(bucket, 0);
3301 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3302 size = count * sizeof(struct ocfs2_xattr_entry);
3303 memcpy(target + offset, (char *)xb_xh + offset, size);
3305 /* Change the xe offset for all the xe because of the move. */
3306 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3307 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3308 for (i = 0; i < count; i++)
3309 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3311 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3312 offset, size, off_change);
3314 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3319 * After we move xattr from block to index btree, we have to
3320 * update ocfs2_xattr_search to the new xe and base.
3322 * When the entry is in xattr block, xattr_bh indicates the storage place.
3323 * While if the entry is in index b-tree, "bucket" indicates the
3324 * real place of the xattr.
3326 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3327 struct ocfs2_xattr_search *xs,
3328 struct buffer_head *old_bh)
3330 char *buf = old_bh->b_data;
3331 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3332 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
3335 xs->header = bucket_xh(xs->bucket);
3336 xs->base = bucket_block(xs->bucket, 0);
3337 xs->end = xs->base + inode->i_sb->s_blocksize;
3342 i = xs->here - old_xh->xh_entries;
3343 xs->here = &xs->header->xh_entries[i];
3346 static int ocfs2_xattr_create_index_block(struct inode *inode,
3347 struct ocfs2_xattr_search *xs,
3348 struct ocfs2_xattr_set_ctxt *ctxt)
3353 handle_t *handle = ctxt->handle;
3354 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3355 struct ocfs2_inode_info *oi = OCFS2_I(inode);
3356 struct buffer_head *xb_bh = xs->xattr_bh;
3357 struct ocfs2_xattr_block *xb =
3358 (struct ocfs2_xattr_block *)xb_bh->b_data;
3359 struct ocfs2_xattr_tree_root *xr;
3360 u16 xb_flags = le16_to_cpu(xb->xb_flags);
3362 mlog(0, "create xattr index block for %llu\n",
3363 (unsigned long long)xb_bh->b_blocknr);
3365 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
3366 BUG_ON(!xs->bucket);
3370 * We can use this lock for now, and maybe move to a dedicated mutex
3371 * if performance becomes a problem later.
3373 down_write(&oi->ip_alloc_sem);
3375 ret = ocfs2_journal_access(handle, inode, xb_bh,
3376 OCFS2_JOURNAL_ACCESS_WRITE);
3382 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3383 1, 1, &bit_off, &len);
3390 * The bucket may spread in many blocks, and
3391 * we will only touch the 1st block and the last block
3392 * in the whole bucket(one for entry and one for data).
3394 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3396 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3397 (unsigned long long)blkno);
3399 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
3405 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3406 OCFS2_JOURNAL_ACCESS_CREATE);
3412 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3413 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3415 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3417 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3418 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3419 offsetof(struct ocfs2_xattr_block, xb_attrs));
3421 xr = &xb->xb_attrs.xb_root;
3422 xr->xt_clusters = cpu_to_le32(1);
3423 xr->xt_last_eb_blk = 0;
3424 xr->xt_list.l_tree_depth = 0;
3425 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3426 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3428 xr->xt_list.l_recs[0].e_cpos = 0;
3429 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3430 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3432 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3434 ocfs2_journal_dirty(handle, xb_bh);
3437 up_write(&oi->ip_alloc_sem);
3442 static int cmp_xe_offset(const void *a, const void *b)
3444 const struct ocfs2_xattr_entry *l = a, *r = b;
3445 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3446 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3448 if (l_name_offset < r_name_offset)
3450 if (l_name_offset > r_name_offset)
3456 * defrag a xattr bucket if we find that the bucket has some
3457 * holes beteen name/value pairs.
3458 * We will move all the name/value pairs to the end of the bucket
3459 * so that we can spare some space for insertion.
3461 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3463 struct ocfs2_xattr_bucket *bucket)
3466 size_t end, offset, len, value_len;
3467 struct ocfs2_xattr_header *xh;
3468 char *entries, *buf, *bucket_buf = NULL;
3469 u64 blkno = bucket_blkno(bucket);
3471 size_t blocksize = inode->i_sb->s_blocksize;
3472 struct ocfs2_xattr_entry *xe;
3475 * In order to make the operation more efficient and generic,
3476 * we copy all the blocks into a contiguous memory and do the
3477 * defragment there, so if anything is error, we will not touch
3480 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3487 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3488 memcpy(buf, bucket_block(bucket, i), blocksize);
3490 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
3491 OCFS2_JOURNAL_ACCESS_WRITE);
3497 xh = (struct ocfs2_xattr_header *)bucket_buf;
3498 entries = (char *)xh->xh_entries;
3499 xh_free_start = le16_to_cpu(xh->xh_free_start);
3501 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3502 "xh_free_start = %u, xh_name_value_len = %u.\n",
3503 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3504 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
3507 * sort all the entries by their offset.
3508 * the largest will be the first, so that we can
3509 * move them to the end one by one.
3511 sort(entries, le16_to_cpu(xh->xh_count),
3512 sizeof(struct ocfs2_xattr_entry),
3513 cmp_xe_offset, swap_xe);
3515 /* Move all name/values to the end of the bucket. */
3516 xe = xh->xh_entries;
3517 end = OCFS2_XATTR_BUCKET_SIZE;
3518 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3519 offset = le16_to_cpu(xe->xe_name_offset);
3520 if (ocfs2_xattr_is_local(xe))
3521 value_len = OCFS2_XATTR_SIZE(
3522 le64_to_cpu(xe->xe_value_size));
3524 value_len = OCFS2_XATTR_ROOT_SIZE;
3525 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3528 * We must make sure that the name/value pair
3529 * exist in the same block. So adjust end to
3530 * the previous block end if needed.
3532 if (((end - len) / blocksize !=
3533 (end - 1) / blocksize))
3534 end = end - end % blocksize;
3536 if (end > offset + len) {
3537 memmove(bucket_buf + end - len,
3538 bucket_buf + offset, len);
3539 xe->xe_name_offset = cpu_to_le16(end - len);
3542 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3543 "bucket %llu\n", (unsigned long long)blkno);
3548 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3549 "bucket %llu\n", (unsigned long long)blkno);
3551 if (xh_free_start == end)
3554 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3555 xh->xh_free_start = cpu_to_le16(end);
3557 /* sort the entries by their name_hash. */
3558 sort(entries, le16_to_cpu(xh->xh_count),
3559 sizeof(struct ocfs2_xattr_entry),
3563 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3564 memcpy(bucket_block(bucket, i), buf, blocksize);
3565 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
3573 * prev_blkno points to the start of an existing extent. new_blkno
3574 * points to a newly allocated extent. Because we know each of our
3575 * clusters contains more than bucket, we can easily split one cluster
3576 * at a bucket boundary. So we take the last cluster of the existing
3577 * extent and split it down the middle. We move the last half of the
3578 * buckets in the last cluster of the existing extent over to the new
3581 * first_bh is the buffer at prev_blkno so we can update the existing
3582 * extent's bucket count. header_bh is the bucket were we were hoping
3583 * to insert our xattr. If the bucket move places the target in the new
3584 * extent, we'll update first_bh and header_bh after modifying the old
3587 * first_hash will be set as the 1st xe's name_hash in the new extent.
3589 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3591 struct ocfs2_xattr_bucket *first,
3592 struct ocfs2_xattr_bucket *target,
3598 struct super_block *sb = inode->i_sb;
3599 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3600 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
3601 int to_move = num_buckets / 2;
3603 u64 last_cluster_blkno = bucket_blkno(first) +
3604 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
3606 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3607 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
3609 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3610 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
3612 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
3613 last_cluster_blkno, new_blkno,
3614 to_move, first_hash);
3620 /* This is the first bucket that got moved */
3621 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3624 * If the target bucket was part of the moved buckets, we need to
3625 * update first and target.
3627 if (bucket_blkno(target) >= src_blkno) {
3628 /* Find the block for the new target bucket */
3629 src_blkno = new_blkno +
3630 (bucket_blkno(target) - src_blkno);
3632 ocfs2_xattr_bucket_relse(first);
3633 ocfs2_xattr_bucket_relse(target);
3636 * These shouldn't fail - the buffers are in the
3637 * journal from ocfs2_cp_xattr_bucket().
3639 ret = ocfs2_read_xattr_bucket(first, new_blkno);
3644 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3655 * Find the suitable pos when we divide a bucket into 2.
3656 * We have to make sure the xattrs with the same hash value exist
3657 * in the same bucket.
3659 * If this ocfs2_xattr_header covers more than one hash value, find a
3660 * place where the hash value changes. Try to find the most even split.
3661 * The most common case is that all entries have different hash values,
3662 * and the first check we make will find a place to split.
3664 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3666 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3667 int count = le16_to_cpu(xh->xh_count);
3668 int delta, middle = count / 2;
3671 * We start at the middle. Each step gets farther away in both
3672 * directions. We therefore hit the change in hash value
3673 * nearest to the middle. Note that this loop does not execute for
3676 for (delta = 0; delta < middle; delta++) {
3677 /* Let's check delta earlier than middle */
3678 if (cmp_xe(&entries[middle - delta - 1],
3679 &entries[middle - delta]))
3680 return middle - delta;
3682 /* For even counts, don't walk off the end */
3683 if ((middle + delta + 1) == count)
3686 /* Now try delta past middle */
3687 if (cmp_xe(&entries[middle + delta],
3688 &entries[middle + delta + 1]))
3689 return middle + delta + 1;
3692 /* Every entry had the same hash */
3697 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3698 * first_hash will record the 1st hash of the new bucket.
3700 * Normally half of the xattrs will be moved. But we have to make
3701 * sure that the xattrs with the same hash value are stored in the
3702 * same bucket. If all the xattrs in this bucket have the same hash
3703 * value, the new bucket will be initialized as an empty one and the
3704 * first_hash will be initialized as (hash_value+1).
3706 static int ocfs2_divide_xattr_bucket(struct inode *inode,
3711 int new_bucket_head)
3714 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
3715 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3716 struct ocfs2_xattr_header *xh;
3717 struct ocfs2_xattr_entry *xe;
3718 int blocksize = inode->i_sb->s_blocksize;
3720 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3721 (unsigned long long)blk, (unsigned long long)new_blk);
3723 s_bucket = ocfs2_xattr_bucket_new(inode);
3724 t_bucket = ocfs2_xattr_bucket_new(inode);
3725 if (!s_bucket || !t_bucket) {
3731 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
3737 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
3738 OCFS2_JOURNAL_ACCESS_WRITE);
3745 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3746 * there's no need to read it.
3748 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
3755 * Hey, if we're overwriting t_bucket, what difference does
3756 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
3757 * same part of ocfs2_cp_xattr_bucket().
3759 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3761 OCFS2_JOURNAL_ACCESS_CREATE :
3762 OCFS2_JOURNAL_ACCESS_WRITE);
3768 xh = bucket_xh(s_bucket);
3769 count = le16_to_cpu(xh->xh_count);
3770 start = ocfs2_xattr_find_divide_pos(xh);
3772 if (start == count) {
3773 xe = &xh->xh_entries[start-1];
3776 * initialized a new empty bucket here.
3777 * The hash value is set as one larger than
3778 * that of the last entry in the previous bucket.
3780 for (i = 0; i < t_bucket->bu_blocks; i++)
3781 memset(bucket_block(t_bucket, i), 0, blocksize);
3783 xh = bucket_xh(t_bucket);
3784 xh->xh_free_start = cpu_to_le16(blocksize);
3785 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3786 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3788 goto set_num_buckets;
3791 /* copy the whole bucket to the new first. */
3792 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3794 /* update the new bucket. */
3795 xh = bucket_xh(t_bucket);
3798 * Calculate the total name/value len and xh_free_start for
3799 * the old bucket first.
3801 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3803 for (i = 0; i < start; i++) {
3804 xe = &xh->xh_entries[i];
3805 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3806 if (ocfs2_xattr_is_local(xe))
3808 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3810 xe_len += OCFS2_XATTR_ROOT_SIZE;
3811 name_value_len += xe_len;
3812 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3813 name_offset = le16_to_cpu(xe->xe_name_offset);
3817 * Now begin the modification to the new bucket.
3819 * In the new bucket, We just move the xattr entry to the beginning
3820 * and don't touch the name/value. So there will be some holes in the
3821 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3824 xe = &xh->xh_entries[start];
3825 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3826 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3827 (int)((char *)xe - (char *)xh),
3828 (int)((char *)xh->xh_entries - (char *)xh));
3829 memmove((char *)xh->xh_entries, (char *)xe, len);
3830 xe = &xh->xh_entries[count - start];
3831 len = sizeof(struct ocfs2_xattr_entry) * start;
3832 memset((char *)xe, 0, len);
3834 le16_add_cpu(&xh->xh_count, -start);
3835 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3837 /* Calculate xh_free_start for the new bucket. */
3838 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3839 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3840 xe = &xh->xh_entries[i];
3841 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3842 if (ocfs2_xattr_is_local(xe))
3844 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3846 xe_len += OCFS2_XATTR_ROOT_SIZE;
3847 if (le16_to_cpu(xe->xe_name_offset) <
3848 le16_to_cpu(xh->xh_free_start))
3849 xh->xh_free_start = xe->xe_name_offset;
3853 /* set xh->xh_num_buckets for the new xh. */
3854 if (new_bucket_head)
3855 xh->xh_num_buckets = cpu_to_le16(1);
3857 xh->xh_num_buckets = 0;
3859 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3861 /* store the first_hash of the new bucket. */
3863 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3866 * Now only update the 1st block of the old bucket. If we
3867 * just added a new empty bucket, there is no need to modify
3873 xh = bucket_xh(s_bucket);
3874 memset(&xh->xh_entries[start], 0,
3875 sizeof(struct ocfs2_xattr_entry) * (count - start));
3876 xh->xh_count = cpu_to_le16(start);
3877 xh->xh_free_start = cpu_to_le16(name_offset);
3878 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3880 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
3883 ocfs2_xattr_bucket_free(s_bucket);
3884 ocfs2_xattr_bucket_free(t_bucket);
3890 * Copy xattr from one bucket to another bucket.
3892 * The caller must make sure that the journal transaction
3893 * has enough space for journaling.
3895 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3902 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3904 BUG_ON(s_blkno == t_blkno);
3906 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3907 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3910 s_bucket = ocfs2_xattr_bucket_new(inode);
3911 t_bucket = ocfs2_xattr_bucket_new(inode);
3912 if (!s_bucket || !t_bucket) {
3918 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
3923 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3924 * there's no need to read it.
3926 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
3931 * Hey, if we're overwriting t_bucket, what difference does
3932 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
3933 * cluster to fill, we came here from
3934 * ocfs2_mv_xattr_buckets(), and it is really new -
3935 * ACCESS_CREATE is required. But we also might have moved data
3936 * out of t_bucket before extending back into it.
3937 * ocfs2_add_new_xattr_bucket() can do this - its call to
3938 * ocfs2_add_new_xattr_cluster() may have created a new extent
3939 * and copied out the end of the old extent. Then it re-extends
3940 * the old extent back to create space for new xattrs. That's
3941 * how we get here, and the bucket isn't really new.
3943 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3945 OCFS2_JOURNAL_ACCESS_CREATE :
3946 OCFS2_JOURNAL_ACCESS_WRITE);
3950 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3951 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3954 ocfs2_xattr_bucket_free(t_bucket);
3955 ocfs2_xattr_bucket_free(s_bucket);
3961 * src_blk points to the start of an existing extent. last_blk points to
3962 * last cluster in that extent. to_blk points to a newly allocated
3963 * extent. We copy the buckets from the cluster at last_blk to the new
3964 * extent. If start_bucket is non-zero, we skip that many buckets before
3965 * we start copying. The new extent's xh_num_buckets gets set to the
3966 * number of buckets we copied. The old extent's xh_num_buckets shrinks
3967 * by the same amount.
3969 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
3970 u64 src_blk, u64 last_blk, u64 to_blk,
3971 unsigned int start_bucket,
3974 int i, ret, credits;
3975 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3976 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3977 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3978 struct ocfs2_xattr_bucket *old_first, *new_first;
3980 mlog(0, "mv xattrs from cluster %llu to %llu\n",
3981 (unsigned long long)last_blk, (unsigned long long)to_blk);
3983 BUG_ON(start_bucket >= num_buckets);
3985 num_buckets -= start_bucket;
3986 last_blk += (start_bucket * blks_per_bucket);
3989 /* The first bucket of the original extent */
3990 old_first = ocfs2_xattr_bucket_new(inode);
3991 /* The first bucket of the new extent */
3992 new_first = ocfs2_xattr_bucket_new(inode);
3993 if (!old_first || !new_first) {
3999 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4006 * We need to update the first bucket of the old extent and all
4007 * the buckets going to the new extent.
4009 credits = ((num_buckets + 1) * blks_per_bucket) +
4010 handle->h_buffer_credits;
4011 ret = ocfs2_extend_trans(handle, credits);
4017 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4018 OCFS2_JOURNAL_ACCESS_WRITE);
4024 for (i = 0; i < num_buckets; i++) {
4025 ret = ocfs2_cp_xattr_bucket(inode, handle,
4026 last_blk + (i * blks_per_bucket),
4027 to_blk + (i * blks_per_bucket),
4036 * Get the new bucket ready before we dirty anything
4037 * (This actually shouldn't fail, because we already dirtied
4038 * it once in ocfs2_cp_xattr_bucket()).
4040 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4045 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4046 OCFS2_JOURNAL_ACCESS_WRITE);
4052 /* Now update the headers */
4053 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4054 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4056 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4057 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4060 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4063 ocfs2_xattr_bucket_free(new_first);
4064 ocfs2_xattr_bucket_free(old_first);
4069 * Move some xattrs in this cluster to the new cluster.
4070 * This function should only be called when bucket size == cluster size.
4071 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4073 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4079 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4080 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4082 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4084 ret = ocfs2_extend_trans(handle, credits);
4090 /* Move half of the xattr in start_blk to the next bucket. */
4091 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4092 new_blk, first_hash, 1);
4096 * Move some xattrs from the old cluster to the new one since they are not
4097 * contiguous in ocfs2 xattr tree.
4099 * new_blk starts a new separate cluster, and we will move some xattrs from
4100 * prev_blk to it. v_start will be set as the first name hash value in this
4101 * new cluster so that it can be used as e_cpos during tree insertion and
4102 * don't collide with our original b-tree operations. first_bh and header_bh
4103 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4104 * to extend the insert bucket.
4106 * The problem is how much xattr should we move to the new one and when should
4107 * we update first_bh and header_bh?
4108 * 1. If cluster size > bucket size, that means the previous cluster has more
4109 * than 1 bucket, so just move half nums of bucket into the new cluster and
4110 * update the first_bh and header_bh if the insert bucket has been moved
4111 * to the new cluster.
4112 * 2. If cluster_size == bucket_size:
4113 * a) If the previous extent rec has more than one cluster and the insert
4114 * place isn't in the last cluster, copy the entire last cluster to the
4115 * new one. This time, we don't need to upate the first_bh and header_bh
4116 * since they will not be moved into the new cluster.
4117 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4118 * the new one. And we set the extend flag to zero if the insert place is
4119 * moved into the new allocated cluster since no extend is needed.
4121 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4123 struct ocfs2_xattr_bucket *first,
4124 struct ocfs2_xattr_bucket *target,
4132 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4133 (unsigned long long)bucket_blkno(first), prev_clusters,
4134 (unsigned long long)new_blk);
4136 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
4137 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4146 /* The start of the last cluster in the first extent */
4147 u64 last_blk = bucket_blkno(first) +
4148 ((prev_clusters - 1) *
4149 ocfs2_clusters_to_blocks(inode->i_sb, 1));
4151 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
4152 ret = ocfs2_mv_xattr_buckets(inode, handle,
4153 bucket_blkno(first),
4154 last_blk, new_blk, 0,
4159 ret = ocfs2_divide_xattr_cluster(inode, handle,
4165 if ((bucket_blkno(target) == last_blk) && extend)
4174 * Add a new cluster for xattr storage.
4176 * If the new cluster is contiguous with the previous one, it will be
4177 * appended to the same extent record, and num_clusters will be updated.
4178 * If not, we will insert a new extent for it and move some xattrs in
4179 * the last cluster into the new allocated one.
4180 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4181 * lose the benefits of hashing because we'll have to search large leaves.
4182 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4185 * first_bh is the first block of the previous extent rec and header_bh
4186 * indicates the bucket we will insert the new xattrs. They will be updated
4187 * when the header_bh is moved into the new cluster.
4189 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4190 struct buffer_head *root_bh,
4191 struct ocfs2_xattr_bucket *first,
4192 struct ocfs2_xattr_bucket *target,
4196 struct ocfs2_xattr_set_ctxt *ctxt)
4199 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4200 u32 prev_clusters = *num_clusters;
4201 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4203 handle_t *handle = ctxt->handle;
4204 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4205 struct ocfs2_extent_tree et;
4207 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4208 "previous xattr blkno = %llu\n",
4209 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4210 prev_cpos, (unsigned long long)bucket_blkno(first));
4212 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4214 ret = ocfs2_journal_access(handle, inode, root_bh,
4215 OCFS2_JOURNAL_ACCESS_WRITE);
4221 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
4222 clusters_to_add, &bit_off, &num_bits);
4229 BUG_ON(num_bits > clusters_to_add);
4231 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4232 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4233 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4235 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
4236 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4237 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4239 * If this cluster is contiguous with the old one and
4240 * adding this new cluster, we don't surpass the limit of
4241 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4242 * initialized and used like other buckets in the previous
4244 * So add it as a contiguous one. The caller will handle
4247 v_start = prev_cpos + prev_clusters;
4248 *num_clusters = prev_clusters + num_bits;
4249 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4252 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4266 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4267 num_bits, (unsigned long long)block, v_start);
4268 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
4269 num_bits, 0, ctxt->meta_ac);
4275 ret = ocfs2_journal_dirty(handle, root_bh);
4284 * We are given an extent. 'first' is the bucket at the very front of
4285 * the extent. The extent has space for an additional bucket past
4286 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4287 * of the target bucket. We wish to shift every bucket past the target
4288 * down one, filling in that additional space. When we get back to the
4289 * target, we split the target between itself and the now-empty bucket
4290 * at target+1 (aka, target_blkno + blks_per_bucket).
4292 static int ocfs2_extend_xattr_bucket(struct inode *inode,
4294 struct ocfs2_xattr_bucket *first,
4299 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4300 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4302 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
4304 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4305 "from %llu, len = %u\n", (unsigned long long)target_blk,
4306 (unsigned long long)bucket_blkno(first), num_clusters);
4308 /* The extent must have room for an additional bucket */
4309 BUG_ON(new_bucket >=
4310 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
4312 /* end_blk points to the last existing bucket */
4313 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
4316 * end_blk is the start of the last existing bucket.
4317 * Thus, (end_blk - target_blk) covers the target bucket and
4318 * every bucket after it up to, but not including, the last
4319 * existing bucket. Then we add the last existing bucket, the
4320 * new bucket, and the first bucket (3 * blk_per_bucket).
4322 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
4323 handle->h_buffer_credits;
4324 ret = ocfs2_extend_trans(handle, credits);
4330 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4331 OCFS2_JOURNAL_ACCESS_WRITE);
4337 while (end_blk != target_blk) {
4338 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4339 end_blk + blk_per_bucket, 0);
4342 end_blk -= blk_per_bucket;
4345 /* Move half of the xattr in target_blkno to the next bucket. */
4346 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4347 target_blk + blk_per_bucket, NULL, 0);
4349 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4350 ocfs2_xattr_bucket_journal_dirty(handle, first);
4357 * Add new xattr bucket in an extent record and adjust the buckets
4358 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4359 * bucket we want to insert into.
4361 * In the easy case, we will move all the buckets after target down by
4362 * one. Half of target's xattrs will be moved to the next bucket.
4364 * If current cluster is full, we'll allocate a new one. This may not
4365 * be contiguous. The underlying calls will make sure that there is
4366 * space for the insert, shifting buckets around if necessary.
4367 * 'target' may be moved by those calls.
4369 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4370 struct buffer_head *xb_bh,
4371 struct ocfs2_xattr_bucket *target,
4372 struct ocfs2_xattr_set_ctxt *ctxt)
4374 struct ocfs2_xattr_block *xb =
4375 (struct ocfs2_xattr_block *)xb_bh->b_data;
4376 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4377 struct ocfs2_extent_list *el = &xb_root->xt_list;
4379 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
4380 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4381 int ret, num_buckets, extend = 1;
4383 u32 e_cpos, num_clusters;
4384 /* The bucket at the front of the extent */
4385 struct ocfs2_xattr_bucket *first;
4387 mlog(0, "Add new xattr bucket starting from %llu\n",
4388 (unsigned long long)bucket_blkno(target));
4390 /* The first bucket of the original extent */
4391 first = ocfs2_xattr_bucket_new(inode);
4398 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4405 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4411 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4412 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4414 * This can move first+target if the target bucket moves
4415 * to the new extent.
4417 ret = ocfs2_add_new_xattr_cluster(inode,
4432 ret = ocfs2_extend_xattr_bucket(inode,
4435 bucket_blkno(target),
4442 ocfs2_xattr_bucket_free(first);
4447 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4448 struct ocfs2_xattr_bucket *bucket,
4451 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4453 offs = offs % inode->i_sb->s_blocksize;
4454 return bucket_block(bucket, block_off) + offs;
4458 * Handle the normal xattr set, including replace, delete and new.
4460 * Note: "local" indicates the real data's locality. So we can't
4461 * just its bucket locality by its length.
4463 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4464 struct ocfs2_xattr_info *xi,
4465 struct ocfs2_xattr_search *xs,
4469 struct ocfs2_xattr_entry *last, *xe;
4470 int name_len = strlen(xi->name);
4471 struct ocfs2_xattr_header *xh = xs->header;
4472 u16 count = le16_to_cpu(xh->xh_count), start;
4473 size_t blocksize = inode->i_sb->s_blocksize;
4475 size_t offs, size, new_size;
4477 last = &xh->xh_entries[count];
4478 if (!xs->not_found) {
4480 offs = le16_to_cpu(xe->xe_name_offset);
4481 if (ocfs2_xattr_is_local(xe))
4482 size = OCFS2_XATTR_SIZE(name_len) +
4483 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4485 size = OCFS2_XATTR_SIZE(name_len) +
4486 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4489 * If the new value will be stored outside, xi->value has been
4490 * initalized as an empty ocfs2_xattr_value_root, and the same
4491 * goes with xi->value_len, so we can set new_size safely here.
4492 * See ocfs2_xattr_set_in_bucket.
4494 new_size = OCFS2_XATTR_SIZE(name_len) +
4495 OCFS2_XATTR_SIZE(xi->value_len);
4497 le16_add_cpu(&xh->xh_name_value_len, -size);
4499 if (new_size > size)
4500 goto set_new_name_value;
4502 /* Now replace the old value with new one. */
4504 xe->xe_value_size = cpu_to_le64(xi->value_len);
4506 xe->xe_value_size = 0;
4508 val = ocfs2_xattr_bucket_get_val(inode,
4510 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4511 size - OCFS2_XATTR_SIZE(name_len));
4512 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4513 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4514 xi->value, xi->value_len);
4516 le16_add_cpu(&xh->xh_name_value_len, new_size);
4517 ocfs2_xattr_set_local(xe, local);
4521 * Remove the old entry if there is more than one.
4522 * We don't remove the last entry so that we can
4523 * use it to indicate the hash value of the empty
4527 le16_add_cpu(&xh->xh_count, -1);
4530 (void *)last - (void *)xe);
4532 sizeof(struct ocfs2_xattr_entry));
4535 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4540 /* find a new entry for insert. */
4541 int low = 0, high = count - 1, tmp;
4542 struct ocfs2_xattr_entry *tmp_xe;
4544 while (low <= high && count) {
4545 tmp = (low + high) / 2;
4546 tmp_xe = &xh->xh_entries[tmp];
4548 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4550 else if (name_hash <
4551 le32_to_cpu(tmp_xe->xe_name_hash))
4559 xe = &xh->xh_entries[low];
4561 memmove(xe + 1, xe, (void *)last - (void *)xe);
4563 le16_add_cpu(&xh->xh_count, 1);
4564 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4565 xe->xe_name_hash = cpu_to_le32(name_hash);
4566 xe->xe_name_len = name_len;
4567 ocfs2_xattr_set_type(xe, xi->name_index);
4571 /* Insert the new name+value. */
4572 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4575 * We must make sure that the name/value pair
4576 * exists in the same block.
4578 offs = le16_to_cpu(xh->xh_free_start);
4579 start = offs - size;
4581 if (start >> inode->i_sb->s_blocksize_bits !=
4582 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4583 offs = offs - offs % blocksize;
4584 xh->xh_free_start = cpu_to_le16(offs);
4587 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
4588 xe->xe_name_offset = cpu_to_le16(offs - size);
4590 memset(val, 0, size);
4591 memcpy(val, xi->name, name_len);
4592 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4594 xe->xe_value_size = cpu_to_le64(xi->value_len);
4595 ocfs2_xattr_set_local(xe, local);
4597 le16_add_cpu(&xh->xh_free_start, -size);
4598 le16_add_cpu(&xh->xh_name_value_len, size);
4604 * Set the xattr entry in the specified bucket.
4605 * The bucket is indicated by xs->bucket and it should have the enough
4606 * space for the xattr insertion.
4608 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4610 struct ocfs2_xattr_info *xi,
4611 struct ocfs2_xattr_search *xs,
4618 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4619 (unsigned long)xi->value_len, xi->name_index,
4620 (unsigned long long)bucket_blkno(xs->bucket));
4622 if (!xs->bucket->bu_bhs[1]) {
4623 blkno = bucket_blkno(xs->bucket);
4624 ocfs2_xattr_bucket_relse(xs->bucket);
4625 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
4632 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4633 OCFS2_JOURNAL_ACCESS_WRITE);
4639 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4640 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4647 * Truncate the specified xe_off entry in xattr bucket.
4648 * bucket is indicated by header_bh and len is the new length.
4649 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4651 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4653 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4654 struct ocfs2_xattr_bucket *bucket,
4657 struct ocfs2_xattr_set_ctxt *ctxt)
4661 struct buffer_head *value_bh = NULL;
4662 struct ocfs2_xattr_value_root *xv;
4663 struct ocfs2_xattr_entry *xe;
4664 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4665 size_t blocksize = inode->i_sb->s_blocksize;
4667 xe = &xh->xh_entries[xe_off];
4669 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4671 offset = le16_to_cpu(xe->xe_name_offset) +
4672 OCFS2_XATTR_SIZE(xe->xe_name_len);
4674 value_blk = offset / blocksize;
4676 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4677 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4679 value_bh = bucket->bu_bhs[value_blk];
4682 xv = (struct ocfs2_xattr_value_root *)
4683 (value_bh->b_data + offset % blocksize);
4685 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
4686 OCFS2_JOURNAL_ACCESS_WRITE);
4693 * From here on out we have to dirty the bucket. The generic
4694 * value calls only modify one of the bucket's bhs, but we need
4695 * to send the bucket at once. So if they error, they *could* have
4696 * modified something. We have to assume they did, and dirty
4697 * the whole bucket. This leaves us in a consistent state.
4699 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4700 xe_off, (unsigned long long)bucket_blkno(bucket), len);
4701 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
4707 xe->xe_value_size = cpu_to_le64(len);
4710 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
4716 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4717 struct ocfs2_xattr_search *xs,
4719 struct ocfs2_xattr_set_ctxt *ctxt)
4722 struct ocfs2_xattr_entry *xe = xs->here;
4723 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4725 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4727 offset = xe - xh->xh_entries;
4728 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
4736 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4738 struct ocfs2_xattr_search *xs,
4743 struct ocfs2_xattr_value_root *xv;
4744 struct ocfs2_xattr_entry *xe = xs->here;
4746 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4748 offset = le16_to_cpu(xe->xe_name_offset) +
4749 OCFS2_XATTR_SIZE(xe->xe_name_len);
4751 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4753 return __ocfs2_xattr_set_value_outside(inode, handle,
4754 xv, val, value_len);
4757 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4758 struct buffer_head *root_bh,
4764 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4765 struct inode *tl_inode = osb->osb_tl_inode;
4767 struct ocfs2_xattr_block *xb =
4768 (struct ocfs2_xattr_block *)root_bh->b_data;
4769 struct ocfs2_alloc_context *meta_ac = NULL;
4770 struct ocfs2_cached_dealloc_ctxt dealloc;
4771 struct ocfs2_extent_tree et;
4773 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4775 ocfs2_init_dealloc_ctxt(&dealloc);
4777 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4778 cpos, len, (unsigned long long)blkno);
4780 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4782 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4788 mutex_lock(&tl_inode->i_mutex);
4790 if (ocfs2_truncate_log_needs_flush(osb)) {
4791 ret = __ocfs2_flush_truncate_log(osb);
4798 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
4799 if (IS_ERR(handle)) {
4805 ret = ocfs2_journal_access(handle, inode, root_bh,
4806 OCFS2_JOURNAL_ACCESS_WRITE);
4812 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4819 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4821 ret = ocfs2_journal_dirty(handle, root_bh);
4827 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4832 ocfs2_commit_trans(osb, handle);
4834 ocfs2_schedule_truncate_log_flush(osb, 1);
4836 mutex_unlock(&tl_inode->i_mutex);
4839 ocfs2_free_alloc_context(meta_ac);
4841 ocfs2_run_deallocs(osb, &dealloc);
4846 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4848 struct ocfs2_xattr_search *xs)
4850 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
4851 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4852 le16_to_cpu(xh->xh_count) - 1];
4855 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4856 OCFS2_JOURNAL_ACCESS_WRITE);
4862 /* Remove the old entry. */
4863 memmove(xs->here, xs->here + 1,
4864 (void *)last - (void *)xs->here);
4865 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4866 le16_add_cpu(&xh->xh_count, -1);
4868 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4872 * Set the xattr name/value in the bucket specified in xs.
4874 * As the new value in xi may be stored in the bucket or in an outside cluster,
4875 * we divide the whole process into 3 steps:
4876 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4877 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4878 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4879 * 4. If the clusters for the new outside value can't be allocated, we need
4880 * to free the xattr we allocated in set.
4882 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4883 struct ocfs2_xattr_info *xi,
4884 struct ocfs2_xattr_search *xs,
4885 struct ocfs2_xattr_set_ctxt *ctxt)
4889 char *val = (char *)xi->value;
4890 struct ocfs2_xattr_entry *xe = xs->here;
4891 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4894 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4896 * We need to truncate the xattr storage first.
4898 * If both the old and new value are stored to
4899 * outside block, we only need to truncate
4900 * the storage and then set the value outside.
4902 * If the new value should be stored within block,
4903 * we should free all the outside block first and
4904 * the modification to the xattr block will be done
4905 * by following steps.
4907 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4908 value_len = xi->value_len;
4912 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4919 goto set_value_outside;
4922 value_len = xi->value_len;
4923 /* So we have to handle the inside block change now. */
4924 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4926 * If the new value will be stored outside of block,
4927 * initalize a new empty value root and insert it first.
4930 xi->value = &def_xv;
4931 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4934 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4941 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4944 /* allocate the space now for the outside block storage. */
4945 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4950 if (xs->not_found) {
4952 * We can't allocate enough clusters for outside
4953 * storage and we have allocated xattr already,
4954 * so need to remove it.
4956 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
4962 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4963 xs, val, value_len);
4969 * check whether the xattr bucket is filled up with the same hash value.
4970 * If we want to insert the xattr with the same hash, return -ENOSPC.
4971 * If we want to insert a xattr with different hash value, go ahead
4972 * and ocfs2_divide_xattr_bucket will handle this.
4974 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4975 struct ocfs2_xattr_bucket *bucket,
4978 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4979 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4981 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4984 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4985 xh->xh_entries[0].xe_name_hash) {
4986 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4988 (unsigned long long)bucket_blkno(bucket),
4989 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4996 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4997 struct ocfs2_xattr_info *xi,
4998 struct ocfs2_xattr_search *xs,
4999 struct ocfs2_xattr_set_ctxt *ctxt)
5001 struct ocfs2_xattr_header *xh;
5002 struct ocfs2_xattr_entry *xe;
5003 u16 count, header_size, xh_free_start;
5004 int free, max_free, need, old;
5005 size_t value_size = 0, name_len = strlen(xi->name);
5006 size_t blocksize = inode->i_sb->s_blocksize;
5007 int ret, allocation = 0;
5009 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5013 count = le16_to_cpu(xh->xh_count);
5014 xh_free_start = le16_to_cpu(xh->xh_free_start);
5015 header_size = sizeof(struct ocfs2_xattr_header) +
5016 count * sizeof(struct ocfs2_xattr_entry);
5017 max_free = OCFS2_XATTR_BUCKET_SIZE -
5018 le16_to_cpu(xh->xh_name_value_len) - header_size;
5020 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5021 "of %u which exceed block size\n",
5022 (unsigned long long)bucket_blkno(xs->bucket),
5025 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5026 value_size = OCFS2_XATTR_ROOT_SIZE;
5028 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5031 need = sizeof(struct ocfs2_xattr_entry) +
5032 OCFS2_XATTR_SIZE(name_len) + value_size;
5034 need = value_size + OCFS2_XATTR_SIZE(name_len);
5037 * We only replace the old value if the new length is smaller
5038 * than the old one. Otherwise we will allocate new space in the
5039 * bucket to store it.
5042 if (ocfs2_xattr_is_local(xe))
5043 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5045 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5047 if (old >= value_size)
5051 free = xh_free_start - header_size;
5053 * We need to make sure the new name/value pair
5054 * can exist in the same block.
5056 if (xh_free_start % blocksize < need)
5057 free -= xh_free_start % blocksize;
5059 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5060 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5061 " %u\n", xs->not_found,
5062 (unsigned long long)bucket_blkno(xs->bucket),
5063 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5064 le16_to_cpu(xh->xh_name_value_len));
5068 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5069 if (need <= max_free &&
5070 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5072 * We can create the space by defragment. Since only the
5073 * name/value will be moved, the xe shouldn't be changed
5076 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5083 xh_free_start = le16_to_cpu(xh->xh_free_start);
5084 free = xh_free_start - header_size;
5085 if (xh_free_start % blocksize < need)
5086 free -= xh_free_start % blocksize;
5091 mlog(0, "Can't get enough space for xattr insert by "
5092 "defragment. Need %u bytes, but we have %d, so "
5093 "allocate new bucket for it.\n", need, free);
5097 * We have to add new buckets or clusters and one
5098 * allocation should leave us enough space for insert.
5103 * We do not allow for overlapping ranges between buckets. And
5104 * the maximum number of collisions we will allow for then is
5105 * one bucket's worth, so check it here whether we need to
5106 * add a new bucket for the insert.
5108 ret = ocfs2_check_xattr_bucket_collision(inode,
5116 ret = ocfs2_add_new_xattr_bucket(inode,
5126 * ocfs2_add_new_xattr_bucket() will have updated
5127 * xs->bucket if it moved, but it will not have updated
5128 * any of the other search fields. Thus, we drop it and
5129 * re-search. Everything should be cached, so it'll be
5132 ocfs2_xattr_bucket_relse(xs->bucket);
5133 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5136 if (ret && ret != -ENODATA)
5138 xs->not_found = ret;
5144 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5150 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5151 struct ocfs2_xattr_bucket *bucket,
5155 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5157 struct ocfs2_xattr_entry *xe;
5158 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5159 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5160 int credits = ocfs2_remove_extent_credits(osb->sb) +
5161 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5164 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5166 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5167 xe = &xh->xh_entries[i];
5168 if (ocfs2_xattr_is_local(xe))
5171 ctxt.handle = ocfs2_start_trans(osb, credits);
5172 if (IS_ERR(ctxt.handle)) {
5173 ret = PTR_ERR(ctxt.handle);
5178 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5181 ocfs2_commit_trans(osb, ctxt.handle);
5188 ocfs2_schedule_truncate_log_flush(osb, 1);
5189 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5193 static int ocfs2_delete_xattr_index_block(struct inode *inode,
5194 struct buffer_head *xb_bh)
5196 struct ocfs2_xattr_block *xb =
5197 (struct ocfs2_xattr_block *)xb_bh->b_data;
5198 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5200 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5203 if (le16_to_cpu(el->l_next_free_rec) == 0)
5206 while (name_hash > 0) {
5207 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5208 &e_cpos, &num_clusters, el);
5214 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5215 ocfs2_delete_xattr_in_bucket,
5222 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5223 p_blkno, e_cpos, num_clusters);
5232 name_hash = e_cpos - 1;
5240 * 'security' attributes support
5242 static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5243 size_t list_size, const char *name,
5246 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5247 const size_t total_len = prefix_len + name_len + 1;
5249 if (list && total_len <= list_size) {
5250 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5251 memcpy(list + prefix_len, name, name_len);
5252 list[prefix_len + name_len] = '\0';
5257 static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5258 void *buffer, size_t size)
5260 if (strcmp(name, "") == 0)
5262 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5266 static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5267 const void *value, size_t size, int flags)
5269 if (strcmp(name, "") == 0)
5272 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5276 int ocfs2_init_security_get(struct inode *inode,
5278 struct ocfs2_security_xattr_info *si)
5280 return security_inode_init_security(inode, dir, &si->name, &si->value,
5284 int ocfs2_init_security_set(handle_t *handle,
5285 struct inode *inode,
5286 struct buffer_head *di_bh,
5287 struct ocfs2_security_xattr_info *si,
5288 struct ocfs2_alloc_context *xattr_ac,
5289 struct ocfs2_alloc_context *data_ac)
5291 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5292 OCFS2_XATTR_INDEX_SECURITY,
5293 si->name, si->value, si->value_len, 0,
5297 struct xattr_handler ocfs2_xattr_security_handler = {
5298 .prefix = XATTR_SECURITY_PREFIX,
5299 .list = ocfs2_xattr_security_list,
5300 .get = ocfs2_xattr_security_get,
5301 .set = ocfs2_xattr_security_set,
5305 * 'trusted' attributes support
5307 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5308 size_t list_size, const char *name,
5311 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
5312 const size_t total_len = prefix_len + name_len + 1;
5314 if (list && total_len <= list_size) {
5315 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5316 memcpy(list + prefix_len, name, name_len);
5317 list[prefix_len + name_len] = '\0';
5322 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5323 void *buffer, size_t size)
5325 if (strcmp(name, "") == 0)
5327 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5331 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5332 const void *value, size_t size, int flags)
5334 if (strcmp(name, "") == 0)
5337 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5341 struct xattr_handler ocfs2_xattr_trusted_handler = {
5342 .prefix = XATTR_TRUSTED_PREFIX,
5343 .list = ocfs2_xattr_trusted_list,
5344 .get = ocfs2_xattr_trusted_get,
5345 .set = ocfs2_xattr_trusted_set,
5349 * 'user' attributes support
5351 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5352 size_t list_size, const char *name,
5355 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
5356 const size_t total_len = prefix_len + name_len + 1;
5357 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5359 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5362 if (list && total_len <= list_size) {
5363 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5364 memcpy(list + prefix_len, name, name_len);
5365 list[prefix_len + name_len] = '\0';
5370 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5371 void *buffer, size_t size)
5373 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5375 if (strcmp(name, "") == 0)
5377 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5379 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5383 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5384 const void *value, size_t size, int flags)
5386 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5388 if (strcmp(name, "") == 0)
5390 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5393 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5397 struct xattr_handler ocfs2_xattr_user_handler = {
5398 .prefix = XATTR_USER_PREFIX,
5399 .list = ocfs2_xattr_user_list,
5400 .get = ocfs2_xattr_user_get,
5401 .set = ocfs2_xattr_user_set,