xref: /openbmc/linux/fs/ocfs2/xattr.c (revision 10fc3a18)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fa60ce2cSMasahiro Yamada /*
3f56654c4STao Ma  * xattr.c
4f56654c4STao Ma  *
5c3cb6827STiger Yang  * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
6f56654c4STao Ma  *
7cf1d6c76STiger Yang  * CREDITS:
8c3cb6827STiger Yang  * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
9c3cb6827STiger Yang  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
10f56654c4STao Ma  */
11f56654c4STao Ma 
12cf1d6c76STiger Yang #include <linux/capability.h>
13cf1d6c76STiger Yang #include <linux/fs.h>
14cf1d6c76STiger Yang #include <linux/types.h>
15cf1d6c76STiger Yang #include <linux/slab.h>
16cf1d6c76STiger Yang #include <linux/highmem.h>
17cf1d6c76STiger Yang #include <linux/pagemap.h>
18cf1d6c76STiger Yang #include <linux/uio.h>
19cf1d6c76STiger Yang #include <linux/sched.h>
20cf1d6c76STiger Yang #include <linux/splice.h>
21cf1d6c76STiger Yang #include <linux/mount.h>
22cf1d6c76STiger Yang #include <linux/writeback.h>
23cf1d6c76STiger Yang #include <linux/falloc.h>
2401225596STao Ma #include <linux/sort.h>
2599219aeaSMark Fasheh #include <linux/init.h>
2699219aeaSMark Fasheh #include <linux/module.h>
2799219aeaSMark Fasheh #include <linux/string.h>
28923f7f31STiger Yang #include <linux/security.h>
29cf1d6c76STiger Yang 
30f56654c4STao Ma #include <cluster/masklog.h>
31f56654c4STao Ma 
32f56654c4STao Ma #include "ocfs2.h"
33f56654c4STao Ma #include "alloc.h"
34d6b32bbbSJoel Becker #include "blockcheck.h"
35f56654c4STao Ma #include "dlmglue.h"
36f56654c4STao Ma #include "file.h"
37cf1d6c76STiger Yang #include "symlink.h"
38cf1d6c76STiger Yang #include "sysfile.h"
39f56654c4STao Ma #include "inode.h"
40f56654c4STao Ma #include "journal.h"
41f56654c4STao Ma #include "ocfs2_fs.h"
42f56654c4STao Ma #include "suballoc.h"
43f56654c4STao Ma #include "uptodate.h"
44f56654c4STao Ma #include "buffer_head_io.h"
450c044f0bSTao Ma #include "super.h"
46cf1d6c76STiger Yang #include "xattr.h"
47492a8a33STao Ma #include "refcounttree.h"
480fe9b66cSTao Ma #include "acl.h"
49402b4183STao Ma #include "ocfs2_trace.h"
50cf1d6c76STiger Yang 
51cf1d6c76STiger Yang struct ocfs2_xattr_def_value_root {
52cf1d6c76STiger Yang 	struct ocfs2_xattr_value_root	xv;
53cf1d6c76STiger Yang 	struct ocfs2_extent_rec		er;
54cf1d6c76STiger Yang };
55cf1d6c76STiger Yang 
560c044f0bSTao Ma struct ocfs2_xattr_bucket {
57ba937127SJoel Becker 	/* The inode these xattrs are associated with */
58ba937127SJoel Becker 	struct inode *bu_inode;
59ba937127SJoel Becker 
60ba937127SJoel Becker 	/* The actual buffers that make up the bucket */
614ac6032dSJoel Becker 	struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
62ba937127SJoel Becker 
63ba937127SJoel Becker 	/* How many blocks make up one bucket for this filesystem */
64ba937127SJoel Becker 	int bu_blocks;
650c044f0bSTao Ma };
660c044f0bSTao Ma 
6778f30c31STao Ma struct ocfs2_xattr_set_ctxt {
6885db90e7STao Ma 	handle_t *handle;
6978f30c31STao Ma 	struct ocfs2_alloc_context *meta_ac;
7078f30c31STao Ma 	struct ocfs2_alloc_context *data_ac;
7178f30c31STao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
725f5261acSTao Ma 	int set_abort;
7378f30c31STao Ma };
7478f30c31STao Ma 
75cf1d6c76STiger Yang #define OCFS2_XATTR_ROOT_SIZE	(sizeof(struct ocfs2_xattr_def_value_root))
76cf1d6c76STiger Yang #define OCFS2_XATTR_INLINE_SIZE	80
774442f518STiger Yang #define OCFS2_XATTR_HEADER_GAP	4
78534eadddSTiger Yang #define OCFS2_XATTR_FREE_IN_IBODY	(OCFS2_MIN_XATTR_INLINE_SIZE \
79534eadddSTiger Yang 					 - sizeof(struct ocfs2_xattr_header) \
804442f518STiger Yang 					 - OCFS2_XATTR_HEADER_GAP)
8189c38bd0STiger Yang #define OCFS2_XATTR_FREE_IN_BLOCK(ptr)	((ptr)->i_sb->s_blocksize \
8289c38bd0STiger Yang 					 - sizeof(struct ocfs2_xattr_block) \
8389c38bd0STiger Yang 					 - sizeof(struct ocfs2_xattr_header) \
844442f518STiger Yang 					 - OCFS2_XATTR_HEADER_GAP)
85cf1d6c76STiger Yang 
86cf1d6c76STiger Yang static struct ocfs2_xattr_def_value_root def_xv = {
87cf1d6c76STiger Yang 	.xv.xr_list.l_count = cpu_to_le16(1),
88cf1d6c76STiger Yang };
89cf1d6c76STiger Yang 
90537d81caSStephen Hemminger const struct xattr_handler *ocfs2_xattr_handlers[] = {
91cf1d6c76STiger Yang 	&ocfs2_xattr_user_handler,
92cf1d6c76STiger Yang 	&ocfs2_xattr_trusted_handler,
93923f7f31STiger Yang 	&ocfs2_xattr_security_handler,
94cf1d6c76STiger Yang 	NULL
95cf1d6c76STiger Yang };
96cf1d6c76STiger Yang 
97537d81caSStephen Hemminger static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
98cf1d6c76STiger Yang 	[OCFS2_XATTR_INDEX_USER]		= &ocfs2_xattr_user_handler,
99d549b741SChristian Brauner 	[OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]	= &nop_posix_acl_access,
100d549b741SChristian Brauner 	[OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]	= &nop_posix_acl_default,
101cf1d6c76STiger Yang 	[OCFS2_XATTR_INDEX_TRUSTED]		= &ocfs2_xattr_trusted_handler,
102923f7f31STiger Yang 	[OCFS2_XATTR_INDEX_SECURITY]		= &ocfs2_xattr_security_handler,
103cf1d6c76STiger Yang };
104cf1d6c76STiger Yang 
105cf1d6c76STiger Yang struct ocfs2_xattr_info {
1066b240ff6SJoel Becker 	int		xi_name_index;
1076b240ff6SJoel Becker 	const char	*xi_name;
10818853b95SJoel Becker 	int		xi_name_len;
1096b240ff6SJoel Becker 	const void	*xi_value;
1106b240ff6SJoel Becker 	size_t		xi_value_len;
111cf1d6c76STiger Yang };
112cf1d6c76STiger Yang 
113cf1d6c76STiger Yang struct ocfs2_xattr_search {
114cf1d6c76STiger Yang 	struct buffer_head *inode_bh;
115cf1d6c76STiger Yang 	/*
116cf1d6c76STiger Yang 	 * xattr_bh point to the block buffer head which has extended attribute
117cf1d6c76STiger Yang 	 * when extended attribute in inode, xattr_bh is equal to inode_bh.
118cf1d6c76STiger Yang 	 */
119cf1d6c76STiger Yang 	struct buffer_head *xattr_bh;
120cf1d6c76STiger Yang 	struct ocfs2_xattr_header *header;
121ba937127SJoel Becker 	struct ocfs2_xattr_bucket *bucket;
122cf1d6c76STiger Yang 	void *base;
123cf1d6c76STiger Yang 	void *end;
124cf1d6c76STiger Yang 	struct ocfs2_xattr_entry *here;
125cf1d6c76STiger Yang 	int not_found;
126cf1d6c76STiger Yang };
127cf1d6c76STiger Yang 
12811179f2cSJoel Becker /* Operations on struct ocfs2_xa_entry */
12911179f2cSJoel Becker struct ocfs2_xa_loc;
13011179f2cSJoel Becker struct ocfs2_xa_loc_operations {
13111179f2cSJoel Becker 	/*
132cf2bc809SJoel Becker 	 * Journal functions
133cf2bc809SJoel Becker 	 */
134cf2bc809SJoel Becker 	int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
135cf2bc809SJoel Becker 				  int type);
136cf2bc809SJoel Becker 	void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
137cf2bc809SJoel Becker 
138cf2bc809SJoel Becker 	/*
13911179f2cSJoel Becker 	 * Return a pointer to the appropriate buffer in loc->xl_storage
14011179f2cSJoel Becker 	 * at the given offset from loc->xl_header.
14111179f2cSJoel Becker 	 */
14211179f2cSJoel Becker 	void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
14311179f2cSJoel Becker 
14469a3e539SJoel Becker 	/* Can we reuse the existing entry for the new value? */
14569a3e539SJoel Becker 	int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
14669a3e539SJoel Becker 			     struct ocfs2_xattr_info *xi);
14769a3e539SJoel Becker 
14869a3e539SJoel Becker 	/* How much space is needed for the new value? */
14969a3e539SJoel Becker 	int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
15069a3e539SJoel Becker 			       struct ocfs2_xattr_info *xi);
15169a3e539SJoel Becker 
15269a3e539SJoel Becker 	/*
15369a3e539SJoel Becker 	 * Return the offset of the first name+value pair.  This is
15469a3e539SJoel Becker 	 * the start of our downward-filling free space.
15569a3e539SJoel Becker 	 */
15669a3e539SJoel Becker 	int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
15769a3e539SJoel Becker 
15811179f2cSJoel Becker 	/*
15911179f2cSJoel Becker 	 * Remove the name+value at this location.  Do whatever is
16011179f2cSJoel Becker 	 * appropriate with the remaining name+value pairs.
16111179f2cSJoel Becker 	 */
16211179f2cSJoel Becker 	void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
16369a3e539SJoel Becker 
16469a3e539SJoel Becker 	/* Fill xl_entry with a new entry */
16569a3e539SJoel Becker 	void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
16669a3e539SJoel Becker 
16769a3e539SJoel Becker 	/* Add name+value storage to an entry */
16869a3e539SJoel Becker 	void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
1693fc12afaSJoel Becker 
1703fc12afaSJoel Becker 	/*
1713fc12afaSJoel Becker 	 * Initialize the value buf's access and bh fields for this entry.
1723fc12afaSJoel Becker 	 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
1733fc12afaSJoel Becker 	 */
1743fc12afaSJoel Becker 	void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
1753fc12afaSJoel Becker 				   struct ocfs2_xattr_value_buf *vb);
17611179f2cSJoel Becker };
17711179f2cSJoel Becker 
17811179f2cSJoel Becker /*
17911179f2cSJoel Becker  * Describes an xattr entry location.  This is a memory structure
18011179f2cSJoel Becker  * tracking the on-disk structure.
18111179f2cSJoel Becker  */
18211179f2cSJoel Becker struct ocfs2_xa_loc {
183cf2bc809SJoel Becker 	/* This xattr belongs to this inode */
184cf2bc809SJoel Becker 	struct inode *xl_inode;
185cf2bc809SJoel Becker 
18611179f2cSJoel Becker 	/* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
18711179f2cSJoel Becker 	struct ocfs2_xattr_header *xl_header;
18811179f2cSJoel Becker 
18911179f2cSJoel Becker 	/* Bytes from xl_header to the end of the storage */
19011179f2cSJoel Becker 	int xl_size;
19111179f2cSJoel Becker 
19211179f2cSJoel Becker 	/*
19311179f2cSJoel Becker 	 * The ocfs2_xattr_entry this location describes.  If this is
19411179f2cSJoel Becker 	 * NULL, this location describes the on-disk structure where it
19511179f2cSJoel Becker 	 * would have been.
19611179f2cSJoel Becker 	 */
19711179f2cSJoel Becker 	struct ocfs2_xattr_entry *xl_entry;
19811179f2cSJoel Becker 
19911179f2cSJoel Becker 	/*
20011179f2cSJoel Becker 	 * Internal housekeeping
20111179f2cSJoel Becker 	 */
20211179f2cSJoel Becker 
20311179f2cSJoel Becker 	/* Buffer(s) containing this entry */
20411179f2cSJoel Becker 	void *xl_storage;
20511179f2cSJoel Becker 
20611179f2cSJoel Becker 	/* Operations on the storage backing this location */
20711179f2cSJoel Becker 	const struct ocfs2_xa_loc_operations *xl_ops;
20811179f2cSJoel Becker };
20911179f2cSJoel Becker 
210199799a3SJoel Becker /*
211199799a3SJoel Becker  * Convenience functions to calculate how much space is needed for a
212199799a3SJoel Becker  * given name+value pair
213199799a3SJoel Becker  */
namevalue_size(int name_len,uint64_t value_len)214199799a3SJoel Becker static int namevalue_size(int name_len, uint64_t value_len)
215199799a3SJoel Becker {
216199799a3SJoel Becker 	if (value_len > OCFS2_XATTR_INLINE_SIZE)
217199799a3SJoel Becker 		return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
218199799a3SJoel Becker 	else
219199799a3SJoel Becker 		return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
220199799a3SJoel Becker }
221199799a3SJoel Becker 
namevalue_size_xi(struct ocfs2_xattr_info * xi)222199799a3SJoel Becker static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
223199799a3SJoel Becker {
224199799a3SJoel Becker 	return namevalue_size(xi->xi_name_len, xi->xi_value_len);
225199799a3SJoel Becker }
226199799a3SJoel Becker 
namevalue_size_xe(struct ocfs2_xattr_entry * xe)227199799a3SJoel Becker static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
228199799a3SJoel Becker {
229199799a3SJoel Becker 	u64 value_len = le64_to_cpu(xe->xe_value_size);
230199799a3SJoel Becker 
231199799a3SJoel Becker 	BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
232199799a3SJoel Becker 	       ocfs2_xattr_is_local(xe));
233199799a3SJoel Becker 	return namevalue_size(xe->xe_name_len, value_len);
234199799a3SJoel Becker }
235199799a3SJoel Becker 
236199799a3SJoel Becker 
237fd68a894STao Ma static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
238589dc260STao Ma 					     struct ocfs2_xattr_header *xh,
239589dc260STao Ma 					     int index,
240589dc260STao Ma 					     int *block_off,
241589dc260STao Ma 					     int *new_offset);
242589dc260STao Ma 
24354f443f4SJoel Becker static int ocfs2_xattr_block_find(struct inode *inode,
24454f443f4SJoel Becker 				  int name_index,
24554f443f4SJoel Becker 				  const char *name,
24654f443f4SJoel Becker 				  struct ocfs2_xattr_search *xs);
247589dc260STao Ma static int ocfs2_xattr_index_block_find(struct inode *inode,
248589dc260STao Ma 					struct buffer_head *root_bh,
249589dc260STao Ma 					int name_index,
250589dc260STao Ma 					const char *name,
251589dc260STao Ma 					struct ocfs2_xattr_search *xs);
252589dc260STao Ma 
2530c044f0bSTao Ma static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
25447bca495STao Ma 					struct buffer_head *blk_bh,
2550c044f0bSTao Ma 					char *buffer,
2560c044f0bSTao Ma 					size_t buffer_size);
2570c044f0bSTao Ma 
25801225596STao Ma static int ocfs2_xattr_create_index_block(struct inode *inode,
25978f30c31STao Ma 					  struct ocfs2_xattr_search *xs,
26078f30c31STao Ma 					  struct ocfs2_xattr_set_ctxt *ctxt);
26101225596STao Ma 
26201225596STao Ma static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
26301225596STao Ma 					     struct ocfs2_xattr_info *xi,
26478f30c31STao Ma 					     struct ocfs2_xattr_search *xs,
26578f30c31STao Ma 					     struct ocfs2_xattr_set_ctxt *ctxt);
26601225596STao Ma 
26747bca495STao Ma typedef int (xattr_tree_rec_func)(struct inode *inode,
26847bca495STao Ma 				  struct buffer_head *root_bh,
26947bca495STao Ma 				  u64 blkno, u32 cpos, u32 len, void *para);
27047bca495STao Ma static int ocfs2_iterate_xattr_index_block(struct inode *inode,
27147bca495STao Ma 					   struct buffer_head *root_bh,
27247bca495STao Ma 					   xattr_tree_rec_func *rec_func,
27347bca495STao Ma 					   void *para);
27447bca495STao Ma static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
27547bca495STao Ma 					struct ocfs2_xattr_bucket *bucket,
27647bca495STao Ma 					void *para);
27747bca495STao Ma static int ocfs2_rm_xattr_cluster(struct inode *inode,
27847bca495STao Ma 				  struct buffer_head *root_bh,
27947bca495STao Ma 				  u64 blkno,
28047bca495STao Ma 				  u32 cpos,
28147bca495STao Ma 				  u32 len,
28247bca495STao Ma 				  void *para);
28347bca495STao Ma 
284c58b6032SJoel Becker static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
285c58b6032SJoel Becker 				  u64 src_blk, u64 last_blk, u64 to_blk,
286c58b6032SJoel Becker 				  unsigned int start_bucket,
287c58b6032SJoel Becker 				  u32 *first_hash);
288492a8a33STao Ma static int ocfs2_prepare_refcount_xattr(struct inode *inode,
289492a8a33STao Ma 					struct ocfs2_dinode *di,
290492a8a33STao Ma 					struct ocfs2_xattr_info *xi,
291492a8a33STao Ma 					struct ocfs2_xattr_search *xis,
292492a8a33STao Ma 					struct ocfs2_xattr_search *xbs,
293492a8a33STao Ma 					struct ocfs2_refcount_tree **ref_tree,
294492a8a33STao Ma 					int *meta_need,
295492a8a33STao Ma 					int *credits);
296ce9c5a54STao Ma static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
297ce9c5a54STao Ma 					   struct ocfs2_xattr_bucket *bucket,
298ce9c5a54STao Ma 					   int offset,
299ce9c5a54STao Ma 					   struct ocfs2_xattr_value_root **xv,
300ce9c5a54STao Ma 					   struct buffer_head **bh);
301a3944256STao Ma 
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super * osb)3020030e001STiger Yang static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
3030030e001STiger Yang {
3040030e001STiger Yang 	return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
3050030e001STiger Yang }
3060030e001STiger Yang 
ocfs2_blocks_per_xattr_bucket(struct super_block * sb)3070030e001STiger Yang static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
3080030e001STiger Yang {
3090030e001STiger Yang 	return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
3100030e001STiger Yang }
3110030e001STiger Yang 
3129c7759aaSJoel Becker #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
31351def39fSJoel Becker #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
3143e632946SJoel Becker #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
3159c7759aaSJoel Becker 
ocfs2_xattr_bucket_new(struct inode * inode)316ba937127SJoel Becker static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
3176dde41d9SJoel Becker {
318ba937127SJoel Becker 	struct ocfs2_xattr_bucket *bucket;
319ba937127SJoel Becker 	int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3206dde41d9SJoel Becker 
321ba937127SJoel Becker 	BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
322ba937127SJoel Becker 
323ba937127SJoel Becker 	bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
324ba937127SJoel Becker 	if (bucket) {
325ba937127SJoel Becker 		bucket->bu_inode = inode;
326ba937127SJoel Becker 		bucket->bu_blocks = blks;
327ba937127SJoel Becker 	}
328ba937127SJoel Becker 
329ba937127SJoel Becker 	return bucket;
330ba937127SJoel Becker }
331ba937127SJoel Becker 
ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket * bucket)332ba937127SJoel Becker static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
333ba937127SJoel Becker {
334ba937127SJoel Becker 	int i;
335ba937127SJoel Becker 
336ba937127SJoel Becker 	for (i = 0; i < bucket->bu_blocks; i++) {
3376dde41d9SJoel Becker 		brelse(bucket->bu_bhs[i]);
3386dde41d9SJoel Becker 		bucket->bu_bhs[i] = NULL;
3396dde41d9SJoel Becker 	}
3406dde41d9SJoel Becker }
3416dde41d9SJoel Becker 
ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket * bucket)342ba937127SJoel Becker static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
343ba937127SJoel Becker {
344ba937127SJoel Becker 	if (bucket) {
345ba937127SJoel Becker 		ocfs2_xattr_bucket_relse(bucket);
346ba937127SJoel Becker 		bucket->bu_inode = NULL;
347ba937127SJoel Becker 		kfree(bucket);
348ba937127SJoel Becker 	}
349ba937127SJoel Becker }
350ba937127SJoel Becker 
351784b816aSJoel Becker /*
352784b816aSJoel Becker  * A bucket that has never been written to disk doesn't need to be
353784b816aSJoel Becker  * read.  We just need the buffer_heads.  Don't call this for
354784b816aSJoel Becker  * buckets that are already on disk.  ocfs2_read_xattr_bucket() initializes
355784b816aSJoel Becker  * them fully.
356784b816aSJoel Becker  */
ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket * bucket,u64 xb_blkno,int new)357ba937127SJoel Becker static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
3589c339255SWengang Wang 				   u64 xb_blkno, int new)
359784b816aSJoel Becker {
360784b816aSJoel Becker 	int i, rc = 0;
361784b816aSJoel Becker 
362ba937127SJoel Becker 	for (i = 0; i < bucket->bu_blocks; i++) {
363ba937127SJoel Becker 		bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
364ba937127SJoel Becker 					      xb_blkno + i);
365784b816aSJoel Becker 		if (!bucket->bu_bhs[i]) {
3667391a294SRui Xiang 			rc = -ENOMEM;
367784b816aSJoel Becker 			mlog_errno(rc);
368784b816aSJoel Becker 			break;
369784b816aSJoel Becker 		}
370784b816aSJoel Becker 
3718cb471e8SJoel Becker 		if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
3729c339255SWengang Wang 					   bucket->bu_bhs[i])) {
3739c339255SWengang Wang 			if (new)
3748cb471e8SJoel Becker 				ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
375ba937127SJoel Becker 							      bucket->bu_bhs[i]);
3769c339255SWengang Wang 			else {
3779c339255SWengang Wang 				set_buffer_uptodate(bucket->bu_bhs[i]);
3789c339255SWengang Wang 				ocfs2_set_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
3799c339255SWengang Wang 							  bucket->bu_bhs[i]);
3809c339255SWengang Wang 			}
3819c339255SWengang Wang 		}
382784b816aSJoel Becker 	}
383784b816aSJoel Becker 
384784b816aSJoel Becker 	if (rc)
385ba937127SJoel Becker 		ocfs2_xattr_bucket_relse(bucket);
386784b816aSJoel Becker 	return rc;
387784b816aSJoel Becker }
388784b816aSJoel Becker 
389784b816aSJoel Becker /* Read the xattr bucket at xb_blkno */
ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket * bucket,u64 xb_blkno)390ba937127SJoel Becker static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
391784b816aSJoel Becker 				   u64 xb_blkno)
392784b816aSJoel Becker {
393ba937127SJoel Becker 	int rc;
394784b816aSJoel Becker 
3958cb471e8SJoel Becker 	rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
396970e4936SJoel Becker 			       bucket->bu_blocks, bucket->bu_bhs, 0,
397970e4936SJoel Becker 			       NULL);
3984d0e214eSJoel Becker 	if (!rc) {
399c8b9cf9aSTao Ma 		spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
4004d0e214eSJoel Becker 		rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
4014d0e214eSJoel Becker 						 bucket->bu_bhs,
4024d0e214eSJoel Becker 						 bucket->bu_blocks,
4034d0e214eSJoel Becker 						 &bucket_xh(bucket)->xh_check);
404c8b9cf9aSTao Ma 		spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
4054d0e214eSJoel Becker 		if (rc)
4064d0e214eSJoel Becker 			mlog_errno(rc);
4074d0e214eSJoel Becker 	}
4084d0e214eSJoel Becker 
409784b816aSJoel Becker 	if (rc)
410ba937127SJoel Becker 		ocfs2_xattr_bucket_relse(bucket);
411784b816aSJoel Becker 	return rc;
412784b816aSJoel Becker }
413784b816aSJoel Becker 
ocfs2_xattr_bucket_journal_access(handle_t * handle,struct ocfs2_xattr_bucket * bucket,int type)4141224be02SJoel Becker static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
4151224be02SJoel Becker 					     struct ocfs2_xattr_bucket *bucket,
4161224be02SJoel Becker 					     int type)
4171224be02SJoel Becker {
4181224be02SJoel Becker 	int i, rc = 0;
4191224be02SJoel Becker 
420ba937127SJoel Becker 	for (i = 0; i < bucket->bu_blocks; i++) {
4210cf2f763SJoel Becker 		rc = ocfs2_journal_access(handle,
4220cf2f763SJoel Becker 					  INODE_CACHE(bucket->bu_inode),
4231224be02SJoel Becker 					  bucket->bu_bhs[i], type);
4241224be02SJoel Becker 		if (rc) {
4251224be02SJoel Becker 			mlog_errno(rc);
4261224be02SJoel Becker 			break;
4271224be02SJoel Becker 		}
4281224be02SJoel Becker 	}
4291224be02SJoel Becker 
4301224be02SJoel Becker 	return rc;
4311224be02SJoel Becker }
4321224be02SJoel Becker 
ocfs2_xattr_bucket_journal_dirty(handle_t * handle,struct ocfs2_xattr_bucket * bucket)4331224be02SJoel Becker static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
4341224be02SJoel Becker 					     struct ocfs2_xattr_bucket *bucket)
4351224be02SJoel Becker {
436ba937127SJoel Becker 	int i;
4371224be02SJoel Becker 
438c8b9cf9aSTao Ma 	spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
4394d0e214eSJoel Becker 	ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
4404d0e214eSJoel Becker 				   bucket->bu_bhs, bucket->bu_blocks,
4414d0e214eSJoel Becker 				   &bucket_xh(bucket)->xh_check);
442c8b9cf9aSTao Ma 	spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
4434d0e214eSJoel Becker 
444ba937127SJoel Becker 	for (i = 0; i < bucket->bu_blocks; i++)
4451224be02SJoel Becker 		ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
4461224be02SJoel Becker }
4471224be02SJoel Becker 
ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket * dest,struct ocfs2_xattr_bucket * src)448ba937127SJoel Becker static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
4494980c6daSJoel Becker 					 struct ocfs2_xattr_bucket *src)
4504980c6daSJoel Becker {
4514980c6daSJoel Becker 	int i;
452ba937127SJoel Becker 	int blocksize = src->bu_inode->i_sb->s_blocksize;
4534980c6daSJoel Becker 
454ba937127SJoel Becker 	BUG_ON(dest->bu_blocks != src->bu_blocks);
455ba937127SJoel Becker 	BUG_ON(dest->bu_inode != src->bu_inode);
456ba937127SJoel Becker 
457ba937127SJoel Becker 	for (i = 0; i < src->bu_blocks; i++) {
4584980c6daSJoel Becker 		memcpy(bucket_block(dest, i), bucket_block(src, i),
4594980c6daSJoel Becker 		       blocksize);
4604980c6daSJoel Becker 	}
4614980c6daSJoel Becker }
4621224be02SJoel Becker 
ocfs2_validate_xattr_block(struct super_block * sb,struct buffer_head * bh)4634ae1d69bSJoel Becker static int ocfs2_validate_xattr_block(struct super_block *sb,
4644ae1d69bSJoel Becker 				      struct buffer_head *bh)
4654ae1d69bSJoel Becker {
466d6b32bbbSJoel Becker 	int rc;
4674ae1d69bSJoel Becker 	struct ocfs2_xattr_block *xb =
4684ae1d69bSJoel Becker 		(struct ocfs2_xattr_block *)bh->b_data;
4694ae1d69bSJoel Becker 
470402b4183STao Ma 	trace_ocfs2_validate_xattr_block((unsigned long long)bh->b_blocknr);
4714ae1d69bSJoel Becker 
472d6b32bbbSJoel Becker 	BUG_ON(!buffer_uptodate(bh));
473d6b32bbbSJoel Becker 
474d6b32bbbSJoel Becker 	/*
475d6b32bbbSJoel Becker 	 * If the ecc fails, we return the error but otherwise
476d6b32bbbSJoel Becker 	 * leave the filesystem running.  We know any error is
477d6b32bbbSJoel Becker 	 * local to this block.
478d6b32bbbSJoel Becker 	 */
479d6b32bbbSJoel Becker 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
480d6b32bbbSJoel Becker 	if (rc)
481d6b32bbbSJoel Becker 		return rc;
482d6b32bbbSJoel Becker 
483d6b32bbbSJoel Becker 	/*
484d6b32bbbSJoel Becker 	 * Errors after here are fatal
485d6b32bbbSJoel Becker 	 */
486d6b32bbbSJoel Becker 
4874ae1d69bSJoel Becker 	if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
48817a5b9abSGoldwyn Rodrigues 		return ocfs2_error(sb,
4897ecef14aSJoe Perches 				   "Extended attribute block #%llu has bad signature %.*s\n",
4904ae1d69bSJoel Becker 				   (unsigned long long)bh->b_blocknr, 7,
4914ae1d69bSJoel Becker 				   xb->xb_signature);
4924ae1d69bSJoel Becker 	}
4934ae1d69bSJoel Becker 
4944ae1d69bSJoel Becker 	if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
49517a5b9abSGoldwyn Rodrigues 		return ocfs2_error(sb,
4967ecef14aSJoe Perches 				   "Extended attribute block #%llu has an invalid xb_blkno of %llu\n",
4974ae1d69bSJoel Becker 				   (unsigned long long)bh->b_blocknr,
4984ae1d69bSJoel Becker 				   (unsigned long long)le64_to_cpu(xb->xb_blkno));
4994ae1d69bSJoel Becker 	}
5004ae1d69bSJoel Becker 
5014ae1d69bSJoel Becker 	if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
50217a5b9abSGoldwyn Rodrigues 		return ocfs2_error(sb,
5037ecef14aSJoe Perches 				   "Extended attribute block #%llu has an invalid xb_fs_generation of #%u\n",
5044ae1d69bSJoel Becker 				   (unsigned long long)bh->b_blocknr,
5054ae1d69bSJoel Becker 				   le32_to_cpu(xb->xb_fs_generation));
5064ae1d69bSJoel Becker 	}
5074ae1d69bSJoel Becker 
5084ae1d69bSJoel Becker 	return 0;
5094ae1d69bSJoel Becker }
5104ae1d69bSJoel Becker 
ocfs2_read_xattr_block(struct inode * inode,u64 xb_blkno,struct buffer_head ** bh)5114ae1d69bSJoel Becker static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
5124ae1d69bSJoel Becker 				  struct buffer_head **bh)
5134ae1d69bSJoel Becker {
5144ae1d69bSJoel Becker 	int rc;
5154ae1d69bSJoel Becker 	struct buffer_head *tmp = *bh;
5164ae1d69bSJoel Becker 
5178cb471e8SJoel Becker 	rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
518970e4936SJoel Becker 			      ocfs2_validate_xattr_block);
5194ae1d69bSJoel Becker 
5204ae1d69bSJoel Becker 	/* If ocfs2_read_block() got us a new bh, pass it up. */
5214ae1d69bSJoel Becker 	if (!rc && !*bh)
5224ae1d69bSJoel Becker 		*bh = tmp;
5234ae1d69bSJoel Becker 
5244ae1d69bSJoel Becker 	return rc;
5254ae1d69bSJoel Becker }
5264ae1d69bSJoel Becker 
ocfs2_xattr_prefix(int name_index)527936b8834STao Ma static inline const char *ocfs2_xattr_prefix(int name_index)
528cf1d6c76STiger Yang {
529537d81caSStephen Hemminger 	const struct xattr_handler *handler = NULL;
530cf1d6c76STiger Yang 
531cf1d6c76STiger Yang 	if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
532cf1d6c76STiger Yang 		handler = ocfs2_xattr_handler_map[name_index];
53398e9cb57SAndreas Gruenbacher 	return handler ? xattr_prefix(handler) : NULL;
534cf1d6c76STiger Yang }
535cf1d6c76STiger Yang 
ocfs2_xattr_name_hash(struct inode * inode,const char * name,int name_len)53640daa16aSMark Fasheh static u32 ocfs2_xattr_name_hash(struct inode *inode,
5372057e5c6STao Ma 				 const char *name,
538cf1d6c76STiger Yang 				 int name_len)
539cf1d6c76STiger Yang {
540cf1d6c76STiger Yang 	/* Get hash value of uuid from super block */
541cf1d6c76STiger Yang 	u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
542cf1d6c76STiger Yang 	int i;
543cf1d6c76STiger Yang 
544cf1d6c76STiger Yang 	/* hash extended attribute name */
545cf1d6c76STiger Yang 	for (i = 0; i < name_len; i++) {
546cf1d6c76STiger Yang 		hash = (hash << OCFS2_HASH_SHIFT) ^
547cf1d6c76STiger Yang 		       (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
548cf1d6c76STiger Yang 		       *name++;
549cf1d6c76STiger Yang 	}
550cf1d6c76STiger Yang 
551cf1d6c76STiger Yang 	return hash;
552cf1d6c76STiger Yang }
553cf1d6c76STiger Yang 
ocfs2_xattr_entry_real_size(int name_len,size_t value_len)554534eadddSTiger Yang static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
555534eadddSTiger Yang {
556199799a3SJoel Becker 	return namevalue_size(name_len, value_len) +
557199799a3SJoel Becker 		sizeof(struct ocfs2_xattr_entry);
558199799a3SJoel Becker }
559534eadddSTiger Yang 
ocfs2_xi_entry_usage(struct ocfs2_xattr_info * xi)560199799a3SJoel Becker static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
561199799a3SJoel Becker {
562199799a3SJoel Becker 	return namevalue_size_xi(xi) +
563199799a3SJoel Becker 		sizeof(struct ocfs2_xattr_entry);
564199799a3SJoel Becker }
565534eadddSTiger Yang 
ocfs2_xe_entry_usage(struct ocfs2_xattr_entry * xe)566199799a3SJoel Becker static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
567199799a3SJoel Becker {
568199799a3SJoel Becker 	return namevalue_size_xe(xe) +
569199799a3SJoel Becker 		sizeof(struct ocfs2_xattr_entry);
570534eadddSTiger Yang }
571534eadddSTiger Yang 
ocfs2_calc_security_init(struct inode * dir,struct ocfs2_security_xattr_info * si,int * want_clusters,int * xattr_credits,struct ocfs2_alloc_context ** xattr_ac)572534eadddSTiger Yang int ocfs2_calc_security_init(struct inode *dir,
573534eadddSTiger Yang 			     struct ocfs2_security_xattr_info *si,
574534eadddSTiger Yang 			     int *want_clusters,
575534eadddSTiger Yang 			     int *xattr_credits,
576534eadddSTiger Yang 			     struct ocfs2_alloc_context **xattr_ac)
577534eadddSTiger Yang {
578534eadddSTiger Yang 	int ret = 0;
579534eadddSTiger Yang 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
580534eadddSTiger Yang 	int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
581534eadddSTiger Yang 						 si->value_len);
582534eadddSTiger Yang 
583534eadddSTiger Yang 	/*
584534eadddSTiger Yang 	 * The max space of security xattr taken inline is
585534eadddSTiger Yang 	 * 256(name) + 80(value) + 16(entry) = 352 bytes,
586534eadddSTiger Yang 	 * So reserve one metadata block for it is ok.
587534eadddSTiger Yang 	 */
588534eadddSTiger Yang 	if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
589534eadddSTiger Yang 	    s_size > OCFS2_XATTR_FREE_IN_IBODY) {
590534eadddSTiger Yang 		ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
591534eadddSTiger Yang 		if (ret) {
592534eadddSTiger Yang 			mlog_errno(ret);
593534eadddSTiger Yang 			return ret;
594534eadddSTiger Yang 		}
595534eadddSTiger Yang 		*xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
596534eadddSTiger Yang 	}
597534eadddSTiger Yang 
598534eadddSTiger Yang 	/* reserve clusters for xattr value which will be set in B tree*/
5990e445b6fSTiger Yang 	if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
6000e445b6fSTiger Yang 		int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
601534eadddSTiger Yang 							    si->value_len);
6020e445b6fSTiger Yang 
6030e445b6fSTiger Yang 		*xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
6040e445b6fSTiger Yang 							   new_clusters);
6050e445b6fSTiger Yang 		*want_clusters += new_clusters;
6060e445b6fSTiger Yang 	}
607534eadddSTiger Yang 	return ret;
608534eadddSTiger Yang }
609534eadddSTiger Yang 
ocfs2_calc_xattr_init(struct inode * dir,struct buffer_head * dir_bh,umode_t mode,struct ocfs2_security_xattr_info * si,int * want_clusters,int * xattr_credits,int * want_meta)61089c38bd0STiger Yang int ocfs2_calc_xattr_init(struct inode *dir,
61189c38bd0STiger Yang 			  struct buffer_head *dir_bh,
61267697cbdSAl Viro 			  umode_t mode,
61389c38bd0STiger Yang 			  struct ocfs2_security_xattr_info *si,
61489c38bd0STiger Yang 			  int *want_clusters,
61589c38bd0STiger Yang 			  int *xattr_credits,
6169b7895efSMark Fasheh 			  int *want_meta)
61789c38bd0STiger Yang {
61889c38bd0STiger Yang 	int ret = 0;
61989c38bd0STiger Yang 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
6200e445b6fSTiger Yang 	int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
62189c38bd0STiger Yang 
62289c38bd0STiger Yang 	if (si->enable)
62389c38bd0STiger Yang 		s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
62489c38bd0STiger Yang 						     si->value_len);
62589c38bd0STiger Yang 
62689c38bd0STiger Yang 	if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
62716c8d569Spiaojun 		down_read(&OCFS2_I(dir)->ip_xattr_sem);
62889c38bd0STiger Yang 		acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
62989c38bd0STiger Yang 					OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
63089c38bd0STiger Yang 					"", NULL, 0);
63116c8d569Spiaojun 		up_read(&OCFS2_I(dir)->ip_xattr_sem);
63289c38bd0STiger Yang 		if (acl_len > 0) {
63389c38bd0STiger Yang 			a_size = ocfs2_xattr_entry_real_size(0, acl_len);
63489c38bd0STiger Yang 			if (S_ISDIR(mode))
63589c38bd0STiger Yang 				a_size <<= 1;
63689c38bd0STiger Yang 		} else if (acl_len != 0 && acl_len != -ENODATA) {
637c0a1a6d7Spiaojun 			ret = acl_len;
63889c38bd0STiger Yang 			mlog_errno(ret);
63989c38bd0STiger Yang 			return ret;
64089c38bd0STiger Yang 		}
64189c38bd0STiger Yang 	}
64289c38bd0STiger Yang 
64389c38bd0STiger Yang 	if (!(s_size + a_size))
64489c38bd0STiger Yang 		return ret;
64589c38bd0STiger Yang 
64689c38bd0STiger Yang 	/*
64789c38bd0STiger Yang 	 * The max space of security xattr taken inline is
64889c38bd0STiger Yang 	 * 256(name) + 80(value) + 16(entry) = 352 bytes,
64989c38bd0STiger Yang 	 * The max space of acl xattr taken inline is
65089c38bd0STiger Yang 	 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
65189c38bd0STiger Yang 	 * when blocksize = 512, may reserve one more cluser for
65289c38bd0STiger Yang 	 * xattr bucket, otherwise reserve one metadata block
65389c38bd0STiger Yang 	 * for them is ok.
6546c9fd1dcSTiger Yang 	 * If this is a new directory with inline data,
6556c9fd1dcSTiger Yang 	 * we choose to reserve the entire inline area for
6566c9fd1dcSTiger Yang 	 * directory contents and force an external xattr block.
65789c38bd0STiger Yang 	 */
65889c38bd0STiger Yang 	if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
6596c9fd1dcSTiger Yang 	    (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
66089c38bd0STiger Yang 	    (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
6619b7895efSMark Fasheh 		*want_meta = *want_meta + 1;
66289c38bd0STiger Yang 		*xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
66389c38bd0STiger Yang 	}
66489c38bd0STiger Yang 
66589c38bd0STiger Yang 	if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
66689c38bd0STiger Yang 	    (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
66789c38bd0STiger Yang 		*want_clusters += 1;
66889c38bd0STiger Yang 		*xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
66989c38bd0STiger Yang 	}
67089c38bd0STiger Yang 
6710e445b6fSTiger Yang 	/*
6720e445b6fSTiger Yang 	 * reserve credits and clusters for xattrs which has large value
6730e445b6fSTiger Yang 	 * and have to be set outside
6740e445b6fSTiger Yang 	 */
6750e445b6fSTiger Yang 	if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
6760e445b6fSTiger Yang 		new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
67789c38bd0STiger Yang 							si->value_len);
6780e445b6fSTiger Yang 		*xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
6790e445b6fSTiger Yang 							   new_clusters);
6800e445b6fSTiger Yang 		*want_clusters += new_clusters;
6810e445b6fSTiger Yang 	}
68289c38bd0STiger Yang 	if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
68389c38bd0STiger Yang 	    acl_len > OCFS2_XATTR_INLINE_SIZE) {
6840e445b6fSTiger Yang 		/* for directory, it has DEFAULT and ACCESS two types of acls */
6850e445b6fSTiger Yang 		new_clusters = (S_ISDIR(mode) ? 2 : 1) *
6860e445b6fSTiger Yang 				ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
6870e445b6fSTiger Yang 		*xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
6880e445b6fSTiger Yang 							   new_clusters);
6890e445b6fSTiger Yang 		*want_clusters += new_clusters;
69089c38bd0STiger Yang 	}
69189c38bd0STiger Yang 
69289c38bd0STiger Yang 	return ret;
69389c38bd0STiger Yang }
69489c38bd0STiger Yang 
ocfs2_xattr_extend_allocation(struct inode * inode,u32 clusters_to_add,struct ocfs2_xattr_value_buf * vb,struct ocfs2_xattr_set_ctxt * ctxt)695f56654c4STao Ma static int ocfs2_xattr_extend_allocation(struct inode *inode,
696f56654c4STao Ma 					 u32 clusters_to_add,
69719b801f4SJoel Becker 					 struct ocfs2_xattr_value_buf *vb,
69878f30c31STao Ma 					 struct ocfs2_xattr_set_ctxt *ctxt)
699f56654c4STao Ma {
700a78f9f46STao Ma 	int status = 0, credits;
70185db90e7STao Ma 	handle_t *handle = ctxt->handle;
702f56654c4STao Ma 	enum ocfs2_alloc_restarted why;
70319b801f4SJoel Becker 	u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
704f99b9b7cSJoel Becker 	struct ocfs2_extent_tree et;
705f56654c4STao Ma 
7065e404e9eSJoel Becker 	ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
707f99b9b7cSJoel Becker 
708a78f9f46STao Ma 	while (clusters_to_add) {
709402b4183STao Ma 		trace_ocfs2_xattr_extend_allocation(clusters_to_add);
710402b4183STao Ma 
7110cf2f763SJoel Becker 		status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
712f56654c4STao Ma 				       OCFS2_JOURNAL_ACCESS_WRITE);
713f56654c4STao Ma 		if (status < 0) {
714f56654c4STao Ma 			mlog_errno(status);
715a78f9f46STao Ma 			break;
716f56654c4STao Ma 		}
717f56654c4STao Ma 
71819b801f4SJoel Becker 		prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
719cbee7e1aSJoel Becker 		status = ocfs2_add_clusters_in_btree(handle,
720cbee7e1aSJoel Becker 						     &et,
721f56654c4STao Ma 						     &logical_start,
722f56654c4STao Ma 						     clusters_to_add,
723f56654c4STao Ma 						     0,
72478f30c31STao Ma 						     ctxt->data_ac,
72578f30c31STao Ma 						     ctxt->meta_ac,
726f99b9b7cSJoel Becker 						     &why);
727a78f9f46STao Ma 		if ((status < 0) && (status != -EAGAIN)) {
728a78f9f46STao Ma 			if (status != -ENOSPC)
729f56654c4STao Ma 				mlog_errno(status);
730a78f9f46STao Ma 			break;
731f56654c4STao Ma 		}
732f56654c4STao Ma 
733ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, vb->vb_bh);
734f56654c4STao Ma 
735a78f9f46STao Ma 		clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) -
736a78f9f46STao Ma 					 prev_clusters;
737f56654c4STao Ma 
738a78f9f46STao Ma 		if (why != RESTART_NONE && clusters_to_add) {
73985db90e7STao Ma 			/*
740a78f9f46STao Ma 			 * We can only fail in case the alloc file doesn't give
741a78f9f46STao Ma 			 * up enough clusters.
74285db90e7STao Ma 			 */
743a78f9f46STao Ma 			BUG_ON(why == RESTART_META);
744f56654c4STao Ma 
745a78f9f46STao Ma 			credits = ocfs2_calc_extend_credits(inode->i_sb,
74606f9da6eSGoldwyn Rodrigues 							    &vb->vb_xv->xr_list);
747a78f9f46STao Ma 			status = ocfs2_extend_trans(handle, credits);
748a78f9f46STao Ma 			if (status < 0) {
749a78f9f46STao Ma 				status = -ENOMEM;
750a78f9f46STao Ma 				mlog_errno(status);
751a78f9f46STao Ma 				break;
752a78f9f46STao Ma 			}
753a78f9f46STao Ma 		}
754a78f9f46STao Ma 	}
755f56654c4STao Ma 
756f56654c4STao Ma 	return status;
757f56654c4STao Ma }
758f56654c4STao Ma 
__ocfs2_remove_xattr_range(struct inode * inode,struct ocfs2_xattr_value_buf * vb,u32 cpos,u32 phys_cpos,u32 len,unsigned int ext_flags,struct ocfs2_xattr_set_ctxt * ctxt)759f56654c4STao Ma static int __ocfs2_remove_xattr_range(struct inode *inode,
760d72cc72dSJoel Becker 				      struct ocfs2_xattr_value_buf *vb,
761f56654c4STao Ma 				      u32 cpos, u32 phys_cpos, u32 len,
762492a8a33STao Ma 				      unsigned int ext_flags,
76378f30c31STao Ma 				      struct ocfs2_xattr_set_ctxt *ctxt)
764f56654c4STao Ma {
765f56654c4STao Ma 	int ret;
766f56654c4STao Ma 	u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
76785db90e7STao Ma 	handle_t *handle = ctxt->handle;
768f99b9b7cSJoel Becker 	struct ocfs2_extent_tree et;
769f56654c4STao Ma 
7705e404e9eSJoel Becker 	ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
771f99b9b7cSJoel Becker 
7720cf2f763SJoel Becker 	ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
773f56654c4STao Ma 			    OCFS2_JOURNAL_ACCESS_WRITE);
774f56654c4STao Ma 	if (ret) {
775f56654c4STao Ma 		mlog_errno(ret);
77685db90e7STao Ma 		goto out;
777f56654c4STao Ma 	}
778f56654c4STao Ma 
779dbdcf6a4SJoel Becker 	ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
78078f30c31STao Ma 				  &ctxt->dealloc);
781f56654c4STao Ma 	if (ret) {
782f56654c4STao Ma 		mlog_errno(ret);
78385db90e7STao Ma 		goto out;
784f56654c4STao Ma 	}
785f56654c4STao Ma 
786d72cc72dSJoel Becker 	le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
787ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, vb->vb_bh);
788f56654c4STao Ma 
789492a8a33STao Ma 	if (ext_flags & OCFS2_EXT_REFCOUNTED)
790492a8a33STao Ma 		ret = ocfs2_decrease_refcount(inode, handle,
791492a8a33STao Ma 					ocfs2_blocks_to_clusters(inode->i_sb,
792492a8a33STao Ma 								 phys_blkno),
793492a8a33STao Ma 					len, ctxt->meta_ac, &ctxt->dealloc, 1);
794492a8a33STao Ma 	else
795492a8a33STao Ma 		ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
796492a8a33STao Ma 						  phys_blkno, len);
797f56654c4STao Ma 	if (ret)
798f56654c4STao Ma 		mlog_errno(ret);
799f56654c4STao Ma 
800f56654c4STao Ma out:
801f56654c4STao Ma 	return ret;
802f56654c4STao Ma }
803f56654c4STao Ma 
ocfs2_xattr_shrink_size(struct inode * inode,u32 old_clusters,u32 new_clusters,struct ocfs2_xattr_value_buf * vb,struct ocfs2_xattr_set_ctxt * ctxt)804f56654c4STao Ma static int ocfs2_xattr_shrink_size(struct inode *inode,
805f56654c4STao Ma 				   u32 old_clusters,
806f56654c4STao Ma 				   u32 new_clusters,
80719b801f4SJoel Becker 				   struct ocfs2_xattr_value_buf *vb,
80878f30c31STao Ma 				   struct ocfs2_xattr_set_ctxt *ctxt)
809f56654c4STao Ma {
810f56654c4STao Ma 	int ret = 0;
811492a8a33STao Ma 	unsigned int ext_flags;
812f56654c4STao Ma 	u32 trunc_len, cpos, phys_cpos, alloc_size;
813f56654c4STao Ma 	u64 block;
814f56654c4STao Ma 
815f56654c4STao Ma 	if (old_clusters <= new_clusters)
816f56654c4STao Ma 		return 0;
817f56654c4STao Ma 
818f56654c4STao Ma 	cpos = new_clusters;
819f56654c4STao Ma 	trunc_len = old_clusters - new_clusters;
820f56654c4STao Ma 	while (trunc_len) {
821f56654c4STao Ma 		ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
822d72cc72dSJoel Becker 					       &alloc_size,
823492a8a33STao Ma 					       &vb->vb_xv->xr_list, &ext_flags);
824f56654c4STao Ma 		if (ret) {
825f56654c4STao Ma 			mlog_errno(ret);
826f56654c4STao Ma 			goto out;
827f56654c4STao Ma 		}
828f56654c4STao Ma 
829f56654c4STao Ma 		if (alloc_size > trunc_len)
830f56654c4STao Ma 			alloc_size = trunc_len;
831f56654c4STao Ma 
83219b801f4SJoel Becker 		ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
833f56654c4STao Ma 						 phys_cpos, alloc_size,
834492a8a33STao Ma 						 ext_flags, ctxt);
835f56654c4STao Ma 		if (ret) {
836f56654c4STao Ma 			mlog_errno(ret);
837f56654c4STao Ma 			goto out;
838f56654c4STao Ma 		}
839f56654c4STao Ma 
840f56654c4STao Ma 		block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
8418cb471e8SJoel Becker 		ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
8428cb471e8SJoel Becker 						       block, alloc_size);
843f56654c4STao Ma 		cpos += alloc_size;
844f56654c4STao Ma 		trunc_len -= alloc_size;
845f56654c4STao Ma 	}
846f56654c4STao Ma 
847f56654c4STao Ma out:
848f56654c4STao Ma 	return ret;
849f56654c4STao Ma }
850f56654c4STao Ma 
ocfs2_xattr_value_truncate(struct inode * inode,struct ocfs2_xattr_value_buf * vb,int len,struct ocfs2_xattr_set_ctxt * ctxt)851f56654c4STao Ma static int ocfs2_xattr_value_truncate(struct inode *inode,
852b3e5d379SJoel Becker 				      struct ocfs2_xattr_value_buf *vb,
85378f30c31STao Ma 				      int len,
85478f30c31STao Ma 				      struct ocfs2_xattr_set_ctxt *ctxt)
855f56654c4STao Ma {
856f56654c4STao Ma 	int ret;
857f56654c4STao Ma 	u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
858b3e5d379SJoel Becker 	u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
859f56654c4STao Ma 
860f56654c4STao Ma 	if (new_clusters == old_clusters)
861f56654c4STao Ma 		return 0;
862f56654c4STao Ma 
863f56654c4STao Ma 	if (new_clusters > old_clusters)
864f56654c4STao Ma 		ret = ocfs2_xattr_extend_allocation(inode,
865f56654c4STao Ma 						    new_clusters - old_clusters,
866b3e5d379SJoel Becker 						    vb, ctxt);
867f56654c4STao Ma 	else
868f56654c4STao Ma 		ret = ocfs2_xattr_shrink_size(inode,
869f56654c4STao Ma 					      old_clusters, new_clusters,
870b3e5d379SJoel Becker 					      vb, ctxt);
871f56654c4STao Ma 
872f56654c4STao Ma 	return ret;
873f56654c4STao Ma }
874cf1d6c76STiger Yang 
ocfs2_xattr_list_entry(struct super_block * sb,char * buffer,size_t size,size_t * result,int type,const char * name,int name_len)8751046cb11SAndreas Gruenbacher static int ocfs2_xattr_list_entry(struct super_block *sb,
8761046cb11SAndreas Gruenbacher 				  char *buffer, size_t size,
8771046cb11SAndreas Gruenbacher 				  size_t *result, int type,
878936b8834STao Ma 				  const char *name, int name_len)
879936b8834STao Ma {
880936b8834STao Ma 	char *p = buffer + *result;
8811046cb11SAndreas Gruenbacher 	const char *prefix;
8821046cb11SAndreas Gruenbacher 	int prefix_len;
8831046cb11SAndreas Gruenbacher 	int total_len;
884936b8834STao Ma 
8851046cb11SAndreas Gruenbacher 	switch(type) {
8861046cb11SAndreas Gruenbacher 	case OCFS2_XATTR_INDEX_USER:
8871046cb11SAndreas Gruenbacher 		if (OCFS2_SB(sb)->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
8881046cb11SAndreas Gruenbacher 			return 0;
8891046cb11SAndreas Gruenbacher 		break;
8901046cb11SAndreas Gruenbacher 
8911046cb11SAndreas Gruenbacher 	case OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS:
8921046cb11SAndreas Gruenbacher 	case OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT:
8931751e8a6SLinus Torvalds 		if (!(sb->s_flags & SB_POSIXACL))
8941046cb11SAndreas Gruenbacher 			return 0;
8951046cb11SAndreas Gruenbacher 		break;
8961046cb11SAndreas Gruenbacher 
8971046cb11SAndreas Gruenbacher 	case OCFS2_XATTR_INDEX_TRUSTED:
8981046cb11SAndreas Gruenbacher 		if (!capable(CAP_SYS_ADMIN))
8991046cb11SAndreas Gruenbacher 			return 0;
9001046cb11SAndreas Gruenbacher 		break;
9011046cb11SAndreas Gruenbacher 	}
9021046cb11SAndreas Gruenbacher 
9031046cb11SAndreas Gruenbacher 	prefix = ocfs2_xattr_prefix(type);
9041046cb11SAndreas Gruenbacher 	if (!prefix)
9051046cb11SAndreas Gruenbacher 		return 0;
9061046cb11SAndreas Gruenbacher 	prefix_len = strlen(prefix);
9071046cb11SAndreas Gruenbacher 	total_len = prefix_len + name_len + 1;
908936b8834STao Ma 	*result += total_len;
909936b8834STao Ma 
910936b8834STao Ma 	/* we are just looking for how big our buffer needs to be */
911936b8834STao Ma 	if (!size)
912936b8834STao Ma 		return 0;
913936b8834STao Ma 
914936b8834STao Ma 	if (*result > size)
915936b8834STao Ma 		return -ERANGE;
916936b8834STao Ma 
917936b8834STao Ma 	memcpy(p, prefix, prefix_len);
918936b8834STao Ma 	memcpy(p + prefix_len, name, name_len);
919936b8834STao Ma 	p[prefix_len + name_len] = '\0';
920936b8834STao Ma 
921936b8834STao Ma 	return 0;
922936b8834STao Ma }
923936b8834STao Ma 
ocfs2_xattr_list_entries(struct inode * inode,struct ocfs2_xattr_header * header,char * buffer,size_t buffer_size)924cf1d6c76STiger Yang static int ocfs2_xattr_list_entries(struct inode *inode,
925cf1d6c76STiger Yang 				    struct ocfs2_xattr_header *header,
926cf1d6c76STiger Yang 				    char *buffer, size_t buffer_size)
927cf1d6c76STiger Yang {
928936b8834STao Ma 	size_t result = 0;
929936b8834STao Ma 	int i, type, ret;
9301046cb11SAndreas Gruenbacher 	const char *name;
931cf1d6c76STiger Yang 
932cf1d6c76STiger Yang 	for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
933cf1d6c76STiger Yang 		struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
934936b8834STao Ma 		type = ocfs2_xattr_get_type(entry);
935936b8834STao Ma 		name = (const char *)header +
936936b8834STao Ma 			le16_to_cpu(entry->xe_name_offset);
937936b8834STao Ma 
9381046cb11SAndreas Gruenbacher 		ret = ocfs2_xattr_list_entry(inode->i_sb,
9391046cb11SAndreas Gruenbacher 					     buffer, buffer_size,
9401046cb11SAndreas Gruenbacher 					     &result, type, name,
941cf1d6c76STiger Yang 					     entry->xe_name_len);
942936b8834STao Ma 		if (ret)
943936b8834STao Ma 			return ret;
944cf1d6c76STiger Yang 	}
945cf1d6c76STiger Yang 
946936b8834STao Ma 	return result;
947cf1d6c76STiger Yang }
948cf1d6c76STiger Yang 
ocfs2_has_inline_xattr_value_outside(struct inode * inode,struct ocfs2_dinode * di)9498b2c0dbaSTao Ma int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
9508b2c0dbaSTao Ma 					 struct ocfs2_dinode *di)
9518b2c0dbaSTao Ma {
9528b2c0dbaSTao Ma 	struct ocfs2_xattr_header *xh;
9538b2c0dbaSTao Ma 	int i;
9548b2c0dbaSTao Ma 
9558b2c0dbaSTao Ma 	xh = (struct ocfs2_xattr_header *)
9568b2c0dbaSTao Ma 		 ((void *)di + inode->i_sb->s_blocksize -
9578b2c0dbaSTao Ma 		 le16_to_cpu(di->i_xattr_inline_size));
9588b2c0dbaSTao Ma 
9598b2c0dbaSTao Ma 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
9608b2c0dbaSTao Ma 		if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
9618b2c0dbaSTao Ma 			return 1;
9628b2c0dbaSTao Ma 
9638b2c0dbaSTao Ma 	return 0;
9648b2c0dbaSTao Ma }
9658b2c0dbaSTao Ma 
ocfs2_xattr_ibody_list(struct inode * inode,struct ocfs2_dinode * di,char * buffer,size_t buffer_size)966cf1d6c76STiger Yang static int ocfs2_xattr_ibody_list(struct inode *inode,
967cf1d6c76STiger Yang 				  struct ocfs2_dinode *di,
968cf1d6c76STiger Yang 				  char *buffer,
969cf1d6c76STiger Yang 				  size_t buffer_size)
970cf1d6c76STiger Yang {
971cf1d6c76STiger Yang 	struct ocfs2_xattr_header *header = NULL;
972cf1d6c76STiger Yang 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
973cf1d6c76STiger Yang 	int ret = 0;
974cf1d6c76STiger Yang 
975cf1d6c76STiger Yang 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
976cf1d6c76STiger Yang 		return ret;
977cf1d6c76STiger Yang 
978cf1d6c76STiger Yang 	header = (struct ocfs2_xattr_header *)
979cf1d6c76STiger Yang 		 ((void *)di + inode->i_sb->s_blocksize -
980cf1d6c76STiger Yang 		 le16_to_cpu(di->i_xattr_inline_size));
981cf1d6c76STiger Yang 
982cf1d6c76STiger Yang 	ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
983cf1d6c76STiger Yang 
984cf1d6c76STiger Yang 	return ret;
985cf1d6c76STiger Yang }
986cf1d6c76STiger Yang 
ocfs2_xattr_block_list(struct inode * inode,struct ocfs2_dinode * di,char * buffer,size_t buffer_size)987cf1d6c76STiger Yang static int ocfs2_xattr_block_list(struct inode *inode,
988cf1d6c76STiger Yang 				  struct ocfs2_dinode *di,
989cf1d6c76STiger Yang 				  char *buffer,
990cf1d6c76STiger Yang 				  size_t buffer_size)
991cf1d6c76STiger Yang {
992cf1d6c76STiger Yang 	struct buffer_head *blk_bh = NULL;
9930c044f0bSTao Ma 	struct ocfs2_xattr_block *xb;
994cf1d6c76STiger Yang 	int ret = 0;
995cf1d6c76STiger Yang 
996cf1d6c76STiger Yang 	if (!di->i_xattr_loc)
997cf1d6c76STiger Yang 		return ret;
998cf1d6c76STiger Yang 
9994ae1d69bSJoel Becker 	ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
10004ae1d69bSJoel Becker 				     &blk_bh);
1001cf1d6c76STiger Yang 	if (ret < 0) {
1002cf1d6c76STiger Yang 		mlog_errno(ret);
1003cf1d6c76STiger Yang 		return ret;
1004cf1d6c76STiger Yang 	}
1005cf1d6c76STiger Yang 
10060c044f0bSTao Ma 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
10070c044f0bSTao Ma 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
10080c044f0bSTao Ma 		struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
10090c044f0bSTao Ma 		ret = ocfs2_xattr_list_entries(inode, header,
10100c044f0bSTao Ma 					       buffer, buffer_size);
101147bca495STao Ma 	} else
101247bca495STao Ma 		ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
10130c044f0bSTao Ma 						   buffer, buffer_size);
10144ae1d69bSJoel Becker 
1015cf1d6c76STiger Yang 	brelse(blk_bh);
1016cf1d6c76STiger Yang 
1017cf1d6c76STiger Yang 	return ret;
1018cf1d6c76STiger Yang }
1019cf1d6c76STiger Yang 
ocfs2_listxattr(struct dentry * dentry,char * buffer,size_t size)1020cf1d6c76STiger Yang ssize_t ocfs2_listxattr(struct dentry *dentry,
1021cf1d6c76STiger Yang 			char *buffer,
1022cf1d6c76STiger Yang 			size_t size)
1023cf1d6c76STiger Yang {
1024cf1d6c76STiger Yang 	int ret = 0, i_ret = 0, b_ret = 0;
1025cf1d6c76STiger Yang 	struct buffer_head *di_bh = NULL;
1026cf1d6c76STiger Yang 	struct ocfs2_dinode *di = NULL;
10272b0143b5SDavid Howells 	struct ocfs2_inode_info *oi = OCFS2_I(d_inode(dentry));
1028cf1d6c76STiger Yang 
10298154da3dSTiger Yang 	if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
10308154da3dSTiger Yang 		return -EOPNOTSUPP;
10318154da3dSTiger Yang 
1032cf1d6c76STiger Yang 	if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1033cf1d6c76STiger Yang 		return ret;
1034cf1d6c76STiger Yang 
10352b0143b5SDavid Howells 	ret = ocfs2_inode_lock(d_inode(dentry), &di_bh, 0);
1036cf1d6c76STiger Yang 	if (ret < 0) {
1037cf1d6c76STiger Yang 		mlog_errno(ret);
1038cf1d6c76STiger Yang 		return ret;
1039cf1d6c76STiger Yang 	}
1040cf1d6c76STiger Yang 
1041cf1d6c76STiger Yang 	di = (struct ocfs2_dinode *)di_bh->b_data;
1042cf1d6c76STiger Yang 
1043cf1d6c76STiger Yang 	down_read(&oi->ip_xattr_sem);
10442b0143b5SDavid Howells 	i_ret = ocfs2_xattr_ibody_list(d_inode(dentry), di, buffer, size);
1045cf1d6c76STiger Yang 	if (i_ret < 0)
1046cf1d6c76STiger Yang 		b_ret = 0;
1047cf1d6c76STiger Yang 	else {
1048cf1d6c76STiger Yang 		if (buffer) {
1049cf1d6c76STiger Yang 			buffer += i_ret;
1050cf1d6c76STiger Yang 			size -= i_ret;
1051cf1d6c76STiger Yang 		}
10522b0143b5SDavid Howells 		b_ret = ocfs2_xattr_block_list(d_inode(dentry), di,
1053cf1d6c76STiger Yang 					       buffer, size);
1054cf1d6c76STiger Yang 		if (b_ret < 0)
1055cf1d6c76STiger Yang 			i_ret = 0;
1056cf1d6c76STiger Yang 	}
1057cf1d6c76STiger Yang 	up_read(&oi->ip_xattr_sem);
10582b0143b5SDavid Howells 	ocfs2_inode_unlock(d_inode(dentry), 0);
1059cf1d6c76STiger Yang 
1060cf1d6c76STiger Yang 	brelse(di_bh);
1061cf1d6c76STiger Yang 
1062cf1d6c76STiger Yang 	return i_ret + b_ret;
1063cf1d6c76STiger Yang }
1064cf1d6c76STiger Yang 
ocfs2_xattr_find_entry(int name_index,const char * name,struct ocfs2_xattr_search * xs)1065cf1d6c76STiger Yang static int ocfs2_xattr_find_entry(int name_index,
1066cf1d6c76STiger Yang 				  const char *name,
1067cf1d6c76STiger Yang 				  struct ocfs2_xattr_search *xs)
1068cf1d6c76STiger Yang {
1069cf1d6c76STiger Yang 	struct ocfs2_xattr_entry *entry;
1070cf1d6c76STiger Yang 	size_t name_len;
1071cf1d6c76STiger Yang 	int i, cmp = 1;
1072cf1d6c76STiger Yang 
1073cf1d6c76STiger Yang 	if (name == NULL)
1074cf1d6c76STiger Yang 		return -EINVAL;
1075cf1d6c76STiger Yang 
1076cf1d6c76STiger Yang 	name_len = strlen(name);
1077cf1d6c76STiger Yang 	entry = xs->here;
1078cf1d6c76STiger Yang 	for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1079cf1d6c76STiger Yang 		cmp = name_index - ocfs2_xattr_get_type(entry);
1080cf1d6c76STiger Yang 		if (!cmp)
1081cf1d6c76STiger Yang 			cmp = name_len - entry->xe_name_len;
1082cf1d6c76STiger Yang 		if (!cmp)
1083cf1d6c76STiger Yang 			cmp = memcmp(name, (xs->base +
1084cf1d6c76STiger Yang 				     le16_to_cpu(entry->xe_name_offset)),
1085cf1d6c76STiger Yang 				     name_len);
1086cf1d6c76STiger Yang 		if (cmp == 0)
1087cf1d6c76STiger Yang 			break;
1088cf1d6c76STiger Yang 		entry += 1;
1089cf1d6c76STiger Yang 	}
1090cf1d6c76STiger Yang 	xs->here = entry;
1091cf1d6c76STiger Yang 
1092cf1d6c76STiger Yang 	return cmp ? -ENODATA : 0;
1093cf1d6c76STiger Yang }
1094cf1d6c76STiger Yang 
ocfs2_xattr_get_value_outside(struct inode * inode,struct ocfs2_xattr_value_root * xv,void * buffer,size_t len)1095cf1d6c76STiger Yang static int ocfs2_xattr_get_value_outside(struct inode *inode,
1096589dc260STao Ma 					 struct ocfs2_xattr_value_root *xv,
1097cf1d6c76STiger Yang 					 void *buffer,
1098cf1d6c76STiger Yang 					 size_t len)
1099cf1d6c76STiger Yang {
1100cf1d6c76STiger Yang 	u32 cpos, p_cluster, num_clusters, bpc, clusters;
1101cf1d6c76STiger Yang 	u64 blkno;
1102cf1d6c76STiger Yang 	int i, ret = 0;
1103cf1d6c76STiger Yang 	size_t cplen, blocksize;
1104cf1d6c76STiger Yang 	struct buffer_head *bh = NULL;
1105cf1d6c76STiger Yang 	struct ocfs2_extent_list *el;
1106cf1d6c76STiger Yang 
1107cf1d6c76STiger Yang 	el = &xv->xr_list;
1108cf1d6c76STiger Yang 	clusters = le32_to_cpu(xv->xr_clusters);
1109cf1d6c76STiger Yang 	bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1110cf1d6c76STiger Yang 	blocksize = inode->i_sb->s_blocksize;
1111cf1d6c76STiger Yang 
1112cf1d6c76STiger Yang 	cpos = 0;
1113cf1d6c76STiger Yang 	while (cpos < clusters) {
1114cf1d6c76STiger Yang 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
11151061f9c1STao Ma 					       &num_clusters, el, NULL);
1116cf1d6c76STiger Yang 		if (ret) {
1117cf1d6c76STiger Yang 			mlog_errno(ret);
1118cf1d6c76STiger Yang 			goto out;
1119cf1d6c76STiger Yang 		}
1120cf1d6c76STiger Yang 
1121cf1d6c76STiger Yang 		blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1122cf1d6c76STiger Yang 		/* Copy ocfs2_xattr_value */
1123cf1d6c76STiger Yang 		for (i = 0; i < num_clusters * bpc; i++, blkno++) {
11248cb471e8SJoel Becker 			ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
11258cb471e8SJoel Becker 					       &bh, NULL);
1126cf1d6c76STiger Yang 			if (ret) {
1127cf1d6c76STiger Yang 				mlog_errno(ret);
1128cf1d6c76STiger Yang 				goto out;
1129cf1d6c76STiger Yang 			}
1130cf1d6c76STiger Yang 
1131cf1d6c76STiger Yang 			cplen = len >= blocksize ? blocksize : len;
1132cf1d6c76STiger Yang 			memcpy(buffer, bh->b_data, cplen);
1133cf1d6c76STiger Yang 			len -= cplen;
1134cf1d6c76STiger Yang 			buffer += cplen;
1135cf1d6c76STiger Yang 
1136cf1d6c76STiger Yang 			brelse(bh);
1137cf1d6c76STiger Yang 			bh = NULL;
1138cf1d6c76STiger Yang 			if (len == 0)
1139cf1d6c76STiger Yang 				break;
1140cf1d6c76STiger Yang 		}
1141cf1d6c76STiger Yang 		cpos += num_clusters;
1142cf1d6c76STiger Yang 	}
1143cf1d6c76STiger Yang out:
1144cf1d6c76STiger Yang 	return ret;
1145cf1d6c76STiger Yang }
1146cf1d6c76STiger Yang 
ocfs2_xattr_ibody_get(struct inode * inode,int name_index,const char * name,void * buffer,size_t buffer_size,struct ocfs2_xattr_search * xs)1147cf1d6c76STiger Yang static int ocfs2_xattr_ibody_get(struct inode *inode,
1148cf1d6c76STiger Yang 				 int name_index,
1149cf1d6c76STiger Yang 				 const char *name,
1150cf1d6c76STiger Yang 				 void *buffer,
1151cf1d6c76STiger Yang 				 size_t buffer_size,
1152cf1d6c76STiger Yang 				 struct ocfs2_xattr_search *xs)
1153cf1d6c76STiger Yang {
1154cf1d6c76STiger Yang 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1155cf1d6c76STiger Yang 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1156589dc260STao Ma 	struct ocfs2_xattr_value_root *xv;
1157cf1d6c76STiger Yang 	size_t size;
1158cf1d6c76STiger Yang 	int ret = 0;
1159cf1d6c76STiger Yang 
1160cf1d6c76STiger Yang 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1161cf1d6c76STiger Yang 		return -ENODATA;
1162cf1d6c76STiger Yang 
1163cf1d6c76STiger Yang 	xs->end = (void *)di + inode->i_sb->s_blocksize;
1164cf1d6c76STiger Yang 	xs->header = (struct ocfs2_xattr_header *)
1165cf1d6c76STiger Yang 			(xs->end - le16_to_cpu(di->i_xattr_inline_size));
1166cf1d6c76STiger Yang 	xs->base = (void *)xs->header;
1167cf1d6c76STiger Yang 	xs->here = xs->header->xh_entries;
1168cf1d6c76STiger Yang 
1169cf1d6c76STiger Yang 	ret = ocfs2_xattr_find_entry(name_index, name, xs);
1170cf1d6c76STiger Yang 	if (ret)
1171cf1d6c76STiger Yang 		return ret;
1172cf1d6c76STiger Yang 	size = le64_to_cpu(xs->here->xe_value_size);
1173cf1d6c76STiger Yang 	if (buffer) {
1174cf1d6c76STiger Yang 		if (size > buffer_size)
1175cf1d6c76STiger Yang 			return -ERANGE;
1176cf1d6c76STiger Yang 		if (ocfs2_xattr_is_local(xs->here)) {
1177cf1d6c76STiger Yang 			memcpy(buffer, (void *)xs->base +
1178cf1d6c76STiger Yang 			       le16_to_cpu(xs->here->xe_name_offset) +
1179cf1d6c76STiger Yang 			       OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1180cf1d6c76STiger Yang 		} else {
1181589dc260STao Ma 			xv = (struct ocfs2_xattr_value_root *)
1182589dc260STao Ma 				(xs->base + le16_to_cpu(
1183589dc260STao Ma 				 xs->here->xe_name_offset) +
1184589dc260STao Ma 				OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1185589dc260STao Ma 			ret = ocfs2_xattr_get_value_outside(inode, xv,
1186cf1d6c76STiger Yang 							    buffer, size);
1187cf1d6c76STiger Yang 			if (ret < 0) {
1188cf1d6c76STiger Yang 				mlog_errno(ret);
1189cf1d6c76STiger Yang 				return ret;
1190cf1d6c76STiger Yang 			}
1191cf1d6c76STiger Yang 		}
1192cf1d6c76STiger Yang 	}
1193cf1d6c76STiger Yang 
1194cf1d6c76STiger Yang 	return size;
1195cf1d6c76STiger Yang }
1196cf1d6c76STiger Yang 
ocfs2_xattr_block_get(struct inode * inode,int name_index,const char * name,void * buffer,size_t buffer_size,struct ocfs2_xattr_search * xs)1197cf1d6c76STiger Yang static int ocfs2_xattr_block_get(struct inode *inode,
1198cf1d6c76STiger Yang 				 int name_index,
1199cf1d6c76STiger Yang 				 const char *name,
1200cf1d6c76STiger Yang 				 void *buffer,
1201cf1d6c76STiger Yang 				 size_t buffer_size,
1202cf1d6c76STiger Yang 				 struct ocfs2_xattr_search *xs)
1203cf1d6c76STiger Yang {
1204cf1d6c76STiger Yang 	struct ocfs2_xattr_block *xb;
1205589dc260STao Ma 	struct ocfs2_xattr_value_root *xv;
1206cf1d6c76STiger Yang 	size_t size;
120744d8e4e1SSubrata Modak 	int ret = -ENODATA, name_offset, name_len, i;
12083f649ab7SKees Cook 	int block_off;
1209cf1d6c76STiger Yang 
1210ba937127SJoel Becker 	xs->bucket = ocfs2_xattr_bucket_new(inode);
1211ba937127SJoel Becker 	if (!xs->bucket) {
1212ba937127SJoel Becker 		ret = -ENOMEM;
1213ba937127SJoel Becker 		mlog_errno(ret);
1214ba937127SJoel Becker 		goto cleanup;
1215ba937127SJoel Becker 	}
1216589dc260STao Ma 
121754f443f4SJoel Becker 	ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
121854f443f4SJoel Becker 	if (ret) {
1219cf1d6c76STiger Yang 		mlog_errno(ret);
1220cf1d6c76STiger Yang 		goto cleanup;
1221cf1d6c76STiger Yang 	}
1222cf1d6c76STiger Yang 
12236c1e183eSTiger Yang 	if (xs->not_found) {
12246c1e183eSTiger Yang 		ret = -ENODATA;
12256c1e183eSTiger Yang 		goto cleanup;
12266c1e183eSTiger Yang 	}
12276c1e183eSTiger Yang 
122854f443f4SJoel Becker 	xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1229cf1d6c76STiger Yang 	size = le64_to_cpu(xs->here->xe_value_size);
1230cf1d6c76STiger Yang 	if (buffer) {
1231cf1d6c76STiger Yang 		ret = -ERANGE;
1232cf1d6c76STiger Yang 		if (size > buffer_size)
1233cf1d6c76STiger Yang 			goto cleanup;
1234589dc260STao Ma 
1235589dc260STao Ma 		name_offset = le16_to_cpu(xs->here->xe_name_offset);
1236589dc260STao Ma 		name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1237589dc260STao Ma 		i = xs->here - xs->header->xh_entries;
1238589dc260STao Ma 
1239589dc260STao Ma 		if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1240fd68a894STao Ma 			ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1241ba937127SJoel Becker 								bucket_xh(xs->bucket),
1242589dc260STao Ma 								i,
1243589dc260STao Ma 								&block_off,
1244589dc260STao Ma 								&name_offset);
1245023d4ea3SJoseph Qi 			if (ret) {
1246023d4ea3SJoseph Qi 				mlog_errno(ret);
1247023d4ea3SJoseph Qi 				goto cleanup;
1248023d4ea3SJoseph Qi 			}
1249ba937127SJoel Becker 			xs->base = bucket_block(xs->bucket, block_off);
1250589dc260STao Ma 		}
1251cf1d6c76STiger Yang 		if (ocfs2_xattr_is_local(xs->here)) {
1252cf1d6c76STiger Yang 			memcpy(buffer, (void *)xs->base +
1253589dc260STao Ma 			       name_offset + name_len, size);
1254cf1d6c76STiger Yang 		} else {
1255589dc260STao Ma 			xv = (struct ocfs2_xattr_value_root *)
1256589dc260STao Ma 				(xs->base + name_offset + name_len);
1257589dc260STao Ma 			ret = ocfs2_xattr_get_value_outside(inode, xv,
1258cf1d6c76STiger Yang 							    buffer, size);
1259cf1d6c76STiger Yang 			if (ret < 0) {
1260cf1d6c76STiger Yang 				mlog_errno(ret);
1261cf1d6c76STiger Yang 				goto cleanup;
1262cf1d6c76STiger Yang 			}
1263cf1d6c76STiger Yang 		}
1264cf1d6c76STiger Yang 	}
1265cf1d6c76STiger Yang 	ret = size;
1266cf1d6c76STiger Yang cleanup:
1267ba937127SJoel Becker 	ocfs2_xattr_bucket_free(xs->bucket);
1268cf1d6c76STiger Yang 
126954f443f4SJoel Becker 	brelse(xs->xattr_bh);
127054f443f4SJoel Becker 	xs->xattr_bh = NULL;
1271cf1d6c76STiger Yang 	return ret;
1272cf1d6c76STiger Yang }
1273cf1d6c76STiger Yang 
ocfs2_xattr_get_nolock(struct inode * inode,struct buffer_head * di_bh,int name_index,const char * name,void * buffer,size_t buffer_size)12744e3e9d02STiger Yang int ocfs2_xattr_get_nolock(struct inode *inode,
12754e3e9d02STiger Yang 			   struct buffer_head *di_bh,
1276cf1d6c76STiger Yang 			   int name_index,
1277cf1d6c76STiger Yang 			   const char *name,
1278cf1d6c76STiger Yang 			   void *buffer,
1279cf1d6c76STiger Yang 			   size_t buffer_size)
1280cf1d6c76STiger Yang {
1281cf1d6c76STiger Yang 	int ret;
1282cf1d6c76STiger Yang 	struct ocfs2_dinode *di = NULL;
1283cf1d6c76STiger Yang 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1284cf1d6c76STiger Yang 	struct ocfs2_xattr_search xis = {
1285cf1d6c76STiger Yang 		.not_found = -ENODATA,
1286cf1d6c76STiger Yang 	};
1287cf1d6c76STiger Yang 	struct ocfs2_xattr_search xbs = {
1288cf1d6c76STiger Yang 		.not_found = -ENODATA,
1289cf1d6c76STiger Yang 	};
1290cf1d6c76STiger Yang 
12918154da3dSTiger Yang 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
12928154da3dSTiger Yang 		return -EOPNOTSUPP;
12938154da3dSTiger Yang 
1294cf1d6c76STiger Yang 	if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
12952b693005SJan Kara 		return -ENODATA;
1296cf1d6c76STiger Yang 
1297cf1d6c76STiger Yang 	xis.inode_bh = xbs.inode_bh = di_bh;
1298cf1d6c76STiger Yang 	di = (struct ocfs2_dinode *)di_bh->b_data;
1299cf1d6c76STiger Yang 
1300cf1d6c76STiger Yang 	ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1301cf1d6c76STiger Yang 				    buffer_size, &xis);
13026c1e183eSTiger Yang 	if (ret == -ENODATA && di->i_xattr_loc)
1303cf1d6c76STiger Yang 		ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1304cf1d6c76STiger Yang 					    buffer_size, &xbs);
13054e3e9d02STiger Yang 
13064e3e9d02STiger Yang 	return ret;
13074e3e9d02STiger Yang }
13084e3e9d02STiger Yang 
13094e3e9d02STiger Yang /* ocfs2_xattr_get()
13104e3e9d02STiger Yang  *
13114e3e9d02STiger Yang  * Copy an extended attribute into the buffer provided.
13124e3e9d02STiger Yang  * Buffer is NULL to compute the size of buffer required.
13134e3e9d02STiger Yang  */
ocfs2_xattr_get(struct inode * inode,int name_index,const char * name,void * buffer,size_t buffer_size)13144e3e9d02STiger Yang static int ocfs2_xattr_get(struct inode *inode,
13154e3e9d02STiger Yang 			   int name_index,
13164e3e9d02STiger Yang 			   const char *name,
13174e3e9d02STiger Yang 			   void *buffer,
13184e3e9d02STiger Yang 			   size_t buffer_size)
13194e3e9d02STiger Yang {
13208818efaaSEric Ren 	int ret, had_lock;
13214e3e9d02STiger Yang 	struct buffer_head *di_bh = NULL;
13228818efaaSEric Ren 	struct ocfs2_lock_holder oh;
13234e3e9d02STiger Yang 
13248818efaaSEric Ren 	had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh);
13258818efaaSEric Ren 	if (had_lock < 0) {
13268818efaaSEric Ren 		mlog_errno(had_lock);
13278818efaaSEric Ren 		return had_lock;
13284e3e9d02STiger Yang 	}
13295e64b0d9STao Ma 	down_read(&OCFS2_I(inode)->ip_xattr_sem);
13304e3e9d02STiger Yang 	ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
13314e3e9d02STiger Yang 				     name, buffer, buffer_size);
13325e64b0d9STao Ma 	up_read(&OCFS2_I(inode)->ip_xattr_sem);
13334e3e9d02STiger Yang 
13348818efaaSEric Ren 	ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
1335cf1d6c76STiger Yang 
1336cf1d6c76STiger Yang 	brelse(di_bh);
1337cf1d6c76STiger Yang 
1338cf1d6c76STiger Yang 	return ret;
1339cf1d6c76STiger Yang }
1340cf1d6c76STiger Yang 
__ocfs2_xattr_set_value_outside(struct inode * inode,handle_t * handle,struct ocfs2_xattr_value_buf * vb,const void * value,int value_len)1341cf1d6c76STiger Yang static int __ocfs2_xattr_set_value_outside(struct inode *inode,
134285db90e7STao Ma 					   handle_t *handle,
1343492a8a33STao Ma 					   struct ocfs2_xattr_value_buf *vb,
1344cf1d6c76STiger Yang 					   const void *value,
1345cf1d6c76STiger Yang 					   int value_len)
1346cf1d6c76STiger Yang {
134771d548a6STao Ma 	int ret = 0, i, cp_len;
1348cf1d6c76STiger Yang 	u16 blocksize = inode->i_sb->s_blocksize;
1349cf1d6c76STiger Yang 	u32 p_cluster, num_clusters;
1350cf1d6c76STiger Yang 	u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1351cf1d6c76STiger Yang 	u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1352cf1d6c76STiger Yang 	u64 blkno;
1353cf1d6c76STiger Yang 	struct buffer_head *bh = NULL;
1354492a8a33STao Ma 	unsigned int ext_flags;
1355492a8a33STao Ma 	struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1356cf1d6c76STiger Yang 
1357cf1d6c76STiger Yang 	BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1358cf1d6c76STiger Yang 
1359cf1d6c76STiger Yang 	while (cpos < clusters) {
1360cf1d6c76STiger Yang 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
13611061f9c1STao Ma 					       &num_clusters, &xv->xr_list,
1362492a8a33STao Ma 					       &ext_flags);
1363cf1d6c76STiger Yang 		if (ret) {
1364cf1d6c76STiger Yang 			mlog_errno(ret);
136585db90e7STao Ma 			goto out;
1366cf1d6c76STiger Yang 		}
1367cf1d6c76STiger Yang 
1368492a8a33STao Ma 		BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1369492a8a33STao Ma 
1370cf1d6c76STiger Yang 		blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1371cf1d6c76STiger Yang 
1372cf1d6c76STiger Yang 		for (i = 0; i < num_clusters * bpc; i++, blkno++) {
13738cb471e8SJoel Becker 			ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
13748cb471e8SJoel Becker 					       &bh, NULL);
1375cf1d6c76STiger Yang 			if (ret) {
1376cf1d6c76STiger Yang 				mlog_errno(ret);
137785db90e7STao Ma 				goto out;
1378cf1d6c76STiger Yang 			}
1379cf1d6c76STiger Yang 
1380cf1d6c76STiger Yang 			ret = ocfs2_journal_access(handle,
13810cf2f763SJoel Becker 						   INODE_CACHE(inode),
1382cf1d6c76STiger Yang 						   bh,
1383cf1d6c76STiger Yang 						   OCFS2_JOURNAL_ACCESS_WRITE);
1384cf1d6c76STiger Yang 			if (ret < 0) {
1385cf1d6c76STiger Yang 				mlog_errno(ret);
138685db90e7STao Ma 				goto out;
1387cf1d6c76STiger Yang 			}
1388cf1d6c76STiger Yang 
1389cf1d6c76STiger Yang 			cp_len = value_len > blocksize ? blocksize : value_len;
1390cf1d6c76STiger Yang 			memcpy(bh->b_data, value, cp_len);
1391cf1d6c76STiger Yang 			value_len -= cp_len;
1392cf1d6c76STiger Yang 			value += cp_len;
1393cf1d6c76STiger Yang 			if (cp_len < blocksize)
1394cf1d6c76STiger Yang 				memset(bh->b_data + cp_len, 0,
1395cf1d6c76STiger Yang 				       blocksize - cp_len);
1396cf1d6c76STiger Yang 
1397ec20cec7SJoel Becker 			ocfs2_journal_dirty(handle, bh);
1398cf1d6c76STiger Yang 			brelse(bh);
1399cf1d6c76STiger Yang 			bh = NULL;
1400cf1d6c76STiger Yang 
1401cf1d6c76STiger Yang 			/*
1402cf1d6c76STiger Yang 			 * XXX: do we need to empty all the following
1403cf1d6c76STiger Yang 			 * blocks in this cluster?
1404cf1d6c76STiger Yang 			 */
1405cf1d6c76STiger Yang 			if (!value_len)
1406cf1d6c76STiger Yang 				break;
1407cf1d6c76STiger Yang 		}
1408cf1d6c76STiger Yang 		cpos += num_clusters;
1409cf1d6c76STiger Yang 	}
1410cf1d6c76STiger Yang out:
1411cf1d6c76STiger Yang 	brelse(bh);
1412cf1d6c76STiger Yang 
1413cf1d6c76STiger Yang 	return ret;
1414cf1d6c76STiger Yang }
1415cf1d6c76STiger Yang 
ocfs2_xa_check_space_helper(int needed_space,int free_start,int num_entries)141669a3e539SJoel Becker static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
141769a3e539SJoel Becker 				       int num_entries)
141869a3e539SJoel Becker {
141969a3e539SJoel Becker 	int free_space;
142069a3e539SJoel Becker 
142169a3e539SJoel Becker 	if (!needed_space)
142269a3e539SJoel Becker 		return 0;
142369a3e539SJoel Becker 
142469a3e539SJoel Becker 	free_space = free_start -
142569a3e539SJoel Becker 		sizeof(struct ocfs2_xattr_header) -
142669a3e539SJoel Becker 		(num_entries * sizeof(struct ocfs2_xattr_entry)) -
142769a3e539SJoel Becker 		OCFS2_XATTR_HEADER_GAP;
142869a3e539SJoel Becker 	if (free_space < 0)
142969a3e539SJoel Becker 		return -EIO;
143069a3e539SJoel Becker 	if (free_space < needed_space)
143169a3e539SJoel Becker 		return -ENOSPC;
143269a3e539SJoel Becker 
143369a3e539SJoel Becker 	return 0;
143469a3e539SJoel Becker }
143569a3e539SJoel Becker 
ocfs2_xa_journal_access(handle_t * handle,struct ocfs2_xa_loc * loc,int type)1436cf2bc809SJoel Becker static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1437cf2bc809SJoel Becker 				   int type)
1438cf2bc809SJoel Becker {
1439cf2bc809SJoel Becker 	return loc->xl_ops->xlo_journal_access(handle, loc, type);
1440cf2bc809SJoel Becker }
1441cf2bc809SJoel Becker 
ocfs2_xa_journal_dirty(handle_t * handle,struct ocfs2_xa_loc * loc)1442cf2bc809SJoel Becker static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1443cf2bc809SJoel Becker {
1444cf2bc809SJoel Becker 	loc->xl_ops->xlo_journal_dirty(handle, loc);
1445cf2bc809SJoel Becker }
1446cf2bc809SJoel Becker 
144769a3e539SJoel Becker /* Give a pointer into the storage for the given offset */
ocfs2_xa_offset_pointer(struct ocfs2_xa_loc * loc,int offset)144869a3e539SJoel Becker static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
144969a3e539SJoel Becker {
145069a3e539SJoel Becker 	BUG_ON(offset >= loc->xl_size);
145169a3e539SJoel Becker 	return loc->xl_ops->xlo_offset_pointer(loc, offset);
145269a3e539SJoel Becker }
145369a3e539SJoel Becker 
1454cf1d6c76STiger Yang /*
145511179f2cSJoel Becker  * Wipe the name+value pair and allow the storage to reclaim it.  This
145611179f2cSJoel Becker  * must be followed by either removal of the entry or a call to
145711179f2cSJoel Becker  * ocfs2_xa_add_namevalue().
145811179f2cSJoel Becker  */
ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc * loc)145911179f2cSJoel Becker static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
146011179f2cSJoel Becker {
146111179f2cSJoel Becker 	loc->xl_ops->xlo_wipe_namevalue(loc);
146211179f2cSJoel Becker }
146311179f2cSJoel Becker 
146469a3e539SJoel Becker /*
146569a3e539SJoel Becker  * Find lowest offset to a name+value pair.  This is the start of our
146669a3e539SJoel Becker  * downward-growing free space.
146769a3e539SJoel Becker  */
ocfs2_xa_get_free_start(struct ocfs2_xa_loc * loc)146869a3e539SJoel Becker static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
146969a3e539SJoel Becker {
147069a3e539SJoel Becker 	return loc->xl_ops->xlo_get_free_start(loc);
147169a3e539SJoel Becker }
147269a3e539SJoel Becker 
147369a3e539SJoel Becker /* Can we reuse loc->xl_entry for xi? */
ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)147469a3e539SJoel Becker static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
147569a3e539SJoel Becker 				    struct ocfs2_xattr_info *xi)
147669a3e539SJoel Becker {
147769a3e539SJoel Becker 	return loc->xl_ops->xlo_can_reuse(loc, xi);
147869a3e539SJoel Becker }
147969a3e539SJoel Becker 
148069a3e539SJoel Becker /* How much free space is needed to set the new value */
ocfs2_xa_check_space(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)148169a3e539SJoel Becker static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
148269a3e539SJoel Becker 				struct ocfs2_xattr_info *xi)
148369a3e539SJoel Becker {
148469a3e539SJoel Becker 	return loc->xl_ops->xlo_check_space(loc, xi);
148569a3e539SJoel Becker }
148669a3e539SJoel Becker 
ocfs2_xa_add_entry(struct ocfs2_xa_loc * loc,u32 name_hash)148794b07b6fSJoseph Qi static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
148894b07b6fSJoseph Qi {
148994b07b6fSJoseph Qi 	loc->xl_ops->xlo_add_entry(loc, name_hash);
149094b07b6fSJoseph Qi 	loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
149194b07b6fSJoseph Qi 	/*
149294b07b6fSJoseph Qi 	 * We can't leave the new entry's xe_name_offset at zero or
149394b07b6fSJoseph Qi 	 * add_namevalue() will go nuts.  We set it to the size of our
149494b07b6fSJoseph Qi 	 * storage so that it can never be less than any other entry.
149594b07b6fSJoseph Qi 	 */
149694b07b6fSJoseph Qi 	loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
149794b07b6fSJoseph Qi }
149894b07b6fSJoseph Qi 
ocfs2_xa_add_namevalue(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)149969a3e539SJoel Becker static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
150069a3e539SJoel Becker 				   struct ocfs2_xattr_info *xi)
150169a3e539SJoel Becker {
150269a3e539SJoel Becker 	int size = namevalue_size_xi(xi);
150369a3e539SJoel Becker 	int nameval_offset;
150469a3e539SJoel Becker 	char *nameval_buf;
150569a3e539SJoel Becker 
150669a3e539SJoel Becker 	loc->xl_ops->xlo_add_namevalue(loc, size);
150769a3e539SJoel Becker 	loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
150869a3e539SJoel Becker 	loc->xl_entry->xe_name_len = xi->xi_name_len;
150969a3e539SJoel Becker 	ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
151069a3e539SJoel Becker 	ocfs2_xattr_set_local(loc->xl_entry,
151169a3e539SJoel Becker 			      xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
151269a3e539SJoel Becker 
151369a3e539SJoel Becker 	nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
151469a3e539SJoel Becker 	nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
151569a3e539SJoel Becker 	memset(nameval_buf, 0, size);
151669a3e539SJoel Becker 	memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
151769a3e539SJoel Becker }
151869a3e539SJoel Becker 
ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_value_buf * vb)15193fc12afaSJoel Becker static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
15203fc12afaSJoel Becker 				    struct ocfs2_xattr_value_buf *vb)
15213fc12afaSJoel Becker {
15223fc12afaSJoel Becker 	int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
15233fc12afaSJoel Becker 	int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
15243fc12afaSJoel Becker 
15253fc12afaSJoel Becker 	/* Value bufs are for value trees */
152673857ee0SJoel Becker 	BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
15273fc12afaSJoel Becker 	BUG_ON(namevalue_size_xe(loc->xl_entry) !=
15283fc12afaSJoel Becker 	       (name_size + OCFS2_XATTR_ROOT_SIZE));
15293fc12afaSJoel Becker 
15303fc12afaSJoel Becker 	loc->xl_ops->xlo_fill_value_buf(loc, vb);
15313fc12afaSJoel Becker 	vb->vb_xv =
15323fc12afaSJoel Becker 		(struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
15333fc12afaSJoel Becker 							nameval_offset +
15343fc12afaSJoel Becker 							name_size);
15353fc12afaSJoel Becker }
15363fc12afaSJoel Becker 
ocfs2_xa_block_journal_access(handle_t * handle,struct ocfs2_xa_loc * loc,int type)1537cf2bc809SJoel Becker static int ocfs2_xa_block_journal_access(handle_t *handle,
1538cf2bc809SJoel Becker 					 struct ocfs2_xa_loc *loc, int type)
1539cf2bc809SJoel Becker {
1540cf2bc809SJoel Becker 	struct buffer_head *bh = loc->xl_storage;
1541cf2bc809SJoel Becker 	ocfs2_journal_access_func access;
1542cf2bc809SJoel Becker 
1543cf2bc809SJoel Becker 	if (loc->xl_size == (bh->b_size -
1544cf2bc809SJoel Becker 			     offsetof(struct ocfs2_xattr_block,
1545cf2bc809SJoel Becker 				      xb_attrs.xb_header)))
1546cf2bc809SJoel Becker 		access = ocfs2_journal_access_xb;
1547cf2bc809SJoel Becker 	else
1548cf2bc809SJoel Becker 		access = ocfs2_journal_access_di;
1549cf2bc809SJoel Becker 	return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1550cf2bc809SJoel Becker }
1551cf2bc809SJoel Becker 
ocfs2_xa_block_journal_dirty(handle_t * handle,struct ocfs2_xa_loc * loc)1552cf2bc809SJoel Becker static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1553cf2bc809SJoel Becker 					 struct ocfs2_xa_loc *loc)
1554cf2bc809SJoel Becker {
1555cf2bc809SJoel Becker 	struct buffer_head *bh = loc->xl_storage;
1556cf2bc809SJoel Becker 
1557cf2bc809SJoel Becker 	ocfs2_journal_dirty(handle, bh);
1558cf2bc809SJoel Becker }
1559cf2bc809SJoel Becker 
ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc * loc,int offset)156011179f2cSJoel Becker static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
156111179f2cSJoel Becker 					   int offset)
156211179f2cSJoel Becker {
156311179f2cSJoel Becker 	return (char *)loc->xl_header + offset;
156411179f2cSJoel Becker }
156511179f2cSJoel Becker 
ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)156669a3e539SJoel Becker static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
156769a3e539SJoel Becker 				    struct ocfs2_xattr_info *xi)
156869a3e539SJoel Becker {
156969a3e539SJoel Becker 	/*
157069a3e539SJoel Becker 	 * Block storage is strict.  If the sizes aren't exact, we will
157169a3e539SJoel Becker 	 * remove the old one and reinsert the new.
157269a3e539SJoel Becker 	 */
157369a3e539SJoel Becker 	return namevalue_size_xe(loc->xl_entry) ==
157469a3e539SJoel Becker 		namevalue_size_xi(xi);
157569a3e539SJoel Becker }
157669a3e539SJoel Becker 
ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc * loc)157769a3e539SJoel Becker static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
157869a3e539SJoel Becker {
157969a3e539SJoel Becker 	struct ocfs2_xattr_header *xh = loc->xl_header;
158069a3e539SJoel Becker 	int i, count = le16_to_cpu(xh->xh_count);
158169a3e539SJoel Becker 	int offset, free_start = loc->xl_size;
158269a3e539SJoel Becker 
158369a3e539SJoel Becker 	for (i = 0; i < count; i++) {
158469a3e539SJoel Becker 		offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
158569a3e539SJoel Becker 		if (offset < free_start)
158669a3e539SJoel Becker 			free_start = offset;
158769a3e539SJoel Becker 	}
158869a3e539SJoel Becker 
158969a3e539SJoel Becker 	return free_start;
159069a3e539SJoel Becker }
159169a3e539SJoel Becker 
ocfs2_xa_block_check_space(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)159269a3e539SJoel Becker static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
159369a3e539SJoel Becker 				      struct ocfs2_xattr_info *xi)
159469a3e539SJoel Becker {
159569a3e539SJoel Becker 	int count = le16_to_cpu(loc->xl_header->xh_count);
159669a3e539SJoel Becker 	int free_start = ocfs2_xa_get_free_start(loc);
159769a3e539SJoel Becker 	int needed_space = ocfs2_xi_entry_usage(xi);
159869a3e539SJoel Becker 
159969a3e539SJoel Becker 	/*
160069a3e539SJoel Becker 	 * Block storage will reclaim the original entry before inserting
160169a3e539SJoel Becker 	 * the new value, so we only need the difference.  If the new
160269a3e539SJoel Becker 	 * entry is smaller than the old one, we don't need anything.
160369a3e539SJoel Becker 	 */
160469a3e539SJoel Becker 	if (loc->xl_entry) {
160569a3e539SJoel Becker 		/* Don't need space if we're reusing! */
160669a3e539SJoel Becker 		if (ocfs2_xa_can_reuse_entry(loc, xi))
160769a3e539SJoel Becker 			needed_space = 0;
160869a3e539SJoel Becker 		else
160969a3e539SJoel Becker 			needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
161069a3e539SJoel Becker 	}
161169a3e539SJoel Becker 	if (needed_space < 0)
161269a3e539SJoel Becker 		needed_space = 0;
161369a3e539SJoel Becker 	return ocfs2_xa_check_space_helper(needed_space, free_start, count);
161469a3e539SJoel Becker }
161569a3e539SJoel Becker 
161611179f2cSJoel Becker /*
161711179f2cSJoel Becker  * Block storage for xattrs keeps the name+value pairs compacted.  When
161811179f2cSJoel Becker  * we remove one, we have to shift any that preceded it towards the end.
161911179f2cSJoel Becker  */
ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc * loc)162011179f2cSJoel Becker static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
162111179f2cSJoel Becker {
162211179f2cSJoel Becker 	int i, offset;
162311179f2cSJoel Becker 	int namevalue_offset, first_namevalue_offset, namevalue_size;
162411179f2cSJoel Becker 	struct ocfs2_xattr_entry *entry = loc->xl_entry;
162511179f2cSJoel Becker 	struct ocfs2_xattr_header *xh = loc->xl_header;
162611179f2cSJoel Becker 	int count = le16_to_cpu(xh->xh_count);
162711179f2cSJoel Becker 
162811179f2cSJoel Becker 	namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1629199799a3SJoel Becker 	namevalue_size = namevalue_size_xe(entry);
163069a3e539SJoel Becker 	first_namevalue_offset = ocfs2_xa_get_free_start(loc);
163111179f2cSJoel Becker 
163211179f2cSJoel Becker 	/* Shift the name+value pairs */
163311179f2cSJoel Becker 	memmove((char *)xh + first_namevalue_offset + namevalue_size,
163411179f2cSJoel Becker 		(char *)xh + first_namevalue_offset,
163511179f2cSJoel Becker 		namevalue_offset - first_namevalue_offset);
163611179f2cSJoel Becker 	memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
163711179f2cSJoel Becker 
163811179f2cSJoel Becker 	/* Now tell xh->xh_entries about it */
163911179f2cSJoel Becker 	for (i = 0; i < count; i++) {
164011179f2cSJoel Becker 		offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1641dfe4d3d6STao Ma 		if (offset <= namevalue_offset)
164211179f2cSJoel Becker 			le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
164311179f2cSJoel Becker 				     namevalue_size);
164411179f2cSJoel Becker 	}
164511179f2cSJoel Becker 
164611179f2cSJoel Becker 	/*
164711179f2cSJoel Becker 	 * Note that we don't update xh_free_start or xh_name_value_len
164811179f2cSJoel Becker 	 * because they're not used in block-stored xattrs.
164911179f2cSJoel Becker 	 */
165011179f2cSJoel Becker }
165111179f2cSJoel Becker 
ocfs2_xa_block_add_entry(struct ocfs2_xa_loc * loc,u32 name_hash)165269a3e539SJoel Becker static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
165369a3e539SJoel Becker {
165469a3e539SJoel Becker 	int count = le16_to_cpu(loc->xl_header->xh_count);
165569a3e539SJoel Becker 	loc->xl_entry = &(loc->xl_header->xh_entries[count]);
165669a3e539SJoel Becker 	le16_add_cpu(&loc->xl_header->xh_count, 1);
165769a3e539SJoel Becker 	memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
165869a3e539SJoel Becker }
165969a3e539SJoel Becker 
ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc * loc,int size)166069a3e539SJoel Becker static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
166169a3e539SJoel Becker {
166269a3e539SJoel Becker 	int free_start = ocfs2_xa_get_free_start(loc);
166369a3e539SJoel Becker 
166469a3e539SJoel Becker 	loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
166569a3e539SJoel Becker }
166669a3e539SJoel Becker 
ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_value_buf * vb)16673fc12afaSJoel Becker static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
16683fc12afaSJoel Becker 					  struct ocfs2_xattr_value_buf *vb)
16693fc12afaSJoel Becker {
16703fc12afaSJoel Becker 	struct buffer_head *bh = loc->xl_storage;
16713fc12afaSJoel Becker 
16723fc12afaSJoel Becker 	if (loc->xl_size == (bh->b_size -
16733fc12afaSJoel Becker 			     offsetof(struct ocfs2_xattr_block,
16743fc12afaSJoel Becker 				      xb_attrs.xb_header)))
16753fc12afaSJoel Becker 		vb->vb_access = ocfs2_journal_access_xb;
16763fc12afaSJoel Becker 	else
16773fc12afaSJoel Becker 		vb->vb_access = ocfs2_journal_access_di;
16783fc12afaSJoel Becker 	vb->vb_bh = bh;
16793fc12afaSJoel Becker }
16803fc12afaSJoel Becker 
168111179f2cSJoel Becker /*
168211179f2cSJoel Becker  * Operations for xattrs stored in blocks.  This includes inline inode
168311179f2cSJoel Becker  * storage and unindexed ocfs2_xattr_blocks.
168411179f2cSJoel Becker  */
168511179f2cSJoel Becker static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1686cf2bc809SJoel Becker 	.xlo_journal_access	= ocfs2_xa_block_journal_access,
1687cf2bc809SJoel Becker 	.xlo_journal_dirty	= ocfs2_xa_block_journal_dirty,
168811179f2cSJoel Becker 	.xlo_offset_pointer	= ocfs2_xa_block_offset_pointer,
168969a3e539SJoel Becker 	.xlo_check_space	= ocfs2_xa_block_check_space,
169069a3e539SJoel Becker 	.xlo_can_reuse		= ocfs2_xa_block_can_reuse,
169169a3e539SJoel Becker 	.xlo_get_free_start	= ocfs2_xa_block_get_free_start,
169211179f2cSJoel Becker 	.xlo_wipe_namevalue	= ocfs2_xa_block_wipe_namevalue,
169369a3e539SJoel Becker 	.xlo_add_entry		= ocfs2_xa_block_add_entry,
169469a3e539SJoel Becker 	.xlo_add_namevalue	= ocfs2_xa_block_add_namevalue,
16953fc12afaSJoel Becker 	.xlo_fill_value_buf	= ocfs2_xa_block_fill_value_buf,
169611179f2cSJoel Becker };
169711179f2cSJoel Becker 
ocfs2_xa_bucket_journal_access(handle_t * handle,struct ocfs2_xa_loc * loc,int type)1698cf2bc809SJoel Becker static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1699cf2bc809SJoel Becker 					  struct ocfs2_xa_loc *loc, int type)
1700cf2bc809SJoel Becker {
1701cf2bc809SJoel Becker 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1702cf2bc809SJoel Becker 
1703cf2bc809SJoel Becker 	return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1704cf2bc809SJoel Becker }
1705cf2bc809SJoel Becker 
ocfs2_xa_bucket_journal_dirty(handle_t * handle,struct ocfs2_xa_loc * loc)1706cf2bc809SJoel Becker static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1707cf2bc809SJoel Becker 					  struct ocfs2_xa_loc *loc)
1708cf2bc809SJoel Becker {
1709cf2bc809SJoel Becker 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1710cf2bc809SJoel Becker 
1711cf2bc809SJoel Becker 	ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1712cf2bc809SJoel Becker }
1713cf2bc809SJoel Becker 
ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc * loc,int offset)171411179f2cSJoel Becker static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
171511179f2cSJoel Becker 					    int offset)
171611179f2cSJoel Becker {
171711179f2cSJoel Becker 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
171811179f2cSJoel Becker 	int block, block_offset;
171911179f2cSJoel Becker 
172011179f2cSJoel Becker 	/* The header is at the front of the bucket */
1721cf2bc809SJoel Becker 	block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1722cf2bc809SJoel Becker 	block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
172311179f2cSJoel Becker 
172411179f2cSJoel Becker 	return bucket_block(bucket, block) + block_offset;
172511179f2cSJoel Becker }
172611179f2cSJoel Becker 
ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)172769a3e539SJoel Becker static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
172869a3e539SJoel Becker 				     struct ocfs2_xattr_info *xi)
172969a3e539SJoel Becker {
173069a3e539SJoel Becker 	return namevalue_size_xe(loc->xl_entry) >=
173169a3e539SJoel Becker 		namevalue_size_xi(xi);
173269a3e539SJoel Becker }
173369a3e539SJoel Becker 
ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc * loc)173469a3e539SJoel Becker static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
173569a3e539SJoel Becker {
173669a3e539SJoel Becker 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
173769a3e539SJoel Becker 	return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
173869a3e539SJoel Becker }
173969a3e539SJoel Becker 
ocfs2_bucket_align_free_start(struct super_block * sb,int free_start,int size)174069a3e539SJoel Becker static int ocfs2_bucket_align_free_start(struct super_block *sb,
174169a3e539SJoel Becker 					 int free_start, int size)
174269a3e539SJoel Becker {
174369a3e539SJoel Becker 	/*
174469a3e539SJoel Becker 	 * We need to make sure that the name+value pair fits within
174569a3e539SJoel Becker 	 * one block.
174669a3e539SJoel Becker 	 */
174769a3e539SJoel Becker 	if (((free_start - size) >> sb->s_blocksize_bits) !=
174869a3e539SJoel Becker 	    ((free_start - 1) >> sb->s_blocksize_bits))
174969a3e539SJoel Becker 		free_start -= free_start % sb->s_blocksize;
175069a3e539SJoel Becker 
175169a3e539SJoel Becker 	return free_start;
175269a3e539SJoel Becker }
175369a3e539SJoel Becker 
ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)175469a3e539SJoel Becker static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
175569a3e539SJoel Becker 				       struct ocfs2_xattr_info *xi)
175669a3e539SJoel Becker {
175769a3e539SJoel Becker 	int rc;
175869a3e539SJoel Becker 	int count = le16_to_cpu(loc->xl_header->xh_count);
175969a3e539SJoel Becker 	int free_start = ocfs2_xa_get_free_start(loc);
176069a3e539SJoel Becker 	int needed_space = ocfs2_xi_entry_usage(xi);
176169a3e539SJoel Becker 	int size = namevalue_size_xi(xi);
1762cf2bc809SJoel Becker 	struct super_block *sb = loc->xl_inode->i_sb;
176369a3e539SJoel Becker 
176469a3e539SJoel Becker 	/*
176569a3e539SJoel Becker 	 * Bucket storage does not reclaim name+value pairs it cannot
176669a3e539SJoel Becker 	 * reuse.  They live as holes until the bucket fills, and then
176769a3e539SJoel Becker 	 * the bucket is defragmented.  However, the bucket can reclaim
176869a3e539SJoel Becker 	 * the ocfs2_xattr_entry.
176969a3e539SJoel Becker 	 */
177069a3e539SJoel Becker 	if (loc->xl_entry) {
177169a3e539SJoel Becker 		/* Don't need space if we're reusing! */
177269a3e539SJoel Becker 		if (ocfs2_xa_can_reuse_entry(loc, xi))
177369a3e539SJoel Becker 			needed_space = 0;
177469a3e539SJoel Becker 		else
177569a3e539SJoel Becker 			needed_space -= sizeof(struct ocfs2_xattr_entry);
177669a3e539SJoel Becker 	}
177769a3e539SJoel Becker 	BUG_ON(needed_space < 0);
177869a3e539SJoel Becker 
177969a3e539SJoel Becker 	if (free_start < size) {
178069a3e539SJoel Becker 		if (needed_space)
178169a3e539SJoel Becker 			return -ENOSPC;
178269a3e539SJoel Becker 	} else {
178369a3e539SJoel Becker 		/*
178469a3e539SJoel Becker 		 * First we check if it would fit in the first place.
178569a3e539SJoel Becker 		 * Below, we align the free start to a block.  This may
178669a3e539SJoel Becker 		 * slide us below the minimum gap.  By checking unaligned
178769a3e539SJoel Becker 		 * first, we avoid that error.
178869a3e539SJoel Becker 		 */
178969a3e539SJoel Becker 		rc = ocfs2_xa_check_space_helper(needed_space, free_start,
179069a3e539SJoel Becker 						 count);
179169a3e539SJoel Becker 		if (rc)
179269a3e539SJoel Becker 			return rc;
179369a3e539SJoel Becker 		free_start = ocfs2_bucket_align_free_start(sb, free_start,
179469a3e539SJoel Becker 							   size);
179569a3e539SJoel Becker 	}
179669a3e539SJoel Becker 	return ocfs2_xa_check_space_helper(needed_space, free_start, count);
179769a3e539SJoel Becker }
179869a3e539SJoel Becker 
ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc * loc)179911179f2cSJoel Becker static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
180011179f2cSJoel Becker {
1801199799a3SJoel Becker 	le16_add_cpu(&loc->xl_header->xh_name_value_len,
1802199799a3SJoel Becker 		     -namevalue_size_xe(loc->xl_entry));
180311179f2cSJoel Becker }
180411179f2cSJoel Becker 
ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc * loc,u32 name_hash)180569a3e539SJoel Becker static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
180669a3e539SJoel Becker {
180769a3e539SJoel Becker 	struct ocfs2_xattr_header *xh = loc->xl_header;
180869a3e539SJoel Becker 	int count = le16_to_cpu(xh->xh_count);
180969a3e539SJoel Becker 	int low = 0, high = count - 1, tmp;
181069a3e539SJoel Becker 	struct ocfs2_xattr_entry *tmp_xe;
181169a3e539SJoel Becker 
181269a3e539SJoel Becker 	/*
181369a3e539SJoel Becker 	 * We keep buckets sorted by name_hash, so we need to find
181469a3e539SJoel Becker 	 * our insert place.
181569a3e539SJoel Becker 	 */
181669a3e539SJoel Becker 	while (low <= high && count) {
181769a3e539SJoel Becker 		tmp = (low + high) / 2;
181869a3e539SJoel Becker 		tmp_xe = &xh->xh_entries[tmp];
181969a3e539SJoel Becker 
182069a3e539SJoel Becker 		if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
182169a3e539SJoel Becker 			low = tmp + 1;
182269a3e539SJoel Becker 		else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
182369a3e539SJoel Becker 			high = tmp - 1;
182469a3e539SJoel Becker 		else {
182569a3e539SJoel Becker 			low = tmp;
182669a3e539SJoel Becker 			break;
182769a3e539SJoel Becker 		}
182869a3e539SJoel Becker 	}
182969a3e539SJoel Becker 
183069a3e539SJoel Becker 	if (low != count)
183169a3e539SJoel Becker 		memmove(&xh->xh_entries[low + 1],
183269a3e539SJoel Becker 			&xh->xh_entries[low],
183369a3e539SJoel Becker 			((count - low) * sizeof(struct ocfs2_xattr_entry)));
183469a3e539SJoel Becker 
183569a3e539SJoel Becker 	le16_add_cpu(&xh->xh_count, 1);
183669a3e539SJoel Becker 	loc->xl_entry = &xh->xh_entries[low];
183769a3e539SJoel Becker 	memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
183869a3e539SJoel Becker }
183969a3e539SJoel Becker 
ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc * loc,int size)184069a3e539SJoel Becker static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
184169a3e539SJoel Becker {
184269a3e539SJoel Becker 	int free_start = ocfs2_xa_get_free_start(loc);
184369a3e539SJoel Becker 	struct ocfs2_xattr_header *xh = loc->xl_header;
1844cf2bc809SJoel Becker 	struct super_block *sb = loc->xl_inode->i_sb;
184569a3e539SJoel Becker 	int nameval_offset;
184669a3e539SJoel Becker 
184769a3e539SJoel Becker 	free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
184869a3e539SJoel Becker 	nameval_offset = free_start - size;
184969a3e539SJoel Becker 	loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
185069a3e539SJoel Becker 	xh->xh_free_start = cpu_to_le16(nameval_offset);
185169a3e539SJoel Becker 	le16_add_cpu(&xh->xh_name_value_len, size);
185269a3e539SJoel Becker 
185369a3e539SJoel Becker }
185469a3e539SJoel Becker 
ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_value_buf * vb)18553fc12afaSJoel Becker static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
18563fc12afaSJoel Becker 					   struct ocfs2_xattr_value_buf *vb)
18573fc12afaSJoel Becker {
18583fc12afaSJoel Becker 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1859cf2bc809SJoel Becker 	struct super_block *sb = loc->xl_inode->i_sb;
18603fc12afaSJoel Becker 	int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
18613fc12afaSJoel Becker 	int size = namevalue_size_xe(loc->xl_entry);
18623fc12afaSJoel Becker 	int block_offset = nameval_offset >> sb->s_blocksize_bits;
18633fc12afaSJoel Becker 
18643fc12afaSJoel Becker 	/* Values are not allowed to straddle block boundaries */
18653fc12afaSJoel Becker 	BUG_ON(block_offset !=
18663fc12afaSJoel Becker 	       ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
18673fc12afaSJoel Becker 	/* We expect the bucket to be filled in */
18683fc12afaSJoel Becker 	BUG_ON(!bucket->bu_bhs[block_offset]);
18693fc12afaSJoel Becker 
18703fc12afaSJoel Becker 	vb->vb_access = ocfs2_journal_access;
18713fc12afaSJoel Becker 	vb->vb_bh = bucket->bu_bhs[block_offset];
18723fc12afaSJoel Becker }
18733fc12afaSJoel Becker 
187411179f2cSJoel Becker /* Operations for xattrs stored in buckets. */
187511179f2cSJoel Becker static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1876cf2bc809SJoel Becker 	.xlo_journal_access	= ocfs2_xa_bucket_journal_access,
1877cf2bc809SJoel Becker 	.xlo_journal_dirty	= ocfs2_xa_bucket_journal_dirty,
187811179f2cSJoel Becker 	.xlo_offset_pointer	= ocfs2_xa_bucket_offset_pointer,
187969a3e539SJoel Becker 	.xlo_check_space	= ocfs2_xa_bucket_check_space,
188069a3e539SJoel Becker 	.xlo_can_reuse		= ocfs2_xa_bucket_can_reuse,
188169a3e539SJoel Becker 	.xlo_get_free_start	= ocfs2_xa_bucket_get_free_start,
188211179f2cSJoel Becker 	.xlo_wipe_namevalue	= ocfs2_xa_bucket_wipe_namevalue,
188369a3e539SJoel Becker 	.xlo_add_entry		= ocfs2_xa_bucket_add_entry,
188469a3e539SJoel Becker 	.xlo_add_namevalue	= ocfs2_xa_bucket_add_namevalue,
18853fc12afaSJoel Becker 	.xlo_fill_value_buf	= ocfs2_xa_bucket_fill_value_buf,
188611179f2cSJoel Becker };
188711179f2cSJoel Becker 
ocfs2_xa_value_clusters(struct ocfs2_xa_loc * loc)1888399ff3a7SJoel Becker static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1889399ff3a7SJoel Becker {
1890399ff3a7SJoel Becker 	struct ocfs2_xattr_value_buf vb;
1891399ff3a7SJoel Becker 
1892399ff3a7SJoel Becker 	if (ocfs2_xattr_is_local(loc->xl_entry))
1893399ff3a7SJoel Becker 		return 0;
1894399ff3a7SJoel Becker 
1895399ff3a7SJoel Becker 	ocfs2_xa_fill_value_buf(loc, &vb);
1896399ff3a7SJoel Becker 	return le32_to_cpu(vb.vb_xv->xr_clusters);
1897399ff3a7SJoel Becker }
1898399ff3a7SJoel Becker 
ocfs2_xa_value_truncate(struct ocfs2_xa_loc * loc,u64 bytes,struct ocfs2_xattr_set_ctxt * ctxt)189973857ee0SJoel Becker static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
190073857ee0SJoel Becker 				   struct ocfs2_xattr_set_ctxt *ctxt)
190173857ee0SJoel Becker {
190273857ee0SJoel Becker 	int trunc_rc, access_rc;
190373857ee0SJoel Becker 	struct ocfs2_xattr_value_buf vb;
190473857ee0SJoel Becker 
190573857ee0SJoel Becker 	ocfs2_xa_fill_value_buf(loc, &vb);
190673857ee0SJoel Becker 	trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
190773857ee0SJoel Becker 					      ctxt);
190873857ee0SJoel Becker 
190973857ee0SJoel Becker 	/*
191073857ee0SJoel Becker 	 * The caller of ocfs2_xa_value_truncate() has already called
191173857ee0SJoel Becker 	 * ocfs2_xa_journal_access on the loc.  However, The truncate code
191273857ee0SJoel Becker 	 * calls ocfs2_extend_trans().  This may commit the previous
191373857ee0SJoel Becker 	 * transaction and open a new one.  If this is a bucket, truncate
191473857ee0SJoel Becker 	 * could leave only vb->vb_bh set up for journaling.  Meanwhile,
191573857ee0SJoel Becker 	 * the caller is expecting to dirty the entire bucket.  So we must
191673857ee0SJoel Becker 	 * reset the journal work.  We do this even if truncate has failed,
191773857ee0SJoel Becker 	 * as it could have failed after committing the extend.
191873857ee0SJoel Becker 	 */
191973857ee0SJoel Becker 	access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
192073857ee0SJoel Becker 					    OCFS2_JOURNAL_ACCESS_WRITE);
192173857ee0SJoel Becker 
192273857ee0SJoel Becker 	/* Errors in truncate take precedence */
192373857ee0SJoel Becker 	return trunc_rc ? trunc_rc : access_rc;
192473857ee0SJoel Becker }
192573857ee0SJoel Becker 
ocfs2_xa_remove_entry(struct ocfs2_xa_loc * loc)192611179f2cSJoel Becker static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
192711179f2cSJoel Becker {
1928bde1e540SJoel Becker 	int index, count;
1929bde1e540SJoel Becker 	struct ocfs2_xattr_header *xh = loc->xl_header;
1930bde1e540SJoel Becker 	struct ocfs2_xattr_entry *entry = loc->xl_entry;
1931bde1e540SJoel Becker 
193211179f2cSJoel Becker 	ocfs2_xa_wipe_namevalue(loc);
1933bde1e540SJoel Becker 	loc->xl_entry = NULL;
1934bde1e540SJoel Becker 
1935bde1e540SJoel Becker 	le16_add_cpu(&xh->xh_count, -1);
1936bde1e540SJoel Becker 	count = le16_to_cpu(xh->xh_count);
1937bde1e540SJoel Becker 
1938bde1e540SJoel Becker 	/*
1939bde1e540SJoel Becker 	 * Only zero out the entry if there are more remaining.  This is
1940bde1e540SJoel Becker 	 * important for an empty bucket, as it keeps track of the
1941bde1e540SJoel Becker 	 * bucket's hash value.  It doesn't hurt empty block storage.
1942bde1e540SJoel Becker 	 */
1943bde1e540SJoel Becker 	if (count) {
1944bde1e540SJoel Becker 		index = ((char *)entry - (char *)&xh->xh_entries) /
1945bde1e540SJoel Becker 			sizeof(struct ocfs2_xattr_entry);
1946bde1e540SJoel Becker 		memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1947bde1e540SJoel Becker 			(count - index) * sizeof(struct ocfs2_xattr_entry));
1948bde1e540SJoel Becker 		memset(&xh->xh_entries[count], 0,
1949bde1e540SJoel Becker 		       sizeof(struct ocfs2_xattr_entry));
1950bde1e540SJoel Becker 	}
195111179f2cSJoel Becker }
195211179f2cSJoel Becker 
1953399ff3a7SJoel Becker /*
1954399ff3a7SJoel Becker  * If we have a problem adjusting the size of an external value during
1955399ff3a7SJoel Becker  * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1956399ff3a7SJoel Becker  * in an intermediate state.  For example, the value may be partially
1957399ff3a7SJoel Becker  * truncated.
1958399ff3a7SJoel Becker  *
1959399ff3a7SJoel Becker  * If the value tree hasn't changed, the extend/truncate went nowhere.
1960399ff3a7SJoel Becker  * We have nothing to do.  The caller can treat it as a straight error.
1961399ff3a7SJoel Becker  *
1962399ff3a7SJoel Becker  * If the value tree got partially truncated, we now have a corrupted
1963399ff3a7SJoel Becker  * extended attribute.  We're going to wipe its entry and leak the
1964399ff3a7SJoel Becker  * clusters.  Better to leak some storage than leave a corrupt entry.
1965399ff3a7SJoel Becker  *
1966399ff3a7SJoel Becker  * If the value tree grew, it obviously didn't grow enough for the
1967399ff3a7SJoel Becker  * new entry.  We're not going to try and reclaim those clusters either.
1968399ff3a7SJoel Becker  * If there was already an external value there (orig_clusters != 0),
1969399ff3a7SJoel Becker  * the new clusters are attached safely and we can just leave the old
1970399ff3a7SJoel Becker  * value in place.  If there was no external value there, we remove
1971399ff3a7SJoel Becker  * the entry.
1972399ff3a7SJoel Becker  *
1973399ff3a7SJoel Becker  * This way, the xattr block we store in the journal will be consistent.
1974399ff3a7SJoel Becker  * If the size change broke because of the journal, no changes will hit
1975399ff3a7SJoel Becker  * disk anyway.
1976399ff3a7SJoel Becker  */
ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc * loc,const char * what,unsigned int orig_clusters)1977399ff3a7SJoel Becker static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1978399ff3a7SJoel Becker 					    const char *what,
1979399ff3a7SJoel Becker 					    unsigned int orig_clusters)
1980399ff3a7SJoel Becker {
1981399ff3a7SJoel Becker 	unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1982399ff3a7SJoel Becker 	char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1983399ff3a7SJoel Becker 				le16_to_cpu(loc->xl_entry->xe_name_offset));
1984399ff3a7SJoel Becker 
1985399ff3a7SJoel Becker 	if (new_clusters < orig_clusters) {
1986399ff3a7SJoel Becker 		mlog(ML_ERROR,
1987399ff3a7SJoel Becker 		     "Partial truncate while %s xattr %.*s.  Leaking "
1988399ff3a7SJoel Becker 		     "%u clusters and removing the entry\n",
1989399ff3a7SJoel Becker 		     what, loc->xl_entry->xe_name_len, nameval_buf,
1990399ff3a7SJoel Becker 		     orig_clusters - new_clusters);
1991399ff3a7SJoel Becker 		ocfs2_xa_remove_entry(loc);
1992399ff3a7SJoel Becker 	} else if (!orig_clusters) {
1993399ff3a7SJoel Becker 		mlog(ML_ERROR,
1994399ff3a7SJoel Becker 		     "Unable to allocate an external value for xattr "
1995399ff3a7SJoel Becker 		     "%.*s safely.  Leaking %u clusters and removing the "
1996399ff3a7SJoel Becker 		     "entry\n",
1997399ff3a7SJoel Becker 		     loc->xl_entry->xe_name_len, nameval_buf,
1998399ff3a7SJoel Becker 		     new_clusters - orig_clusters);
1999399ff3a7SJoel Becker 		ocfs2_xa_remove_entry(loc);
2000399ff3a7SJoel Becker 	} else if (new_clusters > orig_clusters)
2001399ff3a7SJoel Becker 		mlog(ML_ERROR,
2002399ff3a7SJoel Becker 		     "Unable to grow xattr %.*s safely.  %u new clusters "
2003399ff3a7SJoel Becker 		     "have been added, but the value will not be "
2004399ff3a7SJoel Becker 		     "modified\n",
2005399ff3a7SJoel Becker 		     loc->xl_entry->xe_name_len, nameval_buf,
2006399ff3a7SJoel Becker 		     new_clusters - orig_clusters);
2007399ff3a7SJoel Becker }
2008399ff3a7SJoel Becker 
ocfs2_xa_remove(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_set_ctxt * ctxt)200973857ee0SJoel Becker static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
201073857ee0SJoel Becker 			   struct ocfs2_xattr_set_ctxt *ctxt)
201173857ee0SJoel Becker {
201273857ee0SJoel Becker 	int rc = 0;
2013399ff3a7SJoel Becker 	unsigned int orig_clusters;
201473857ee0SJoel Becker 
201573857ee0SJoel Becker 	if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2016399ff3a7SJoel Becker 		orig_clusters = ocfs2_xa_value_clusters(loc);
201773857ee0SJoel Becker 		rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
201873857ee0SJoel Becker 		if (rc) {
201973857ee0SJoel Becker 			mlog_errno(rc);
2020399ff3a7SJoel Becker 			/*
2021399ff3a7SJoel Becker 			 * Since this is remove, we can return 0 if
2022399ff3a7SJoel Becker 			 * ocfs2_xa_cleanup_value_truncate() is going to
2023399ff3a7SJoel Becker 			 * wipe the entry anyway.  So we check the
2024399ff3a7SJoel Becker 			 * cluster count as well.
2025399ff3a7SJoel Becker 			 */
2026399ff3a7SJoel Becker 			if (orig_clusters != ocfs2_xa_value_clusters(loc))
2027399ff3a7SJoel Becker 				rc = 0;
2028399ff3a7SJoel Becker 			ocfs2_xa_cleanup_value_truncate(loc, "removing",
2029399ff3a7SJoel Becker 							orig_clusters);
2030399ff3a7SJoel Becker 			if (rc)
203173857ee0SJoel Becker 				goto out;
203273857ee0SJoel Becker 		}
203373857ee0SJoel Becker 	}
203473857ee0SJoel Becker 
203573857ee0SJoel Becker 	ocfs2_xa_remove_entry(loc);
203673857ee0SJoel Becker 
203773857ee0SJoel Becker out:
203873857ee0SJoel Becker 	return rc;
203973857ee0SJoel Becker }
204073857ee0SJoel Becker 
ocfs2_xa_install_value_root(struct ocfs2_xa_loc * loc)204173857ee0SJoel Becker static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
204273857ee0SJoel Becker {
204373857ee0SJoel Becker 	int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
204473857ee0SJoel Becker 	char *nameval_buf;
204573857ee0SJoel Becker 
204673857ee0SJoel Becker 	nameval_buf = ocfs2_xa_offset_pointer(loc,
204773857ee0SJoel Becker 				le16_to_cpu(loc->xl_entry->xe_name_offset));
204873857ee0SJoel Becker 	memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
204973857ee0SJoel Becker }
205073857ee0SJoel Becker 
205173857ee0SJoel Becker /*
205273857ee0SJoel Becker  * Take an existing entry and make it ready for the new value.  This
205373857ee0SJoel Becker  * won't allocate space, but it may free space.  It should be ready for
205473857ee0SJoel Becker  * ocfs2_xa_prepare_entry() to finish the work.
205573857ee0SJoel Becker  */
ocfs2_xa_reuse_entry(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_set_ctxt * ctxt)205673857ee0SJoel Becker static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
205773857ee0SJoel Becker 				struct ocfs2_xattr_info *xi,
205873857ee0SJoel Becker 				struct ocfs2_xattr_set_ctxt *ctxt)
205973857ee0SJoel Becker {
206073857ee0SJoel Becker 	int rc = 0;
206173857ee0SJoel Becker 	int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2062399ff3a7SJoel Becker 	unsigned int orig_clusters;
206373857ee0SJoel Becker 	char *nameval_buf;
206473857ee0SJoel Becker 	int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
206573857ee0SJoel Becker 	int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
206673857ee0SJoel Becker 
206773857ee0SJoel Becker 	BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
206873857ee0SJoel Becker 	       name_size);
206973857ee0SJoel Becker 
207073857ee0SJoel Becker 	nameval_buf = ocfs2_xa_offset_pointer(loc,
207173857ee0SJoel Becker 				le16_to_cpu(loc->xl_entry->xe_name_offset));
207273857ee0SJoel Becker 	if (xe_local) {
207373857ee0SJoel Becker 		memset(nameval_buf + name_size, 0,
207473857ee0SJoel Becker 		       namevalue_size_xe(loc->xl_entry) - name_size);
207573857ee0SJoel Becker 		if (!xi_local)
207673857ee0SJoel Becker 			ocfs2_xa_install_value_root(loc);
207773857ee0SJoel Becker 	} else {
2078399ff3a7SJoel Becker 		orig_clusters = ocfs2_xa_value_clusters(loc);
207973857ee0SJoel Becker 		if (xi_local) {
208073857ee0SJoel Becker 			rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2081399ff3a7SJoel Becker 			if (rc < 0)
208273857ee0SJoel Becker 				mlog_errno(rc);
2083399ff3a7SJoel Becker 			else
208473857ee0SJoel Becker 				memset(nameval_buf + name_size, 0,
208573857ee0SJoel Becker 				       namevalue_size_xe(loc->xl_entry) -
208673857ee0SJoel Becker 				       name_size);
208773857ee0SJoel Becker 		} else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
208873857ee0SJoel Becker 			   xi->xi_value_len) {
208973857ee0SJoel Becker 			rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
209073857ee0SJoel Becker 						     ctxt);
2091399ff3a7SJoel Becker 			if (rc < 0)
209273857ee0SJoel Becker 				mlog_errno(rc);
209373857ee0SJoel Becker 		}
2094399ff3a7SJoel Becker 
2095399ff3a7SJoel Becker 		if (rc) {
2096399ff3a7SJoel Becker 			ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2097399ff3a7SJoel Becker 							orig_clusters);
2098399ff3a7SJoel Becker 			goto out;
209973857ee0SJoel Becker 		}
210073857ee0SJoel Becker 	}
210173857ee0SJoel Becker 
210273857ee0SJoel Becker 	loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
210373857ee0SJoel Becker 	ocfs2_xattr_set_local(loc->xl_entry, xi_local);
210473857ee0SJoel Becker 
210573857ee0SJoel Becker out:
210673857ee0SJoel Becker 	return rc;
210773857ee0SJoel Becker }
210873857ee0SJoel Becker 
210969a3e539SJoel Becker /*
211069a3e539SJoel Becker  * Prepares loc->xl_entry to receive the new xattr.  This includes
211169a3e539SJoel Becker  * properly setting up the name+value pair region.  If loc->xl_entry
211269a3e539SJoel Becker  * already exists, it will take care of modifying it appropriately.
211369a3e539SJoel Becker  *
211469a3e539SJoel Becker  * Note that this modifies the data.  You did journal_access already,
211569a3e539SJoel Becker  * right?
211669a3e539SJoel Becker  */
ocfs2_xa_prepare_entry(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi,u32 name_hash,struct ocfs2_xattr_set_ctxt * ctxt)211769a3e539SJoel Becker static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
211869a3e539SJoel Becker 				  struct ocfs2_xattr_info *xi,
211973857ee0SJoel Becker 				  u32 name_hash,
212073857ee0SJoel Becker 				  struct ocfs2_xattr_set_ctxt *ctxt)
212169a3e539SJoel Becker {
212269a3e539SJoel Becker 	int rc = 0;
2123399ff3a7SJoel Becker 	unsigned int orig_clusters;
2124399ff3a7SJoel Becker 	__le64 orig_value_size = 0;
212569a3e539SJoel Becker 
212669a3e539SJoel Becker 	rc = ocfs2_xa_check_space(loc, xi);
212769a3e539SJoel Becker 	if (rc)
212869a3e539SJoel Becker 		goto out;
212969a3e539SJoel Becker 
213094b07b6fSJoseph Qi 	if (loc->xl_entry) {
213169a3e539SJoel Becker 		if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2132399ff3a7SJoel Becker 			orig_value_size = loc->xl_entry->xe_value_size;
213373857ee0SJoel Becker 			rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
213473857ee0SJoel Becker 			if (rc)
213569a3e539SJoel Becker 				goto out;
213673857ee0SJoel Becker 			goto alloc_value;
213769a3e539SJoel Becker 		}
213869a3e539SJoel Becker 
213973857ee0SJoel Becker 		if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2140399ff3a7SJoel Becker 			orig_clusters = ocfs2_xa_value_clusters(loc);
214173857ee0SJoel Becker 			rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
214273857ee0SJoel Becker 			if (rc) {
214373857ee0SJoel Becker 				mlog_errno(rc);
2144399ff3a7SJoel Becker 				ocfs2_xa_cleanup_value_truncate(loc,
2145399ff3a7SJoel Becker 								"overwriting",
2146399ff3a7SJoel Becker 								orig_clusters);
214773857ee0SJoel Becker 				goto out;
214873857ee0SJoel Becker 			}
214973857ee0SJoel Becker 		}
215069a3e539SJoel Becker 		ocfs2_xa_wipe_namevalue(loc);
215194b07b6fSJoseph Qi 	} else
215294b07b6fSJoseph Qi 		ocfs2_xa_add_entry(loc, name_hash);
215369a3e539SJoel Becker 
215469a3e539SJoel Becker 	/*
215569a3e539SJoel Becker 	 * If we get here, we have a blank entry.  Fill it.  We grow our
215669a3e539SJoel Becker 	 * name+value pair back from the end.
215769a3e539SJoel Becker 	 */
215869a3e539SJoel Becker 	ocfs2_xa_add_namevalue(loc, xi);
215973857ee0SJoel Becker 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
216073857ee0SJoel Becker 		ocfs2_xa_install_value_root(loc);
216173857ee0SJoel Becker 
216273857ee0SJoel Becker alloc_value:
216373857ee0SJoel Becker 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2164399ff3a7SJoel Becker 		orig_clusters = ocfs2_xa_value_clusters(loc);
216573857ee0SJoel Becker 		rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2166399ff3a7SJoel Becker 		if (rc < 0) {
21675f5261acSTao Ma 			ctxt->set_abort = 1;
2168399ff3a7SJoel Becker 			ocfs2_xa_cleanup_value_truncate(loc, "growing",
2169399ff3a7SJoel Becker 							orig_clusters);
2170d5a7df06STao Ma 			/*
2171d5a7df06STao Ma 			 * If we were growing an existing value,
2172d5a7df06STao Ma 			 * ocfs2_xa_cleanup_value_truncate() won't remove
2173d5a7df06STao Ma 			 * the entry. We need to restore the original value
2174d5a7df06STao Ma 			 * size.
2175d5a7df06STao Ma 			 */
2176d5a7df06STao Ma 			if (loc->xl_entry) {
2177d5a7df06STao Ma 				BUG_ON(!orig_value_size);
2178d5a7df06STao Ma 				loc->xl_entry->xe_value_size = orig_value_size;
2179d5a7df06STao Ma 			}
218073857ee0SJoel Becker 			mlog_errno(rc);
218173857ee0SJoel Becker 		}
2182399ff3a7SJoel Becker 	}
218369a3e539SJoel Becker 
218469a3e539SJoel Becker out:
218569a3e539SJoel Becker 	return rc;
218669a3e539SJoel Becker }
218769a3e539SJoel Becker 
218869a3e539SJoel Becker /*
218973857ee0SJoel Becker  * Store the value portion of the name+value pair.  This will skip
219073857ee0SJoel Becker  * values that are stored externally.  Their tree roots were set up
219173857ee0SJoel Becker  * by ocfs2_xa_prepare_entry().
219269a3e539SJoel Becker  */
ocfs2_xa_store_value(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_set_ctxt * ctxt)219373857ee0SJoel Becker static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
219473857ee0SJoel Becker 				struct ocfs2_xattr_info *xi,
219573857ee0SJoel Becker 				struct ocfs2_xattr_set_ctxt *ctxt)
219669a3e539SJoel Becker {
219773857ee0SJoel Becker 	int rc = 0;
219869a3e539SJoel Becker 	int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
219969a3e539SJoel Becker 	int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
220069a3e539SJoel Becker 	char *nameval_buf;
220173857ee0SJoel Becker 	struct ocfs2_xattr_value_buf vb;
220269a3e539SJoel Becker 
220369a3e539SJoel Becker 	nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
220473857ee0SJoel Becker 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
220573857ee0SJoel Becker 		ocfs2_xa_fill_value_buf(loc, &vb);
220673857ee0SJoel Becker 		rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
220773857ee0SJoel Becker 						     ctxt->handle, &vb,
220873857ee0SJoel Becker 						     xi->xi_value,
220973857ee0SJoel Becker 						     xi->xi_value_len);
221073857ee0SJoel Becker 	} else
221173857ee0SJoel Becker 		memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
221273857ee0SJoel Becker 
221373857ee0SJoel Becker 	return rc;
221469a3e539SJoel Becker }
221569a3e539SJoel Becker 
ocfs2_xa_set(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_set_ctxt * ctxt)2216bca5e9bdSJoel Becker static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2217bca5e9bdSJoel Becker 			struct ocfs2_xattr_info *xi,
2218bca5e9bdSJoel Becker 			struct ocfs2_xattr_set_ctxt *ctxt)
2219bca5e9bdSJoel Becker {
2220bca5e9bdSJoel Becker 	int ret;
2221bca5e9bdSJoel Becker 	u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2222bca5e9bdSJoel Becker 					      xi->xi_name_len);
2223bca5e9bdSJoel Becker 
2224bca5e9bdSJoel Becker 	ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2225bca5e9bdSJoel Becker 				      OCFS2_JOURNAL_ACCESS_WRITE);
2226bca5e9bdSJoel Becker 	if (ret) {
2227bca5e9bdSJoel Becker 		mlog_errno(ret);
2228bca5e9bdSJoel Becker 		goto out;
2229bca5e9bdSJoel Becker 	}
2230bca5e9bdSJoel Becker 
2231399ff3a7SJoel Becker 	/*
2232399ff3a7SJoel Becker 	 * From here on out, everything is going to modify the buffer a
2233399ff3a7SJoel Becker 	 * little.  Errors are going to leave the xattr header in a
2234399ff3a7SJoel Becker 	 * sane state.  Thus, even with errors we dirty the sucker.
2235399ff3a7SJoel Becker 	 */
2236399ff3a7SJoel Becker 
2237bca5e9bdSJoel Becker 	/* Don't worry, we are never called with !xi_value and !xl_entry */
2238bca5e9bdSJoel Becker 	if (!xi->xi_value) {
2239bca5e9bdSJoel Becker 		ret = ocfs2_xa_remove(loc, ctxt);
2240399ff3a7SJoel Becker 		goto out_dirty;
2241bca5e9bdSJoel Becker 	}
2242bca5e9bdSJoel Becker 
2243bca5e9bdSJoel Becker 	ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2244bca5e9bdSJoel Becker 	if (ret) {
2245bca5e9bdSJoel Becker 		if (ret != -ENOSPC)
2246bca5e9bdSJoel Becker 			mlog_errno(ret);
2247399ff3a7SJoel Becker 		goto out_dirty;
2248bca5e9bdSJoel Becker 	}
2249bca5e9bdSJoel Becker 
2250bca5e9bdSJoel Becker 	ret = ocfs2_xa_store_value(loc, xi, ctxt);
2251399ff3a7SJoel Becker 	if (ret)
2252bca5e9bdSJoel Becker 		mlog_errno(ret);
2253bca5e9bdSJoel Becker 
2254399ff3a7SJoel Becker out_dirty:
2255bca5e9bdSJoel Becker 	ocfs2_xa_journal_dirty(ctxt->handle, loc);
2256bca5e9bdSJoel Becker 
2257bca5e9bdSJoel Becker out:
2258bca5e9bdSJoel Becker 	return ret;
2259bca5e9bdSJoel Becker }
2260bca5e9bdSJoel Becker 
ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc * loc,struct inode * inode,struct buffer_head * bh,struct ocfs2_xattr_entry * entry)226111179f2cSJoel Becker static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
226211179f2cSJoel Becker 				     struct inode *inode,
226311179f2cSJoel Becker 				     struct buffer_head *bh,
226411179f2cSJoel Becker 				     struct ocfs2_xattr_entry *entry)
226511179f2cSJoel Becker {
226611179f2cSJoel Becker 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
226711179f2cSJoel Becker 
2268139ffaceSJoel Becker 	BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2269139ffaceSJoel Becker 
2270cf2bc809SJoel Becker 	loc->xl_inode = inode;
227111179f2cSJoel Becker 	loc->xl_ops = &ocfs2_xa_block_loc_ops;
227211179f2cSJoel Becker 	loc->xl_storage = bh;
227311179f2cSJoel Becker 	loc->xl_entry = entry;
227411179f2cSJoel Becker 	loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
227511179f2cSJoel Becker 	loc->xl_header =
227611179f2cSJoel Becker 		(struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
227711179f2cSJoel Becker 					      loc->xl_size);
227811179f2cSJoel Becker }
227911179f2cSJoel Becker 
ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc * loc,struct inode * inode,struct buffer_head * bh,struct ocfs2_xattr_entry * entry)228011179f2cSJoel Becker static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2281cf2bc809SJoel Becker 					  struct inode *inode,
228211179f2cSJoel Becker 					  struct buffer_head *bh,
228311179f2cSJoel Becker 					  struct ocfs2_xattr_entry *entry)
228411179f2cSJoel Becker {
228511179f2cSJoel Becker 	struct ocfs2_xattr_block *xb =
228611179f2cSJoel Becker 		(struct ocfs2_xattr_block *)bh->b_data;
228711179f2cSJoel Becker 
228811179f2cSJoel Becker 	BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
228911179f2cSJoel Becker 
2290cf2bc809SJoel Becker 	loc->xl_inode = inode;
229111179f2cSJoel Becker 	loc->xl_ops = &ocfs2_xa_block_loc_ops;
229211179f2cSJoel Becker 	loc->xl_storage = bh;
229311179f2cSJoel Becker 	loc->xl_header = &(xb->xb_attrs.xb_header);
229411179f2cSJoel Becker 	loc->xl_entry = entry;
229511179f2cSJoel Becker 	loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
229611179f2cSJoel Becker 					     xb_attrs.xb_header);
229711179f2cSJoel Becker }
229811179f2cSJoel Becker 
ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_bucket * bucket,struct ocfs2_xattr_entry * entry)229911179f2cSJoel Becker static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
230011179f2cSJoel Becker 					   struct ocfs2_xattr_bucket *bucket,
230111179f2cSJoel Becker 					   struct ocfs2_xattr_entry *entry)
230211179f2cSJoel Becker {
2303cf2bc809SJoel Becker 	loc->xl_inode = bucket->bu_inode;
230411179f2cSJoel Becker 	loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
230511179f2cSJoel Becker 	loc->xl_storage = bucket;
230611179f2cSJoel Becker 	loc->xl_header = bucket_xh(bucket);
230711179f2cSJoel Becker 	loc->xl_entry = entry;
230811179f2cSJoel Becker 	loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
230911179f2cSJoel Becker }
231011179f2cSJoel Becker 
2311ce9c5a54STao Ma /*
2312ce9c5a54STao Ma  * In xattr remove, if it is stored outside and refcounted, we may have
2313ce9c5a54STao Ma  * the chance to split the refcount tree. So need the allocators.
2314ce9c5a54STao Ma  */
ocfs2_lock_xattr_remove_allocators(struct inode * inode,struct ocfs2_xattr_value_root * xv,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_alloc_context ** meta_ac,int * ref_credits)2315ce9c5a54STao Ma static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2316ce9c5a54STao Ma 					struct ocfs2_xattr_value_root *xv,
2317ce9c5a54STao Ma 					struct ocfs2_caching_info *ref_ci,
2318ce9c5a54STao Ma 					struct buffer_head *ref_root_bh,
2319ce9c5a54STao Ma 					struct ocfs2_alloc_context **meta_ac,
2320ce9c5a54STao Ma 					int *ref_credits)
2321cf1d6c76STiger Yang {
2322ce9c5a54STao Ma 	int ret, meta_add = 0;
2323ce9c5a54STao Ma 	u32 p_cluster, num_clusters;
2324ce9c5a54STao Ma 	unsigned int ext_flags;
232578f30c31STao Ma 
2326ce9c5a54STao Ma 	*ref_credits = 0;
2327ce9c5a54STao Ma 	ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2328ce9c5a54STao Ma 				       &num_clusters,
2329ce9c5a54STao Ma 				       &xv->xr_list,
2330ce9c5a54STao Ma 				       &ext_flags);
2331ce9c5a54STao Ma 	if (ret) {
233285db90e7STao Ma 		mlog_errno(ret);
233385db90e7STao Ma 		goto out;
233485db90e7STao Ma 	}
233585db90e7STao Ma 
2336ce9c5a54STao Ma 	if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2337ce9c5a54STao Ma 		goto out;
2338ce9c5a54STao Ma 
2339ce9c5a54STao Ma 	ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2340ce9c5a54STao Ma 						 ref_root_bh, xv,
2341ce9c5a54STao Ma 						 &meta_add, ref_credits);
2342ce9c5a54STao Ma 	if (ret) {
2343ce9c5a54STao Ma 		mlog_errno(ret);
2344ce9c5a54STao Ma 		goto out;
2345ce9c5a54STao Ma 	}
2346ce9c5a54STao Ma 
2347ce9c5a54STao Ma 	ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2348ce9c5a54STao Ma 						meta_add, meta_ac);
2349ce9c5a54STao Ma 	if (ret)
2350ce9c5a54STao Ma 		mlog_errno(ret);
2351ce9c5a54STao Ma 
2352ce9c5a54STao Ma out:
2353ce9c5a54STao Ma 	return ret;
2354ce9c5a54STao Ma }
2355ce9c5a54STao Ma 
ocfs2_remove_value_outside(struct inode * inode,struct ocfs2_xattr_value_buf * vb,struct ocfs2_xattr_header * header,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh)2356ce9c5a54STao Ma static int ocfs2_remove_value_outside(struct inode*inode,
2357ce9c5a54STao Ma 				      struct ocfs2_xattr_value_buf *vb,
2358ce9c5a54STao Ma 				      struct ocfs2_xattr_header *header,
2359ce9c5a54STao Ma 				      struct ocfs2_caching_info *ref_ci,
2360ce9c5a54STao Ma 				      struct buffer_head *ref_root_bh)
2361ce9c5a54STao Ma {
2362ce9c5a54STao Ma 	int ret = 0, i, ref_credits;
2363ce9c5a54STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2364ce9c5a54STao Ma 	struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2365ce9c5a54STao Ma 	void *val;
2366ce9c5a54STao Ma 
2367ce9c5a54STao Ma 	ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2368ce9c5a54STao Ma 
2369cf1d6c76STiger Yang 	for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2370cf1d6c76STiger Yang 		struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2371cf1d6c76STiger Yang 
2372ce9c5a54STao Ma 		if (ocfs2_xattr_is_local(entry))
2373ce9c5a54STao Ma 			continue;
2374cf1d6c76STiger Yang 
2375cf1d6c76STiger Yang 		val = (void *)header +
2376cf1d6c76STiger Yang 			le16_to_cpu(entry->xe_name_offset);
23774311901dSJoel Becker 		vb->vb_xv = (struct ocfs2_xattr_value_root *)
2378cf1d6c76STiger Yang 			(val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2379ce9c5a54STao Ma 
2380ce9c5a54STao Ma 		ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2381ce9c5a54STao Ma 							 ref_ci, ref_root_bh,
2382ce9c5a54STao Ma 							 &ctxt.meta_ac,
2383ce9c5a54STao Ma 							 &ref_credits);
2384ce9c5a54STao Ma 
2385ce9c5a54STao Ma 		ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2386ce9c5a54STao Ma 					ocfs2_remove_extent_credits(osb->sb));
2387ce9c5a54STao Ma 		if (IS_ERR(ctxt.handle)) {
2388ce9c5a54STao Ma 			ret = PTR_ERR(ctxt.handle);
2389ce9c5a54STao Ma 			mlog_errno(ret);
2390ce9c5a54STao Ma 			break;
2391ce9c5a54STao Ma 		}
2392ce9c5a54STao Ma 
23934311901dSJoel Becker 		ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2394ce9c5a54STao Ma 
2395ce9c5a54STao Ma 		ocfs2_commit_trans(osb, ctxt.handle);
2396ce9c5a54STao Ma 		if (ctxt.meta_ac) {
2397ce9c5a54STao Ma 			ocfs2_free_alloc_context(ctxt.meta_ac);
2398ce9c5a54STao Ma 			ctxt.meta_ac = NULL;
2399cf1d6c76STiger Yang 		}
2400b8a0ae57SWengang Wang 
2401b8a0ae57SWengang Wang 		if (ret < 0) {
2402b8a0ae57SWengang Wang 			mlog_errno(ret);
2403b8a0ae57SWengang Wang 			break;
2404b8a0ae57SWengang Wang 		}
2405b8a0ae57SWengang Wang 
2406cf1d6c76STiger Yang 	}
2407cf1d6c76STiger Yang 
2408ce9c5a54STao Ma 	if (ctxt.meta_ac)
2409ce9c5a54STao Ma 		ocfs2_free_alloc_context(ctxt.meta_ac);
241078f30c31STao Ma 	ocfs2_schedule_truncate_log_flush(osb, 1);
241178f30c31STao Ma 	ocfs2_run_deallocs(osb, &ctxt.dealloc);
2412cf1d6c76STiger Yang 	return ret;
2413cf1d6c76STiger Yang }
2414cf1d6c76STiger Yang 
ocfs2_xattr_ibody_remove(struct inode * inode,struct buffer_head * di_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh)2415cf1d6c76STiger Yang static int ocfs2_xattr_ibody_remove(struct inode *inode,
2416ce9c5a54STao Ma 				    struct buffer_head *di_bh,
2417ce9c5a54STao Ma 				    struct ocfs2_caching_info *ref_ci,
2418ce9c5a54STao Ma 				    struct buffer_head *ref_root_bh)
2419cf1d6c76STiger Yang {
2420cf1d6c76STiger Yang 
2421cf1d6c76STiger Yang 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2422cf1d6c76STiger Yang 	struct ocfs2_xattr_header *header;
2423cf1d6c76STiger Yang 	int ret;
24244311901dSJoel Becker 	struct ocfs2_xattr_value_buf vb = {
24254311901dSJoel Becker 		.vb_bh = di_bh,
24264311901dSJoel Becker 		.vb_access = ocfs2_journal_access_di,
24274311901dSJoel Becker 	};
2428cf1d6c76STiger Yang 
2429cf1d6c76STiger Yang 	header = (struct ocfs2_xattr_header *)
2430cf1d6c76STiger Yang 		 ((void *)di + inode->i_sb->s_blocksize -
2431cf1d6c76STiger Yang 		 le16_to_cpu(di->i_xattr_inline_size));
2432cf1d6c76STiger Yang 
2433ce9c5a54STao Ma 	ret = ocfs2_remove_value_outside(inode, &vb, header,
2434ce9c5a54STao Ma 					 ref_ci, ref_root_bh);
2435cf1d6c76STiger Yang 
2436cf1d6c76STiger Yang 	return ret;
2437cf1d6c76STiger Yang }
2438cf1d6c76STiger Yang 
2439ce9c5a54STao Ma struct ocfs2_rm_xattr_bucket_para {
2440ce9c5a54STao Ma 	struct ocfs2_caching_info *ref_ci;
2441ce9c5a54STao Ma 	struct buffer_head *ref_root_bh;
2442ce9c5a54STao Ma };
2443ce9c5a54STao Ma 
ocfs2_xattr_block_remove(struct inode * inode,struct buffer_head * blk_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh)2444cf1d6c76STiger Yang static int ocfs2_xattr_block_remove(struct inode *inode,
2445ce9c5a54STao Ma 				    struct buffer_head *blk_bh,
2446ce9c5a54STao Ma 				    struct ocfs2_caching_info *ref_ci,
2447ce9c5a54STao Ma 				    struct buffer_head *ref_root_bh)
2448cf1d6c76STiger Yang {
2449cf1d6c76STiger Yang 	struct ocfs2_xattr_block *xb;
2450cf1d6c76STiger Yang 	int ret = 0;
24514311901dSJoel Becker 	struct ocfs2_xattr_value_buf vb = {
24524311901dSJoel Becker 		.vb_bh = blk_bh,
24534311901dSJoel Becker 		.vb_access = ocfs2_journal_access_xb,
24544311901dSJoel Becker 	};
2455ce9c5a54STao Ma 	struct ocfs2_rm_xattr_bucket_para args = {
2456ce9c5a54STao Ma 		.ref_ci = ref_ci,
2457ce9c5a54STao Ma 		.ref_root_bh = ref_root_bh,
2458ce9c5a54STao Ma 	};
2459cf1d6c76STiger Yang 
2460cf1d6c76STiger Yang 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2461a3944256STao Ma 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2462a3944256STao Ma 		struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2463ce9c5a54STao Ma 		ret = ocfs2_remove_value_outside(inode, &vb, header,
2464ce9c5a54STao Ma 						 ref_ci, ref_root_bh);
2465a3944256STao Ma 	} else
246647bca495STao Ma 		ret = ocfs2_iterate_xattr_index_block(inode,
246747bca495STao Ma 						blk_bh,
246847bca495STao Ma 						ocfs2_rm_xattr_cluster,
2469ce9c5a54STao Ma 						&args);
2470cf1d6c76STiger Yang 
2471cf1d6c76STiger Yang 	return ret;
2472cf1d6c76STiger Yang }
2473cf1d6c76STiger Yang 
ocfs2_xattr_free_block(struct inode * inode,u64 block,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh)247408413899STao Ma static int ocfs2_xattr_free_block(struct inode *inode,
2475ce9c5a54STao Ma 				  u64 block,
2476ce9c5a54STao Ma 				  struct ocfs2_caching_info *ref_ci,
2477ce9c5a54STao Ma 				  struct buffer_head *ref_root_bh)
247808413899STao Ma {
247908413899STao Ma 	struct inode *xb_alloc_inode;
248008413899STao Ma 	struct buffer_head *xb_alloc_bh = NULL;
248108413899STao Ma 	struct buffer_head *blk_bh = NULL;
248208413899STao Ma 	struct ocfs2_xattr_block *xb;
248308413899STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
248408413899STao Ma 	handle_t *handle;
248508413899STao Ma 	int ret = 0;
248608413899STao Ma 	u64 blk, bg_blkno;
248708413899STao Ma 	u16 bit;
248808413899STao Ma 
24894ae1d69bSJoel Becker 	ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
249008413899STao Ma 	if (ret < 0) {
249108413899STao Ma 		mlog_errno(ret);
249208413899STao Ma 		goto out;
249308413899STao Ma 	}
249408413899STao Ma 
2495ce9c5a54STao Ma 	ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
249608413899STao Ma 	if (ret < 0) {
249708413899STao Ma 		mlog_errno(ret);
249808413899STao Ma 		goto out;
249908413899STao Ma 	}
250008413899STao Ma 
25014ae1d69bSJoel Becker 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
250208413899STao Ma 	blk = le64_to_cpu(xb->xb_blkno);
250308413899STao Ma 	bit = le16_to_cpu(xb->xb_suballoc_bit);
250474380c47STao Ma 	if (xb->xb_suballoc_loc)
250574380c47STao Ma 		bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
250674380c47STao Ma 	else
250708413899STao Ma 		bg_blkno = ocfs2_which_suballoc_group(blk, bit);
250808413899STao Ma 
250908413899STao Ma 	xb_alloc_inode = ocfs2_get_system_file_inode(osb,
251008413899STao Ma 				EXTENT_ALLOC_SYSTEM_INODE,
251108413899STao Ma 				le16_to_cpu(xb->xb_suballoc_slot));
251208413899STao Ma 	if (!xb_alloc_inode) {
251308413899STao Ma 		ret = -ENOMEM;
251408413899STao Ma 		mlog_errno(ret);
251508413899STao Ma 		goto out;
251608413899STao Ma 	}
25175955102cSAl Viro 	inode_lock(xb_alloc_inode);
251808413899STao Ma 
251908413899STao Ma 	ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
252008413899STao Ma 	if (ret < 0) {
252108413899STao Ma 		mlog_errno(ret);
252208413899STao Ma 		goto out_mutex;
252308413899STao Ma 	}
252408413899STao Ma 
252508413899STao Ma 	handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
252608413899STao Ma 	if (IS_ERR(handle)) {
252708413899STao Ma 		ret = PTR_ERR(handle);
252808413899STao Ma 		mlog_errno(ret);
252908413899STao Ma 		goto out_unlock;
253008413899STao Ma 	}
253108413899STao Ma 
253208413899STao Ma 	ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
253308413899STao Ma 				       bit, bg_blkno, 1);
253408413899STao Ma 	if (ret < 0)
253508413899STao Ma 		mlog_errno(ret);
253608413899STao Ma 
253708413899STao Ma 	ocfs2_commit_trans(osb, handle);
253808413899STao Ma out_unlock:
253908413899STao Ma 	ocfs2_inode_unlock(xb_alloc_inode, 1);
254008413899STao Ma 	brelse(xb_alloc_bh);
254108413899STao Ma out_mutex:
25425955102cSAl Viro 	inode_unlock(xb_alloc_inode);
254308413899STao Ma 	iput(xb_alloc_inode);
254408413899STao Ma out:
254508413899STao Ma 	brelse(blk_bh);
254608413899STao Ma 	return ret;
254708413899STao Ma }
254808413899STao Ma 
2549cf1d6c76STiger Yang /*
2550cf1d6c76STiger Yang  * ocfs2_xattr_remove()
2551cf1d6c76STiger Yang  *
2552cf1d6c76STiger Yang  * Free extended attribute resources associated with this inode.
2553cf1d6c76STiger Yang  */
ocfs2_xattr_remove(struct inode * inode,struct buffer_head * di_bh)2554cf1d6c76STiger Yang int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2555cf1d6c76STiger Yang {
2556cf1d6c76STiger Yang 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2557cf1d6c76STiger Yang 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2558ce9c5a54STao Ma 	struct ocfs2_refcount_tree *ref_tree = NULL;
2559ce9c5a54STao Ma 	struct buffer_head *ref_root_bh = NULL;
2560ce9c5a54STao Ma 	struct ocfs2_caching_info *ref_ci = NULL;
2561cf1d6c76STiger Yang 	handle_t *handle;
2562cf1d6c76STiger Yang 	int ret;
2563cf1d6c76STiger Yang 
25648154da3dSTiger Yang 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
25658154da3dSTiger Yang 		return 0;
25668154da3dSTiger Yang 
2567cf1d6c76STiger Yang 	if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2568cf1d6c76STiger Yang 		return 0;
2569cf1d6c76STiger Yang 
257084e40080SDarrick J. Wong 	if (ocfs2_is_refcount_inode(inode)) {
2571ce9c5a54STao Ma 		ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2572ce9c5a54STao Ma 					       le64_to_cpu(di->i_refcount_loc),
2573ce9c5a54STao Ma 					       1, &ref_tree, &ref_root_bh);
2574ce9c5a54STao Ma 		if (ret) {
2575ce9c5a54STao Ma 			mlog_errno(ret);
2576ce9c5a54STao Ma 			goto out;
2577ce9c5a54STao Ma 		}
2578ce9c5a54STao Ma 		ref_ci = &ref_tree->rf_ci;
2579ce9c5a54STao Ma 
2580ce9c5a54STao Ma 	}
2581ce9c5a54STao Ma 
2582cf1d6c76STiger Yang 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2583ce9c5a54STao Ma 		ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2584ce9c5a54STao Ma 					       ref_ci, ref_root_bh);
2585cf1d6c76STiger Yang 		if (ret < 0) {
2586cf1d6c76STiger Yang 			mlog_errno(ret);
2587cf1d6c76STiger Yang 			goto out;
2588cf1d6c76STiger Yang 		}
2589cf1d6c76STiger Yang 	}
2590cf1d6c76STiger Yang 
259108413899STao Ma 	if (di->i_xattr_loc) {
259208413899STao Ma 		ret = ocfs2_xattr_free_block(inode,
2593ce9c5a54STao Ma 					     le64_to_cpu(di->i_xattr_loc),
2594ce9c5a54STao Ma 					     ref_ci, ref_root_bh);
2595cf1d6c76STiger Yang 		if (ret < 0) {
2596cf1d6c76STiger Yang 			mlog_errno(ret);
2597cf1d6c76STiger Yang 			goto out;
2598cf1d6c76STiger Yang 		}
2599cf1d6c76STiger Yang 	}
2600cf1d6c76STiger Yang 
2601cf1d6c76STiger Yang 	handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2602cf1d6c76STiger Yang 				   OCFS2_INODE_UPDATE_CREDITS);
2603cf1d6c76STiger Yang 	if (IS_ERR(handle)) {
2604cf1d6c76STiger Yang 		ret = PTR_ERR(handle);
2605cf1d6c76STiger Yang 		mlog_errno(ret);
2606cf1d6c76STiger Yang 		goto out;
2607cf1d6c76STiger Yang 	}
26080cf2f763SJoel Becker 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2609cf1d6c76STiger Yang 				      OCFS2_JOURNAL_ACCESS_WRITE);
2610cf1d6c76STiger Yang 	if (ret) {
2611cf1d6c76STiger Yang 		mlog_errno(ret);
2612cf1d6c76STiger Yang 		goto out_commit;
2613cf1d6c76STiger Yang 	}
2614cf1d6c76STiger Yang 
261508413899STao Ma 	di->i_xattr_loc = 0;
2616cf1d6c76STiger Yang 
2617cf1d6c76STiger Yang 	spin_lock(&oi->ip_lock);
2618cf1d6c76STiger Yang 	oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2619cf1d6c76STiger Yang 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2620cf1d6c76STiger Yang 	spin_unlock(&oi->ip_lock);
26216fdb702dSDarrick J. Wong 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
2622cf1d6c76STiger Yang 
2623ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, di_bh);
2624cf1d6c76STiger Yang out_commit:
2625cf1d6c76STiger Yang 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2626cf1d6c76STiger Yang out:
2627ce9c5a54STao Ma 	if (ref_tree)
2628ce9c5a54STao Ma 		ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2629ce9c5a54STao Ma 	brelse(ref_root_bh);
2630cf1d6c76STiger Yang 	return ret;
2631cf1d6c76STiger Yang }
2632cf1d6c76STiger Yang 
ocfs2_xattr_has_space_inline(struct inode * inode,struct ocfs2_dinode * di)2633cf1d6c76STiger Yang static int ocfs2_xattr_has_space_inline(struct inode *inode,
2634cf1d6c76STiger Yang 					struct ocfs2_dinode *di)
2635cf1d6c76STiger Yang {
2636cf1d6c76STiger Yang 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2637cf1d6c76STiger Yang 	unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2638cf1d6c76STiger Yang 	int free;
2639cf1d6c76STiger Yang 
2640cf1d6c76STiger Yang 	if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2641cf1d6c76STiger Yang 		return 0;
2642cf1d6c76STiger Yang 
2643cf1d6c76STiger Yang 	if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2644cf1d6c76STiger Yang 		struct ocfs2_inline_data *idata = &di->id2.i_data;
2645cf1d6c76STiger Yang 		free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2646cf1d6c76STiger Yang 	} else if (ocfs2_inode_is_fast_symlink(inode)) {
2647cf1d6c76STiger Yang 		free = ocfs2_fast_symlink_chars(inode->i_sb) -
2648cf1d6c76STiger Yang 			le64_to_cpu(di->i_size);
2649cf1d6c76STiger Yang 	} else {
2650cf1d6c76STiger Yang 		struct ocfs2_extent_list *el = &di->id2.i_list;
2651cf1d6c76STiger Yang 		free = (le16_to_cpu(el->l_count) -
2652cf1d6c76STiger Yang 			le16_to_cpu(el->l_next_free_rec)) *
2653cf1d6c76STiger Yang 			sizeof(struct ocfs2_extent_rec);
2654cf1d6c76STiger Yang 	}
2655cf1d6c76STiger Yang 	if (free >= xattrsize)
2656cf1d6c76STiger Yang 		return 1;
2657cf1d6c76STiger Yang 
2658cf1d6c76STiger Yang 	return 0;
2659cf1d6c76STiger Yang }
2660cf1d6c76STiger Yang 
2661cf1d6c76STiger Yang /*
2662cf1d6c76STiger Yang  * ocfs2_xattr_ibody_find()
2663cf1d6c76STiger Yang  *
2664cf1d6c76STiger Yang  * Find extended attribute in inode block and
2665cf1d6c76STiger Yang  * fill search info into struct ocfs2_xattr_search.
2666cf1d6c76STiger Yang  */
ocfs2_xattr_ibody_find(struct inode * inode,int name_index,const char * name,struct ocfs2_xattr_search * xs)2667cf1d6c76STiger Yang static int ocfs2_xattr_ibody_find(struct inode *inode,
2668cf1d6c76STiger Yang 				  int name_index,
2669cf1d6c76STiger Yang 				  const char *name,
2670cf1d6c76STiger Yang 				  struct ocfs2_xattr_search *xs)
2671cf1d6c76STiger Yang {
2672cf1d6c76STiger Yang 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2673cf1d6c76STiger Yang 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2674cf1d6c76STiger Yang 	int ret;
2675cf1d6c76STiger Yang 	int has_space = 0;
2676cf1d6c76STiger Yang 
2677cf1d6c76STiger Yang 	if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2678cf1d6c76STiger Yang 		return 0;
2679cf1d6c76STiger Yang 
2680cf1d6c76STiger Yang 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2681cf1d6c76STiger Yang 		down_read(&oi->ip_alloc_sem);
2682cf1d6c76STiger Yang 		has_space = ocfs2_xattr_has_space_inline(inode, di);
2683cf1d6c76STiger Yang 		up_read(&oi->ip_alloc_sem);
2684cf1d6c76STiger Yang 		if (!has_space)
2685cf1d6c76STiger Yang 			return 0;
2686cf1d6c76STiger Yang 	}
2687cf1d6c76STiger Yang 
2688cf1d6c76STiger Yang 	xs->xattr_bh = xs->inode_bh;
2689cf1d6c76STiger Yang 	xs->end = (void *)di + inode->i_sb->s_blocksize;
2690cf1d6c76STiger Yang 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2691cf1d6c76STiger Yang 		xs->header = (struct ocfs2_xattr_header *)
2692cf1d6c76STiger Yang 			(xs->end - le16_to_cpu(di->i_xattr_inline_size));
2693cf1d6c76STiger Yang 	else
2694cf1d6c76STiger Yang 		xs->header = (struct ocfs2_xattr_header *)
2695cf1d6c76STiger Yang 			(xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2696cf1d6c76STiger Yang 	xs->base = (void *)xs->header;
2697cf1d6c76STiger Yang 	xs->here = xs->header->xh_entries;
2698cf1d6c76STiger Yang 
2699cf1d6c76STiger Yang 	/* Find the named attribute. */
2700cf1d6c76STiger Yang 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2701cf1d6c76STiger Yang 		ret = ocfs2_xattr_find_entry(name_index, name, xs);
2702cf1d6c76STiger Yang 		if (ret && ret != -ENODATA)
2703cf1d6c76STiger Yang 			return ret;
2704cf1d6c76STiger Yang 		xs->not_found = ret;
2705cf1d6c76STiger Yang 	}
2706cf1d6c76STiger Yang 
2707cf1d6c76STiger Yang 	return 0;
2708cf1d6c76STiger Yang }
2709cf1d6c76STiger Yang 
ocfs2_xattr_ibody_init(struct inode * inode,struct buffer_head * di_bh,struct ocfs2_xattr_set_ctxt * ctxt)2710139ffaceSJoel Becker static int ocfs2_xattr_ibody_init(struct inode *inode,
2711139ffaceSJoel Becker 				  struct buffer_head *di_bh,
2712139ffaceSJoel Becker 				  struct ocfs2_xattr_set_ctxt *ctxt)
2713139ffaceSJoel Becker {
2714139ffaceSJoel Becker 	int ret;
2715139ffaceSJoel Becker 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2716139ffaceSJoel Becker 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2717139ffaceSJoel Becker 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2718139ffaceSJoel Becker 	unsigned int xattrsize = osb->s_xattr_inline_size;
2719139ffaceSJoel Becker 
2720139ffaceSJoel Becker 	if (!ocfs2_xattr_has_space_inline(inode, di)) {
2721139ffaceSJoel Becker 		ret = -ENOSPC;
2722139ffaceSJoel Becker 		goto out;
2723139ffaceSJoel Becker 	}
2724139ffaceSJoel Becker 
2725139ffaceSJoel Becker 	ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2726139ffaceSJoel Becker 				      OCFS2_JOURNAL_ACCESS_WRITE);
2727139ffaceSJoel Becker 	if (ret) {
2728139ffaceSJoel Becker 		mlog_errno(ret);
2729139ffaceSJoel Becker 		goto out;
2730139ffaceSJoel Becker 	}
2731139ffaceSJoel Becker 
2732139ffaceSJoel Becker 	/*
2733139ffaceSJoel Becker 	 * Adjust extent record count or inline data size
2734139ffaceSJoel Becker 	 * to reserve space for extended attribute.
2735139ffaceSJoel Becker 	 */
2736139ffaceSJoel Becker 	if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2737139ffaceSJoel Becker 		struct ocfs2_inline_data *idata = &di->id2.i_data;
2738139ffaceSJoel Becker 		le16_add_cpu(&idata->id_count, -xattrsize);
2739139ffaceSJoel Becker 	} else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2740139ffaceSJoel Becker 		struct ocfs2_extent_list *el = &di->id2.i_list;
2741139ffaceSJoel Becker 		le16_add_cpu(&el->l_count, -(xattrsize /
2742139ffaceSJoel Becker 					     sizeof(struct ocfs2_extent_rec)));
2743139ffaceSJoel Becker 	}
2744139ffaceSJoel Becker 	di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2745139ffaceSJoel Becker 
2746139ffaceSJoel Becker 	spin_lock(&oi->ip_lock);
2747139ffaceSJoel Becker 	oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2748139ffaceSJoel Becker 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2749139ffaceSJoel Becker 	spin_unlock(&oi->ip_lock);
2750139ffaceSJoel Becker 
2751ec20cec7SJoel Becker 	ocfs2_journal_dirty(ctxt->handle, di_bh);
2752139ffaceSJoel Becker 
2753139ffaceSJoel Becker out:
2754139ffaceSJoel Becker 	return ret;
2755139ffaceSJoel Becker }
2756139ffaceSJoel Becker 
2757cf1d6c76STiger Yang /*
2758cf1d6c76STiger Yang  * ocfs2_xattr_ibody_set()
2759cf1d6c76STiger Yang  *
2760cf1d6c76STiger Yang  * Set, replace or remove an extended attribute into inode block.
2761cf1d6c76STiger Yang  *
2762cf1d6c76STiger Yang  */
ocfs2_xattr_ibody_set(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)2763cf1d6c76STiger Yang static int ocfs2_xattr_ibody_set(struct inode *inode,
2764cf1d6c76STiger Yang 				 struct ocfs2_xattr_info *xi,
276578f30c31STao Ma 				 struct ocfs2_xattr_search *xs,
276678f30c31STao Ma 				 struct ocfs2_xattr_set_ctxt *ctxt)
2767cf1d6c76STiger Yang {
2768139ffaceSJoel Becker 	int ret;
2769cf1d6c76STiger Yang 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2770139ffaceSJoel Becker 	struct ocfs2_xa_loc loc;
2771cf1d6c76STiger Yang 
2772cf1d6c76STiger Yang 	if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2773cf1d6c76STiger Yang 		return -ENOSPC;
2774cf1d6c76STiger Yang 
2775cf1d6c76STiger Yang 	down_write(&oi->ip_alloc_sem);
2776cf1d6c76STiger Yang 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2777139ffaceSJoel Becker 		ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2778139ffaceSJoel Becker 		if (ret) {
2779139ffaceSJoel Becker 			if (ret != -ENOSPC)
2780139ffaceSJoel Becker 				mlog_errno(ret);
2781139ffaceSJoel Becker 			goto out;
2782139ffaceSJoel Becker 		}
2783139ffaceSJoel Becker 	}
2784139ffaceSJoel Becker 
2785139ffaceSJoel Becker 	ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2786139ffaceSJoel Becker 				 xs->not_found ? NULL : xs->here);
2787139ffaceSJoel Becker 	ret = ocfs2_xa_set(&loc, xi, ctxt);
2788139ffaceSJoel Becker 	if (ret) {
2789139ffaceSJoel Becker 		if (ret != -ENOSPC)
2790139ffaceSJoel Becker 			mlog_errno(ret);
2791139ffaceSJoel Becker 		goto out;
2792139ffaceSJoel Becker 	}
2793139ffaceSJoel Becker 	xs->here = loc.xl_entry;
2794139ffaceSJoel Becker 
2795cf1d6c76STiger Yang out:
2796cf1d6c76STiger Yang 	up_write(&oi->ip_alloc_sem);
2797cf1d6c76STiger Yang 
2798cf1d6c76STiger Yang 	return ret;
2799cf1d6c76STiger Yang }
2800cf1d6c76STiger Yang 
2801cf1d6c76STiger Yang /*
2802cf1d6c76STiger Yang  * ocfs2_xattr_block_find()
2803cf1d6c76STiger Yang  *
2804cf1d6c76STiger Yang  * Find extended attribute in external block and
2805cf1d6c76STiger Yang  * fill search info into struct ocfs2_xattr_search.
2806cf1d6c76STiger Yang  */
ocfs2_xattr_block_find(struct inode * inode,int name_index,const char * name,struct ocfs2_xattr_search * xs)2807cf1d6c76STiger Yang static int ocfs2_xattr_block_find(struct inode *inode,
2808cf1d6c76STiger Yang 				  int name_index,
2809cf1d6c76STiger Yang 				  const char *name,
2810cf1d6c76STiger Yang 				  struct ocfs2_xattr_search *xs)
2811cf1d6c76STiger Yang {
2812cf1d6c76STiger Yang 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2813cf1d6c76STiger Yang 	struct buffer_head *blk_bh = NULL;
2814589dc260STao Ma 	struct ocfs2_xattr_block *xb;
2815cf1d6c76STiger Yang 	int ret = 0;
2816cf1d6c76STiger Yang 
2817cf1d6c76STiger Yang 	if (!di->i_xattr_loc)
2818cf1d6c76STiger Yang 		return ret;
2819cf1d6c76STiger Yang 
28204ae1d69bSJoel Becker 	ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
28214ae1d69bSJoel Becker 				     &blk_bh);
2822cf1d6c76STiger Yang 	if (ret < 0) {
2823cf1d6c76STiger Yang 		mlog_errno(ret);
2824cf1d6c76STiger Yang 		return ret;
2825cf1d6c76STiger Yang 	}
2826f6087fb7SJoel Becker 
2827cf1d6c76STiger Yang 	xs->xattr_bh = blk_bh;
28284ae1d69bSJoel Becker 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2829589dc260STao Ma 
2830589dc260STao Ma 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2831589dc260STao Ma 		xs->header = &xb->xb_attrs.xb_header;
2832cf1d6c76STiger Yang 		xs->base = (void *)xs->header;
2833cf1d6c76STiger Yang 		xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2834cf1d6c76STiger Yang 		xs->here = xs->header->xh_entries;
2835cf1d6c76STiger Yang 
2836cf1d6c76STiger Yang 		ret = ocfs2_xattr_find_entry(name_index, name, xs);
2837589dc260STao Ma 	} else
2838589dc260STao Ma 		ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2839589dc260STao Ma 						   name_index,
2840589dc260STao Ma 						   name, xs);
2841589dc260STao Ma 
2842cf1d6c76STiger Yang 	if (ret && ret != -ENODATA) {
2843cf1d6c76STiger Yang 		xs->xattr_bh = NULL;
2844cf1d6c76STiger Yang 		goto cleanup;
2845cf1d6c76STiger Yang 	}
2846cf1d6c76STiger Yang 	xs->not_found = ret;
2847cf1d6c76STiger Yang 	return 0;
2848cf1d6c76STiger Yang cleanup:
2849cf1d6c76STiger Yang 	brelse(blk_bh);
2850cf1d6c76STiger Yang 
2851cf1d6c76STiger Yang 	return ret;
2852cf1d6c76STiger Yang }
2853cf1d6c76STiger Yang 
ocfs2_create_xattr_block(struct inode * inode,struct buffer_head * inode_bh,struct ocfs2_xattr_set_ctxt * ctxt,int indexed,struct buffer_head ** ret_bh)2854d3981544SJoel Becker static int ocfs2_create_xattr_block(struct inode *inode,
28555aea1f0eSTao Ma 				    struct buffer_head *inode_bh,
2856d3981544SJoel Becker 				    struct ocfs2_xattr_set_ctxt *ctxt,
2857d3981544SJoel Becker 				    int indexed,
2858d3981544SJoel Becker 				    struct buffer_head **ret_bh)
2859cf1d6c76STiger Yang {
28605aea1f0eSTao Ma 	int ret;
2861cf1d6c76STiger Yang 	u16 suballoc_bit_start;
2862cf1d6c76STiger Yang 	u32 num_got;
28632b6cb576SJoel Becker 	u64 suballoc_loc, first_blkno;
28645aea1f0eSTao Ma 	struct ocfs2_dinode *di =  (struct ocfs2_dinode *)inode_bh->b_data;
28655aea1f0eSTao Ma 	struct buffer_head *new_bh = NULL;
28665aea1f0eSTao Ma 	struct ocfs2_xattr_block *xblk;
2867cf1d6c76STiger Yang 
2868d3981544SJoel Becker 	ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2869d3981544SJoel Becker 				      inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2870cf1d6c76STiger Yang 	if (ret < 0) {
2871cf1d6c76STiger Yang 		mlog_errno(ret);
287285db90e7STao Ma 		goto end;
2873cf1d6c76STiger Yang 	}
2874cf1d6c76STiger Yang 
28751ed9b777SJoel Becker 	ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
28762b6cb576SJoel Becker 				   &suballoc_loc, &suballoc_bit_start,
28772b6cb576SJoel Becker 				   &num_got, &first_blkno);
2878cf1d6c76STiger Yang 	if (ret < 0) {
2879cf1d6c76STiger Yang 		mlog_errno(ret);
288085db90e7STao Ma 		goto end;
2881cf1d6c76STiger Yang 	}
2882cf1d6c76STiger Yang 
2883cf1d6c76STiger Yang 	new_bh = sb_getblk(inode->i_sb, first_blkno);
288458796207SRui Xiang 	if (!new_bh) {
288558796207SRui Xiang 		ret = -ENOMEM;
288658796207SRui Xiang 		mlog_errno(ret);
288758796207SRui Xiang 		goto end;
288858796207SRui Xiang 	}
288958796207SRui Xiang 
28908cb471e8SJoel Becker 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2891cf1d6c76STiger Yang 
2892d3981544SJoel Becker 	ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
28930cf2f763SJoel Becker 				      new_bh,
2894cf1d6c76STiger Yang 				      OCFS2_JOURNAL_ACCESS_CREATE);
2895cf1d6c76STiger Yang 	if (ret < 0) {
2896cf1d6c76STiger Yang 		mlog_errno(ret);
289785db90e7STao Ma 		goto end;
2898cf1d6c76STiger Yang 	}
2899cf1d6c76STiger Yang 
2900cf1d6c76STiger Yang 	/* Initialize ocfs2_xattr_block */
2901cf1d6c76STiger Yang 	xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2902cf1d6c76STiger Yang 	memset(xblk, 0, inode->i_sb->s_blocksize);
2903cf1d6c76STiger Yang 	strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2904d3981544SJoel Becker 	xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
29052b6cb576SJoel Becker 	xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2906cf1d6c76STiger Yang 	xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
29071ed9b777SJoel Becker 	xblk->xb_fs_generation =
29081ed9b777SJoel Becker 		cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2909cf1d6c76STiger Yang 	xblk->xb_blkno = cpu_to_le64(first_blkno);
2910a7fe7a3aSTao Ma 	if (indexed) {
2911a7fe7a3aSTao Ma 		struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2912a7fe7a3aSTao Ma 		xr->xt_clusters = cpu_to_le32(1);
2913a7fe7a3aSTao Ma 		xr->xt_last_eb_blk = 0;
2914a7fe7a3aSTao Ma 		xr->xt_list.l_tree_depth = 0;
2915a7fe7a3aSTao Ma 		xr->xt_list.l_count = cpu_to_le16(
2916a7fe7a3aSTao Ma 					ocfs2_xattr_recs_per_xb(inode->i_sb));
2917a7fe7a3aSTao Ma 		xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2918a7fe7a3aSTao Ma 		xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2919a7fe7a3aSTao Ma 	}
2920d3981544SJoel Becker 	ocfs2_journal_dirty(ctxt->handle, new_bh);
2921a7fe7a3aSTao Ma 
2922d3981544SJoel Becker 	/* Add it to the inode */
2923cf1d6c76STiger Yang 	di->i_xattr_loc = cpu_to_le64(first_blkno);
2924d3981544SJoel Becker 
2925d3981544SJoel Becker 	spin_lock(&OCFS2_I(inode)->ip_lock);
2926d3981544SJoel Becker 	OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2927d3981544SJoel Becker 	di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2928d3981544SJoel Becker 	spin_unlock(&OCFS2_I(inode)->ip_lock);
2929d3981544SJoel Becker 
2930d3981544SJoel Becker 	ocfs2_journal_dirty(ctxt->handle, inode_bh);
29315aea1f0eSTao Ma 
29325aea1f0eSTao Ma 	*ret_bh = new_bh;
29335aea1f0eSTao Ma 	new_bh = NULL;
29345aea1f0eSTao Ma 
29355aea1f0eSTao Ma end:
29365aea1f0eSTao Ma 	brelse(new_bh);
29375aea1f0eSTao Ma 	return ret;
29385aea1f0eSTao Ma }
29395aea1f0eSTao Ma 
29405aea1f0eSTao Ma /*
29415aea1f0eSTao Ma  * ocfs2_xattr_block_set()
29425aea1f0eSTao Ma  *
29435aea1f0eSTao Ma  * Set, replace or remove an extended attribute into external block.
29445aea1f0eSTao Ma  *
29455aea1f0eSTao Ma  */
ocfs2_xattr_block_set(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)29465aea1f0eSTao Ma static int ocfs2_xattr_block_set(struct inode *inode,
29475aea1f0eSTao Ma 				 struct ocfs2_xattr_info *xi,
29485aea1f0eSTao Ma 				 struct ocfs2_xattr_search *xs,
29495aea1f0eSTao Ma 				 struct ocfs2_xattr_set_ctxt *ctxt)
29505aea1f0eSTao Ma {
29515aea1f0eSTao Ma 	struct buffer_head *new_bh = NULL;
29525aea1f0eSTao Ma 	struct ocfs2_xattr_block *xblk = NULL;
29535aea1f0eSTao Ma 	int ret;
2954d3981544SJoel Becker 	struct ocfs2_xa_loc loc;
29555aea1f0eSTao Ma 
29565aea1f0eSTao Ma 	if (!xs->xattr_bh) {
2957d3981544SJoel Becker 		ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2958d3981544SJoel Becker 					       0, &new_bh);
29595aea1f0eSTao Ma 		if (ret) {
29605aea1f0eSTao Ma 			mlog_errno(ret);
29615aea1f0eSTao Ma 			goto end;
29625aea1f0eSTao Ma 		}
29635aea1f0eSTao Ma 
29645aea1f0eSTao Ma 		xs->xattr_bh = new_bh;
29655aea1f0eSTao Ma 		xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
29665aea1f0eSTao Ma 		xs->header = &xblk->xb_attrs.xb_header;
29675aea1f0eSTao Ma 		xs->base = (void *)xs->header;
29685aea1f0eSTao Ma 		xs->end = (void *)xblk + inode->i_sb->s_blocksize;
29695aea1f0eSTao Ma 		xs->here = xs->header->xh_entries;
297001225596STao Ma 	} else
297101225596STao Ma 		xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2972cf1d6c76STiger Yang 
297301225596STao Ma 	if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2974d3981544SJoel Becker 		ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2975d3981544SJoel Becker 					      xs->not_found ? NULL : xs->here);
297601225596STao Ma 
2977d3981544SJoel Becker 		ret = ocfs2_xa_set(&loc, xi, ctxt);
2978d3981544SJoel Becker 		if (!ret)
2979d3981544SJoel Becker 			xs->here = loc.xl_entry;
29805f5261acSTao Ma 		else if ((ret != -ENOSPC) || ctxt->set_abort)
2981d3981544SJoel Becker 			goto end;
2982d3981544SJoel Becker 		else {
298378f30c31STao Ma 			ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
298401225596STao Ma 			if (ret)
298501225596STao Ma 				goto end;
298601225596STao Ma 		}
2987d3981544SJoel Becker 	}
298801225596STao Ma 
2989d3981544SJoel Becker 	if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
299078f30c31STao Ma 		ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
299101225596STao Ma 
299201225596STao Ma end:
2993cf1d6c76STiger Yang 	return ret;
2994cf1d6c76STiger Yang }
2995cf1d6c76STiger Yang 
299678f30c31STao Ma /* Check whether the new xattr can be inserted into the inode. */
ocfs2_xattr_can_be_in_inode(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs)299778f30c31STao Ma static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
299878f30c31STao Ma 				       struct ocfs2_xattr_info *xi,
299978f30c31STao Ma 				       struct ocfs2_xattr_search *xs)
300078f30c31STao Ma {
300178f30c31STao Ma 	struct ocfs2_xattr_entry *last;
300278f30c31STao Ma 	int free, i;
300378f30c31STao Ma 	size_t min_offs = xs->end - xs->base;
300478f30c31STao Ma 
300578f30c31STao Ma 	if (!xs->header)
300678f30c31STao Ma 		return 0;
300778f30c31STao Ma 
300878f30c31STao Ma 	last = xs->header->xh_entries;
300978f30c31STao Ma 
301078f30c31STao Ma 	for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
301178f30c31STao Ma 		size_t offs = le16_to_cpu(last->xe_name_offset);
301278f30c31STao Ma 		if (offs < min_offs)
301378f30c31STao Ma 			min_offs = offs;
301478f30c31STao Ma 		last += 1;
301578f30c31STao Ma 	}
301678f30c31STao Ma 
30174442f518STiger Yang 	free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
301878f30c31STao Ma 	if (free < 0)
301978f30c31STao Ma 		return 0;
302078f30c31STao Ma 
302178f30c31STao Ma 	BUG_ON(!xs->not_found);
302278f30c31STao Ma 
3023199799a3SJoel Becker 	if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
302478f30c31STao Ma 		return 1;
302578f30c31STao Ma 
302678f30c31STao Ma 	return 0;
302778f30c31STao Ma }
302878f30c31STao Ma 
ocfs2_calc_xattr_set_need(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xis,struct ocfs2_xattr_search * xbs,int * clusters_need,int * meta_need,int * credits_need)302978f30c31STao Ma static int ocfs2_calc_xattr_set_need(struct inode *inode,
303078f30c31STao Ma 				     struct ocfs2_dinode *di,
303178f30c31STao Ma 				     struct ocfs2_xattr_info *xi,
303278f30c31STao Ma 				     struct ocfs2_xattr_search *xis,
303378f30c31STao Ma 				     struct ocfs2_xattr_search *xbs,
303478f30c31STao Ma 				     int *clusters_need,
303585db90e7STao Ma 				     int *meta_need,
303685db90e7STao Ma 				     int *credits_need)
303778f30c31STao Ma {
303878f30c31STao Ma 	int ret = 0, old_in_xb = 0;
303985db90e7STao Ma 	int clusters_add = 0, meta_add = 0, credits = 0;
304078f30c31STao Ma 	struct buffer_head *bh = NULL;
304178f30c31STao Ma 	struct ocfs2_xattr_block *xb = NULL;
304278f30c31STao Ma 	struct ocfs2_xattr_entry *xe = NULL;
304378f30c31STao Ma 	struct ocfs2_xattr_value_root *xv = NULL;
304478f30c31STao Ma 	char *base = NULL;
304578f30c31STao Ma 	int name_offset, name_len = 0;
304678f30c31STao Ma 	u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
30476b240ff6SJoel Becker 						    xi->xi_value_len);
304878f30c31STao Ma 	u64 value_size;
304978f30c31STao Ma 
305071d548a6STao Ma 	/*
305171d548a6STao Ma 	 * Calculate the clusters we need to write.
305271d548a6STao Ma 	 * No matter whether we replace an old one or add a new one,
305371d548a6STao Ma 	 * we need this for writing.
305471d548a6STao Ma 	 */
30556b240ff6SJoel Becker 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
305671d548a6STao Ma 		credits += new_clusters *
305771d548a6STao Ma 			   ocfs2_clusters_to_blocks(inode->i_sb, 1);
305871d548a6STao Ma 
305978f30c31STao Ma 	if (xis->not_found && xbs->not_found) {
306085db90e7STao Ma 		credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
306185db90e7STao Ma 
30626b240ff6SJoel Becker 		if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
306378f30c31STao Ma 			clusters_add += new_clusters;
306485db90e7STao Ma 			credits += ocfs2_calc_extend_credits(inode->i_sb,
306506f9da6eSGoldwyn Rodrigues 							&def_xv.xv.xr_list);
306685db90e7STao Ma 		}
306778f30c31STao Ma 
306878f30c31STao Ma 		goto meta_guess;
306978f30c31STao Ma 	}
307078f30c31STao Ma 
307178f30c31STao Ma 	if (!xis->not_found) {
307278f30c31STao Ma 		xe = xis->here;
307378f30c31STao Ma 		name_offset = le16_to_cpu(xe->xe_name_offset);
307478f30c31STao Ma 		name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
307578f30c31STao Ma 		base = xis->base;
307685db90e7STao Ma 		credits += OCFS2_INODE_UPDATE_CREDITS;
307778f30c31STao Ma 	} else {
3078970e4936SJoel Becker 		int i, block_off = 0;
307978f30c31STao Ma 		xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
308078f30c31STao Ma 		xe = xbs->here;
308178f30c31STao Ma 		name_offset = le16_to_cpu(xe->xe_name_offset);
308278f30c31STao Ma 		name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
308378f30c31STao Ma 		i = xbs->here - xbs->header->xh_entries;
308478f30c31STao Ma 		old_in_xb = 1;
308578f30c31STao Ma 
308678f30c31STao Ma 		if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3087fd68a894STao Ma 			ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
308878f30c31STao Ma 							bucket_xh(xbs->bucket),
308978f30c31STao Ma 							i, &block_off,
309078f30c31STao Ma 							&name_offset);
309178f30c31STao Ma 			base = bucket_block(xbs->bucket, block_off);
309285db90e7STao Ma 			credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
309385db90e7STao Ma 		} else {
309478f30c31STao Ma 			base = xbs->base;
309585db90e7STao Ma 			credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
309685db90e7STao Ma 		}
309785db90e7STao Ma 	}
309885db90e7STao Ma 
309985db90e7STao Ma 	/*
310085db90e7STao Ma 	 * delete a xattr doesn't need metadata and cluster allocation.
310185db90e7STao Ma 	 * so just calculate the credits and return.
310285db90e7STao Ma 	 *
310385db90e7STao Ma 	 * The credits for removing the value tree will be extended
310485db90e7STao Ma 	 * by ocfs2_remove_extent itself.
310585db90e7STao Ma 	 */
31066b240ff6SJoel Becker 	if (!xi->xi_value) {
310785db90e7STao Ma 		if (!ocfs2_xattr_is_local(xe))
3108a90714c1SJan Kara 			credits += ocfs2_remove_extent_credits(inode->i_sb);
310985db90e7STao Ma 
311085db90e7STao Ma 		goto out;
311178f30c31STao Ma 	}
311278f30c31STao Ma 
311378f30c31STao Ma 	/* do cluster allocation guess first. */
311478f30c31STao Ma 	value_size = le64_to_cpu(xe->xe_value_size);
311578f30c31STao Ma 
311678f30c31STao Ma 	if (old_in_xb) {
311778f30c31STao Ma 		/*
311878f30c31STao Ma 		 * In xattr set, we always try to set the xe in inode first,
311978f30c31STao Ma 		 * so if it can be inserted into inode successfully, the old
312078f30c31STao Ma 		 * one will be removed from the xattr block, and this xattr
312178f30c31STao Ma 		 * will be inserted into inode as a new xattr in inode.
312278f30c31STao Ma 		 */
312378f30c31STao Ma 		if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
312478f30c31STao Ma 			clusters_add += new_clusters;
3125a90714c1SJan Kara 			credits += ocfs2_remove_extent_credits(inode->i_sb) +
312685db90e7STao Ma 				    OCFS2_INODE_UPDATE_CREDITS;
312785db90e7STao Ma 			if (!ocfs2_xattr_is_local(xe))
312885db90e7STao Ma 				credits += ocfs2_calc_extend_credits(
312985db90e7STao Ma 							inode->i_sb,
313006f9da6eSGoldwyn Rodrigues 							&def_xv.xv.xr_list);
313178f30c31STao Ma 			goto out;
313278f30c31STao Ma 		}
313378f30c31STao Ma 	}
313478f30c31STao Ma 
31356b240ff6SJoel Becker 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
313678f30c31STao Ma 		/* the new values will be stored outside. */
313778f30c31STao Ma 		u32 old_clusters = 0;
313878f30c31STao Ma 
313978f30c31STao Ma 		if (!ocfs2_xattr_is_local(xe)) {
314078f30c31STao Ma 			old_clusters =	ocfs2_clusters_for_bytes(inode->i_sb,
314178f30c31STao Ma 								 value_size);
314278f30c31STao Ma 			xv = (struct ocfs2_xattr_value_root *)
314378f30c31STao Ma 			     (base + name_offset + name_len);
314497aff52aSTao Ma 			value_size = OCFS2_XATTR_ROOT_SIZE;
314578f30c31STao Ma 		} else
314678f30c31STao Ma 			xv = &def_xv.xv;
314778f30c31STao Ma 
314885db90e7STao Ma 		if (old_clusters >= new_clusters) {
3149a90714c1SJan Kara 			credits += ocfs2_remove_extent_credits(inode->i_sb);
315078f30c31STao Ma 			goto out;
315185db90e7STao Ma 		} else {
315278f30c31STao Ma 			meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
315378f30c31STao Ma 			clusters_add += new_clusters - old_clusters;
315485db90e7STao Ma 			credits += ocfs2_calc_extend_credits(inode->i_sb,
315506f9da6eSGoldwyn Rodrigues 							     &xv->xr_list);
315697aff52aSTao Ma 			if (value_size >= OCFS2_XATTR_ROOT_SIZE)
315778f30c31STao Ma 				goto out;
315878f30c31STao Ma 		}
315978f30c31STao Ma 	} else {
316078f30c31STao Ma 		/*
316178f30c31STao Ma 		 * Now the new value will be stored inside. So if the new
316278f30c31STao Ma 		 * value is smaller than the size of value root or the old
316378f30c31STao Ma 		 * value, we don't need any allocation, otherwise we have
316478f30c31STao Ma 		 * to guess metadata allocation.
316578f30c31STao Ma 		 */
31666b240ff6SJoel Becker 		if ((ocfs2_xattr_is_local(xe) &&
31676b240ff6SJoel Becker 		     (value_size >= xi->xi_value_len)) ||
316878f30c31STao Ma 		    (!ocfs2_xattr_is_local(xe) &&
31696b240ff6SJoel Becker 		     OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
317078f30c31STao Ma 			goto out;
317178f30c31STao Ma 	}
317278f30c31STao Ma 
317378f30c31STao Ma meta_guess:
317478f30c31STao Ma 	/* calculate metadata allocation. */
317578f30c31STao Ma 	if (di->i_xattr_loc) {
317678f30c31STao Ma 		if (!xbs->xattr_bh) {
31774ae1d69bSJoel Becker 			ret = ocfs2_read_xattr_block(inode,
317878f30c31STao Ma 						     le64_to_cpu(di->i_xattr_loc),
317978f30c31STao Ma 						     &bh);
318078f30c31STao Ma 			if (ret) {
318178f30c31STao Ma 				mlog_errno(ret);
318278f30c31STao Ma 				goto out;
318378f30c31STao Ma 			}
318478f30c31STao Ma 
318578f30c31STao Ma 			xb = (struct ocfs2_xattr_block *)bh->b_data;
318678f30c31STao Ma 		} else
318778f30c31STao Ma 			xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
318878f30c31STao Ma 
318990cb546cSTao Ma 		/*
319090cb546cSTao Ma 		 * If there is already an xattr tree, good, we can calculate
319190cb546cSTao Ma 		 * like other b-trees. Otherwise we may have the chance of
319290cb546cSTao Ma 		 * create a tree, the credit calculation is borrowed from
319390cb546cSTao Ma 		 * ocfs2_calc_extend_credits with root_el = NULL. And the
319490cb546cSTao Ma 		 * new tree will be cluster based, so no meta is needed.
319590cb546cSTao Ma 		 */
319678f30c31STao Ma 		if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
319778f30c31STao Ma 			struct ocfs2_extent_list *el =
319878f30c31STao Ma 				 &xb->xb_attrs.xb_root.xt_list;
319978f30c31STao Ma 			meta_add += ocfs2_extend_meta_needed(el);
320085db90e7STao Ma 			credits += ocfs2_calc_extend_credits(inode->i_sb,
320106f9da6eSGoldwyn Rodrigues 							     el);
320290cb546cSTao Ma 		} else
320390cb546cSTao Ma 			credits += OCFS2_SUBALLOC_ALLOC + 1;
320478f30c31STao Ma 
320578f30c31STao Ma 		/*
320678f30c31STao Ma 		 * This cluster will be used either for new bucket or for
320778f30c31STao Ma 		 * new xattr block.
320878f30c31STao Ma 		 * If the cluster size is the same as the bucket size, one
320978f30c31STao Ma 		 * more is needed since we may need to extend the bucket
321078f30c31STao Ma 		 * also.
321178f30c31STao Ma 		 */
321278f30c31STao Ma 		clusters_add += 1;
321385db90e7STao Ma 		credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
321478f30c31STao Ma 		if (OCFS2_XATTR_BUCKET_SIZE ==
321585db90e7STao Ma 			OCFS2_SB(inode->i_sb)->s_clustersize) {
321685db90e7STao Ma 			credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
321778f30c31STao Ma 			clusters_add += 1;
321885db90e7STao Ma 		}
321985db90e7STao Ma 	} else {
322085db90e7STao Ma 		credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
32213ed2be71STariq Saeed 		if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
32223ed2be71STariq Saeed 			struct ocfs2_extent_list *el = &def_xv.xv.xr_list;
32233ed2be71STariq Saeed 			meta_add += ocfs2_extend_meta_needed(el);
32243ed2be71STariq Saeed 			credits += ocfs2_calc_extend_credits(inode->i_sb,
32253ed2be71STariq Saeed 							     el);
32263ed2be71STariq Saeed 		} else {
32273ed2be71STariq Saeed 			meta_add += 1;
32283ed2be71STariq Saeed 		}
322985db90e7STao Ma 	}
323078f30c31STao Ma out:
323178f30c31STao Ma 	if (clusters_need)
323278f30c31STao Ma 		*clusters_need = clusters_add;
323378f30c31STao Ma 	if (meta_need)
323478f30c31STao Ma 		*meta_need = meta_add;
323585db90e7STao Ma 	if (credits_need)
323685db90e7STao Ma 		*credits_need = credits;
323778f30c31STao Ma 	brelse(bh);
323878f30c31STao Ma 	return ret;
323978f30c31STao Ma }
324078f30c31STao Ma 
ocfs2_init_xattr_set_ctxt(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xis,struct ocfs2_xattr_search * xbs,struct ocfs2_xattr_set_ctxt * ctxt,int extra_meta,int * credits)324178f30c31STao Ma static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
324278f30c31STao Ma 				     struct ocfs2_dinode *di,
324378f30c31STao Ma 				     struct ocfs2_xattr_info *xi,
324478f30c31STao Ma 				     struct ocfs2_xattr_search *xis,
324578f30c31STao Ma 				     struct ocfs2_xattr_search *xbs,
324685db90e7STao Ma 				     struct ocfs2_xattr_set_ctxt *ctxt,
3247492a8a33STao Ma 				     int extra_meta,
324885db90e7STao Ma 				     int *credits)
324978f30c31STao Ma {
325078f30c31STao Ma 	int clusters_add, meta_add, ret;
325178f30c31STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
325278f30c31STao Ma 
325378f30c31STao Ma 	memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
325478f30c31STao Ma 
325578f30c31STao Ma 	ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
325678f30c31STao Ma 
325778f30c31STao Ma 	ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
325885db90e7STao Ma 					&clusters_add, &meta_add, credits);
325978f30c31STao Ma 	if (ret) {
326078f30c31STao Ma 		mlog_errno(ret);
326178f30c31STao Ma 		return ret;
326278f30c31STao Ma 	}
326378f30c31STao Ma 
3264492a8a33STao Ma 	meta_add += extra_meta;
3265402b4183STao Ma 	trace_ocfs2_init_xattr_set_ctxt(xi->xi_name, meta_add,
3266402b4183STao Ma 					clusters_add, *credits);
326778f30c31STao Ma 
326878f30c31STao Ma 	if (meta_add) {
326978f30c31STao Ma 		ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
327078f30c31STao Ma 							&ctxt->meta_ac);
327178f30c31STao Ma 		if (ret) {
327278f30c31STao Ma 			mlog_errno(ret);
327378f30c31STao Ma 			goto out;
327478f30c31STao Ma 		}
327578f30c31STao Ma 	}
327678f30c31STao Ma 
327778f30c31STao Ma 	if (clusters_add) {
327878f30c31STao Ma 		ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
327978f30c31STao Ma 		if (ret)
328078f30c31STao Ma 			mlog_errno(ret);
328178f30c31STao Ma 	}
328278f30c31STao Ma out:
328378f30c31STao Ma 	if (ret) {
328478f30c31STao Ma 		if (ctxt->meta_ac) {
328578f30c31STao Ma 			ocfs2_free_alloc_context(ctxt->meta_ac);
328678f30c31STao Ma 			ctxt->meta_ac = NULL;
328778f30c31STao Ma 		}
328878f30c31STao Ma 
328978f30c31STao Ma 		/*
329078f30c31STao Ma 		 * We cannot have an error and a non null ctxt->data_ac.
329178f30c31STao Ma 		 */
329278f30c31STao Ma 	}
329378f30c31STao Ma 
329478f30c31STao Ma 	return ret;
329578f30c31STao Ma }
329678f30c31STao Ma 
__ocfs2_xattr_set_handle(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xis,struct ocfs2_xattr_search * xbs,struct ocfs2_xattr_set_ctxt * ctxt)329785db90e7STao Ma static int __ocfs2_xattr_set_handle(struct inode *inode,
329885db90e7STao Ma 				    struct ocfs2_dinode *di,
329985db90e7STao Ma 				    struct ocfs2_xattr_info *xi,
330085db90e7STao Ma 				    struct ocfs2_xattr_search *xis,
330185db90e7STao Ma 				    struct ocfs2_xattr_search *xbs,
330285db90e7STao Ma 				    struct ocfs2_xattr_set_ctxt *ctxt)
330385db90e7STao Ma {
33049f868f16STao Ma 	int ret = 0, credits, old_found;
330585db90e7STao Ma 
33066b240ff6SJoel Becker 	if (!xi->xi_value) {
330785db90e7STao Ma 		/* Remove existing extended attribute */
330885db90e7STao Ma 		if (!xis->not_found)
330985db90e7STao Ma 			ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
331085db90e7STao Ma 		else if (!xbs->not_found)
331185db90e7STao Ma 			ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
331285db90e7STao Ma 	} else {
331385db90e7STao Ma 		/* We always try to set extended attribute into inode first*/
331485db90e7STao Ma 		ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
331585db90e7STao Ma 		if (!ret && !xbs->not_found) {
331685db90e7STao Ma 			/*
331785db90e7STao Ma 			 * If succeed and that extended attribute existing in
331885db90e7STao Ma 			 * external block, then we will remove it.
331985db90e7STao Ma 			 */
33206b240ff6SJoel Becker 			xi->xi_value = NULL;
33216b240ff6SJoel Becker 			xi->xi_value_len = 0;
332285db90e7STao Ma 
33239f868f16STao Ma 			old_found = xis->not_found;
332485db90e7STao Ma 			xis->not_found = -ENODATA;
332585db90e7STao Ma 			ret = ocfs2_calc_xattr_set_need(inode,
332685db90e7STao Ma 							di,
332785db90e7STao Ma 							xi,
332885db90e7STao Ma 							xis,
332985db90e7STao Ma 							xbs,
333085db90e7STao Ma 							NULL,
333185db90e7STao Ma 							NULL,
333285db90e7STao Ma 							&credits);
33339f868f16STao Ma 			xis->not_found = old_found;
333485db90e7STao Ma 			if (ret) {
333585db90e7STao Ma 				mlog_errno(ret);
333685db90e7STao Ma 				goto out;
333785db90e7STao Ma 			}
333885db90e7STao Ma 
3339c901fb00STao Ma 			ret = ocfs2_extend_trans(ctxt->handle, credits);
334085db90e7STao Ma 			if (ret) {
334185db90e7STao Ma 				mlog_errno(ret);
334285db90e7STao Ma 				goto out;
334385db90e7STao Ma 			}
334485db90e7STao Ma 			ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
33455f5261acSTao Ma 		} else if ((ret == -ENOSPC) && !ctxt->set_abort) {
334685db90e7STao Ma 			if (di->i_xattr_loc && !xbs->xattr_bh) {
334785db90e7STao Ma 				ret = ocfs2_xattr_block_find(inode,
33486b240ff6SJoel Becker 							     xi->xi_name_index,
33496b240ff6SJoel Becker 							     xi->xi_name, xbs);
335085db90e7STao Ma 				if (ret)
335185db90e7STao Ma 					goto out;
335285db90e7STao Ma 
33539f868f16STao Ma 				old_found = xis->not_found;
335485db90e7STao Ma 				xis->not_found = -ENODATA;
335585db90e7STao Ma 				ret = ocfs2_calc_xattr_set_need(inode,
335685db90e7STao Ma 								di,
335785db90e7STao Ma 								xi,
335885db90e7STao Ma 								xis,
335985db90e7STao Ma 								xbs,
336085db90e7STao Ma 								NULL,
336185db90e7STao Ma 								NULL,
336285db90e7STao Ma 								&credits);
33639f868f16STao Ma 				xis->not_found = old_found;
336485db90e7STao Ma 				if (ret) {
336585db90e7STao Ma 					mlog_errno(ret);
336685db90e7STao Ma 					goto out;
336785db90e7STao Ma 				}
336885db90e7STao Ma 
3369c901fb00STao Ma 				ret = ocfs2_extend_trans(ctxt->handle, credits);
337085db90e7STao Ma 				if (ret) {
337185db90e7STao Ma 					mlog_errno(ret);
337285db90e7STao Ma 					goto out;
337385db90e7STao Ma 				}
337485db90e7STao Ma 			}
337585db90e7STao Ma 			/*
337685db90e7STao Ma 			 * If no space in inode, we will set extended attribute
337785db90e7STao Ma 			 * into external block.
337885db90e7STao Ma 			 */
337985db90e7STao Ma 			ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
338085db90e7STao Ma 			if (ret)
338185db90e7STao Ma 				goto out;
338285db90e7STao Ma 			if (!xis->not_found) {
338385db90e7STao Ma 				/*
338485db90e7STao Ma 				 * If succeed and that extended attribute
338585db90e7STao Ma 				 * existing in inode, we will remove it.
338685db90e7STao Ma 				 */
33876b240ff6SJoel Becker 				xi->xi_value = NULL;
33886b240ff6SJoel Becker 				xi->xi_value_len = 0;
338985db90e7STao Ma 				xbs->not_found = -ENODATA;
339085db90e7STao Ma 				ret = ocfs2_calc_xattr_set_need(inode,
339185db90e7STao Ma 								di,
339285db90e7STao Ma 								xi,
339385db90e7STao Ma 								xis,
339485db90e7STao Ma 								xbs,
339585db90e7STao Ma 								NULL,
339685db90e7STao Ma 								NULL,
339785db90e7STao Ma 								&credits);
339885db90e7STao Ma 				if (ret) {
339985db90e7STao Ma 					mlog_errno(ret);
340085db90e7STao Ma 					goto out;
340185db90e7STao Ma 				}
340285db90e7STao Ma 
3403c901fb00STao Ma 				ret = ocfs2_extend_trans(ctxt->handle, credits);
340485db90e7STao Ma 				if (ret) {
340585db90e7STao Ma 					mlog_errno(ret);
340685db90e7STao Ma 					goto out;
340785db90e7STao Ma 				}
340885db90e7STao Ma 				ret = ocfs2_xattr_ibody_set(inode, xi,
340985db90e7STao Ma 							    xis, ctxt);
341085db90e7STao Ma 			}
341185db90e7STao Ma 		}
341285db90e7STao Ma 	}
341385db90e7STao Ma 
34144b3f6209STao Ma 	if (!ret) {
34154b3f6209STao Ma 		/* Update inode ctime. */
34160cf2f763SJoel Becker 		ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
341789a907afSTao Ma 					      xis->inode_bh,
34184b3f6209STao Ma 					      OCFS2_JOURNAL_ACCESS_WRITE);
34194b3f6209STao Ma 		if (ret) {
34204b3f6209STao Ma 			mlog_errno(ret);
34214b3f6209STao Ma 			goto out;
34224b3f6209STao Ma 		}
34234b3f6209STao Ma 
34246861de97SJeff Layton 		inode_set_ctime_current(inode);
3425*10fc3a18SJeff Layton 		di->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode));
3426*10fc3a18SJeff Layton 		di->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
34274b3f6209STao Ma 		ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
34284b3f6209STao Ma 	}
342985db90e7STao Ma out:
343085db90e7STao Ma 	return ret;
343185db90e7STao Ma }
343285db90e7STao Ma 
3433cf1d6c76STiger Yang /*
34346c3faba4STiger Yang  * This function only called duing creating inode
34356c3faba4STiger Yang  * for init security/acl xattrs of the new inode.
3436008aafafSTiger Yang  * All transanction credits have been reserved in mknod.
34376c3faba4STiger Yang  */
ocfs2_xattr_set_handle(handle_t * handle,struct inode * inode,struct buffer_head * di_bh,int name_index,const char * name,const void * value,size_t value_len,int flags,struct ocfs2_alloc_context * meta_ac,struct ocfs2_alloc_context * data_ac)34386c3faba4STiger Yang int ocfs2_xattr_set_handle(handle_t *handle,
34396c3faba4STiger Yang 			   struct inode *inode,
34406c3faba4STiger Yang 			   struct buffer_head *di_bh,
34416c3faba4STiger Yang 			   int name_index,
34426c3faba4STiger Yang 			   const char *name,
34436c3faba4STiger Yang 			   const void *value,
34446c3faba4STiger Yang 			   size_t value_len,
34456c3faba4STiger Yang 			   int flags,
34466c3faba4STiger Yang 			   struct ocfs2_alloc_context *meta_ac,
34476c3faba4STiger Yang 			   struct ocfs2_alloc_context *data_ac)
34486c3faba4STiger Yang {
34496c3faba4STiger Yang 	struct ocfs2_dinode *di;
34506c3faba4STiger Yang 	int ret;
34516c3faba4STiger Yang 
34526c3faba4STiger Yang 	struct ocfs2_xattr_info xi = {
34536b240ff6SJoel Becker 		.xi_name_index = name_index,
34546b240ff6SJoel Becker 		.xi_name = name,
345518853b95SJoel Becker 		.xi_name_len = strlen(name),
34566b240ff6SJoel Becker 		.xi_value = value,
34576b240ff6SJoel Becker 		.xi_value_len = value_len,
34586c3faba4STiger Yang 	};
34596c3faba4STiger Yang 
34606c3faba4STiger Yang 	struct ocfs2_xattr_search xis = {
34616c3faba4STiger Yang 		.not_found = -ENODATA,
34626c3faba4STiger Yang 	};
34636c3faba4STiger Yang 
34646c3faba4STiger Yang 	struct ocfs2_xattr_search xbs = {
34656c3faba4STiger Yang 		.not_found = -ENODATA,
34666c3faba4STiger Yang 	};
34676c3faba4STiger Yang 
34686c3faba4STiger Yang 	struct ocfs2_xattr_set_ctxt ctxt = {
34696c3faba4STiger Yang 		.handle = handle,
34706c3faba4STiger Yang 		.meta_ac = meta_ac,
34716c3faba4STiger Yang 		.data_ac = data_ac,
34726c3faba4STiger Yang 	};
34736c3faba4STiger Yang 
34746c3faba4STiger Yang 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
34756c3faba4STiger Yang 		return -EOPNOTSUPP;
34766c3faba4STiger Yang 
3477008aafafSTiger Yang 	/*
3478008aafafSTiger Yang 	 * In extreme situation, may need xattr bucket when
3479008aafafSTiger Yang 	 * block size is too small. And we have already reserved
3480008aafafSTiger Yang 	 * the credits for bucket in mknod.
3481008aafafSTiger Yang 	 */
3482008aafafSTiger Yang 	if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3483008aafafSTiger Yang 		xbs.bucket = ocfs2_xattr_bucket_new(inode);
3484008aafafSTiger Yang 		if (!xbs.bucket) {
3485008aafafSTiger Yang 			mlog_errno(-ENOMEM);
3486008aafafSTiger Yang 			return -ENOMEM;
3487008aafafSTiger Yang 		}
3488008aafafSTiger Yang 	}
3489008aafafSTiger Yang 
34906c3faba4STiger Yang 	xis.inode_bh = xbs.inode_bh = di_bh;
34916c3faba4STiger Yang 	di = (struct ocfs2_dinode *)di_bh->b_data;
34926c3faba4STiger Yang 
34936c3faba4STiger Yang 	down_write(&OCFS2_I(inode)->ip_xattr_sem);
34946c3faba4STiger Yang 
34956c3faba4STiger Yang 	ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
34966c3faba4STiger Yang 	if (ret)
34976c3faba4STiger Yang 		goto cleanup;
34986c3faba4STiger Yang 	if (xis.not_found) {
34996c3faba4STiger Yang 		ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
35006c3faba4STiger Yang 		if (ret)
35016c3faba4STiger Yang 			goto cleanup;
35026c3faba4STiger Yang 	}
35036c3faba4STiger Yang 
35046c3faba4STiger Yang 	ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
35056c3faba4STiger Yang 
35066c3faba4STiger Yang cleanup:
35076c3faba4STiger Yang 	up_write(&OCFS2_I(inode)->ip_xattr_sem);
35086c3faba4STiger Yang 	brelse(xbs.xattr_bh);
3509008aafafSTiger Yang 	ocfs2_xattr_bucket_free(xbs.bucket);
35106c3faba4STiger Yang 
35116c3faba4STiger Yang 	return ret;
35126c3faba4STiger Yang }
35136c3faba4STiger Yang 
35146c3faba4STiger Yang /*
3515cf1d6c76STiger Yang  * ocfs2_xattr_set()
3516cf1d6c76STiger Yang  *
3517cf1d6c76STiger Yang  * Set, replace or remove an extended attribute for this inode.
3518cf1d6c76STiger Yang  * value is NULL to remove an existing extended attribute, else either
3519cf1d6c76STiger Yang  * create or replace an extended attribute.
3520cf1d6c76STiger Yang  */
ocfs2_xattr_set(struct inode * inode,int name_index,const char * name,const void * value,size_t value_len,int flags)3521cf1d6c76STiger Yang int ocfs2_xattr_set(struct inode *inode,
3522cf1d6c76STiger Yang 		    int name_index,
3523cf1d6c76STiger Yang 		    const char *name,
3524cf1d6c76STiger Yang 		    const void *value,
3525cf1d6c76STiger Yang 		    size_t value_len,
3526cf1d6c76STiger Yang 		    int flags)
3527cf1d6c76STiger Yang {
3528cf1d6c76STiger Yang 	struct buffer_head *di_bh = NULL;
3529cf1d6c76STiger Yang 	struct ocfs2_dinode *di;
35308818efaaSEric Ren 	int ret, credits, had_lock, ref_meta = 0, ref_credits = 0;
353178f30c31STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
353285db90e7STao Ma 	struct inode *tl_inode = osb->osb_tl_inode;
35336ea437a3SYounger Liu 	struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, NULL, };
3534492a8a33STao Ma 	struct ocfs2_refcount_tree *ref_tree = NULL;
35358818efaaSEric Ren 	struct ocfs2_lock_holder oh;
3536cf1d6c76STiger Yang 
3537cf1d6c76STiger Yang 	struct ocfs2_xattr_info xi = {
35386b240ff6SJoel Becker 		.xi_name_index = name_index,
35396b240ff6SJoel Becker 		.xi_name = name,
354018853b95SJoel Becker 		.xi_name_len = strlen(name),
35416b240ff6SJoel Becker 		.xi_value = value,
35426b240ff6SJoel Becker 		.xi_value_len = value_len,
3543cf1d6c76STiger Yang 	};
3544cf1d6c76STiger Yang 
3545cf1d6c76STiger Yang 	struct ocfs2_xattr_search xis = {
3546cf1d6c76STiger Yang 		.not_found = -ENODATA,
3547cf1d6c76STiger Yang 	};
3548cf1d6c76STiger Yang 
3549cf1d6c76STiger Yang 	struct ocfs2_xattr_search xbs = {
3550cf1d6c76STiger Yang 		.not_found = -ENODATA,
3551cf1d6c76STiger Yang 	};
3552cf1d6c76STiger Yang 
35531119d3c0Spiaojun 	if (!ocfs2_supports_xattr(osb))
35548154da3dSTiger Yang 		return -EOPNOTSUPP;
35558154da3dSTiger Yang 
3556ba937127SJoel Becker 	/*
3557ba937127SJoel Becker 	 * Only xbs will be used on indexed trees.  xis doesn't need a
3558ba937127SJoel Becker 	 * bucket.
3559ba937127SJoel Becker 	 */
3560ba937127SJoel Becker 	xbs.bucket = ocfs2_xattr_bucket_new(inode);
3561ba937127SJoel Becker 	if (!xbs.bucket) {
3562ba937127SJoel Becker 		mlog_errno(-ENOMEM);
3563ba937127SJoel Becker 		return -ENOMEM;
3564ba937127SJoel Becker 	}
3565ba937127SJoel Becker 
35668818efaaSEric Ren 	had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 1, &oh);
35678818efaaSEric Ren 	if (had_lock < 0) {
35688818efaaSEric Ren 		ret = had_lock;
3569cf1d6c76STiger Yang 		mlog_errno(ret);
3570ba937127SJoel Becker 		goto cleanup_nolock;
3571cf1d6c76STiger Yang 	}
3572cf1d6c76STiger Yang 	xis.inode_bh = xbs.inode_bh = di_bh;
3573cf1d6c76STiger Yang 	di = (struct ocfs2_dinode *)di_bh->b_data;
3574cf1d6c76STiger Yang 
3575cf1d6c76STiger Yang 	down_write(&OCFS2_I(inode)->ip_xattr_sem);
3576cf1d6c76STiger Yang 	/*
3577cf1d6c76STiger Yang 	 * Scan inode and external block to find the same name
357825985edcSLucas De Marchi 	 * extended attribute and collect search information.
3579cf1d6c76STiger Yang 	 */
3580cf1d6c76STiger Yang 	ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3581cf1d6c76STiger Yang 	if (ret)
3582cf1d6c76STiger Yang 		goto cleanup;
3583cf1d6c76STiger Yang 	if (xis.not_found) {
3584cf1d6c76STiger Yang 		ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3585cf1d6c76STiger Yang 		if (ret)
3586cf1d6c76STiger Yang 			goto cleanup;
3587cf1d6c76STiger Yang 	}
3588cf1d6c76STiger Yang 
3589cf1d6c76STiger Yang 	if (xis.not_found && xbs.not_found) {
3590cf1d6c76STiger Yang 		ret = -ENODATA;
3591cf1d6c76STiger Yang 		if (flags & XATTR_REPLACE)
3592cf1d6c76STiger Yang 			goto cleanup;
3593cf1d6c76STiger Yang 		ret = 0;
3594cf1d6c76STiger Yang 		if (!value)
3595cf1d6c76STiger Yang 			goto cleanup;
3596cf1d6c76STiger Yang 	} else {
3597cf1d6c76STiger Yang 		ret = -EEXIST;
3598cf1d6c76STiger Yang 		if (flags & XATTR_CREATE)
3599cf1d6c76STiger Yang 			goto cleanup;
3600cf1d6c76STiger Yang 	}
3601cf1d6c76STiger Yang 
360225985edcSLucas De Marchi 	/* Check whether the value is refcounted and do some preparation. */
360384e40080SDarrick J. Wong 	if (ocfs2_is_refcount_inode(inode) &&
3604492a8a33STao Ma 	    (!xis.not_found || !xbs.not_found)) {
3605492a8a33STao Ma 		ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3606492a8a33STao Ma 						   &xis, &xbs, &ref_tree,
3607492a8a33STao Ma 						   &ref_meta, &ref_credits);
3608492a8a33STao Ma 		if (ret) {
3609492a8a33STao Ma 			mlog_errno(ret);
3610492a8a33STao Ma 			goto cleanup;
3611492a8a33STao Ma 		}
3612492a8a33STao Ma 	}
361385db90e7STao Ma 
36145955102cSAl Viro 	inode_lock(tl_inode);
361585db90e7STao Ma 
361685db90e7STao Ma 	if (ocfs2_truncate_log_needs_flush(osb)) {
361785db90e7STao Ma 		ret = __ocfs2_flush_truncate_log(osb);
361885db90e7STao Ma 		if (ret < 0) {
36195955102cSAl Viro 			inode_unlock(tl_inode);
362085db90e7STao Ma 			mlog_errno(ret);
362185db90e7STao Ma 			goto cleanup;
362285db90e7STao Ma 		}
362385db90e7STao Ma 	}
36245955102cSAl Viro 	inode_unlock(tl_inode);
362585db90e7STao Ma 
362685db90e7STao Ma 	ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3627492a8a33STao Ma 					&xbs, &ctxt, ref_meta, &credits);
362878f30c31STao Ma 	if (ret) {
362978f30c31STao Ma 		mlog_errno(ret);
363078f30c31STao Ma 		goto cleanup;
363178f30c31STao Ma 	}
363278f30c31STao Ma 
36334b3f6209STao Ma 	/* we need to update inode's ctime field, so add credit for it. */
36344b3f6209STao Ma 	credits += OCFS2_INODE_UPDATE_CREDITS;
3635492a8a33STao Ma 	ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
363685db90e7STao Ma 	if (IS_ERR(ctxt.handle)) {
363785db90e7STao Ma 		ret = PTR_ERR(ctxt.handle);
363885db90e7STao Ma 		mlog_errno(ret);
36396ea437a3SYounger Liu 		goto out_free_ac;
3640cf1d6c76STiger Yang 	}
364185db90e7STao Ma 
364285db90e7STao Ma 	ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
36436fdb702dSDarrick J. Wong 	ocfs2_update_inode_fsync_trans(ctxt.handle, inode, 0);
364485db90e7STao Ma 
364585db90e7STao Ma 	ocfs2_commit_trans(osb, ctxt.handle);
364685db90e7STao Ma 
36476ea437a3SYounger Liu out_free_ac:
364878f30c31STao Ma 	if (ctxt.data_ac)
364978f30c31STao Ma 		ocfs2_free_alloc_context(ctxt.data_ac);
365078f30c31STao Ma 	if (ctxt.meta_ac)
365178f30c31STao Ma 		ocfs2_free_alloc_context(ctxt.meta_ac);
365278f30c31STao Ma 	if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
365378f30c31STao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
365478f30c31STao Ma 	ocfs2_run_deallocs(osb, &ctxt.dealloc);
36558b2c0dbaSTao Ma 
3656cf1d6c76STiger Yang cleanup:
3657492a8a33STao Ma 	if (ref_tree)
3658492a8a33STao Ma 		ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3659cf1d6c76STiger Yang 	up_write(&OCFS2_I(inode)->ip_xattr_sem);
36608b2c0dbaSTao Ma 	if (!value && !ret) {
36618b2c0dbaSTao Ma 		ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
36628b2c0dbaSTao Ma 		if (ret)
36638b2c0dbaSTao Ma 			mlog_errno(ret);
36648b2c0dbaSTao Ma 	}
36658818efaaSEric Ren 	ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
3666ba937127SJoel Becker cleanup_nolock:
3667cf1d6c76STiger Yang 	brelse(di_bh);
3668cf1d6c76STiger Yang 	brelse(xbs.xattr_bh);
3669ba937127SJoel Becker 	ocfs2_xattr_bucket_free(xbs.bucket);
3670cf1d6c76STiger Yang 
3671cf1d6c76STiger Yang 	return ret;
3672cf1d6c76STiger Yang }
3673cf1d6c76STiger Yang 
36740c044f0bSTao Ma /*
36750c044f0bSTao Ma  * Find the xattr extent rec which may contains name_hash.
36760c044f0bSTao Ma  * e_cpos will be the first name hash of the xattr rec.
36770c044f0bSTao Ma  * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
36780c044f0bSTao Ma  */
ocfs2_xattr_get_rec(struct inode * inode,u32 name_hash,u64 * p_blkno,u32 * e_cpos,u32 * num_clusters,struct ocfs2_extent_list * el)36790c044f0bSTao Ma static int ocfs2_xattr_get_rec(struct inode *inode,
36800c044f0bSTao Ma 			       u32 name_hash,
36810c044f0bSTao Ma 			       u64 *p_blkno,
36820c044f0bSTao Ma 			       u32 *e_cpos,
36830c044f0bSTao Ma 			       u32 *num_clusters,
36840c044f0bSTao Ma 			       struct ocfs2_extent_list *el)
36850c044f0bSTao Ma {
36860c044f0bSTao Ma 	int ret = 0, i;
36870c044f0bSTao Ma 	struct buffer_head *eb_bh = NULL;
36880c044f0bSTao Ma 	struct ocfs2_extent_block *eb;
36890c044f0bSTao Ma 	struct ocfs2_extent_rec *rec = NULL;
36900c044f0bSTao Ma 	u64 e_blkno = 0;
36910c044f0bSTao Ma 
36920c044f0bSTao Ma 	if (el->l_tree_depth) {
3693facdb77fSJoel Becker 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3694facdb77fSJoel Becker 				      &eb_bh);
36950c044f0bSTao Ma 		if (ret) {
36960c044f0bSTao Ma 			mlog_errno(ret);
36970c044f0bSTao Ma 			goto out;
36980c044f0bSTao Ma 		}
36990c044f0bSTao Ma 
37000c044f0bSTao Ma 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
37010c044f0bSTao Ma 		el = &eb->h_list;
37020c044f0bSTao Ma 
37030c044f0bSTao Ma 		if (el->l_tree_depth) {
370417a5b9abSGoldwyn Rodrigues 			ret = ocfs2_error(inode->i_sb,
37057ecef14aSJoe Perches 					  "Inode %lu has non zero tree depth in xattr tree block %llu\n",
37067ecef14aSJoe Perches 					  inode->i_ino,
37070c044f0bSTao Ma 					  (unsigned long long)eb_bh->b_blocknr);
37080c044f0bSTao Ma 			goto out;
37090c044f0bSTao Ma 		}
37100c044f0bSTao Ma 	}
37110c044f0bSTao Ma 
37120c044f0bSTao Ma 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
37130c044f0bSTao Ma 		rec = &el->l_recs[i];
37140c044f0bSTao Ma 
37150c044f0bSTao Ma 		if (le32_to_cpu(rec->e_cpos) <= name_hash) {
37160c044f0bSTao Ma 			e_blkno = le64_to_cpu(rec->e_blkno);
37170c044f0bSTao Ma 			break;
37180c044f0bSTao Ma 		}
37190c044f0bSTao Ma 	}
37200c044f0bSTao Ma 
37210c044f0bSTao Ma 	if (!e_blkno) {
37227ecef14aSJoe Perches 		ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
37237ecef14aSJoe Perches 				  inode->i_ino,
37240c044f0bSTao Ma 				  le32_to_cpu(rec->e_cpos),
37250c044f0bSTao Ma 				  ocfs2_rec_clusters(el, rec));
37260c044f0bSTao Ma 		goto out;
37270c044f0bSTao Ma 	}
37280c044f0bSTao Ma 
37290c044f0bSTao Ma 	*p_blkno = le64_to_cpu(rec->e_blkno);
37300c044f0bSTao Ma 	*num_clusters = le16_to_cpu(rec->e_leaf_clusters);
37310c044f0bSTao Ma 	if (e_cpos)
37320c044f0bSTao Ma 		*e_cpos = le32_to_cpu(rec->e_cpos);
37330c044f0bSTao Ma out:
37340c044f0bSTao Ma 	brelse(eb_bh);
37350c044f0bSTao Ma 	return ret;
37360c044f0bSTao Ma }
37370c044f0bSTao Ma 
37380c044f0bSTao Ma typedef int (xattr_bucket_func)(struct inode *inode,
37390c044f0bSTao Ma 				struct ocfs2_xattr_bucket *bucket,
37400c044f0bSTao Ma 				void *para);
37410c044f0bSTao Ma 
ocfs2_find_xe_in_bucket(struct inode * inode,struct ocfs2_xattr_bucket * bucket,int name_index,const char * name,u32 name_hash,u16 * xe_index,int * found)3742589dc260STao Ma static int ocfs2_find_xe_in_bucket(struct inode *inode,
3743e2356a3fSJoel Becker 				   struct ocfs2_xattr_bucket *bucket,
3744589dc260STao Ma 				   int name_index,
3745589dc260STao Ma 				   const char *name,
3746589dc260STao Ma 				   u32 name_hash,
3747589dc260STao Ma 				   u16 *xe_index,
3748589dc260STao Ma 				   int *found)
3749589dc260STao Ma {
3750589dc260STao Ma 	int i, ret = 0, cmp = 1, block_off, new_offset;
3751e2356a3fSJoel Becker 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3752589dc260STao Ma 	size_t name_len = strlen(name);
3753589dc260STao Ma 	struct ocfs2_xattr_entry *xe = NULL;
3754589dc260STao Ma 	char *xe_name;
3755589dc260STao Ma 
3756589dc260STao Ma 	/*
3757589dc260STao Ma 	 * We don't use binary search in the bucket because there
3758589dc260STao Ma 	 * may be multiple entries with the same name hash.
3759589dc260STao Ma 	 */
3760589dc260STao Ma 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3761589dc260STao Ma 		xe = &xh->xh_entries[i];
3762589dc260STao Ma 
3763589dc260STao Ma 		if (name_hash > le32_to_cpu(xe->xe_name_hash))
3764589dc260STao Ma 			continue;
3765589dc260STao Ma 		else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3766589dc260STao Ma 			break;
3767589dc260STao Ma 
3768589dc260STao Ma 		cmp = name_index - ocfs2_xattr_get_type(xe);
3769589dc260STao Ma 		if (!cmp)
3770589dc260STao Ma 			cmp = name_len - xe->xe_name_len;
3771589dc260STao Ma 		if (cmp)
3772589dc260STao Ma 			continue;
3773589dc260STao Ma 
3774fd68a894STao Ma 		ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3775589dc260STao Ma 							xh,
3776589dc260STao Ma 							i,
3777589dc260STao Ma 							&block_off,
3778589dc260STao Ma 							&new_offset);
3779589dc260STao Ma 		if (ret) {
3780589dc260STao Ma 			mlog_errno(ret);
3781589dc260STao Ma 			break;
3782589dc260STao Ma 		}
3783589dc260STao Ma 
3784970e4936SJoel Becker 
3785e2356a3fSJoel Becker 		xe_name = bucket_block(bucket, block_off) + new_offset;
3786e2356a3fSJoel Becker 		if (!memcmp(name, xe_name, name_len)) {
3787589dc260STao Ma 			*xe_index = i;
3788589dc260STao Ma 			*found = 1;
3789589dc260STao Ma 			ret = 0;
3790589dc260STao Ma 			break;
3791589dc260STao Ma 		}
3792589dc260STao Ma 	}
3793589dc260STao Ma 
3794589dc260STao Ma 	return ret;
3795589dc260STao Ma }
3796589dc260STao Ma 
3797589dc260STao Ma /*
3798589dc260STao Ma  * Find the specified xattr entry in a series of buckets.
3799589dc260STao Ma  * This series start from p_blkno and last for num_clusters.
3800589dc260STao Ma  * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3801589dc260STao Ma  * the num of the valid buckets.
3802589dc260STao Ma  *
3803589dc260STao Ma  * Return the buffer_head this xattr should reside in. And if the xattr's
3804589dc260STao Ma  * hash is in the gap of 2 buckets, return the lower bucket.
3805589dc260STao Ma  */
ocfs2_xattr_bucket_find(struct inode * inode,int name_index,const char * name,u32 name_hash,u64 p_blkno,u32 first_hash,u32 num_clusters,struct ocfs2_xattr_search * xs)3806589dc260STao Ma static int ocfs2_xattr_bucket_find(struct inode *inode,
3807589dc260STao Ma 				   int name_index,
3808589dc260STao Ma 				   const char *name,
3809589dc260STao Ma 				   u32 name_hash,
3810589dc260STao Ma 				   u64 p_blkno,
3811589dc260STao Ma 				   u32 first_hash,
3812589dc260STao Ma 				   u32 num_clusters,
3813589dc260STao Ma 				   struct ocfs2_xattr_search *xs)
3814589dc260STao Ma {
3815589dc260STao Ma 	int ret, found = 0;
3816589dc260STao Ma 	struct ocfs2_xattr_header *xh = NULL;
3817589dc260STao Ma 	struct ocfs2_xattr_entry *xe = NULL;
3818589dc260STao Ma 	u16 index = 0;
3819589dc260STao Ma 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3820589dc260STao Ma 	int low_bucket = 0, bucket, high_bucket;
3821e2356a3fSJoel Becker 	struct ocfs2_xattr_bucket *search;
3822e2356a3fSJoel Becker 	u64 blkno, lower_blkno = 0;
3823589dc260STao Ma 
3824e2356a3fSJoel Becker 	search = ocfs2_xattr_bucket_new(inode);
3825e2356a3fSJoel Becker 	if (!search) {
3826e2356a3fSJoel Becker 		ret = -ENOMEM;
3827e2356a3fSJoel Becker 		mlog_errno(ret);
3828e2356a3fSJoel Becker 		goto out;
3829e2356a3fSJoel Becker 	}
3830e2356a3fSJoel Becker 
3831e2356a3fSJoel Becker 	ret = ocfs2_read_xattr_bucket(search, p_blkno);
3832589dc260STao Ma 	if (ret) {
3833589dc260STao Ma 		mlog_errno(ret);
3834589dc260STao Ma 		goto out;
3835589dc260STao Ma 	}
3836589dc260STao Ma 
3837e2356a3fSJoel Becker 	xh = bucket_xh(search);
3838589dc260STao Ma 	high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3839589dc260STao Ma 	while (low_bucket <= high_bucket) {
3840e2356a3fSJoel Becker 		ocfs2_xattr_bucket_relse(search);
3841e2356a3fSJoel Becker 
3842589dc260STao Ma 		bucket = (low_bucket + high_bucket) / 2;
3843589dc260STao Ma 		blkno = p_blkno + bucket * blk_per_bucket;
3844e2356a3fSJoel Becker 		ret = ocfs2_read_xattr_bucket(search, blkno);
3845589dc260STao Ma 		if (ret) {
3846589dc260STao Ma 			mlog_errno(ret);
3847589dc260STao Ma 			goto out;
3848589dc260STao Ma 		}
3849589dc260STao Ma 
3850e2356a3fSJoel Becker 		xh = bucket_xh(search);
3851589dc260STao Ma 		xe = &xh->xh_entries[0];
3852589dc260STao Ma 		if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3853589dc260STao Ma 			high_bucket = bucket - 1;
3854589dc260STao Ma 			continue;
3855589dc260STao Ma 		}
3856589dc260STao Ma 
3857589dc260STao Ma 		/*
3858589dc260STao Ma 		 * Check whether the hash of the last entry in our
38595a095611STao Ma 		 * bucket is larger than the search one. for an empty
38605a095611STao Ma 		 * bucket, the last one is also the first one.
3861589dc260STao Ma 		 */
38625a095611STao Ma 		if (xh->xh_count)
3863589dc260STao Ma 			xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
38645a095611STao Ma 
3865e2356a3fSJoel Becker 		/* record lower_blkno which may be the insert place. */
3866e2356a3fSJoel Becker 		lower_blkno = blkno;
3867589dc260STao Ma 
3868589dc260STao Ma 		if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3869589dc260STao Ma 			low_bucket = bucket + 1;
3870589dc260STao Ma 			continue;
3871589dc260STao Ma 		}
3872589dc260STao Ma 
3873589dc260STao Ma 		/* the searched xattr should reside in this bucket if exists. */
3874e2356a3fSJoel Becker 		ret = ocfs2_find_xe_in_bucket(inode, search,
3875589dc260STao Ma 					      name_index, name, name_hash,
3876589dc260STao Ma 					      &index, &found);
3877589dc260STao Ma 		if (ret) {
3878589dc260STao Ma 			mlog_errno(ret);
3879589dc260STao Ma 			goto out;
3880589dc260STao Ma 		}
3881589dc260STao Ma 		break;
3882589dc260STao Ma 	}
3883589dc260STao Ma 
3884589dc260STao Ma 	/*
3885589dc260STao Ma 	 * Record the bucket we have found.
3886589dc260STao Ma 	 * When the xattr's hash value is in the gap of 2 buckets, we will
3887589dc260STao Ma 	 * always set it to the previous bucket.
3888589dc260STao Ma 	 */
3889e2356a3fSJoel Becker 	if (!lower_blkno)
3890e2356a3fSJoel Becker 		lower_blkno = p_blkno;
3891e2356a3fSJoel Becker 
3892e2356a3fSJoel Becker 	/* This should be in cache - we just read it during the search */
3893e2356a3fSJoel Becker 	ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3894e2356a3fSJoel Becker 	if (ret) {
3895e2356a3fSJoel Becker 		mlog_errno(ret);
3896e2356a3fSJoel Becker 		goto out;
3897589dc260STao Ma 	}
3898589dc260STao Ma 
3899ba937127SJoel Becker 	xs->header = bucket_xh(xs->bucket);
3900ba937127SJoel Becker 	xs->base = bucket_block(xs->bucket, 0);
3901589dc260STao Ma 	xs->end = xs->base + inode->i_sb->s_blocksize;
3902589dc260STao Ma 
3903589dc260STao Ma 	if (found) {
3904589dc260STao Ma 		xs->here = &xs->header->xh_entries[index];
3905402b4183STao Ma 		trace_ocfs2_xattr_bucket_find(OCFS2_I(inode)->ip_blkno,
3906402b4183STao Ma 			name, name_index, name_hash,
3907402b4183STao Ma 			(unsigned long long)bucket_blkno(xs->bucket),
3908402b4183STao Ma 			index);
3909589dc260STao Ma 	} else
3910589dc260STao Ma 		ret = -ENODATA;
3911589dc260STao Ma 
3912589dc260STao Ma out:
3913e2356a3fSJoel Becker 	ocfs2_xattr_bucket_free(search);
3914589dc260STao Ma 	return ret;
3915589dc260STao Ma }
3916589dc260STao Ma 
ocfs2_xattr_index_block_find(struct inode * inode,struct buffer_head * root_bh,int name_index,const char * name,struct ocfs2_xattr_search * xs)3917589dc260STao Ma static int ocfs2_xattr_index_block_find(struct inode *inode,
3918589dc260STao Ma 					struct buffer_head *root_bh,
3919589dc260STao Ma 					int name_index,
3920589dc260STao Ma 					const char *name,
3921589dc260STao Ma 					struct ocfs2_xattr_search *xs)
3922589dc260STao Ma {
3923589dc260STao Ma 	int ret;
3924589dc260STao Ma 	struct ocfs2_xattr_block *xb =
3925589dc260STao Ma 			(struct ocfs2_xattr_block *)root_bh->b_data;
3926589dc260STao Ma 	struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3927589dc260STao Ma 	struct ocfs2_extent_list *el = &xb_root->xt_list;
3928589dc260STao Ma 	u64 p_blkno = 0;
3929589dc260STao Ma 	u32 first_hash, num_clusters = 0;
39302057e5c6STao Ma 	u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3931589dc260STao Ma 
3932589dc260STao Ma 	if (le16_to_cpu(el->l_next_free_rec) == 0)
3933589dc260STao Ma 		return -ENODATA;
3934589dc260STao Ma 
3935402b4183STao Ma 	trace_ocfs2_xattr_index_block_find(OCFS2_I(inode)->ip_blkno,
3936402b4183STao Ma 					name, name_index, name_hash,
3937402b4183STao Ma 					(unsigned long long)root_bh->b_blocknr,
3938402b4183STao Ma 					-1);
3939589dc260STao Ma 
3940589dc260STao Ma 	ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3941589dc260STao Ma 				  &num_clusters, el);
3942589dc260STao Ma 	if (ret) {
3943589dc260STao Ma 		mlog_errno(ret);
3944589dc260STao Ma 		goto out;
3945589dc260STao Ma 	}
3946589dc260STao Ma 
3947589dc260STao Ma 	BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3948589dc260STao Ma 
3949402b4183STao Ma 	trace_ocfs2_xattr_index_block_find_rec(OCFS2_I(inode)->ip_blkno,
3950402b4183STao Ma 					name, name_index, first_hash,
3951402b4183STao Ma 					(unsigned long long)p_blkno,
3952402b4183STao Ma 					num_clusters);
3953589dc260STao Ma 
3954589dc260STao Ma 	ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3955589dc260STao Ma 				      p_blkno, first_hash, num_clusters, xs);
3956589dc260STao Ma 
3957589dc260STao Ma out:
3958589dc260STao Ma 	return ret;
3959589dc260STao Ma }
3960589dc260STao Ma 
ocfs2_iterate_xattr_buckets(struct inode * inode,u64 blkno,u32 clusters,xattr_bucket_func * func,void * para)39610c044f0bSTao Ma static int ocfs2_iterate_xattr_buckets(struct inode *inode,
39620c044f0bSTao Ma 				       u64 blkno,
39630c044f0bSTao Ma 				       u32 clusters,
39640c044f0bSTao Ma 				       xattr_bucket_func *func,
39650c044f0bSTao Ma 				       void *para)
39660c044f0bSTao Ma {
39676dde41d9SJoel Becker 	int i, ret = 0;
39680c044f0bSTao Ma 	u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
39690c044f0bSTao Ma 	u32 num_buckets = clusters * bpc;
3970ba937127SJoel Becker 	struct ocfs2_xattr_bucket *bucket;
39710c044f0bSTao Ma 
3972ba937127SJoel Becker 	bucket = ocfs2_xattr_bucket_new(inode);
3973ba937127SJoel Becker 	if (!bucket) {
3974ba937127SJoel Becker 		mlog_errno(-ENOMEM);
3975ba937127SJoel Becker 		return -ENOMEM;
3976ba937127SJoel Becker 	}
39770c044f0bSTao Ma 
3978402b4183STao Ma 	trace_ocfs2_iterate_xattr_buckets(
3979402b4183STao Ma 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
3980402b4183STao Ma 		(unsigned long long)blkno, clusters);
39810c044f0bSTao Ma 
3982ba937127SJoel Becker 	for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3983ba937127SJoel Becker 		ret = ocfs2_read_xattr_bucket(bucket, blkno);
39840c044f0bSTao Ma 		if (ret) {
39850c044f0bSTao Ma 			mlog_errno(ret);
3986ba937127SJoel Becker 			break;
39870c044f0bSTao Ma 		}
39880c044f0bSTao Ma 
39890c044f0bSTao Ma 		/*
39900c044f0bSTao Ma 		 * The real bucket num in this series of blocks is stored
39910c044f0bSTao Ma 		 * in the 1st bucket.
39920c044f0bSTao Ma 		 */
39930c044f0bSTao Ma 		if (i == 0)
3994ba937127SJoel Becker 			num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
39950c044f0bSTao Ma 
3996402b4183STao Ma 		trace_ocfs2_iterate_xattr_bucket((unsigned long long)blkno,
3997ba937127SJoel Becker 		     le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
39980c044f0bSTao Ma 		if (func) {
3999ba937127SJoel Becker 			ret = func(inode, bucket, para);
4000a46fa684STao Ma 			if (ret && ret != -ERANGE)
40010c044f0bSTao Ma 				mlog_errno(ret);
4002ba937127SJoel Becker 			/* Fall through to bucket_relse() */
4003ba937127SJoel Becker 		}
4004ba937127SJoel Becker 
4005ba937127SJoel Becker 		ocfs2_xattr_bucket_relse(bucket);
4006ba937127SJoel Becker 		if (ret)
40070c044f0bSTao Ma 			break;
40080c044f0bSTao Ma 	}
40090c044f0bSTao Ma 
4010ba937127SJoel Becker 	ocfs2_xattr_bucket_free(bucket);
40110c044f0bSTao Ma 	return ret;
40120c044f0bSTao Ma }
40130c044f0bSTao Ma 
40140c044f0bSTao Ma struct ocfs2_xattr_tree_list {
40150c044f0bSTao Ma 	char *buffer;
40160c044f0bSTao Ma 	size_t buffer_size;
4017936b8834STao Ma 	size_t result;
40180c044f0bSTao Ma };
40190c044f0bSTao Ma 
ocfs2_xattr_bucket_get_name_value(struct super_block * sb,struct ocfs2_xattr_header * xh,int index,int * block_off,int * new_offset)4020fd68a894STao Ma static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
40210c044f0bSTao Ma 					     struct ocfs2_xattr_header *xh,
40220c044f0bSTao Ma 					     int index,
40230c044f0bSTao Ma 					     int *block_off,
40240c044f0bSTao Ma 					     int *new_offset)
40250c044f0bSTao Ma {
40260c044f0bSTao Ma 	u16 name_offset;
40270c044f0bSTao Ma 
40280c044f0bSTao Ma 	if (index < 0 || index >= le16_to_cpu(xh->xh_count))
40290c044f0bSTao Ma 		return -EINVAL;
40300c044f0bSTao Ma 
40310c044f0bSTao Ma 	name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
40320c044f0bSTao Ma 
4033fd68a894STao Ma 	*block_off = name_offset >> sb->s_blocksize_bits;
4034fd68a894STao Ma 	*new_offset = name_offset % sb->s_blocksize;
40350c044f0bSTao Ma 
40360c044f0bSTao Ma 	return 0;
40370c044f0bSTao Ma }
40380c044f0bSTao Ma 
ocfs2_list_xattr_bucket(struct inode * inode,struct ocfs2_xattr_bucket * bucket,void * para)40390c044f0bSTao Ma static int ocfs2_list_xattr_bucket(struct inode *inode,
40400c044f0bSTao Ma 				   struct ocfs2_xattr_bucket *bucket,
40410c044f0bSTao Ma 				   void *para)
40420c044f0bSTao Ma {
4043936b8834STao Ma 	int ret = 0, type;
40440c044f0bSTao Ma 	struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
40450c044f0bSTao Ma 	int i, block_off, new_offset;
40461046cb11SAndreas Gruenbacher 	const char *name;
40470c044f0bSTao Ma 
40483e632946SJoel Becker 	for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
40493e632946SJoel Becker 		struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4050936b8834STao Ma 		type = ocfs2_xattr_get_type(entry);
40510c044f0bSTao Ma 
4052fd68a894STao Ma 		ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
40533e632946SJoel Becker 							bucket_xh(bucket),
40540c044f0bSTao Ma 							i,
40550c044f0bSTao Ma 							&block_off,
40560c044f0bSTao Ma 							&new_offset);
40570c044f0bSTao Ma 		if (ret)
40580c044f0bSTao Ma 			break;
4059936b8834STao Ma 
406051def39fSJoel Becker 		name = (const char *)bucket_block(bucket, block_off) +
4061936b8834STao Ma 			new_offset;
40621046cb11SAndreas Gruenbacher 		ret = ocfs2_xattr_list_entry(inode->i_sb,
40631046cb11SAndreas Gruenbacher 					     xl->buffer,
4064936b8834STao Ma 					     xl->buffer_size,
4065936b8834STao Ma 					     &xl->result,
40661046cb11SAndreas Gruenbacher 					     type, name,
40670c044f0bSTao Ma 					     entry->xe_name_len);
4068936b8834STao Ma 		if (ret)
4069936b8834STao Ma 			break;
40700c044f0bSTao Ma 	}
40710c044f0bSTao Ma 
40720c044f0bSTao Ma 	return ret;
40730c044f0bSTao Ma }
40740c044f0bSTao Ma 
ocfs2_iterate_xattr_index_block(struct inode * inode,struct buffer_head * blk_bh,xattr_tree_rec_func * rec_func,void * para)407547bca495STao Ma static int ocfs2_iterate_xattr_index_block(struct inode *inode,
407647bca495STao Ma 					   struct buffer_head *blk_bh,
407747bca495STao Ma 					   xattr_tree_rec_func *rec_func,
407847bca495STao Ma 					   void *para)
40790c044f0bSTao Ma {
408047bca495STao Ma 	struct ocfs2_xattr_block *xb =
408147bca495STao Ma 			(struct ocfs2_xattr_block *)blk_bh->b_data;
408247bca495STao Ma 	struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
40830c044f0bSTao Ma 	int ret = 0;
40840c044f0bSTao Ma 	u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
40850c044f0bSTao Ma 	u64 p_blkno = 0;
40860c044f0bSTao Ma 
408747bca495STao Ma 	if (!el->l_next_free_rec || !rec_func)
40880c044f0bSTao Ma 		return 0;
40890c044f0bSTao Ma 
40900c044f0bSTao Ma 	while (name_hash > 0) {
40910c044f0bSTao Ma 		ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
40920c044f0bSTao Ma 					  &e_cpos, &num_clusters, el);
40930c044f0bSTao Ma 		if (ret) {
40940c044f0bSTao Ma 			mlog_errno(ret);
409547bca495STao Ma 			break;
40960c044f0bSTao Ma 		}
40970c044f0bSTao Ma 
409847bca495STao Ma 		ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
409947bca495STao Ma 			       num_clusters, para);
41000c044f0bSTao Ma 		if (ret) {
4101a46fa684STao Ma 			if (ret != -ERANGE)
41020c044f0bSTao Ma 				mlog_errno(ret);
410347bca495STao Ma 			break;
41040c044f0bSTao Ma 		}
41050c044f0bSTao Ma 
41060c044f0bSTao Ma 		if (e_cpos == 0)
41070c044f0bSTao Ma 			break;
41080c044f0bSTao Ma 
41090c044f0bSTao Ma 		name_hash = e_cpos - 1;
41100c044f0bSTao Ma 	}
41110c044f0bSTao Ma 
411247bca495STao Ma 	return ret;
411347bca495STao Ma 
411447bca495STao Ma }
411547bca495STao Ma 
ocfs2_list_xattr_tree_rec(struct inode * inode,struct buffer_head * root_bh,u64 blkno,u32 cpos,u32 len,void * para)411647bca495STao Ma static int ocfs2_list_xattr_tree_rec(struct inode *inode,
411747bca495STao Ma 				     struct buffer_head *root_bh,
411847bca495STao Ma 				     u64 blkno, u32 cpos, u32 len, void *para)
411947bca495STao Ma {
412047bca495STao Ma 	return ocfs2_iterate_xattr_buckets(inode, blkno, len,
412147bca495STao Ma 					   ocfs2_list_xattr_bucket, para);
412247bca495STao Ma }
412347bca495STao Ma 
ocfs2_xattr_tree_list_index_block(struct inode * inode,struct buffer_head * blk_bh,char * buffer,size_t buffer_size)412447bca495STao Ma static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
412547bca495STao Ma 					     struct buffer_head *blk_bh,
412647bca495STao Ma 					     char *buffer,
412747bca495STao Ma 					     size_t buffer_size)
412847bca495STao Ma {
412947bca495STao Ma 	int ret;
413047bca495STao Ma 	struct ocfs2_xattr_tree_list xl = {
413147bca495STao Ma 		.buffer = buffer,
413247bca495STao Ma 		.buffer_size = buffer_size,
413347bca495STao Ma 		.result = 0,
413447bca495STao Ma 	};
413547bca495STao Ma 
413647bca495STao Ma 	ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
413747bca495STao Ma 					      ocfs2_list_xattr_tree_rec, &xl);
413847bca495STao Ma 	if (ret) {
413947bca495STao Ma 		mlog_errno(ret);
414047bca495STao Ma 		goto out;
414147bca495STao Ma 	}
414247bca495STao Ma 
4143936b8834STao Ma 	ret = xl.result;
41440c044f0bSTao Ma out:
41450c044f0bSTao Ma 	return ret;
41460c044f0bSTao Ma }
414701225596STao Ma 
cmp_xe(const void * a,const void * b)414801225596STao Ma static int cmp_xe(const void *a, const void *b)
414901225596STao Ma {
415001225596STao Ma 	const struct ocfs2_xattr_entry *l = a, *r = b;
415101225596STao Ma 	u32 l_hash = le32_to_cpu(l->xe_name_hash);
415201225596STao Ma 	u32 r_hash = le32_to_cpu(r->xe_name_hash);
415301225596STao Ma 
415401225596STao Ma 	if (l_hash > r_hash)
415501225596STao Ma 		return 1;
415601225596STao Ma 	if (l_hash < r_hash)
415701225596STao Ma 		return -1;
415801225596STao Ma 	return 0;
415901225596STao Ma }
416001225596STao Ma 
swap_xe(void * a,void * b,int size)416101225596STao Ma static void swap_xe(void *a, void *b, int size)
416201225596STao Ma {
416301225596STao Ma 	struct ocfs2_xattr_entry *l = a, *r = b, tmp;
416401225596STao Ma 
416501225596STao Ma 	tmp = *l;
416601225596STao Ma 	memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
416701225596STao Ma 	memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
416801225596STao Ma }
416901225596STao Ma 
417001225596STao Ma /*
417101225596STao Ma  * When the ocfs2_xattr_block is filled up, new bucket will be created
417201225596STao Ma  * and all the xattr entries will be moved to the new bucket.
4173178eeac3SJoel Becker  * The header goes at the start of the bucket, and the names+values are
4174178eeac3SJoel Becker  * filled from the end.  This is why *target starts as the last buffer.
417501225596STao Ma  * Note: we need to sort the entries since they are not saved in order
417601225596STao Ma  * in the ocfs2_xattr_block.
417701225596STao Ma  */
ocfs2_cp_xattr_block_to_bucket(struct inode * inode,struct buffer_head * xb_bh,struct ocfs2_xattr_bucket * bucket)417801225596STao Ma static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
417901225596STao Ma 					   struct buffer_head *xb_bh,
4180178eeac3SJoel Becker 					   struct ocfs2_xattr_bucket *bucket)
418101225596STao Ma {
418201225596STao Ma 	int i, blocksize = inode->i_sb->s_blocksize;
4183178eeac3SJoel Becker 	int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
418401225596STao Ma 	u16 offset, size, off_change;
418501225596STao Ma 	struct ocfs2_xattr_entry *xe;
418601225596STao Ma 	struct ocfs2_xattr_block *xb =
418701225596STao Ma 				(struct ocfs2_xattr_block *)xb_bh->b_data;
418801225596STao Ma 	struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4189178eeac3SJoel Becker 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
419001225596STao Ma 	u16 count = le16_to_cpu(xb_xh->xh_count);
4191178eeac3SJoel Becker 	char *src = xb_bh->b_data;
4192178eeac3SJoel Becker 	char *target = bucket_block(bucket, blks - 1);
419301225596STao Ma 
4194402b4183STao Ma 	trace_ocfs2_cp_xattr_block_to_bucket_begin(
419501225596STao Ma 				(unsigned long long)xb_bh->b_blocknr,
4196178eeac3SJoel Becker 				(unsigned long long)bucket_blkno(bucket));
419701225596STao Ma 
4198178eeac3SJoel Becker 	for (i = 0; i < blks; i++)
4199178eeac3SJoel Becker 		memset(bucket_block(bucket, i), 0, blocksize);
4200178eeac3SJoel Becker 
420101225596STao Ma 	/*
420201225596STao Ma 	 * Since the xe_name_offset is based on ocfs2_xattr_header,
420301225596STao Ma 	 * there is a offset change corresponding to the change of
420401225596STao Ma 	 * ocfs2_xattr_header's position.
420501225596STao Ma 	 */
420601225596STao Ma 	off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
420701225596STao Ma 	xe = &xb_xh->xh_entries[count - 1];
420801225596STao Ma 	offset = le16_to_cpu(xe->xe_name_offset) + off_change;
420901225596STao Ma 	size = blocksize - offset;
421001225596STao Ma 
421101225596STao Ma 	/* copy all the names and values. */
421201225596STao Ma 	memcpy(target + offset, src + offset, size);
421301225596STao Ma 
421401225596STao Ma 	/* Init new header now. */
421501225596STao Ma 	xh->xh_count = xb_xh->xh_count;
421601225596STao Ma 	xh->xh_num_buckets = cpu_to_le16(1);
421701225596STao Ma 	xh->xh_name_value_len = cpu_to_le16(size);
421801225596STao Ma 	xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
421901225596STao Ma 
422001225596STao Ma 	/* copy all the entries. */
4221178eeac3SJoel Becker 	target = bucket_block(bucket, 0);
422201225596STao Ma 	offset = offsetof(struct ocfs2_xattr_header, xh_entries);
422301225596STao Ma 	size = count * sizeof(struct ocfs2_xattr_entry);
422401225596STao Ma 	memcpy(target + offset, (char *)xb_xh + offset, size);
422501225596STao Ma 
422601225596STao Ma 	/* Change the xe offset for all the xe because of the move. */
422701225596STao Ma 	off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
422801225596STao Ma 		 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
422901225596STao Ma 	for (i = 0; i < count; i++)
423001225596STao Ma 		le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
423101225596STao Ma 
4232402b4183STao Ma 	trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
423301225596STao Ma 
423401225596STao Ma 	sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
423501225596STao Ma 	     cmp_xe, swap_xe);
423601225596STao Ma }
423701225596STao Ma 
423801225596STao Ma /*
423901225596STao Ma  * After we move xattr from block to index btree, we have to
424001225596STao Ma  * update ocfs2_xattr_search to the new xe and base.
424101225596STao Ma  *
424201225596STao Ma  * When the entry is in xattr block, xattr_bh indicates the storage place.
424301225596STao Ma  * While if the entry is in index b-tree, "bucket" indicates the
424401225596STao Ma  * real place of the xattr.
424501225596STao Ma  */
ocfs2_xattr_update_xattr_search(struct inode * inode,struct ocfs2_xattr_search * xs,struct buffer_head * old_bh)4246178eeac3SJoel Becker static void ocfs2_xattr_update_xattr_search(struct inode *inode,
424701225596STao Ma 					    struct ocfs2_xattr_search *xs,
4248178eeac3SJoel Becker 					    struct buffer_head *old_bh)
424901225596STao Ma {
425001225596STao Ma 	char *buf = old_bh->b_data;
425101225596STao Ma 	struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
425201225596STao Ma 	struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4253178eeac3SJoel Becker 	int i;
425401225596STao Ma 
4255ba937127SJoel Becker 	xs->header = bucket_xh(xs->bucket);
4256178eeac3SJoel Becker 	xs->base = bucket_block(xs->bucket, 0);
425701225596STao Ma 	xs->end = xs->base + inode->i_sb->s_blocksize;
425801225596STao Ma 
4259178eeac3SJoel Becker 	if (xs->not_found)
4260178eeac3SJoel Becker 		return;
426101225596STao Ma 
426201225596STao Ma 	i = xs->here - old_xh->xh_entries;
426301225596STao Ma 	xs->here = &xs->header->xh_entries[i];
426401225596STao Ma }
426501225596STao Ma 
ocfs2_xattr_create_index_block(struct inode * inode,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)426601225596STao Ma static int ocfs2_xattr_create_index_block(struct inode *inode,
426778f30c31STao Ma 					  struct ocfs2_xattr_search *xs,
426878f30c31STao Ma 					  struct ocfs2_xattr_set_ctxt *ctxt)
426901225596STao Ma {
427085db90e7STao Ma 	int ret;
427101225596STao Ma 	u32 bit_off, len;
427201225596STao Ma 	u64 blkno;
427385db90e7STao Ma 	handle_t *handle = ctxt->handle;
427401225596STao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
427501225596STao Ma 	struct buffer_head *xb_bh = xs->xattr_bh;
427601225596STao Ma 	struct ocfs2_xattr_block *xb =
427701225596STao Ma 			(struct ocfs2_xattr_block *)xb_bh->b_data;
427801225596STao Ma 	struct ocfs2_xattr_tree_root *xr;
427901225596STao Ma 	u16 xb_flags = le16_to_cpu(xb->xb_flags);
428001225596STao Ma 
4281402b4183STao Ma 	trace_ocfs2_xattr_create_index_block_begin(
428201225596STao Ma 				(unsigned long long)xb_bh->b_blocknr);
428301225596STao Ma 
428401225596STao Ma 	BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4285178eeac3SJoel Becker 	BUG_ON(!xs->bucket);
428601225596STao Ma 
428701225596STao Ma 	/*
428801225596STao Ma 	 * XXX:
428901225596STao Ma 	 * We can use this lock for now, and maybe move to a dedicated mutex
429001225596STao Ma 	 * if performance becomes a problem later.
429101225596STao Ma 	 */
429201225596STao Ma 	down_write(&oi->ip_alloc_sem);
429301225596STao Ma 
42940cf2f763SJoel Becker 	ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
429501225596STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
429601225596STao Ma 	if (ret) {
429701225596STao Ma 		mlog_errno(ret);
429885db90e7STao Ma 		goto out;
429901225596STao Ma 	}
430001225596STao Ma 
43011ed9b777SJoel Becker 	ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
430278f30c31STao Ma 				     1, 1, &bit_off, &len);
430301225596STao Ma 	if (ret) {
430401225596STao Ma 		mlog_errno(ret);
430585db90e7STao Ma 		goto out;
430601225596STao Ma 	}
430701225596STao Ma 
430801225596STao Ma 	/*
430901225596STao Ma 	 * The bucket may spread in many blocks, and
431001225596STao Ma 	 * we will only touch the 1st block and the last block
431101225596STao Ma 	 * in the whole bucket(one for entry and one for data).
431201225596STao Ma 	 */
431301225596STao Ma 	blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
431401225596STao Ma 
4315402b4183STao Ma 	trace_ocfs2_xattr_create_index_block((unsigned long long)blkno);
431601225596STao Ma 
43179c339255SWengang Wang 	ret = ocfs2_init_xattr_bucket(xs->bucket, blkno, 1);
4318178eeac3SJoel Becker 	if (ret) {
431901225596STao Ma 		mlog_errno(ret);
432085db90e7STao Ma 		goto out;
432101225596STao Ma 	}
432201225596STao Ma 
4323178eeac3SJoel Becker 	ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
432401225596STao Ma 						OCFS2_JOURNAL_ACCESS_CREATE);
432501225596STao Ma 	if (ret) {
432601225596STao Ma 		mlog_errno(ret);
432785db90e7STao Ma 		goto out;
432801225596STao Ma 	}
432901225596STao Ma 
4330178eeac3SJoel Becker 	ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4331178eeac3SJoel Becker 	ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
433201225596STao Ma 
4333178eeac3SJoel Becker 	ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
433401225596STao Ma 
433501225596STao Ma 	/* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
433601225596STao Ma 	memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
433701225596STao Ma 	       offsetof(struct ocfs2_xattr_block, xb_attrs));
433801225596STao Ma 
433901225596STao Ma 	xr = &xb->xb_attrs.xb_root;
434001225596STao Ma 	xr->xt_clusters = cpu_to_le32(1);
434101225596STao Ma 	xr->xt_last_eb_blk = 0;
434201225596STao Ma 	xr->xt_list.l_tree_depth = 0;
434301225596STao Ma 	xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
434401225596STao Ma 	xr->xt_list.l_next_free_rec = cpu_to_le16(1);
434501225596STao Ma 
434601225596STao Ma 	xr->xt_list.l_recs[0].e_cpos = 0;
434701225596STao Ma 	xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
434801225596STao Ma 	xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
434901225596STao Ma 
435001225596STao Ma 	xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
435101225596STao Ma 
435285db90e7STao Ma 	ocfs2_journal_dirty(handle, xb_bh);
435301225596STao Ma 
435485db90e7STao Ma out:
435501225596STao Ma 	up_write(&oi->ip_alloc_sem);
435601225596STao Ma 
435701225596STao Ma 	return ret;
435801225596STao Ma }
435901225596STao Ma 
cmp_xe_offset(const void * a,const void * b)436001225596STao Ma static int cmp_xe_offset(const void *a, const void *b)
436101225596STao Ma {
436201225596STao Ma 	const struct ocfs2_xattr_entry *l = a, *r = b;
436301225596STao Ma 	u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
436401225596STao Ma 	u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
436501225596STao Ma 
436601225596STao Ma 	if (l_name_offset < r_name_offset)
436701225596STao Ma 		return 1;
436801225596STao Ma 	if (l_name_offset > r_name_offset)
436901225596STao Ma 		return -1;
437001225596STao Ma 	return 0;
437101225596STao Ma }
437201225596STao Ma 
437301225596STao Ma /*
437401225596STao Ma  * defrag a xattr bucket if we find that the bucket has some
437501225596STao Ma  * holes beteen name/value pairs.
437601225596STao Ma  * We will move all the name/value pairs to the end of the bucket
437701225596STao Ma  * so that we can spare some space for insertion.
437801225596STao Ma  */
ocfs2_defrag_xattr_bucket(struct inode * inode,handle_t * handle,struct ocfs2_xattr_bucket * bucket)437901225596STao Ma static int ocfs2_defrag_xattr_bucket(struct inode *inode,
438085db90e7STao Ma 				     handle_t *handle,
438101225596STao Ma 				     struct ocfs2_xattr_bucket *bucket)
438201225596STao Ma {
438301225596STao Ma 	int ret, i;
4384199799a3SJoel Becker 	size_t end, offset, len;
438501225596STao Ma 	struct ocfs2_xattr_header *xh;
438601225596STao Ma 	char *entries, *buf, *bucket_buf = NULL;
43879c7759aaSJoel Becker 	u64 blkno = bucket_blkno(bucket);
438801225596STao Ma 	u16 xh_free_start;
438901225596STao Ma 	size_t blocksize = inode->i_sb->s_blocksize;
439001225596STao Ma 	struct ocfs2_xattr_entry *xe;
439101225596STao Ma 
439201225596STao Ma 	/*
439301225596STao Ma 	 * In order to make the operation more efficient and generic,
439401225596STao Ma 	 * we copy all the blocks into a contiguous memory and do the
439501225596STao Ma 	 * defragment there, so if anything is error, we will not touch
439601225596STao Ma 	 * the real block.
439701225596STao Ma 	 */
439801225596STao Ma 	bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
439901225596STao Ma 	if (!bucket_buf) {
440001225596STao Ma 		ret = -EIO;
440101225596STao Ma 		goto out;
440201225596STao Ma 	}
440301225596STao Ma 
4404161d6f30SJoel Becker 	buf = bucket_buf;
44051c32a2fdSTao Ma 	for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
44061c32a2fdSTao Ma 		memcpy(buf, bucket_block(bucket, i), blocksize);
4407161d6f30SJoel Becker 
44081c32a2fdSTao Ma 	ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
440901225596STao Ma 						OCFS2_JOURNAL_ACCESS_WRITE);
441001225596STao Ma 	if (ret < 0) {
441101225596STao Ma 		mlog_errno(ret);
441285db90e7STao Ma 		goto out;
441301225596STao Ma 	}
441401225596STao Ma 
441501225596STao Ma 	xh = (struct ocfs2_xattr_header *)bucket_buf;
441601225596STao Ma 	entries = (char *)xh->xh_entries;
441701225596STao Ma 	xh_free_start = le16_to_cpu(xh->xh_free_start);
441801225596STao Ma 
4419402b4183STao Ma 	trace_ocfs2_defrag_xattr_bucket(
4420de29c085SMark Fasheh 	     (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4421de29c085SMark Fasheh 	     xh_free_start, le16_to_cpu(xh->xh_name_value_len));
442201225596STao Ma 
442301225596STao Ma 	/*
442401225596STao Ma 	 * sort all the entries by their offset.
442501225596STao Ma 	 * the largest will be the first, so that we can
442601225596STao Ma 	 * move them to the end one by one.
442701225596STao Ma 	 */
442801225596STao Ma 	sort(entries, le16_to_cpu(xh->xh_count),
442901225596STao Ma 	     sizeof(struct ocfs2_xattr_entry),
443001225596STao Ma 	     cmp_xe_offset, swap_xe);
443101225596STao Ma 
443201225596STao Ma 	/* Move all name/values to the end of the bucket. */
443301225596STao Ma 	xe = xh->xh_entries;
443401225596STao Ma 	end = OCFS2_XATTR_BUCKET_SIZE;
443501225596STao Ma 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
443601225596STao Ma 		offset = le16_to_cpu(xe->xe_name_offset);
4437199799a3SJoel Becker 		len = namevalue_size_xe(xe);
443801225596STao Ma 
443901225596STao Ma 		/*
444001225596STao Ma 		 * We must make sure that the name/value pair
444101225596STao Ma 		 * exist in the same block. So adjust end to
444201225596STao Ma 		 * the previous block end if needed.
444301225596STao Ma 		 */
444401225596STao Ma 		if (((end - len) / blocksize !=
444501225596STao Ma 			(end - 1) / blocksize))
444601225596STao Ma 			end = end - end % blocksize;
444701225596STao Ma 
444801225596STao Ma 		if (end > offset + len) {
444901225596STao Ma 			memmove(bucket_buf + end - len,
445001225596STao Ma 				bucket_buf + offset, len);
445101225596STao Ma 			xe->xe_name_offset = cpu_to_le16(end - len);
445201225596STao Ma 		}
445301225596STao Ma 
445401225596STao Ma 		mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
445501225596STao Ma 				"bucket %llu\n", (unsigned long long)blkno);
445601225596STao Ma 
445701225596STao Ma 		end -= len;
445801225596STao Ma 	}
445901225596STao Ma 
446001225596STao Ma 	mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
446101225596STao Ma 			"bucket %llu\n", (unsigned long long)blkno);
446201225596STao Ma 
446301225596STao Ma 	if (xh_free_start == end)
446485db90e7STao Ma 		goto out;
446501225596STao Ma 
446601225596STao Ma 	memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
446701225596STao Ma 	xh->xh_free_start = cpu_to_le16(end);
446801225596STao Ma 
446901225596STao Ma 	/* sort the entries by their name_hash. */
447001225596STao Ma 	sort(entries, le16_to_cpu(xh->xh_count),
447101225596STao Ma 	     sizeof(struct ocfs2_xattr_entry),
447201225596STao Ma 	     cmp_xe, swap_xe);
447301225596STao Ma 
447401225596STao Ma 	buf = bucket_buf;
44751c32a2fdSTao Ma 	for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
44761c32a2fdSTao Ma 		memcpy(bucket_block(bucket, i), buf, blocksize);
44771c32a2fdSTao Ma 	ocfs2_xattr_bucket_journal_dirty(handle, bucket);
447801225596STao Ma 
447901225596STao Ma out:
448001225596STao Ma 	kfree(bucket_buf);
448101225596STao Ma 	return ret;
448201225596STao Ma }
448301225596STao Ma 
448401225596STao Ma /*
4485b5c03e74SJoel Becker  * prev_blkno points to the start of an existing extent.  new_blkno
4486b5c03e74SJoel Becker  * points to a newly allocated extent.  Because we know each of our
4487b5c03e74SJoel Becker  * clusters contains more than bucket, we can easily split one cluster
4488b5c03e74SJoel Becker  * at a bucket boundary.  So we take the last cluster of the existing
4489b5c03e74SJoel Becker  * extent and split it down the middle.  We move the last half of the
4490b5c03e74SJoel Becker  * buckets in the last cluster of the existing extent over to the new
4491b5c03e74SJoel Becker  * extent.
449201225596STao Ma  *
4493b5c03e74SJoel Becker  * first_bh is the buffer at prev_blkno so we can update the existing
4494b5c03e74SJoel Becker  * extent's bucket count.  header_bh is the bucket were we were hoping
4495b5c03e74SJoel Becker  * to insert our xattr.  If the bucket move places the target in the new
4496b5c03e74SJoel Becker  * extent, we'll update first_bh and header_bh after modifying the old
4497b5c03e74SJoel Becker  * extent.
4498b5c03e74SJoel Becker  *
4499b5c03e74SJoel Becker  * first_hash will be set as the 1st xe's name_hash in the new extent.
450001225596STao Ma  */
ocfs2_mv_xattr_bucket_cross_cluster(struct inode * inode,handle_t * handle,struct ocfs2_xattr_bucket * first,struct ocfs2_xattr_bucket * target,u64 new_blkno,u32 num_clusters,u32 * first_hash)450101225596STao Ma static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
450201225596STao Ma 					       handle_t *handle,
450341cb8148SJoel Becker 					       struct ocfs2_xattr_bucket *first,
450441cb8148SJoel Becker 					       struct ocfs2_xattr_bucket *target,
450501225596STao Ma 					       u64 new_blkno,
450601225596STao Ma 					       u32 num_clusters,
450701225596STao Ma 					       u32 *first_hash)
450801225596STao Ma {
4509c58b6032SJoel Becker 	int ret;
451041cb8148SJoel Becker 	struct super_block *sb = inode->i_sb;
451141cb8148SJoel Becker 	int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
451241cb8148SJoel Becker 	int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4513b5c03e74SJoel Becker 	int to_move = num_buckets / 2;
4514c58b6032SJoel Becker 	u64 src_blkno;
451541cb8148SJoel Becker 	u64 last_cluster_blkno = bucket_blkno(first) +
451641cb8148SJoel Becker 		((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
451701225596STao Ma 
451841cb8148SJoel Becker 	BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
451941cb8148SJoel Becker 	BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
452001225596STao Ma 
4521402b4183STao Ma 	trace_ocfs2_mv_xattr_bucket_cross_cluster(
4522402b4183STao Ma 				(unsigned long long)last_cluster_blkno,
4523402b4183STao Ma 				(unsigned long long)new_blkno);
452401225596STao Ma 
452541cb8148SJoel Becker 	ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4526c58b6032SJoel Becker 				     last_cluster_blkno, new_blkno,
4527c58b6032SJoel Becker 				     to_move, first_hash);
4528b5c03e74SJoel Becker 	if (ret) {
4529b5c03e74SJoel Becker 		mlog_errno(ret);
4530b5c03e74SJoel Becker 		goto out;
4531b5c03e74SJoel Becker 	}
4532b5c03e74SJoel Becker 
4533c58b6032SJoel Becker 	/* This is the first bucket that got moved */
4534c58b6032SJoel Becker 	src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4535c58b6032SJoel Becker 
453601225596STao Ma 	/*
4537c58b6032SJoel Becker 	 * If the target bucket was part of the moved buckets, we need to
453841cb8148SJoel Becker 	 * update first and target.
453901225596STao Ma 	 */
454041cb8148SJoel Becker 	if (bucket_blkno(target) >= src_blkno) {
4541c58b6032SJoel Becker 		/* Find the block for the new target bucket */
4542c58b6032SJoel Becker 		src_blkno = new_blkno +
454341cb8148SJoel Becker 			(bucket_blkno(target) - src_blkno);
454441cb8148SJoel Becker 
454541cb8148SJoel Becker 		ocfs2_xattr_bucket_relse(first);
454641cb8148SJoel Becker 		ocfs2_xattr_bucket_relse(target);
4547b5c03e74SJoel Becker 
4548b5c03e74SJoel Becker 		/*
4549c58b6032SJoel Becker 		 * These shouldn't fail - the buffers are in the
4550c58b6032SJoel Becker 		 * journal from ocfs2_cp_xattr_bucket().
4551b5c03e74SJoel Becker 		 */
455241cb8148SJoel Becker 		ret = ocfs2_read_xattr_bucket(first, new_blkno);
4553b5c03e74SJoel Becker 		if (ret) {
4554b5c03e74SJoel Becker 			mlog_errno(ret);
4555b5c03e74SJoel Becker 			goto out;
4556b5c03e74SJoel Becker 		}
455741cb8148SJoel Becker 		ret = ocfs2_read_xattr_bucket(target, src_blkno);
455841cb8148SJoel Becker 		if (ret)
4559b5c03e74SJoel Becker 			mlog_errno(ret);
456001225596STao Ma 
456101225596STao Ma 	}
456201225596STao Ma 
456301225596STao Ma out:
456401225596STao Ma 	return ret;
456501225596STao Ma }
456601225596STao Ma 
456701225596STao Ma /*
456880bcaf34STao Ma  * Find the suitable pos when we divide a bucket into 2.
456980bcaf34STao Ma  * We have to make sure the xattrs with the same hash value exist
457080bcaf34STao Ma  * in the same bucket.
457180bcaf34STao Ma  *
457280bcaf34STao Ma  * If this ocfs2_xattr_header covers more than one hash value, find a
457380bcaf34STao Ma  * place where the hash value changes.  Try to find the most even split.
457480bcaf34STao Ma  * The most common case is that all entries have different hash values,
457580bcaf34STao Ma  * and the first check we make will find a place to split.
457601225596STao Ma  */
ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header * xh)457780bcaf34STao Ma static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
457880bcaf34STao Ma {
457980bcaf34STao Ma 	struct ocfs2_xattr_entry *entries = xh->xh_entries;
458080bcaf34STao Ma 	int count = le16_to_cpu(xh->xh_count);
458180bcaf34STao Ma 	int delta, middle = count / 2;
458280bcaf34STao Ma 
458380bcaf34STao Ma 	/*
458480bcaf34STao Ma 	 * We start at the middle.  Each step gets farther away in both
458580bcaf34STao Ma 	 * directions.  We therefore hit the change in hash value
458680bcaf34STao Ma 	 * nearest to the middle.  Note that this loop does not execute for
458780bcaf34STao Ma 	 * count < 2.
458880bcaf34STao Ma 	 */
458980bcaf34STao Ma 	for (delta = 0; delta < middle; delta++) {
459080bcaf34STao Ma 		/* Let's check delta earlier than middle */
459180bcaf34STao Ma 		if (cmp_xe(&entries[middle - delta - 1],
459280bcaf34STao Ma 			   &entries[middle - delta]))
459380bcaf34STao Ma 			return middle - delta;
459480bcaf34STao Ma 
459580bcaf34STao Ma 		/* For even counts, don't walk off the end */
459680bcaf34STao Ma 		if ((middle + delta + 1) == count)
459780bcaf34STao Ma 			continue;
459880bcaf34STao Ma 
459980bcaf34STao Ma 		/* Now try delta past middle */
460080bcaf34STao Ma 		if (cmp_xe(&entries[middle + delta],
460180bcaf34STao Ma 			   &entries[middle + delta + 1]))
460280bcaf34STao Ma 			return middle + delta + 1;
460380bcaf34STao Ma 	}
460480bcaf34STao Ma 
460580bcaf34STao Ma 	/* Every entry had the same hash */
460680bcaf34STao Ma 	return count;
460780bcaf34STao Ma }
460880bcaf34STao Ma 
460980bcaf34STao Ma /*
461080bcaf34STao Ma  * Move some xattrs in old bucket(blk) to new bucket(new_blk).
461180bcaf34STao Ma  * first_hash will record the 1st hash of the new bucket.
461280bcaf34STao Ma  *
461380bcaf34STao Ma  * Normally half of the xattrs will be moved.  But we have to make
461480bcaf34STao Ma  * sure that the xattrs with the same hash value are stored in the
461580bcaf34STao Ma  * same bucket. If all the xattrs in this bucket have the same hash
461680bcaf34STao Ma  * value, the new bucket will be initialized as an empty one and the
461780bcaf34STao Ma  * first_hash will be initialized as (hash_value+1).
461880bcaf34STao Ma  */
ocfs2_divide_xattr_bucket(struct inode * inode,handle_t * handle,u64 blk,u64 new_blk,u32 * first_hash,int new_bucket_head)461980bcaf34STao Ma static int ocfs2_divide_xattr_bucket(struct inode *inode,
462001225596STao Ma 				    handle_t *handle,
462101225596STao Ma 				    u64 blk,
462201225596STao Ma 				    u64 new_blk,
462301225596STao Ma 				    u32 *first_hash,
462401225596STao Ma 				    int new_bucket_head)
462501225596STao Ma {
462601225596STao Ma 	int ret, i;
4627199799a3SJoel Becker 	int count, start, len, name_value_len = 0, name_offset = 0;
4628ba937127SJoel Becker 	struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
462901225596STao Ma 	struct ocfs2_xattr_header *xh;
463001225596STao Ma 	struct ocfs2_xattr_entry *xe;
463101225596STao Ma 	int blocksize = inode->i_sb->s_blocksize;
463201225596STao Ma 
4633402b4183STao Ma 	trace_ocfs2_divide_xattr_bucket_begin((unsigned long long)blk,
4634402b4183STao Ma 					      (unsigned long long)new_blk);
463501225596STao Ma 
4636ba937127SJoel Becker 	s_bucket = ocfs2_xattr_bucket_new(inode);
4637ba937127SJoel Becker 	t_bucket = ocfs2_xattr_bucket_new(inode);
4638ba937127SJoel Becker 	if (!s_bucket || !t_bucket) {
4639ba937127SJoel Becker 		ret = -ENOMEM;
4640ba937127SJoel Becker 		mlog_errno(ret);
4641ba937127SJoel Becker 		goto out;
4642ba937127SJoel Becker 	}
464301225596STao Ma 
4644ba937127SJoel Becker 	ret = ocfs2_read_xattr_bucket(s_bucket, blk);
464501225596STao Ma 	if (ret) {
464601225596STao Ma 		mlog_errno(ret);
464701225596STao Ma 		goto out;
464801225596STao Ma 	}
464901225596STao Ma 
4650ba937127SJoel Becker 	ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
465101225596STao Ma 						OCFS2_JOURNAL_ACCESS_WRITE);
465201225596STao Ma 	if (ret) {
465301225596STao Ma 		mlog_errno(ret);
465401225596STao Ma 		goto out;
465501225596STao Ma 	}
465601225596STao Ma 
4657784b816aSJoel Becker 	/*
4658784b816aSJoel Becker 	 * Even if !new_bucket_head, we're overwriting t_bucket.  Thus,
4659784b816aSJoel Becker 	 * there's no need to read it.
4660784b816aSJoel Becker 	 */
46619c339255SWengang Wang 	ret = ocfs2_init_xattr_bucket(t_bucket, new_blk, new_bucket_head);
466201225596STao Ma 	if (ret) {
466301225596STao Ma 		mlog_errno(ret);
466401225596STao Ma 		goto out;
466501225596STao Ma 	}
466601225596STao Ma 
46672b656c1dSJoel Becker 	/*
46682b656c1dSJoel Becker 	 * Hey, if we're overwriting t_bucket, what difference does
46692b656c1dSJoel Becker 	 * ACCESS_CREATE vs ACCESS_WRITE make?  See the comment in the
46702b656c1dSJoel Becker 	 * same part of ocfs2_cp_xattr_bucket().
46712b656c1dSJoel Becker 	 */
4672ba937127SJoel Becker 	ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4673eb6ff239SJoel Becker 						new_bucket_head ?
4674eb6ff239SJoel Becker 						OCFS2_JOURNAL_ACCESS_CREATE :
4675eb6ff239SJoel Becker 						OCFS2_JOURNAL_ACCESS_WRITE);
467601225596STao Ma 	if (ret) {
467701225596STao Ma 		mlog_errno(ret);
467801225596STao Ma 		goto out;
467901225596STao Ma 	}
468001225596STao Ma 
4681ba937127SJoel Becker 	xh = bucket_xh(s_bucket);
468280bcaf34STao Ma 	count = le16_to_cpu(xh->xh_count);
468380bcaf34STao Ma 	start = ocfs2_xattr_find_divide_pos(xh);
468480bcaf34STao Ma 
468580bcaf34STao Ma 	if (start == count) {
468680bcaf34STao Ma 		xe = &xh->xh_entries[start-1];
468780bcaf34STao Ma 
468880bcaf34STao Ma 		/*
468980bcaf34STao Ma 		 * initialized a new empty bucket here.
469080bcaf34STao Ma 		 * The hash value is set as one larger than
469180bcaf34STao Ma 		 * that of the last entry in the previous bucket.
469280bcaf34STao Ma 		 */
4693ba937127SJoel Becker 		for (i = 0; i < t_bucket->bu_blocks; i++)
4694ba937127SJoel Becker 			memset(bucket_block(t_bucket, i), 0, blocksize);
469580bcaf34STao Ma 
4696ba937127SJoel Becker 		xh = bucket_xh(t_bucket);
469780bcaf34STao Ma 		xh->xh_free_start = cpu_to_le16(blocksize);
469880bcaf34STao Ma 		xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
469980bcaf34STao Ma 		le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
470080bcaf34STao Ma 
470180bcaf34STao Ma 		goto set_num_buckets;
470280bcaf34STao Ma 	}
470380bcaf34STao Ma 
470401225596STao Ma 	/* copy the whole bucket to the new first. */
4705ba937127SJoel Becker 	ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
470601225596STao Ma 
470701225596STao Ma 	/* update the new bucket. */
4708ba937127SJoel Becker 	xh = bucket_xh(t_bucket);
470901225596STao Ma 
471001225596STao Ma 	/*
471101225596STao Ma 	 * Calculate the total name/value len and xh_free_start for
471201225596STao Ma 	 * the old bucket first.
471301225596STao Ma 	 */
471401225596STao Ma 	name_offset = OCFS2_XATTR_BUCKET_SIZE;
471501225596STao Ma 	name_value_len = 0;
471601225596STao Ma 	for (i = 0; i < start; i++) {
471701225596STao Ma 		xe = &xh->xh_entries[i];
4718199799a3SJoel Becker 		name_value_len += namevalue_size_xe(xe);
471901225596STao Ma 		if (le16_to_cpu(xe->xe_name_offset) < name_offset)
472001225596STao Ma 			name_offset = le16_to_cpu(xe->xe_name_offset);
472101225596STao Ma 	}
472201225596STao Ma 
472301225596STao Ma 	/*
472401225596STao Ma 	 * Now begin the modification to the new bucket.
472501225596STao Ma 	 *
472601225596STao Ma 	 * In the new bucket, We just move the xattr entry to the beginning
472701225596STao Ma 	 * and don't touch the name/value. So there will be some holes in the
472801225596STao Ma 	 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
472901225596STao Ma 	 * called.
473001225596STao Ma 	 */
473101225596STao Ma 	xe = &xh->xh_entries[start];
473201225596STao Ma 	len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4733402b4183STao Ma 	trace_ocfs2_divide_xattr_bucket_move(len,
4734ff1ec20eSMark Fasheh 			(int)((char *)xe - (char *)xh),
4735ff1ec20eSMark Fasheh 			(int)((char *)xh->xh_entries - (char *)xh));
473601225596STao Ma 	memmove((char *)xh->xh_entries, (char *)xe, len);
473701225596STao Ma 	xe = &xh->xh_entries[count - start];
473801225596STao Ma 	len = sizeof(struct ocfs2_xattr_entry) * start;
473901225596STao Ma 	memset((char *)xe, 0, len);
474001225596STao Ma 
474101225596STao Ma 	le16_add_cpu(&xh->xh_count, -start);
474201225596STao Ma 	le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
474301225596STao Ma 
474401225596STao Ma 	/* Calculate xh_free_start for the new bucket. */
474501225596STao Ma 	xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
474601225596STao Ma 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
474701225596STao Ma 		xe = &xh->xh_entries[i];
474801225596STao Ma 		if (le16_to_cpu(xe->xe_name_offset) <
474901225596STao Ma 		    le16_to_cpu(xh->xh_free_start))
475001225596STao Ma 			xh->xh_free_start = xe->xe_name_offset;
475101225596STao Ma 	}
475201225596STao Ma 
475380bcaf34STao Ma set_num_buckets:
475401225596STao Ma 	/* set xh->xh_num_buckets for the new xh. */
475501225596STao Ma 	if (new_bucket_head)
475601225596STao Ma 		xh->xh_num_buckets = cpu_to_le16(1);
475701225596STao Ma 	else
475801225596STao Ma 		xh->xh_num_buckets = 0;
475901225596STao Ma 
4760ba937127SJoel Becker 	ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
476101225596STao Ma 
476201225596STao Ma 	/* store the first_hash of the new bucket. */
476301225596STao Ma 	if (first_hash)
476401225596STao Ma 		*first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
476501225596STao Ma 
476601225596STao Ma 	/*
476780bcaf34STao Ma 	 * Now only update the 1st block of the old bucket.  If we
476880bcaf34STao Ma 	 * just added a new empty bucket, there is no need to modify
476980bcaf34STao Ma 	 * it.
477001225596STao Ma 	 */
477180bcaf34STao Ma 	if (start == count)
477280bcaf34STao Ma 		goto out;
477380bcaf34STao Ma 
4774ba937127SJoel Becker 	xh = bucket_xh(s_bucket);
477501225596STao Ma 	memset(&xh->xh_entries[start], 0,
477601225596STao Ma 	       sizeof(struct ocfs2_xattr_entry) * (count - start));
477701225596STao Ma 	xh->xh_count = cpu_to_le16(start);
477801225596STao Ma 	xh->xh_free_start = cpu_to_le16(name_offset);
477901225596STao Ma 	xh->xh_name_value_len = cpu_to_le16(name_value_len);
478001225596STao Ma 
4781ba937127SJoel Becker 	ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
478201225596STao Ma 
478301225596STao Ma out:
4784ba937127SJoel Becker 	ocfs2_xattr_bucket_free(s_bucket);
4785ba937127SJoel Becker 	ocfs2_xattr_bucket_free(t_bucket);
478601225596STao Ma 
478701225596STao Ma 	return ret;
478801225596STao Ma }
478901225596STao Ma 
479001225596STao Ma /*
479101225596STao Ma  * Copy xattr from one bucket to another bucket.
479201225596STao Ma  *
479301225596STao Ma  * The caller must make sure that the journal transaction
479401225596STao Ma  * has enough space for journaling.
479501225596STao Ma  */
ocfs2_cp_xattr_bucket(struct inode * inode,handle_t * handle,u64 s_blkno,u64 t_blkno,int t_is_new)479601225596STao Ma static int ocfs2_cp_xattr_bucket(struct inode *inode,
479701225596STao Ma 				 handle_t *handle,
479801225596STao Ma 				 u64 s_blkno,
479901225596STao Ma 				 u64 t_blkno,
480001225596STao Ma 				 int t_is_new)
480101225596STao Ma {
48024980c6daSJoel Becker 	int ret;
4803ba937127SJoel Becker 	struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
480401225596STao Ma 
480501225596STao Ma 	BUG_ON(s_blkno == t_blkno);
480601225596STao Ma 
4807402b4183STao Ma 	trace_ocfs2_cp_xattr_bucket((unsigned long long)s_blkno,
4808402b4183STao Ma 				    (unsigned long long)t_blkno,
4809de29c085SMark Fasheh 				    t_is_new);
481001225596STao Ma 
4811ba937127SJoel Becker 	s_bucket = ocfs2_xattr_bucket_new(inode);
4812ba937127SJoel Becker 	t_bucket = ocfs2_xattr_bucket_new(inode);
4813ba937127SJoel Becker 	if (!s_bucket || !t_bucket) {
4814ba937127SJoel Becker 		ret = -ENOMEM;
4815ba937127SJoel Becker 		mlog_errno(ret);
4816ba937127SJoel Becker 		goto out;
4817ba937127SJoel Becker 	}
481801225596STao Ma 
4819ba937127SJoel Becker 	ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
482001225596STao Ma 	if (ret)
482101225596STao Ma 		goto out;
482201225596STao Ma 
4823784b816aSJoel Becker 	/*
4824784b816aSJoel Becker 	 * Even if !t_is_new, we're overwriting t_bucket.  Thus,
4825784b816aSJoel Becker 	 * there's no need to read it.
4826784b816aSJoel Becker 	 */
48279c339255SWengang Wang 	ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno, t_is_new);
482801225596STao Ma 	if (ret)
482901225596STao Ma 		goto out;
483001225596STao Ma 
48312b656c1dSJoel Becker 	/*
48322b656c1dSJoel Becker 	 * Hey, if we're overwriting t_bucket, what difference does
48332b656c1dSJoel Becker 	 * ACCESS_CREATE vs ACCESS_WRITE make?  Well, if we allocated a new
4834874d65afSJoel Becker 	 * cluster to fill, we came here from
4835874d65afSJoel Becker 	 * ocfs2_mv_xattr_buckets(), and it is really new -
4836874d65afSJoel Becker 	 * ACCESS_CREATE is required.  But we also might have moved data
4837874d65afSJoel Becker 	 * out of t_bucket before extending back into it.
4838874d65afSJoel Becker 	 * ocfs2_add_new_xattr_bucket() can do this - its call to
4839874d65afSJoel Becker 	 * ocfs2_add_new_xattr_cluster() may have created a new extent
48402b656c1dSJoel Becker 	 * and copied out the end of the old extent.  Then it re-extends
48412b656c1dSJoel Becker 	 * the old extent back to create space for new xattrs.  That's
48422b656c1dSJoel Becker 	 * how we get here, and the bucket isn't really new.
48432b656c1dSJoel Becker 	 */
4844ba937127SJoel Becker 	ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4845eb6ff239SJoel Becker 						t_is_new ?
4846eb6ff239SJoel Becker 						OCFS2_JOURNAL_ACCESS_CREATE :
484701225596STao Ma 						OCFS2_JOURNAL_ACCESS_WRITE);
484801225596STao Ma 	if (ret)
484901225596STao Ma 		goto out;
485001225596STao Ma 
4851ba937127SJoel Becker 	ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4852ba937127SJoel Becker 	ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
485301225596STao Ma 
485401225596STao Ma out:
4855ba937127SJoel Becker 	ocfs2_xattr_bucket_free(t_bucket);
4856ba937127SJoel Becker 	ocfs2_xattr_bucket_free(s_bucket);
485701225596STao Ma 
485801225596STao Ma 	return ret;
485901225596STao Ma }
486001225596STao Ma 
486101225596STao Ma /*
4862874d65afSJoel Becker  * src_blk points to the start of an existing extent.  last_blk points to
4863874d65afSJoel Becker  * last cluster in that extent.  to_blk points to a newly allocated
486454ecb6b6SJoel Becker  * extent.  We copy the buckets from the cluster at last_blk to the new
486554ecb6b6SJoel Becker  * extent.  If start_bucket is non-zero, we skip that many buckets before
486654ecb6b6SJoel Becker  * we start copying.  The new extent's xh_num_buckets gets set to the
486754ecb6b6SJoel Becker  * number of buckets we copied.  The old extent's xh_num_buckets shrinks
486854ecb6b6SJoel Becker  * by the same amount.
486901225596STao Ma  */
ocfs2_mv_xattr_buckets(struct inode * inode,handle_t * handle,u64 src_blk,u64 last_blk,u64 to_blk,unsigned int start_bucket,u32 * first_hash)487054ecb6b6SJoel Becker static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
487154ecb6b6SJoel Becker 				  u64 src_blk, u64 last_blk, u64 to_blk,
487254ecb6b6SJoel Becker 				  unsigned int start_bucket,
487354ecb6b6SJoel Becker 				  u32 *first_hash)
487401225596STao Ma {
487501225596STao Ma 	int i, ret, credits;
487601225596STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
487715d60929SJoel Becker 	int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
487801225596STao Ma 	int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
487915d60929SJoel Becker 	struct ocfs2_xattr_bucket *old_first, *new_first;
488001225596STao Ma 
4881402b4183STao Ma 	trace_ocfs2_mv_xattr_buckets((unsigned long long)last_blk,
4882402b4183STao Ma 				     (unsigned long long)to_blk);
488301225596STao Ma 
488454ecb6b6SJoel Becker 	BUG_ON(start_bucket >= num_buckets);
488554ecb6b6SJoel Becker 	if (start_bucket) {
488654ecb6b6SJoel Becker 		num_buckets -= start_bucket;
488754ecb6b6SJoel Becker 		last_blk += (start_bucket * blks_per_bucket);
488854ecb6b6SJoel Becker 	}
488954ecb6b6SJoel Becker 
489015d60929SJoel Becker 	/* The first bucket of the original extent */
489115d60929SJoel Becker 	old_first = ocfs2_xattr_bucket_new(inode);
489215d60929SJoel Becker 	/* The first bucket of the new extent */
489315d60929SJoel Becker 	new_first = ocfs2_xattr_bucket_new(inode);
489415d60929SJoel Becker 	if (!old_first || !new_first) {
489515d60929SJoel Becker 		ret = -ENOMEM;
489615d60929SJoel Becker 		mlog_errno(ret);
489715d60929SJoel Becker 		goto out;
489815d60929SJoel Becker 	}
489915d60929SJoel Becker 
4900874d65afSJoel Becker 	ret = ocfs2_read_xattr_bucket(old_first, src_blk);
490115d60929SJoel Becker 	if (ret) {
490215d60929SJoel Becker 		mlog_errno(ret);
490315d60929SJoel Becker 		goto out;
490415d60929SJoel Becker 	}
490515d60929SJoel Becker 
490601225596STao Ma 	/*
490754ecb6b6SJoel Becker 	 * We need to update the first bucket of the old extent and all
490854ecb6b6SJoel Becker 	 * the buckets going to the new extent.
490901225596STao Ma 	 */
4910c901fb00STao Ma 	credits = ((num_buckets + 1) * blks_per_bucket);
491101225596STao Ma 	ret = ocfs2_extend_trans(handle, credits);
491201225596STao Ma 	if (ret) {
491301225596STao Ma 		mlog_errno(ret);
491401225596STao Ma 		goto out;
491501225596STao Ma 	}
491601225596STao Ma 
491715d60929SJoel Becker 	ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
491801225596STao Ma 						OCFS2_JOURNAL_ACCESS_WRITE);
491901225596STao Ma 	if (ret) {
492001225596STao Ma 		mlog_errno(ret);
492101225596STao Ma 		goto out;
492201225596STao Ma 	}
492301225596STao Ma 
492401225596STao Ma 	for (i = 0; i < num_buckets; i++) {
492501225596STao Ma 		ret = ocfs2_cp_xattr_bucket(inode, handle,
4926874d65afSJoel Becker 					    last_blk + (i * blks_per_bucket),
492715d60929SJoel Becker 					    to_blk + (i * blks_per_bucket),
492815d60929SJoel Becker 					    1);
492901225596STao Ma 		if (ret) {
493001225596STao Ma 			mlog_errno(ret);
493101225596STao Ma 			goto out;
493201225596STao Ma 		}
493301225596STao Ma 	}
493401225596STao Ma 
493515d60929SJoel Becker 	/*
493615d60929SJoel Becker 	 * Get the new bucket ready before we dirty anything
493715d60929SJoel Becker 	 * (This actually shouldn't fail, because we already dirtied
493815d60929SJoel Becker 	 * it once in ocfs2_cp_xattr_bucket()).
493915d60929SJoel Becker 	 */
494015d60929SJoel Becker 	ret = ocfs2_read_xattr_bucket(new_first, to_blk);
494115d60929SJoel Becker 	if (ret) {
494201225596STao Ma 		mlog_errno(ret);
494301225596STao Ma 		goto out;
494401225596STao Ma 	}
494515d60929SJoel Becker 	ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
494601225596STao Ma 						OCFS2_JOURNAL_ACCESS_WRITE);
494701225596STao Ma 	if (ret) {
494801225596STao Ma 		mlog_errno(ret);
494901225596STao Ma 		goto out;
495001225596STao Ma 	}
495101225596STao Ma 
495215d60929SJoel Becker 	/* Now update the headers */
495315d60929SJoel Becker 	le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
495415d60929SJoel Becker 	ocfs2_xattr_bucket_journal_dirty(handle, old_first);
495501225596STao Ma 
495615d60929SJoel Becker 	bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
495715d60929SJoel Becker 	ocfs2_xattr_bucket_journal_dirty(handle, new_first);
495801225596STao Ma 
495901225596STao Ma 	if (first_hash)
496015d60929SJoel Becker 		*first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
496115d60929SJoel Becker 
496201225596STao Ma out:
496315d60929SJoel Becker 	ocfs2_xattr_bucket_free(new_first);
496415d60929SJoel Becker 	ocfs2_xattr_bucket_free(old_first);
496501225596STao Ma 	return ret;
496601225596STao Ma }
496701225596STao Ma 
496801225596STao Ma /*
496980bcaf34STao Ma  * Move some xattrs in this cluster to the new cluster.
497001225596STao Ma  * This function should only be called when bucket size == cluster size.
497101225596STao Ma  * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
497201225596STao Ma  */
ocfs2_divide_xattr_cluster(struct inode * inode,handle_t * handle,u64 prev_blk,u64 new_blk,u32 * first_hash)497380bcaf34STao Ma static int ocfs2_divide_xattr_cluster(struct inode *inode,
497401225596STao Ma 				      handle_t *handle,
497501225596STao Ma 				      u64 prev_blk,
497601225596STao Ma 				      u64 new_blk,
497701225596STao Ma 				      u32 *first_hash)
497801225596STao Ma {
497901225596STao Ma 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4980c901fb00STao Ma 	int ret, credits = 2 * blk_per_bucket;
498101225596STao Ma 
498201225596STao Ma 	BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
498301225596STao Ma 
498401225596STao Ma 	ret = ocfs2_extend_trans(handle, credits);
498501225596STao Ma 	if (ret) {
498601225596STao Ma 		mlog_errno(ret);
498701225596STao Ma 		return ret;
498801225596STao Ma 	}
498901225596STao Ma 
499001225596STao Ma 	/* Move half of the xattr in start_blk to the next bucket. */
499180bcaf34STao Ma 	return  ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
499201225596STao Ma 					  new_blk, first_hash, 1);
499301225596STao Ma }
499401225596STao Ma 
499501225596STao Ma /*
499601225596STao Ma  * Move some xattrs from the old cluster to the new one since they are not
499701225596STao Ma  * contiguous in ocfs2 xattr tree.
499801225596STao Ma  *
499901225596STao Ma  * new_blk starts a new separate cluster, and we will move some xattrs from
500001225596STao Ma  * prev_blk to it. v_start will be set as the first name hash value in this
500101225596STao Ma  * new cluster so that it can be used as e_cpos during tree insertion and
500201225596STao Ma  * don't collide with our original b-tree operations. first_bh and header_bh
500301225596STao Ma  * will also be updated since they will be used in ocfs2_extend_xattr_bucket
500401225596STao Ma  * to extend the insert bucket.
500501225596STao Ma  *
500601225596STao Ma  * The problem is how much xattr should we move to the new one and when should
500701225596STao Ma  * we update first_bh and header_bh?
500801225596STao Ma  * 1. If cluster size > bucket size, that means the previous cluster has more
500901225596STao Ma  *    than 1 bucket, so just move half nums of bucket into the new cluster and
501001225596STao Ma  *    update the first_bh and header_bh if the insert bucket has been moved
501101225596STao Ma  *    to the new cluster.
501201225596STao Ma  * 2. If cluster_size == bucket_size:
501301225596STao Ma  *    a) If the previous extent rec has more than one cluster and the insert
501401225596STao Ma  *       place isn't in the last cluster, copy the entire last cluster to the
501501225596STao Ma  *       new one. This time, we don't need to upate the first_bh and header_bh
501601225596STao Ma  *       since they will not be moved into the new cluster.
501701225596STao Ma  *    b) Otherwise, move the bottom half of the xattrs in the last cluster into
501801225596STao Ma  *       the new one. And we set the extend flag to zero if the insert place is
501901225596STao Ma  *       moved into the new allocated cluster since no extend is needed.
502001225596STao Ma  */
ocfs2_adjust_xattr_cross_cluster(struct inode * inode,handle_t * handle,struct ocfs2_xattr_bucket * first,struct ocfs2_xattr_bucket * target,u64 new_blk,u32 prev_clusters,u32 * v_start,int * extend)502101225596STao Ma static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
502201225596STao Ma 					    handle_t *handle,
5023012ee910SJoel Becker 					    struct ocfs2_xattr_bucket *first,
5024012ee910SJoel Becker 					    struct ocfs2_xattr_bucket *target,
502501225596STao Ma 					    u64 new_blk,
502601225596STao Ma 					    u32 prev_clusters,
502701225596STao Ma 					    u32 *v_start,
502801225596STao Ma 					    int *extend)
502901225596STao Ma {
503092cf3adfSJoel Becker 	int ret;
503101225596STao Ma 
5032402b4183STao Ma 	trace_ocfs2_adjust_xattr_cross_cluster(
5033402b4183STao Ma 			(unsigned long long)bucket_blkno(first),
5034402b4183STao Ma 			(unsigned long long)new_blk, prev_clusters);
503501225596STao Ma 
503641cb8148SJoel Becker 	if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
503701225596STao Ma 		ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
503801225596STao Ma 							  handle,
503941cb8148SJoel Becker 							  first, target,
504001225596STao Ma 							  new_blk,
504101225596STao Ma 							  prev_clusters,
504201225596STao Ma 							  v_start);
5043012ee910SJoel Becker 		if (ret)
504441cb8148SJoel Becker 			mlog_errno(ret);
504541cb8148SJoel Becker 	} else {
504692cf3adfSJoel Becker 		/* The start of the last cluster in the first extent */
504792cf3adfSJoel Becker 		u64 last_blk = bucket_blkno(first) +
504892cf3adfSJoel Becker 			((prev_clusters - 1) *
504992cf3adfSJoel Becker 			 ocfs2_clusters_to_blocks(inode->i_sb, 1));
505001225596STao Ma 
5051012ee910SJoel Becker 		if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
5052874d65afSJoel Becker 			ret = ocfs2_mv_xattr_buckets(inode, handle,
505392cf3adfSJoel Becker 						     bucket_blkno(first),
505454ecb6b6SJoel Becker 						     last_blk, new_blk, 0,
505501225596STao Ma 						     v_start);
5056012ee910SJoel Becker 			if (ret)
5057012ee910SJoel Becker 				mlog_errno(ret);
5058012ee910SJoel Becker 		} else {
505980bcaf34STao Ma 			ret = ocfs2_divide_xattr_cluster(inode, handle,
506001225596STao Ma 							 last_blk, new_blk,
506101225596STao Ma 							 v_start);
5062012ee910SJoel Becker 			if (ret)
5063012ee910SJoel Becker 				mlog_errno(ret);
506401225596STao Ma 
506592cf3adfSJoel Becker 			if ((bucket_blkno(target) == last_blk) && extend)
506601225596STao Ma 				*extend = 0;
506701225596STao Ma 		}
506801225596STao Ma 	}
506901225596STao Ma 
507001225596STao Ma 	return ret;
507101225596STao Ma }
507201225596STao Ma 
507301225596STao Ma /*
507401225596STao Ma  * Add a new cluster for xattr storage.
507501225596STao Ma  *
507601225596STao Ma  * If the new cluster is contiguous with the previous one, it will be
507701225596STao Ma  * appended to the same extent record, and num_clusters will be updated.
507801225596STao Ma  * If not, we will insert a new extent for it and move some xattrs in
507901225596STao Ma  * the last cluster into the new allocated one.
508001225596STao Ma  * We also need to limit the maximum size of a btree leaf, otherwise we'll
508101225596STao Ma  * lose the benefits of hashing because we'll have to search large leaves.
508201225596STao Ma  * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
508301225596STao Ma  * if it's bigger).
508401225596STao Ma  *
508501225596STao Ma  * first_bh is the first block of the previous extent rec and header_bh
508601225596STao Ma  * indicates the bucket we will insert the new xattrs. They will be updated
508701225596STao Ma  * when the header_bh is moved into the new cluster.
508801225596STao Ma  */
ocfs2_add_new_xattr_cluster(struct inode * inode,struct buffer_head * root_bh,struct ocfs2_xattr_bucket * first,struct ocfs2_xattr_bucket * target,u32 * num_clusters,u32 prev_cpos,int * extend,struct ocfs2_xattr_set_ctxt * ctxt)508901225596STao Ma static int ocfs2_add_new_xattr_cluster(struct inode *inode,
509001225596STao Ma 				       struct buffer_head *root_bh,
5091ed29c0caSJoel Becker 				       struct ocfs2_xattr_bucket *first,
5092ed29c0caSJoel Becker 				       struct ocfs2_xattr_bucket *target,
509301225596STao Ma 				       u32 *num_clusters,
509401225596STao Ma 				       u32 prev_cpos,
509578f30c31STao Ma 				       int *extend,
509678f30c31STao Ma 				       struct ocfs2_xattr_set_ctxt *ctxt)
509701225596STao Ma {
509885db90e7STao Ma 	int ret;
509901225596STao Ma 	u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
510001225596STao Ma 	u32 prev_clusters = *num_clusters;
510101225596STao Ma 	u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
510201225596STao Ma 	u64 block;
510385db90e7STao Ma 	handle_t *handle = ctxt->handle;
510401225596STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5105f99b9b7cSJoel Becker 	struct ocfs2_extent_tree et;
510601225596STao Ma 
5107402b4183STao Ma 	trace_ocfs2_add_new_xattr_cluster_begin(
510801225596STao Ma 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
5109402b4183STao Ma 		(unsigned long long)bucket_blkno(first),
5110402b4183STao Ma 		prev_cpos, prev_clusters);
511101225596STao Ma 
51125e404e9eSJoel Becker 	ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5113f99b9b7cSJoel Becker 
51140cf2f763SJoel Becker 	ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
511501225596STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
511601225596STao Ma 	if (ret < 0) {
511701225596STao Ma 		mlog_errno(ret);
511801225596STao Ma 		goto leave;
511901225596STao Ma 	}
512001225596STao Ma 
51211ed9b777SJoel Becker 	ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
512201225596STao Ma 				     clusters_to_add, &bit_off, &num_bits);
512301225596STao Ma 	if (ret < 0) {
512401225596STao Ma 		if (ret != -ENOSPC)
512501225596STao Ma 			mlog_errno(ret);
512601225596STao Ma 		goto leave;
512701225596STao Ma 	}
512801225596STao Ma 
512901225596STao Ma 	BUG_ON(num_bits > clusters_to_add);
513001225596STao Ma 
513101225596STao Ma 	block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5132402b4183STao Ma 	trace_ocfs2_add_new_xattr_cluster((unsigned long long)block, num_bits);
513301225596STao Ma 
5134ed29c0caSJoel Becker 	if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
513501225596STao Ma 	    (prev_clusters + num_bits) << osb->s_clustersize_bits <=
513601225596STao Ma 	     OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
513701225596STao Ma 		/*
513801225596STao Ma 		 * If this cluster is contiguous with the old one and
513901225596STao Ma 		 * adding this new cluster, we don't surpass the limit of
514001225596STao Ma 		 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
514101225596STao Ma 		 * initialized and used like other buckets in the previous
514201225596STao Ma 		 * cluster.
514301225596STao Ma 		 * So add it as a contiguous one. The caller will handle
514401225596STao Ma 		 * its init process.
514501225596STao Ma 		 */
514601225596STao Ma 		v_start = prev_cpos + prev_clusters;
514701225596STao Ma 		*num_clusters = prev_clusters + num_bits;
514801225596STao Ma 	} else {
514901225596STao Ma 		ret = ocfs2_adjust_xattr_cross_cluster(inode,
515001225596STao Ma 						       handle,
5151012ee910SJoel Becker 						       first,
5152012ee910SJoel Becker 						       target,
515301225596STao Ma 						       block,
515401225596STao Ma 						       prev_clusters,
515501225596STao Ma 						       &v_start,
515601225596STao Ma 						       extend);
515701225596STao Ma 		if (ret) {
515801225596STao Ma 			mlog_errno(ret);
515901225596STao Ma 			goto leave;
516001225596STao Ma 		}
516101225596STao Ma 	}
516201225596STao Ma 
5163402b4183STao Ma 	trace_ocfs2_add_new_xattr_cluster_insert((unsigned long long)block,
5164402b4183STao Ma 						 v_start, num_bits);
5165cc79d8c1SJoel Becker 	ret = ocfs2_insert_extent(handle, &et, v_start, block,
516678f30c31STao Ma 				  num_bits, 0, ctxt->meta_ac);
516701225596STao Ma 	if (ret < 0) {
516801225596STao Ma 		mlog_errno(ret);
516901225596STao Ma 		goto leave;
517001225596STao Ma 	}
517101225596STao Ma 
5172ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, root_bh);
517301225596STao Ma 
517401225596STao Ma leave:
517501225596STao Ma 	return ret;
517601225596STao Ma }
517701225596STao Ma 
517801225596STao Ma /*
517992de109aSJoel Becker  * We are given an extent.  'first' is the bucket at the very front of
518092de109aSJoel Becker  * the extent.  The extent has space for an additional bucket past
518192de109aSJoel Becker  * bucket_xh(first)->xh_num_buckets.  'target_blkno' is the block number
518292de109aSJoel Becker  * of the target bucket.  We wish to shift every bucket past the target
518392de109aSJoel Becker  * down one, filling in that additional space.  When we get back to the
518492de109aSJoel Becker  * target, we split the target between itself and the now-empty bucket
518592de109aSJoel Becker  * at target+1 (aka, target_blkno + blks_per_bucket).
518601225596STao Ma  */
ocfs2_extend_xattr_bucket(struct inode * inode,handle_t * handle,struct ocfs2_xattr_bucket * first,u64 target_blk,u32 num_clusters)518701225596STao Ma static int ocfs2_extend_xattr_bucket(struct inode *inode,
518885db90e7STao Ma 				     handle_t *handle,
518992de109aSJoel Becker 				     struct ocfs2_xattr_bucket *first,
519092de109aSJoel Becker 				     u64 target_blk,
519101225596STao Ma 				     u32 num_clusters)
519201225596STao Ma {
519301225596STao Ma 	int ret, credits;
519401225596STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
519501225596STao Ma 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
519692de109aSJoel Becker 	u64 end_blk;
519792de109aSJoel Becker 	u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
519801225596STao Ma 
5199402b4183STao Ma 	trace_ocfs2_extend_xattr_bucket((unsigned long long)target_blk,
5200402b4183STao Ma 					(unsigned long long)bucket_blkno(first),
5201402b4183STao Ma 					num_clusters, new_bucket);
520201225596STao Ma 
520392de109aSJoel Becker 	/* The extent must have room for an additional bucket */
520492de109aSJoel Becker 	BUG_ON(new_bucket >=
520592de109aSJoel Becker 	       (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
520601225596STao Ma 
520792de109aSJoel Becker 	/* end_blk points to the last existing bucket */
520892de109aSJoel Becker 	end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
520901225596STao Ma 
521001225596STao Ma 	/*
521192de109aSJoel Becker 	 * end_blk is the start of the last existing bucket.
521292de109aSJoel Becker 	 * Thus, (end_blk - target_blk) covers the target bucket and
521392de109aSJoel Becker 	 * every bucket after it up to, but not including, the last
521492de109aSJoel Becker 	 * existing bucket.  Then we add the last existing bucket, the
521592de109aSJoel Becker 	 * new bucket, and the first bucket (3 * blk_per_bucket).
521601225596STao Ma 	 */
5217c901fb00STao Ma 	credits = (end_blk - target_blk) + (3 * blk_per_bucket);
521885db90e7STao Ma 	ret = ocfs2_extend_trans(handle, credits);
521985db90e7STao Ma 	if (ret) {
522001225596STao Ma 		mlog_errno(ret);
522101225596STao Ma 		goto out;
522201225596STao Ma 	}
522301225596STao Ma 
522492de109aSJoel Becker 	ret = ocfs2_xattr_bucket_journal_access(handle, first,
522501225596STao Ma 						OCFS2_JOURNAL_ACCESS_WRITE);
522601225596STao Ma 	if (ret) {
522701225596STao Ma 		mlog_errno(ret);
522885db90e7STao Ma 		goto out;
522901225596STao Ma 	}
523001225596STao Ma 
523192de109aSJoel Becker 	while (end_blk != target_blk) {
523201225596STao Ma 		ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
523301225596STao Ma 					    end_blk + blk_per_bucket, 0);
523401225596STao Ma 		if (ret)
523585db90e7STao Ma 			goto out;
523601225596STao Ma 		end_blk -= blk_per_bucket;
523701225596STao Ma 	}
523801225596STao Ma 
523992de109aSJoel Becker 	/* Move half of the xattr in target_blkno to the next bucket. */
524092de109aSJoel Becker 	ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
524192de109aSJoel Becker 					target_blk + blk_per_bucket, NULL, 0);
524201225596STao Ma 
524392de109aSJoel Becker 	le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
524492de109aSJoel Becker 	ocfs2_xattr_bucket_journal_dirty(handle, first);
524501225596STao Ma 
524601225596STao Ma out:
524701225596STao Ma 	return ret;
524801225596STao Ma }
524901225596STao Ma 
525001225596STao Ma /*
525191f2033fSJoel Becker  * Add new xattr bucket in an extent record and adjust the buckets
525291f2033fSJoel Becker  * accordingly.  xb_bh is the ocfs2_xattr_block, and target is the
525391f2033fSJoel Becker  * bucket we want to insert into.
525401225596STao Ma  *
525591f2033fSJoel Becker  * In the easy case, we will move all the buckets after target down by
525691f2033fSJoel Becker  * one. Half of target's xattrs will be moved to the next bucket.
525791f2033fSJoel Becker  *
525891f2033fSJoel Becker  * If current cluster is full, we'll allocate a new one.  This may not
525991f2033fSJoel Becker  * be contiguous.  The underlying calls will make sure that there is
526091f2033fSJoel Becker  * space for the insert, shifting buckets around if necessary.
526191f2033fSJoel Becker  * 'target' may be moved by those calls.
526201225596STao Ma  */
ocfs2_add_new_xattr_bucket(struct inode * inode,struct buffer_head * xb_bh,struct ocfs2_xattr_bucket * target,struct ocfs2_xattr_set_ctxt * ctxt)526301225596STao Ma static int ocfs2_add_new_xattr_bucket(struct inode *inode,
526401225596STao Ma 				      struct buffer_head *xb_bh,
526591f2033fSJoel Becker 				      struct ocfs2_xattr_bucket *target,
526678f30c31STao Ma 				      struct ocfs2_xattr_set_ctxt *ctxt)
526701225596STao Ma {
526801225596STao Ma 	struct ocfs2_xattr_block *xb =
526901225596STao Ma 			(struct ocfs2_xattr_block *)xb_bh->b_data;
527001225596STao Ma 	struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
527101225596STao Ma 	struct ocfs2_extent_list *el = &xb_root->xt_list;
527291f2033fSJoel Becker 	u32 name_hash =
527391f2033fSJoel Becker 		le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5274ed29c0caSJoel Becker 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
527501225596STao Ma 	int ret, num_buckets, extend = 1;
527601225596STao Ma 	u64 p_blkno;
527701225596STao Ma 	u32 e_cpos, num_clusters;
527892de109aSJoel Becker 	/* The bucket at the front of the extent */
527991f2033fSJoel Becker 	struct ocfs2_xattr_bucket *first;
528001225596STao Ma 
5281402b4183STao Ma 	trace_ocfs2_add_new_xattr_bucket(
528291f2033fSJoel Becker 				(unsigned long long)bucket_blkno(target));
528301225596STao Ma 
5284ed29c0caSJoel Becker 	/* The first bucket of the original extent */
528592de109aSJoel Becker 	first = ocfs2_xattr_bucket_new(inode);
528691f2033fSJoel Becker 	if (!first) {
528792de109aSJoel Becker 		ret = -ENOMEM;
528892de109aSJoel Becker 		mlog_errno(ret);
528992de109aSJoel Becker 		goto out;
529092de109aSJoel Becker 	}
529192de109aSJoel Becker 
529201225596STao Ma 	ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
529301225596STao Ma 				  &num_clusters, el);
529401225596STao Ma 	if (ret) {
529501225596STao Ma 		mlog_errno(ret);
529601225596STao Ma 		goto out;
529701225596STao Ma 	}
529801225596STao Ma 
5299ed29c0caSJoel Becker 	ret = ocfs2_read_xattr_bucket(first, p_blkno);
5300ed29c0caSJoel Becker 	if (ret) {
5301ed29c0caSJoel Becker 		mlog_errno(ret);
5302ed29c0caSJoel Becker 		goto out;
5303ed29c0caSJoel Becker 	}
5304ed29c0caSJoel Becker 
530501225596STao Ma 	num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5306ed29c0caSJoel Becker 	if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5307ed29c0caSJoel Becker 		/*
5308ed29c0caSJoel Becker 		 * This can move first+target if the target bucket moves
5309ed29c0caSJoel Becker 		 * to the new extent.
5310ed29c0caSJoel Becker 		 */
531101225596STao Ma 		ret = ocfs2_add_new_xattr_cluster(inode,
531201225596STao Ma 						  xb_bh,
5313ed29c0caSJoel Becker 						  first,
5314ed29c0caSJoel Becker 						  target,
531501225596STao Ma 						  &num_clusters,
531601225596STao Ma 						  e_cpos,
531778f30c31STao Ma 						  &extend,
531878f30c31STao Ma 						  ctxt);
531901225596STao Ma 		if (ret) {
532001225596STao Ma 			mlog_errno(ret);
532101225596STao Ma 			goto out;
532201225596STao Ma 		}
532301225596STao Ma 	}
532401225596STao Ma 
532592de109aSJoel Becker 	if (extend) {
532601225596STao Ma 		ret = ocfs2_extend_xattr_bucket(inode,
532785db90e7STao Ma 						ctxt->handle,
5328ed29c0caSJoel Becker 						first,
5329ed29c0caSJoel Becker 						bucket_blkno(target),
533001225596STao Ma 						num_clusters);
533101225596STao Ma 		if (ret)
533201225596STao Ma 			mlog_errno(ret);
533392de109aSJoel Becker 	}
533492de109aSJoel Becker 
533501225596STao Ma out:
533692de109aSJoel Becker 	ocfs2_xattr_bucket_free(first);
5337ed29c0caSJoel Becker 
533801225596STao Ma 	return ret;
533901225596STao Ma }
534001225596STao Ma 
534101225596STao Ma /*
534201225596STao Ma  * Truncate the specified xe_off entry in xattr bucket.
534301225596STao Ma  * bucket is indicated by header_bh and len is the new length.
534401225596STao Ma  * Both the ocfs2_xattr_value_root and the entry will be updated here.
534501225596STao Ma  *
534601225596STao Ma  * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
534701225596STao Ma  */
ocfs2_xattr_bucket_value_truncate(struct inode * inode,struct ocfs2_xattr_bucket * bucket,int xe_off,int len,struct ocfs2_xattr_set_ctxt * ctxt)534801225596STao Ma static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5349548b0f22SJoel Becker 					     struct ocfs2_xattr_bucket *bucket,
535001225596STao Ma 					     int xe_off,
535178f30c31STao Ma 					     int len,
535278f30c31STao Ma 					     struct ocfs2_xattr_set_ctxt *ctxt)
535301225596STao Ma {
535401225596STao Ma 	int ret, offset;
535501225596STao Ma 	u64 value_blk;
535601225596STao Ma 	struct ocfs2_xattr_entry *xe;
5357548b0f22SJoel Becker 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
535801225596STao Ma 	size_t blocksize = inode->i_sb->s_blocksize;
5359b3e5d379SJoel Becker 	struct ocfs2_xattr_value_buf vb = {
5360b3e5d379SJoel Becker 		.vb_access = ocfs2_journal_access,
5361b3e5d379SJoel Becker 	};
536201225596STao Ma 
536301225596STao Ma 	xe = &xh->xh_entries[xe_off];
536401225596STao Ma 
536501225596STao Ma 	BUG_ON(!xe || ocfs2_xattr_is_local(xe));
536601225596STao Ma 
536701225596STao Ma 	offset = le16_to_cpu(xe->xe_name_offset) +
536801225596STao Ma 		 OCFS2_XATTR_SIZE(xe->xe_name_len);
536901225596STao Ma 
537001225596STao Ma 	value_blk = offset / blocksize;
537101225596STao Ma 
537201225596STao Ma 	/* We don't allow ocfs2_xattr_value to be stored in different block. */
537301225596STao Ma 	BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
537401225596STao Ma 
5375b3e5d379SJoel Becker 	vb.vb_bh = bucket->bu_bhs[value_blk];
5376b3e5d379SJoel Becker 	BUG_ON(!vb.vb_bh);
537701225596STao Ma 
5378b3e5d379SJoel Becker 	vb.vb_xv = (struct ocfs2_xattr_value_root *)
5379b3e5d379SJoel Becker 		(vb.vb_bh->b_data + offset % blocksize);
538001225596STao Ma 
5381548b0f22SJoel Becker 	/*
5382548b0f22SJoel Becker 	 * From here on out we have to dirty the bucket.  The generic
5383548b0f22SJoel Becker 	 * value calls only modify one of the bucket's bhs, but we need
5384548b0f22SJoel Becker 	 * to send the bucket at once.  So if they error, they *could* have
5385548b0f22SJoel Becker 	 * modified something.  We have to assume they did, and dirty
5386548b0f22SJoel Becker 	 * the whole bucket.  This leaves us in a consistent state.
5387548b0f22SJoel Becker 	 */
5388402b4183STao Ma 	trace_ocfs2_xattr_bucket_value_truncate(
5389402b4183STao Ma 			(unsigned long long)bucket_blkno(bucket), xe_off, len);
5390b3e5d379SJoel Becker 	ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
539101225596STao Ma 	if (ret) {
539201225596STao Ma 		mlog_errno(ret);
5393554e7f9eSTao Ma 		goto out;
5394554e7f9eSTao Ma 	}
5395554e7f9eSTao Ma 
5396554e7f9eSTao Ma 	ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5397554e7f9eSTao Ma 						OCFS2_JOURNAL_ACCESS_WRITE);
5398554e7f9eSTao Ma 	if (ret) {
5399554e7f9eSTao Ma 		mlog_errno(ret);
5400554e7f9eSTao Ma 		goto out;
540101225596STao Ma 	}
540201225596STao Ma 
5403548b0f22SJoel Becker 	xe->xe_value_size = cpu_to_le64(len);
5404548b0f22SJoel Becker 
5405548b0f22SJoel Becker 	ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
540601225596STao Ma 
540701225596STao Ma out:
540801225596STao Ma 	return ret;
540901225596STao Ma }
541001225596STao Ma 
ocfs2_rm_xattr_cluster(struct inode * inode,struct buffer_head * root_bh,u64 blkno,u32 cpos,u32 len,void * para)541101225596STao Ma static int ocfs2_rm_xattr_cluster(struct inode *inode,
541201225596STao Ma 				  struct buffer_head *root_bh,
541301225596STao Ma 				  u64 blkno,
541401225596STao Ma 				  u32 cpos,
541547bca495STao Ma 				  u32 len,
541647bca495STao Ma 				  void *para)
541701225596STao Ma {
541801225596STao Ma 	int ret;
541901225596STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
542001225596STao Ma 	struct inode *tl_inode = osb->osb_tl_inode;
542101225596STao Ma 	handle_t *handle;
542201225596STao Ma 	struct ocfs2_xattr_block *xb =
542301225596STao Ma 			(struct ocfs2_xattr_block *)root_bh->b_data;
542401225596STao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
542501225596STao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
5426f99b9b7cSJoel Becker 	struct ocfs2_extent_tree et;
5427f99b9b7cSJoel Becker 
542847bca495STao Ma 	ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5429ce9c5a54STao Ma 					  ocfs2_delete_xattr_in_bucket, para);
543047bca495STao Ma 	if (ret) {
543147bca495STao Ma 		mlog_errno(ret);
543247bca495STao Ma 		return ret;
543347bca495STao Ma 	}
543447bca495STao Ma 
54355e404e9eSJoel Becker 	ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
543601225596STao Ma 
543701225596STao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
543801225596STao Ma 
5439402b4183STao Ma 	trace_ocfs2_rm_xattr_cluster(
5440402b4183STao Ma 			(unsigned long long)OCFS2_I(inode)->ip_blkno,
5441402b4183STao Ma 			(unsigned long long)blkno, cpos, len);
544201225596STao Ma 
54438cb471e8SJoel Becker 	ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
54448cb471e8SJoel Becker 					       len);
544501225596STao Ma 
5446f99b9b7cSJoel Becker 	ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
544701225596STao Ma 	if (ret) {
544801225596STao Ma 		mlog_errno(ret);
544901225596STao Ma 		return ret;
545001225596STao Ma 	}
545101225596STao Ma 
54525955102cSAl Viro 	inode_lock(tl_inode);
545301225596STao Ma 
545401225596STao Ma 	if (ocfs2_truncate_log_needs_flush(osb)) {
545501225596STao Ma 		ret = __ocfs2_flush_truncate_log(osb);
545601225596STao Ma 		if (ret < 0) {
545701225596STao Ma 			mlog_errno(ret);
545801225596STao Ma 			goto out;
545901225596STao Ma 		}
546001225596STao Ma 	}
546101225596STao Ma 
5462a90714c1SJan Kara 	handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5463d3264799STao Ma 	if (IS_ERR(handle)) {
546401225596STao Ma 		ret = -ENOMEM;
546501225596STao Ma 		mlog_errno(ret);
546601225596STao Ma 		goto out;
546701225596STao Ma 	}
546801225596STao Ma 
54690cf2f763SJoel Becker 	ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
547001225596STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
547101225596STao Ma 	if (ret) {
547201225596STao Ma 		mlog_errno(ret);
547301225596STao Ma 		goto out_commit;
547401225596STao Ma 	}
547501225596STao Ma 
5476dbdcf6a4SJoel Becker 	ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5477f99b9b7cSJoel Becker 				  &dealloc);
547801225596STao Ma 	if (ret) {
547901225596STao Ma 		mlog_errno(ret);
548001225596STao Ma 		goto out_commit;
548101225596STao Ma 	}
548201225596STao Ma 
548301225596STao Ma 	le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5484ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, root_bh);
548501225596STao Ma 
548601225596STao Ma 	ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
548701225596STao Ma 	if (ret)
548801225596STao Ma 		mlog_errno(ret);
54896fdb702dSDarrick J. Wong 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
549001225596STao Ma 
549101225596STao Ma out_commit:
549201225596STao Ma 	ocfs2_commit_trans(osb, handle);
549301225596STao Ma out:
549401225596STao Ma 	ocfs2_schedule_truncate_log_flush(osb, 1);
549501225596STao Ma 
54965955102cSAl Viro 	inode_unlock(tl_inode);
549701225596STao Ma 
549801225596STao Ma 	if (meta_ac)
549901225596STao Ma 		ocfs2_free_alloc_context(meta_ac);
550001225596STao Ma 
550101225596STao Ma 	ocfs2_run_deallocs(osb, &dealloc);
550201225596STao Ma 
550301225596STao Ma 	return ret;
550401225596STao Ma }
550501225596STao Ma 
550601225596STao Ma /*
550780bcaf34STao Ma  * check whether the xattr bucket is filled up with the same hash value.
550880bcaf34STao Ma  * If we want to insert the xattr with the same hash, return -ENOSPC.
550980bcaf34STao Ma  * If we want to insert a xattr with different hash value, go ahead
551080bcaf34STao Ma  * and ocfs2_divide_xattr_bucket will handle this.
551180bcaf34STao Ma  */
ocfs2_check_xattr_bucket_collision(struct inode * inode,struct ocfs2_xattr_bucket * bucket,const char * name)551201225596STao Ma static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
551380bcaf34STao Ma 					      struct ocfs2_xattr_bucket *bucket,
551480bcaf34STao Ma 					      const char *name)
551501225596STao Ma {
55163e632946SJoel Becker 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
551780bcaf34STao Ma 	u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
551880bcaf34STao Ma 
551980bcaf34STao Ma 	if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
552080bcaf34STao Ma 		return 0;
552101225596STao Ma 
552201225596STao Ma 	if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
552301225596STao Ma 	    xh->xh_entries[0].xe_name_hash) {
552401225596STao Ma 		mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
552501225596STao Ma 		     "hash = %u\n",
55269c7759aaSJoel Becker 		     (unsigned long long)bucket_blkno(bucket),
552701225596STao Ma 		     le32_to_cpu(xh->xh_entries[0].xe_name_hash));
552801225596STao Ma 		return -ENOSPC;
552901225596STao Ma 	}
553001225596STao Ma 
553101225596STao Ma 	return 0;
553201225596STao Ma }
553301225596STao Ma 
5534c5d95df5SJoel Becker /*
5535c5d95df5SJoel Becker  * Try to set the entry in the current bucket.  If we fail, the caller
5536c5d95df5SJoel Becker  * will handle getting us another bucket.
5537c5d95df5SJoel Becker  */
ocfs2_xattr_set_entry_bucket(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)5538c5d95df5SJoel Becker static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
553901225596STao Ma 					struct ocfs2_xattr_info *xi,
554078f30c31STao Ma 					struct ocfs2_xattr_search *xs,
554178f30c31STao Ma 					struct ocfs2_xattr_set_ctxt *ctxt)
554201225596STao Ma {
5543c5d95df5SJoel Becker 	int ret;
5544c5d95df5SJoel Becker 	struct ocfs2_xa_loc loc;
554501225596STao Ma 
5546402b4183STao Ma 	trace_ocfs2_xattr_set_entry_bucket(xi->xi_name);
554701225596STao Ma 
5548c5d95df5SJoel Becker 	ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5549c5d95df5SJoel Becker 				       xs->not_found ? NULL : xs->here);
5550c5d95df5SJoel Becker 	ret = ocfs2_xa_set(&loc, xi, ctxt);
5551c5d95df5SJoel Becker 	if (!ret) {
5552c5d95df5SJoel Becker 		xs->here = loc.xl_entry;
5553c5d95df5SJoel Becker 		goto out;
5554c5d95df5SJoel Becker 	}
5555c5d95df5SJoel Becker 	if (ret != -ENOSPC) {
5556c5d95df5SJoel Becker 		mlog_errno(ret);
5557c5d95df5SJoel Becker 		goto out;
555801225596STao Ma 	}
555901225596STao Ma 
5560c5d95df5SJoel Becker 	/* Ok, we need space.  Let's try defragmenting the bucket. */
556185db90e7STao Ma 	ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
556285db90e7STao Ma 					xs->bucket);
556301225596STao Ma 	if (ret) {
556401225596STao Ma 		mlog_errno(ret);
556501225596STao Ma 		goto out;
556601225596STao Ma 	}
556701225596STao Ma 
5568c5d95df5SJoel Becker 	ret = ocfs2_xa_set(&loc, xi, ctxt);
5569c5d95df5SJoel Becker 	if (!ret) {
5570c5d95df5SJoel Becker 		xs->here = loc.xl_entry;
5571c5d95df5SJoel Becker 		goto out;
5572c5d95df5SJoel Becker 	}
5573c5d95df5SJoel Becker 	if (ret != -ENOSPC)
5574c5d95df5SJoel Becker 		mlog_errno(ret);
557501225596STao Ma 
557601225596STao Ma 
5577c5d95df5SJoel Becker out:
5578c5d95df5SJoel Becker 	return ret;
557901225596STao Ma }
558001225596STao Ma 
ocfs2_xattr_set_entry_index_block(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)5581c5d95df5SJoel Becker static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5582c5d95df5SJoel Becker 					     struct ocfs2_xattr_info *xi,
5583c5d95df5SJoel Becker 					     struct ocfs2_xattr_search *xs,
5584c5d95df5SJoel Becker 					     struct ocfs2_xattr_set_ctxt *ctxt)
5585c5d95df5SJoel Becker {
5586c5d95df5SJoel Becker 	int ret;
5587c5d95df5SJoel Becker 
5588402b4183STao Ma 	trace_ocfs2_xattr_set_entry_index_block(xi->xi_name);
5589c5d95df5SJoel Becker 
5590c5d95df5SJoel Becker 	ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5591c5d95df5SJoel Becker 	if (!ret)
5592c5d95df5SJoel Becker 		goto out;
5593c5d95df5SJoel Becker 	if (ret != -ENOSPC) {
5594c5d95df5SJoel Becker 		mlog_errno(ret);
5595c5d95df5SJoel Becker 		goto out;
5596c5d95df5SJoel Becker 	}
5597c5d95df5SJoel Becker 
5598c5d95df5SJoel Becker 	/* Ack, need more space.  Let's try to get another bucket! */
559901225596STao Ma 
560001225596STao Ma 	/*
560101225596STao Ma 	 * We do not allow for overlapping ranges between buckets. And
560201225596STao Ma 	 * the maximum number of collisions we will allow for then is
560301225596STao Ma 	 * one bucket's worth, so check it here whether we need to
560401225596STao Ma 	 * add a new bucket for the insert.
560501225596STao Ma 	 */
560680bcaf34STao Ma 	ret = ocfs2_check_xattr_bucket_collision(inode,
5607ba937127SJoel Becker 						 xs->bucket,
56086b240ff6SJoel Becker 						 xi->xi_name);
560901225596STao Ma 	if (ret) {
561001225596STao Ma 		mlog_errno(ret);
561101225596STao Ma 		goto out;
561201225596STao Ma 	}
561301225596STao Ma 
561401225596STao Ma 	ret = ocfs2_add_new_xattr_bucket(inode,
561501225596STao Ma 					 xs->xattr_bh,
561691f2033fSJoel Becker 					 xs->bucket,
561778f30c31STao Ma 					 ctxt);
561801225596STao Ma 	if (ret) {
561901225596STao Ma 		mlog_errno(ret);
562001225596STao Ma 		goto out;
562101225596STao Ma 	}
562201225596STao Ma 
562391f2033fSJoel Becker 	/*
562491f2033fSJoel Becker 	 * ocfs2_add_new_xattr_bucket() will have updated
562591f2033fSJoel Becker 	 * xs->bucket if it moved, but it will not have updated
562691f2033fSJoel Becker 	 * any of the other search fields.  Thus, we drop it and
562791f2033fSJoel Becker 	 * re-search.  Everything should be cached, so it'll be
562891f2033fSJoel Becker 	 * quick.
562991f2033fSJoel Becker 	 */
5630ba937127SJoel Becker 	ocfs2_xattr_bucket_relse(xs->bucket);
563101225596STao Ma 	ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
56326b240ff6SJoel Becker 					   xi->xi_name_index,
56336b240ff6SJoel Becker 					   xi->xi_name, xs);
563401225596STao Ma 	if (ret && ret != -ENODATA)
563501225596STao Ma 		goto out;
563601225596STao Ma 	xs->not_found = ret;
563701225596STao Ma 
5638c5d95df5SJoel Becker 	/* Ok, we have a new bucket, let's try again */
5639c5d95df5SJoel Becker 	ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5640c5d95df5SJoel Becker 	if (ret && (ret != -ENOSPC))
5641c5d95df5SJoel Becker 		mlog_errno(ret);
5642c5d95df5SJoel Becker 
564301225596STao Ma out:
564401225596STao Ma 	return ret;
564501225596STao Ma }
5646a3944256STao Ma 
ocfs2_delete_xattr_in_bucket(struct inode * inode,struct ocfs2_xattr_bucket * bucket,void * para)5647a3944256STao Ma static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5648a3944256STao Ma 					struct ocfs2_xattr_bucket *bucket,
5649a3944256STao Ma 					void *para)
5650a3944256STao Ma {
5651ce9c5a54STao Ma 	int ret = 0, ref_credits;
56523e632946SJoel Becker 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5653a3944256STao Ma 	u16 i;
5654a3944256STao Ma 	struct ocfs2_xattr_entry *xe;
565578f30c31STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
565678f30c31STao Ma 	struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5657548b0f22SJoel Becker 	int credits = ocfs2_remove_extent_credits(osb->sb) +
5658548b0f22SJoel Becker 		ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5659ce9c5a54STao Ma 	struct ocfs2_xattr_value_root *xv;
5660ce9c5a54STao Ma 	struct ocfs2_rm_xattr_bucket_para *args =
5661ce9c5a54STao Ma 			(struct ocfs2_rm_xattr_bucket_para *)para;
566278f30c31STao Ma 
566378f30c31STao Ma 	ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5664a3944256STao Ma 
5665a3944256STao Ma 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5666a3944256STao Ma 		xe = &xh->xh_entries[i];
5667a3944256STao Ma 		if (ocfs2_xattr_is_local(xe))
5668a3944256STao Ma 			continue;
5669a3944256STao Ma 
5670ce9c5a54STao Ma 		ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5671ce9c5a54STao Ma 						      i, &xv, NULL);
5672023d4ea3SJoseph Qi 		if (ret) {
5673023d4ea3SJoseph Qi 			mlog_errno(ret);
5674023d4ea3SJoseph Qi 			break;
5675023d4ea3SJoseph Qi 		}
5676ce9c5a54STao Ma 
5677ce9c5a54STao Ma 		ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5678ce9c5a54STao Ma 							 args->ref_ci,
5679ce9c5a54STao Ma 							 args->ref_root_bh,
5680ce9c5a54STao Ma 							 &ctxt.meta_ac,
5681ce9c5a54STao Ma 							 &ref_credits);
5682ce9c5a54STao Ma 
5683ce9c5a54STao Ma 		ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
568488c3b062STao Ma 		if (IS_ERR(ctxt.handle)) {
568588c3b062STao Ma 			ret = PTR_ERR(ctxt.handle);
568688c3b062STao Ma 			mlog_errno(ret);
568788c3b062STao Ma 			break;
568888c3b062STao Ma 		}
568988c3b062STao Ma 
5690548b0f22SJoel Becker 		ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
569178f30c31STao Ma 							i, 0, &ctxt);
569288c3b062STao Ma 
569388c3b062STao Ma 		ocfs2_commit_trans(osb, ctxt.handle);
5694ce9c5a54STao Ma 		if (ctxt.meta_ac) {
5695ce9c5a54STao Ma 			ocfs2_free_alloc_context(ctxt.meta_ac);
5696ce9c5a54STao Ma 			ctxt.meta_ac = NULL;
5697ce9c5a54STao Ma 		}
5698a3944256STao Ma 		if (ret) {
5699a3944256STao Ma 			mlog_errno(ret);
5700a3944256STao Ma 			break;
5701a3944256STao Ma 		}
5702a3944256STao Ma 	}
5703a3944256STao Ma 
5704ce9c5a54STao Ma 	if (ctxt.meta_ac)
5705ce9c5a54STao Ma 		ocfs2_free_alloc_context(ctxt.meta_ac);
570678f30c31STao Ma 	ocfs2_schedule_truncate_log_flush(osb, 1);
570778f30c31STao Ma 	ocfs2_run_deallocs(osb, &ctxt.dealloc);
5708a3944256STao Ma 	return ret;
5709a3944256STao Ma }
5710a3944256STao Ma 
571199219aeaSMark Fasheh /*
5712492a8a33STao Ma  * Whenever we modify a xattr value root in the bucket(e.g, CoW
5713492a8a33STao Ma  * or change the extent record flag), we need to recalculate
5714492a8a33STao Ma  * the metaecc for the whole bucket. So it is done here.
5715492a8a33STao Ma  *
5716492a8a33STao Ma  * Note:
5717492a8a33STao Ma  * We have to give the extra credits for the caller.
5718492a8a33STao Ma  */
ocfs2_xattr_bucket_post_refcount(struct inode * inode,handle_t * handle,void * para)5719492a8a33STao Ma static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5720492a8a33STao Ma 					    handle_t *handle,
5721492a8a33STao Ma 					    void *para)
5722492a8a33STao Ma {
5723492a8a33STao Ma 	int ret;
5724492a8a33STao Ma 	struct ocfs2_xattr_bucket *bucket =
5725492a8a33STao Ma 			(struct ocfs2_xattr_bucket *)para;
5726492a8a33STao Ma 
5727492a8a33STao Ma 	ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5728492a8a33STao Ma 						OCFS2_JOURNAL_ACCESS_WRITE);
5729492a8a33STao Ma 	if (ret) {
5730492a8a33STao Ma 		mlog_errno(ret);
5731492a8a33STao Ma 		return ret;
5732492a8a33STao Ma 	}
5733492a8a33STao Ma 
5734492a8a33STao Ma 	ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5735492a8a33STao Ma 
5736492a8a33STao Ma 	return 0;
5737492a8a33STao Ma }
5738492a8a33STao Ma 
5739492a8a33STao Ma /*
5740492a8a33STao Ma  * Special action we need if the xattr value is refcounted.
5741492a8a33STao Ma  *
5742492a8a33STao Ma  * 1. If the xattr is refcounted, lock the tree.
5743492a8a33STao Ma  * 2. CoW the xattr if we are setting the new value and the value
5744492a8a33STao Ma  *    will be stored outside.
5745492a8a33STao Ma  * 3. In other case, decrease_refcount will work for us, so just
5746492a8a33STao Ma  *    lock the refcount tree, calculate the meta and credits is OK.
5747492a8a33STao Ma  *
5748492a8a33STao Ma  * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5749492a8a33STao Ma  * currently CoW is a completed transaction, while this function
5750492a8a33STao Ma  * will also lock the allocators and let us deadlock. So we will
5751492a8a33STao Ma  * CoW the whole xattr value.
5752492a8a33STao Ma  */
ocfs2_prepare_refcount_xattr(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xis,struct ocfs2_xattr_search * xbs,struct ocfs2_refcount_tree ** ref_tree,int * meta_add,int * credits)5753492a8a33STao Ma static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5754492a8a33STao Ma 					struct ocfs2_dinode *di,
5755492a8a33STao Ma 					struct ocfs2_xattr_info *xi,
5756492a8a33STao Ma 					struct ocfs2_xattr_search *xis,
5757492a8a33STao Ma 					struct ocfs2_xattr_search *xbs,
5758492a8a33STao Ma 					struct ocfs2_refcount_tree **ref_tree,
5759492a8a33STao Ma 					int *meta_add,
5760492a8a33STao Ma 					int *credits)
5761492a8a33STao Ma {
5762492a8a33STao Ma 	int ret = 0;
5763492a8a33STao Ma 	struct ocfs2_xattr_block *xb;
5764492a8a33STao Ma 	struct ocfs2_xattr_entry *xe;
5765492a8a33STao Ma 	char *base;
5766492a8a33STao Ma 	u32 p_cluster, num_clusters;
5767492a8a33STao Ma 	unsigned int ext_flags;
5768492a8a33STao Ma 	int name_offset, name_len;
5769492a8a33STao Ma 	struct ocfs2_xattr_value_buf vb;
5770492a8a33STao Ma 	struct ocfs2_xattr_bucket *bucket = NULL;
5771492a8a33STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5772492a8a33STao Ma 	struct ocfs2_post_refcount refcount;
5773492a8a33STao Ma 	struct ocfs2_post_refcount *p = NULL;
5774492a8a33STao Ma 	struct buffer_head *ref_root_bh = NULL;
5775492a8a33STao Ma 
5776492a8a33STao Ma 	if (!xis->not_found) {
5777492a8a33STao Ma 		xe = xis->here;
5778492a8a33STao Ma 		name_offset = le16_to_cpu(xe->xe_name_offset);
5779492a8a33STao Ma 		name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5780492a8a33STao Ma 		base = xis->base;
5781492a8a33STao Ma 		vb.vb_bh = xis->inode_bh;
5782492a8a33STao Ma 		vb.vb_access = ocfs2_journal_access_di;
5783492a8a33STao Ma 	} else {
5784492a8a33STao Ma 		int i, block_off = 0;
5785492a8a33STao Ma 		xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5786492a8a33STao Ma 		xe = xbs->here;
5787492a8a33STao Ma 		name_offset = le16_to_cpu(xe->xe_name_offset);
5788492a8a33STao Ma 		name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5789492a8a33STao Ma 		i = xbs->here - xbs->header->xh_entries;
5790492a8a33STao Ma 
5791492a8a33STao Ma 		if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5792fd68a894STao Ma 			ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5793492a8a33STao Ma 							bucket_xh(xbs->bucket),
5794492a8a33STao Ma 							i, &block_off,
5795492a8a33STao Ma 							&name_offset);
5796492a8a33STao Ma 			if (ret) {
5797492a8a33STao Ma 				mlog_errno(ret);
5798492a8a33STao Ma 				goto out;
5799492a8a33STao Ma 			}
5800492a8a33STao Ma 			base = bucket_block(xbs->bucket, block_off);
5801492a8a33STao Ma 			vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5802492a8a33STao Ma 			vb.vb_access = ocfs2_journal_access;
5803492a8a33STao Ma 
5804492a8a33STao Ma 			if (ocfs2_meta_ecc(osb)) {
5805492a8a33STao Ma 				/*create parameters for ocfs2_post_refcount. */
5806492a8a33STao Ma 				bucket = xbs->bucket;
5807492a8a33STao Ma 				refcount.credits = bucket->bu_blocks;
5808492a8a33STao Ma 				refcount.para = bucket;
5809492a8a33STao Ma 				refcount.func =
5810492a8a33STao Ma 					ocfs2_xattr_bucket_post_refcount;
5811492a8a33STao Ma 				p = &refcount;
5812492a8a33STao Ma 			}
5813492a8a33STao Ma 		} else {
5814492a8a33STao Ma 			base = xbs->base;
5815492a8a33STao Ma 			vb.vb_bh = xbs->xattr_bh;
5816492a8a33STao Ma 			vb.vb_access = ocfs2_journal_access_xb;
5817492a8a33STao Ma 		}
5818492a8a33STao Ma 	}
5819492a8a33STao Ma 
5820492a8a33STao Ma 	if (ocfs2_xattr_is_local(xe))
5821492a8a33STao Ma 		goto out;
5822492a8a33STao Ma 
5823492a8a33STao Ma 	vb.vb_xv = (struct ocfs2_xattr_value_root *)
5824492a8a33STao Ma 				(base + name_offset + name_len);
5825492a8a33STao Ma 
5826492a8a33STao Ma 	ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5827492a8a33STao Ma 				       &num_clusters, &vb.vb_xv->xr_list,
5828492a8a33STao Ma 				       &ext_flags);
5829492a8a33STao Ma 	if (ret) {
5830492a8a33STao Ma 		mlog_errno(ret);
5831492a8a33STao Ma 		goto out;
5832492a8a33STao Ma 	}
5833492a8a33STao Ma 
5834492a8a33STao Ma 	/*
5835492a8a33STao Ma 	 * We just need to check the 1st extent record, since we always
5836492a8a33STao Ma 	 * CoW the whole xattr. So there shouldn't be a xattr with
5837492a8a33STao Ma 	 * some REFCOUNT extent recs after the 1st one.
5838492a8a33STao Ma 	 */
5839492a8a33STao Ma 	if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5840492a8a33STao Ma 		goto out;
5841492a8a33STao Ma 
5842492a8a33STao Ma 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5843492a8a33STao Ma 				       1, ref_tree, &ref_root_bh);
5844492a8a33STao Ma 	if (ret) {
5845492a8a33STao Ma 		mlog_errno(ret);
5846492a8a33STao Ma 		goto out;
5847492a8a33STao Ma 	}
5848492a8a33STao Ma 
5849492a8a33STao Ma 	/*
5850492a8a33STao Ma 	 * If we are deleting the xattr or the new size will be stored inside,
5851492a8a33STao Ma 	 * cool, leave it there, the xattr truncate process will remove them
5852492a8a33STao Ma 	 * for us(it still needs the refcount tree lock and the meta, credits).
5853492a8a33STao Ma 	 * And the worse case is that every cluster truncate will split the
5854492a8a33STao Ma 	 * refcount tree, and make the original extent become 3. So we will need
5855492a8a33STao Ma 	 * 2 * cluster more extent recs at most.
5856492a8a33STao Ma 	 */
58576b240ff6SJoel Becker 	if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5858492a8a33STao Ma 
5859492a8a33STao Ma 		ret = ocfs2_refcounted_xattr_delete_need(inode,
5860492a8a33STao Ma 							 &(*ref_tree)->rf_ci,
5861492a8a33STao Ma 							 ref_root_bh, vb.vb_xv,
5862492a8a33STao Ma 							 meta_add, credits);
5863492a8a33STao Ma 		if (ret)
5864492a8a33STao Ma 			mlog_errno(ret);
5865492a8a33STao Ma 		goto out;
5866492a8a33STao Ma 	}
5867492a8a33STao Ma 
5868492a8a33STao Ma 	ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5869492a8a33STao Ma 				       *ref_tree, ref_root_bh, 0,
5870492a8a33STao Ma 				       le32_to_cpu(vb.vb_xv->xr_clusters), p);
5871492a8a33STao Ma 	if (ret)
5872492a8a33STao Ma 		mlog_errno(ret);
5873492a8a33STao Ma 
5874492a8a33STao Ma out:
5875492a8a33STao Ma 	brelse(ref_root_bh);
5876492a8a33STao Ma 	return ret;
5877492a8a33STao Ma }
5878492a8a33STao Ma 
5879492a8a33STao Ma /*
58800129241eSTao Ma  * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
58810129241eSTao Ma  * The physical clusters will be added to refcount tree.
58820129241eSTao Ma  */
ocfs2_xattr_value_attach_refcount(struct inode * inode,struct ocfs2_xattr_value_root * xv,struct ocfs2_extent_tree * value_et,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc,struct ocfs2_post_refcount * refcount)58830129241eSTao Ma static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
58840129241eSTao Ma 				struct ocfs2_xattr_value_root *xv,
58850129241eSTao Ma 				struct ocfs2_extent_tree *value_et,
58860129241eSTao Ma 				struct ocfs2_caching_info *ref_ci,
58870129241eSTao Ma 				struct buffer_head *ref_root_bh,
58880129241eSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc,
58890129241eSTao Ma 				struct ocfs2_post_refcount *refcount)
58900129241eSTao Ma {
58910129241eSTao Ma 	int ret = 0;
58920129241eSTao Ma 	u32 clusters = le32_to_cpu(xv->xr_clusters);
58930129241eSTao Ma 	u32 cpos, p_cluster, num_clusters;
58940129241eSTao Ma 	struct ocfs2_extent_list *el = &xv->xr_list;
58950129241eSTao Ma 	unsigned int ext_flags;
58960129241eSTao Ma 
58970129241eSTao Ma 	cpos = 0;
58980129241eSTao Ma 	while (cpos < clusters) {
58990129241eSTao Ma 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
59000129241eSTao Ma 					       &num_clusters, el, &ext_flags);
590117caf955SJoseph Qi 		if (ret) {
590217caf955SJoseph Qi 			mlog_errno(ret);
590317caf955SJoseph Qi 			break;
590417caf955SJoseph Qi 		}
59050129241eSTao Ma 
59060129241eSTao Ma 		cpos += num_clusters;
59070129241eSTao Ma 		if ((ext_flags & OCFS2_EXT_REFCOUNTED))
59080129241eSTao Ma 			continue;
59090129241eSTao Ma 
59100129241eSTao Ma 		BUG_ON(!p_cluster);
59110129241eSTao Ma 
59120129241eSTao Ma 		ret = ocfs2_add_refcount_flag(inode, value_et,
59130129241eSTao Ma 					      ref_ci, ref_root_bh,
59140129241eSTao Ma 					      cpos - num_clusters,
59150129241eSTao Ma 					      p_cluster, num_clusters,
59160129241eSTao Ma 					      dealloc, refcount);
59170129241eSTao Ma 		if (ret) {
59180129241eSTao Ma 			mlog_errno(ret);
59190129241eSTao Ma 			break;
59200129241eSTao Ma 		}
59210129241eSTao Ma 	}
59220129241eSTao Ma 
59230129241eSTao Ma 	return ret;
59240129241eSTao Ma }
59250129241eSTao Ma 
59260129241eSTao Ma /*
59270129241eSTao Ma  * Given a normal ocfs2_xattr_header, refcount all the entries which
59280129241eSTao Ma  * have value stored outside.
59290129241eSTao Ma  * Used for xattrs stored in inode and ocfs2_xattr_block.
59300129241eSTao Ma  */
ocfs2_xattr_attach_refcount_normal(struct inode * inode,struct ocfs2_xattr_value_buf * vb,struct ocfs2_xattr_header * header,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)59310129241eSTao Ma static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
59320129241eSTao Ma 				struct ocfs2_xattr_value_buf *vb,
59330129241eSTao Ma 				struct ocfs2_xattr_header *header,
59340129241eSTao Ma 				struct ocfs2_caching_info *ref_ci,
59350129241eSTao Ma 				struct buffer_head *ref_root_bh,
59360129241eSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
59370129241eSTao Ma {
59380129241eSTao Ma 
59390129241eSTao Ma 	struct ocfs2_xattr_entry *xe;
59400129241eSTao Ma 	struct ocfs2_xattr_value_root *xv;
59410129241eSTao Ma 	struct ocfs2_extent_tree et;
59420129241eSTao Ma 	int i, ret = 0;
59430129241eSTao Ma 
59440129241eSTao Ma 	for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
59450129241eSTao Ma 		xe = &header->xh_entries[i];
59460129241eSTao Ma 
59470129241eSTao Ma 		if (ocfs2_xattr_is_local(xe))
59480129241eSTao Ma 			continue;
59490129241eSTao Ma 
59500129241eSTao Ma 		xv = (struct ocfs2_xattr_value_root *)((void *)header +
59510129241eSTao Ma 			le16_to_cpu(xe->xe_name_offset) +
59520129241eSTao Ma 			OCFS2_XATTR_SIZE(xe->xe_name_len));
59530129241eSTao Ma 
59540129241eSTao Ma 		vb->vb_xv = xv;
59550129241eSTao Ma 		ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
59560129241eSTao Ma 
59570129241eSTao Ma 		ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
59580129241eSTao Ma 							ref_ci, ref_root_bh,
59590129241eSTao Ma 							dealloc, NULL);
59600129241eSTao Ma 		if (ret) {
59610129241eSTao Ma 			mlog_errno(ret);
59620129241eSTao Ma 			break;
59630129241eSTao Ma 		}
59640129241eSTao Ma 	}
59650129241eSTao Ma 
59660129241eSTao Ma 	return ret;
59670129241eSTao Ma }
59680129241eSTao Ma 
ocfs2_xattr_inline_attach_refcount(struct inode * inode,struct buffer_head * fe_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)59690129241eSTao Ma static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
59700129241eSTao Ma 				struct buffer_head *fe_bh,
59710129241eSTao Ma 				struct ocfs2_caching_info *ref_ci,
59720129241eSTao Ma 				struct buffer_head *ref_root_bh,
59730129241eSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
59740129241eSTao Ma {
59750129241eSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
59760129241eSTao Ma 	struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
59770129241eSTao Ma 				(fe_bh->b_data + inode->i_sb->s_blocksize -
59780129241eSTao Ma 				le16_to_cpu(di->i_xattr_inline_size));
59790129241eSTao Ma 	struct ocfs2_xattr_value_buf vb = {
59800129241eSTao Ma 		.vb_bh = fe_bh,
59810129241eSTao Ma 		.vb_access = ocfs2_journal_access_di,
59820129241eSTao Ma 	};
59830129241eSTao Ma 
59840129241eSTao Ma 	return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
59850129241eSTao Ma 						  ref_ci, ref_root_bh, dealloc);
59860129241eSTao Ma }
59870129241eSTao Ma 
59880129241eSTao Ma struct ocfs2_xattr_tree_value_refcount_para {
59890129241eSTao Ma 	struct ocfs2_caching_info *ref_ci;
59900129241eSTao Ma 	struct buffer_head *ref_root_bh;
59910129241eSTao Ma 	struct ocfs2_cached_dealloc_ctxt *dealloc;
59920129241eSTao Ma };
59930129241eSTao Ma 
ocfs2_get_xattr_tree_value_root(struct super_block * sb,struct ocfs2_xattr_bucket * bucket,int offset,struct ocfs2_xattr_value_root ** xv,struct buffer_head ** bh)59940129241eSTao Ma static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
59950129241eSTao Ma 					   struct ocfs2_xattr_bucket *bucket,
59960129241eSTao Ma 					   int offset,
59970129241eSTao Ma 					   struct ocfs2_xattr_value_root **xv,
59980129241eSTao Ma 					   struct buffer_head **bh)
59990129241eSTao Ma {
60000129241eSTao Ma 	int ret, block_off, name_offset;
60010129241eSTao Ma 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
60020129241eSTao Ma 	struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
60030129241eSTao Ma 	void *base;
60040129241eSTao Ma 
60050129241eSTao Ma 	ret = ocfs2_xattr_bucket_get_name_value(sb,
60060129241eSTao Ma 						bucket_xh(bucket),
60070129241eSTao Ma 						offset,
60080129241eSTao Ma 						&block_off,
60090129241eSTao Ma 						&name_offset);
60100129241eSTao Ma 	if (ret) {
60110129241eSTao Ma 		mlog_errno(ret);
60120129241eSTao Ma 		goto out;
60130129241eSTao Ma 	}
60140129241eSTao Ma 
60150129241eSTao Ma 	base = bucket_block(bucket, block_off);
60160129241eSTao Ma 
60170129241eSTao Ma 	*xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
60180129241eSTao Ma 			 OCFS2_XATTR_SIZE(xe->xe_name_len));
60190129241eSTao Ma 
60200129241eSTao Ma 	if (bh)
60210129241eSTao Ma 		*bh = bucket->bu_bhs[block_off];
60220129241eSTao Ma out:
60230129241eSTao Ma 	return ret;
60240129241eSTao Ma }
60250129241eSTao Ma 
60260129241eSTao Ma /*
60270129241eSTao Ma  * For a given xattr bucket, refcount all the entries which
60280129241eSTao Ma  * have value stored outside.
60290129241eSTao Ma  */
ocfs2_xattr_bucket_value_refcount(struct inode * inode,struct ocfs2_xattr_bucket * bucket,void * para)60300129241eSTao Ma static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
60310129241eSTao Ma 					     struct ocfs2_xattr_bucket *bucket,
60320129241eSTao Ma 					     void *para)
60330129241eSTao Ma {
60340129241eSTao Ma 	int i, ret = 0;
60350129241eSTao Ma 	struct ocfs2_extent_tree et;
60360129241eSTao Ma 	struct ocfs2_xattr_tree_value_refcount_para *ref =
60370129241eSTao Ma 			(struct ocfs2_xattr_tree_value_refcount_para *)para;
60380129241eSTao Ma 	struct ocfs2_xattr_header *xh =
60390129241eSTao Ma 			(struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
60400129241eSTao Ma 	struct ocfs2_xattr_entry *xe;
60410129241eSTao Ma 	struct ocfs2_xattr_value_buf vb = {
60420129241eSTao Ma 		.vb_access = ocfs2_journal_access,
60430129241eSTao Ma 	};
60440129241eSTao Ma 	struct ocfs2_post_refcount refcount = {
60450129241eSTao Ma 		.credits = bucket->bu_blocks,
60460129241eSTao Ma 		.para = bucket,
60470129241eSTao Ma 		.func = ocfs2_xattr_bucket_post_refcount,
60480129241eSTao Ma 	};
60490129241eSTao Ma 	struct ocfs2_post_refcount *p = NULL;
60500129241eSTao Ma 
60510129241eSTao Ma 	/* We only need post_refcount if we support metaecc. */
60520129241eSTao Ma 	if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
60530129241eSTao Ma 		p = &refcount;
60540129241eSTao Ma 
6055402b4183STao Ma 	trace_ocfs2_xattr_bucket_value_refcount(
60560129241eSTao Ma 				(unsigned long long)bucket_blkno(bucket),
60570129241eSTao Ma 				le16_to_cpu(xh->xh_count));
60580129241eSTao Ma 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
60590129241eSTao Ma 		xe = &xh->xh_entries[i];
60600129241eSTao Ma 
60610129241eSTao Ma 		if (ocfs2_xattr_is_local(xe))
60620129241eSTao Ma 			continue;
60630129241eSTao Ma 
60640129241eSTao Ma 		ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
60650129241eSTao Ma 						      &vb.vb_xv, &vb.vb_bh);
60660129241eSTao Ma 		if (ret) {
60670129241eSTao Ma 			mlog_errno(ret);
60680129241eSTao Ma 			break;
60690129241eSTao Ma 		}
60700129241eSTao Ma 
60710129241eSTao Ma 		ocfs2_init_xattr_value_extent_tree(&et,
60720129241eSTao Ma 						   INODE_CACHE(inode), &vb);
60730129241eSTao Ma 
60740129241eSTao Ma 		ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
60750129241eSTao Ma 							&et, ref->ref_ci,
60760129241eSTao Ma 							ref->ref_root_bh,
60770129241eSTao Ma 							ref->dealloc, p);
60780129241eSTao Ma 		if (ret) {
60790129241eSTao Ma 			mlog_errno(ret);
60800129241eSTao Ma 			break;
60810129241eSTao Ma 		}
60820129241eSTao Ma 	}
60830129241eSTao Ma 
60840129241eSTao Ma 	return ret;
60850129241eSTao Ma 
60860129241eSTao Ma }
60870129241eSTao Ma 
ocfs2_refcount_xattr_tree_rec(struct inode * inode,struct buffer_head * root_bh,u64 blkno,u32 cpos,u32 len,void * para)60880129241eSTao Ma static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
60890129241eSTao Ma 				     struct buffer_head *root_bh,
60900129241eSTao Ma 				     u64 blkno, u32 cpos, u32 len, void *para)
60910129241eSTao Ma {
60920129241eSTao Ma 	return ocfs2_iterate_xattr_buckets(inode, blkno, len,
60930129241eSTao Ma 					   ocfs2_xattr_bucket_value_refcount,
60940129241eSTao Ma 					   para);
60950129241eSTao Ma }
60960129241eSTao Ma 
ocfs2_xattr_block_attach_refcount(struct inode * inode,struct buffer_head * blk_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)60970129241eSTao Ma static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
60980129241eSTao Ma 				struct buffer_head *blk_bh,
60990129241eSTao Ma 				struct ocfs2_caching_info *ref_ci,
61000129241eSTao Ma 				struct buffer_head *ref_root_bh,
61010129241eSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
61020129241eSTao Ma {
61030129241eSTao Ma 	int ret = 0;
61040129241eSTao Ma 	struct ocfs2_xattr_block *xb =
61050129241eSTao Ma 				(struct ocfs2_xattr_block *)blk_bh->b_data;
61060129241eSTao Ma 
61070129241eSTao Ma 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
61080129241eSTao Ma 		struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
61090129241eSTao Ma 		struct ocfs2_xattr_value_buf vb = {
61100129241eSTao Ma 			.vb_bh = blk_bh,
61110129241eSTao Ma 			.vb_access = ocfs2_journal_access_xb,
61120129241eSTao Ma 		};
61130129241eSTao Ma 
61140129241eSTao Ma 		ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
61150129241eSTao Ma 							 ref_ci, ref_root_bh,
61160129241eSTao Ma 							 dealloc);
61170129241eSTao Ma 	} else {
61180129241eSTao Ma 		struct ocfs2_xattr_tree_value_refcount_para para = {
61190129241eSTao Ma 			.ref_ci = ref_ci,
61200129241eSTao Ma 			.ref_root_bh = ref_root_bh,
61210129241eSTao Ma 			.dealloc = dealloc,
61220129241eSTao Ma 		};
61230129241eSTao Ma 
61240129241eSTao Ma 		ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
61250129241eSTao Ma 						ocfs2_refcount_xattr_tree_rec,
61260129241eSTao Ma 						&para);
61270129241eSTao Ma 	}
61280129241eSTao Ma 
61290129241eSTao Ma 	return ret;
61300129241eSTao Ma }
61310129241eSTao Ma 
ocfs2_xattr_attach_refcount_tree(struct inode * inode,struct buffer_head * fe_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)61320129241eSTao Ma int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
61330129241eSTao Ma 				     struct buffer_head *fe_bh,
61340129241eSTao Ma 				     struct ocfs2_caching_info *ref_ci,
61350129241eSTao Ma 				     struct buffer_head *ref_root_bh,
61360129241eSTao Ma 				     struct ocfs2_cached_dealloc_ctxt *dealloc)
61370129241eSTao Ma {
61380129241eSTao Ma 	int ret = 0;
61390129241eSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
61400129241eSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
61410129241eSTao Ma 	struct buffer_head *blk_bh = NULL;
61420129241eSTao Ma 
61430129241eSTao Ma 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
61440129241eSTao Ma 		ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
61450129241eSTao Ma 							 ref_ci, ref_root_bh,
61460129241eSTao Ma 							 dealloc);
61470129241eSTao Ma 		if (ret) {
61480129241eSTao Ma 			mlog_errno(ret);
61490129241eSTao Ma 			goto out;
61500129241eSTao Ma 		}
61510129241eSTao Ma 	}
61520129241eSTao Ma 
61530129241eSTao Ma 	if (!di->i_xattr_loc)
61540129241eSTao Ma 		goto out;
61550129241eSTao Ma 
61560129241eSTao Ma 	ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
61570129241eSTao Ma 				     &blk_bh);
61580129241eSTao Ma 	if (ret < 0) {
61590129241eSTao Ma 		mlog_errno(ret);
61600129241eSTao Ma 		goto out;
61610129241eSTao Ma 	}
61620129241eSTao Ma 
61630129241eSTao Ma 	ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
61640129241eSTao Ma 						ref_root_bh, dealloc);
61650129241eSTao Ma 	if (ret)
61660129241eSTao Ma 		mlog_errno(ret);
61670129241eSTao Ma 
61680129241eSTao Ma 	brelse(blk_bh);
61690129241eSTao Ma out:
61700129241eSTao Ma 
61710129241eSTao Ma 	return ret;
61720129241eSTao Ma }
61730129241eSTao Ma 
61740fe9b66cSTao Ma typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
61750129241eSTao Ma /*
61762999d12fSTao Ma  * Store the information we need in xattr reflink.
61772999d12fSTao Ma  * old_bh and new_bh are inode bh for the old and new inode.
61782999d12fSTao Ma  */
61792999d12fSTao Ma struct ocfs2_xattr_reflink {
61802999d12fSTao Ma 	struct inode *old_inode;
61812999d12fSTao Ma 	struct inode *new_inode;
61822999d12fSTao Ma 	struct buffer_head *old_bh;
61832999d12fSTao Ma 	struct buffer_head *new_bh;
61842999d12fSTao Ma 	struct ocfs2_caching_info *ref_ci;
61852999d12fSTao Ma 	struct buffer_head *ref_root_bh;
61862999d12fSTao Ma 	struct ocfs2_cached_dealloc_ctxt *dealloc;
61870fe9b66cSTao Ma 	should_xattr_reflinked *xattr_reflinked;
61882999d12fSTao Ma };
61892999d12fSTao Ma 
61902999d12fSTao Ma /*
61912999d12fSTao Ma  * Given a xattr header and xe offset,
61922999d12fSTao Ma  * return the proper xv and the corresponding bh.
61932999d12fSTao Ma  * xattr in inode, block and xattr tree have different implementaions.
61942999d12fSTao Ma  */
61952999d12fSTao Ma typedef int (get_xattr_value_root)(struct super_block *sb,
61962999d12fSTao Ma 				   struct buffer_head *bh,
61972999d12fSTao Ma 				   struct ocfs2_xattr_header *xh,
61982999d12fSTao Ma 				   int offset,
61992999d12fSTao Ma 				   struct ocfs2_xattr_value_root **xv,
62002999d12fSTao Ma 				   struct buffer_head **ret_bh,
62012999d12fSTao Ma 				   void *para);
62022999d12fSTao Ma 
62032999d12fSTao Ma /*
62042999d12fSTao Ma  * Calculate all the xattr value root metadata stored in this xattr header and
62052999d12fSTao Ma  * credits we need if we create them from the scratch.
62062999d12fSTao Ma  * We use get_xattr_value_root so that all types of xattr container can use it.
62072999d12fSTao Ma  */
ocfs2_value_metas_in_xattr_header(struct super_block * sb,struct buffer_head * bh,struct ocfs2_xattr_header * xh,int * metas,int * credits,int * num_recs,get_xattr_value_root * func,void * para)62082999d12fSTao Ma static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
62092999d12fSTao Ma 					     struct buffer_head *bh,
62102999d12fSTao Ma 					     struct ocfs2_xattr_header *xh,
62112999d12fSTao Ma 					     int *metas, int *credits,
62122999d12fSTao Ma 					     int *num_recs,
62132999d12fSTao Ma 					     get_xattr_value_root *func,
62142999d12fSTao Ma 					     void *para)
62152999d12fSTao Ma {
62162999d12fSTao Ma 	int i, ret = 0;
62172999d12fSTao Ma 	struct ocfs2_xattr_value_root *xv;
62182999d12fSTao Ma 	struct ocfs2_xattr_entry *xe;
62192999d12fSTao Ma 
62202999d12fSTao Ma 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
62212999d12fSTao Ma 		xe = &xh->xh_entries[i];
62222999d12fSTao Ma 		if (ocfs2_xattr_is_local(xe))
62232999d12fSTao Ma 			continue;
62242999d12fSTao Ma 
62252999d12fSTao Ma 		ret = func(sb, bh, xh, i, &xv, NULL, para);
62262999d12fSTao Ma 		if (ret) {
62272999d12fSTao Ma 			mlog_errno(ret);
62282999d12fSTao Ma 			break;
62292999d12fSTao Ma 		}
62302999d12fSTao Ma 
62312999d12fSTao Ma 		*metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
62322999d12fSTao Ma 			  le16_to_cpu(xv->xr_list.l_next_free_rec);
62332999d12fSTao Ma 
62342999d12fSTao Ma 		*credits += ocfs2_calc_extend_credits(sb,
623506f9da6eSGoldwyn Rodrigues 						&def_xv.xv.xr_list);
62362999d12fSTao Ma 
62372999d12fSTao Ma 		/*
62382999d12fSTao Ma 		 * If the value is a tree with depth > 1, We don't go deep
62392999d12fSTao Ma 		 * to the extent block, so just calculate a maximum record num.
62402999d12fSTao Ma 		 */
62412999d12fSTao Ma 		if (!xv->xr_list.l_tree_depth)
62428ff6af88STao Ma 			*num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
62432999d12fSTao Ma 		else
62442999d12fSTao Ma 			*num_recs += ocfs2_clusters_for_bytes(sb,
62452999d12fSTao Ma 							      XATTR_SIZE_MAX);
62462999d12fSTao Ma 	}
62472999d12fSTao Ma 
62482999d12fSTao Ma 	return ret;
62492999d12fSTao Ma }
62502999d12fSTao Ma 
62512999d12fSTao Ma /* Used by xattr inode and block to return the right xv and buffer_head. */
ocfs2_get_xattr_value_root(struct super_block * sb,struct buffer_head * bh,struct ocfs2_xattr_header * xh,int offset,struct ocfs2_xattr_value_root ** xv,struct buffer_head ** ret_bh,void * para)62522999d12fSTao Ma static int ocfs2_get_xattr_value_root(struct super_block *sb,
62532999d12fSTao Ma 				      struct buffer_head *bh,
62542999d12fSTao Ma 				      struct ocfs2_xattr_header *xh,
62552999d12fSTao Ma 				      int offset,
62562999d12fSTao Ma 				      struct ocfs2_xattr_value_root **xv,
62572999d12fSTao Ma 				      struct buffer_head **ret_bh,
62582999d12fSTao Ma 				      void *para)
62592999d12fSTao Ma {
62602999d12fSTao Ma 	struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
62612999d12fSTao Ma 
62622999d12fSTao Ma 	*xv = (struct ocfs2_xattr_value_root *)((void *)xh +
62632999d12fSTao Ma 		le16_to_cpu(xe->xe_name_offset) +
62642999d12fSTao Ma 		OCFS2_XATTR_SIZE(xe->xe_name_len));
62652999d12fSTao Ma 
62662999d12fSTao Ma 	if (ret_bh)
62672999d12fSTao Ma 		*ret_bh = bh;
62682999d12fSTao Ma 
62692999d12fSTao Ma 	return 0;
62702999d12fSTao Ma }
62712999d12fSTao Ma 
62722999d12fSTao Ma /*
62732999d12fSTao Ma  * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
62742999d12fSTao Ma  * It is only used for inline xattr and xattr block.
62752999d12fSTao Ma  */
ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super * osb,struct ocfs2_xattr_header * xh,struct buffer_head * ref_root_bh,int * credits,struct ocfs2_alloc_context ** meta_ac)62762999d12fSTao Ma static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
62772999d12fSTao Ma 					struct ocfs2_xattr_header *xh,
62782999d12fSTao Ma 					struct buffer_head *ref_root_bh,
62792999d12fSTao Ma 					int *credits,
62802999d12fSTao Ma 					struct ocfs2_alloc_context **meta_ac)
62812999d12fSTao Ma {
62822999d12fSTao Ma 	int ret, meta_add = 0, num_recs = 0;
62832999d12fSTao Ma 	struct ocfs2_refcount_block *rb =
62842999d12fSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
62852999d12fSTao Ma 
62862999d12fSTao Ma 	*credits = 0;
62872999d12fSTao Ma 
62882999d12fSTao Ma 	ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
62892999d12fSTao Ma 						&meta_add, credits, &num_recs,
62902999d12fSTao Ma 						ocfs2_get_xattr_value_root,
62912999d12fSTao Ma 						NULL);
62922999d12fSTao Ma 	if (ret) {
62932999d12fSTao Ma 		mlog_errno(ret);
62942999d12fSTao Ma 		goto out;
62952999d12fSTao Ma 	}
62962999d12fSTao Ma 
62972999d12fSTao Ma 	/*
62982999d12fSTao Ma 	 * We need to add/modify num_recs in refcount tree, so just calculate
62992999d12fSTao Ma 	 * an approximate number we need for refcount tree change.
63002999d12fSTao Ma 	 * Sometimes we need to split the tree, and after split,  half recs
63012999d12fSTao Ma 	 * will be moved to the new block, and a new block can only provide
63022999d12fSTao Ma 	 * half number of recs. So we multiple new blocks by 2.
63032999d12fSTao Ma 	 */
63042999d12fSTao Ma 	num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
63052999d12fSTao Ma 	meta_add += num_recs;
63062999d12fSTao Ma 	*credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
63072999d12fSTao Ma 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
63082999d12fSTao Ma 		*credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
63092999d12fSTao Ma 			    le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
63102999d12fSTao Ma 	else
63112999d12fSTao Ma 		*credits += 1;
63122999d12fSTao Ma 
63132999d12fSTao Ma 	ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
63142999d12fSTao Ma 	if (ret)
63152999d12fSTao Ma 		mlog_errno(ret);
63162999d12fSTao Ma 
63172999d12fSTao Ma out:
63182999d12fSTao Ma 	return ret;
63192999d12fSTao Ma }
63202999d12fSTao Ma 
63212999d12fSTao Ma /*
63222999d12fSTao Ma  * Given a xattr header, reflink all the xattrs in this container.
63232999d12fSTao Ma  * It can be used for inode, block and bucket.
63242999d12fSTao Ma  *
63252999d12fSTao Ma  * NOTE:
63262999d12fSTao Ma  * Before we call this function, the caller has memcpy the xattr in
63272999d12fSTao Ma  * old_xh to the new_xh.
63280fe9b66cSTao Ma  *
63290fe9b66cSTao Ma  * If args.xattr_reflinked is set, call it to decide whether the xe should
63300fe9b66cSTao Ma  * be reflinked or not. If not, remove it from the new xattr header.
63312999d12fSTao Ma  */
ocfs2_reflink_xattr_header(handle_t * handle,struct ocfs2_xattr_reflink * args,struct buffer_head * old_bh,struct ocfs2_xattr_header * xh,struct buffer_head * new_bh,struct ocfs2_xattr_header * new_xh,struct ocfs2_xattr_value_buf * vb,struct ocfs2_alloc_context * meta_ac,get_xattr_value_root * func,void * para)63322999d12fSTao Ma static int ocfs2_reflink_xattr_header(handle_t *handle,
63332999d12fSTao Ma 				      struct ocfs2_xattr_reflink *args,
63342999d12fSTao Ma 				      struct buffer_head *old_bh,
63352999d12fSTao Ma 				      struct ocfs2_xattr_header *xh,
63362999d12fSTao Ma 				      struct buffer_head *new_bh,
63372999d12fSTao Ma 				      struct ocfs2_xattr_header *new_xh,
63382999d12fSTao Ma 				      struct ocfs2_xattr_value_buf *vb,
63392999d12fSTao Ma 				      struct ocfs2_alloc_context *meta_ac,
63402999d12fSTao Ma 				      get_xattr_value_root *func,
63412999d12fSTao Ma 				      void *para)
63422999d12fSTao Ma {
63430fe9b66cSTao Ma 	int ret = 0, i, j;
63442999d12fSTao Ma 	struct super_block *sb = args->old_inode->i_sb;
63452999d12fSTao Ma 	struct buffer_head *value_bh;
63460fe9b66cSTao Ma 	struct ocfs2_xattr_entry *xe, *last;
63472999d12fSTao Ma 	struct ocfs2_xattr_value_root *xv, *new_xv;
63482999d12fSTao Ma 	struct ocfs2_extent_tree data_et;
63492999d12fSTao Ma 	u32 clusters, cpos, p_cluster, num_clusters;
63502999d12fSTao Ma 	unsigned int ext_flags = 0;
63512999d12fSTao Ma 
6352402b4183STao Ma 	trace_ocfs2_reflink_xattr_header((unsigned long long)old_bh->b_blocknr,
6353402b4183STao Ma 					 le16_to_cpu(xh->xh_count));
63540fe9b66cSTao Ma 
63550fe9b66cSTao Ma 	last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
63560fe9b66cSTao Ma 	for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
63572999d12fSTao Ma 		xe = &xh->xh_entries[i];
63582999d12fSTao Ma 
63590fe9b66cSTao Ma 		if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
63600fe9b66cSTao Ma 			xe = &new_xh->xh_entries[j];
63610fe9b66cSTao Ma 
63620fe9b66cSTao Ma 			le16_add_cpu(&new_xh->xh_count, -1);
63630fe9b66cSTao Ma 			if (new_xh->xh_count) {
63640fe9b66cSTao Ma 				memmove(xe, xe + 1,
63650fe9b66cSTao Ma 					(void *)last - (void *)xe);
63660fe9b66cSTao Ma 				memset(last, 0,
63670fe9b66cSTao Ma 				       sizeof(struct ocfs2_xattr_entry));
63680fe9b66cSTao Ma 			}
63690fe9b66cSTao Ma 
63700fe9b66cSTao Ma 			/*
63710fe9b66cSTao Ma 			 * We don't want j to increase in the next round since
63720fe9b66cSTao Ma 			 * it is already moved ahead.
63730fe9b66cSTao Ma 			 */
63740fe9b66cSTao Ma 			j--;
63750fe9b66cSTao Ma 			continue;
63760fe9b66cSTao Ma 		}
63770fe9b66cSTao Ma 
63782999d12fSTao Ma 		if (ocfs2_xattr_is_local(xe))
63792999d12fSTao Ma 			continue;
63802999d12fSTao Ma 
63812999d12fSTao Ma 		ret = func(sb, old_bh, xh, i, &xv, NULL, para);
63822999d12fSTao Ma 		if (ret) {
63832999d12fSTao Ma 			mlog_errno(ret);
63842999d12fSTao Ma 			break;
63852999d12fSTao Ma 		}
63862999d12fSTao Ma 
63870fe9b66cSTao Ma 		ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
63882999d12fSTao Ma 		if (ret) {
63892999d12fSTao Ma 			mlog_errno(ret);
63902999d12fSTao Ma 			break;
63912999d12fSTao Ma 		}
63922999d12fSTao Ma 
63932999d12fSTao Ma 		/*
63942999d12fSTao Ma 		 * For the xattr which has l_tree_depth = 0, all the extent
63952999d12fSTao Ma 		 * recs have already be copied to the new xh with the
63962999d12fSTao Ma 		 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
63972999d12fSTao Ma 		 * increase the refount count int the refcount tree.
63982999d12fSTao Ma 		 *
63992999d12fSTao Ma 		 * For the xattr which has l_tree_depth > 0, we need
64002999d12fSTao Ma 		 * to initialize it to the empty default value root,
64012999d12fSTao Ma 		 * and then insert the extents one by one.
64022999d12fSTao Ma 		 */
64032999d12fSTao Ma 		if (xv->xr_list.l_tree_depth) {
640432ed0bd7Salex chen 			memcpy(new_xv, &def_xv, OCFS2_XATTR_ROOT_SIZE);
64052999d12fSTao Ma 			vb->vb_xv = new_xv;
64062999d12fSTao Ma 			vb->vb_bh = value_bh;
64072999d12fSTao Ma 			ocfs2_init_xattr_value_extent_tree(&data_et,
64082999d12fSTao Ma 					INODE_CACHE(args->new_inode), vb);
64092999d12fSTao Ma 		}
64102999d12fSTao Ma 
64112999d12fSTao Ma 		clusters = le32_to_cpu(xv->xr_clusters);
64122999d12fSTao Ma 		cpos = 0;
64132999d12fSTao Ma 		while (cpos < clusters) {
64142999d12fSTao Ma 			ret = ocfs2_xattr_get_clusters(args->old_inode,
64152999d12fSTao Ma 						       cpos,
64162999d12fSTao Ma 						       &p_cluster,
64172999d12fSTao Ma 						       &num_clusters,
64182999d12fSTao Ma 						       &xv->xr_list,
64192999d12fSTao Ma 						       &ext_flags);
64202999d12fSTao Ma 			if (ret) {
64212999d12fSTao Ma 				mlog_errno(ret);
64222999d12fSTao Ma 				goto out;
64232999d12fSTao Ma 			}
64242999d12fSTao Ma 
64252999d12fSTao Ma 			BUG_ON(!p_cluster);
64262999d12fSTao Ma 
64272999d12fSTao Ma 			if (xv->xr_list.l_tree_depth) {
64282999d12fSTao Ma 				ret = ocfs2_insert_extent(handle,
64292999d12fSTao Ma 						&data_et, cpos,
64302999d12fSTao Ma 						ocfs2_clusters_to_blocks(
64312999d12fSTao Ma 							args->old_inode->i_sb,
64322999d12fSTao Ma 							p_cluster),
64332999d12fSTao Ma 						num_clusters, ext_flags,
64342999d12fSTao Ma 						meta_ac);
64352999d12fSTao Ma 				if (ret) {
64362999d12fSTao Ma 					mlog_errno(ret);
64372999d12fSTao Ma 					goto out;
64382999d12fSTao Ma 				}
64392999d12fSTao Ma 			}
64402999d12fSTao Ma 
64412999d12fSTao Ma 			ret = ocfs2_increase_refcount(handle, args->ref_ci,
64422999d12fSTao Ma 						      args->ref_root_bh,
64432999d12fSTao Ma 						      p_cluster, num_clusters,
64442999d12fSTao Ma 						      meta_ac, args->dealloc);
64452999d12fSTao Ma 			if (ret) {
64462999d12fSTao Ma 				mlog_errno(ret);
64472999d12fSTao Ma 				goto out;
64482999d12fSTao Ma 			}
64492999d12fSTao Ma 
64502999d12fSTao Ma 			cpos += num_clusters;
64512999d12fSTao Ma 		}
64522999d12fSTao Ma 	}
64532999d12fSTao Ma 
64542999d12fSTao Ma out:
64552999d12fSTao Ma 	return ret;
64562999d12fSTao Ma }
64572999d12fSTao Ma 
ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink * args)64582999d12fSTao Ma static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
64592999d12fSTao Ma {
64602999d12fSTao Ma 	int ret = 0, credits = 0;
64612999d12fSTao Ma 	handle_t *handle;
64622999d12fSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
64632999d12fSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
64642999d12fSTao Ma 	int inline_size = le16_to_cpu(di->i_xattr_inline_size);
64652999d12fSTao Ma 	int header_off = osb->sb->s_blocksize - inline_size;
64662999d12fSTao Ma 	struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
64672999d12fSTao Ma 					(args->old_bh->b_data + header_off);
64682999d12fSTao Ma 	struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
64692999d12fSTao Ma 					(args->new_bh->b_data + header_off);
64702999d12fSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
64712999d12fSTao Ma 	struct ocfs2_inode_info *new_oi;
64722999d12fSTao Ma 	struct ocfs2_dinode *new_di;
64732999d12fSTao Ma 	struct ocfs2_xattr_value_buf vb = {
64742999d12fSTao Ma 		.vb_bh = args->new_bh,
64752999d12fSTao Ma 		.vb_access = ocfs2_journal_access_di,
64762999d12fSTao Ma 	};
64772999d12fSTao Ma 
64782999d12fSTao Ma 	ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
64792999d12fSTao Ma 						  &credits, &meta_ac);
64802999d12fSTao Ma 	if (ret) {
64812999d12fSTao Ma 		mlog_errno(ret);
64822999d12fSTao Ma 		goto out;
64832999d12fSTao Ma 	}
64842999d12fSTao Ma 
64852999d12fSTao Ma 	handle = ocfs2_start_trans(osb, credits);
64862999d12fSTao Ma 	if (IS_ERR(handle)) {
64872999d12fSTao Ma 		ret = PTR_ERR(handle);
64882999d12fSTao Ma 		mlog_errno(ret);
64892999d12fSTao Ma 		goto out;
64902999d12fSTao Ma 	}
64912999d12fSTao Ma 
64922999d12fSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
64932999d12fSTao Ma 				      args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
64942999d12fSTao Ma 	if (ret) {
64952999d12fSTao Ma 		mlog_errno(ret);
64962999d12fSTao Ma 		goto out_commit;
64972999d12fSTao Ma 	}
64982999d12fSTao Ma 
64992999d12fSTao Ma 	memcpy(args->new_bh->b_data + header_off,
65002999d12fSTao Ma 	       args->old_bh->b_data + header_off, inline_size);
65012999d12fSTao Ma 
65022999d12fSTao Ma 	new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
65032999d12fSTao Ma 	new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
65042999d12fSTao Ma 
65052999d12fSTao Ma 	ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
65062999d12fSTao Ma 					 args->new_bh, new_xh, &vb, meta_ac,
65072999d12fSTao Ma 					 ocfs2_get_xattr_value_root, NULL);
65082999d12fSTao Ma 	if (ret) {
65092999d12fSTao Ma 		mlog_errno(ret);
65102999d12fSTao Ma 		goto out_commit;
65112999d12fSTao Ma 	}
65122999d12fSTao Ma 
65132999d12fSTao Ma 	new_oi = OCFS2_I(args->new_inode);
6514ef962df0SJunxiao Bi 	/*
6515ef962df0SJunxiao Bi 	 * Adjust extent record count to reserve space for extended attribute.
6516ef962df0SJunxiao Bi 	 * Inline data count had been adjusted in ocfs2_duplicate_inline_data().
6517ef962df0SJunxiao Bi 	 */
6518ef962df0SJunxiao Bi 	if (!(new_oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) &&
6519ef962df0SJunxiao Bi 	    !(ocfs2_inode_is_fast_symlink(args->new_inode))) {
6520ef962df0SJunxiao Bi 		struct ocfs2_extent_list *el = &new_di->id2.i_list;
6521ef962df0SJunxiao Bi 		le16_add_cpu(&el->l_count, -(inline_size /
6522ef962df0SJunxiao Bi 					sizeof(struct ocfs2_extent_rec)));
6523ef962df0SJunxiao Bi 	}
65242999d12fSTao Ma 	spin_lock(&new_oi->ip_lock);
65252999d12fSTao Ma 	new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
65262999d12fSTao Ma 	new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
65272999d12fSTao Ma 	spin_unlock(&new_oi->ip_lock);
65282999d12fSTao Ma 
65292999d12fSTao Ma 	ocfs2_journal_dirty(handle, args->new_bh);
65302999d12fSTao Ma 
65312999d12fSTao Ma out_commit:
65322999d12fSTao Ma 	ocfs2_commit_trans(osb, handle);
65332999d12fSTao Ma 
65342999d12fSTao Ma out:
65352999d12fSTao Ma 	if (meta_ac)
65362999d12fSTao Ma 		ocfs2_free_alloc_context(meta_ac);
65372999d12fSTao Ma 	return ret;
65382999d12fSTao Ma }
65392999d12fSTao Ma 
ocfs2_create_empty_xattr_block(struct inode * inode,struct buffer_head * fe_bh,struct buffer_head ** ret_bh,int indexed)65402999d12fSTao Ma static int ocfs2_create_empty_xattr_block(struct inode *inode,
65412999d12fSTao Ma 					  struct buffer_head *fe_bh,
65422999d12fSTao Ma 					  struct buffer_head **ret_bh,
65432999d12fSTao Ma 					  int indexed)
65442999d12fSTao Ma {
65452999d12fSTao Ma 	int ret;
65462999d12fSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6547b2317968STao Ma 	struct ocfs2_xattr_set_ctxt ctxt;
65482999d12fSTao Ma 
6549b2317968STao Ma 	memset(&ctxt, 0, sizeof(ctxt));
6550b2317968STao Ma 	ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
65512999d12fSTao Ma 	if (ret < 0) {
65522999d12fSTao Ma 		mlog_errno(ret);
65532999d12fSTao Ma 		return ret;
65542999d12fSTao Ma 	}
65552999d12fSTao Ma 
6556d3981544SJoel Becker 	ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6557d3981544SJoel Becker 	if (IS_ERR(ctxt.handle)) {
6558d3981544SJoel Becker 		ret = PTR_ERR(ctxt.handle);
65592999d12fSTao Ma 		mlog_errno(ret);
65602999d12fSTao Ma 		goto out;
65612999d12fSTao Ma 	}
65622999d12fSTao Ma 
6563402b4183STao Ma 	trace_ocfs2_create_empty_xattr_block(
65642999d12fSTao Ma 				(unsigned long long)fe_bh->b_blocknr, indexed);
6565d3981544SJoel Becker 	ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6566d3981544SJoel Becker 				       ret_bh);
65672999d12fSTao Ma 	if (ret)
65682999d12fSTao Ma 		mlog_errno(ret);
65692999d12fSTao Ma 
6570d3981544SJoel Becker 	ocfs2_commit_trans(osb, ctxt.handle);
65712999d12fSTao Ma out:
6572b2317968STao Ma 	ocfs2_free_alloc_context(ctxt.meta_ac);
65732999d12fSTao Ma 	return ret;
65742999d12fSTao Ma }
65752999d12fSTao Ma 
ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink * args,struct buffer_head * blk_bh,struct buffer_head * new_blk_bh)65762999d12fSTao Ma static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
65772999d12fSTao Ma 				     struct buffer_head *blk_bh,
65782999d12fSTao Ma 				     struct buffer_head *new_blk_bh)
65792999d12fSTao Ma {
65802999d12fSTao Ma 	int ret = 0, credits = 0;
65812999d12fSTao Ma 	handle_t *handle;
65822999d12fSTao Ma 	struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
65832999d12fSTao Ma 	struct ocfs2_dinode *new_di;
65842999d12fSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
65852999d12fSTao Ma 	int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
65862999d12fSTao Ma 	struct ocfs2_xattr_block *xb =
65872999d12fSTao Ma 			(struct ocfs2_xattr_block *)blk_bh->b_data;
65882999d12fSTao Ma 	struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
65892999d12fSTao Ma 	struct ocfs2_xattr_block *new_xb =
65902999d12fSTao Ma 			(struct ocfs2_xattr_block *)new_blk_bh->b_data;
65912999d12fSTao Ma 	struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
65922999d12fSTao Ma 	struct ocfs2_alloc_context *meta_ac;
65932999d12fSTao Ma 	struct ocfs2_xattr_value_buf vb = {
65942999d12fSTao Ma 		.vb_bh = new_blk_bh,
65952999d12fSTao Ma 		.vb_access = ocfs2_journal_access_xb,
65962999d12fSTao Ma 	};
65972999d12fSTao Ma 
65982999d12fSTao Ma 	ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
65992999d12fSTao Ma 						  &credits, &meta_ac);
66002999d12fSTao Ma 	if (ret) {
66012999d12fSTao Ma 		mlog_errno(ret);
66022999d12fSTao Ma 		return ret;
66032999d12fSTao Ma 	}
66042999d12fSTao Ma 
66052999d12fSTao Ma 	/* One more credits in case we need to add xattr flags in new inode. */
66062999d12fSTao Ma 	handle = ocfs2_start_trans(osb, credits + 1);
66072999d12fSTao Ma 	if (IS_ERR(handle)) {
66082999d12fSTao Ma 		ret = PTR_ERR(handle);
66092999d12fSTao Ma 		mlog_errno(ret);
66102999d12fSTao Ma 		goto out;
66112999d12fSTao Ma 	}
66122999d12fSTao Ma 
66132999d12fSTao Ma 	if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
66142999d12fSTao Ma 		ret = ocfs2_journal_access_di(handle,
66152999d12fSTao Ma 					      INODE_CACHE(args->new_inode),
66162999d12fSTao Ma 					      args->new_bh,
66172999d12fSTao Ma 					      OCFS2_JOURNAL_ACCESS_WRITE);
66182999d12fSTao Ma 		if (ret) {
66192999d12fSTao Ma 			mlog_errno(ret);
66202999d12fSTao Ma 			goto out_commit;
66212999d12fSTao Ma 		}
66222999d12fSTao Ma 	}
66232999d12fSTao Ma 
66242999d12fSTao Ma 	ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
66252999d12fSTao Ma 				      new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
66262999d12fSTao Ma 	if (ret) {
66272999d12fSTao Ma 		mlog_errno(ret);
66282999d12fSTao Ma 		goto out_commit;
66292999d12fSTao Ma 	}
66302999d12fSTao Ma 
66312999d12fSTao Ma 	memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
66322999d12fSTao Ma 	       osb->sb->s_blocksize - header_off);
66332999d12fSTao Ma 
66342999d12fSTao Ma 	ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
66352999d12fSTao Ma 					 new_blk_bh, new_xh, &vb, meta_ac,
66362999d12fSTao Ma 					 ocfs2_get_xattr_value_root, NULL);
66372999d12fSTao Ma 	if (ret) {
66382999d12fSTao Ma 		mlog_errno(ret);
66392999d12fSTao Ma 		goto out_commit;
66402999d12fSTao Ma 	}
66412999d12fSTao Ma 
66422999d12fSTao Ma 	ocfs2_journal_dirty(handle, new_blk_bh);
66432999d12fSTao Ma 
66442999d12fSTao Ma 	if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
66452999d12fSTao Ma 		new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
66462999d12fSTao Ma 		spin_lock(&new_oi->ip_lock);
66472999d12fSTao Ma 		new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
66482999d12fSTao Ma 		new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
66492999d12fSTao Ma 		spin_unlock(&new_oi->ip_lock);
66502999d12fSTao Ma 
66512999d12fSTao Ma 		ocfs2_journal_dirty(handle, args->new_bh);
66522999d12fSTao Ma 	}
66532999d12fSTao Ma 
66542999d12fSTao Ma out_commit:
66552999d12fSTao Ma 	ocfs2_commit_trans(osb, handle);
66562999d12fSTao Ma 
66572999d12fSTao Ma out:
66582999d12fSTao Ma 	ocfs2_free_alloc_context(meta_ac);
66592999d12fSTao Ma 	return ret;
66602999d12fSTao Ma }
66612999d12fSTao Ma 
66622999d12fSTao Ma struct ocfs2_reflink_xattr_tree_args {
66632999d12fSTao Ma 	struct ocfs2_xattr_reflink *reflink;
66642999d12fSTao Ma 	struct buffer_head *old_blk_bh;
66652999d12fSTao Ma 	struct buffer_head *new_blk_bh;
66662999d12fSTao Ma 	struct ocfs2_xattr_bucket *old_bucket;
66672999d12fSTao Ma 	struct ocfs2_xattr_bucket *new_bucket;
66682999d12fSTao Ma };
66692999d12fSTao Ma 
66702999d12fSTao Ma /*
66712999d12fSTao Ma  * NOTE:
66722999d12fSTao Ma  * We have to handle the case that both old bucket and new bucket
66732999d12fSTao Ma  * will call this function to get the right ret_bh.
66742999d12fSTao Ma  * So The caller must give us the right bh.
66752999d12fSTao Ma  */
ocfs2_get_reflink_xattr_value_root(struct super_block * sb,struct buffer_head * bh,struct ocfs2_xattr_header * xh,int offset,struct ocfs2_xattr_value_root ** xv,struct buffer_head ** ret_bh,void * para)66762999d12fSTao Ma static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
66772999d12fSTao Ma 					struct buffer_head *bh,
66782999d12fSTao Ma 					struct ocfs2_xattr_header *xh,
66792999d12fSTao Ma 					int offset,
66802999d12fSTao Ma 					struct ocfs2_xattr_value_root **xv,
66812999d12fSTao Ma 					struct buffer_head **ret_bh,
66822999d12fSTao Ma 					void *para)
66832999d12fSTao Ma {
66842999d12fSTao Ma 	struct ocfs2_reflink_xattr_tree_args *args =
66852999d12fSTao Ma 			(struct ocfs2_reflink_xattr_tree_args *)para;
66862999d12fSTao Ma 	struct ocfs2_xattr_bucket *bucket;
66872999d12fSTao Ma 
66882999d12fSTao Ma 	if (bh == args->old_bucket->bu_bhs[0])
66892999d12fSTao Ma 		bucket = args->old_bucket;
66902999d12fSTao Ma 	else
66912999d12fSTao Ma 		bucket = args->new_bucket;
66922999d12fSTao Ma 
66932999d12fSTao Ma 	return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
66942999d12fSTao Ma 					       xv, ret_bh);
66952999d12fSTao Ma }
66962999d12fSTao Ma 
66972999d12fSTao Ma struct ocfs2_value_tree_metas {
66982999d12fSTao Ma 	int num_metas;
66992999d12fSTao Ma 	int credits;
67002999d12fSTao Ma 	int num_recs;
67012999d12fSTao Ma };
67022999d12fSTao Ma 
ocfs2_value_tree_metas_in_bucket(struct super_block * sb,struct buffer_head * bh,struct ocfs2_xattr_header * xh,int offset,struct ocfs2_xattr_value_root ** xv,struct buffer_head ** ret_bh,void * para)67032999d12fSTao Ma static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
67042999d12fSTao Ma 					struct buffer_head *bh,
67052999d12fSTao Ma 					struct ocfs2_xattr_header *xh,
67062999d12fSTao Ma 					int offset,
67072999d12fSTao Ma 					struct ocfs2_xattr_value_root **xv,
67082999d12fSTao Ma 					struct buffer_head **ret_bh,
67092999d12fSTao Ma 					void *para)
67102999d12fSTao Ma {
67112999d12fSTao Ma 	struct ocfs2_xattr_bucket *bucket =
67122999d12fSTao Ma 				(struct ocfs2_xattr_bucket *)para;
67132999d12fSTao Ma 
67142999d12fSTao Ma 	return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
67152999d12fSTao Ma 					       xv, ret_bh);
67162999d12fSTao Ma }
67172999d12fSTao Ma 
ocfs2_calc_value_tree_metas(struct inode * inode,struct ocfs2_xattr_bucket * bucket,void * para)67182999d12fSTao Ma static int ocfs2_calc_value_tree_metas(struct inode *inode,
67192999d12fSTao Ma 				      struct ocfs2_xattr_bucket *bucket,
67202999d12fSTao Ma 				      void *para)
67212999d12fSTao Ma {
67222999d12fSTao Ma 	struct ocfs2_value_tree_metas *metas =
67232999d12fSTao Ma 			(struct ocfs2_value_tree_metas *)para;
67242999d12fSTao Ma 	struct ocfs2_xattr_header *xh =
67252999d12fSTao Ma 			(struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
67262999d12fSTao Ma 
67272999d12fSTao Ma 	/* Add the credits for this bucket first. */
67282999d12fSTao Ma 	metas->credits += bucket->bu_blocks;
67292999d12fSTao Ma 	return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
67302999d12fSTao Ma 					xh, &metas->num_metas,
67312999d12fSTao Ma 					&metas->credits, &metas->num_recs,
67322999d12fSTao Ma 					ocfs2_value_tree_metas_in_bucket,
67332999d12fSTao Ma 					bucket);
67342999d12fSTao Ma }
67352999d12fSTao Ma 
67362999d12fSTao Ma /*
67372999d12fSTao Ma  * Given a xattr extent rec starting from blkno and having len clusters,
67382999d12fSTao Ma  * iterate all the buckets calculate how much metadata we need for reflinking
67392999d12fSTao Ma  * all the ocfs2_xattr_value_root and lock the allocators accordingly.
67402999d12fSTao Ma  */
ocfs2_lock_reflink_xattr_rec_allocators(struct ocfs2_reflink_xattr_tree_args * args,struct ocfs2_extent_tree * xt_et,u64 blkno,u32 len,int * credits,struct ocfs2_alloc_context ** meta_ac,struct ocfs2_alloc_context ** data_ac)67412999d12fSTao Ma static int ocfs2_lock_reflink_xattr_rec_allocators(
67422999d12fSTao Ma 				struct ocfs2_reflink_xattr_tree_args *args,
67432999d12fSTao Ma 				struct ocfs2_extent_tree *xt_et,
67442999d12fSTao Ma 				u64 blkno, u32 len, int *credits,
67452999d12fSTao Ma 				struct ocfs2_alloc_context **meta_ac,
67462999d12fSTao Ma 				struct ocfs2_alloc_context **data_ac)
67472999d12fSTao Ma {
67482999d12fSTao Ma 	int ret, num_free_extents;
67492999d12fSTao Ma 	struct ocfs2_value_tree_metas metas;
67502999d12fSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
67512999d12fSTao Ma 	struct ocfs2_refcount_block *rb;
67522999d12fSTao Ma 
67532999d12fSTao Ma 	memset(&metas, 0, sizeof(metas));
67542999d12fSTao Ma 
67552999d12fSTao Ma 	ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
67562999d12fSTao Ma 					  ocfs2_calc_value_tree_metas, &metas);
67572999d12fSTao Ma 	if (ret) {
67582999d12fSTao Ma 		mlog_errno(ret);
67592999d12fSTao Ma 		goto out;
67602999d12fSTao Ma 	}
67612999d12fSTao Ma 
67622999d12fSTao Ma 	*credits = metas.credits;
67632999d12fSTao Ma 
67642999d12fSTao Ma 	/*
67652999d12fSTao Ma 	 * Calculate we need for refcount tree change.
67662999d12fSTao Ma 	 *
67672999d12fSTao Ma 	 * We need to add/modify num_recs in refcount tree, so just calculate
67682999d12fSTao Ma 	 * an approximate number we need for refcount tree change.
67692999d12fSTao Ma 	 * Sometimes we need to split the tree, and after split,  half recs
67702999d12fSTao Ma 	 * will be moved to the new block, and a new block can only provide
67712999d12fSTao Ma 	 * half number of recs. So we multiple new blocks by 2.
67722999d12fSTao Ma 	 * In the end, we have to add credits for modifying the already
67732999d12fSTao Ma 	 * existed refcount block.
67742999d12fSTao Ma 	 */
67752999d12fSTao Ma 	rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
67762999d12fSTao Ma 	metas.num_recs =
67772999d12fSTao Ma 		(metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
67782999d12fSTao Ma 		 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
67792999d12fSTao Ma 	metas.num_metas += metas.num_recs;
67802999d12fSTao Ma 	*credits += metas.num_recs +
67812999d12fSTao Ma 		    metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
67822999d12fSTao Ma 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
67832999d12fSTao Ma 		*credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
67842999d12fSTao Ma 			    le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
67852999d12fSTao Ma 	else
67862999d12fSTao Ma 		*credits += 1;
67872999d12fSTao Ma 
67882999d12fSTao Ma 	/* count in the xattr tree change. */
6789964f14a0SJun Piao 	num_free_extents = ocfs2_num_free_extents(xt_et);
67902999d12fSTao Ma 	if (num_free_extents < 0) {
67912999d12fSTao Ma 		ret = num_free_extents;
67922999d12fSTao Ma 		mlog_errno(ret);
67932999d12fSTao Ma 		goto out;
67942999d12fSTao Ma 	}
67952999d12fSTao Ma 
67962999d12fSTao Ma 	if (num_free_extents < len)
67972999d12fSTao Ma 		metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
67982999d12fSTao Ma 
67992999d12fSTao Ma 	*credits += ocfs2_calc_extend_credits(osb->sb,
680006f9da6eSGoldwyn Rodrigues 					      xt_et->et_root_el);
68012999d12fSTao Ma 
68022999d12fSTao Ma 	if (metas.num_metas) {
68032999d12fSTao Ma 		ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
68042999d12fSTao Ma 							meta_ac);
68052999d12fSTao Ma 		if (ret) {
68062999d12fSTao Ma 			mlog_errno(ret);
68072999d12fSTao Ma 			goto out;
68082999d12fSTao Ma 		}
68092999d12fSTao Ma 	}
68102999d12fSTao Ma 
68112999d12fSTao Ma 	if (len) {
68122999d12fSTao Ma 		ret = ocfs2_reserve_clusters(osb, len, data_ac);
68132999d12fSTao Ma 		if (ret)
68142999d12fSTao Ma 			mlog_errno(ret);
68152999d12fSTao Ma 	}
68162999d12fSTao Ma out:
68172999d12fSTao Ma 	if (ret) {
68182999d12fSTao Ma 		if (*meta_ac) {
68192999d12fSTao Ma 			ocfs2_free_alloc_context(*meta_ac);
68206cae6d31SJoseph Qi 			*meta_ac = NULL;
68212999d12fSTao Ma 		}
68222999d12fSTao Ma 	}
68232999d12fSTao Ma 
68242999d12fSTao Ma 	return ret;
68252999d12fSTao Ma }
68262999d12fSTao Ma 
ocfs2_reflink_xattr_bucket(handle_t * handle,u64 blkno,u64 new_blkno,u32 clusters,u32 * cpos,int num_buckets,struct ocfs2_alloc_context * meta_ac,struct ocfs2_alloc_context * data_ac,struct ocfs2_reflink_xattr_tree_args * args)6827121a39bbSTao Ma static int ocfs2_reflink_xattr_bucket(handle_t *handle,
68282999d12fSTao Ma 				u64 blkno, u64 new_blkno, u32 clusters,
6829121a39bbSTao Ma 				u32 *cpos, int num_buckets,
68302999d12fSTao Ma 				struct ocfs2_alloc_context *meta_ac,
68312999d12fSTao Ma 				struct ocfs2_alloc_context *data_ac,
68322999d12fSTao Ma 				struct ocfs2_reflink_xattr_tree_args *args)
68332999d12fSTao Ma {
68342999d12fSTao Ma 	int i, j, ret = 0;
68352999d12fSTao Ma 	struct super_block *sb = args->reflink->old_inode->i_sb;
68362999d12fSTao Ma 	int bpb = args->old_bucket->bu_blocks;
68372999d12fSTao Ma 	struct ocfs2_xattr_value_buf vb = {
68382999d12fSTao Ma 		.vb_access = ocfs2_journal_access,
68392999d12fSTao Ma 	};
68402999d12fSTao Ma 
68412999d12fSTao Ma 	for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
68422999d12fSTao Ma 		ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
68432999d12fSTao Ma 		if (ret) {
68442999d12fSTao Ma 			mlog_errno(ret);
68452999d12fSTao Ma 			break;
68462999d12fSTao Ma 		}
68472999d12fSTao Ma 
68489c339255SWengang Wang 		ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno, 1);
68492999d12fSTao Ma 		if (ret) {
68502999d12fSTao Ma 			mlog_errno(ret);
68512999d12fSTao Ma 			break;
68522999d12fSTao Ma 		}
68532999d12fSTao Ma 
68542999d12fSTao Ma 		ret = ocfs2_xattr_bucket_journal_access(handle,
68552999d12fSTao Ma 						args->new_bucket,
68562999d12fSTao Ma 						OCFS2_JOURNAL_ACCESS_CREATE);
68572999d12fSTao Ma 		if (ret) {
68582999d12fSTao Ma 			mlog_errno(ret);
68592999d12fSTao Ma 			break;
68602999d12fSTao Ma 		}
68612999d12fSTao Ma 
68622999d12fSTao Ma 		for (j = 0; j < bpb; j++)
68632999d12fSTao Ma 			memcpy(bucket_block(args->new_bucket, j),
68642999d12fSTao Ma 			       bucket_block(args->old_bucket, j),
68652999d12fSTao Ma 			       sb->s_blocksize);
68662999d12fSTao Ma 
6867121a39bbSTao Ma 		/*
6868121a39bbSTao Ma 		 * Record the start cpos so that we can use it to initialize
6869121a39bbSTao Ma 		 * our xattr tree we also set the xh_num_bucket for the new
6870121a39bbSTao Ma 		 * bucket.
6871121a39bbSTao Ma 		 */
6872121a39bbSTao Ma 		if (i == 0) {
6873121a39bbSTao Ma 			*cpos = le32_to_cpu(bucket_xh(args->new_bucket)->
6874121a39bbSTao Ma 					    xh_entries[0].xe_name_hash);
6875121a39bbSTao Ma 			bucket_xh(args->new_bucket)->xh_num_buckets =
6876121a39bbSTao Ma 				cpu_to_le16(num_buckets);
6877121a39bbSTao Ma 		}
6878121a39bbSTao Ma 
68792999d12fSTao Ma 		ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
68802999d12fSTao Ma 
68812999d12fSTao Ma 		ret = ocfs2_reflink_xattr_header(handle, args->reflink,
68822999d12fSTao Ma 					args->old_bucket->bu_bhs[0],
68832999d12fSTao Ma 					bucket_xh(args->old_bucket),
68842999d12fSTao Ma 					args->new_bucket->bu_bhs[0],
68852999d12fSTao Ma 					bucket_xh(args->new_bucket),
68862999d12fSTao Ma 					&vb, meta_ac,
68872999d12fSTao Ma 					ocfs2_get_reflink_xattr_value_root,
68882999d12fSTao Ma 					args);
68892999d12fSTao Ma 		if (ret) {
68902999d12fSTao Ma 			mlog_errno(ret);
68912999d12fSTao Ma 			break;
68922999d12fSTao Ma 		}
68932999d12fSTao Ma 
68942999d12fSTao Ma 		/*
68952999d12fSTao Ma 		 * Re-access and dirty the bucket to calculate metaecc.
68962999d12fSTao Ma 		 * Because we may extend the transaction in reflink_xattr_header
68972999d12fSTao Ma 		 * which will let the already accessed block gone.
68982999d12fSTao Ma 		 */
68992999d12fSTao Ma 		ret = ocfs2_xattr_bucket_journal_access(handle,
69002999d12fSTao Ma 						args->new_bucket,
69012999d12fSTao Ma 						OCFS2_JOURNAL_ACCESS_WRITE);
69022999d12fSTao Ma 		if (ret) {
69032999d12fSTao Ma 			mlog_errno(ret);
69042999d12fSTao Ma 			break;
69052999d12fSTao Ma 		}
69062999d12fSTao Ma 
69072999d12fSTao Ma 		ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6908121a39bbSTao Ma 
69092999d12fSTao Ma 		ocfs2_xattr_bucket_relse(args->old_bucket);
69102999d12fSTao Ma 		ocfs2_xattr_bucket_relse(args->new_bucket);
69112999d12fSTao Ma 	}
69122999d12fSTao Ma 
69132999d12fSTao Ma 	ocfs2_xattr_bucket_relse(args->old_bucket);
69142999d12fSTao Ma 	ocfs2_xattr_bucket_relse(args->new_bucket);
69152999d12fSTao Ma 	return ret;
69162999d12fSTao Ma }
6917121a39bbSTao Ma 
ocfs2_reflink_xattr_buckets(handle_t * handle,struct inode * inode,struct ocfs2_reflink_xattr_tree_args * args,struct ocfs2_extent_tree * et,struct ocfs2_alloc_context * meta_ac,struct ocfs2_alloc_context * data_ac,u64 blkno,u32 cpos,u32 len)6918121a39bbSTao Ma static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6919121a39bbSTao Ma 				struct inode *inode,
6920121a39bbSTao Ma 				struct ocfs2_reflink_xattr_tree_args *args,
6921121a39bbSTao Ma 				struct ocfs2_extent_tree *et,
6922121a39bbSTao Ma 				struct ocfs2_alloc_context *meta_ac,
6923121a39bbSTao Ma 				struct ocfs2_alloc_context *data_ac,
6924121a39bbSTao Ma 				u64 blkno, u32 cpos, u32 len)
6925121a39bbSTao Ma {
6926121a39bbSTao Ma 	int ret, first_inserted = 0;
6927121a39bbSTao Ma 	u32 p_cluster, num_clusters, reflink_cpos = 0;
6928121a39bbSTao Ma 	u64 new_blkno;
6929121a39bbSTao Ma 	unsigned int num_buckets, reflink_buckets;
6930121a39bbSTao Ma 	unsigned int bpc =
6931121a39bbSTao Ma 		ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
6932121a39bbSTao Ma 
6933121a39bbSTao Ma 	ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6934121a39bbSTao Ma 	if (ret) {
6935121a39bbSTao Ma 		mlog_errno(ret);
6936121a39bbSTao Ma 		goto out;
6937121a39bbSTao Ma 	}
6938121a39bbSTao Ma 	num_buckets = le16_to_cpu(bucket_xh(args->old_bucket)->xh_num_buckets);
6939121a39bbSTao Ma 	ocfs2_xattr_bucket_relse(args->old_bucket);
6940121a39bbSTao Ma 
6941121a39bbSTao Ma 	while (len && num_buckets) {
6942121a39bbSTao Ma 		ret = ocfs2_claim_clusters(handle, data_ac,
6943121a39bbSTao Ma 					   1, &p_cluster, &num_clusters);
6944121a39bbSTao Ma 		if (ret) {
6945121a39bbSTao Ma 			mlog_errno(ret);
6946121a39bbSTao Ma 			goto out;
6947121a39bbSTao Ma 		}
6948121a39bbSTao Ma 
6949121a39bbSTao Ma 		new_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
6950121a39bbSTao Ma 		reflink_buckets = min(num_buckets, bpc * num_clusters);
6951121a39bbSTao Ma 
6952121a39bbSTao Ma 		ret = ocfs2_reflink_xattr_bucket(handle, blkno,
6953121a39bbSTao Ma 						 new_blkno, num_clusters,
6954121a39bbSTao Ma 						 &reflink_cpos, reflink_buckets,
6955121a39bbSTao Ma 						 meta_ac, data_ac, args);
6956121a39bbSTao Ma 		if (ret) {
6957121a39bbSTao Ma 			mlog_errno(ret);
6958121a39bbSTao Ma 			goto out;
6959121a39bbSTao Ma 		}
6960121a39bbSTao Ma 
6961121a39bbSTao Ma 		/*
6962121a39bbSTao Ma 		 * For the 1st allocated cluster, we make it use the same cpos
6963121a39bbSTao Ma 		 * so that the xattr tree looks the same as the original one
6964121a39bbSTao Ma 		 * in the most case.
6965121a39bbSTao Ma 		 */
6966121a39bbSTao Ma 		if (!first_inserted) {
6967121a39bbSTao Ma 			reflink_cpos = cpos;
6968121a39bbSTao Ma 			first_inserted = 1;
6969121a39bbSTao Ma 		}
6970121a39bbSTao Ma 		ret = ocfs2_insert_extent(handle, et, reflink_cpos, new_blkno,
6971121a39bbSTao Ma 					  num_clusters, 0, meta_ac);
6972121a39bbSTao Ma 		if (ret)
6973121a39bbSTao Ma 			mlog_errno(ret);
6974121a39bbSTao Ma 
6975402b4183STao Ma 		trace_ocfs2_reflink_xattr_buckets((unsigned long long)new_blkno,
6976402b4183STao Ma 						  num_clusters, reflink_cpos);
6977121a39bbSTao Ma 
6978121a39bbSTao Ma 		len -= num_clusters;
6979121a39bbSTao Ma 		blkno += ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
6980121a39bbSTao Ma 		num_buckets -= reflink_buckets;
6981121a39bbSTao Ma 	}
6982121a39bbSTao Ma out:
6983121a39bbSTao Ma 	return ret;
6984121a39bbSTao Ma }
6985121a39bbSTao Ma 
69862999d12fSTao Ma /*
69872999d12fSTao Ma  * Create the same xattr extent record in the new inode's xattr tree.
69882999d12fSTao Ma  */
ocfs2_reflink_xattr_rec(struct inode * inode,struct buffer_head * root_bh,u64 blkno,u32 cpos,u32 len,void * para)69892999d12fSTao Ma static int ocfs2_reflink_xattr_rec(struct inode *inode,
69902999d12fSTao Ma 				   struct buffer_head *root_bh,
69912999d12fSTao Ma 				   u64 blkno,
69922999d12fSTao Ma 				   u32 cpos,
69932999d12fSTao Ma 				   u32 len,
69942999d12fSTao Ma 				   void *para)
69952999d12fSTao Ma {
69962999d12fSTao Ma 	int ret, credits = 0;
69972999d12fSTao Ma 	handle_t *handle;
69982999d12fSTao Ma 	struct ocfs2_reflink_xattr_tree_args *args =
69992999d12fSTao Ma 			(struct ocfs2_reflink_xattr_tree_args *)para;
70002999d12fSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
70012999d12fSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
70022999d12fSTao Ma 	struct ocfs2_alloc_context *data_ac = NULL;
70032999d12fSTao Ma 	struct ocfs2_extent_tree et;
70042999d12fSTao Ma 
7005402b4183STao Ma 	trace_ocfs2_reflink_xattr_rec((unsigned long long)blkno, len);
7006121a39bbSTao Ma 
70072999d12fSTao Ma 	ocfs2_init_xattr_tree_extent_tree(&et,
70082999d12fSTao Ma 					  INODE_CACHE(args->reflink->new_inode),
70092999d12fSTao Ma 					  args->new_blk_bh);
70102999d12fSTao Ma 
70112999d12fSTao Ma 	ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
70122999d12fSTao Ma 						      len, &credits,
70132999d12fSTao Ma 						      &meta_ac, &data_ac);
70142999d12fSTao Ma 	if (ret) {
70152999d12fSTao Ma 		mlog_errno(ret);
70162999d12fSTao Ma 		goto out;
70172999d12fSTao Ma 	}
70182999d12fSTao Ma 
70192999d12fSTao Ma 	handle = ocfs2_start_trans(osb, credits);
70202999d12fSTao Ma 	if (IS_ERR(handle)) {
70212999d12fSTao Ma 		ret = PTR_ERR(handle);
70222999d12fSTao Ma 		mlog_errno(ret);
70232999d12fSTao Ma 		goto out;
70242999d12fSTao Ma 	}
70252999d12fSTao Ma 
7026121a39bbSTao Ma 	ret = ocfs2_reflink_xattr_buckets(handle, inode, args, &et,
7027121a39bbSTao Ma 					  meta_ac, data_ac,
7028121a39bbSTao Ma 					  blkno, cpos, len);
70292999d12fSTao Ma 	if (ret)
70302999d12fSTao Ma 		mlog_errno(ret);
70312999d12fSTao Ma 
70322999d12fSTao Ma 	ocfs2_commit_trans(osb, handle);
70332999d12fSTao Ma 
70342999d12fSTao Ma out:
70352999d12fSTao Ma 	if (meta_ac)
70362999d12fSTao Ma 		ocfs2_free_alloc_context(meta_ac);
70372999d12fSTao Ma 	if (data_ac)
70382999d12fSTao Ma 		ocfs2_free_alloc_context(data_ac);
70392999d12fSTao Ma 	return ret;
70402999d12fSTao Ma }
70412999d12fSTao Ma 
70422999d12fSTao Ma /*
70432999d12fSTao Ma  * Create reflinked xattr buckets.
70442999d12fSTao Ma  * We will add bucket one by one, and refcount all the xattrs in the bucket
70452999d12fSTao Ma  * if they are stored outside.
70462999d12fSTao Ma  */
ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink * args,struct buffer_head * blk_bh,struct buffer_head * new_blk_bh)70472999d12fSTao Ma static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
70482999d12fSTao Ma 				    struct buffer_head *blk_bh,
70492999d12fSTao Ma 				    struct buffer_head *new_blk_bh)
70502999d12fSTao Ma {
70512999d12fSTao Ma 	int ret;
70522999d12fSTao Ma 	struct ocfs2_reflink_xattr_tree_args para;
70532999d12fSTao Ma 
70542999d12fSTao Ma 	memset(&para, 0, sizeof(para));
70552999d12fSTao Ma 	para.reflink = args;
70562999d12fSTao Ma 	para.old_blk_bh = blk_bh;
70572999d12fSTao Ma 	para.new_blk_bh = new_blk_bh;
70582999d12fSTao Ma 
70592999d12fSTao Ma 	para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
70602999d12fSTao Ma 	if (!para.old_bucket) {
70612999d12fSTao Ma 		mlog_errno(-ENOMEM);
70622999d12fSTao Ma 		return -ENOMEM;
70632999d12fSTao Ma 	}
70642999d12fSTao Ma 
70652999d12fSTao Ma 	para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
70662999d12fSTao Ma 	if (!para.new_bucket) {
70672999d12fSTao Ma 		ret = -ENOMEM;
70682999d12fSTao Ma 		mlog_errno(ret);
70692999d12fSTao Ma 		goto out;
70702999d12fSTao Ma 	}
70712999d12fSTao Ma 
70722999d12fSTao Ma 	ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
70732999d12fSTao Ma 					      ocfs2_reflink_xattr_rec,
70742999d12fSTao Ma 					      &para);
70752999d12fSTao Ma 	if (ret)
70762999d12fSTao Ma 		mlog_errno(ret);
70772999d12fSTao Ma 
70782999d12fSTao Ma out:
70792999d12fSTao Ma 	ocfs2_xattr_bucket_free(para.old_bucket);
70802999d12fSTao Ma 	ocfs2_xattr_bucket_free(para.new_bucket);
70812999d12fSTao Ma 	return ret;
70822999d12fSTao Ma }
70832999d12fSTao Ma 
ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink * args,struct buffer_head * blk_bh)70842999d12fSTao Ma static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
70852999d12fSTao Ma 					struct buffer_head *blk_bh)
70862999d12fSTao Ma {
70872999d12fSTao Ma 	int ret, indexed = 0;
70882999d12fSTao Ma 	struct buffer_head *new_blk_bh = NULL;
70892999d12fSTao Ma 	struct ocfs2_xattr_block *xb =
70902999d12fSTao Ma 			(struct ocfs2_xattr_block *)blk_bh->b_data;
70912999d12fSTao Ma 
70922999d12fSTao Ma 
70932999d12fSTao Ma 	if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
70942999d12fSTao Ma 		indexed = 1;
70952999d12fSTao Ma 
70962999d12fSTao Ma 	ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
70972999d12fSTao Ma 					     &new_blk_bh, indexed);
70982999d12fSTao Ma 	if (ret) {
70992999d12fSTao Ma 		mlog_errno(ret);
71002999d12fSTao Ma 		goto out;
71012999d12fSTao Ma 	}
71022999d12fSTao Ma 
71032decd65aSJeff Liu 	if (!indexed)
71042999d12fSTao Ma 		ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
71052999d12fSTao Ma 	else
71062999d12fSTao Ma 		ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
71072999d12fSTao Ma 	if (ret)
71082999d12fSTao Ma 		mlog_errno(ret);
71092999d12fSTao Ma 
71102999d12fSTao Ma out:
71112999d12fSTao Ma 	brelse(new_blk_bh);
71122999d12fSTao Ma 	return ret;
71132999d12fSTao Ma }
71142999d12fSTao Ma 
ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry * xe)71150fe9b66cSTao Ma static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
71160fe9b66cSTao Ma {
71170fe9b66cSTao Ma 	int type = ocfs2_xattr_get_type(xe);
71180fe9b66cSTao Ma 
71190fe9b66cSTao Ma 	return type != OCFS2_XATTR_INDEX_SECURITY &&
71200fe9b66cSTao Ma 	       type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
71210fe9b66cSTao Ma 	       type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
71220fe9b66cSTao Ma }
71230fe9b66cSTao Ma 
ocfs2_reflink_xattrs(struct inode * old_inode,struct buffer_head * old_bh,struct inode * new_inode,struct buffer_head * new_bh,bool preserve_security)71242999d12fSTao Ma int ocfs2_reflink_xattrs(struct inode *old_inode,
71252999d12fSTao Ma 			 struct buffer_head *old_bh,
71262999d12fSTao Ma 			 struct inode *new_inode,
71270fe9b66cSTao Ma 			 struct buffer_head *new_bh,
71280fe9b66cSTao Ma 			 bool preserve_security)
71292999d12fSTao Ma {
71302999d12fSTao Ma 	int ret;
71312999d12fSTao Ma 	struct ocfs2_xattr_reflink args;
71322999d12fSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
71332999d12fSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
71342999d12fSTao Ma 	struct buffer_head *blk_bh = NULL;
71352999d12fSTao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
71362999d12fSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
71372999d12fSTao Ma 	struct buffer_head *ref_root_bh = NULL;
71382999d12fSTao Ma 
71392999d12fSTao Ma 	ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
71402999d12fSTao Ma 				       le64_to_cpu(di->i_refcount_loc),
71412999d12fSTao Ma 				       1, &ref_tree, &ref_root_bh);
71422999d12fSTao Ma 	if (ret) {
71432999d12fSTao Ma 		mlog_errno(ret);
71442999d12fSTao Ma 		goto out;
71452999d12fSTao Ma 	}
71462999d12fSTao Ma 
71472999d12fSTao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
71482999d12fSTao Ma 
71492999d12fSTao Ma 	args.old_inode = old_inode;
71502999d12fSTao Ma 	args.new_inode = new_inode;
71512999d12fSTao Ma 	args.old_bh = old_bh;
71522999d12fSTao Ma 	args.new_bh = new_bh;
71532999d12fSTao Ma 	args.ref_ci = &ref_tree->rf_ci;
71542999d12fSTao Ma 	args.ref_root_bh = ref_root_bh;
71552999d12fSTao Ma 	args.dealloc = &dealloc;
71560fe9b66cSTao Ma 	if (preserve_security)
71570fe9b66cSTao Ma 		args.xattr_reflinked = NULL;
71580fe9b66cSTao Ma 	else
71590fe9b66cSTao Ma 		args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
71602999d12fSTao Ma 
71612999d12fSTao Ma 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
71622999d12fSTao Ma 		ret = ocfs2_reflink_xattr_inline(&args);
71632999d12fSTao Ma 		if (ret) {
71642999d12fSTao Ma 			mlog_errno(ret);
71652999d12fSTao Ma 			goto out_unlock;
71662999d12fSTao Ma 		}
71672999d12fSTao Ma 	}
71682999d12fSTao Ma 
71692999d12fSTao Ma 	if (!di->i_xattr_loc)
71702999d12fSTao Ma 		goto out_unlock;
71712999d12fSTao Ma 
71722999d12fSTao Ma 	ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
71732999d12fSTao Ma 				     &blk_bh);
71742999d12fSTao Ma 	if (ret < 0) {
71752999d12fSTao Ma 		mlog_errno(ret);
71762999d12fSTao Ma 		goto out_unlock;
71772999d12fSTao Ma 	}
71782999d12fSTao Ma 
71792999d12fSTao Ma 	ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
71802999d12fSTao Ma 	if (ret)
71812999d12fSTao Ma 		mlog_errno(ret);
71822999d12fSTao Ma 
71832999d12fSTao Ma 	brelse(blk_bh);
71842999d12fSTao Ma 
71852999d12fSTao Ma out_unlock:
71862999d12fSTao Ma 	ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
71872999d12fSTao Ma 				   ref_tree, 1);
71882999d12fSTao Ma 	brelse(ref_root_bh);
71892999d12fSTao Ma 
71902999d12fSTao Ma 	if (ocfs2_dealloc_has_cluster(&dealloc)) {
71912999d12fSTao Ma 		ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
71922999d12fSTao Ma 		ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
71932999d12fSTao Ma 	}
71942999d12fSTao Ma 
71952999d12fSTao Ma out:
71962999d12fSTao Ma 	return ret;
71972999d12fSTao Ma }
71982999d12fSTao Ma 
71992999d12fSTao Ma /*
72000fe9b66cSTao Ma  * Initialize security and acl for a already created inode.
72010fe9b66cSTao Ma  * Used for reflink a non-preserve-security file.
72020fe9b66cSTao Ma  *
72030fe9b66cSTao Ma  * It uses common api like ocfs2_xattr_set, so the caller
7204137cebf9Shongnanli  * must not hold any lock expect i_rwsem.
72050fe9b66cSTao Ma  */
ocfs2_init_security_and_acl(struct inode * dir,struct inode * inode,const struct qstr * qstr)72060fe9b66cSTao Ma int ocfs2_init_security_and_acl(struct inode *dir,
72072a7dba39SEric Paris 				struct inode *inode,
7208c25a1e06SJunxiao Bi 				const struct qstr *qstr)
72090fe9b66cSTao Ma {
7210702e5bc6SChristoph Hellwig 	int ret = 0;
7211c25a1e06SJunxiao Bi 	struct buffer_head *dir_bh = NULL;
72120fe9b66cSTao Ma 
72139d8f13baSMimi Zohar 	ret = ocfs2_init_security_get(inode, dir, qstr, NULL);
721432918dd9SJeff Liu 	if (ret) {
72150fe9b66cSTao Ma 		mlog_errno(ret);
72160fe9b66cSTao Ma 		goto leave;
72170fe9b66cSTao Ma 	}
72180fe9b66cSTao Ma 
72190fe9b66cSTao Ma 	ret = ocfs2_inode_lock(dir, &dir_bh, 0);
72200fe9b66cSTao Ma 	if (ret) {
72210fe9b66cSTao Ma 		mlog_errno(ret);
72220fe9b66cSTao Ma 		goto leave;
72230fe9b66cSTao Ma 	}
7224c25a1e06SJunxiao Bi 	ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7225c25a1e06SJunxiao Bi 	if (ret)
7226c25a1e06SJunxiao Bi 		mlog_errno(ret);
72270fe9b66cSTao Ma 
72280fe9b66cSTao Ma 	ocfs2_inode_unlock(dir, 0);
72290fe9b66cSTao Ma 	brelse(dir_bh);
72300fe9b66cSTao Ma leave:
72310fe9b66cSTao Ma 	return ret;
72320fe9b66cSTao Ma }
72331046cb11SAndreas Gruenbacher 
72340fe9b66cSTao Ma /*
7235923f7f31STiger Yang  * 'security' attributes support
7236923f7f31STiger Yang  */
ocfs2_xattr_security_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)7237d9a82a04SAndreas Gruenbacher static int ocfs2_xattr_security_get(const struct xattr_handler *handler,
7238b296821aSAl Viro 				    struct dentry *unused, struct inode *inode,
7239b296821aSAl Viro 				    const char *name, void *buffer, size_t size)
7240923f7f31STiger Yang {
7241b296821aSAl Viro 	return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY,
7242431547b3SChristoph Hellwig 			       name, buffer, size);
7243923f7f31STiger Yang }
7244923f7f31STiger Yang 
ocfs2_xattr_security_set(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)7245d9a82a04SAndreas Gruenbacher static int ocfs2_xattr_security_set(const struct xattr_handler *handler,
724639f60c1cSChristian Brauner 				    struct mnt_idmap *idmap,
724759301226SAl Viro 				    struct dentry *unused, struct inode *inode,
724859301226SAl Viro 				    const char *name, const void *value,
724959301226SAl Viro 				    size_t size, int flags)
7250923f7f31STiger Yang {
725159301226SAl Viro 	return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7252431547b3SChristoph Hellwig 			       name, value, size, flags);
7253923f7f31STiger Yang }
7254923f7f31STiger Yang 
ocfs2_initxattrs(struct inode * inode,const struct xattr * xattr_array,void * fs_info)7255b519ea6dSJoseph Qi static int ocfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
72569d8f13baSMimi Zohar 		     void *fs_info)
72579d8f13baSMimi Zohar {
7258de3004c8SRoberto Sassu 	struct ocfs2_security_xattr_info *si = fs_info;
72599d8f13baSMimi Zohar 	const struct xattr *xattr;
72609d8f13baSMimi Zohar 	int err = 0;
72619d8f13baSMimi Zohar 
7262de3004c8SRoberto Sassu 	if (si) {
7263de3004c8SRoberto Sassu 		si->value = kmemdup(xattr_array->value, xattr_array->value_len,
7264de3004c8SRoberto Sassu 				    GFP_KERNEL);
7265de3004c8SRoberto Sassu 		if (!si->value)
7266de3004c8SRoberto Sassu 			return -ENOMEM;
7267de3004c8SRoberto Sassu 
7268de3004c8SRoberto Sassu 		si->name = xattr_array->name;
7269de3004c8SRoberto Sassu 		si->value_len = xattr_array->value_len;
7270de3004c8SRoberto Sassu 		return 0;
7271de3004c8SRoberto Sassu 	}
7272de3004c8SRoberto Sassu 
72739d8f13baSMimi Zohar 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
72749d8f13baSMimi Zohar 		err = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
72759d8f13baSMimi Zohar 				      xattr->name, xattr->value,
72769d8f13baSMimi Zohar 				      xattr->value_len, XATTR_CREATE);
72779d8f13baSMimi Zohar 		if (err)
72789d8f13baSMimi Zohar 			break;
72799d8f13baSMimi Zohar 	}
72809d8f13baSMimi Zohar 	return err;
72819d8f13baSMimi Zohar }
72829d8f13baSMimi Zohar 
ocfs2_init_security_get(struct inode * inode,struct inode * dir,const struct qstr * qstr,struct ocfs2_security_xattr_info * si)7283534eadddSTiger Yang int ocfs2_init_security_get(struct inode *inode,
7284534eadddSTiger Yang 			    struct inode *dir,
72852a7dba39SEric Paris 			    const struct qstr *qstr,
7286534eadddSTiger Yang 			    struct ocfs2_security_xattr_info *si)
7287534eadddSTiger Yang {
7288de3004c8SRoberto Sassu 	int ret;
7289de3004c8SRoberto Sassu 
729038d59ef6STiger Yang 	/* check whether ocfs2 support feature xattr */
729138d59ef6STiger Yang 	if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
729238d59ef6STiger Yang 		return -EOPNOTSUPP;
7293de3004c8SRoberto Sassu 	if (si) {
7294de3004c8SRoberto Sassu 		ret = security_inode_init_security(inode, dir, qstr,
7295de3004c8SRoberto Sassu 						   &ocfs2_initxattrs, si);
7296de3004c8SRoberto Sassu 		/*
7297de3004c8SRoberto Sassu 		 * security_inode_init_security() does not return -EOPNOTSUPP,
7298de3004c8SRoberto Sassu 		 * we have to check the xattr ourselves.
7299de3004c8SRoberto Sassu 		 */
7300de3004c8SRoberto Sassu 		if (!ret && !si->name)
7301de3004c8SRoberto Sassu 			si->enable = 0;
7302de3004c8SRoberto Sassu 
7303de3004c8SRoberto Sassu 		return ret;
7304de3004c8SRoberto Sassu 	}
73059d8f13baSMimi Zohar 
73069d8f13baSMimi Zohar 	return security_inode_init_security(inode, dir, qstr,
73079d8f13baSMimi Zohar 					    &ocfs2_initxattrs, NULL);
7308534eadddSTiger Yang }
7309534eadddSTiger Yang 
ocfs2_init_security_set(handle_t * handle,struct inode * inode,struct buffer_head * di_bh,struct ocfs2_security_xattr_info * si,struct ocfs2_alloc_context * xattr_ac,struct ocfs2_alloc_context * data_ac)7310534eadddSTiger Yang int ocfs2_init_security_set(handle_t *handle,
7311534eadddSTiger Yang 			    struct inode *inode,
7312534eadddSTiger Yang 			    struct buffer_head *di_bh,
7313534eadddSTiger Yang 			    struct ocfs2_security_xattr_info *si,
7314534eadddSTiger Yang 			    struct ocfs2_alloc_context *xattr_ac,
7315534eadddSTiger Yang 			    struct ocfs2_alloc_context *data_ac)
7316534eadddSTiger Yang {
7317534eadddSTiger Yang 	return ocfs2_xattr_set_handle(handle, inode, di_bh,
7318534eadddSTiger Yang 				     OCFS2_XATTR_INDEX_SECURITY,
7319534eadddSTiger Yang 				     si->name, si->value, si->value_len, 0,
7320534eadddSTiger Yang 				     xattr_ac, data_ac);
7321534eadddSTiger Yang }
7322534eadddSTiger Yang 
7323537d81caSStephen Hemminger const struct xattr_handler ocfs2_xattr_security_handler = {
7324923f7f31STiger Yang 	.prefix	= XATTR_SECURITY_PREFIX,
7325923f7f31STiger Yang 	.get	= ocfs2_xattr_security_get,
7326923f7f31STiger Yang 	.set	= ocfs2_xattr_security_set,
7327923f7f31STiger Yang };
7328923f7f31STiger Yang 
7329923f7f31STiger Yang /*
733099219aeaSMark Fasheh  * 'trusted' attributes support
733199219aeaSMark Fasheh  */
ocfs2_xattr_trusted_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)7332d9a82a04SAndreas Gruenbacher static int ocfs2_xattr_trusted_get(const struct xattr_handler *handler,
7333b296821aSAl Viro 				   struct dentry *unused, struct inode *inode,
7334b296821aSAl Viro 				   const char *name, void *buffer, size_t size)
733599219aeaSMark Fasheh {
7336b296821aSAl Viro 	return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED,
7337431547b3SChristoph Hellwig 			       name, buffer, size);
733899219aeaSMark Fasheh }
733999219aeaSMark Fasheh 
ocfs2_xattr_trusted_set(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)7340d9a82a04SAndreas Gruenbacher static int ocfs2_xattr_trusted_set(const struct xattr_handler *handler,
734139f60c1cSChristian Brauner 				   struct mnt_idmap *idmap,
734259301226SAl Viro 				   struct dentry *unused, struct inode *inode,
734359301226SAl Viro 				   const char *name, const void *value,
734459301226SAl Viro 				   size_t size, int flags)
734599219aeaSMark Fasheh {
734659301226SAl Viro 	return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED,
7347431547b3SChristoph Hellwig 			       name, value, size, flags);
734899219aeaSMark Fasheh }
734999219aeaSMark Fasheh 
7350537d81caSStephen Hemminger const struct xattr_handler ocfs2_xattr_trusted_handler = {
735199219aeaSMark Fasheh 	.prefix	= XATTR_TRUSTED_PREFIX,
735299219aeaSMark Fasheh 	.get	= ocfs2_xattr_trusted_get,
735399219aeaSMark Fasheh 	.set	= ocfs2_xattr_trusted_set,
735499219aeaSMark Fasheh };
735599219aeaSMark Fasheh 
735699219aeaSMark Fasheh /*
735799219aeaSMark Fasheh  * 'user' attributes support
735899219aeaSMark Fasheh  */
ocfs2_xattr_user_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)7359d9a82a04SAndreas Gruenbacher static int ocfs2_xattr_user_get(const struct xattr_handler *handler,
736084d86e6dSAndreas Gruenbacher 				struct dentry *unused, struct inode *inode,
7361b296821aSAl Viro 				const char *name, void *buffer, size_t size)
736299219aeaSMark Fasheh {
7363b296821aSAl Viro 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
736499219aeaSMark Fasheh 
736599219aeaSMark Fasheh 	if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
736699219aeaSMark Fasheh 		return -EOPNOTSUPP;
7367b296821aSAl Viro 	return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
736899219aeaSMark Fasheh 			       buffer, size);
736999219aeaSMark Fasheh }
737099219aeaSMark Fasheh 
ocfs2_xattr_user_set(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)7371d9a82a04SAndreas Gruenbacher static int ocfs2_xattr_user_set(const struct xattr_handler *handler,
737239f60c1cSChristian Brauner 				struct mnt_idmap *idmap,
737359301226SAl Viro 				struct dentry *unused, struct inode *inode,
737459301226SAl Viro 				const char *name, const void *value,
737559301226SAl Viro 				size_t size, int flags)
737699219aeaSMark Fasheh {
737759301226SAl Viro 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
737899219aeaSMark Fasheh 
737999219aeaSMark Fasheh 	if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
738099219aeaSMark Fasheh 		return -EOPNOTSUPP;
738199219aeaSMark Fasheh 
738259301226SAl Viro 	return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER,
7383431547b3SChristoph Hellwig 			       name, value, size, flags);
738499219aeaSMark Fasheh }
738599219aeaSMark Fasheh 
7386537d81caSStephen Hemminger const struct xattr_handler ocfs2_xattr_user_handler = {
738799219aeaSMark Fasheh 	.prefix	= XATTR_USER_PREFIX,
738899219aeaSMark Fasheh 	.get	= ocfs2_xattr_user_get,
738999219aeaSMark Fasheh 	.set	= ocfs2_xattr_user_set,
739099219aeaSMark Fasheh };
7391