1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
54 #include "buffer_head_io.h"
59 struct ocfs2_xattr_def_value_root {
60 struct ocfs2_xattr_value_root xv;
61 struct ocfs2_extent_rec er;
64 struct ocfs2_xattr_bucket {
65 /* The inode these xattrs are associated with */
66 struct inode *bu_inode;
68 /* The actual buffers that make up the bucket */
69 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
71 /* How many blocks make up one bucket for this filesystem */
75 struct ocfs2_xattr_set_ctxt {
77 struct ocfs2_alloc_context *meta_ac;
78 struct ocfs2_alloc_context *data_ac;
79 struct ocfs2_cached_dealloc_ctxt dealloc;
82 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
83 #define OCFS2_XATTR_INLINE_SIZE 80
84 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
85 - sizeof(struct ocfs2_xattr_header) \
87 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
88 - sizeof(struct ocfs2_xattr_block) \
89 - sizeof(struct ocfs2_xattr_header) \
92 static struct ocfs2_xattr_def_value_root def_xv = {
93 .xv.xr_list.l_count = cpu_to_le16(1),
96 struct xattr_handler *ocfs2_xattr_handlers[] = {
97 &ocfs2_xattr_user_handler,
98 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
99 &ocfs2_xattr_acl_access_handler,
100 &ocfs2_xattr_acl_default_handler,
102 &ocfs2_xattr_trusted_handler,
103 &ocfs2_xattr_security_handler,
107 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
108 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
109 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
115 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
116 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
119 struct ocfs2_xattr_info {
126 struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
134 struct ocfs2_xattr_bucket *bucket;
137 struct ocfs2_xattr_entry *here;
141 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
142 struct ocfs2_xattr_header *xh,
147 static int ocfs2_xattr_block_find(struct inode *inode,
150 struct ocfs2_xattr_search *xs);
151 static int ocfs2_xattr_index_block_find(struct inode *inode,
152 struct buffer_head *root_bh,
155 struct ocfs2_xattr_search *xs);
157 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
158 struct ocfs2_xattr_tree_root *xt,
162 static int ocfs2_xattr_create_index_block(struct inode *inode,
163 struct ocfs2_xattr_search *xs,
164 struct ocfs2_xattr_set_ctxt *ctxt);
166 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
167 struct ocfs2_xattr_info *xi,
168 struct ocfs2_xattr_search *xs,
169 struct ocfs2_xattr_set_ctxt *ctxt);
171 static int ocfs2_delete_xattr_index_block(struct inode *inode,
172 struct buffer_head *xb_bh);
173 static int ocfs2_cp_xattr_bucket(struct inode *inode,
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 ocfs2_xattr_bucket_relse(bucket);
281 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
282 struct ocfs2_xattr_bucket *bucket,
287 for (i = 0; i < bucket->bu_blocks; i++) {
288 rc = ocfs2_journal_access(handle, bucket->bu_inode,
289 bucket->bu_bhs[i], type);
299 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
300 struct ocfs2_xattr_bucket *bucket)
304 for (i = 0; i < bucket->bu_blocks; i++)
305 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
308 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
309 struct ocfs2_xattr_bucket *src)
312 int blocksize = src->bu_inode->i_sb->s_blocksize;
314 BUG_ON(dest->bu_blocks != src->bu_blocks);
315 BUG_ON(dest->bu_inode != src->bu_inode);
317 for (i = 0; i < src->bu_blocks; i++) {
318 memcpy(bucket_block(dest, i), bucket_block(src, i),
323 static int ocfs2_validate_xattr_block(struct super_block *sb,
324 struct buffer_head *bh)
326 struct ocfs2_xattr_block *xb =
327 (struct ocfs2_xattr_block *)bh->b_data;
329 mlog(0, "Validating xattr block %llu\n",
330 (unsigned long long)bh->b_blocknr);
332 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
334 "Extended attribute block #%llu has bad "
336 (unsigned long long)bh->b_blocknr, 7,
341 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
343 "Extended attribute block #%llu has an "
344 "invalid xb_blkno of %llu",
345 (unsigned long long)bh->b_blocknr,
346 (unsigned long long)le64_to_cpu(xb->xb_blkno));
350 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
352 "Extended attribute block #%llu has an invalid "
353 "xb_fs_generation of #%u",
354 (unsigned long long)bh->b_blocknr,
355 le32_to_cpu(xb->xb_fs_generation));
362 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
363 struct buffer_head **bh)
366 struct buffer_head *tmp = *bh;
368 rc = ocfs2_read_block(inode, xb_blkno, &tmp,
369 ocfs2_validate_xattr_block);
371 /* If ocfs2_read_block() got us a new bh, pass it up. */
378 static inline const char *ocfs2_xattr_prefix(int name_index)
380 struct xattr_handler *handler = NULL;
382 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
383 handler = ocfs2_xattr_handler_map[name_index];
385 return handler ? handler->prefix : NULL;
388 static u32 ocfs2_xattr_name_hash(struct inode *inode,
392 /* Get hash value of uuid from super block */
393 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
396 /* hash extended attribute name */
397 for (i = 0; i < name_len; i++) {
398 hash = (hash << OCFS2_HASH_SHIFT) ^
399 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
407 * ocfs2_xattr_hash_entry()
409 * Compute the hash of an extended attribute.
411 static void ocfs2_xattr_hash_entry(struct inode *inode,
412 struct ocfs2_xattr_header *header,
413 struct ocfs2_xattr_entry *entry)
416 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
418 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
419 entry->xe_name_hash = cpu_to_le32(hash);
424 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
428 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
429 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
431 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
432 size += sizeof(struct ocfs2_xattr_entry);
437 int ocfs2_calc_security_init(struct inode *dir,
438 struct ocfs2_security_xattr_info *si,
441 struct ocfs2_alloc_context **xattr_ac)
444 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
445 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
449 * The max space of security xattr taken inline is
450 * 256(name) + 80(value) + 16(entry) = 352 bytes,
451 * So reserve one metadata block for it is ok.
453 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
454 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
455 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
460 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
463 /* reserve clusters for xattr value which will be set in B tree*/
464 if (si->value_len > OCFS2_XATTR_INLINE_SIZE)
465 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
470 int ocfs2_calc_xattr_init(struct inode *dir,
471 struct buffer_head *dir_bh,
473 struct ocfs2_security_xattr_info *si,
476 struct ocfs2_alloc_context **xattr_ac)
479 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
485 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
488 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
489 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
490 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
493 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
496 } else if (acl_len != 0 && acl_len != -ENODATA) {
502 if (!(s_size + a_size))
506 * The max space of security xattr taken inline is
507 * 256(name) + 80(value) + 16(entry) = 352 bytes,
508 * The max space of acl xattr taken inline is
509 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
510 * when blocksize = 512, may reserve one more cluser for
511 * xattr bucket, otherwise reserve one metadata block
514 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
515 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
516 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
521 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
524 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
525 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
527 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
530 /* reserve clusters for xattr value which will be set in B tree*/
531 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE)
532 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
534 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
535 acl_len > OCFS2_XATTR_INLINE_SIZE) {
536 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
538 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
545 static int ocfs2_xattr_extend_allocation(struct inode *inode,
547 struct buffer_head *xattr_bh,
548 struct ocfs2_xattr_value_root *xv,
549 struct ocfs2_xattr_set_ctxt *ctxt)
552 handle_t *handle = ctxt->handle;
553 enum ocfs2_alloc_restarted why;
554 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
555 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
556 struct ocfs2_extent_tree et;
558 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
560 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
562 status = ocfs2_journal_access(handle, inode, xattr_bh,
563 OCFS2_JOURNAL_ACCESS_WRITE);
569 prev_clusters = le32_to_cpu(xv->xr_clusters);
570 status = ocfs2_add_clusters_in_btree(osb,
585 status = ocfs2_journal_dirty(handle, xattr_bh);
591 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
594 * We should have already allocated enough space before the transaction,
595 * so no need to restart.
597 BUG_ON(why != RESTART_NONE || clusters_to_add);
604 static int __ocfs2_remove_xattr_range(struct inode *inode,
605 struct buffer_head *root_bh,
606 struct ocfs2_xattr_value_root *xv,
607 u32 cpos, u32 phys_cpos, u32 len,
608 struct ocfs2_xattr_set_ctxt *ctxt)
611 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
612 handle_t *handle = ctxt->handle;
613 struct ocfs2_extent_tree et;
615 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
617 ret = ocfs2_journal_access(handle, inode, root_bh,
618 OCFS2_JOURNAL_ACCESS_WRITE);
624 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
631 le32_add_cpu(&xv->xr_clusters, -len);
633 ret = ocfs2_journal_dirty(handle, root_bh);
639 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
647 static int ocfs2_xattr_shrink_size(struct inode *inode,
650 struct buffer_head *root_bh,
651 struct ocfs2_xattr_value_root *xv,
652 struct ocfs2_xattr_set_ctxt *ctxt)
655 u32 trunc_len, cpos, phys_cpos, alloc_size;
658 if (old_clusters <= new_clusters)
662 trunc_len = old_clusters - new_clusters;
664 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
665 &alloc_size, &xv->xr_list);
671 if (alloc_size > trunc_len)
672 alloc_size = trunc_len;
674 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
675 phys_cpos, alloc_size,
682 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
683 ocfs2_remove_xattr_clusters_from_cache(inode, block,
686 trunc_len -= alloc_size;
693 static int ocfs2_xattr_value_truncate(struct inode *inode,
694 struct buffer_head *root_bh,
695 struct ocfs2_xattr_value_root *xv,
697 struct ocfs2_xattr_set_ctxt *ctxt)
700 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
701 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
703 if (new_clusters == old_clusters)
706 if (new_clusters > old_clusters)
707 ret = ocfs2_xattr_extend_allocation(inode,
708 new_clusters - old_clusters,
711 ret = ocfs2_xattr_shrink_size(inode,
712 old_clusters, new_clusters,
718 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
719 size_t *result, const char *prefix,
720 const char *name, int name_len)
722 char *p = buffer + *result;
723 int prefix_len = strlen(prefix);
724 int total_len = prefix_len + name_len + 1;
726 *result += total_len;
728 /* we are just looking for how big our buffer needs to be */
735 memcpy(p, prefix, prefix_len);
736 memcpy(p + prefix_len, name, name_len);
737 p[prefix_len + name_len] = '\0';
742 static int ocfs2_xattr_list_entries(struct inode *inode,
743 struct ocfs2_xattr_header *header,
744 char *buffer, size_t buffer_size)
748 const char *prefix, *name;
750 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
751 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
752 type = ocfs2_xattr_get_type(entry);
753 prefix = ocfs2_xattr_prefix(type);
756 name = (const char *)header +
757 le16_to_cpu(entry->xe_name_offset);
759 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
760 &result, prefix, name,
770 static int ocfs2_xattr_ibody_list(struct inode *inode,
771 struct ocfs2_dinode *di,
775 struct ocfs2_xattr_header *header = NULL;
776 struct ocfs2_inode_info *oi = OCFS2_I(inode);
779 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
782 header = (struct ocfs2_xattr_header *)
783 ((void *)di + inode->i_sb->s_blocksize -
784 le16_to_cpu(di->i_xattr_inline_size));
786 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
791 static int ocfs2_xattr_block_list(struct inode *inode,
792 struct ocfs2_dinode *di,
796 struct buffer_head *blk_bh = NULL;
797 struct ocfs2_xattr_block *xb;
800 if (!di->i_xattr_loc)
803 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
810 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
811 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
812 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
813 ret = ocfs2_xattr_list_entries(inode, header,
814 buffer, buffer_size);
816 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
817 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
818 buffer, buffer_size);
826 ssize_t ocfs2_listxattr(struct dentry *dentry,
830 int ret = 0, i_ret = 0, b_ret = 0;
831 struct buffer_head *di_bh = NULL;
832 struct ocfs2_dinode *di = NULL;
833 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
835 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
838 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
841 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
847 di = (struct ocfs2_dinode *)di_bh->b_data;
849 down_read(&oi->ip_xattr_sem);
850 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
858 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
863 up_read(&oi->ip_xattr_sem);
864 ocfs2_inode_unlock(dentry->d_inode, 0);
868 return i_ret + b_ret;
871 static int ocfs2_xattr_find_entry(int name_index,
873 struct ocfs2_xattr_search *xs)
875 struct ocfs2_xattr_entry *entry;
882 name_len = strlen(name);
884 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
885 cmp = name_index - ocfs2_xattr_get_type(entry);
887 cmp = name_len - entry->xe_name_len;
889 cmp = memcmp(name, (xs->base +
890 le16_to_cpu(entry->xe_name_offset)),
898 return cmp ? -ENODATA : 0;
901 static int ocfs2_xattr_get_value_outside(struct inode *inode,
902 struct ocfs2_xattr_value_root *xv,
906 u32 cpos, p_cluster, num_clusters, bpc, clusters;
909 size_t cplen, blocksize;
910 struct buffer_head *bh = NULL;
911 struct ocfs2_extent_list *el;
914 clusters = le32_to_cpu(xv->xr_clusters);
915 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
916 blocksize = inode->i_sb->s_blocksize;
919 while (cpos < clusters) {
920 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
927 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
928 /* Copy ocfs2_xattr_value */
929 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
930 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
936 cplen = len >= blocksize ? blocksize : len;
937 memcpy(buffer, bh->b_data, cplen);
946 cpos += num_clusters;
952 static int ocfs2_xattr_ibody_get(struct inode *inode,
957 struct ocfs2_xattr_search *xs)
959 struct ocfs2_inode_info *oi = OCFS2_I(inode);
960 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
961 struct ocfs2_xattr_value_root *xv;
965 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
968 xs->end = (void *)di + inode->i_sb->s_blocksize;
969 xs->header = (struct ocfs2_xattr_header *)
970 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
971 xs->base = (void *)xs->header;
972 xs->here = xs->header->xh_entries;
974 ret = ocfs2_xattr_find_entry(name_index, name, xs);
977 size = le64_to_cpu(xs->here->xe_value_size);
979 if (size > buffer_size)
981 if (ocfs2_xattr_is_local(xs->here)) {
982 memcpy(buffer, (void *)xs->base +
983 le16_to_cpu(xs->here->xe_name_offset) +
984 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
986 xv = (struct ocfs2_xattr_value_root *)
987 (xs->base + le16_to_cpu(
988 xs->here->xe_name_offset) +
989 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
990 ret = ocfs2_xattr_get_value_outside(inode, xv,
1002 static int ocfs2_xattr_block_get(struct inode *inode,
1007 struct ocfs2_xattr_search *xs)
1009 struct ocfs2_xattr_block *xb;
1010 struct ocfs2_xattr_value_root *xv;
1012 int ret = -ENODATA, name_offset, name_len, block_off, i;
1014 xs->bucket = ocfs2_xattr_bucket_new(inode);
1021 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1027 if (xs->not_found) {
1032 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1033 size = le64_to_cpu(xs->here->xe_value_size);
1036 if (size > buffer_size)
1039 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1040 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1041 i = xs->here - xs->header->xh_entries;
1043 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1044 ret = ocfs2_xattr_bucket_get_name_value(inode,
1045 bucket_xh(xs->bucket),
1049 xs->base = bucket_block(xs->bucket, block_off);
1051 if (ocfs2_xattr_is_local(xs->here)) {
1052 memcpy(buffer, (void *)xs->base +
1053 name_offset + name_len, size);
1055 xv = (struct ocfs2_xattr_value_root *)
1056 (xs->base + name_offset + name_len);
1057 ret = ocfs2_xattr_get_value_outside(inode, xv,
1067 ocfs2_xattr_bucket_free(xs->bucket);
1069 brelse(xs->xattr_bh);
1070 xs->xattr_bh = NULL;
1074 int ocfs2_xattr_get_nolock(struct inode *inode,
1075 struct buffer_head *di_bh,
1082 struct ocfs2_dinode *di = NULL;
1083 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1084 struct ocfs2_xattr_search xis = {
1085 .not_found = -ENODATA,
1087 struct ocfs2_xattr_search xbs = {
1088 .not_found = -ENODATA,
1091 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1094 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1097 xis.inode_bh = xbs.inode_bh = di_bh;
1098 di = (struct ocfs2_dinode *)di_bh->b_data;
1100 down_read(&oi->ip_xattr_sem);
1101 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1103 if (ret == -ENODATA && di->i_xattr_loc)
1104 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1106 up_read(&oi->ip_xattr_sem);
1111 /* ocfs2_xattr_get()
1113 * Copy an extended attribute into the buffer provided.
1114 * Buffer is NULL to compute the size of buffer required.
1116 static int ocfs2_xattr_get(struct inode *inode,
1123 struct buffer_head *di_bh = NULL;
1125 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1130 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1131 name, buffer, buffer_size);
1133 ocfs2_inode_unlock(inode, 0);
1140 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1142 struct ocfs2_xattr_value_root *xv,
1146 int ret = 0, i, cp_len, credits;
1147 u16 blocksize = inode->i_sb->s_blocksize;
1148 u32 p_cluster, num_clusters;
1149 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1150 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1152 struct buffer_head *bh = NULL;
1154 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1157 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1158 * so we don't need to worry about whether ocfs2_extend_trans
1159 * will create a new transactio for us or not.
1161 credits = clusters * bpc;
1162 ret = ocfs2_extend_trans(handle, credits);
1168 while (cpos < clusters) {
1169 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1170 &num_clusters, &xv->xr_list);
1176 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1178 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1179 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
1185 ret = ocfs2_journal_access(handle,
1188 OCFS2_JOURNAL_ACCESS_WRITE);
1194 cp_len = value_len > blocksize ? blocksize : value_len;
1195 memcpy(bh->b_data, value, cp_len);
1196 value_len -= cp_len;
1198 if (cp_len < blocksize)
1199 memset(bh->b_data + cp_len, 0,
1200 blocksize - cp_len);
1202 ret = ocfs2_journal_dirty(handle, bh);
1211 * XXX: do we need to empty all the following
1212 * blocks in this cluster?
1217 cpos += num_clusters;
1225 static int ocfs2_xattr_cleanup(struct inode *inode,
1227 struct ocfs2_xattr_info *xi,
1228 struct ocfs2_xattr_search *xs,
1232 size_t name_len = strlen(xi->name);
1233 void *val = xs->base + offs;
1234 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1236 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1237 OCFS2_JOURNAL_ACCESS_WRITE);
1242 /* Decrease xattr count */
1243 le16_add_cpu(&xs->header->xh_count, -1);
1244 /* Remove the xattr entry and tree root which has already be set*/
1245 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1246 memset(val, 0, size);
1248 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1255 static int ocfs2_xattr_update_entry(struct inode *inode,
1257 struct ocfs2_xattr_info *xi,
1258 struct ocfs2_xattr_search *xs,
1263 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1264 OCFS2_JOURNAL_ACCESS_WRITE);
1270 xs->here->xe_name_offset = cpu_to_le16(offs);
1271 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1272 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1273 ocfs2_xattr_set_local(xs->here, 1);
1275 ocfs2_xattr_set_local(xs->here, 0);
1276 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1278 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1286 * ocfs2_xattr_set_value_outside()
1288 * Set large size value in B tree.
1290 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1291 struct ocfs2_xattr_info *xi,
1292 struct ocfs2_xattr_search *xs,
1293 struct ocfs2_xattr_set_ctxt *ctxt,
1296 size_t name_len = strlen(xi->name);
1297 void *val = xs->base + offs;
1298 struct ocfs2_xattr_value_root *xv = NULL;
1299 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1302 memset(val, 0, size);
1303 memcpy(val, xi->name, name_len);
1304 xv = (struct ocfs2_xattr_value_root *)
1305 (val + OCFS2_XATTR_SIZE(name_len));
1306 xv->xr_clusters = 0;
1307 xv->xr_last_eb_blk = 0;
1308 xv->xr_list.l_tree_depth = 0;
1309 xv->xr_list.l_count = cpu_to_le16(1);
1310 xv->xr_list.l_next_free_rec = 0;
1312 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1313 xi->value_len, ctxt);
1318 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
1323 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1324 xi->value, xi->value_len);
1332 * ocfs2_xattr_set_entry_local()
1334 * Set, replace or remove extended attribute in local.
1336 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1337 struct ocfs2_xattr_info *xi,
1338 struct ocfs2_xattr_search *xs,
1339 struct ocfs2_xattr_entry *last,
1342 size_t name_len = strlen(xi->name);
1345 if (xi->value && xs->not_found) {
1346 /* Insert the new xattr entry. */
1347 le16_add_cpu(&xs->header->xh_count, 1);
1348 ocfs2_xattr_set_type(last, xi->name_index);
1349 ocfs2_xattr_set_local(last, 1);
1350 last->xe_name_len = name_len;
1356 first_val = xs->base + min_offs;
1357 offs = le16_to_cpu(xs->here->xe_name_offset);
1358 val = xs->base + offs;
1360 if (le64_to_cpu(xs->here->xe_value_size) >
1361 OCFS2_XATTR_INLINE_SIZE)
1362 size = OCFS2_XATTR_SIZE(name_len) +
1363 OCFS2_XATTR_ROOT_SIZE;
1365 size = OCFS2_XATTR_SIZE(name_len) +
1366 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1368 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1369 OCFS2_XATTR_SIZE(xi->value_len)) {
1370 /* The old and the new value have the
1371 same size. Just replace the value. */
1372 ocfs2_xattr_set_local(xs->here, 1);
1373 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1374 /* Clear value bytes. */
1375 memset(val + OCFS2_XATTR_SIZE(name_len),
1377 OCFS2_XATTR_SIZE(xi->value_len));
1378 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1383 /* Remove the old name+value. */
1384 memmove(first_val + size, first_val, val - first_val);
1385 memset(first_val, 0, size);
1386 xs->here->xe_name_hash = 0;
1387 xs->here->xe_name_offset = 0;
1388 ocfs2_xattr_set_local(xs->here, 1);
1389 xs->here->xe_value_size = 0;
1393 /* Adjust all value offsets. */
1394 last = xs->header->xh_entries;
1395 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1396 size_t o = le16_to_cpu(last->xe_name_offset);
1399 last->xe_name_offset = cpu_to_le16(o + size);
1404 /* Remove the old entry. */
1406 memmove(xs->here, xs->here + 1,
1407 (void *)last - (void *)xs->here);
1408 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1409 le16_add_cpu(&xs->header->xh_count, -1);
1413 /* Insert the new name+value. */
1414 size_t size = OCFS2_XATTR_SIZE(name_len) +
1415 OCFS2_XATTR_SIZE(xi->value_len);
1416 void *val = xs->base + min_offs - size;
1418 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1419 memset(val, 0, size);
1420 memcpy(val, xi->name, name_len);
1421 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1424 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1425 ocfs2_xattr_set_local(xs->here, 1);
1426 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1433 * ocfs2_xattr_set_entry()
1435 * Set extended attribute entry into inode or block.
1437 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1438 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1439 * then set value in B tree with set_value_outside().
1441 static int ocfs2_xattr_set_entry(struct inode *inode,
1442 struct ocfs2_xattr_info *xi,
1443 struct ocfs2_xattr_search *xs,
1444 struct ocfs2_xattr_set_ctxt *ctxt,
1447 struct ocfs2_xattr_entry *last;
1448 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1449 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1450 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1452 handle_t *handle = ctxt->handle;
1454 struct ocfs2_xattr_info xi_l = {
1455 .name_index = xi->name_index,
1458 .value_len = xi->value_len,
1461 /* Compute min_offs, last and free space. */
1462 last = xs->header->xh_entries;
1464 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1465 size_t offs = le16_to_cpu(last->xe_name_offset);
1466 if (offs < min_offs)
1471 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1475 if (!xs->not_found) {
1477 if (ocfs2_xattr_is_local(xs->here))
1478 size = OCFS2_XATTR_SIZE(name_len) +
1479 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1481 size = OCFS2_XATTR_SIZE(name_len) +
1482 OCFS2_XATTR_ROOT_SIZE;
1483 free += (size + sizeof(struct ocfs2_xattr_entry));
1485 /* Check free space in inode or block */
1486 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1487 if (free < sizeof(struct ocfs2_xattr_entry) +
1488 OCFS2_XATTR_SIZE(name_len) +
1489 OCFS2_XATTR_ROOT_SIZE) {
1493 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1494 xi_l.value = (void *)&def_xv;
1495 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1496 } else if (xi->value) {
1497 if (free < sizeof(struct ocfs2_xattr_entry) +
1498 OCFS2_XATTR_SIZE(name_len) +
1499 OCFS2_XATTR_SIZE(xi->value_len)) {
1505 if (!xs->not_found) {
1506 /* For existing extended attribute */
1507 size_t size = OCFS2_XATTR_SIZE(name_len) +
1508 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1509 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1510 void *val = xs->base + offs;
1512 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1513 /* Replace existing local xattr with tree root */
1514 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1519 } else if (!ocfs2_xattr_is_local(xs->here)) {
1520 /* For existing xattr which has value outside */
1521 struct ocfs2_xattr_value_root *xv = NULL;
1522 xv = (struct ocfs2_xattr_value_root *)(val +
1523 OCFS2_XATTR_SIZE(name_len));
1525 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1527 * If new value need set outside also,
1528 * first truncate old value to new value,
1529 * then set new value with set_value_outside().
1531 ret = ocfs2_xattr_value_truncate(inode,
1541 ret = ocfs2_xattr_update_entry(inode,
1551 ret = __ocfs2_xattr_set_value_outside(inode,
1561 * If new value need set in local,
1562 * just trucate old value to zero.
1564 ret = ocfs2_xattr_value_truncate(inode,
1575 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1576 OCFS2_JOURNAL_ACCESS_WRITE);
1582 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1583 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1584 OCFS2_JOURNAL_ACCESS_WRITE);
1592 * Set value in local, include set tree root in local.
1593 * This is the first step for value size >INLINE_SIZE.
1595 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1597 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1598 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1605 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1606 (flag & OCFS2_INLINE_XATTR_FL)) {
1607 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1608 unsigned int xattrsize = osb->s_xattr_inline_size;
1611 * Adjust extent record count or inline data size
1612 * to reserve space for extended attribute.
1614 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1615 struct ocfs2_inline_data *idata = &di->id2.i_data;
1616 le16_add_cpu(&idata->id_count, -xattrsize);
1617 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1618 struct ocfs2_extent_list *el = &di->id2.i_list;
1619 le16_add_cpu(&el->l_count, -(xattrsize /
1620 sizeof(struct ocfs2_extent_rec)));
1622 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1624 /* Update xattr flag */
1625 spin_lock(&oi->ip_lock);
1626 oi->ip_dyn_features |= flag;
1627 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1628 spin_unlock(&oi->ip_lock);
1629 /* Update inode ctime */
1630 inode->i_ctime = CURRENT_TIME;
1631 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1632 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1634 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1638 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1640 * Set value outside in B tree.
1641 * This is the second step for value size > INLINE_SIZE.
1643 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1644 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
1650 * If set value outside failed, we have to clean
1651 * the junk tree root we have already set in local.
1653 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1663 static int ocfs2_remove_value_outside(struct inode*inode,
1664 struct buffer_head *bh,
1665 struct ocfs2_xattr_header *header)
1668 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1669 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1671 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1673 ctxt.handle = ocfs2_start_trans(osb,
1674 ocfs2_remove_extent_credits(osb->sb));
1675 if (IS_ERR(ctxt.handle)) {
1676 ret = PTR_ERR(ctxt.handle);
1681 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1682 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1684 if (!ocfs2_xattr_is_local(entry)) {
1685 struct ocfs2_xattr_value_root *xv;
1688 val = (void *)header +
1689 le16_to_cpu(entry->xe_name_offset);
1690 xv = (struct ocfs2_xattr_value_root *)
1691 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1692 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1701 ocfs2_commit_trans(osb, ctxt.handle);
1702 ocfs2_schedule_truncate_log_flush(osb, 1);
1703 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1708 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1709 struct buffer_head *di_bh)
1712 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1713 struct ocfs2_xattr_header *header;
1716 header = (struct ocfs2_xattr_header *)
1717 ((void *)di + inode->i_sb->s_blocksize -
1718 le16_to_cpu(di->i_xattr_inline_size));
1720 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1725 static int ocfs2_xattr_block_remove(struct inode *inode,
1726 struct buffer_head *blk_bh)
1728 struct ocfs2_xattr_block *xb;
1731 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1732 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1733 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1734 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1736 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1741 static int ocfs2_xattr_free_block(struct inode *inode,
1744 struct inode *xb_alloc_inode;
1745 struct buffer_head *xb_alloc_bh = NULL;
1746 struct buffer_head *blk_bh = NULL;
1747 struct ocfs2_xattr_block *xb;
1748 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1754 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
1760 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1766 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1767 blk = le64_to_cpu(xb->xb_blkno);
1768 bit = le16_to_cpu(xb->xb_suballoc_bit);
1769 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1771 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1772 EXTENT_ALLOC_SYSTEM_INODE,
1773 le16_to_cpu(xb->xb_suballoc_slot));
1774 if (!xb_alloc_inode) {
1779 mutex_lock(&xb_alloc_inode->i_mutex);
1781 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1787 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1788 if (IS_ERR(handle)) {
1789 ret = PTR_ERR(handle);
1794 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1799 ocfs2_commit_trans(osb, handle);
1801 ocfs2_inode_unlock(xb_alloc_inode, 1);
1802 brelse(xb_alloc_bh);
1804 mutex_unlock(&xb_alloc_inode->i_mutex);
1805 iput(xb_alloc_inode);
1812 * ocfs2_xattr_remove()
1814 * Free extended attribute resources associated with this inode.
1816 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1818 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1819 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1823 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1826 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1829 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1830 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1837 if (di->i_xattr_loc) {
1838 ret = ocfs2_xattr_free_block(inode,
1839 le64_to_cpu(di->i_xattr_loc));
1846 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1847 OCFS2_INODE_UPDATE_CREDITS);
1848 if (IS_ERR(handle)) {
1849 ret = PTR_ERR(handle);
1853 ret = ocfs2_journal_access(handle, inode, di_bh,
1854 OCFS2_JOURNAL_ACCESS_WRITE);
1860 di->i_xattr_loc = 0;
1862 spin_lock(&oi->ip_lock);
1863 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1864 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1865 spin_unlock(&oi->ip_lock);
1867 ret = ocfs2_journal_dirty(handle, di_bh);
1871 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1876 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1877 struct ocfs2_dinode *di)
1879 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1880 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1883 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1886 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1887 struct ocfs2_inline_data *idata = &di->id2.i_data;
1888 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1889 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1890 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1891 le64_to_cpu(di->i_size);
1893 struct ocfs2_extent_list *el = &di->id2.i_list;
1894 free = (le16_to_cpu(el->l_count) -
1895 le16_to_cpu(el->l_next_free_rec)) *
1896 sizeof(struct ocfs2_extent_rec);
1898 if (free >= xattrsize)
1905 * ocfs2_xattr_ibody_find()
1907 * Find extended attribute in inode block and
1908 * fill search info into struct ocfs2_xattr_search.
1910 static int ocfs2_xattr_ibody_find(struct inode *inode,
1913 struct ocfs2_xattr_search *xs)
1915 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1916 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1920 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1923 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1924 down_read(&oi->ip_alloc_sem);
1925 has_space = ocfs2_xattr_has_space_inline(inode, di);
1926 up_read(&oi->ip_alloc_sem);
1931 xs->xattr_bh = xs->inode_bh;
1932 xs->end = (void *)di + inode->i_sb->s_blocksize;
1933 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1934 xs->header = (struct ocfs2_xattr_header *)
1935 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1937 xs->header = (struct ocfs2_xattr_header *)
1938 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1939 xs->base = (void *)xs->header;
1940 xs->here = xs->header->xh_entries;
1942 /* Find the named attribute. */
1943 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1944 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1945 if (ret && ret != -ENODATA)
1947 xs->not_found = ret;
1954 * ocfs2_xattr_ibody_set()
1956 * Set, replace or remove an extended attribute into inode block.
1959 static int ocfs2_xattr_ibody_set(struct inode *inode,
1960 struct ocfs2_xattr_info *xi,
1961 struct ocfs2_xattr_search *xs,
1962 struct ocfs2_xattr_set_ctxt *ctxt)
1964 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1965 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1968 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1971 down_write(&oi->ip_alloc_sem);
1972 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1973 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1979 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
1980 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1982 up_write(&oi->ip_alloc_sem);
1988 * ocfs2_xattr_block_find()
1990 * Find extended attribute in external block and
1991 * fill search info into struct ocfs2_xattr_search.
1993 static int ocfs2_xattr_block_find(struct inode *inode,
1996 struct ocfs2_xattr_search *xs)
1998 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1999 struct buffer_head *blk_bh = NULL;
2000 struct ocfs2_xattr_block *xb;
2003 if (!di->i_xattr_loc)
2006 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2013 xs->xattr_bh = blk_bh;
2014 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2016 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2017 xs->header = &xb->xb_attrs.xb_header;
2018 xs->base = (void *)xs->header;
2019 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2020 xs->here = xs->header->xh_entries;
2022 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2024 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2028 if (ret && ret != -ENODATA) {
2029 xs->xattr_bh = NULL;
2032 xs->not_found = ret;
2041 * ocfs2_xattr_block_set()
2043 * Set, replace or remove an extended attribute into external block.
2046 static int ocfs2_xattr_block_set(struct inode *inode,
2047 struct ocfs2_xattr_info *xi,
2048 struct ocfs2_xattr_search *xs,
2049 struct ocfs2_xattr_set_ctxt *ctxt)
2051 struct buffer_head *new_bh = NULL;
2052 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2053 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2054 handle_t *handle = ctxt->handle;
2055 struct ocfs2_xattr_block *xblk = NULL;
2056 u16 suballoc_bit_start;
2061 if (!xs->xattr_bh) {
2062 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
2063 OCFS2_JOURNAL_ACCESS_CREATE);
2069 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
2070 &suballoc_bit_start, &num_got,
2077 new_bh = sb_getblk(inode->i_sb, first_blkno);
2078 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2080 ret = ocfs2_journal_access(handle, inode, new_bh,
2081 OCFS2_JOURNAL_ACCESS_CREATE);
2087 /* Initialize ocfs2_xattr_block */
2088 xs->xattr_bh = new_bh;
2089 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2090 memset(xblk, 0, inode->i_sb->s_blocksize);
2091 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2092 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2093 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2094 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2095 xblk->xb_blkno = cpu_to_le64(first_blkno);
2097 xs->header = &xblk->xb_attrs.xb_header;
2098 xs->base = (void *)xs->header;
2099 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2100 xs->here = xs->header->xh_entries;
2102 ret = ocfs2_journal_dirty(handle, new_bh);
2107 di->i_xattr_loc = cpu_to_le64(first_blkno);
2108 ocfs2_journal_dirty(handle, xs->inode_bh);
2110 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2112 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2113 /* Set extended attribute into external block */
2114 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2115 OCFS2_HAS_XATTR_FL);
2116 if (!ret || ret != -ENOSPC)
2119 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2124 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2131 /* Check whether the new xattr can be inserted into the inode. */
2132 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2133 struct ocfs2_xattr_info *xi,
2134 struct ocfs2_xattr_search *xs)
2137 struct ocfs2_xattr_entry *last;
2139 size_t min_offs = xs->end - xs->base;
2144 last = xs->header->xh_entries;
2146 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2147 size_t offs = le16_to_cpu(last->xe_name_offset);
2148 if (offs < min_offs)
2153 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
2157 BUG_ON(!xs->not_found);
2159 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2160 value_size = OCFS2_XATTR_ROOT_SIZE;
2162 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2164 if (free >= sizeof(struct ocfs2_xattr_entry) +
2165 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2171 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2172 struct ocfs2_dinode *di,
2173 struct ocfs2_xattr_info *xi,
2174 struct ocfs2_xattr_search *xis,
2175 struct ocfs2_xattr_search *xbs,
2180 int ret = 0, old_in_xb = 0;
2181 int clusters_add = 0, meta_add = 0, credits = 0;
2182 struct buffer_head *bh = NULL;
2183 struct ocfs2_xattr_block *xb = NULL;
2184 struct ocfs2_xattr_entry *xe = NULL;
2185 struct ocfs2_xattr_value_root *xv = NULL;
2187 int name_offset, name_len = 0;
2188 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2192 if (xis->not_found && xbs->not_found) {
2193 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2195 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2196 clusters_add += new_clusters;
2197 credits += ocfs2_calc_extend_credits(inode->i_sb,
2205 if (!xis->not_found) {
2207 name_offset = le16_to_cpu(xe->xe_name_offset);
2208 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2210 credits += OCFS2_INODE_UPDATE_CREDITS;
2212 int i, block_off = 0;
2213 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2215 name_offset = le16_to_cpu(xe->xe_name_offset);
2216 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2217 i = xbs->here - xbs->header->xh_entries;
2220 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2221 ret = ocfs2_xattr_bucket_get_name_value(inode,
2222 bucket_xh(xbs->bucket),
2225 base = bucket_block(xbs->bucket, block_off);
2226 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2229 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2234 * delete a xattr doesn't need metadata and cluster allocation.
2235 * so just calculate the credits and return.
2237 * The credits for removing the value tree will be extended
2238 * by ocfs2_remove_extent itself.
2241 if (!ocfs2_xattr_is_local(xe))
2242 credits += ocfs2_remove_extent_credits(inode->i_sb);
2247 /* do cluster allocation guess first. */
2248 value_size = le64_to_cpu(xe->xe_value_size);
2252 * In xattr set, we always try to set the xe in inode first,
2253 * so if it can be inserted into inode successfully, the old
2254 * one will be removed from the xattr block, and this xattr
2255 * will be inserted into inode as a new xattr in inode.
2257 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2258 clusters_add += new_clusters;
2259 credits += ocfs2_remove_extent_credits(inode->i_sb) +
2260 OCFS2_INODE_UPDATE_CREDITS;
2261 if (!ocfs2_xattr_is_local(xe))
2262 credits += ocfs2_calc_extend_credits(
2270 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2271 /* the new values will be stored outside. */
2272 u32 old_clusters = 0;
2274 if (!ocfs2_xattr_is_local(xe)) {
2275 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2277 xv = (struct ocfs2_xattr_value_root *)
2278 (base + name_offset + name_len);
2279 value_size = OCFS2_XATTR_ROOT_SIZE;
2283 if (old_clusters >= new_clusters) {
2284 credits += ocfs2_remove_extent_credits(inode->i_sb);
2287 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2288 clusters_add += new_clusters - old_clusters;
2289 credits += ocfs2_calc_extend_credits(inode->i_sb,
2293 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2298 * Now the new value will be stored inside. So if the new
2299 * value is smaller than the size of value root or the old
2300 * value, we don't need any allocation, otherwise we have
2301 * to guess metadata allocation.
2303 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2304 (!ocfs2_xattr_is_local(xe) &&
2305 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2310 /* calculate metadata allocation. */
2311 if (di->i_xattr_loc) {
2312 if (!xbs->xattr_bh) {
2313 ret = ocfs2_read_xattr_block(inode,
2314 le64_to_cpu(di->i_xattr_loc),
2321 xb = (struct ocfs2_xattr_block *)bh->b_data;
2323 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2325 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2326 struct ocfs2_extent_list *el =
2327 &xb->xb_attrs.xb_root.xt_list;
2328 meta_add += ocfs2_extend_meta_needed(el);
2329 credits += ocfs2_calc_extend_credits(inode->i_sb,
2334 * This cluster will be used either for new bucket or for
2336 * If the cluster size is the same as the bucket size, one
2337 * more is needed since we may need to extend the bucket
2341 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2342 if (OCFS2_XATTR_BUCKET_SIZE ==
2343 OCFS2_SB(inode->i_sb)->s_clustersize) {
2344 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2349 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2353 *clusters_need = clusters_add;
2355 *meta_need = meta_add;
2357 *credits_need = credits;
2362 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2363 struct ocfs2_dinode *di,
2364 struct ocfs2_xattr_info *xi,
2365 struct ocfs2_xattr_search *xis,
2366 struct ocfs2_xattr_search *xbs,
2367 struct ocfs2_xattr_set_ctxt *ctxt,
2370 int clusters_add, meta_add, ret;
2371 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2373 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2375 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2377 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2378 &clusters_add, &meta_add, credits);
2384 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2385 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2388 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2397 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2403 if (ctxt->meta_ac) {
2404 ocfs2_free_alloc_context(ctxt->meta_ac);
2405 ctxt->meta_ac = NULL;
2409 * We cannot have an error and a non null ctxt->data_ac.
2416 static int __ocfs2_xattr_set_handle(struct inode *inode,
2417 struct ocfs2_dinode *di,
2418 struct ocfs2_xattr_info *xi,
2419 struct ocfs2_xattr_search *xis,
2420 struct ocfs2_xattr_search *xbs,
2421 struct ocfs2_xattr_set_ctxt *ctxt)
2423 int ret = 0, credits, old_found;
2426 /* Remove existing extended attribute */
2427 if (!xis->not_found)
2428 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2429 else if (!xbs->not_found)
2430 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2432 /* We always try to set extended attribute into inode first*/
2433 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2434 if (!ret && !xbs->not_found) {
2436 * If succeed and that extended attribute existing in
2437 * external block, then we will remove it.
2442 old_found = xis->not_found;
2443 xis->not_found = -ENODATA;
2444 ret = ocfs2_calc_xattr_set_need(inode,
2452 xis->not_found = old_found;
2458 ret = ocfs2_extend_trans(ctxt->handle, credits +
2459 ctxt->handle->h_buffer_credits);
2464 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2465 } else if (ret == -ENOSPC) {
2466 if (di->i_xattr_loc && !xbs->xattr_bh) {
2467 ret = ocfs2_xattr_block_find(inode,
2473 old_found = xis->not_found;
2474 xis->not_found = -ENODATA;
2475 ret = ocfs2_calc_xattr_set_need(inode,
2483 xis->not_found = old_found;
2489 ret = ocfs2_extend_trans(ctxt->handle, credits +
2490 ctxt->handle->h_buffer_credits);
2497 * If no space in inode, we will set extended attribute
2498 * into external block.
2500 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2503 if (!xis->not_found) {
2505 * If succeed and that extended attribute
2506 * existing in inode, we will remove it.
2510 xbs->not_found = -ENODATA;
2511 ret = ocfs2_calc_xattr_set_need(inode,
2524 ret = ocfs2_extend_trans(ctxt->handle, credits +
2525 ctxt->handle->h_buffer_credits);
2530 ret = ocfs2_xattr_ibody_set(inode, xi,
2541 * This function only called duing creating inode
2542 * for init security/acl xattrs of the new inode.
2543 * The xattrs could be put into ibody or extent block,
2544 * xattr bucket would not be use in this case.
2545 * transanction credits also be reserved in here.
2547 int ocfs2_xattr_set_handle(handle_t *handle,
2548 struct inode *inode,
2549 struct buffer_head *di_bh,
2555 struct ocfs2_alloc_context *meta_ac,
2556 struct ocfs2_alloc_context *data_ac)
2558 struct ocfs2_dinode *di;
2561 struct ocfs2_xattr_info xi = {
2562 .name_index = name_index,
2565 .value_len = value_len,
2568 struct ocfs2_xattr_search xis = {
2569 .not_found = -ENODATA,
2572 struct ocfs2_xattr_search xbs = {
2573 .not_found = -ENODATA,
2576 struct ocfs2_xattr_set_ctxt ctxt = {
2582 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2585 xis.inode_bh = xbs.inode_bh = di_bh;
2586 di = (struct ocfs2_dinode *)di_bh->b_data;
2588 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2590 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2593 if (xis.not_found) {
2594 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2599 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2602 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2603 brelse(xbs.xattr_bh);
2611 * Set, replace or remove an extended attribute for this inode.
2612 * value is NULL to remove an existing extended attribute, else either
2613 * create or replace an extended attribute.
2615 int ocfs2_xattr_set(struct inode *inode,
2622 struct buffer_head *di_bh = NULL;
2623 struct ocfs2_dinode *di;
2625 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2626 struct inode *tl_inode = osb->osb_tl_inode;
2627 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2629 struct ocfs2_xattr_info xi = {
2630 .name_index = name_index,
2633 .value_len = value_len,
2636 struct ocfs2_xattr_search xis = {
2637 .not_found = -ENODATA,
2640 struct ocfs2_xattr_search xbs = {
2641 .not_found = -ENODATA,
2644 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2648 * Only xbs will be used on indexed trees. xis doesn't need a
2651 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2653 mlog_errno(-ENOMEM);
2657 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2660 goto cleanup_nolock;
2662 xis.inode_bh = xbs.inode_bh = di_bh;
2663 di = (struct ocfs2_dinode *)di_bh->b_data;
2665 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2667 * Scan inode and external block to find the same name
2668 * extended attribute and collect search infomation.
2670 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2673 if (xis.not_found) {
2674 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2679 if (xis.not_found && xbs.not_found) {
2681 if (flags & XATTR_REPLACE)
2688 if (flags & XATTR_CREATE)
2693 mutex_lock(&tl_inode->i_mutex);
2695 if (ocfs2_truncate_log_needs_flush(osb)) {
2696 ret = __ocfs2_flush_truncate_log(osb);
2698 mutex_unlock(&tl_inode->i_mutex);
2703 mutex_unlock(&tl_inode->i_mutex);
2705 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2706 &xbs, &ctxt, &credits);
2712 ctxt.handle = ocfs2_start_trans(osb, credits);
2713 if (IS_ERR(ctxt.handle)) {
2714 ret = PTR_ERR(ctxt.handle);
2719 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2721 ocfs2_commit_trans(osb, ctxt.handle);
2724 ocfs2_free_alloc_context(ctxt.data_ac);
2726 ocfs2_free_alloc_context(ctxt.meta_ac);
2727 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2728 ocfs2_schedule_truncate_log_flush(osb, 1);
2729 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2731 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2732 ocfs2_inode_unlock(inode, 1);
2735 brelse(xbs.xattr_bh);
2736 ocfs2_xattr_bucket_free(xbs.bucket);
2742 * Find the xattr extent rec which may contains name_hash.
2743 * e_cpos will be the first name hash of the xattr rec.
2744 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2746 static int ocfs2_xattr_get_rec(struct inode *inode,
2751 struct ocfs2_extent_list *el)
2754 struct buffer_head *eb_bh = NULL;
2755 struct ocfs2_extent_block *eb;
2756 struct ocfs2_extent_rec *rec = NULL;
2759 if (el->l_tree_depth) {
2760 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2766 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2769 if (el->l_tree_depth) {
2770 ocfs2_error(inode->i_sb,
2771 "Inode %lu has non zero tree depth in "
2772 "xattr tree block %llu\n", inode->i_ino,
2773 (unsigned long long)eb_bh->b_blocknr);
2779 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2780 rec = &el->l_recs[i];
2782 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2783 e_blkno = le64_to_cpu(rec->e_blkno);
2789 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2790 "record (%u, %u, 0) in xattr", inode->i_ino,
2791 le32_to_cpu(rec->e_cpos),
2792 ocfs2_rec_clusters(el, rec));
2797 *p_blkno = le64_to_cpu(rec->e_blkno);
2798 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2800 *e_cpos = le32_to_cpu(rec->e_cpos);
2806 typedef int (xattr_bucket_func)(struct inode *inode,
2807 struct ocfs2_xattr_bucket *bucket,
2810 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2811 struct ocfs2_xattr_bucket *bucket,
2818 int i, ret = 0, cmp = 1, block_off, new_offset;
2819 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
2820 size_t name_len = strlen(name);
2821 struct ocfs2_xattr_entry *xe = NULL;
2825 * We don't use binary search in the bucket because there
2826 * may be multiple entries with the same name hash.
2828 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2829 xe = &xh->xh_entries[i];
2831 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2833 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2836 cmp = name_index - ocfs2_xattr_get_type(xe);
2838 cmp = name_len - xe->xe_name_len;
2842 ret = ocfs2_xattr_bucket_get_name_value(inode,
2853 xe_name = bucket_block(bucket, block_off) + new_offset;
2854 if (!memcmp(name, xe_name, name_len)) {
2866 * Find the specified xattr entry in a series of buckets.
2867 * This series start from p_blkno and last for num_clusters.
2868 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2869 * the num of the valid buckets.
2871 * Return the buffer_head this xattr should reside in. And if the xattr's
2872 * hash is in the gap of 2 buckets, return the lower bucket.
2874 static int ocfs2_xattr_bucket_find(struct inode *inode,
2881 struct ocfs2_xattr_search *xs)
2884 struct ocfs2_xattr_header *xh = NULL;
2885 struct ocfs2_xattr_entry *xe = NULL;
2887 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2888 int low_bucket = 0, bucket, high_bucket;
2889 struct ocfs2_xattr_bucket *search;
2891 u64 blkno, lower_blkno = 0;
2893 search = ocfs2_xattr_bucket_new(inode);
2900 ret = ocfs2_read_xattr_bucket(search, p_blkno);
2906 xh = bucket_xh(search);
2907 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2908 while (low_bucket <= high_bucket) {
2909 ocfs2_xattr_bucket_relse(search);
2911 bucket = (low_bucket + high_bucket) / 2;
2912 blkno = p_blkno + bucket * blk_per_bucket;
2913 ret = ocfs2_read_xattr_bucket(search, blkno);
2919 xh = bucket_xh(search);
2920 xe = &xh->xh_entries[0];
2921 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2922 high_bucket = bucket - 1;
2927 * Check whether the hash of the last entry in our
2928 * bucket is larger than the search one. for an empty
2929 * bucket, the last one is also the first one.
2932 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2934 last_hash = le32_to_cpu(xe->xe_name_hash);
2936 /* record lower_blkno which may be the insert place. */
2937 lower_blkno = blkno;
2939 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2940 low_bucket = bucket + 1;
2944 /* the searched xattr should reside in this bucket if exists. */
2945 ret = ocfs2_find_xe_in_bucket(inode, search,
2946 name_index, name, name_hash,
2956 * Record the bucket we have found.
2957 * When the xattr's hash value is in the gap of 2 buckets, we will
2958 * always set it to the previous bucket.
2961 lower_blkno = p_blkno;
2963 /* This should be in cache - we just read it during the search */
2964 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2970 xs->header = bucket_xh(xs->bucket);
2971 xs->base = bucket_block(xs->bucket, 0);
2972 xs->end = xs->base + inode->i_sb->s_blocksize;
2975 xs->here = &xs->header->xh_entries[index];
2976 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2977 (unsigned long long)bucket_blkno(xs->bucket), index);
2982 ocfs2_xattr_bucket_free(search);
2986 static int ocfs2_xattr_index_block_find(struct inode *inode,
2987 struct buffer_head *root_bh,
2990 struct ocfs2_xattr_search *xs)
2993 struct ocfs2_xattr_block *xb =
2994 (struct ocfs2_xattr_block *)root_bh->b_data;
2995 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2996 struct ocfs2_extent_list *el = &xb_root->xt_list;
2998 u32 first_hash, num_clusters = 0;
2999 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3001 if (le16_to_cpu(el->l_next_free_rec) == 0)
3004 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3005 name, name_hash, name_index);
3007 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3014 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3016 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3017 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3020 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3021 p_blkno, first_hash, num_clusters, xs);
3027 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3030 xattr_bucket_func *func,
3034 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3035 u32 num_buckets = clusters * bpc;
3036 struct ocfs2_xattr_bucket *bucket;
3038 bucket = ocfs2_xattr_bucket_new(inode);
3040 mlog_errno(-ENOMEM);
3044 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3045 clusters, (unsigned long long)blkno);
3047 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3048 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3055 * The real bucket num in this series of blocks is stored
3056 * in the 1st bucket.
3059 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3061 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3062 (unsigned long long)blkno,
3063 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3065 ret = func(inode, bucket, para);
3068 /* Fall through to bucket_relse() */
3071 ocfs2_xattr_bucket_relse(bucket);
3076 ocfs2_xattr_bucket_free(bucket);
3080 struct ocfs2_xattr_tree_list {
3086 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
3087 struct ocfs2_xattr_header *xh,
3094 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3097 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3099 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
3100 *new_offset = name_offset % inode->i_sb->s_blocksize;
3105 static int ocfs2_list_xattr_bucket(struct inode *inode,
3106 struct ocfs2_xattr_bucket *bucket,
3110 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3111 int i, block_off, new_offset;
3112 const char *prefix, *name;
3114 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3115 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3116 type = ocfs2_xattr_get_type(entry);
3117 prefix = ocfs2_xattr_prefix(type);
3120 ret = ocfs2_xattr_bucket_get_name_value(inode,
3128 name = (const char *)bucket_block(bucket, block_off) +
3130 ret = ocfs2_xattr_list_entry(xl->buffer,
3134 entry->xe_name_len);
3143 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3144 struct ocfs2_xattr_tree_root *xt,
3148 struct ocfs2_extent_list *el = &xt->xt_list;
3150 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3152 struct ocfs2_xattr_tree_list xl = {
3154 .buffer_size = buffer_size,
3158 if (le16_to_cpu(el->l_next_free_rec) == 0)
3161 while (name_hash > 0) {
3162 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3163 &e_cpos, &num_clusters, el);
3169 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3170 ocfs2_list_xattr_bucket,
3180 name_hash = e_cpos - 1;
3188 static int cmp_xe(const void *a, const void *b)
3190 const struct ocfs2_xattr_entry *l = a, *r = b;
3191 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3192 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3194 if (l_hash > r_hash)
3196 if (l_hash < r_hash)
3201 static void swap_xe(void *a, void *b, int size)
3203 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3206 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3207 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3211 * When the ocfs2_xattr_block is filled up, new bucket will be created
3212 * and all the xattr entries will be moved to the new bucket.
3213 * The header goes at the start of the bucket, and the names+values are
3214 * filled from the end. This is why *target starts as the last buffer.
3215 * Note: we need to sort the entries since they are not saved in order
3216 * in the ocfs2_xattr_block.
3218 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3219 struct buffer_head *xb_bh,
3220 struct ocfs2_xattr_bucket *bucket)
3222 int i, blocksize = inode->i_sb->s_blocksize;
3223 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3224 u16 offset, size, off_change;
3225 struct ocfs2_xattr_entry *xe;
3226 struct ocfs2_xattr_block *xb =
3227 (struct ocfs2_xattr_block *)xb_bh->b_data;
3228 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
3229 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3230 u16 count = le16_to_cpu(xb_xh->xh_count);
3231 char *src = xb_bh->b_data;
3232 char *target = bucket_block(bucket, blks - 1);
3234 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3235 (unsigned long long)xb_bh->b_blocknr,
3236 (unsigned long long)bucket_blkno(bucket));
3238 for (i = 0; i < blks; i++)
3239 memset(bucket_block(bucket, i), 0, blocksize);
3242 * Since the xe_name_offset is based on ocfs2_xattr_header,
3243 * there is a offset change corresponding to the change of
3244 * ocfs2_xattr_header's position.
3246 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3247 xe = &xb_xh->xh_entries[count - 1];
3248 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3249 size = blocksize - offset;
3251 /* copy all the names and values. */
3252 memcpy(target + offset, src + offset, size);
3254 /* Init new header now. */
3255 xh->xh_count = xb_xh->xh_count;
3256 xh->xh_num_buckets = cpu_to_le16(1);
3257 xh->xh_name_value_len = cpu_to_le16(size);
3258 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3260 /* copy all the entries. */
3261 target = bucket_block(bucket, 0);
3262 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3263 size = count * sizeof(struct ocfs2_xattr_entry);
3264 memcpy(target + offset, (char *)xb_xh + offset, size);
3266 /* Change the xe offset for all the xe because of the move. */
3267 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3268 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3269 for (i = 0; i < count; i++)
3270 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3272 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3273 offset, size, off_change);
3275 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3280 * After we move xattr from block to index btree, we have to
3281 * update ocfs2_xattr_search to the new xe and base.
3283 * When the entry is in xattr block, xattr_bh indicates the storage place.
3284 * While if the entry is in index b-tree, "bucket" indicates the
3285 * real place of the xattr.
3287 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3288 struct ocfs2_xattr_search *xs,
3289 struct buffer_head *old_bh)
3291 char *buf = old_bh->b_data;
3292 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3293 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
3296 xs->header = bucket_xh(xs->bucket);
3297 xs->base = bucket_block(xs->bucket, 0);
3298 xs->end = xs->base + inode->i_sb->s_blocksize;
3303 i = xs->here - old_xh->xh_entries;
3304 xs->here = &xs->header->xh_entries[i];
3307 static int ocfs2_xattr_create_index_block(struct inode *inode,
3308 struct ocfs2_xattr_search *xs,
3309 struct ocfs2_xattr_set_ctxt *ctxt)
3314 handle_t *handle = ctxt->handle;
3315 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3316 struct ocfs2_inode_info *oi = OCFS2_I(inode);
3317 struct buffer_head *xb_bh = xs->xattr_bh;
3318 struct ocfs2_xattr_block *xb =
3319 (struct ocfs2_xattr_block *)xb_bh->b_data;
3320 struct ocfs2_xattr_tree_root *xr;
3321 u16 xb_flags = le16_to_cpu(xb->xb_flags);
3323 mlog(0, "create xattr index block for %llu\n",
3324 (unsigned long long)xb_bh->b_blocknr);
3326 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
3327 BUG_ON(!xs->bucket);
3331 * We can use this lock for now, and maybe move to a dedicated mutex
3332 * if performance becomes a problem later.
3334 down_write(&oi->ip_alloc_sem);
3336 ret = ocfs2_journal_access(handle, inode, xb_bh,
3337 OCFS2_JOURNAL_ACCESS_WRITE);
3343 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3344 1, 1, &bit_off, &len);
3351 * The bucket may spread in many blocks, and
3352 * we will only touch the 1st block and the last block
3353 * in the whole bucket(one for entry and one for data).
3355 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3357 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3358 (unsigned long long)blkno);
3360 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
3366 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3367 OCFS2_JOURNAL_ACCESS_CREATE);
3373 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3374 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3376 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3378 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3379 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3380 offsetof(struct ocfs2_xattr_block, xb_attrs));
3382 xr = &xb->xb_attrs.xb_root;
3383 xr->xt_clusters = cpu_to_le32(1);
3384 xr->xt_last_eb_blk = 0;
3385 xr->xt_list.l_tree_depth = 0;
3386 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3387 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3389 xr->xt_list.l_recs[0].e_cpos = 0;
3390 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3391 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3393 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3395 ocfs2_journal_dirty(handle, xb_bh);
3398 up_write(&oi->ip_alloc_sem);
3403 static int cmp_xe_offset(const void *a, const void *b)
3405 const struct ocfs2_xattr_entry *l = a, *r = b;
3406 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3407 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3409 if (l_name_offset < r_name_offset)
3411 if (l_name_offset > r_name_offset)
3417 * defrag a xattr bucket if we find that the bucket has some
3418 * holes beteen name/value pairs.
3419 * We will move all the name/value pairs to the end of the bucket
3420 * so that we can spare some space for insertion.
3422 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3424 struct ocfs2_xattr_bucket *bucket)
3427 size_t end, offset, len, value_len;
3428 struct ocfs2_xattr_header *xh;
3429 char *entries, *buf, *bucket_buf = NULL;
3430 u64 blkno = bucket_blkno(bucket);
3432 size_t blocksize = inode->i_sb->s_blocksize;
3433 struct ocfs2_xattr_entry *xe;
3436 * In order to make the operation more efficient and generic,
3437 * we copy all the blocks into a contiguous memory and do the
3438 * defragment there, so if anything is error, we will not touch
3441 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3448 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3449 memcpy(buf, bucket_block(bucket, i), blocksize);
3451 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
3452 OCFS2_JOURNAL_ACCESS_WRITE);
3458 xh = (struct ocfs2_xattr_header *)bucket_buf;
3459 entries = (char *)xh->xh_entries;
3460 xh_free_start = le16_to_cpu(xh->xh_free_start);
3462 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3463 "xh_free_start = %u, xh_name_value_len = %u.\n",
3464 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3465 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
3468 * sort all the entries by their offset.
3469 * the largest will be the first, so that we can
3470 * move them to the end one by one.
3472 sort(entries, le16_to_cpu(xh->xh_count),
3473 sizeof(struct ocfs2_xattr_entry),
3474 cmp_xe_offset, swap_xe);
3476 /* Move all name/values to the end of the bucket. */
3477 xe = xh->xh_entries;
3478 end = OCFS2_XATTR_BUCKET_SIZE;
3479 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3480 offset = le16_to_cpu(xe->xe_name_offset);
3481 if (ocfs2_xattr_is_local(xe))
3482 value_len = OCFS2_XATTR_SIZE(
3483 le64_to_cpu(xe->xe_value_size));
3485 value_len = OCFS2_XATTR_ROOT_SIZE;
3486 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3489 * We must make sure that the name/value pair
3490 * exist in the same block. So adjust end to
3491 * the previous block end if needed.
3493 if (((end - len) / blocksize !=
3494 (end - 1) / blocksize))
3495 end = end - end % blocksize;
3497 if (end > offset + len) {
3498 memmove(bucket_buf + end - len,
3499 bucket_buf + offset, len);
3500 xe->xe_name_offset = cpu_to_le16(end - len);
3503 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3504 "bucket %llu\n", (unsigned long long)blkno);
3509 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3510 "bucket %llu\n", (unsigned long long)blkno);
3512 if (xh_free_start == end)
3515 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3516 xh->xh_free_start = cpu_to_le16(end);
3518 /* sort the entries by their name_hash. */
3519 sort(entries, le16_to_cpu(xh->xh_count),
3520 sizeof(struct ocfs2_xattr_entry),
3524 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3525 memcpy(bucket_block(bucket, i), buf, blocksize);
3526 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
3534 * prev_blkno points to the start of an existing extent. new_blkno
3535 * points to a newly allocated extent. Because we know each of our
3536 * clusters contains more than bucket, we can easily split one cluster
3537 * at a bucket boundary. So we take the last cluster of the existing
3538 * extent and split it down the middle. We move the last half of the
3539 * buckets in the last cluster of the existing extent over to the new
3542 * first_bh is the buffer at prev_blkno so we can update the existing
3543 * extent's bucket count. header_bh is the bucket were we were hoping
3544 * to insert our xattr. If the bucket move places the target in the new
3545 * extent, we'll update first_bh and header_bh after modifying the old
3548 * first_hash will be set as the 1st xe's name_hash in the new extent.
3550 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3552 struct buffer_head **first_bh,
3553 struct buffer_head **header_bh,
3559 int i, ret, credits;
3560 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3561 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3562 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3563 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3564 int to_move = num_buckets / 2;
3565 u64 last_cluster_blkno, src_blkno;
3566 struct ocfs2_xattr_header *xh =
3567 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3568 struct ocfs2_xattr_bucket *old_first, *new_first;
3570 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3571 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3573 last_cluster_blkno = prev_blkno + ((num_clusters - 1) * bpc);
3574 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3576 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3577 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
3579 /* The first bucket of the original extent */
3580 old_first = ocfs2_xattr_bucket_new(inode);
3581 /* The first bucket of the new extent */
3582 new_first = ocfs2_xattr_bucket_new(inode);
3583 if (!old_first || !new_first) {
3589 ret = ocfs2_read_xattr_bucket(old_first, prev_blkno);
3596 * We need to update the 1st half of the new extent, and we
3597 * need to update the first bucket of the old extent.
3599 credits = ((to_move + 1) * blks_per_bucket) + handle->h_buffer_credits;
3600 ret = ocfs2_extend_trans(handle, credits);
3606 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
3607 OCFS2_JOURNAL_ACCESS_WRITE);
3613 for (i = 0; i < to_move; i++) {
3614 ret = ocfs2_cp_xattr_bucket(inode, handle,
3615 src_blkno + (i * blks_per_bucket),
3616 new_blkno + (i * blks_per_bucket),
3625 * Get the new bucket ready before we dirty anything
3626 * (This actually shouldn't fail, because we already dirtied
3627 * it once in ocfs2_cp_xattr_bucket()).
3629 ret = ocfs2_read_xattr_bucket(new_first, new_blkno);
3634 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
3635 OCFS2_JOURNAL_ACCESS_WRITE);
3641 /* Now update the headers */
3642 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -to_move);
3643 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
3645 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(to_move);
3646 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
3649 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
3652 * If the target bucket is anywhere past src_blkno, we moved
3653 * it to the new extent. We need to update first_bh and header_bh.
3655 if ((*header_bh)->b_blocknr >= src_blkno) {
3656 /* We're done with old_first, so we can re-use it. */
3657 ocfs2_xattr_bucket_relse(old_first);
3659 /* Find the block for the new target bucket */
3660 src_blkno = new_blkno +
3661 ((*header_bh)->b_blocknr - src_blkno);
3664 * This shouldn't fail - the buffers are in the
3665 * journal from ocfs2_cp_xattr_bucket().
3667 ret = ocfs2_read_xattr_bucket(old_first, src_blkno);
3674 *first_bh = new_first->bu_bhs[0];
3678 *header_bh = old_first->bu_bhs[0];
3683 ocfs2_xattr_bucket_free(new_first);
3684 ocfs2_xattr_bucket_free(old_first);
3690 * Find the suitable pos when we divide a bucket into 2.
3691 * We have to make sure the xattrs with the same hash value exist
3692 * in the same bucket.
3694 * If this ocfs2_xattr_header covers more than one hash value, find a
3695 * place where the hash value changes. Try to find the most even split.
3696 * The most common case is that all entries have different hash values,
3697 * and the first check we make will find a place to split.
3699 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3701 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3702 int count = le16_to_cpu(xh->xh_count);
3703 int delta, middle = count / 2;
3706 * We start at the middle. Each step gets farther away in both
3707 * directions. We therefore hit the change in hash value
3708 * nearest to the middle. Note that this loop does not execute for
3711 for (delta = 0; delta < middle; delta++) {
3712 /* Let's check delta earlier than middle */
3713 if (cmp_xe(&entries[middle - delta - 1],
3714 &entries[middle - delta]))
3715 return middle - delta;
3717 /* For even counts, don't walk off the end */
3718 if ((middle + delta + 1) == count)
3721 /* Now try delta past middle */
3722 if (cmp_xe(&entries[middle + delta],
3723 &entries[middle + delta + 1]))
3724 return middle + delta + 1;
3727 /* Every entry had the same hash */
3732 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3733 * first_hash will record the 1st hash of the new bucket.
3735 * Normally half of the xattrs will be moved. But we have to make
3736 * sure that the xattrs with the same hash value are stored in the
3737 * same bucket. If all the xattrs in this bucket have the same hash
3738 * value, the new bucket will be initialized as an empty one and the
3739 * first_hash will be initialized as (hash_value+1).
3741 static int ocfs2_divide_xattr_bucket(struct inode *inode,
3746 int new_bucket_head)
3749 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
3750 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3751 struct ocfs2_xattr_header *xh;
3752 struct ocfs2_xattr_entry *xe;
3753 int blocksize = inode->i_sb->s_blocksize;
3755 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3756 (unsigned long long)blk, (unsigned long long)new_blk);
3758 s_bucket = ocfs2_xattr_bucket_new(inode);
3759 t_bucket = ocfs2_xattr_bucket_new(inode);
3760 if (!s_bucket || !t_bucket) {
3766 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
3772 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
3773 OCFS2_JOURNAL_ACCESS_WRITE);
3780 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3781 * there's no need to read it.
3783 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
3790 * Hey, if we're overwriting t_bucket, what difference does
3791 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
3792 * same part of ocfs2_cp_xattr_bucket().
3794 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3796 OCFS2_JOURNAL_ACCESS_CREATE :
3797 OCFS2_JOURNAL_ACCESS_WRITE);
3803 xh = bucket_xh(s_bucket);
3804 count = le16_to_cpu(xh->xh_count);
3805 start = ocfs2_xattr_find_divide_pos(xh);
3807 if (start == count) {
3808 xe = &xh->xh_entries[start-1];
3811 * initialized a new empty bucket here.
3812 * The hash value is set as one larger than
3813 * that of the last entry in the previous bucket.
3815 for (i = 0; i < t_bucket->bu_blocks; i++)
3816 memset(bucket_block(t_bucket, i), 0, blocksize);
3818 xh = bucket_xh(t_bucket);
3819 xh->xh_free_start = cpu_to_le16(blocksize);
3820 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3821 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3823 goto set_num_buckets;
3826 /* copy the whole bucket to the new first. */
3827 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3829 /* update the new bucket. */
3830 xh = bucket_xh(t_bucket);
3833 * Calculate the total name/value len and xh_free_start for
3834 * the old bucket first.
3836 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3838 for (i = 0; i < start; i++) {
3839 xe = &xh->xh_entries[i];
3840 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3841 if (ocfs2_xattr_is_local(xe))
3843 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3845 xe_len += OCFS2_XATTR_ROOT_SIZE;
3846 name_value_len += xe_len;
3847 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3848 name_offset = le16_to_cpu(xe->xe_name_offset);
3852 * Now begin the modification to the new bucket.
3854 * In the new bucket, We just move the xattr entry to the beginning
3855 * and don't touch the name/value. So there will be some holes in the
3856 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3859 xe = &xh->xh_entries[start];
3860 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3861 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3862 (int)((char *)xe - (char *)xh),
3863 (int)((char *)xh->xh_entries - (char *)xh));
3864 memmove((char *)xh->xh_entries, (char *)xe, len);
3865 xe = &xh->xh_entries[count - start];
3866 len = sizeof(struct ocfs2_xattr_entry) * start;
3867 memset((char *)xe, 0, len);
3869 le16_add_cpu(&xh->xh_count, -start);
3870 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3872 /* Calculate xh_free_start for the new bucket. */
3873 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3874 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3875 xe = &xh->xh_entries[i];
3876 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3877 if (ocfs2_xattr_is_local(xe))
3879 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3881 xe_len += OCFS2_XATTR_ROOT_SIZE;
3882 if (le16_to_cpu(xe->xe_name_offset) <
3883 le16_to_cpu(xh->xh_free_start))
3884 xh->xh_free_start = xe->xe_name_offset;
3888 /* set xh->xh_num_buckets for the new xh. */
3889 if (new_bucket_head)
3890 xh->xh_num_buckets = cpu_to_le16(1);
3892 xh->xh_num_buckets = 0;
3894 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3896 /* store the first_hash of the new bucket. */
3898 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3901 * Now only update the 1st block of the old bucket. If we
3902 * just added a new empty bucket, there is no need to modify
3908 xh = bucket_xh(s_bucket);
3909 memset(&xh->xh_entries[start], 0,
3910 sizeof(struct ocfs2_xattr_entry) * (count - start));
3911 xh->xh_count = cpu_to_le16(start);
3912 xh->xh_free_start = cpu_to_le16(name_offset);
3913 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3915 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
3918 ocfs2_xattr_bucket_free(s_bucket);
3919 ocfs2_xattr_bucket_free(t_bucket);
3925 * Copy xattr from one bucket to another bucket.
3927 * The caller must make sure that the journal transaction
3928 * has enough space for journaling.
3930 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3937 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3939 BUG_ON(s_blkno == t_blkno);
3941 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3942 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3945 s_bucket = ocfs2_xattr_bucket_new(inode);
3946 t_bucket = ocfs2_xattr_bucket_new(inode);
3947 if (!s_bucket || !t_bucket) {
3953 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
3958 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3959 * there's no need to read it.
3961 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
3966 * Hey, if we're overwriting t_bucket, what difference does
3967 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
3968 * cluster to fill, we came here from
3969 * ocfs2_mv_xattr_buckets(), and it is really new -
3970 * ACCESS_CREATE is required. But we also might have moved data
3971 * out of t_bucket before extending back into it.
3972 * ocfs2_add_new_xattr_bucket() can do this - its call to
3973 * ocfs2_add_new_xattr_cluster() may have created a new extent
3974 * and copied out the end of the old extent. Then it re-extends
3975 * the old extent back to create space for new xattrs. That's
3976 * how we get here, and the bucket isn't really new.
3978 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3980 OCFS2_JOURNAL_ACCESS_CREATE :
3981 OCFS2_JOURNAL_ACCESS_WRITE);
3985 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3986 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3989 ocfs2_xattr_bucket_free(t_bucket);
3990 ocfs2_xattr_bucket_free(s_bucket);
3996 * src_blk points to the start of an existing extent. last_blk points to
3997 * last cluster in that extent. to_blk points to a newly allocated
3998 * extent. We copy the buckets from cluster at last_blk to the new extent,
3999 * initializing its xh_num_buckets. The old extent's xh_num_buckets
4000 * shrinks by the same amount.
4002 static int ocfs2_mv_xattr_buckets(struct inode *inode,
4004 u64 src_blk, u64 last_blk,
4005 u64 to_blk, u32 *first_hash)
4007 int i, ret, credits;
4008 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4009 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4010 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4011 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4012 struct ocfs2_xattr_bucket *old_first, *new_first;
4014 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4015 (unsigned long long)last_blk, (unsigned long long)to_blk);
4017 /* The first bucket of the original extent */
4018 old_first = ocfs2_xattr_bucket_new(inode);
4019 /* The first bucket of the new extent */
4020 new_first = ocfs2_xattr_bucket_new(inode);
4021 if (!old_first || !new_first) {
4027 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4034 * We need to update the first bucket of the old extent and the
4035 * entire first cluster of the new extent.
4037 credits = blks_per_bucket + bpc + handle->h_buffer_credits;
4038 ret = ocfs2_extend_trans(handle, credits);
4044 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4045 OCFS2_JOURNAL_ACCESS_WRITE);
4051 for (i = 0; i < num_buckets; i++) {
4052 ret = ocfs2_cp_xattr_bucket(inode, handle,
4053 last_blk + (i * blks_per_bucket),
4054 to_blk + (i * blks_per_bucket),
4063 * Get the new bucket ready before we dirty anything
4064 * (This actually shouldn't fail, because we already dirtied
4065 * it once in ocfs2_cp_xattr_bucket()).
4067 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4072 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4073 OCFS2_JOURNAL_ACCESS_WRITE);
4079 /* Now update the headers */
4080 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4081 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4083 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4084 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4087 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4090 ocfs2_xattr_bucket_free(new_first);
4091 ocfs2_xattr_bucket_free(old_first);
4096 * Move some xattrs in this cluster to the new cluster.
4097 * This function should only be called when bucket size == cluster size.
4098 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4100 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4106 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4107 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4109 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4111 ret = ocfs2_extend_trans(handle, credits);
4117 /* Move half of the xattr in start_blk to the next bucket. */
4118 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4119 new_blk, first_hash, 1);
4123 * Move some xattrs from the old cluster to the new one since they are not
4124 * contiguous in ocfs2 xattr tree.
4126 * new_blk starts a new separate cluster, and we will move some xattrs from
4127 * prev_blk to it. v_start will be set as the first name hash value in this
4128 * new cluster so that it can be used as e_cpos during tree insertion and
4129 * don't collide with our original b-tree operations. first_bh and header_bh
4130 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4131 * to extend the insert bucket.
4133 * The problem is how much xattr should we move to the new one and when should
4134 * we update first_bh and header_bh?
4135 * 1. If cluster size > bucket size, that means the previous cluster has more
4136 * than 1 bucket, so just move half nums of bucket into the new cluster and
4137 * update the first_bh and header_bh if the insert bucket has been moved
4138 * to the new cluster.
4139 * 2. If cluster_size == bucket_size:
4140 * a) If the previous extent rec has more than one cluster and the insert
4141 * place isn't in the last cluster, copy the entire last cluster to the
4142 * new one. This time, we don't need to upate the first_bh and header_bh
4143 * since they will not be moved into the new cluster.
4144 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4145 * the new one. And we set the extend flag to zero if the insert place is
4146 * moved into the new allocated cluster since no extend is needed.
4148 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4150 struct buffer_head **first_bh,
4151 struct buffer_head **header_bh,
4159 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4161 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4162 (unsigned long long)prev_blk, prev_clusters,
4163 (unsigned long long)new_blk);
4165 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
4166 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4175 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
4177 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
4178 ret = ocfs2_mv_xattr_buckets(inode, handle,
4179 (*first_bh)->b_blocknr,
4184 ret = ocfs2_divide_xattr_cluster(inode, handle,
4188 if ((*header_bh)->b_blocknr == last_blk && extend)
4197 * Add a new cluster for xattr storage.
4199 * If the new cluster is contiguous with the previous one, it will be
4200 * appended to the same extent record, and num_clusters will be updated.
4201 * If not, we will insert a new extent for it and move some xattrs in
4202 * the last cluster into the new allocated one.
4203 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4204 * lose the benefits of hashing because we'll have to search large leaves.
4205 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4208 * first_bh is the first block of the previous extent rec and header_bh
4209 * indicates the bucket we will insert the new xattrs. They will be updated
4210 * when the header_bh is moved into the new cluster.
4212 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4213 struct buffer_head *root_bh,
4214 struct buffer_head **first_bh,
4215 struct buffer_head **header_bh,
4220 struct ocfs2_xattr_set_ctxt *ctxt)
4223 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4224 u32 prev_clusters = *num_clusters;
4225 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4227 handle_t *handle = ctxt->handle;
4228 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4229 struct ocfs2_extent_tree et;
4231 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4232 "previous xattr blkno = %llu\n",
4233 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4234 prev_cpos, (unsigned long long)prev_blkno);
4236 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4238 ret = ocfs2_journal_access(handle, inode, root_bh,
4239 OCFS2_JOURNAL_ACCESS_WRITE);
4245 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
4246 clusters_to_add, &bit_off, &num_bits);
4253 BUG_ON(num_bits > clusters_to_add);
4255 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4256 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4257 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4259 if (prev_blkno + prev_clusters * bpc == block &&
4260 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4261 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4263 * If this cluster is contiguous with the old one and
4264 * adding this new cluster, we don't surpass the limit of
4265 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4266 * initialized and used like other buckets in the previous
4268 * So add it as a contiguous one. The caller will handle
4271 v_start = prev_cpos + prev_clusters;
4272 *num_clusters = prev_clusters + num_bits;
4273 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4276 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4291 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4292 num_bits, (unsigned long long)block, v_start);
4293 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
4294 num_bits, 0, ctxt->meta_ac);
4300 ret = ocfs2_journal_dirty(handle, root_bh);
4309 * We are given an extent. 'first' is the bucket at the very front of
4310 * the extent. The extent has space for an additional bucket past
4311 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4312 * of the target bucket. We wish to shift every bucket past the target
4313 * down one, filling in that additional space. When we get back to the
4314 * target, we split the target between itself and the now-empty bucket
4315 * at target+1 (aka, target_blkno + blks_per_bucket).
4317 static int ocfs2_extend_xattr_bucket(struct inode *inode,
4319 struct ocfs2_xattr_bucket *first,
4324 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4325 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4327 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
4329 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4330 "from %llu, len = %u\n", (unsigned long long)target_blk,
4331 (unsigned long long)bucket_blkno(first), num_clusters);
4333 /* The extent must have room for an additional bucket */
4334 BUG_ON(new_bucket >=
4335 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
4337 /* end_blk points to the last existing bucket */
4338 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
4341 * end_blk is the start of the last existing bucket.
4342 * Thus, (end_blk - target_blk) covers the target bucket and
4343 * every bucket after it up to, but not including, the last
4344 * existing bucket. Then we add the last existing bucket, the
4345 * new bucket, and the first bucket (3 * blk_per_bucket).
4347 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
4348 handle->h_buffer_credits;
4349 ret = ocfs2_extend_trans(handle, credits);
4355 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4356 OCFS2_JOURNAL_ACCESS_WRITE);
4362 while (end_blk != target_blk) {
4363 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4364 end_blk + blk_per_bucket, 0);
4367 end_blk -= blk_per_bucket;
4370 /* Move half of the xattr in target_blkno to the next bucket. */
4371 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4372 target_blk + blk_per_bucket, NULL, 0);
4374 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4375 ocfs2_xattr_bucket_journal_dirty(handle, first);
4382 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
4383 * xb_bh is the ocfs2_xattr_block.
4384 * We will move all the buckets starting from header_bh to the next place. As
4385 * for this one, half num of its xattrs will be moved to the next one.
4387 * We will allocate a new cluster if current cluster is full and adjust
4388 * header_bh and first_bh if the insert place is moved to the new cluster.
4390 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4391 struct buffer_head *xb_bh,
4392 struct buffer_head *header_bh,
4393 struct ocfs2_xattr_set_ctxt *ctxt)
4395 struct ocfs2_xattr_header *first_xh = NULL;
4396 struct buffer_head *first_bh = NULL;
4397 struct ocfs2_xattr_block *xb =
4398 (struct ocfs2_xattr_block *)xb_bh->b_data;
4399 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4400 struct ocfs2_extent_list *el = &xb_root->xt_list;
4401 struct ocfs2_xattr_header *xh =
4402 (struct ocfs2_xattr_header *)header_bh->b_data;
4403 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4404 struct super_block *sb = inode->i_sb;
4405 struct ocfs2_super *osb = OCFS2_SB(sb);
4406 int ret, num_buckets, extend = 1;
4408 u32 e_cpos, num_clusters;
4409 /* The bucket at the front of the extent */
4410 struct ocfs2_xattr_bucket *first;
4412 mlog(0, "Add new xattr bucket starting form %llu\n",
4413 (unsigned long long)header_bh->b_blocknr);
4415 first = ocfs2_xattr_bucket_new(inode);
4423 * Add refrence for header_bh here because it may be
4424 * changed in ocfs2_add_new_xattr_cluster and we need
4425 * to free it in the end.
4429 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4436 ret = ocfs2_read_block(inode, p_blkno, &first_bh, NULL);
4442 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4443 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4445 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
4446 ret = ocfs2_add_new_xattr_cluster(inode,
4462 /* These bucket reads should be cached */
4463 ret = ocfs2_read_xattr_bucket(first, first_bh->b_blocknr);
4468 ret = ocfs2_extend_xattr_bucket(inode,
4470 first, header_bh->b_blocknr,
4479 ocfs2_xattr_bucket_free(first);
4483 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4484 struct ocfs2_xattr_bucket *bucket,
4487 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4489 offs = offs % inode->i_sb->s_blocksize;
4490 return bucket_block(bucket, block_off) + offs;
4494 * Handle the normal xattr set, including replace, delete and new.
4496 * Note: "local" indicates the real data's locality. So we can't
4497 * just its bucket locality by its length.
4499 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4500 struct ocfs2_xattr_info *xi,
4501 struct ocfs2_xattr_search *xs,
4505 struct ocfs2_xattr_entry *last, *xe;
4506 int name_len = strlen(xi->name);
4507 struct ocfs2_xattr_header *xh = xs->header;
4508 u16 count = le16_to_cpu(xh->xh_count), start;
4509 size_t blocksize = inode->i_sb->s_blocksize;
4511 size_t offs, size, new_size;
4513 last = &xh->xh_entries[count];
4514 if (!xs->not_found) {
4516 offs = le16_to_cpu(xe->xe_name_offset);
4517 if (ocfs2_xattr_is_local(xe))
4518 size = OCFS2_XATTR_SIZE(name_len) +
4519 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4521 size = OCFS2_XATTR_SIZE(name_len) +
4522 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4525 * If the new value will be stored outside, xi->value has been
4526 * initalized as an empty ocfs2_xattr_value_root, and the same
4527 * goes with xi->value_len, so we can set new_size safely here.
4528 * See ocfs2_xattr_set_in_bucket.
4530 new_size = OCFS2_XATTR_SIZE(name_len) +
4531 OCFS2_XATTR_SIZE(xi->value_len);
4533 le16_add_cpu(&xh->xh_name_value_len, -size);
4535 if (new_size > size)
4536 goto set_new_name_value;
4538 /* Now replace the old value with new one. */
4540 xe->xe_value_size = cpu_to_le64(xi->value_len);
4542 xe->xe_value_size = 0;
4544 val = ocfs2_xattr_bucket_get_val(inode,
4546 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4547 size - OCFS2_XATTR_SIZE(name_len));
4548 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4549 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4550 xi->value, xi->value_len);
4552 le16_add_cpu(&xh->xh_name_value_len, new_size);
4553 ocfs2_xattr_set_local(xe, local);
4557 * Remove the old entry if there is more than one.
4558 * We don't remove the last entry so that we can
4559 * use it to indicate the hash value of the empty
4563 le16_add_cpu(&xh->xh_count, -1);
4566 (void *)last - (void *)xe);
4568 sizeof(struct ocfs2_xattr_entry));
4571 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4576 /* find a new entry for insert. */
4577 int low = 0, high = count - 1, tmp;
4578 struct ocfs2_xattr_entry *tmp_xe;
4580 while (low <= high && count) {
4581 tmp = (low + high) / 2;
4582 tmp_xe = &xh->xh_entries[tmp];
4584 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4586 else if (name_hash <
4587 le32_to_cpu(tmp_xe->xe_name_hash))
4595 xe = &xh->xh_entries[low];
4597 memmove(xe + 1, xe, (void *)last - (void *)xe);
4599 le16_add_cpu(&xh->xh_count, 1);
4600 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4601 xe->xe_name_hash = cpu_to_le32(name_hash);
4602 xe->xe_name_len = name_len;
4603 ocfs2_xattr_set_type(xe, xi->name_index);
4607 /* Insert the new name+value. */
4608 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4611 * We must make sure that the name/value pair
4612 * exists in the same block.
4614 offs = le16_to_cpu(xh->xh_free_start);
4615 start = offs - size;
4617 if (start >> inode->i_sb->s_blocksize_bits !=
4618 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4619 offs = offs - offs % blocksize;
4620 xh->xh_free_start = cpu_to_le16(offs);
4623 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
4624 xe->xe_name_offset = cpu_to_le16(offs - size);
4626 memset(val, 0, size);
4627 memcpy(val, xi->name, name_len);
4628 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4630 xe->xe_value_size = cpu_to_le64(xi->value_len);
4631 ocfs2_xattr_set_local(xe, local);
4633 le16_add_cpu(&xh->xh_free_start, -size);
4634 le16_add_cpu(&xh->xh_name_value_len, size);
4640 * Set the xattr entry in the specified bucket.
4641 * The bucket is indicated by xs->bucket and it should have the enough
4642 * space for the xattr insertion.
4644 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4646 struct ocfs2_xattr_info *xi,
4647 struct ocfs2_xattr_search *xs,
4654 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4655 (unsigned long)xi->value_len, xi->name_index,
4656 (unsigned long long)bucket_blkno(xs->bucket));
4658 if (!xs->bucket->bu_bhs[1]) {
4659 blkno = bucket_blkno(xs->bucket);
4660 ocfs2_xattr_bucket_relse(xs->bucket);
4661 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
4668 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4669 OCFS2_JOURNAL_ACCESS_WRITE);
4675 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4676 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4683 * Truncate the specified xe_off entry in xattr bucket.
4684 * bucket is indicated by header_bh and len is the new length.
4685 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4687 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4689 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4690 struct ocfs2_xattr_bucket *bucket,
4693 struct ocfs2_xattr_set_ctxt *ctxt)
4697 struct buffer_head *value_bh = NULL;
4698 struct ocfs2_xattr_value_root *xv;
4699 struct ocfs2_xattr_entry *xe;
4700 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4701 size_t blocksize = inode->i_sb->s_blocksize;
4703 xe = &xh->xh_entries[xe_off];
4705 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4707 offset = le16_to_cpu(xe->xe_name_offset) +
4708 OCFS2_XATTR_SIZE(xe->xe_name_len);
4710 value_blk = offset / blocksize;
4712 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4713 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4715 value_bh = bucket->bu_bhs[value_blk];
4718 xv = (struct ocfs2_xattr_value_root *)
4719 (value_bh->b_data + offset % blocksize);
4721 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
4722 OCFS2_JOURNAL_ACCESS_WRITE);
4729 * From here on out we have to dirty the bucket. The generic
4730 * value calls only modify one of the bucket's bhs, but we need
4731 * to send the bucket at once. So if they error, they *could* have
4732 * modified something. We have to assume they did, and dirty
4733 * the whole bucket. This leaves us in a consistent state.
4735 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4736 xe_off, (unsigned long long)bucket_blkno(bucket), len);
4737 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
4743 xe->xe_value_size = cpu_to_le64(len);
4746 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
4752 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4753 struct ocfs2_xattr_search *xs,
4755 struct ocfs2_xattr_set_ctxt *ctxt)
4758 struct ocfs2_xattr_entry *xe = xs->here;
4759 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4761 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4763 offset = xe - xh->xh_entries;
4764 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
4772 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4774 struct ocfs2_xattr_search *xs,
4779 struct ocfs2_xattr_value_root *xv;
4780 struct ocfs2_xattr_entry *xe = xs->here;
4782 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4784 offset = le16_to_cpu(xe->xe_name_offset) +
4785 OCFS2_XATTR_SIZE(xe->xe_name_len);
4787 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4789 return __ocfs2_xattr_set_value_outside(inode, handle,
4790 xv, val, value_len);
4793 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4794 struct buffer_head *root_bh,
4800 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4801 struct inode *tl_inode = osb->osb_tl_inode;
4803 struct ocfs2_xattr_block *xb =
4804 (struct ocfs2_xattr_block *)root_bh->b_data;
4805 struct ocfs2_alloc_context *meta_ac = NULL;
4806 struct ocfs2_cached_dealloc_ctxt dealloc;
4807 struct ocfs2_extent_tree et;
4809 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4811 ocfs2_init_dealloc_ctxt(&dealloc);
4813 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4814 cpos, len, (unsigned long long)blkno);
4816 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4818 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4824 mutex_lock(&tl_inode->i_mutex);
4826 if (ocfs2_truncate_log_needs_flush(osb)) {
4827 ret = __ocfs2_flush_truncate_log(osb);
4834 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
4835 if (IS_ERR(handle)) {
4841 ret = ocfs2_journal_access(handle, inode, root_bh,
4842 OCFS2_JOURNAL_ACCESS_WRITE);
4848 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4855 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4857 ret = ocfs2_journal_dirty(handle, root_bh);
4863 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4868 ocfs2_commit_trans(osb, handle);
4870 ocfs2_schedule_truncate_log_flush(osb, 1);
4872 mutex_unlock(&tl_inode->i_mutex);
4875 ocfs2_free_alloc_context(meta_ac);
4877 ocfs2_run_deallocs(osb, &dealloc);
4882 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4884 struct ocfs2_xattr_search *xs)
4886 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
4887 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4888 le16_to_cpu(xh->xh_count) - 1];
4891 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4892 OCFS2_JOURNAL_ACCESS_WRITE);
4898 /* Remove the old entry. */
4899 memmove(xs->here, xs->here + 1,
4900 (void *)last - (void *)xs->here);
4901 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4902 le16_add_cpu(&xh->xh_count, -1);
4904 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4908 * Set the xattr name/value in the bucket specified in xs.
4910 * As the new value in xi may be stored in the bucket or in an outside cluster,
4911 * we divide the whole process into 3 steps:
4912 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4913 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4914 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4915 * 4. If the clusters for the new outside value can't be allocated, we need
4916 * to free the xattr we allocated in set.
4918 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4919 struct ocfs2_xattr_info *xi,
4920 struct ocfs2_xattr_search *xs,
4921 struct ocfs2_xattr_set_ctxt *ctxt)
4925 char *val = (char *)xi->value;
4926 struct ocfs2_xattr_entry *xe = xs->here;
4927 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4930 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4932 * We need to truncate the xattr storage first.
4934 * If both the old and new value are stored to
4935 * outside block, we only need to truncate
4936 * the storage and then set the value outside.
4938 * If the new value should be stored within block,
4939 * we should free all the outside block first and
4940 * the modification to the xattr block will be done
4941 * by following steps.
4943 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4944 value_len = xi->value_len;
4948 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4955 goto set_value_outside;
4958 value_len = xi->value_len;
4959 /* So we have to handle the inside block change now. */
4960 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4962 * If the new value will be stored outside of block,
4963 * initalize a new empty value root and insert it first.
4966 xi->value = &def_xv;
4967 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4970 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4977 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4980 /* allocate the space now for the outside block storage. */
4981 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4986 if (xs->not_found) {
4988 * We can't allocate enough clusters for outside
4989 * storage and we have allocated xattr already,
4990 * so need to remove it.
4992 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
4998 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4999 xs, val, value_len);
5005 * check whether the xattr bucket is filled up with the same hash value.
5006 * If we want to insert the xattr with the same hash, return -ENOSPC.
5007 * If we want to insert a xattr with different hash value, go ahead
5008 * and ocfs2_divide_xattr_bucket will handle this.
5010 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5011 struct ocfs2_xattr_bucket *bucket,
5014 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5015 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5017 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5020 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5021 xh->xh_entries[0].xe_name_hash) {
5022 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5024 (unsigned long long)bucket_blkno(bucket),
5025 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5032 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5033 struct ocfs2_xattr_info *xi,
5034 struct ocfs2_xattr_search *xs,
5035 struct ocfs2_xattr_set_ctxt *ctxt)
5037 struct ocfs2_xattr_header *xh;
5038 struct ocfs2_xattr_entry *xe;
5039 u16 count, header_size, xh_free_start;
5040 int free, max_free, need, old;
5041 size_t value_size = 0, name_len = strlen(xi->name);
5042 size_t blocksize = inode->i_sb->s_blocksize;
5043 int ret, allocation = 0;
5045 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5049 count = le16_to_cpu(xh->xh_count);
5050 xh_free_start = le16_to_cpu(xh->xh_free_start);
5051 header_size = sizeof(struct ocfs2_xattr_header) +
5052 count * sizeof(struct ocfs2_xattr_entry);
5053 max_free = OCFS2_XATTR_BUCKET_SIZE -
5054 le16_to_cpu(xh->xh_name_value_len) - header_size;
5056 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5057 "of %u which exceed block size\n",
5058 (unsigned long long)bucket_blkno(xs->bucket),
5061 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5062 value_size = OCFS2_XATTR_ROOT_SIZE;
5064 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5067 need = sizeof(struct ocfs2_xattr_entry) +
5068 OCFS2_XATTR_SIZE(name_len) + value_size;
5070 need = value_size + OCFS2_XATTR_SIZE(name_len);
5073 * We only replace the old value if the new length is smaller
5074 * than the old one. Otherwise we will allocate new space in the
5075 * bucket to store it.
5078 if (ocfs2_xattr_is_local(xe))
5079 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5081 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5083 if (old >= value_size)
5087 free = xh_free_start - header_size;
5089 * We need to make sure the new name/value pair
5090 * can exist in the same block.
5092 if (xh_free_start % blocksize < need)
5093 free -= xh_free_start % blocksize;
5095 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5096 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5097 " %u\n", xs->not_found,
5098 (unsigned long long)bucket_blkno(xs->bucket),
5099 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5100 le16_to_cpu(xh->xh_name_value_len));
5104 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5105 if (need <= max_free &&
5106 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5108 * We can create the space by defragment. Since only the
5109 * name/value will be moved, the xe shouldn't be changed
5112 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5119 xh_free_start = le16_to_cpu(xh->xh_free_start);
5120 free = xh_free_start - header_size;
5121 if (xh_free_start % blocksize < need)
5122 free -= xh_free_start % blocksize;
5127 mlog(0, "Can't get enough space for xattr insert by "
5128 "defragment. Need %u bytes, but we have %d, so "
5129 "allocate new bucket for it.\n", need, free);
5133 * We have to add new buckets or clusters and one
5134 * allocation should leave us enough space for insert.
5139 * We do not allow for overlapping ranges between buckets. And
5140 * the maximum number of collisions we will allow for then is
5141 * one bucket's worth, so check it here whether we need to
5142 * add a new bucket for the insert.
5144 ret = ocfs2_check_xattr_bucket_collision(inode,
5152 ret = ocfs2_add_new_xattr_bucket(inode,
5154 xs->bucket->bu_bhs[0],
5161 ocfs2_xattr_bucket_relse(xs->bucket);
5163 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5166 if (ret && ret != -ENODATA)
5168 xs->not_found = ret;
5174 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5180 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5181 struct ocfs2_xattr_bucket *bucket,
5185 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5187 struct ocfs2_xattr_entry *xe;
5188 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5189 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5190 int credits = ocfs2_remove_extent_credits(osb->sb) +
5191 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5194 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5196 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5197 xe = &xh->xh_entries[i];
5198 if (ocfs2_xattr_is_local(xe))
5201 ctxt.handle = ocfs2_start_trans(osb, credits);
5202 if (IS_ERR(ctxt.handle)) {
5203 ret = PTR_ERR(ctxt.handle);
5208 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5211 ocfs2_commit_trans(osb, ctxt.handle);
5218 ocfs2_schedule_truncate_log_flush(osb, 1);
5219 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5223 static int ocfs2_delete_xattr_index_block(struct inode *inode,
5224 struct buffer_head *xb_bh)
5226 struct ocfs2_xattr_block *xb =
5227 (struct ocfs2_xattr_block *)xb_bh->b_data;
5228 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5230 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5233 if (le16_to_cpu(el->l_next_free_rec) == 0)
5236 while (name_hash > 0) {
5237 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5238 &e_cpos, &num_clusters, el);
5244 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5245 ocfs2_delete_xattr_in_bucket,
5252 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5253 p_blkno, e_cpos, num_clusters);
5262 name_hash = e_cpos - 1;
5270 * 'security' attributes support
5272 static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5273 size_t list_size, const char *name,
5276 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5277 const size_t total_len = prefix_len + name_len + 1;
5279 if (list && total_len <= list_size) {
5280 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5281 memcpy(list + prefix_len, name, name_len);
5282 list[prefix_len + name_len] = '\0';
5287 static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5288 void *buffer, size_t size)
5290 if (strcmp(name, "") == 0)
5292 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5296 static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5297 const void *value, size_t size, int flags)
5299 if (strcmp(name, "") == 0)
5302 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5306 int ocfs2_init_security_get(struct inode *inode,
5308 struct ocfs2_security_xattr_info *si)
5310 return security_inode_init_security(inode, dir, &si->name, &si->value,
5314 int ocfs2_init_security_set(handle_t *handle,
5315 struct inode *inode,
5316 struct buffer_head *di_bh,
5317 struct ocfs2_security_xattr_info *si,
5318 struct ocfs2_alloc_context *xattr_ac,
5319 struct ocfs2_alloc_context *data_ac)
5321 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5322 OCFS2_XATTR_INDEX_SECURITY,
5323 si->name, si->value, si->value_len, 0,
5327 struct xattr_handler ocfs2_xattr_security_handler = {
5328 .prefix = XATTR_SECURITY_PREFIX,
5329 .list = ocfs2_xattr_security_list,
5330 .get = ocfs2_xattr_security_get,
5331 .set = ocfs2_xattr_security_set,
5335 * 'trusted' attributes support
5337 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5338 size_t list_size, const char *name,
5341 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
5342 const size_t total_len = prefix_len + name_len + 1;
5344 if (list && total_len <= list_size) {
5345 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5346 memcpy(list + prefix_len, name, name_len);
5347 list[prefix_len + name_len] = '\0';
5352 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5353 void *buffer, size_t size)
5355 if (strcmp(name, "") == 0)
5357 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5361 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5362 const void *value, size_t size, int flags)
5364 if (strcmp(name, "") == 0)
5367 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5371 struct xattr_handler ocfs2_xattr_trusted_handler = {
5372 .prefix = XATTR_TRUSTED_PREFIX,
5373 .list = ocfs2_xattr_trusted_list,
5374 .get = ocfs2_xattr_trusted_get,
5375 .set = ocfs2_xattr_trusted_set,
5379 * 'user' attributes support
5381 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5382 size_t list_size, const char *name,
5385 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
5386 const size_t total_len = prefix_len + name_len + 1;
5387 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5389 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5392 if (list && total_len <= list_size) {
5393 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5394 memcpy(list + prefix_len, name, name_len);
5395 list[prefix_len + name_len] = '\0';
5400 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5401 void *buffer, size_t size)
5403 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5405 if (strcmp(name, "") == 0)
5407 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5409 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5413 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5414 const void *value, size_t size, int flags)
5416 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5418 if (strcmp(name, "") == 0)
5420 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5423 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5427 struct xattr_handler ocfs2_xattr_user_handler = {
5428 .prefix = XATTR_USER_PREFIX,
5429 .list = ocfs2_xattr_user_list,
5430 .get = ocfs2_xattr_user_get,
5431 .set = ocfs2_xattr_user_set,