1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
39 #define MLOG_MASK_PREFIX ML_XATTR
40 #include <cluster/masklog.h>
53 #include "buffer_head_io.h"
58 struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
63 struct ocfs2_xattr_bucket {
64 /* The inode these xattrs are associated with */
65 struct inode *bu_inode;
67 /* The actual buffers that make up the bucket */
68 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
70 /* How many blocks make up one bucket for this filesystem */
74 struct ocfs2_xattr_set_ctxt {
76 struct ocfs2_alloc_context *meta_ac;
77 struct ocfs2_alloc_context *data_ac;
78 struct ocfs2_cached_dealloc_ctxt dealloc;
81 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
82 #define OCFS2_XATTR_INLINE_SIZE 80
84 static struct ocfs2_xattr_def_value_root def_xv = {
85 .xv.xr_list.l_count = cpu_to_le16(1),
88 struct xattr_handler *ocfs2_xattr_handlers[] = {
89 &ocfs2_xattr_user_handler,
90 &ocfs2_xattr_trusted_handler,
94 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
95 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
96 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
99 struct ocfs2_xattr_info {
106 struct ocfs2_xattr_search {
107 struct buffer_head *inode_bh;
109 * xattr_bh point to the block buffer head which has extended attribute
110 * when extended attribute in inode, xattr_bh is equal to inode_bh.
112 struct buffer_head *xattr_bh;
113 struct ocfs2_xattr_header *header;
114 struct ocfs2_xattr_bucket *bucket;
117 struct ocfs2_xattr_entry *here;
121 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
122 struct ocfs2_xattr_header *xh,
127 static int ocfs2_xattr_block_find(struct inode *inode,
130 struct ocfs2_xattr_search *xs);
131 static int ocfs2_xattr_index_block_find(struct inode *inode,
132 struct buffer_head *root_bh,
135 struct ocfs2_xattr_search *xs);
137 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
138 struct ocfs2_xattr_tree_root *xt,
142 static int ocfs2_xattr_create_index_block(struct inode *inode,
143 struct ocfs2_xattr_search *xs,
144 struct ocfs2_xattr_set_ctxt *ctxt);
146 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
147 struct ocfs2_xattr_info *xi,
148 struct ocfs2_xattr_search *xs,
149 struct ocfs2_xattr_set_ctxt *ctxt);
151 static int ocfs2_delete_xattr_index_block(struct inode *inode,
152 struct buffer_head *xb_bh);
154 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
156 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
159 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
161 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
164 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
166 u16 len = sb->s_blocksize -
167 offsetof(struct ocfs2_xattr_header, xh_entries);
169 return len / sizeof(struct ocfs2_xattr_entry);
172 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
173 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
174 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
176 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
178 struct ocfs2_xattr_bucket *bucket;
179 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
181 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
183 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
185 bucket->bu_inode = inode;
186 bucket->bu_blocks = blks;
192 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
196 for (i = 0; i < bucket->bu_blocks; i++) {
197 brelse(bucket->bu_bhs[i]);
198 bucket->bu_bhs[i] = NULL;
202 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
205 ocfs2_xattr_bucket_relse(bucket);
206 bucket->bu_inode = NULL;
212 * A bucket that has never been written to disk doesn't need to be
213 * read. We just need the buffer_heads. Don't call this for
214 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
217 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
222 for (i = 0; i < bucket->bu_blocks; i++) {
223 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
225 if (!bucket->bu_bhs[i]) {
231 if (!ocfs2_buffer_uptodate(bucket->bu_inode,
233 ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
238 ocfs2_xattr_bucket_relse(bucket);
242 /* Read the xattr bucket at xb_blkno */
243 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
248 rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
249 bucket->bu_blocks, bucket->bu_bhs, 0);
251 ocfs2_xattr_bucket_relse(bucket);
255 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
256 struct ocfs2_xattr_bucket *bucket,
261 for (i = 0; i < bucket->bu_blocks; i++) {
262 rc = ocfs2_journal_access(handle, bucket->bu_inode,
263 bucket->bu_bhs[i], type);
273 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
274 struct ocfs2_xattr_bucket *bucket)
278 for (i = 0; i < bucket->bu_blocks; i++)
279 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
282 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
283 struct ocfs2_xattr_bucket *src)
286 int blocksize = src->bu_inode->i_sb->s_blocksize;
288 BUG_ON(dest->bu_blocks != src->bu_blocks);
289 BUG_ON(dest->bu_inode != src->bu_inode);
291 for (i = 0; i < src->bu_blocks; i++) {
292 memcpy(bucket_block(dest, i), bucket_block(src, i),
297 static inline const char *ocfs2_xattr_prefix(int name_index)
299 struct xattr_handler *handler = NULL;
301 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
302 handler = ocfs2_xattr_handler_map[name_index];
304 return handler ? handler->prefix : NULL;
307 static u32 ocfs2_xattr_name_hash(struct inode *inode,
311 /* Get hash value of uuid from super block */
312 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
315 /* hash extended attribute name */
316 for (i = 0; i < name_len; i++) {
317 hash = (hash << OCFS2_HASH_SHIFT) ^
318 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
326 * ocfs2_xattr_hash_entry()
328 * Compute the hash of an extended attribute.
330 static void ocfs2_xattr_hash_entry(struct inode *inode,
331 struct ocfs2_xattr_header *header,
332 struct ocfs2_xattr_entry *entry)
335 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
337 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
338 entry->xe_name_hash = cpu_to_le32(hash);
343 static int ocfs2_xattr_extend_allocation(struct inode *inode,
345 struct buffer_head *xattr_bh,
346 struct ocfs2_xattr_value_root *xv,
347 struct ocfs2_xattr_set_ctxt *ctxt)
350 handle_t *handle = ctxt->handle;
351 enum ocfs2_alloc_restarted why;
352 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
353 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
354 struct ocfs2_extent_tree et;
356 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
358 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
360 status = ocfs2_journal_access(handle, inode, xattr_bh,
361 OCFS2_JOURNAL_ACCESS_WRITE);
367 prev_clusters = le32_to_cpu(xv->xr_clusters);
368 status = ocfs2_add_clusters_in_btree(osb,
383 status = ocfs2_journal_dirty(handle, xattr_bh);
389 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
392 * We should have already allocated enough space before the transaction,
393 * so no need to restart.
395 BUG_ON(why != RESTART_NONE || clusters_to_add);
402 static int __ocfs2_remove_xattr_range(struct inode *inode,
403 struct buffer_head *root_bh,
404 struct ocfs2_xattr_value_root *xv,
405 u32 cpos, u32 phys_cpos, u32 len,
406 struct ocfs2_xattr_set_ctxt *ctxt)
409 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
410 handle_t *handle = ctxt->handle;
411 struct ocfs2_extent_tree et;
413 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
415 ret = ocfs2_journal_access(handle, inode, root_bh,
416 OCFS2_JOURNAL_ACCESS_WRITE);
422 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
429 le32_add_cpu(&xv->xr_clusters, -len);
431 ret = ocfs2_journal_dirty(handle, root_bh);
437 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
445 static int ocfs2_xattr_shrink_size(struct inode *inode,
448 struct buffer_head *root_bh,
449 struct ocfs2_xattr_value_root *xv,
450 struct ocfs2_xattr_set_ctxt *ctxt)
453 u32 trunc_len, cpos, phys_cpos, alloc_size;
456 if (old_clusters <= new_clusters)
460 trunc_len = old_clusters - new_clusters;
462 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
463 &alloc_size, &xv->xr_list);
469 if (alloc_size > trunc_len)
470 alloc_size = trunc_len;
472 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
473 phys_cpos, alloc_size,
480 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
481 ocfs2_remove_xattr_clusters_from_cache(inode, block,
484 trunc_len -= alloc_size;
491 static int ocfs2_xattr_value_truncate(struct inode *inode,
492 struct buffer_head *root_bh,
493 struct ocfs2_xattr_value_root *xv,
495 struct ocfs2_xattr_set_ctxt *ctxt)
498 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
499 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
501 if (new_clusters == old_clusters)
504 if (new_clusters > old_clusters)
505 ret = ocfs2_xattr_extend_allocation(inode,
506 new_clusters - old_clusters,
509 ret = ocfs2_xattr_shrink_size(inode,
510 old_clusters, new_clusters,
516 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
517 size_t *result, const char *prefix,
518 const char *name, int name_len)
520 char *p = buffer + *result;
521 int prefix_len = strlen(prefix);
522 int total_len = prefix_len + name_len + 1;
524 *result += total_len;
526 /* we are just looking for how big our buffer needs to be */
533 memcpy(p, prefix, prefix_len);
534 memcpy(p + prefix_len, name, name_len);
535 p[prefix_len + name_len] = '\0';
540 static int ocfs2_xattr_list_entries(struct inode *inode,
541 struct ocfs2_xattr_header *header,
542 char *buffer, size_t buffer_size)
546 const char *prefix, *name;
548 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
549 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
550 type = ocfs2_xattr_get_type(entry);
551 prefix = ocfs2_xattr_prefix(type);
554 name = (const char *)header +
555 le16_to_cpu(entry->xe_name_offset);
557 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
558 &result, prefix, name,
568 static int ocfs2_xattr_ibody_list(struct inode *inode,
569 struct ocfs2_dinode *di,
573 struct ocfs2_xattr_header *header = NULL;
574 struct ocfs2_inode_info *oi = OCFS2_I(inode);
577 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
580 header = (struct ocfs2_xattr_header *)
581 ((void *)di + inode->i_sb->s_blocksize -
582 le16_to_cpu(di->i_xattr_inline_size));
584 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
589 static int ocfs2_xattr_block_list(struct inode *inode,
590 struct ocfs2_dinode *di,
594 struct buffer_head *blk_bh = NULL;
595 struct ocfs2_xattr_block *xb;
598 if (!di->i_xattr_loc)
601 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
607 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
608 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
613 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
614 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
615 ret = ocfs2_xattr_list_entries(inode, header,
616 buffer, buffer_size);
618 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
619 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
620 buffer, buffer_size);
628 ssize_t ocfs2_listxattr(struct dentry *dentry,
632 int ret = 0, i_ret = 0, b_ret = 0;
633 struct buffer_head *di_bh = NULL;
634 struct ocfs2_dinode *di = NULL;
635 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
637 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
640 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
643 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
649 di = (struct ocfs2_dinode *)di_bh->b_data;
651 down_read(&oi->ip_xattr_sem);
652 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
660 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
665 up_read(&oi->ip_xattr_sem);
666 ocfs2_inode_unlock(dentry->d_inode, 0);
670 return i_ret + b_ret;
673 static int ocfs2_xattr_find_entry(int name_index,
675 struct ocfs2_xattr_search *xs)
677 struct ocfs2_xattr_entry *entry;
684 name_len = strlen(name);
686 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
687 cmp = name_index - ocfs2_xattr_get_type(entry);
689 cmp = name_len - entry->xe_name_len;
691 cmp = memcmp(name, (xs->base +
692 le16_to_cpu(entry->xe_name_offset)),
700 return cmp ? -ENODATA : 0;
703 static int ocfs2_xattr_get_value_outside(struct inode *inode,
704 struct ocfs2_xattr_value_root *xv,
708 u32 cpos, p_cluster, num_clusters, bpc, clusters;
711 size_t cplen, blocksize;
712 struct buffer_head *bh = NULL;
713 struct ocfs2_extent_list *el;
716 clusters = le32_to_cpu(xv->xr_clusters);
717 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
718 blocksize = inode->i_sb->s_blocksize;
721 while (cpos < clusters) {
722 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
729 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
730 /* Copy ocfs2_xattr_value */
731 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
732 ret = ocfs2_read_block(inode, blkno, &bh);
738 cplen = len >= blocksize ? blocksize : len;
739 memcpy(buffer, bh->b_data, cplen);
748 cpos += num_clusters;
754 static int ocfs2_xattr_ibody_get(struct inode *inode,
759 struct ocfs2_xattr_search *xs)
761 struct ocfs2_inode_info *oi = OCFS2_I(inode);
762 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
763 struct ocfs2_xattr_value_root *xv;
767 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
770 xs->end = (void *)di + inode->i_sb->s_blocksize;
771 xs->header = (struct ocfs2_xattr_header *)
772 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
773 xs->base = (void *)xs->header;
774 xs->here = xs->header->xh_entries;
776 ret = ocfs2_xattr_find_entry(name_index, name, xs);
779 size = le64_to_cpu(xs->here->xe_value_size);
781 if (size > buffer_size)
783 if (ocfs2_xattr_is_local(xs->here)) {
784 memcpy(buffer, (void *)xs->base +
785 le16_to_cpu(xs->here->xe_name_offset) +
786 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
788 xv = (struct ocfs2_xattr_value_root *)
789 (xs->base + le16_to_cpu(
790 xs->here->xe_name_offset) +
791 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
792 ret = ocfs2_xattr_get_value_outside(inode, xv,
804 static int ocfs2_xattr_block_get(struct inode *inode,
809 struct ocfs2_xattr_search *xs)
811 struct ocfs2_xattr_block *xb;
812 struct ocfs2_xattr_value_root *xv;
814 int ret = -ENODATA, name_offset, name_len, block_off, i;
816 xs->bucket = ocfs2_xattr_bucket_new(inode);
823 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
834 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
835 size = le64_to_cpu(xs->here->xe_value_size);
838 if (size > buffer_size)
841 name_offset = le16_to_cpu(xs->here->xe_name_offset);
842 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
843 i = xs->here - xs->header->xh_entries;
845 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
846 ret = ocfs2_xattr_bucket_get_name_value(inode,
847 bucket_xh(xs->bucket),
851 xs->base = bucket_block(xs->bucket, block_off);
853 if (ocfs2_xattr_is_local(xs->here)) {
854 memcpy(buffer, (void *)xs->base +
855 name_offset + name_len, size);
857 xv = (struct ocfs2_xattr_value_root *)
858 (xs->base + name_offset + name_len);
859 ret = ocfs2_xattr_get_value_outside(inode, xv,
869 ocfs2_xattr_bucket_free(xs->bucket);
871 brelse(xs->xattr_bh);
878 * Copy an extended attribute into the buffer provided.
879 * Buffer is NULL to compute the size of buffer required.
881 static int ocfs2_xattr_get(struct inode *inode,
888 struct ocfs2_dinode *di = NULL;
889 struct buffer_head *di_bh = NULL;
890 struct ocfs2_inode_info *oi = OCFS2_I(inode);
891 struct ocfs2_xattr_search xis = {
892 .not_found = -ENODATA,
894 struct ocfs2_xattr_search xbs = {
895 .not_found = -ENODATA,
898 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
901 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
904 ret = ocfs2_inode_lock(inode, &di_bh, 0);
909 xis.inode_bh = xbs.inode_bh = di_bh;
910 di = (struct ocfs2_dinode *)di_bh->b_data;
912 down_read(&oi->ip_xattr_sem);
913 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
915 if (ret == -ENODATA && di->i_xattr_loc)
916 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
918 up_read(&oi->ip_xattr_sem);
919 ocfs2_inode_unlock(inode, 0);
926 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
928 struct ocfs2_xattr_value_root *xv,
932 int ret = 0, i, cp_len, credits;
933 u16 blocksize = inode->i_sb->s_blocksize;
934 u32 p_cluster, num_clusters;
935 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
936 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
938 struct buffer_head *bh = NULL;
940 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
943 * In __ocfs2_xattr_set_value_outside has already been dirtied,
944 * so we don't need to worry about whether ocfs2_extend_trans
945 * will create a new transactio for us or not.
947 credits = clusters * bpc;
948 ret = ocfs2_extend_trans(handle, credits);
954 while (cpos < clusters) {
955 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
956 &num_clusters, &xv->xr_list);
962 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
964 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
965 ret = ocfs2_read_block(inode, blkno, &bh);
971 ret = ocfs2_journal_access(handle,
974 OCFS2_JOURNAL_ACCESS_WRITE);
980 cp_len = value_len > blocksize ? blocksize : value_len;
981 memcpy(bh->b_data, value, cp_len);
984 if (cp_len < blocksize)
985 memset(bh->b_data + cp_len, 0,
988 ret = ocfs2_journal_dirty(handle, bh);
997 * XXX: do we need to empty all the following
998 * blocks in this cluster?
1003 cpos += num_clusters;
1011 static int ocfs2_xattr_cleanup(struct inode *inode,
1013 struct ocfs2_xattr_info *xi,
1014 struct ocfs2_xattr_search *xs,
1018 size_t name_len = strlen(xi->name);
1019 void *val = xs->base + offs;
1020 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1022 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1023 OCFS2_JOURNAL_ACCESS_WRITE);
1028 /* Decrease xattr count */
1029 le16_add_cpu(&xs->header->xh_count, -1);
1030 /* Remove the xattr entry and tree root which has already be set*/
1031 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1032 memset(val, 0, size);
1034 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1041 static int ocfs2_xattr_update_entry(struct inode *inode,
1043 struct ocfs2_xattr_info *xi,
1044 struct ocfs2_xattr_search *xs,
1049 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1050 OCFS2_JOURNAL_ACCESS_WRITE);
1056 xs->here->xe_name_offset = cpu_to_le16(offs);
1057 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1058 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1059 ocfs2_xattr_set_local(xs->here, 1);
1061 ocfs2_xattr_set_local(xs->here, 0);
1062 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1064 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1072 * ocfs2_xattr_set_value_outside()
1074 * Set large size value in B tree.
1076 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1077 struct ocfs2_xattr_info *xi,
1078 struct ocfs2_xattr_search *xs,
1079 struct ocfs2_xattr_set_ctxt *ctxt,
1082 size_t name_len = strlen(xi->name);
1083 void *val = xs->base + offs;
1084 struct ocfs2_xattr_value_root *xv = NULL;
1085 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1088 memset(val, 0, size);
1089 memcpy(val, xi->name, name_len);
1090 xv = (struct ocfs2_xattr_value_root *)
1091 (val + OCFS2_XATTR_SIZE(name_len));
1092 xv->xr_clusters = 0;
1093 xv->xr_last_eb_blk = 0;
1094 xv->xr_list.l_tree_depth = 0;
1095 xv->xr_list.l_count = cpu_to_le16(1);
1096 xv->xr_list.l_next_free_rec = 0;
1098 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1099 xi->value_len, ctxt);
1104 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
1109 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1110 xi->value, xi->value_len);
1118 * ocfs2_xattr_set_entry_local()
1120 * Set, replace or remove extended attribute in local.
1122 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1123 struct ocfs2_xattr_info *xi,
1124 struct ocfs2_xattr_search *xs,
1125 struct ocfs2_xattr_entry *last,
1128 size_t name_len = strlen(xi->name);
1131 if (xi->value && xs->not_found) {
1132 /* Insert the new xattr entry. */
1133 le16_add_cpu(&xs->header->xh_count, 1);
1134 ocfs2_xattr_set_type(last, xi->name_index);
1135 ocfs2_xattr_set_local(last, 1);
1136 last->xe_name_len = name_len;
1142 first_val = xs->base + min_offs;
1143 offs = le16_to_cpu(xs->here->xe_name_offset);
1144 val = xs->base + offs;
1146 if (le64_to_cpu(xs->here->xe_value_size) >
1147 OCFS2_XATTR_INLINE_SIZE)
1148 size = OCFS2_XATTR_SIZE(name_len) +
1149 OCFS2_XATTR_ROOT_SIZE;
1151 size = OCFS2_XATTR_SIZE(name_len) +
1152 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1154 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1155 OCFS2_XATTR_SIZE(xi->value_len)) {
1156 /* The old and the new value have the
1157 same size. Just replace the value. */
1158 ocfs2_xattr_set_local(xs->here, 1);
1159 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1160 /* Clear value bytes. */
1161 memset(val + OCFS2_XATTR_SIZE(name_len),
1163 OCFS2_XATTR_SIZE(xi->value_len));
1164 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1169 /* Remove the old name+value. */
1170 memmove(first_val + size, first_val, val - first_val);
1171 memset(first_val, 0, size);
1172 xs->here->xe_name_hash = 0;
1173 xs->here->xe_name_offset = 0;
1174 ocfs2_xattr_set_local(xs->here, 1);
1175 xs->here->xe_value_size = 0;
1179 /* Adjust all value offsets. */
1180 last = xs->header->xh_entries;
1181 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1182 size_t o = le16_to_cpu(last->xe_name_offset);
1185 last->xe_name_offset = cpu_to_le16(o + size);
1190 /* Remove the old entry. */
1192 memmove(xs->here, xs->here + 1,
1193 (void *)last - (void *)xs->here);
1194 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1195 le16_add_cpu(&xs->header->xh_count, -1);
1199 /* Insert the new name+value. */
1200 size_t size = OCFS2_XATTR_SIZE(name_len) +
1201 OCFS2_XATTR_SIZE(xi->value_len);
1202 void *val = xs->base + min_offs - size;
1204 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1205 memset(val, 0, size);
1206 memcpy(val, xi->name, name_len);
1207 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1210 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1211 ocfs2_xattr_set_local(xs->here, 1);
1212 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1219 * ocfs2_xattr_set_entry()
1221 * Set extended attribute entry into inode or block.
1223 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1224 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1225 * then set value in B tree with set_value_outside().
1227 static int ocfs2_xattr_set_entry(struct inode *inode,
1228 struct ocfs2_xattr_info *xi,
1229 struct ocfs2_xattr_search *xs,
1230 struct ocfs2_xattr_set_ctxt *ctxt,
1233 struct ocfs2_xattr_entry *last;
1234 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1235 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1236 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1238 handle_t *handle = ctxt->handle;
1240 struct ocfs2_xattr_info xi_l = {
1241 .name_index = xi->name_index,
1244 .value_len = xi->value_len,
1247 /* Compute min_offs, last and free space. */
1248 last = xs->header->xh_entries;
1250 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1251 size_t offs = le16_to_cpu(last->xe_name_offset);
1252 if (offs < min_offs)
1257 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1261 if (!xs->not_found) {
1263 if (ocfs2_xattr_is_local(xs->here))
1264 size = OCFS2_XATTR_SIZE(name_len) +
1265 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1267 size = OCFS2_XATTR_SIZE(name_len) +
1268 OCFS2_XATTR_ROOT_SIZE;
1269 free += (size + sizeof(struct ocfs2_xattr_entry));
1271 /* Check free space in inode or block */
1272 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1273 if (free < sizeof(struct ocfs2_xattr_entry) +
1274 OCFS2_XATTR_SIZE(name_len) +
1275 OCFS2_XATTR_ROOT_SIZE) {
1279 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1280 xi_l.value = (void *)&def_xv;
1281 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1282 } else if (xi->value) {
1283 if (free < sizeof(struct ocfs2_xattr_entry) +
1284 OCFS2_XATTR_SIZE(name_len) +
1285 OCFS2_XATTR_SIZE(xi->value_len)) {
1291 if (!xs->not_found) {
1292 /* For existing extended attribute */
1293 size_t size = OCFS2_XATTR_SIZE(name_len) +
1294 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1295 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1296 void *val = xs->base + offs;
1298 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1299 /* Replace existing local xattr with tree root */
1300 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1305 } else if (!ocfs2_xattr_is_local(xs->here)) {
1306 /* For existing xattr which has value outside */
1307 struct ocfs2_xattr_value_root *xv = NULL;
1308 xv = (struct ocfs2_xattr_value_root *)(val +
1309 OCFS2_XATTR_SIZE(name_len));
1311 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1313 * If new value need set outside also,
1314 * first truncate old value to new value,
1315 * then set new value with set_value_outside().
1317 ret = ocfs2_xattr_value_truncate(inode,
1327 ret = ocfs2_xattr_update_entry(inode,
1337 ret = __ocfs2_xattr_set_value_outside(inode,
1347 * If new value need set in local,
1348 * just trucate old value to zero.
1350 ret = ocfs2_xattr_value_truncate(inode,
1361 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1362 OCFS2_JOURNAL_ACCESS_WRITE);
1368 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1369 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1370 OCFS2_JOURNAL_ACCESS_WRITE);
1378 * Set value in local, include set tree root in local.
1379 * This is the first step for value size >INLINE_SIZE.
1381 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1383 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1384 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1391 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1392 (flag & OCFS2_INLINE_XATTR_FL)) {
1393 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1394 unsigned int xattrsize = osb->s_xattr_inline_size;
1397 * Adjust extent record count or inline data size
1398 * to reserve space for extended attribute.
1400 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1401 struct ocfs2_inline_data *idata = &di->id2.i_data;
1402 le16_add_cpu(&idata->id_count, -xattrsize);
1403 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1404 struct ocfs2_extent_list *el = &di->id2.i_list;
1405 le16_add_cpu(&el->l_count, -(xattrsize /
1406 sizeof(struct ocfs2_extent_rec)));
1408 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1410 /* Update xattr flag */
1411 spin_lock(&oi->ip_lock);
1412 oi->ip_dyn_features |= flag;
1413 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1414 spin_unlock(&oi->ip_lock);
1415 /* Update inode ctime */
1416 inode->i_ctime = CURRENT_TIME;
1417 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1418 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1420 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1424 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1426 * Set value outside in B tree.
1427 * This is the second step for value size > INLINE_SIZE.
1429 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1430 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
1436 * If set value outside failed, we have to clean
1437 * the junk tree root we have already set in local.
1439 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1449 static int ocfs2_remove_value_outside(struct inode*inode,
1450 struct buffer_head *bh,
1451 struct ocfs2_xattr_header *header)
1454 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1455 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1457 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1459 ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
1460 if (IS_ERR(ctxt.handle)) {
1461 ret = PTR_ERR(ctxt.handle);
1466 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1467 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1469 if (!ocfs2_xattr_is_local(entry)) {
1470 struct ocfs2_xattr_value_root *xv;
1473 val = (void *)header +
1474 le16_to_cpu(entry->xe_name_offset);
1475 xv = (struct ocfs2_xattr_value_root *)
1476 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1477 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1486 ocfs2_commit_trans(osb, ctxt.handle);
1487 ocfs2_schedule_truncate_log_flush(osb, 1);
1488 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1493 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1494 struct buffer_head *di_bh)
1497 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1498 struct ocfs2_xattr_header *header;
1501 header = (struct ocfs2_xattr_header *)
1502 ((void *)di + inode->i_sb->s_blocksize -
1503 le16_to_cpu(di->i_xattr_inline_size));
1505 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1510 static int ocfs2_xattr_block_remove(struct inode *inode,
1511 struct buffer_head *blk_bh)
1513 struct ocfs2_xattr_block *xb;
1516 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1517 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1518 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1519 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1521 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1526 static int ocfs2_xattr_free_block(struct inode *inode,
1529 struct inode *xb_alloc_inode;
1530 struct buffer_head *xb_alloc_bh = NULL;
1531 struct buffer_head *blk_bh = NULL;
1532 struct ocfs2_xattr_block *xb;
1533 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1539 ret = ocfs2_read_block(inode, block, &blk_bh);
1545 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1546 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1551 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1557 blk = le64_to_cpu(xb->xb_blkno);
1558 bit = le16_to_cpu(xb->xb_suballoc_bit);
1559 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1561 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1562 EXTENT_ALLOC_SYSTEM_INODE,
1563 le16_to_cpu(xb->xb_suballoc_slot));
1564 if (!xb_alloc_inode) {
1569 mutex_lock(&xb_alloc_inode->i_mutex);
1571 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1577 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1578 if (IS_ERR(handle)) {
1579 ret = PTR_ERR(handle);
1584 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1589 ocfs2_commit_trans(osb, handle);
1591 ocfs2_inode_unlock(xb_alloc_inode, 1);
1592 brelse(xb_alloc_bh);
1594 mutex_unlock(&xb_alloc_inode->i_mutex);
1595 iput(xb_alloc_inode);
1602 * ocfs2_xattr_remove()
1604 * Free extended attribute resources associated with this inode.
1606 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1608 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1609 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1613 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1616 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1619 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1620 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1627 if (di->i_xattr_loc) {
1628 ret = ocfs2_xattr_free_block(inode,
1629 le64_to_cpu(di->i_xattr_loc));
1636 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1637 OCFS2_INODE_UPDATE_CREDITS);
1638 if (IS_ERR(handle)) {
1639 ret = PTR_ERR(handle);
1643 ret = ocfs2_journal_access(handle, inode, di_bh,
1644 OCFS2_JOURNAL_ACCESS_WRITE);
1650 di->i_xattr_loc = 0;
1652 spin_lock(&oi->ip_lock);
1653 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1654 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1655 spin_unlock(&oi->ip_lock);
1657 ret = ocfs2_journal_dirty(handle, di_bh);
1661 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1666 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1667 struct ocfs2_dinode *di)
1669 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1670 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1673 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1676 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1677 struct ocfs2_inline_data *idata = &di->id2.i_data;
1678 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1679 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1680 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1681 le64_to_cpu(di->i_size);
1683 struct ocfs2_extent_list *el = &di->id2.i_list;
1684 free = (le16_to_cpu(el->l_count) -
1685 le16_to_cpu(el->l_next_free_rec)) *
1686 sizeof(struct ocfs2_extent_rec);
1688 if (free >= xattrsize)
1695 * ocfs2_xattr_ibody_find()
1697 * Find extended attribute in inode block and
1698 * fill search info into struct ocfs2_xattr_search.
1700 static int ocfs2_xattr_ibody_find(struct inode *inode,
1703 struct ocfs2_xattr_search *xs)
1705 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1706 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1710 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1713 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1714 down_read(&oi->ip_alloc_sem);
1715 has_space = ocfs2_xattr_has_space_inline(inode, di);
1716 up_read(&oi->ip_alloc_sem);
1721 xs->xattr_bh = xs->inode_bh;
1722 xs->end = (void *)di + inode->i_sb->s_blocksize;
1723 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1724 xs->header = (struct ocfs2_xattr_header *)
1725 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1727 xs->header = (struct ocfs2_xattr_header *)
1728 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1729 xs->base = (void *)xs->header;
1730 xs->here = xs->header->xh_entries;
1732 /* Find the named attribute. */
1733 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1734 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1735 if (ret && ret != -ENODATA)
1737 xs->not_found = ret;
1744 * ocfs2_xattr_ibody_set()
1746 * Set, replace or remove an extended attribute into inode block.
1749 static int ocfs2_xattr_ibody_set(struct inode *inode,
1750 struct ocfs2_xattr_info *xi,
1751 struct ocfs2_xattr_search *xs,
1752 struct ocfs2_xattr_set_ctxt *ctxt)
1754 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1755 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1758 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1761 down_write(&oi->ip_alloc_sem);
1762 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1763 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1769 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
1770 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1772 up_write(&oi->ip_alloc_sem);
1778 * ocfs2_xattr_block_find()
1780 * Find extended attribute in external block and
1781 * fill search info into struct ocfs2_xattr_search.
1783 static int ocfs2_xattr_block_find(struct inode *inode,
1786 struct ocfs2_xattr_search *xs)
1788 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1789 struct buffer_head *blk_bh = NULL;
1790 struct ocfs2_xattr_block *xb;
1793 if (!di->i_xattr_loc)
1796 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1802 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1803 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1808 xs->xattr_bh = blk_bh;
1810 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1811 xs->header = &xb->xb_attrs.xb_header;
1812 xs->base = (void *)xs->header;
1813 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1814 xs->here = xs->header->xh_entries;
1816 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1818 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1822 if (ret && ret != -ENODATA) {
1823 xs->xattr_bh = NULL;
1826 xs->not_found = ret;
1835 * ocfs2_xattr_block_set()
1837 * Set, replace or remove an extended attribute into external block.
1840 static int ocfs2_xattr_block_set(struct inode *inode,
1841 struct ocfs2_xattr_info *xi,
1842 struct ocfs2_xattr_search *xs,
1843 struct ocfs2_xattr_set_ctxt *ctxt)
1845 struct buffer_head *new_bh = NULL;
1846 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1847 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1848 handle_t *handle = ctxt->handle;
1849 struct ocfs2_xattr_block *xblk = NULL;
1850 u16 suballoc_bit_start;
1855 if (!xs->xattr_bh) {
1856 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1857 OCFS2_JOURNAL_ACCESS_CREATE);
1863 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
1864 &suballoc_bit_start, &num_got,
1871 new_bh = sb_getblk(inode->i_sb, first_blkno);
1872 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1874 ret = ocfs2_journal_access(handle, inode, new_bh,
1875 OCFS2_JOURNAL_ACCESS_CREATE);
1881 /* Initialize ocfs2_xattr_block */
1882 xs->xattr_bh = new_bh;
1883 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1884 memset(xblk, 0, inode->i_sb->s_blocksize);
1885 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1886 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1887 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1888 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1889 xblk->xb_blkno = cpu_to_le64(first_blkno);
1891 xs->header = &xblk->xb_attrs.xb_header;
1892 xs->base = (void *)xs->header;
1893 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1894 xs->here = xs->header->xh_entries;
1896 ret = ocfs2_journal_dirty(handle, new_bh);
1901 di->i_xattr_loc = cpu_to_le64(first_blkno);
1902 ocfs2_journal_dirty(handle, xs->inode_bh);
1904 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1906 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1907 /* Set extended attribute into external block */
1908 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
1909 OCFS2_HAS_XATTR_FL);
1910 if (!ret || ret != -ENOSPC)
1913 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
1918 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
1925 /* Check whether the new xattr can be inserted into the inode. */
1926 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
1927 struct ocfs2_xattr_info *xi,
1928 struct ocfs2_xattr_search *xs)
1931 struct ocfs2_xattr_entry *last;
1933 size_t min_offs = xs->end - xs->base;
1938 last = xs->header->xh_entries;
1940 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1941 size_t offs = le16_to_cpu(last->xe_name_offset);
1942 if (offs < min_offs)
1947 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1951 BUG_ON(!xs->not_found);
1953 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
1954 value_size = OCFS2_XATTR_ROOT_SIZE;
1956 value_size = OCFS2_XATTR_SIZE(xi->value_len);
1958 if (free >= sizeof(struct ocfs2_xattr_entry) +
1959 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
1965 static int ocfs2_calc_xattr_set_need(struct inode *inode,
1966 struct ocfs2_dinode *di,
1967 struct ocfs2_xattr_info *xi,
1968 struct ocfs2_xattr_search *xis,
1969 struct ocfs2_xattr_search *xbs,
1974 int ret = 0, old_in_xb = 0;
1975 int clusters_add = 0, meta_add = 0, credits = 0;
1976 struct buffer_head *bh = NULL;
1977 struct ocfs2_xattr_block *xb = NULL;
1978 struct ocfs2_xattr_entry *xe = NULL;
1979 struct ocfs2_xattr_value_root *xv = NULL;
1981 int name_offset, name_len = 0;
1982 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
1986 if (xis->not_found && xbs->not_found) {
1987 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1989 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1990 clusters_add += new_clusters;
1991 credits += ocfs2_calc_extend_credits(inode->i_sb,
1999 if (!xis->not_found) {
2001 name_offset = le16_to_cpu(xe->xe_name_offset);
2002 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2004 credits += OCFS2_INODE_UPDATE_CREDITS;
2007 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2009 name_offset = le16_to_cpu(xe->xe_name_offset);
2010 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2011 i = xbs->here - xbs->header->xh_entries;
2014 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2015 ret = ocfs2_xattr_bucket_get_name_value(inode,
2016 bucket_xh(xbs->bucket),
2019 base = bucket_block(xbs->bucket, block_off);
2020 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2023 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2028 * delete a xattr doesn't need metadata and cluster allocation.
2029 * so just calculate the credits and return.
2031 * The credits for removing the value tree will be extended
2032 * by ocfs2_remove_extent itself.
2035 if (!ocfs2_xattr_is_local(xe))
2036 credits += OCFS2_REMOVE_EXTENT_CREDITS;
2041 /* do cluster allocation guess first. */
2042 value_size = le64_to_cpu(xe->xe_value_size);
2046 * In xattr set, we always try to set the xe in inode first,
2047 * so if it can be inserted into inode successfully, the old
2048 * one will be removed from the xattr block, and this xattr
2049 * will be inserted into inode as a new xattr in inode.
2051 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2052 clusters_add += new_clusters;
2053 credits += OCFS2_REMOVE_EXTENT_CREDITS +
2054 OCFS2_INODE_UPDATE_CREDITS;
2055 if (!ocfs2_xattr_is_local(xe))
2056 credits += ocfs2_calc_extend_credits(
2064 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2065 /* the new values will be stored outside. */
2066 u32 old_clusters = 0;
2068 if (!ocfs2_xattr_is_local(xe)) {
2069 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2071 xv = (struct ocfs2_xattr_value_root *)
2072 (base + name_offset + name_len);
2076 if (old_clusters >= new_clusters) {
2077 credits += OCFS2_REMOVE_EXTENT_CREDITS;
2080 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2081 clusters_add += new_clusters - old_clusters;
2082 credits += ocfs2_calc_extend_credits(inode->i_sb,
2090 * Now the new value will be stored inside. So if the new
2091 * value is smaller than the size of value root or the old
2092 * value, we don't need any allocation, otherwise we have
2093 * to guess metadata allocation.
2095 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2096 (!ocfs2_xattr_is_local(xe) &&
2097 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2102 /* calculate metadata allocation. */
2103 if (di->i_xattr_loc) {
2104 if (!xbs->xattr_bh) {
2105 ret = ocfs2_read_block(inode,
2106 le64_to_cpu(di->i_xattr_loc),
2113 xb = (struct ocfs2_xattr_block *)bh->b_data;
2115 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2117 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2118 struct ocfs2_extent_list *el =
2119 &xb->xb_attrs.xb_root.xt_list;
2120 meta_add += ocfs2_extend_meta_needed(el);
2121 credits += ocfs2_calc_extend_credits(inode->i_sb,
2126 * This cluster will be used either for new bucket or for
2128 * If the cluster size is the same as the bucket size, one
2129 * more is needed since we may need to extend the bucket
2133 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2134 if (OCFS2_XATTR_BUCKET_SIZE ==
2135 OCFS2_SB(inode->i_sb)->s_clustersize) {
2136 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2141 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2145 *clusters_need = clusters_add;
2147 *meta_need = meta_add;
2149 *credits_need = credits;
2154 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2155 struct ocfs2_dinode *di,
2156 struct ocfs2_xattr_info *xi,
2157 struct ocfs2_xattr_search *xis,
2158 struct ocfs2_xattr_search *xbs,
2159 struct ocfs2_xattr_set_ctxt *ctxt,
2162 int clusters_add, meta_add, ret;
2163 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2165 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2167 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2169 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2170 &clusters_add, &meta_add, credits);
2176 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2177 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2180 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2189 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2195 if (ctxt->meta_ac) {
2196 ocfs2_free_alloc_context(ctxt->meta_ac);
2197 ctxt->meta_ac = NULL;
2201 * We cannot have an error and a non null ctxt->data_ac.
2208 static int __ocfs2_xattr_set_handle(struct inode *inode,
2209 struct ocfs2_dinode *di,
2210 struct ocfs2_xattr_info *xi,
2211 struct ocfs2_xattr_search *xis,
2212 struct ocfs2_xattr_search *xbs,
2213 struct ocfs2_xattr_set_ctxt *ctxt)
2215 int ret = 0, credits;
2218 /* Remove existing extended attribute */
2219 if (!xis->not_found)
2220 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2221 else if (!xbs->not_found)
2222 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2224 /* We always try to set extended attribute into inode first*/
2225 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2226 if (!ret && !xbs->not_found) {
2228 * If succeed and that extended attribute existing in
2229 * external block, then we will remove it.
2234 xis->not_found = -ENODATA;
2235 ret = ocfs2_calc_xattr_set_need(inode,
2248 ret = ocfs2_extend_trans(ctxt->handle, credits +
2249 ctxt->handle->h_buffer_credits);
2254 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2255 } else if (ret == -ENOSPC) {
2256 if (di->i_xattr_loc && !xbs->xattr_bh) {
2257 ret = ocfs2_xattr_block_find(inode,
2263 xis->not_found = -ENODATA;
2264 ret = ocfs2_calc_xattr_set_need(inode,
2277 ret = ocfs2_extend_trans(ctxt->handle, credits +
2278 ctxt->handle->h_buffer_credits);
2285 * If no space in inode, we will set extended attribute
2286 * into external block.
2288 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2291 if (!xis->not_found) {
2293 * If succeed and that extended attribute
2294 * existing in inode, we will remove it.
2298 xbs->not_found = -ENODATA;
2299 ret = ocfs2_calc_xattr_set_need(inode,
2312 ret = ocfs2_extend_trans(ctxt->handle, credits +
2313 ctxt->handle->h_buffer_credits);
2318 ret = ocfs2_xattr_ibody_set(inode, xi,
2331 * Set, replace or remove an extended attribute for this inode.
2332 * value is NULL to remove an existing extended attribute, else either
2333 * create or replace an extended attribute.
2335 int ocfs2_xattr_set(struct inode *inode,
2342 struct buffer_head *di_bh = NULL;
2343 struct ocfs2_dinode *di;
2345 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2346 struct inode *tl_inode = osb->osb_tl_inode;
2347 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2349 struct ocfs2_xattr_info xi = {
2350 .name_index = name_index,
2353 .value_len = value_len,
2356 struct ocfs2_xattr_search xis = {
2357 .not_found = -ENODATA,
2360 struct ocfs2_xattr_search xbs = {
2361 .not_found = -ENODATA,
2364 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2368 * Only xbs will be used on indexed trees. xis doesn't need a
2371 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2373 mlog_errno(-ENOMEM);
2377 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2380 goto cleanup_nolock;
2382 xis.inode_bh = xbs.inode_bh = di_bh;
2383 di = (struct ocfs2_dinode *)di_bh->b_data;
2385 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2387 * Scan inode and external block to find the same name
2388 * extended attribute and collect search infomation.
2390 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2393 if (xis.not_found) {
2394 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2399 if (xis.not_found && xbs.not_found) {
2401 if (flags & XATTR_REPLACE)
2408 if (flags & XATTR_CREATE)
2413 mutex_lock(&tl_inode->i_mutex);
2415 if (ocfs2_truncate_log_needs_flush(osb)) {
2416 ret = __ocfs2_flush_truncate_log(osb);
2418 mutex_unlock(&tl_inode->i_mutex);
2423 mutex_unlock(&tl_inode->i_mutex);
2425 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2426 &xbs, &ctxt, &credits);
2432 ctxt.handle = ocfs2_start_trans(osb, credits);
2433 if (IS_ERR(ctxt.handle)) {
2434 ret = PTR_ERR(ctxt.handle);
2439 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2441 ocfs2_commit_trans(osb, ctxt.handle);
2444 ocfs2_free_alloc_context(ctxt.data_ac);
2446 ocfs2_free_alloc_context(ctxt.meta_ac);
2447 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2448 ocfs2_schedule_truncate_log_flush(osb, 1);
2449 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2451 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2452 ocfs2_inode_unlock(inode, 1);
2455 brelse(xbs.xattr_bh);
2456 ocfs2_xattr_bucket_free(xbs.bucket);
2462 * Find the xattr extent rec which may contains name_hash.
2463 * e_cpos will be the first name hash of the xattr rec.
2464 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2466 static int ocfs2_xattr_get_rec(struct inode *inode,
2471 struct ocfs2_extent_list *el)
2474 struct buffer_head *eb_bh = NULL;
2475 struct ocfs2_extent_block *eb;
2476 struct ocfs2_extent_rec *rec = NULL;
2479 if (el->l_tree_depth) {
2480 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2486 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2489 if (el->l_tree_depth) {
2490 ocfs2_error(inode->i_sb,
2491 "Inode %lu has non zero tree depth in "
2492 "xattr tree block %llu\n", inode->i_ino,
2493 (unsigned long long)eb_bh->b_blocknr);
2499 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2500 rec = &el->l_recs[i];
2502 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2503 e_blkno = le64_to_cpu(rec->e_blkno);
2509 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2510 "record (%u, %u, 0) in xattr", inode->i_ino,
2511 le32_to_cpu(rec->e_cpos),
2512 ocfs2_rec_clusters(el, rec));
2517 *p_blkno = le64_to_cpu(rec->e_blkno);
2518 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2520 *e_cpos = le32_to_cpu(rec->e_cpos);
2526 typedef int (xattr_bucket_func)(struct inode *inode,
2527 struct ocfs2_xattr_bucket *bucket,
2530 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2531 struct ocfs2_xattr_bucket *bucket,
2538 int i, ret = 0, cmp = 1, block_off, new_offset;
2539 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
2540 size_t name_len = strlen(name);
2541 struct ocfs2_xattr_entry *xe = NULL;
2545 * We don't use binary search in the bucket because there
2546 * may be multiple entries with the same name hash.
2548 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2549 xe = &xh->xh_entries[i];
2551 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2553 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2556 cmp = name_index - ocfs2_xattr_get_type(xe);
2558 cmp = name_len - xe->xe_name_len;
2562 ret = ocfs2_xattr_bucket_get_name_value(inode,
2572 xe_name = bucket_block(bucket, block_off) + new_offset;
2573 if (!memcmp(name, xe_name, name_len)) {
2585 * Find the specified xattr entry in a series of buckets.
2586 * This series start from p_blkno and last for num_clusters.
2587 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2588 * the num of the valid buckets.
2590 * Return the buffer_head this xattr should reside in. And if the xattr's
2591 * hash is in the gap of 2 buckets, return the lower bucket.
2593 static int ocfs2_xattr_bucket_find(struct inode *inode,
2600 struct ocfs2_xattr_search *xs)
2603 struct ocfs2_xattr_header *xh = NULL;
2604 struct ocfs2_xattr_entry *xe = NULL;
2606 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2607 int low_bucket = 0, bucket, high_bucket;
2608 struct ocfs2_xattr_bucket *search;
2610 u64 blkno, lower_blkno = 0;
2612 search = ocfs2_xattr_bucket_new(inode);
2619 ret = ocfs2_read_xattr_bucket(search, p_blkno);
2625 xh = bucket_xh(search);
2626 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2627 while (low_bucket <= high_bucket) {
2628 ocfs2_xattr_bucket_relse(search);
2630 bucket = (low_bucket + high_bucket) / 2;
2631 blkno = p_blkno + bucket * blk_per_bucket;
2632 ret = ocfs2_read_xattr_bucket(search, blkno);
2638 xh = bucket_xh(search);
2639 xe = &xh->xh_entries[0];
2640 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2641 high_bucket = bucket - 1;
2646 * Check whether the hash of the last entry in our
2647 * bucket is larger than the search one. for an empty
2648 * bucket, the last one is also the first one.
2651 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2653 last_hash = le32_to_cpu(xe->xe_name_hash);
2655 /* record lower_blkno which may be the insert place. */
2656 lower_blkno = blkno;
2658 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2659 low_bucket = bucket + 1;
2663 /* the searched xattr should reside in this bucket if exists. */
2664 ret = ocfs2_find_xe_in_bucket(inode, search,
2665 name_index, name, name_hash,
2675 * Record the bucket we have found.
2676 * When the xattr's hash value is in the gap of 2 buckets, we will
2677 * always set it to the previous bucket.
2680 lower_blkno = p_blkno;
2682 /* This should be in cache - we just read it during the search */
2683 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2689 xs->header = bucket_xh(xs->bucket);
2690 xs->base = bucket_block(xs->bucket, 0);
2691 xs->end = xs->base + inode->i_sb->s_blocksize;
2694 xs->here = &xs->header->xh_entries[index];
2695 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2696 (unsigned long long)bucket_blkno(xs->bucket), index);
2701 ocfs2_xattr_bucket_free(search);
2705 static int ocfs2_xattr_index_block_find(struct inode *inode,
2706 struct buffer_head *root_bh,
2709 struct ocfs2_xattr_search *xs)
2712 struct ocfs2_xattr_block *xb =
2713 (struct ocfs2_xattr_block *)root_bh->b_data;
2714 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2715 struct ocfs2_extent_list *el = &xb_root->xt_list;
2717 u32 first_hash, num_clusters = 0;
2718 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2720 if (le16_to_cpu(el->l_next_free_rec) == 0)
2723 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2724 name, name_hash, name_index);
2726 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2733 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2735 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2736 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
2739 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2740 p_blkno, first_hash, num_clusters, xs);
2746 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2749 xattr_bucket_func *func,
2753 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2754 u32 num_buckets = clusters * bpc;
2755 struct ocfs2_xattr_bucket *bucket;
2757 bucket = ocfs2_xattr_bucket_new(inode);
2759 mlog_errno(-ENOMEM);
2763 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2764 clusters, (unsigned long long)blkno);
2766 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
2767 ret = ocfs2_read_xattr_bucket(bucket, blkno);
2774 * The real bucket num in this series of blocks is stored
2775 * in the 1st bucket.
2778 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
2780 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2781 (unsigned long long)blkno,
2782 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
2784 ret = func(inode, bucket, para);
2787 /* Fall through to bucket_relse() */
2790 ocfs2_xattr_bucket_relse(bucket);
2795 ocfs2_xattr_bucket_free(bucket);
2799 struct ocfs2_xattr_tree_list {
2805 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2806 struct ocfs2_xattr_header *xh,
2813 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2816 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2818 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2819 *new_offset = name_offset % inode->i_sb->s_blocksize;
2824 static int ocfs2_list_xattr_bucket(struct inode *inode,
2825 struct ocfs2_xattr_bucket *bucket,
2829 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2830 int i, block_off, new_offset;
2831 const char *prefix, *name;
2833 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
2834 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
2835 type = ocfs2_xattr_get_type(entry);
2836 prefix = ocfs2_xattr_prefix(type);
2839 ret = ocfs2_xattr_bucket_get_name_value(inode,
2847 name = (const char *)bucket_block(bucket, block_off) +
2849 ret = ocfs2_xattr_list_entry(xl->buffer,
2853 entry->xe_name_len);
2862 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2863 struct ocfs2_xattr_tree_root *xt,
2867 struct ocfs2_extent_list *el = &xt->xt_list;
2869 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2871 struct ocfs2_xattr_tree_list xl = {
2873 .buffer_size = buffer_size,
2877 if (le16_to_cpu(el->l_next_free_rec) == 0)
2880 while (name_hash > 0) {
2881 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2882 &e_cpos, &num_clusters, el);
2888 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2889 ocfs2_list_xattr_bucket,
2899 name_hash = e_cpos - 1;
2907 static int cmp_xe(const void *a, const void *b)
2909 const struct ocfs2_xattr_entry *l = a, *r = b;
2910 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2911 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2913 if (l_hash > r_hash)
2915 if (l_hash < r_hash)
2920 static void swap_xe(void *a, void *b, int size)
2922 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2925 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2926 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2930 * When the ocfs2_xattr_block is filled up, new bucket will be created
2931 * and all the xattr entries will be moved to the new bucket.
2932 * The header goes at the start of the bucket, and the names+values are
2933 * filled from the end. This is why *target starts as the last buffer.
2934 * Note: we need to sort the entries since they are not saved in order
2935 * in the ocfs2_xattr_block.
2937 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2938 struct buffer_head *xb_bh,
2939 struct ocfs2_xattr_bucket *bucket)
2941 int i, blocksize = inode->i_sb->s_blocksize;
2942 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2943 u16 offset, size, off_change;
2944 struct ocfs2_xattr_entry *xe;
2945 struct ocfs2_xattr_block *xb =
2946 (struct ocfs2_xattr_block *)xb_bh->b_data;
2947 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2948 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
2949 u16 count = le16_to_cpu(xb_xh->xh_count);
2950 char *src = xb_bh->b_data;
2951 char *target = bucket_block(bucket, blks - 1);
2953 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2954 (unsigned long long)xb_bh->b_blocknr,
2955 (unsigned long long)bucket_blkno(bucket));
2957 for (i = 0; i < blks; i++)
2958 memset(bucket_block(bucket, i), 0, blocksize);
2961 * Since the xe_name_offset is based on ocfs2_xattr_header,
2962 * there is a offset change corresponding to the change of
2963 * ocfs2_xattr_header's position.
2965 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2966 xe = &xb_xh->xh_entries[count - 1];
2967 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2968 size = blocksize - offset;
2970 /* copy all the names and values. */
2971 memcpy(target + offset, src + offset, size);
2973 /* Init new header now. */
2974 xh->xh_count = xb_xh->xh_count;
2975 xh->xh_num_buckets = cpu_to_le16(1);
2976 xh->xh_name_value_len = cpu_to_le16(size);
2977 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2979 /* copy all the entries. */
2980 target = bucket_block(bucket, 0);
2981 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2982 size = count * sizeof(struct ocfs2_xattr_entry);
2983 memcpy(target + offset, (char *)xb_xh + offset, size);
2985 /* Change the xe offset for all the xe because of the move. */
2986 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2987 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2988 for (i = 0; i < count; i++)
2989 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2991 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2992 offset, size, off_change);
2994 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2999 * After we move xattr from block to index btree, we have to
3000 * update ocfs2_xattr_search to the new xe and base.
3002 * When the entry is in xattr block, xattr_bh indicates the storage place.
3003 * While if the entry is in index b-tree, "bucket" indicates the
3004 * real place of the xattr.
3006 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3007 struct ocfs2_xattr_search *xs,
3008 struct buffer_head *old_bh)
3010 char *buf = old_bh->b_data;
3011 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3012 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
3015 xs->header = bucket_xh(xs->bucket);
3016 xs->base = bucket_block(xs->bucket, 0);
3017 xs->end = xs->base + inode->i_sb->s_blocksize;
3022 i = xs->here - old_xh->xh_entries;
3023 xs->here = &xs->header->xh_entries[i];
3026 static int ocfs2_xattr_create_index_block(struct inode *inode,
3027 struct ocfs2_xattr_search *xs,
3028 struct ocfs2_xattr_set_ctxt *ctxt)
3033 handle_t *handle = ctxt->handle;
3034 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3035 struct ocfs2_inode_info *oi = OCFS2_I(inode);
3036 struct buffer_head *xb_bh = xs->xattr_bh;
3037 struct ocfs2_xattr_block *xb =
3038 (struct ocfs2_xattr_block *)xb_bh->b_data;
3039 struct ocfs2_xattr_tree_root *xr;
3040 u16 xb_flags = le16_to_cpu(xb->xb_flags);
3042 mlog(0, "create xattr index block for %llu\n",
3043 (unsigned long long)xb_bh->b_blocknr);
3045 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
3046 BUG_ON(!xs->bucket);
3050 * We can use this lock for now, and maybe move to a dedicated mutex
3051 * if performance becomes a problem later.
3053 down_write(&oi->ip_alloc_sem);
3055 ret = ocfs2_journal_access(handle, inode, xb_bh,
3056 OCFS2_JOURNAL_ACCESS_WRITE);
3062 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3063 1, 1, &bit_off, &len);
3070 * The bucket may spread in many blocks, and
3071 * we will only touch the 1st block and the last block
3072 * in the whole bucket(one for entry and one for data).
3074 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3076 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3077 (unsigned long long)blkno);
3079 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
3085 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3086 OCFS2_JOURNAL_ACCESS_CREATE);
3092 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3093 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3095 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3097 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3098 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3099 offsetof(struct ocfs2_xattr_block, xb_attrs));
3101 xr = &xb->xb_attrs.xb_root;
3102 xr->xt_clusters = cpu_to_le32(1);
3103 xr->xt_last_eb_blk = 0;
3104 xr->xt_list.l_tree_depth = 0;
3105 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3106 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3108 xr->xt_list.l_recs[0].e_cpos = 0;
3109 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3110 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3112 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3114 ocfs2_journal_dirty(handle, xb_bh);
3117 up_write(&oi->ip_alloc_sem);
3122 static int cmp_xe_offset(const void *a, const void *b)
3124 const struct ocfs2_xattr_entry *l = a, *r = b;
3125 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3126 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3128 if (l_name_offset < r_name_offset)
3130 if (l_name_offset > r_name_offset)
3136 * defrag a xattr bucket if we find that the bucket has some
3137 * holes beteen name/value pairs.
3138 * We will move all the name/value pairs to the end of the bucket
3139 * so that we can spare some space for insertion.
3141 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3143 struct ocfs2_xattr_bucket *bucket)
3146 size_t end, offset, len, value_len;
3147 struct ocfs2_xattr_header *xh;
3148 char *entries, *buf, *bucket_buf = NULL;
3149 u64 blkno = bucket_blkno(bucket);
3151 size_t blocksize = inode->i_sb->s_blocksize;
3152 struct ocfs2_xattr_entry *xe;
3155 * In order to make the operation more efficient and generic,
3156 * we copy all the blocks into a contiguous memory and do the
3157 * defragment there, so if anything is error, we will not touch
3160 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3167 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3168 memcpy(buf, bucket_block(bucket, i), blocksize);
3170 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
3171 OCFS2_JOURNAL_ACCESS_WRITE);
3177 xh = (struct ocfs2_xattr_header *)bucket_buf;
3178 entries = (char *)xh->xh_entries;
3179 xh_free_start = le16_to_cpu(xh->xh_free_start);
3181 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3182 "xh_free_start = %u, xh_name_value_len = %u.\n",
3183 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3184 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
3187 * sort all the entries by their offset.
3188 * the largest will be the first, so that we can
3189 * move them to the end one by one.
3191 sort(entries, le16_to_cpu(xh->xh_count),
3192 sizeof(struct ocfs2_xattr_entry),
3193 cmp_xe_offset, swap_xe);
3195 /* Move all name/values to the end of the bucket. */
3196 xe = xh->xh_entries;
3197 end = OCFS2_XATTR_BUCKET_SIZE;
3198 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3199 offset = le16_to_cpu(xe->xe_name_offset);
3200 if (ocfs2_xattr_is_local(xe))
3201 value_len = OCFS2_XATTR_SIZE(
3202 le64_to_cpu(xe->xe_value_size));
3204 value_len = OCFS2_XATTR_ROOT_SIZE;
3205 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3208 * We must make sure that the name/value pair
3209 * exist in the same block. So adjust end to
3210 * the previous block end if needed.
3212 if (((end - len) / blocksize !=
3213 (end - 1) / blocksize))
3214 end = end - end % blocksize;
3216 if (end > offset + len) {
3217 memmove(bucket_buf + end - len,
3218 bucket_buf + offset, len);
3219 xe->xe_name_offset = cpu_to_le16(end - len);
3222 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3223 "bucket %llu\n", (unsigned long long)blkno);
3228 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3229 "bucket %llu\n", (unsigned long long)blkno);
3231 if (xh_free_start == end)
3234 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3235 xh->xh_free_start = cpu_to_le16(end);
3237 /* sort the entries by their name_hash. */
3238 sort(entries, le16_to_cpu(xh->xh_count),
3239 sizeof(struct ocfs2_xattr_entry),
3243 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3244 memcpy(bucket_block(bucket, i), buf, blocksize);
3245 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
3253 * Move half nums of the xattr bucket in the previous cluster to this new
3254 * cluster. We only touch the last cluster of the previous extend record.
3256 * first_bh is the first buffer_head of a series of bucket in the same
3257 * extent rec and header_bh is the header of one bucket in this cluster.
3258 * They will be updated if we move the data header_bh contains to the new
3259 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3261 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3263 struct buffer_head **first_bh,
3264 struct buffer_head **header_bh,
3270 int i, ret, credits;
3271 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3272 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3273 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3274 int blocksize = inode->i_sb->s_blocksize;
3275 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3276 struct ocfs2_xattr_header *new_xh;
3277 struct ocfs2_xattr_header *xh =
3278 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3280 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3281 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3283 prev_bh = *first_bh;
3285 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3287 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3289 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3290 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
3293 * We need to update the 1st half of the new cluster and
3294 * 1 more for the update of the 1st bucket of the previous
3297 credits = bpc / 2 + 1 + handle->h_buffer_credits;
3298 ret = ocfs2_extend_trans(handle, credits);
3304 ret = ocfs2_journal_access(handle, inode, prev_bh,
3305 OCFS2_JOURNAL_ACCESS_WRITE);
3311 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3312 old_bh = new_bh = NULL;
3313 new_bh = sb_getblk(inode->i_sb, new_blkno);
3320 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3322 ret = ocfs2_journal_access(handle, inode, new_bh,
3323 OCFS2_JOURNAL_ACCESS_CREATE);
3330 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3337 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3340 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3341 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3344 *first_hash = le32_to_cpu(
3345 new_xh->xh_entries[0].xe_name_hash);
3346 new_first_bh = new_bh;
3347 get_bh(new_first_bh);
3350 ocfs2_journal_dirty(handle, new_bh);
3352 if (*header_bh == old_bh) {
3354 *header_bh = new_bh;
3358 *first_bh = new_first_bh;
3365 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3367 ocfs2_journal_dirty(handle, prev_bh);
3370 brelse(new_first_bh);
3375 * Find the suitable pos when we divide a bucket into 2.
3376 * We have to make sure the xattrs with the same hash value exist
3377 * in the same bucket.
3379 * If this ocfs2_xattr_header covers more than one hash value, find a
3380 * place where the hash value changes. Try to find the most even split.
3381 * The most common case is that all entries have different hash values,
3382 * and the first check we make will find a place to split.
3384 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3386 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3387 int count = le16_to_cpu(xh->xh_count);
3388 int delta, middle = count / 2;
3391 * We start at the middle. Each step gets farther away in both
3392 * directions. We therefore hit the change in hash value
3393 * nearest to the middle. Note that this loop does not execute for
3396 for (delta = 0; delta < middle; delta++) {
3397 /* Let's check delta earlier than middle */
3398 if (cmp_xe(&entries[middle - delta - 1],
3399 &entries[middle - delta]))
3400 return middle - delta;
3402 /* For even counts, don't walk off the end */
3403 if ((middle + delta + 1) == count)
3406 /* Now try delta past middle */
3407 if (cmp_xe(&entries[middle + delta],
3408 &entries[middle + delta + 1]))
3409 return middle + delta + 1;
3412 /* Every entry had the same hash */
3417 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3418 * first_hash will record the 1st hash of the new bucket.
3420 * Normally half of the xattrs will be moved. But we have to make
3421 * sure that the xattrs with the same hash value are stored in the
3422 * same bucket. If all the xattrs in this bucket have the same hash
3423 * value, the new bucket will be initialized as an empty one and the
3424 * first_hash will be initialized as (hash_value+1).
3426 static int ocfs2_divide_xattr_bucket(struct inode *inode,
3431 int new_bucket_head)
3434 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
3435 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3436 struct ocfs2_xattr_header *xh;
3437 struct ocfs2_xattr_entry *xe;
3438 int blocksize = inode->i_sb->s_blocksize;
3440 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3441 (unsigned long long)blk, (unsigned long long)new_blk);
3443 s_bucket = ocfs2_xattr_bucket_new(inode);
3444 t_bucket = ocfs2_xattr_bucket_new(inode);
3445 if (!s_bucket || !t_bucket) {
3451 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
3457 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
3458 OCFS2_JOURNAL_ACCESS_WRITE);
3465 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3466 * there's no need to read it.
3468 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
3474 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3476 OCFS2_JOURNAL_ACCESS_CREATE :
3477 OCFS2_JOURNAL_ACCESS_WRITE);
3483 xh = bucket_xh(s_bucket);
3484 count = le16_to_cpu(xh->xh_count);
3485 start = ocfs2_xattr_find_divide_pos(xh);
3487 if (start == count) {
3488 xe = &xh->xh_entries[start-1];
3491 * initialized a new empty bucket here.
3492 * The hash value is set as one larger than
3493 * that of the last entry in the previous bucket.
3495 for (i = 0; i < t_bucket->bu_blocks; i++)
3496 memset(bucket_block(t_bucket, i), 0, blocksize);
3498 xh = bucket_xh(t_bucket);
3499 xh->xh_free_start = cpu_to_le16(blocksize);
3500 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3501 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3503 goto set_num_buckets;
3506 /* copy the whole bucket to the new first. */
3507 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3509 /* update the new bucket. */
3510 xh = bucket_xh(t_bucket);
3513 * Calculate the total name/value len and xh_free_start for
3514 * the old bucket first.
3516 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3518 for (i = 0; i < start; i++) {
3519 xe = &xh->xh_entries[i];
3520 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3521 if (ocfs2_xattr_is_local(xe))
3523 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3525 xe_len += OCFS2_XATTR_ROOT_SIZE;
3526 name_value_len += xe_len;
3527 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3528 name_offset = le16_to_cpu(xe->xe_name_offset);
3532 * Now begin the modification to the new bucket.
3534 * In the new bucket, We just move the xattr entry to the beginning
3535 * and don't touch the name/value. So there will be some holes in the
3536 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3539 xe = &xh->xh_entries[start];
3540 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3541 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3542 (int)((char *)xe - (char *)xh),
3543 (int)((char *)xh->xh_entries - (char *)xh));
3544 memmove((char *)xh->xh_entries, (char *)xe, len);
3545 xe = &xh->xh_entries[count - start];
3546 len = sizeof(struct ocfs2_xattr_entry) * start;
3547 memset((char *)xe, 0, len);
3549 le16_add_cpu(&xh->xh_count, -start);
3550 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3552 /* Calculate xh_free_start for the new bucket. */
3553 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3554 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3555 xe = &xh->xh_entries[i];
3556 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3557 if (ocfs2_xattr_is_local(xe))
3559 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3561 xe_len += OCFS2_XATTR_ROOT_SIZE;
3562 if (le16_to_cpu(xe->xe_name_offset) <
3563 le16_to_cpu(xh->xh_free_start))
3564 xh->xh_free_start = xe->xe_name_offset;
3568 /* set xh->xh_num_buckets for the new xh. */
3569 if (new_bucket_head)
3570 xh->xh_num_buckets = cpu_to_le16(1);
3572 xh->xh_num_buckets = 0;
3574 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3576 /* store the first_hash of the new bucket. */
3578 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3581 * Now only update the 1st block of the old bucket. If we
3582 * just added a new empty bucket, there is no need to modify
3588 xh = bucket_xh(s_bucket);
3589 memset(&xh->xh_entries[start], 0,
3590 sizeof(struct ocfs2_xattr_entry) * (count - start));
3591 xh->xh_count = cpu_to_le16(start);
3592 xh->xh_free_start = cpu_to_le16(name_offset);
3593 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3595 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
3598 ocfs2_xattr_bucket_free(s_bucket);
3599 ocfs2_xattr_bucket_free(t_bucket);
3605 * Copy xattr from one bucket to another bucket.
3607 * The caller must make sure that the journal transaction
3608 * has enough space for journaling.
3610 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3617 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
3619 BUG_ON(s_blkno == t_blkno);
3621 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3622 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3625 s_bucket = ocfs2_xattr_bucket_new(inode);
3626 t_bucket = ocfs2_xattr_bucket_new(inode);
3627 if (!s_bucket || !t_bucket) {
3633 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
3638 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3639 * there's no need to read it.
3641 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
3645 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
3647 OCFS2_JOURNAL_ACCESS_CREATE :
3648 OCFS2_JOURNAL_ACCESS_WRITE);
3652 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3653 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
3656 ocfs2_xattr_bucket_free(t_bucket);
3657 ocfs2_xattr_bucket_free(s_bucket);
3663 * Copy one xattr cluster from src_blk to to_blk.
3664 * The to_blk will become the first bucket header of the cluster, so its
3665 * xh_num_buckets will be initialized as the bucket num in the cluster.
3667 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3669 struct buffer_head *first_bh,
3674 int i, ret, credits;
3675 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3676 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3677 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3678 struct buffer_head *bh = NULL;
3679 struct ocfs2_xattr_header *xh;
3680 u64 to_blk_start = to_blk;
3682 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3683 (unsigned long long)src_blk, (unsigned long long)to_blk);
3686 * We need to update the new cluster and 1 more for the update of
3687 * the 1st bucket of the previous extent rec.
3689 credits = bpc + 1 + handle->h_buffer_credits;
3690 ret = ocfs2_extend_trans(handle, credits);
3696 ret = ocfs2_journal_access(handle, inode, first_bh,
3697 OCFS2_JOURNAL_ACCESS_WRITE);
3703 for (i = 0; i < num_buckets; i++) {
3704 ret = ocfs2_cp_xattr_bucket(inode, handle,
3705 src_blk, to_blk, 1);
3711 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3712 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3715 /* update the old bucket header. */
3716 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3717 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3719 ocfs2_journal_dirty(handle, first_bh);
3721 /* update the new bucket header. */
3722 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3728 ret = ocfs2_journal_access(handle, inode, bh,
3729 OCFS2_JOURNAL_ACCESS_WRITE);
3735 xh = (struct ocfs2_xattr_header *)bh->b_data;
3736 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3738 ocfs2_journal_dirty(handle, bh);
3741 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3748 * Move some xattrs in this cluster to the new cluster.
3749 * This function should only be called when bucket size == cluster size.
3750 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3752 static int ocfs2_divide_xattr_cluster(struct inode *inode,
3758 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3759 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
3761 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3763 ret = ocfs2_extend_trans(handle, credits);
3769 /* Move half of the xattr in start_blk to the next bucket. */
3770 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
3771 new_blk, first_hash, 1);
3775 * Move some xattrs from the old cluster to the new one since they are not
3776 * contiguous in ocfs2 xattr tree.
3778 * new_blk starts a new separate cluster, and we will move some xattrs from
3779 * prev_blk to it. v_start will be set as the first name hash value in this
3780 * new cluster so that it can be used as e_cpos during tree insertion and
3781 * don't collide with our original b-tree operations. first_bh and header_bh
3782 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3783 * to extend the insert bucket.
3785 * The problem is how much xattr should we move to the new one and when should
3786 * we update first_bh and header_bh?
3787 * 1. If cluster size > bucket size, that means the previous cluster has more
3788 * than 1 bucket, so just move half nums of bucket into the new cluster and
3789 * update the first_bh and header_bh if the insert bucket has been moved
3790 * to the new cluster.
3791 * 2. If cluster_size == bucket_size:
3792 * a) If the previous extent rec has more than one cluster and the insert
3793 * place isn't in the last cluster, copy the entire last cluster to the
3794 * new one. This time, we don't need to upate the first_bh and header_bh
3795 * since they will not be moved into the new cluster.
3796 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3797 * the new one. And we set the extend flag to zero if the insert place is
3798 * moved into the new allocated cluster since no extend is needed.
3800 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3802 struct buffer_head **first_bh,
3803 struct buffer_head **header_bh,
3811 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3813 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3814 (unsigned long long)prev_blk, prev_clusters,
3815 (unsigned long long)new_blk);
3817 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3818 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3827 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3829 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3830 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3834 ret = ocfs2_divide_xattr_cluster(inode, handle,
3838 if ((*header_bh)->b_blocknr == last_blk && extend)
3847 * Add a new cluster for xattr storage.
3849 * If the new cluster is contiguous with the previous one, it will be
3850 * appended to the same extent record, and num_clusters will be updated.
3851 * If not, we will insert a new extent for it and move some xattrs in
3852 * the last cluster into the new allocated one.
3853 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3854 * lose the benefits of hashing because we'll have to search large leaves.
3855 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3858 * first_bh is the first block of the previous extent rec and header_bh
3859 * indicates the bucket we will insert the new xattrs. They will be updated
3860 * when the header_bh is moved into the new cluster.
3862 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3863 struct buffer_head *root_bh,
3864 struct buffer_head **first_bh,
3865 struct buffer_head **header_bh,
3870 struct ocfs2_xattr_set_ctxt *ctxt)
3873 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3874 u32 prev_clusters = *num_clusters;
3875 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3877 handle_t *handle = ctxt->handle;
3878 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3879 struct ocfs2_extent_tree et;
3881 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3882 "previous xattr blkno = %llu\n",
3883 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3884 prev_cpos, (unsigned long long)prev_blkno);
3886 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3888 ret = ocfs2_journal_access(handle, inode, root_bh,
3889 OCFS2_JOURNAL_ACCESS_WRITE);
3895 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
3896 clusters_to_add, &bit_off, &num_bits);
3903 BUG_ON(num_bits > clusters_to_add);
3905 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3906 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3907 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3909 if (prev_blkno + prev_clusters * bpc == block &&
3910 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3911 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3913 * If this cluster is contiguous with the old one and
3914 * adding this new cluster, we don't surpass the limit of
3915 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3916 * initialized and used like other buckets in the previous
3918 * So add it as a contiguous one. The caller will handle
3921 v_start = prev_cpos + prev_clusters;
3922 *num_clusters = prev_clusters + num_bits;
3923 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3926 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3941 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3942 num_bits, (unsigned long long)block, v_start);
3943 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3944 num_bits, 0, ctxt->meta_ac);
3950 ret = ocfs2_journal_dirty(handle, root_bh);
3959 * Extend a new xattr bucket and move xattrs to the end one by one until
3960 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3962 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3964 struct buffer_head *first_bh,
3965 struct buffer_head *start_bh,
3969 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3970 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3971 u64 start_blk = start_bh->b_blocknr, end_blk;
3972 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3973 struct ocfs2_xattr_header *first_xh =
3974 (struct ocfs2_xattr_header *)first_bh->b_data;
3975 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3977 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3978 "from %llu, len = %u\n", (unsigned long long)start_blk,
3979 (unsigned long long)first_bh->b_blocknr, num_clusters);
3981 BUG_ON(bucket >= num_buckets);
3983 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3986 * We will touch all the buckets after the start_bh(include it).
3987 * Then we add one more bucket.
3989 credits = end_blk - start_blk + 3 * blk_per_bucket + 1 +
3990 handle->h_buffer_credits;
3991 ret = ocfs2_extend_trans(handle, credits);
3997 ret = ocfs2_journal_access(handle, inode, first_bh,
3998 OCFS2_JOURNAL_ACCESS_WRITE);
4004 while (end_blk != start_blk) {
4005 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4006 end_blk + blk_per_bucket, 0);
4009 end_blk -= blk_per_bucket;
4012 /* Move half of the xattr in start_blk to the next bucket. */
4013 ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
4014 start_blk + blk_per_bucket, NULL, 0);
4016 le16_add_cpu(&first_xh->xh_num_buckets, 1);
4017 ocfs2_journal_dirty(handle, first_bh);
4024 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
4025 * xb_bh is the ocfs2_xattr_block.
4026 * We will move all the buckets starting from header_bh to the next place. As
4027 * for this one, half num of its xattrs will be moved to the next one.
4029 * We will allocate a new cluster if current cluster is full and adjust
4030 * header_bh and first_bh if the insert place is moved to the new cluster.
4032 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4033 struct buffer_head *xb_bh,
4034 struct buffer_head *header_bh,
4035 struct ocfs2_xattr_set_ctxt *ctxt)
4037 struct ocfs2_xattr_header *first_xh = NULL;
4038 struct buffer_head *first_bh = NULL;
4039 struct ocfs2_xattr_block *xb =
4040 (struct ocfs2_xattr_block *)xb_bh->b_data;
4041 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4042 struct ocfs2_extent_list *el = &xb_root->xt_list;
4043 struct ocfs2_xattr_header *xh =
4044 (struct ocfs2_xattr_header *)header_bh->b_data;
4045 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4046 struct super_block *sb = inode->i_sb;
4047 struct ocfs2_super *osb = OCFS2_SB(sb);
4048 int ret, num_buckets, extend = 1;
4050 u32 e_cpos, num_clusters;
4052 mlog(0, "Add new xattr bucket starting form %llu\n",
4053 (unsigned long long)header_bh->b_blocknr);
4056 * Add refrence for header_bh here because it may be
4057 * changed in ocfs2_add_new_xattr_cluster and we need
4058 * to free it in the end.
4062 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4069 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
4075 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4076 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4078 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
4079 ret = ocfs2_add_new_xattr_cluster(inode,
4095 ret = ocfs2_extend_xattr_bucket(inode,
4108 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4109 struct ocfs2_xattr_bucket *bucket,
4112 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4114 offs = offs % inode->i_sb->s_blocksize;
4115 return bucket_block(bucket, block_off) + offs;
4119 * Handle the normal xattr set, including replace, delete and new.
4121 * Note: "local" indicates the real data's locality. So we can't
4122 * just its bucket locality by its length.
4124 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4125 struct ocfs2_xattr_info *xi,
4126 struct ocfs2_xattr_search *xs,
4130 struct ocfs2_xattr_entry *last, *xe;
4131 int name_len = strlen(xi->name);
4132 struct ocfs2_xattr_header *xh = xs->header;
4133 u16 count = le16_to_cpu(xh->xh_count), start;
4134 size_t blocksize = inode->i_sb->s_blocksize;
4136 size_t offs, size, new_size;
4138 last = &xh->xh_entries[count];
4139 if (!xs->not_found) {
4141 offs = le16_to_cpu(xe->xe_name_offset);
4142 if (ocfs2_xattr_is_local(xe))
4143 size = OCFS2_XATTR_SIZE(name_len) +
4144 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4146 size = OCFS2_XATTR_SIZE(name_len) +
4147 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4150 * If the new value will be stored outside, xi->value has been
4151 * initalized as an empty ocfs2_xattr_value_root, and the same
4152 * goes with xi->value_len, so we can set new_size safely here.
4153 * See ocfs2_xattr_set_in_bucket.
4155 new_size = OCFS2_XATTR_SIZE(name_len) +
4156 OCFS2_XATTR_SIZE(xi->value_len);
4158 le16_add_cpu(&xh->xh_name_value_len, -size);
4160 if (new_size > size)
4161 goto set_new_name_value;
4163 /* Now replace the old value with new one. */
4165 xe->xe_value_size = cpu_to_le64(xi->value_len);
4167 xe->xe_value_size = 0;
4169 val = ocfs2_xattr_bucket_get_val(inode,
4171 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4172 size - OCFS2_XATTR_SIZE(name_len));
4173 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4174 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4175 xi->value, xi->value_len);
4177 le16_add_cpu(&xh->xh_name_value_len, new_size);
4178 ocfs2_xattr_set_local(xe, local);
4182 * Remove the old entry if there is more than one.
4183 * We don't remove the last entry so that we can
4184 * use it to indicate the hash value of the empty
4188 le16_add_cpu(&xh->xh_count, -1);
4191 (void *)last - (void *)xe);
4193 sizeof(struct ocfs2_xattr_entry));
4196 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4201 /* find a new entry for insert. */
4202 int low = 0, high = count - 1, tmp;
4203 struct ocfs2_xattr_entry *tmp_xe;
4205 while (low <= high && count) {
4206 tmp = (low + high) / 2;
4207 tmp_xe = &xh->xh_entries[tmp];
4209 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4211 else if (name_hash <
4212 le32_to_cpu(tmp_xe->xe_name_hash))
4220 xe = &xh->xh_entries[low];
4222 memmove(xe + 1, xe, (void *)last - (void *)xe);
4224 le16_add_cpu(&xh->xh_count, 1);
4225 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4226 xe->xe_name_hash = cpu_to_le32(name_hash);
4227 xe->xe_name_len = name_len;
4228 ocfs2_xattr_set_type(xe, xi->name_index);
4232 /* Insert the new name+value. */
4233 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4236 * We must make sure that the name/value pair
4237 * exists in the same block.
4239 offs = le16_to_cpu(xh->xh_free_start);
4240 start = offs - size;
4242 if (start >> inode->i_sb->s_blocksize_bits !=
4243 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4244 offs = offs - offs % blocksize;
4245 xh->xh_free_start = cpu_to_le16(offs);
4248 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
4249 xe->xe_name_offset = cpu_to_le16(offs - size);
4251 memset(val, 0, size);
4252 memcpy(val, xi->name, name_len);
4253 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4255 xe->xe_value_size = cpu_to_le64(xi->value_len);
4256 ocfs2_xattr_set_local(xe, local);
4258 le16_add_cpu(&xh->xh_free_start, -size);
4259 le16_add_cpu(&xh->xh_name_value_len, size);
4265 * Set the xattr entry in the specified bucket.
4266 * The bucket is indicated by xs->bucket and it should have the enough
4267 * space for the xattr insertion.
4269 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4271 struct ocfs2_xattr_info *xi,
4272 struct ocfs2_xattr_search *xs,
4279 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4280 (unsigned long)xi->value_len, xi->name_index,
4281 (unsigned long long)bucket_blkno(xs->bucket));
4283 if (!xs->bucket->bu_bhs[1]) {
4284 blkno = bucket_blkno(xs->bucket);
4285 ocfs2_xattr_bucket_relse(xs->bucket);
4286 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
4293 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4294 OCFS2_JOURNAL_ACCESS_WRITE);
4300 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4301 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4307 static int ocfs2_xattr_value_update_size(struct inode *inode,
4309 struct buffer_head *xe_bh,
4310 struct ocfs2_xattr_entry *xe,
4315 ret = ocfs2_journal_access(handle, inode, xe_bh,
4316 OCFS2_JOURNAL_ACCESS_WRITE);
4322 xe->xe_value_size = cpu_to_le64(new_size);
4324 ret = ocfs2_journal_dirty(handle, xe_bh);
4333 * Truncate the specified xe_off entry in xattr bucket.
4334 * bucket is indicated by header_bh and len is the new length.
4335 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4337 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4339 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4340 struct buffer_head *header_bh,
4343 struct ocfs2_xattr_set_ctxt *ctxt)
4347 struct buffer_head *value_bh = NULL;
4348 struct ocfs2_xattr_value_root *xv;
4349 struct ocfs2_xattr_entry *xe;
4350 struct ocfs2_xattr_header *xh =
4351 (struct ocfs2_xattr_header *)header_bh->b_data;
4352 size_t blocksize = inode->i_sb->s_blocksize;
4354 xe = &xh->xh_entries[xe_off];
4356 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4358 offset = le16_to_cpu(xe->xe_name_offset) +
4359 OCFS2_XATTR_SIZE(xe->xe_name_len);
4361 value_blk = offset / blocksize;
4363 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4364 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4365 value_blk += header_bh->b_blocknr;
4367 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4373 xv = (struct ocfs2_xattr_value_root *)
4374 (value_bh->b_data + offset % blocksize);
4376 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4377 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4378 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
4384 ret = ocfs2_xattr_value_update_size(inode, ctxt->handle,
4385 header_bh, xe, len);
4396 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4397 struct ocfs2_xattr_search *xs,
4399 struct ocfs2_xattr_set_ctxt *ctxt)
4402 struct ocfs2_xattr_entry *xe = xs->here;
4403 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4405 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4407 offset = xe - xh->xh_entries;
4408 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0],
4416 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4418 struct ocfs2_xattr_search *xs,
4423 struct ocfs2_xattr_value_root *xv;
4424 struct ocfs2_xattr_entry *xe = xs->here;
4426 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4428 offset = le16_to_cpu(xe->xe_name_offset) +
4429 OCFS2_XATTR_SIZE(xe->xe_name_len);
4431 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4433 return __ocfs2_xattr_set_value_outside(inode, handle,
4434 xv, val, value_len);
4437 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4438 struct buffer_head *root_bh,
4444 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4445 struct inode *tl_inode = osb->osb_tl_inode;
4447 struct ocfs2_xattr_block *xb =
4448 (struct ocfs2_xattr_block *)root_bh->b_data;
4449 struct ocfs2_alloc_context *meta_ac = NULL;
4450 struct ocfs2_cached_dealloc_ctxt dealloc;
4451 struct ocfs2_extent_tree et;
4453 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4455 ocfs2_init_dealloc_ctxt(&dealloc);
4457 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4458 cpos, len, (unsigned long long)blkno);
4460 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4462 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4468 mutex_lock(&tl_inode->i_mutex);
4470 if (ocfs2_truncate_log_needs_flush(osb)) {
4471 ret = __ocfs2_flush_truncate_log(osb);
4478 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4479 if (IS_ERR(handle)) {
4485 ret = ocfs2_journal_access(handle, inode, root_bh,
4486 OCFS2_JOURNAL_ACCESS_WRITE);
4492 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4499 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4501 ret = ocfs2_journal_dirty(handle, root_bh);
4507 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4512 ocfs2_commit_trans(osb, handle);
4514 ocfs2_schedule_truncate_log_flush(osb, 1);
4516 mutex_unlock(&tl_inode->i_mutex);
4519 ocfs2_free_alloc_context(meta_ac);
4521 ocfs2_run_deallocs(osb, &dealloc);
4526 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4528 struct ocfs2_xattr_search *xs)
4530 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
4531 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4532 le16_to_cpu(xh->xh_count) - 1];
4535 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4536 OCFS2_JOURNAL_ACCESS_WRITE);
4542 /* Remove the old entry. */
4543 memmove(xs->here, xs->here + 1,
4544 (void *)last - (void *)xs->here);
4545 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4546 le16_add_cpu(&xh->xh_count, -1);
4548 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4552 * Set the xattr name/value in the bucket specified in xs.
4554 * As the new value in xi may be stored in the bucket or in an outside cluster,
4555 * we divide the whole process into 3 steps:
4556 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4557 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4558 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4559 * 4. If the clusters for the new outside value can't be allocated, we need
4560 * to free the xattr we allocated in set.
4562 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4563 struct ocfs2_xattr_info *xi,
4564 struct ocfs2_xattr_search *xs,
4565 struct ocfs2_xattr_set_ctxt *ctxt)
4569 char *val = (char *)xi->value;
4570 struct ocfs2_xattr_entry *xe = xs->here;
4571 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4574 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4576 * We need to truncate the xattr storage first.
4578 * If both the old and new value are stored to
4579 * outside block, we only need to truncate
4580 * the storage and then set the value outside.
4582 * If the new value should be stored within block,
4583 * we should free all the outside block first and
4584 * the modification to the xattr block will be done
4585 * by following steps.
4587 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4588 value_len = xi->value_len;
4592 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4599 goto set_value_outside;
4602 value_len = xi->value_len;
4603 /* So we have to handle the inside block change now. */
4604 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4606 * If the new value will be stored outside of block,
4607 * initalize a new empty value root and insert it first.
4610 xi->value = &def_xv;
4611 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4614 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4621 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4624 /* allocate the space now for the outside block storage. */
4625 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4630 if (xs->not_found) {
4632 * We can't allocate enough clusters for outside
4633 * storage and we have allocated xattr already,
4634 * so need to remove it.
4636 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
4642 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4643 xs, val, value_len);
4649 * check whether the xattr bucket is filled up with the same hash value.
4650 * If we want to insert the xattr with the same hash, return -ENOSPC.
4651 * If we want to insert a xattr with different hash value, go ahead
4652 * and ocfs2_divide_xattr_bucket will handle this.
4654 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4655 struct ocfs2_xattr_bucket *bucket,
4658 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4659 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4661 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4664 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4665 xh->xh_entries[0].xe_name_hash) {
4666 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4668 (unsigned long long)bucket_blkno(bucket),
4669 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4676 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4677 struct ocfs2_xattr_info *xi,
4678 struct ocfs2_xattr_search *xs,
4679 struct ocfs2_xattr_set_ctxt *ctxt)
4681 struct ocfs2_xattr_header *xh;
4682 struct ocfs2_xattr_entry *xe;
4683 u16 count, header_size, xh_free_start;
4684 int free, max_free, need, old;
4685 size_t value_size = 0, name_len = strlen(xi->name);
4686 size_t blocksize = inode->i_sb->s_blocksize;
4687 int ret, allocation = 0;
4689 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4693 count = le16_to_cpu(xh->xh_count);
4694 xh_free_start = le16_to_cpu(xh->xh_free_start);
4695 header_size = sizeof(struct ocfs2_xattr_header) +
4696 count * sizeof(struct ocfs2_xattr_entry);
4697 max_free = OCFS2_XATTR_BUCKET_SIZE -
4698 le16_to_cpu(xh->xh_name_value_len) - header_size;
4700 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4701 "of %u which exceed block size\n",
4702 (unsigned long long)bucket_blkno(xs->bucket),
4705 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4706 value_size = OCFS2_XATTR_ROOT_SIZE;
4708 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4711 need = sizeof(struct ocfs2_xattr_entry) +
4712 OCFS2_XATTR_SIZE(name_len) + value_size;
4714 need = value_size + OCFS2_XATTR_SIZE(name_len);
4717 * We only replace the old value if the new length is smaller
4718 * than the old one. Otherwise we will allocate new space in the
4719 * bucket to store it.
4722 if (ocfs2_xattr_is_local(xe))
4723 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4725 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4727 if (old >= value_size)
4731 free = xh_free_start - header_size;
4733 * We need to make sure the new name/value pair
4734 * can exist in the same block.
4736 if (xh_free_start % blocksize < need)
4737 free -= xh_free_start % blocksize;
4739 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4740 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4741 " %u\n", xs->not_found,
4742 (unsigned long long)bucket_blkno(xs->bucket),
4743 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4744 le16_to_cpu(xh->xh_name_value_len));
4748 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
4749 if (need <= max_free &&
4750 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4752 * We can create the space by defragment. Since only the
4753 * name/value will be moved, the xe shouldn't be changed
4756 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
4763 xh_free_start = le16_to_cpu(xh->xh_free_start);
4764 free = xh_free_start - header_size;
4765 if (xh_free_start % blocksize < need)
4766 free -= xh_free_start % blocksize;
4771 mlog(0, "Can't get enough space for xattr insert by "
4772 "defragment. Need %u bytes, but we have %d, so "
4773 "allocate new bucket for it.\n", need, free);
4777 * We have to add new buckets or clusters and one
4778 * allocation should leave us enough space for insert.
4783 * We do not allow for overlapping ranges between buckets. And
4784 * the maximum number of collisions we will allow for then is
4785 * one bucket's worth, so check it here whether we need to
4786 * add a new bucket for the insert.
4788 ret = ocfs2_check_xattr_bucket_collision(inode,
4796 ret = ocfs2_add_new_xattr_bucket(inode,
4798 xs->bucket->bu_bhs[0],
4805 ocfs2_xattr_bucket_relse(xs->bucket);
4807 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4810 if (ret && ret != -ENODATA)
4812 xs->not_found = ret;
4818 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
4824 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4825 struct ocfs2_xattr_bucket *bucket,
4829 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4831 struct ocfs2_xattr_entry *xe;
4832 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4833 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
4835 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
4837 ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4838 if (IS_ERR(ctxt.handle)) {
4839 ret = PTR_ERR(ctxt.handle);
4844 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4845 xe = &xh->xh_entries[i];
4846 if (ocfs2_xattr_is_local(xe))
4849 ret = ocfs2_xattr_bucket_value_truncate(inode,
4858 ret = ocfs2_commit_trans(osb, ctxt.handle);
4859 ocfs2_schedule_truncate_log_flush(osb, 1);
4860 ocfs2_run_deallocs(osb, &ctxt.dealloc);
4865 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4866 struct buffer_head *xb_bh)
4868 struct ocfs2_xattr_block *xb =
4869 (struct ocfs2_xattr_block *)xb_bh->b_data;
4870 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4872 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4875 if (le16_to_cpu(el->l_next_free_rec) == 0)
4878 while (name_hash > 0) {
4879 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4880 &e_cpos, &num_clusters, el);
4886 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4887 ocfs2_delete_xattr_in_bucket,
4894 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4895 p_blkno, e_cpos, num_clusters);
4904 name_hash = e_cpos - 1;
4912 * 'trusted' attributes support
4914 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4915 size_t list_size, const char *name,
4918 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
4919 const size_t total_len = prefix_len + name_len + 1;
4921 if (list && total_len <= list_size) {
4922 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4923 memcpy(list + prefix_len, name, name_len);
4924 list[prefix_len + name_len] = '\0';
4929 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4930 void *buffer, size_t size)
4932 if (strcmp(name, "") == 0)
4934 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4938 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4939 const void *value, size_t size, int flags)
4941 if (strcmp(name, "") == 0)
4944 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4948 struct xattr_handler ocfs2_xattr_trusted_handler = {
4949 .prefix = XATTR_TRUSTED_PREFIX,
4950 .list = ocfs2_xattr_trusted_list,
4951 .get = ocfs2_xattr_trusted_get,
4952 .set = ocfs2_xattr_trusted_set,
4956 * 'user' attributes support
4958 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4959 size_t list_size, const char *name,
4962 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
4963 const size_t total_len = prefix_len + name_len + 1;
4964 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4966 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4969 if (list && total_len <= list_size) {
4970 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4971 memcpy(list + prefix_len, name, name_len);
4972 list[prefix_len + name_len] = '\0';
4977 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4978 void *buffer, size_t size)
4980 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4982 if (strcmp(name, "") == 0)
4984 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4986 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4990 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4991 const void *value, size_t size, int flags)
4993 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4995 if (strcmp(name, "") == 0)
4997 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5000 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5004 struct xattr_handler ocfs2_xattr_user_handler = {
5005 .prefix = XATTR_USER_PREFIX,
5006 .list = ocfs2_xattr_user_list,
5007 .get = ocfs2_xattr_user_get,
5008 .set = ocfs2_xattr_user_set,