xref: /openbmc/linux/fs/ocfs2/xattr.c (revision f7777dcc)
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * xattr.c
5  *
6  * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
7  *
8  * CREDITS:
9  * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public
14  * License version 2 as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  */
21 
22 #include <linux/capability.h>
23 #include <linux/fs.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
39 
40 #include <cluster/masklog.h>
41 
42 #include "ocfs2.h"
43 #include "alloc.h"
44 #include "blockcheck.h"
45 #include "dlmglue.h"
46 #include "file.h"
47 #include "symlink.h"
48 #include "sysfile.h"
49 #include "inode.h"
50 #include "journal.h"
51 #include "ocfs2_fs.h"
52 #include "suballoc.h"
53 #include "uptodate.h"
54 #include "buffer_head_io.h"
55 #include "super.h"
56 #include "xattr.h"
57 #include "refcounttree.h"
58 #include "acl.h"
59 #include "ocfs2_trace.h"
60 
61 struct ocfs2_xattr_def_value_root {
62 	struct ocfs2_xattr_value_root	xv;
63 	struct ocfs2_extent_rec		er;
64 };
65 
66 struct ocfs2_xattr_bucket {
67 	/* The inode these xattrs are associated with */
68 	struct inode *bu_inode;
69 
70 	/* The actual buffers that make up the bucket */
71 	struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
72 
73 	/* How many blocks make up one bucket for this filesystem */
74 	int bu_blocks;
75 };
76 
77 struct ocfs2_xattr_set_ctxt {
78 	handle_t *handle;
79 	struct ocfs2_alloc_context *meta_ac;
80 	struct ocfs2_alloc_context *data_ac;
81 	struct ocfs2_cached_dealloc_ctxt dealloc;
82 	int set_abort;
83 };
84 
85 #define OCFS2_XATTR_ROOT_SIZE	(sizeof(struct ocfs2_xattr_def_value_root))
86 #define OCFS2_XATTR_INLINE_SIZE	80
87 #define OCFS2_XATTR_HEADER_GAP	4
88 #define OCFS2_XATTR_FREE_IN_IBODY	(OCFS2_MIN_XATTR_INLINE_SIZE \
89 					 - sizeof(struct ocfs2_xattr_header) \
90 					 - OCFS2_XATTR_HEADER_GAP)
91 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr)	((ptr)->i_sb->s_blocksize \
92 					 - sizeof(struct ocfs2_xattr_block) \
93 					 - sizeof(struct ocfs2_xattr_header) \
94 					 - OCFS2_XATTR_HEADER_GAP)
95 
96 static struct ocfs2_xattr_def_value_root def_xv = {
97 	.xv.xr_list.l_count = cpu_to_le16(1),
98 };
99 
100 const struct xattr_handler *ocfs2_xattr_handlers[] = {
101 	&ocfs2_xattr_user_handler,
102 	&ocfs2_xattr_acl_access_handler,
103 	&ocfs2_xattr_acl_default_handler,
104 	&ocfs2_xattr_trusted_handler,
105 	&ocfs2_xattr_security_handler,
106 	NULL
107 };
108 
109 static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
110 	[OCFS2_XATTR_INDEX_USER]	= &ocfs2_xattr_user_handler,
111 	[OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
112 					= &ocfs2_xattr_acl_access_handler,
113 	[OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
114 					= &ocfs2_xattr_acl_default_handler,
115 	[OCFS2_XATTR_INDEX_TRUSTED]	= &ocfs2_xattr_trusted_handler,
116 	[OCFS2_XATTR_INDEX_SECURITY]	= &ocfs2_xattr_security_handler,
117 };
118 
119 struct ocfs2_xattr_info {
120 	int		xi_name_index;
121 	const char	*xi_name;
122 	int		xi_name_len;
123 	const void	*xi_value;
124 	size_t		xi_value_len;
125 };
126 
127 struct ocfs2_xattr_search {
128 	struct buffer_head *inode_bh;
129 	/*
130 	 * xattr_bh point to the block buffer head which has extended attribute
131 	 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 	 */
133 	struct buffer_head *xattr_bh;
134 	struct ocfs2_xattr_header *header;
135 	struct ocfs2_xattr_bucket *bucket;
136 	void *base;
137 	void *end;
138 	struct ocfs2_xattr_entry *here;
139 	int not_found;
140 };
141 
142 /* Operations on struct ocfs2_xa_entry */
143 struct ocfs2_xa_loc;
144 struct ocfs2_xa_loc_operations {
145 	/*
146 	 * Journal functions
147 	 */
148 	int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
149 				  int type);
150 	void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
151 
152 	/*
153 	 * Return a pointer to the appropriate buffer in loc->xl_storage
154 	 * at the given offset from loc->xl_header.
155 	 */
156 	void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
157 
158 	/* Can we reuse the existing entry for the new value? */
159 	int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
160 			     struct ocfs2_xattr_info *xi);
161 
162 	/* How much space is needed for the new value? */
163 	int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
164 			       struct ocfs2_xattr_info *xi);
165 
166 	/*
167 	 * Return the offset of the first name+value pair.  This is
168 	 * the start of our downward-filling free space.
169 	 */
170 	int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
171 
172 	/*
173 	 * Remove the name+value at this location.  Do whatever is
174 	 * appropriate with the remaining name+value pairs.
175 	 */
176 	void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
177 
178 	/* Fill xl_entry with a new entry */
179 	void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
180 
181 	/* Add name+value storage to an entry */
182 	void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
183 
184 	/*
185 	 * Initialize the value buf's access and bh fields for this entry.
186 	 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
187 	 */
188 	void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
189 				   struct ocfs2_xattr_value_buf *vb);
190 };
191 
192 /*
193  * Describes an xattr entry location.  This is a memory structure
194  * tracking the on-disk structure.
195  */
196 struct ocfs2_xa_loc {
197 	/* This xattr belongs to this inode */
198 	struct inode *xl_inode;
199 
200 	/* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
201 	struct ocfs2_xattr_header *xl_header;
202 
203 	/* Bytes from xl_header to the end of the storage */
204 	int xl_size;
205 
206 	/*
207 	 * The ocfs2_xattr_entry this location describes.  If this is
208 	 * NULL, this location describes the on-disk structure where it
209 	 * would have been.
210 	 */
211 	struct ocfs2_xattr_entry *xl_entry;
212 
213 	/*
214 	 * Internal housekeeping
215 	 */
216 
217 	/* Buffer(s) containing this entry */
218 	void *xl_storage;
219 
220 	/* Operations on the storage backing this location */
221 	const struct ocfs2_xa_loc_operations *xl_ops;
222 };
223 
224 /*
225  * Convenience functions to calculate how much space is needed for a
226  * given name+value pair
227  */
228 static int namevalue_size(int name_len, uint64_t value_len)
229 {
230 	if (value_len > OCFS2_XATTR_INLINE_SIZE)
231 		return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
232 	else
233 		return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
234 }
235 
236 static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
237 {
238 	return namevalue_size(xi->xi_name_len, xi->xi_value_len);
239 }
240 
241 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
242 {
243 	u64 value_len = le64_to_cpu(xe->xe_value_size);
244 
245 	BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
246 	       ocfs2_xattr_is_local(xe));
247 	return namevalue_size(xe->xe_name_len, value_len);
248 }
249 
250 
251 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
252 					     struct ocfs2_xattr_header *xh,
253 					     int index,
254 					     int *block_off,
255 					     int *new_offset);
256 
257 static int ocfs2_xattr_block_find(struct inode *inode,
258 				  int name_index,
259 				  const char *name,
260 				  struct ocfs2_xattr_search *xs);
261 static int ocfs2_xattr_index_block_find(struct inode *inode,
262 					struct buffer_head *root_bh,
263 					int name_index,
264 					const char *name,
265 					struct ocfs2_xattr_search *xs);
266 
267 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
268 					struct buffer_head *blk_bh,
269 					char *buffer,
270 					size_t buffer_size);
271 
272 static int ocfs2_xattr_create_index_block(struct inode *inode,
273 					  struct ocfs2_xattr_search *xs,
274 					  struct ocfs2_xattr_set_ctxt *ctxt);
275 
276 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
277 					     struct ocfs2_xattr_info *xi,
278 					     struct ocfs2_xattr_search *xs,
279 					     struct ocfs2_xattr_set_ctxt *ctxt);
280 
281 typedef int (xattr_tree_rec_func)(struct inode *inode,
282 				  struct buffer_head *root_bh,
283 				  u64 blkno, u32 cpos, u32 len, void *para);
284 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
285 					   struct buffer_head *root_bh,
286 					   xattr_tree_rec_func *rec_func,
287 					   void *para);
288 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
289 					struct ocfs2_xattr_bucket *bucket,
290 					void *para);
291 static int ocfs2_rm_xattr_cluster(struct inode *inode,
292 				  struct buffer_head *root_bh,
293 				  u64 blkno,
294 				  u32 cpos,
295 				  u32 len,
296 				  void *para);
297 
298 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
299 				  u64 src_blk, u64 last_blk, u64 to_blk,
300 				  unsigned int start_bucket,
301 				  u32 *first_hash);
302 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
303 					struct ocfs2_dinode *di,
304 					struct ocfs2_xattr_info *xi,
305 					struct ocfs2_xattr_search *xis,
306 					struct ocfs2_xattr_search *xbs,
307 					struct ocfs2_refcount_tree **ref_tree,
308 					int *meta_need,
309 					int *credits);
310 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
311 					   struct ocfs2_xattr_bucket *bucket,
312 					   int offset,
313 					   struct ocfs2_xattr_value_root **xv,
314 					   struct buffer_head **bh);
315 
316 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
317 {
318 	return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
319 }
320 
321 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
322 {
323 	return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
324 }
325 
326 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
327 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
328 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
329 
330 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
331 {
332 	struct ocfs2_xattr_bucket *bucket;
333 	int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
334 
335 	BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
336 
337 	bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
338 	if (bucket) {
339 		bucket->bu_inode = inode;
340 		bucket->bu_blocks = blks;
341 	}
342 
343 	return bucket;
344 }
345 
346 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
347 {
348 	int i;
349 
350 	for (i = 0; i < bucket->bu_blocks; i++) {
351 		brelse(bucket->bu_bhs[i]);
352 		bucket->bu_bhs[i] = NULL;
353 	}
354 }
355 
356 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
357 {
358 	if (bucket) {
359 		ocfs2_xattr_bucket_relse(bucket);
360 		bucket->bu_inode = NULL;
361 		kfree(bucket);
362 	}
363 }
364 
365 /*
366  * A bucket that has never been written to disk doesn't need to be
367  * read.  We just need the buffer_heads.  Don't call this for
368  * buckets that are already on disk.  ocfs2_read_xattr_bucket() initializes
369  * them fully.
370  */
371 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
372 				   u64 xb_blkno)
373 {
374 	int i, rc = 0;
375 
376 	for (i = 0; i < bucket->bu_blocks; i++) {
377 		bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
378 					      xb_blkno + i);
379 		if (!bucket->bu_bhs[i]) {
380 			rc = -EIO;
381 			mlog_errno(rc);
382 			break;
383 		}
384 
385 		if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
386 					   bucket->bu_bhs[i]))
387 			ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
388 						      bucket->bu_bhs[i]);
389 	}
390 
391 	if (rc)
392 		ocfs2_xattr_bucket_relse(bucket);
393 	return rc;
394 }
395 
396 /* Read the xattr bucket at xb_blkno */
397 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
398 				   u64 xb_blkno)
399 {
400 	int rc;
401 
402 	rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
403 			       bucket->bu_blocks, bucket->bu_bhs, 0,
404 			       NULL);
405 	if (!rc) {
406 		spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
407 		rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
408 						 bucket->bu_bhs,
409 						 bucket->bu_blocks,
410 						 &bucket_xh(bucket)->xh_check);
411 		spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
412 		if (rc)
413 			mlog_errno(rc);
414 	}
415 
416 	if (rc)
417 		ocfs2_xattr_bucket_relse(bucket);
418 	return rc;
419 }
420 
421 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
422 					     struct ocfs2_xattr_bucket *bucket,
423 					     int type)
424 {
425 	int i, rc = 0;
426 
427 	for (i = 0; i < bucket->bu_blocks; i++) {
428 		rc = ocfs2_journal_access(handle,
429 					  INODE_CACHE(bucket->bu_inode),
430 					  bucket->bu_bhs[i], type);
431 		if (rc) {
432 			mlog_errno(rc);
433 			break;
434 		}
435 	}
436 
437 	return rc;
438 }
439 
440 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
441 					     struct ocfs2_xattr_bucket *bucket)
442 {
443 	int i;
444 
445 	spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
446 	ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
447 				   bucket->bu_bhs, bucket->bu_blocks,
448 				   &bucket_xh(bucket)->xh_check);
449 	spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
450 
451 	for (i = 0; i < bucket->bu_blocks; i++)
452 		ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
453 }
454 
455 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
456 					 struct ocfs2_xattr_bucket *src)
457 {
458 	int i;
459 	int blocksize = src->bu_inode->i_sb->s_blocksize;
460 
461 	BUG_ON(dest->bu_blocks != src->bu_blocks);
462 	BUG_ON(dest->bu_inode != src->bu_inode);
463 
464 	for (i = 0; i < src->bu_blocks; i++) {
465 		memcpy(bucket_block(dest, i), bucket_block(src, i),
466 		       blocksize);
467 	}
468 }
469 
470 static int ocfs2_validate_xattr_block(struct super_block *sb,
471 				      struct buffer_head *bh)
472 {
473 	int rc;
474 	struct ocfs2_xattr_block *xb =
475 		(struct ocfs2_xattr_block *)bh->b_data;
476 
477 	trace_ocfs2_validate_xattr_block((unsigned long long)bh->b_blocknr);
478 
479 	BUG_ON(!buffer_uptodate(bh));
480 
481 	/*
482 	 * If the ecc fails, we return the error but otherwise
483 	 * leave the filesystem running.  We know any error is
484 	 * local to this block.
485 	 */
486 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
487 	if (rc)
488 		return rc;
489 
490 	/*
491 	 * Errors after here are fatal
492 	 */
493 
494 	if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
495 		ocfs2_error(sb,
496 			    "Extended attribute block #%llu has bad "
497 			    "signature %.*s",
498 			    (unsigned long long)bh->b_blocknr, 7,
499 			    xb->xb_signature);
500 		return -EINVAL;
501 	}
502 
503 	if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
504 		ocfs2_error(sb,
505 			    "Extended attribute block #%llu has an "
506 			    "invalid xb_blkno of %llu",
507 			    (unsigned long long)bh->b_blocknr,
508 			    (unsigned long long)le64_to_cpu(xb->xb_blkno));
509 		return -EINVAL;
510 	}
511 
512 	if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
513 		ocfs2_error(sb,
514 			    "Extended attribute block #%llu has an invalid "
515 			    "xb_fs_generation of #%u",
516 			    (unsigned long long)bh->b_blocknr,
517 			    le32_to_cpu(xb->xb_fs_generation));
518 		return -EINVAL;
519 	}
520 
521 	return 0;
522 }
523 
524 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
525 				  struct buffer_head **bh)
526 {
527 	int rc;
528 	struct buffer_head *tmp = *bh;
529 
530 	rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
531 			      ocfs2_validate_xattr_block);
532 
533 	/* If ocfs2_read_block() got us a new bh, pass it up. */
534 	if (!rc && !*bh)
535 		*bh = tmp;
536 
537 	return rc;
538 }
539 
540 static inline const char *ocfs2_xattr_prefix(int name_index)
541 {
542 	const struct xattr_handler *handler = NULL;
543 
544 	if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
545 		handler = ocfs2_xattr_handler_map[name_index];
546 
547 	return handler ? handler->prefix : NULL;
548 }
549 
550 static u32 ocfs2_xattr_name_hash(struct inode *inode,
551 				 const char *name,
552 				 int name_len)
553 {
554 	/* Get hash value of uuid from super block */
555 	u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
556 	int i;
557 
558 	/* hash extended attribute name */
559 	for (i = 0; i < name_len; i++) {
560 		hash = (hash << OCFS2_HASH_SHIFT) ^
561 		       (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
562 		       *name++;
563 	}
564 
565 	return hash;
566 }
567 
568 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
569 {
570 	return namevalue_size(name_len, value_len) +
571 		sizeof(struct ocfs2_xattr_entry);
572 }
573 
574 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
575 {
576 	return namevalue_size_xi(xi) +
577 		sizeof(struct ocfs2_xattr_entry);
578 }
579 
580 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
581 {
582 	return namevalue_size_xe(xe) +
583 		sizeof(struct ocfs2_xattr_entry);
584 }
585 
586 int ocfs2_calc_security_init(struct inode *dir,
587 			     struct ocfs2_security_xattr_info *si,
588 			     int *want_clusters,
589 			     int *xattr_credits,
590 			     struct ocfs2_alloc_context **xattr_ac)
591 {
592 	int ret = 0;
593 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
594 	int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
595 						 si->value_len);
596 
597 	/*
598 	 * The max space of security xattr taken inline is
599 	 * 256(name) + 80(value) + 16(entry) = 352 bytes,
600 	 * So reserve one metadata block for it is ok.
601 	 */
602 	if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
603 	    s_size > OCFS2_XATTR_FREE_IN_IBODY) {
604 		ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
605 		if (ret) {
606 			mlog_errno(ret);
607 			return ret;
608 		}
609 		*xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
610 	}
611 
612 	/* reserve clusters for xattr value which will be set in B tree*/
613 	if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
614 		int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
615 							    si->value_len);
616 
617 		*xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
618 							   new_clusters);
619 		*want_clusters += new_clusters;
620 	}
621 	return ret;
622 }
623 
624 int ocfs2_calc_xattr_init(struct inode *dir,
625 			  struct buffer_head *dir_bh,
626 			  umode_t mode,
627 			  struct ocfs2_security_xattr_info *si,
628 			  int *want_clusters,
629 			  int *xattr_credits,
630 			  int *want_meta)
631 {
632 	int ret = 0;
633 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
634 	int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
635 
636 	if (si->enable)
637 		s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
638 						     si->value_len);
639 
640 	if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
641 		acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
642 					OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
643 					"", NULL, 0);
644 		if (acl_len > 0) {
645 			a_size = ocfs2_xattr_entry_real_size(0, acl_len);
646 			if (S_ISDIR(mode))
647 				a_size <<= 1;
648 		} else if (acl_len != 0 && acl_len != -ENODATA) {
649 			mlog_errno(ret);
650 			return ret;
651 		}
652 	}
653 
654 	if (!(s_size + a_size))
655 		return ret;
656 
657 	/*
658 	 * The max space of security xattr taken inline is
659 	 * 256(name) + 80(value) + 16(entry) = 352 bytes,
660 	 * The max space of acl xattr taken inline is
661 	 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
662 	 * when blocksize = 512, may reserve one more cluser for
663 	 * xattr bucket, otherwise reserve one metadata block
664 	 * for them is ok.
665 	 * If this is a new directory with inline data,
666 	 * we choose to reserve the entire inline area for
667 	 * directory contents and force an external xattr block.
668 	 */
669 	if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
670 	    (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
671 	    (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
672 		*want_meta = *want_meta + 1;
673 		*xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
674 	}
675 
676 	if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
677 	    (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
678 		*want_clusters += 1;
679 		*xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
680 	}
681 
682 	/*
683 	 * reserve credits and clusters for xattrs which has large value
684 	 * and have to be set outside
685 	 */
686 	if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
687 		new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
688 							si->value_len);
689 		*xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
690 							   new_clusters);
691 		*want_clusters += new_clusters;
692 	}
693 	if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
694 	    acl_len > OCFS2_XATTR_INLINE_SIZE) {
695 		/* for directory, it has DEFAULT and ACCESS two types of acls */
696 		new_clusters = (S_ISDIR(mode) ? 2 : 1) *
697 				ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
698 		*xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
699 							   new_clusters);
700 		*want_clusters += new_clusters;
701 	}
702 
703 	return ret;
704 }
705 
706 static int ocfs2_xattr_extend_allocation(struct inode *inode,
707 					 u32 clusters_to_add,
708 					 struct ocfs2_xattr_value_buf *vb,
709 					 struct ocfs2_xattr_set_ctxt *ctxt)
710 {
711 	int status = 0, credits;
712 	handle_t *handle = ctxt->handle;
713 	enum ocfs2_alloc_restarted why;
714 	u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
715 	struct ocfs2_extent_tree et;
716 
717 	ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
718 
719 	while (clusters_to_add) {
720 		trace_ocfs2_xattr_extend_allocation(clusters_to_add);
721 
722 		status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
723 				       OCFS2_JOURNAL_ACCESS_WRITE);
724 		if (status < 0) {
725 			mlog_errno(status);
726 			break;
727 		}
728 
729 		prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
730 		status = ocfs2_add_clusters_in_btree(handle,
731 						     &et,
732 						     &logical_start,
733 						     clusters_to_add,
734 						     0,
735 						     ctxt->data_ac,
736 						     ctxt->meta_ac,
737 						     &why);
738 		if ((status < 0) && (status != -EAGAIN)) {
739 			if (status != -ENOSPC)
740 				mlog_errno(status);
741 			break;
742 		}
743 
744 		ocfs2_journal_dirty(handle, vb->vb_bh);
745 
746 		clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) -
747 					 prev_clusters;
748 
749 		if (why != RESTART_NONE && clusters_to_add) {
750 			/*
751 			 * We can only fail in case the alloc file doesn't give
752 			 * up enough clusters.
753 			 */
754 			BUG_ON(why == RESTART_META);
755 
756 			credits = ocfs2_calc_extend_credits(inode->i_sb,
757 							    &vb->vb_xv->xr_list,
758 							    clusters_to_add);
759 			status = ocfs2_extend_trans(handle, credits);
760 			if (status < 0) {
761 				status = -ENOMEM;
762 				mlog_errno(status);
763 				break;
764 			}
765 		}
766 	}
767 
768 	return status;
769 }
770 
771 static int __ocfs2_remove_xattr_range(struct inode *inode,
772 				      struct ocfs2_xattr_value_buf *vb,
773 				      u32 cpos, u32 phys_cpos, u32 len,
774 				      unsigned int ext_flags,
775 				      struct ocfs2_xattr_set_ctxt *ctxt)
776 {
777 	int ret;
778 	u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
779 	handle_t *handle = ctxt->handle;
780 	struct ocfs2_extent_tree et;
781 
782 	ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
783 
784 	ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
785 			    OCFS2_JOURNAL_ACCESS_WRITE);
786 	if (ret) {
787 		mlog_errno(ret);
788 		goto out;
789 	}
790 
791 	ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
792 				  &ctxt->dealloc);
793 	if (ret) {
794 		mlog_errno(ret);
795 		goto out;
796 	}
797 
798 	le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
799 	ocfs2_journal_dirty(handle, vb->vb_bh);
800 
801 	if (ext_flags & OCFS2_EXT_REFCOUNTED)
802 		ret = ocfs2_decrease_refcount(inode, handle,
803 					ocfs2_blocks_to_clusters(inode->i_sb,
804 								 phys_blkno),
805 					len, ctxt->meta_ac, &ctxt->dealloc, 1);
806 	else
807 		ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
808 						  phys_blkno, len);
809 	if (ret)
810 		mlog_errno(ret);
811 
812 out:
813 	return ret;
814 }
815 
816 static int ocfs2_xattr_shrink_size(struct inode *inode,
817 				   u32 old_clusters,
818 				   u32 new_clusters,
819 				   struct ocfs2_xattr_value_buf *vb,
820 				   struct ocfs2_xattr_set_ctxt *ctxt)
821 {
822 	int ret = 0;
823 	unsigned int ext_flags;
824 	u32 trunc_len, cpos, phys_cpos, alloc_size;
825 	u64 block;
826 
827 	if (old_clusters <= new_clusters)
828 		return 0;
829 
830 	cpos = new_clusters;
831 	trunc_len = old_clusters - new_clusters;
832 	while (trunc_len) {
833 		ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
834 					       &alloc_size,
835 					       &vb->vb_xv->xr_list, &ext_flags);
836 		if (ret) {
837 			mlog_errno(ret);
838 			goto out;
839 		}
840 
841 		if (alloc_size > trunc_len)
842 			alloc_size = trunc_len;
843 
844 		ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
845 						 phys_cpos, alloc_size,
846 						 ext_flags, ctxt);
847 		if (ret) {
848 			mlog_errno(ret);
849 			goto out;
850 		}
851 
852 		block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
853 		ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
854 						       block, alloc_size);
855 		cpos += alloc_size;
856 		trunc_len -= alloc_size;
857 	}
858 
859 out:
860 	return ret;
861 }
862 
863 static int ocfs2_xattr_value_truncate(struct inode *inode,
864 				      struct ocfs2_xattr_value_buf *vb,
865 				      int len,
866 				      struct ocfs2_xattr_set_ctxt *ctxt)
867 {
868 	int ret;
869 	u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
870 	u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
871 
872 	if (new_clusters == old_clusters)
873 		return 0;
874 
875 	if (new_clusters > old_clusters)
876 		ret = ocfs2_xattr_extend_allocation(inode,
877 						    new_clusters - old_clusters,
878 						    vb, ctxt);
879 	else
880 		ret = ocfs2_xattr_shrink_size(inode,
881 					      old_clusters, new_clusters,
882 					      vb, ctxt);
883 
884 	return ret;
885 }
886 
887 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
888 				  size_t *result, const char *prefix,
889 				  const char *name, int name_len)
890 {
891 	char *p = buffer + *result;
892 	int prefix_len = strlen(prefix);
893 	int total_len = prefix_len + name_len + 1;
894 
895 	*result += total_len;
896 
897 	/* we are just looking for how big our buffer needs to be */
898 	if (!size)
899 		return 0;
900 
901 	if (*result > size)
902 		return -ERANGE;
903 
904 	memcpy(p, prefix, prefix_len);
905 	memcpy(p + prefix_len, name, name_len);
906 	p[prefix_len + name_len] = '\0';
907 
908 	return 0;
909 }
910 
911 static int ocfs2_xattr_list_entries(struct inode *inode,
912 				    struct ocfs2_xattr_header *header,
913 				    char *buffer, size_t buffer_size)
914 {
915 	size_t result = 0;
916 	int i, type, ret;
917 	const char *prefix, *name;
918 
919 	for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
920 		struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
921 		type = ocfs2_xattr_get_type(entry);
922 		prefix = ocfs2_xattr_prefix(type);
923 
924 		if (prefix) {
925 			name = (const char *)header +
926 				le16_to_cpu(entry->xe_name_offset);
927 
928 			ret = ocfs2_xattr_list_entry(buffer, buffer_size,
929 						     &result, prefix, name,
930 						     entry->xe_name_len);
931 			if (ret)
932 				return ret;
933 		}
934 	}
935 
936 	return result;
937 }
938 
939 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
940 					 struct ocfs2_dinode *di)
941 {
942 	struct ocfs2_xattr_header *xh;
943 	int i;
944 
945 	xh = (struct ocfs2_xattr_header *)
946 		 ((void *)di + inode->i_sb->s_blocksize -
947 		 le16_to_cpu(di->i_xattr_inline_size));
948 
949 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
950 		if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
951 			return 1;
952 
953 	return 0;
954 }
955 
956 static int ocfs2_xattr_ibody_list(struct inode *inode,
957 				  struct ocfs2_dinode *di,
958 				  char *buffer,
959 				  size_t buffer_size)
960 {
961 	struct ocfs2_xattr_header *header = NULL;
962 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
963 	int ret = 0;
964 
965 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
966 		return ret;
967 
968 	header = (struct ocfs2_xattr_header *)
969 		 ((void *)di + inode->i_sb->s_blocksize -
970 		 le16_to_cpu(di->i_xattr_inline_size));
971 
972 	ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
973 
974 	return ret;
975 }
976 
977 static int ocfs2_xattr_block_list(struct inode *inode,
978 				  struct ocfs2_dinode *di,
979 				  char *buffer,
980 				  size_t buffer_size)
981 {
982 	struct buffer_head *blk_bh = NULL;
983 	struct ocfs2_xattr_block *xb;
984 	int ret = 0;
985 
986 	if (!di->i_xattr_loc)
987 		return ret;
988 
989 	ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
990 				     &blk_bh);
991 	if (ret < 0) {
992 		mlog_errno(ret);
993 		return ret;
994 	}
995 
996 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
997 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
998 		struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
999 		ret = ocfs2_xattr_list_entries(inode, header,
1000 					       buffer, buffer_size);
1001 	} else
1002 		ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1003 						   buffer, buffer_size);
1004 
1005 	brelse(blk_bh);
1006 
1007 	return ret;
1008 }
1009 
1010 ssize_t ocfs2_listxattr(struct dentry *dentry,
1011 			char *buffer,
1012 			size_t size)
1013 {
1014 	int ret = 0, i_ret = 0, b_ret = 0;
1015 	struct buffer_head *di_bh = NULL;
1016 	struct ocfs2_dinode *di = NULL;
1017 	struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1018 
1019 	if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1020 		return -EOPNOTSUPP;
1021 
1022 	if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1023 		return ret;
1024 
1025 	ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1026 	if (ret < 0) {
1027 		mlog_errno(ret);
1028 		return ret;
1029 	}
1030 
1031 	di = (struct ocfs2_dinode *)di_bh->b_data;
1032 
1033 	down_read(&oi->ip_xattr_sem);
1034 	i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1035 	if (i_ret < 0)
1036 		b_ret = 0;
1037 	else {
1038 		if (buffer) {
1039 			buffer += i_ret;
1040 			size -= i_ret;
1041 		}
1042 		b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1043 					       buffer, size);
1044 		if (b_ret < 0)
1045 			i_ret = 0;
1046 	}
1047 	up_read(&oi->ip_xattr_sem);
1048 	ocfs2_inode_unlock(dentry->d_inode, 0);
1049 
1050 	brelse(di_bh);
1051 
1052 	return i_ret + b_ret;
1053 }
1054 
1055 static int ocfs2_xattr_find_entry(int name_index,
1056 				  const char *name,
1057 				  struct ocfs2_xattr_search *xs)
1058 {
1059 	struct ocfs2_xattr_entry *entry;
1060 	size_t name_len;
1061 	int i, cmp = 1;
1062 
1063 	if (name == NULL)
1064 		return -EINVAL;
1065 
1066 	name_len = strlen(name);
1067 	entry = xs->here;
1068 	for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1069 		cmp = name_index - ocfs2_xattr_get_type(entry);
1070 		if (!cmp)
1071 			cmp = name_len - entry->xe_name_len;
1072 		if (!cmp)
1073 			cmp = memcmp(name, (xs->base +
1074 				     le16_to_cpu(entry->xe_name_offset)),
1075 				     name_len);
1076 		if (cmp == 0)
1077 			break;
1078 		entry += 1;
1079 	}
1080 	xs->here = entry;
1081 
1082 	return cmp ? -ENODATA : 0;
1083 }
1084 
1085 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1086 					 struct ocfs2_xattr_value_root *xv,
1087 					 void *buffer,
1088 					 size_t len)
1089 {
1090 	u32 cpos, p_cluster, num_clusters, bpc, clusters;
1091 	u64 blkno;
1092 	int i, ret = 0;
1093 	size_t cplen, blocksize;
1094 	struct buffer_head *bh = NULL;
1095 	struct ocfs2_extent_list *el;
1096 
1097 	el = &xv->xr_list;
1098 	clusters = le32_to_cpu(xv->xr_clusters);
1099 	bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1100 	blocksize = inode->i_sb->s_blocksize;
1101 
1102 	cpos = 0;
1103 	while (cpos < clusters) {
1104 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1105 					       &num_clusters, el, NULL);
1106 		if (ret) {
1107 			mlog_errno(ret);
1108 			goto out;
1109 		}
1110 
1111 		blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1112 		/* Copy ocfs2_xattr_value */
1113 		for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1114 			ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1115 					       &bh, NULL);
1116 			if (ret) {
1117 				mlog_errno(ret);
1118 				goto out;
1119 			}
1120 
1121 			cplen = len >= blocksize ? blocksize : len;
1122 			memcpy(buffer, bh->b_data, cplen);
1123 			len -= cplen;
1124 			buffer += cplen;
1125 
1126 			brelse(bh);
1127 			bh = NULL;
1128 			if (len == 0)
1129 				break;
1130 		}
1131 		cpos += num_clusters;
1132 	}
1133 out:
1134 	return ret;
1135 }
1136 
1137 static int ocfs2_xattr_ibody_get(struct inode *inode,
1138 				 int name_index,
1139 				 const char *name,
1140 				 void *buffer,
1141 				 size_t buffer_size,
1142 				 struct ocfs2_xattr_search *xs)
1143 {
1144 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1145 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1146 	struct ocfs2_xattr_value_root *xv;
1147 	size_t size;
1148 	int ret = 0;
1149 
1150 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1151 		return -ENODATA;
1152 
1153 	xs->end = (void *)di + inode->i_sb->s_blocksize;
1154 	xs->header = (struct ocfs2_xattr_header *)
1155 			(xs->end - le16_to_cpu(di->i_xattr_inline_size));
1156 	xs->base = (void *)xs->header;
1157 	xs->here = xs->header->xh_entries;
1158 
1159 	ret = ocfs2_xattr_find_entry(name_index, name, xs);
1160 	if (ret)
1161 		return ret;
1162 	size = le64_to_cpu(xs->here->xe_value_size);
1163 	if (buffer) {
1164 		if (size > buffer_size)
1165 			return -ERANGE;
1166 		if (ocfs2_xattr_is_local(xs->here)) {
1167 			memcpy(buffer, (void *)xs->base +
1168 			       le16_to_cpu(xs->here->xe_name_offset) +
1169 			       OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1170 		} else {
1171 			xv = (struct ocfs2_xattr_value_root *)
1172 				(xs->base + le16_to_cpu(
1173 				 xs->here->xe_name_offset) +
1174 				OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1175 			ret = ocfs2_xattr_get_value_outside(inode, xv,
1176 							    buffer, size);
1177 			if (ret < 0) {
1178 				mlog_errno(ret);
1179 				return ret;
1180 			}
1181 		}
1182 	}
1183 
1184 	return size;
1185 }
1186 
1187 static int ocfs2_xattr_block_get(struct inode *inode,
1188 				 int name_index,
1189 				 const char *name,
1190 				 void *buffer,
1191 				 size_t buffer_size,
1192 				 struct ocfs2_xattr_search *xs)
1193 {
1194 	struct ocfs2_xattr_block *xb;
1195 	struct ocfs2_xattr_value_root *xv;
1196 	size_t size;
1197 	int ret = -ENODATA, name_offset, name_len, i;
1198 	int uninitialized_var(block_off);
1199 
1200 	xs->bucket = ocfs2_xattr_bucket_new(inode);
1201 	if (!xs->bucket) {
1202 		ret = -ENOMEM;
1203 		mlog_errno(ret);
1204 		goto cleanup;
1205 	}
1206 
1207 	ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1208 	if (ret) {
1209 		mlog_errno(ret);
1210 		goto cleanup;
1211 	}
1212 
1213 	if (xs->not_found) {
1214 		ret = -ENODATA;
1215 		goto cleanup;
1216 	}
1217 
1218 	xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1219 	size = le64_to_cpu(xs->here->xe_value_size);
1220 	if (buffer) {
1221 		ret = -ERANGE;
1222 		if (size > buffer_size)
1223 			goto cleanup;
1224 
1225 		name_offset = le16_to_cpu(xs->here->xe_name_offset);
1226 		name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1227 		i = xs->here - xs->header->xh_entries;
1228 
1229 		if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1230 			ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1231 								bucket_xh(xs->bucket),
1232 								i,
1233 								&block_off,
1234 								&name_offset);
1235 			xs->base = bucket_block(xs->bucket, block_off);
1236 		}
1237 		if (ocfs2_xattr_is_local(xs->here)) {
1238 			memcpy(buffer, (void *)xs->base +
1239 			       name_offset + name_len, size);
1240 		} else {
1241 			xv = (struct ocfs2_xattr_value_root *)
1242 				(xs->base + name_offset + name_len);
1243 			ret = ocfs2_xattr_get_value_outside(inode, xv,
1244 							    buffer, size);
1245 			if (ret < 0) {
1246 				mlog_errno(ret);
1247 				goto cleanup;
1248 			}
1249 		}
1250 	}
1251 	ret = size;
1252 cleanup:
1253 	ocfs2_xattr_bucket_free(xs->bucket);
1254 
1255 	brelse(xs->xattr_bh);
1256 	xs->xattr_bh = NULL;
1257 	return ret;
1258 }
1259 
1260 int ocfs2_xattr_get_nolock(struct inode *inode,
1261 			   struct buffer_head *di_bh,
1262 			   int name_index,
1263 			   const char *name,
1264 			   void *buffer,
1265 			   size_t buffer_size)
1266 {
1267 	int ret;
1268 	struct ocfs2_dinode *di = NULL;
1269 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1270 	struct ocfs2_xattr_search xis = {
1271 		.not_found = -ENODATA,
1272 	};
1273 	struct ocfs2_xattr_search xbs = {
1274 		.not_found = -ENODATA,
1275 	};
1276 
1277 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1278 		return -EOPNOTSUPP;
1279 
1280 	if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1281 		ret = -ENODATA;
1282 
1283 	xis.inode_bh = xbs.inode_bh = di_bh;
1284 	di = (struct ocfs2_dinode *)di_bh->b_data;
1285 
1286 	ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1287 				    buffer_size, &xis);
1288 	if (ret == -ENODATA && di->i_xattr_loc)
1289 		ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1290 					    buffer_size, &xbs);
1291 
1292 	return ret;
1293 }
1294 
1295 /* ocfs2_xattr_get()
1296  *
1297  * Copy an extended attribute into the buffer provided.
1298  * Buffer is NULL to compute the size of buffer required.
1299  */
1300 static int ocfs2_xattr_get(struct inode *inode,
1301 			   int name_index,
1302 			   const char *name,
1303 			   void *buffer,
1304 			   size_t buffer_size)
1305 {
1306 	int ret;
1307 	struct buffer_head *di_bh = NULL;
1308 
1309 	ret = ocfs2_inode_lock(inode, &di_bh, 0);
1310 	if (ret < 0) {
1311 		mlog_errno(ret);
1312 		return ret;
1313 	}
1314 	down_read(&OCFS2_I(inode)->ip_xattr_sem);
1315 	ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1316 				     name, buffer, buffer_size);
1317 	up_read(&OCFS2_I(inode)->ip_xattr_sem);
1318 
1319 	ocfs2_inode_unlock(inode, 0);
1320 
1321 	brelse(di_bh);
1322 
1323 	return ret;
1324 }
1325 
1326 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1327 					   handle_t *handle,
1328 					   struct ocfs2_xattr_value_buf *vb,
1329 					   const void *value,
1330 					   int value_len)
1331 {
1332 	int ret = 0, i, cp_len;
1333 	u16 blocksize = inode->i_sb->s_blocksize;
1334 	u32 p_cluster, num_clusters;
1335 	u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1336 	u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1337 	u64 blkno;
1338 	struct buffer_head *bh = NULL;
1339 	unsigned int ext_flags;
1340 	struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1341 
1342 	BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1343 
1344 	while (cpos < clusters) {
1345 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1346 					       &num_clusters, &xv->xr_list,
1347 					       &ext_flags);
1348 		if (ret) {
1349 			mlog_errno(ret);
1350 			goto out;
1351 		}
1352 
1353 		BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1354 
1355 		blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1356 
1357 		for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1358 			ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1359 					       &bh, NULL);
1360 			if (ret) {
1361 				mlog_errno(ret);
1362 				goto out;
1363 			}
1364 
1365 			ret = ocfs2_journal_access(handle,
1366 						   INODE_CACHE(inode),
1367 						   bh,
1368 						   OCFS2_JOURNAL_ACCESS_WRITE);
1369 			if (ret < 0) {
1370 				mlog_errno(ret);
1371 				goto out;
1372 			}
1373 
1374 			cp_len = value_len > blocksize ? blocksize : value_len;
1375 			memcpy(bh->b_data, value, cp_len);
1376 			value_len -= cp_len;
1377 			value += cp_len;
1378 			if (cp_len < blocksize)
1379 				memset(bh->b_data + cp_len, 0,
1380 				       blocksize - cp_len);
1381 
1382 			ocfs2_journal_dirty(handle, bh);
1383 			brelse(bh);
1384 			bh = NULL;
1385 
1386 			/*
1387 			 * XXX: do we need to empty all the following
1388 			 * blocks in this cluster?
1389 			 */
1390 			if (!value_len)
1391 				break;
1392 		}
1393 		cpos += num_clusters;
1394 	}
1395 out:
1396 	brelse(bh);
1397 
1398 	return ret;
1399 }
1400 
1401 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1402 				       int num_entries)
1403 {
1404 	int free_space;
1405 
1406 	if (!needed_space)
1407 		return 0;
1408 
1409 	free_space = free_start -
1410 		sizeof(struct ocfs2_xattr_header) -
1411 		(num_entries * sizeof(struct ocfs2_xattr_entry)) -
1412 		OCFS2_XATTR_HEADER_GAP;
1413 	if (free_space < 0)
1414 		return -EIO;
1415 	if (free_space < needed_space)
1416 		return -ENOSPC;
1417 
1418 	return 0;
1419 }
1420 
1421 static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1422 				   int type)
1423 {
1424 	return loc->xl_ops->xlo_journal_access(handle, loc, type);
1425 }
1426 
1427 static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1428 {
1429 	loc->xl_ops->xlo_journal_dirty(handle, loc);
1430 }
1431 
1432 /* Give a pointer into the storage for the given offset */
1433 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1434 {
1435 	BUG_ON(offset >= loc->xl_size);
1436 	return loc->xl_ops->xlo_offset_pointer(loc, offset);
1437 }
1438 
1439 /*
1440  * Wipe the name+value pair and allow the storage to reclaim it.  This
1441  * must be followed by either removal of the entry or a call to
1442  * ocfs2_xa_add_namevalue().
1443  */
1444 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1445 {
1446 	loc->xl_ops->xlo_wipe_namevalue(loc);
1447 }
1448 
1449 /*
1450  * Find lowest offset to a name+value pair.  This is the start of our
1451  * downward-growing free space.
1452  */
1453 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1454 {
1455 	return loc->xl_ops->xlo_get_free_start(loc);
1456 }
1457 
1458 /* Can we reuse loc->xl_entry for xi? */
1459 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1460 				    struct ocfs2_xattr_info *xi)
1461 {
1462 	return loc->xl_ops->xlo_can_reuse(loc, xi);
1463 }
1464 
1465 /* How much free space is needed to set the new value */
1466 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1467 				struct ocfs2_xattr_info *xi)
1468 {
1469 	return loc->xl_ops->xlo_check_space(loc, xi);
1470 }
1471 
1472 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1473 {
1474 	loc->xl_ops->xlo_add_entry(loc, name_hash);
1475 	loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1476 	/*
1477 	 * We can't leave the new entry's xe_name_offset at zero or
1478 	 * add_namevalue() will go nuts.  We set it to the size of our
1479 	 * storage so that it can never be less than any other entry.
1480 	 */
1481 	loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1482 }
1483 
1484 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1485 				   struct ocfs2_xattr_info *xi)
1486 {
1487 	int size = namevalue_size_xi(xi);
1488 	int nameval_offset;
1489 	char *nameval_buf;
1490 
1491 	loc->xl_ops->xlo_add_namevalue(loc, size);
1492 	loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1493 	loc->xl_entry->xe_name_len = xi->xi_name_len;
1494 	ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1495 	ocfs2_xattr_set_local(loc->xl_entry,
1496 			      xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1497 
1498 	nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1499 	nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1500 	memset(nameval_buf, 0, size);
1501 	memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1502 }
1503 
1504 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1505 				    struct ocfs2_xattr_value_buf *vb)
1506 {
1507 	int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1508 	int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1509 
1510 	/* Value bufs are for value trees */
1511 	BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1512 	BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1513 	       (name_size + OCFS2_XATTR_ROOT_SIZE));
1514 
1515 	loc->xl_ops->xlo_fill_value_buf(loc, vb);
1516 	vb->vb_xv =
1517 		(struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1518 							nameval_offset +
1519 							name_size);
1520 }
1521 
1522 static int ocfs2_xa_block_journal_access(handle_t *handle,
1523 					 struct ocfs2_xa_loc *loc, int type)
1524 {
1525 	struct buffer_head *bh = loc->xl_storage;
1526 	ocfs2_journal_access_func access;
1527 
1528 	if (loc->xl_size == (bh->b_size -
1529 			     offsetof(struct ocfs2_xattr_block,
1530 				      xb_attrs.xb_header)))
1531 		access = ocfs2_journal_access_xb;
1532 	else
1533 		access = ocfs2_journal_access_di;
1534 	return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1535 }
1536 
1537 static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1538 					 struct ocfs2_xa_loc *loc)
1539 {
1540 	struct buffer_head *bh = loc->xl_storage;
1541 
1542 	ocfs2_journal_dirty(handle, bh);
1543 }
1544 
1545 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1546 					   int offset)
1547 {
1548 	return (char *)loc->xl_header + offset;
1549 }
1550 
1551 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1552 				    struct ocfs2_xattr_info *xi)
1553 {
1554 	/*
1555 	 * Block storage is strict.  If the sizes aren't exact, we will
1556 	 * remove the old one and reinsert the new.
1557 	 */
1558 	return namevalue_size_xe(loc->xl_entry) ==
1559 		namevalue_size_xi(xi);
1560 }
1561 
1562 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1563 {
1564 	struct ocfs2_xattr_header *xh = loc->xl_header;
1565 	int i, count = le16_to_cpu(xh->xh_count);
1566 	int offset, free_start = loc->xl_size;
1567 
1568 	for (i = 0; i < count; i++) {
1569 		offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1570 		if (offset < free_start)
1571 			free_start = offset;
1572 	}
1573 
1574 	return free_start;
1575 }
1576 
1577 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1578 				      struct ocfs2_xattr_info *xi)
1579 {
1580 	int count = le16_to_cpu(loc->xl_header->xh_count);
1581 	int free_start = ocfs2_xa_get_free_start(loc);
1582 	int needed_space = ocfs2_xi_entry_usage(xi);
1583 
1584 	/*
1585 	 * Block storage will reclaim the original entry before inserting
1586 	 * the new value, so we only need the difference.  If the new
1587 	 * entry is smaller than the old one, we don't need anything.
1588 	 */
1589 	if (loc->xl_entry) {
1590 		/* Don't need space if we're reusing! */
1591 		if (ocfs2_xa_can_reuse_entry(loc, xi))
1592 			needed_space = 0;
1593 		else
1594 			needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1595 	}
1596 	if (needed_space < 0)
1597 		needed_space = 0;
1598 	return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1599 }
1600 
1601 /*
1602  * Block storage for xattrs keeps the name+value pairs compacted.  When
1603  * we remove one, we have to shift any that preceded it towards the end.
1604  */
1605 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1606 {
1607 	int i, offset;
1608 	int namevalue_offset, first_namevalue_offset, namevalue_size;
1609 	struct ocfs2_xattr_entry *entry = loc->xl_entry;
1610 	struct ocfs2_xattr_header *xh = loc->xl_header;
1611 	int count = le16_to_cpu(xh->xh_count);
1612 
1613 	namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1614 	namevalue_size = namevalue_size_xe(entry);
1615 	first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1616 
1617 	/* Shift the name+value pairs */
1618 	memmove((char *)xh + first_namevalue_offset + namevalue_size,
1619 		(char *)xh + first_namevalue_offset,
1620 		namevalue_offset - first_namevalue_offset);
1621 	memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1622 
1623 	/* Now tell xh->xh_entries about it */
1624 	for (i = 0; i < count; i++) {
1625 		offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1626 		if (offset <= namevalue_offset)
1627 			le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1628 				     namevalue_size);
1629 	}
1630 
1631 	/*
1632 	 * Note that we don't update xh_free_start or xh_name_value_len
1633 	 * because they're not used in block-stored xattrs.
1634 	 */
1635 }
1636 
1637 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1638 {
1639 	int count = le16_to_cpu(loc->xl_header->xh_count);
1640 	loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1641 	le16_add_cpu(&loc->xl_header->xh_count, 1);
1642 	memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1643 }
1644 
1645 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1646 {
1647 	int free_start = ocfs2_xa_get_free_start(loc);
1648 
1649 	loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1650 }
1651 
1652 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1653 					  struct ocfs2_xattr_value_buf *vb)
1654 {
1655 	struct buffer_head *bh = loc->xl_storage;
1656 
1657 	if (loc->xl_size == (bh->b_size -
1658 			     offsetof(struct ocfs2_xattr_block,
1659 				      xb_attrs.xb_header)))
1660 		vb->vb_access = ocfs2_journal_access_xb;
1661 	else
1662 		vb->vb_access = ocfs2_journal_access_di;
1663 	vb->vb_bh = bh;
1664 }
1665 
1666 /*
1667  * Operations for xattrs stored in blocks.  This includes inline inode
1668  * storage and unindexed ocfs2_xattr_blocks.
1669  */
1670 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1671 	.xlo_journal_access	= ocfs2_xa_block_journal_access,
1672 	.xlo_journal_dirty	= ocfs2_xa_block_journal_dirty,
1673 	.xlo_offset_pointer	= ocfs2_xa_block_offset_pointer,
1674 	.xlo_check_space	= ocfs2_xa_block_check_space,
1675 	.xlo_can_reuse		= ocfs2_xa_block_can_reuse,
1676 	.xlo_get_free_start	= ocfs2_xa_block_get_free_start,
1677 	.xlo_wipe_namevalue	= ocfs2_xa_block_wipe_namevalue,
1678 	.xlo_add_entry		= ocfs2_xa_block_add_entry,
1679 	.xlo_add_namevalue	= ocfs2_xa_block_add_namevalue,
1680 	.xlo_fill_value_buf	= ocfs2_xa_block_fill_value_buf,
1681 };
1682 
1683 static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1684 					  struct ocfs2_xa_loc *loc, int type)
1685 {
1686 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1687 
1688 	return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1689 }
1690 
1691 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1692 					  struct ocfs2_xa_loc *loc)
1693 {
1694 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1695 
1696 	ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1697 }
1698 
1699 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1700 					    int offset)
1701 {
1702 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1703 	int block, block_offset;
1704 
1705 	/* The header is at the front of the bucket */
1706 	block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1707 	block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1708 
1709 	return bucket_block(bucket, block) + block_offset;
1710 }
1711 
1712 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1713 				     struct ocfs2_xattr_info *xi)
1714 {
1715 	return namevalue_size_xe(loc->xl_entry) >=
1716 		namevalue_size_xi(xi);
1717 }
1718 
1719 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1720 {
1721 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1722 	return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1723 }
1724 
1725 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1726 					 int free_start, int size)
1727 {
1728 	/*
1729 	 * We need to make sure that the name+value pair fits within
1730 	 * one block.
1731 	 */
1732 	if (((free_start - size) >> sb->s_blocksize_bits) !=
1733 	    ((free_start - 1) >> sb->s_blocksize_bits))
1734 		free_start -= free_start % sb->s_blocksize;
1735 
1736 	return free_start;
1737 }
1738 
1739 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1740 				       struct ocfs2_xattr_info *xi)
1741 {
1742 	int rc;
1743 	int count = le16_to_cpu(loc->xl_header->xh_count);
1744 	int free_start = ocfs2_xa_get_free_start(loc);
1745 	int needed_space = ocfs2_xi_entry_usage(xi);
1746 	int size = namevalue_size_xi(xi);
1747 	struct super_block *sb = loc->xl_inode->i_sb;
1748 
1749 	/*
1750 	 * Bucket storage does not reclaim name+value pairs it cannot
1751 	 * reuse.  They live as holes until the bucket fills, and then
1752 	 * the bucket is defragmented.  However, the bucket can reclaim
1753 	 * the ocfs2_xattr_entry.
1754 	 */
1755 	if (loc->xl_entry) {
1756 		/* Don't need space if we're reusing! */
1757 		if (ocfs2_xa_can_reuse_entry(loc, xi))
1758 			needed_space = 0;
1759 		else
1760 			needed_space -= sizeof(struct ocfs2_xattr_entry);
1761 	}
1762 	BUG_ON(needed_space < 0);
1763 
1764 	if (free_start < size) {
1765 		if (needed_space)
1766 			return -ENOSPC;
1767 	} else {
1768 		/*
1769 		 * First we check if it would fit in the first place.
1770 		 * Below, we align the free start to a block.  This may
1771 		 * slide us below the minimum gap.  By checking unaligned
1772 		 * first, we avoid that error.
1773 		 */
1774 		rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1775 						 count);
1776 		if (rc)
1777 			return rc;
1778 		free_start = ocfs2_bucket_align_free_start(sb, free_start,
1779 							   size);
1780 	}
1781 	return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1782 }
1783 
1784 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1785 {
1786 	le16_add_cpu(&loc->xl_header->xh_name_value_len,
1787 		     -namevalue_size_xe(loc->xl_entry));
1788 }
1789 
1790 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1791 {
1792 	struct ocfs2_xattr_header *xh = loc->xl_header;
1793 	int count = le16_to_cpu(xh->xh_count);
1794 	int low = 0, high = count - 1, tmp;
1795 	struct ocfs2_xattr_entry *tmp_xe;
1796 
1797 	/*
1798 	 * We keep buckets sorted by name_hash, so we need to find
1799 	 * our insert place.
1800 	 */
1801 	while (low <= high && count) {
1802 		tmp = (low + high) / 2;
1803 		tmp_xe = &xh->xh_entries[tmp];
1804 
1805 		if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1806 			low = tmp + 1;
1807 		else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1808 			high = tmp - 1;
1809 		else {
1810 			low = tmp;
1811 			break;
1812 		}
1813 	}
1814 
1815 	if (low != count)
1816 		memmove(&xh->xh_entries[low + 1],
1817 			&xh->xh_entries[low],
1818 			((count - low) * sizeof(struct ocfs2_xattr_entry)));
1819 
1820 	le16_add_cpu(&xh->xh_count, 1);
1821 	loc->xl_entry = &xh->xh_entries[low];
1822 	memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1823 }
1824 
1825 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1826 {
1827 	int free_start = ocfs2_xa_get_free_start(loc);
1828 	struct ocfs2_xattr_header *xh = loc->xl_header;
1829 	struct super_block *sb = loc->xl_inode->i_sb;
1830 	int nameval_offset;
1831 
1832 	free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1833 	nameval_offset = free_start - size;
1834 	loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1835 	xh->xh_free_start = cpu_to_le16(nameval_offset);
1836 	le16_add_cpu(&xh->xh_name_value_len, size);
1837 
1838 }
1839 
1840 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1841 					   struct ocfs2_xattr_value_buf *vb)
1842 {
1843 	struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1844 	struct super_block *sb = loc->xl_inode->i_sb;
1845 	int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1846 	int size = namevalue_size_xe(loc->xl_entry);
1847 	int block_offset = nameval_offset >> sb->s_blocksize_bits;
1848 
1849 	/* Values are not allowed to straddle block boundaries */
1850 	BUG_ON(block_offset !=
1851 	       ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1852 	/* We expect the bucket to be filled in */
1853 	BUG_ON(!bucket->bu_bhs[block_offset]);
1854 
1855 	vb->vb_access = ocfs2_journal_access;
1856 	vb->vb_bh = bucket->bu_bhs[block_offset];
1857 }
1858 
1859 /* Operations for xattrs stored in buckets. */
1860 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1861 	.xlo_journal_access	= ocfs2_xa_bucket_journal_access,
1862 	.xlo_journal_dirty	= ocfs2_xa_bucket_journal_dirty,
1863 	.xlo_offset_pointer	= ocfs2_xa_bucket_offset_pointer,
1864 	.xlo_check_space	= ocfs2_xa_bucket_check_space,
1865 	.xlo_can_reuse		= ocfs2_xa_bucket_can_reuse,
1866 	.xlo_get_free_start	= ocfs2_xa_bucket_get_free_start,
1867 	.xlo_wipe_namevalue	= ocfs2_xa_bucket_wipe_namevalue,
1868 	.xlo_add_entry		= ocfs2_xa_bucket_add_entry,
1869 	.xlo_add_namevalue	= ocfs2_xa_bucket_add_namevalue,
1870 	.xlo_fill_value_buf	= ocfs2_xa_bucket_fill_value_buf,
1871 };
1872 
1873 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1874 {
1875 	struct ocfs2_xattr_value_buf vb;
1876 
1877 	if (ocfs2_xattr_is_local(loc->xl_entry))
1878 		return 0;
1879 
1880 	ocfs2_xa_fill_value_buf(loc, &vb);
1881 	return le32_to_cpu(vb.vb_xv->xr_clusters);
1882 }
1883 
1884 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1885 				   struct ocfs2_xattr_set_ctxt *ctxt)
1886 {
1887 	int trunc_rc, access_rc;
1888 	struct ocfs2_xattr_value_buf vb;
1889 
1890 	ocfs2_xa_fill_value_buf(loc, &vb);
1891 	trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1892 					      ctxt);
1893 
1894 	/*
1895 	 * The caller of ocfs2_xa_value_truncate() has already called
1896 	 * ocfs2_xa_journal_access on the loc.  However, The truncate code
1897 	 * calls ocfs2_extend_trans().  This may commit the previous
1898 	 * transaction and open a new one.  If this is a bucket, truncate
1899 	 * could leave only vb->vb_bh set up for journaling.  Meanwhile,
1900 	 * the caller is expecting to dirty the entire bucket.  So we must
1901 	 * reset the journal work.  We do this even if truncate has failed,
1902 	 * as it could have failed after committing the extend.
1903 	 */
1904 	access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1905 					    OCFS2_JOURNAL_ACCESS_WRITE);
1906 
1907 	/* Errors in truncate take precedence */
1908 	return trunc_rc ? trunc_rc : access_rc;
1909 }
1910 
1911 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1912 {
1913 	int index, count;
1914 	struct ocfs2_xattr_header *xh = loc->xl_header;
1915 	struct ocfs2_xattr_entry *entry = loc->xl_entry;
1916 
1917 	ocfs2_xa_wipe_namevalue(loc);
1918 	loc->xl_entry = NULL;
1919 
1920 	le16_add_cpu(&xh->xh_count, -1);
1921 	count = le16_to_cpu(xh->xh_count);
1922 
1923 	/*
1924 	 * Only zero out the entry if there are more remaining.  This is
1925 	 * important for an empty bucket, as it keeps track of the
1926 	 * bucket's hash value.  It doesn't hurt empty block storage.
1927 	 */
1928 	if (count) {
1929 		index = ((char *)entry - (char *)&xh->xh_entries) /
1930 			sizeof(struct ocfs2_xattr_entry);
1931 		memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1932 			(count - index) * sizeof(struct ocfs2_xattr_entry));
1933 		memset(&xh->xh_entries[count], 0,
1934 		       sizeof(struct ocfs2_xattr_entry));
1935 	}
1936 }
1937 
1938 /*
1939  * If we have a problem adjusting the size of an external value during
1940  * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1941  * in an intermediate state.  For example, the value may be partially
1942  * truncated.
1943  *
1944  * If the value tree hasn't changed, the extend/truncate went nowhere.
1945  * We have nothing to do.  The caller can treat it as a straight error.
1946  *
1947  * If the value tree got partially truncated, we now have a corrupted
1948  * extended attribute.  We're going to wipe its entry and leak the
1949  * clusters.  Better to leak some storage than leave a corrupt entry.
1950  *
1951  * If the value tree grew, it obviously didn't grow enough for the
1952  * new entry.  We're not going to try and reclaim those clusters either.
1953  * If there was already an external value there (orig_clusters != 0),
1954  * the new clusters are attached safely and we can just leave the old
1955  * value in place.  If there was no external value there, we remove
1956  * the entry.
1957  *
1958  * This way, the xattr block we store in the journal will be consistent.
1959  * If the size change broke because of the journal, no changes will hit
1960  * disk anyway.
1961  */
1962 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1963 					    const char *what,
1964 					    unsigned int orig_clusters)
1965 {
1966 	unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1967 	char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1968 				le16_to_cpu(loc->xl_entry->xe_name_offset));
1969 
1970 	if (new_clusters < orig_clusters) {
1971 		mlog(ML_ERROR,
1972 		     "Partial truncate while %s xattr %.*s.  Leaking "
1973 		     "%u clusters and removing the entry\n",
1974 		     what, loc->xl_entry->xe_name_len, nameval_buf,
1975 		     orig_clusters - new_clusters);
1976 		ocfs2_xa_remove_entry(loc);
1977 	} else if (!orig_clusters) {
1978 		mlog(ML_ERROR,
1979 		     "Unable to allocate an external value for xattr "
1980 		     "%.*s safely.  Leaking %u clusters and removing the "
1981 		     "entry\n",
1982 		     loc->xl_entry->xe_name_len, nameval_buf,
1983 		     new_clusters - orig_clusters);
1984 		ocfs2_xa_remove_entry(loc);
1985 	} else if (new_clusters > orig_clusters)
1986 		mlog(ML_ERROR,
1987 		     "Unable to grow xattr %.*s safely.  %u new clusters "
1988 		     "have been added, but the value will not be "
1989 		     "modified\n",
1990 		     loc->xl_entry->xe_name_len, nameval_buf,
1991 		     new_clusters - orig_clusters);
1992 }
1993 
1994 static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1995 			   struct ocfs2_xattr_set_ctxt *ctxt)
1996 {
1997 	int rc = 0;
1998 	unsigned int orig_clusters;
1999 
2000 	if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2001 		orig_clusters = ocfs2_xa_value_clusters(loc);
2002 		rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2003 		if (rc) {
2004 			mlog_errno(rc);
2005 			/*
2006 			 * Since this is remove, we can return 0 if
2007 			 * ocfs2_xa_cleanup_value_truncate() is going to
2008 			 * wipe the entry anyway.  So we check the
2009 			 * cluster count as well.
2010 			 */
2011 			if (orig_clusters != ocfs2_xa_value_clusters(loc))
2012 				rc = 0;
2013 			ocfs2_xa_cleanup_value_truncate(loc, "removing",
2014 							orig_clusters);
2015 			if (rc)
2016 				goto out;
2017 		}
2018 	}
2019 
2020 	ocfs2_xa_remove_entry(loc);
2021 
2022 out:
2023 	return rc;
2024 }
2025 
2026 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2027 {
2028 	int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2029 	char *nameval_buf;
2030 
2031 	nameval_buf = ocfs2_xa_offset_pointer(loc,
2032 				le16_to_cpu(loc->xl_entry->xe_name_offset));
2033 	memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2034 }
2035 
2036 /*
2037  * Take an existing entry and make it ready for the new value.  This
2038  * won't allocate space, but it may free space.  It should be ready for
2039  * ocfs2_xa_prepare_entry() to finish the work.
2040  */
2041 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2042 				struct ocfs2_xattr_info *xi,
2043 				struct ocfs2_xattr_set_ctxt *ctxt)
2044 {
2045 	int rc = 0;
2046 	int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2047 	unsigned int orig_clusters;
2048 	char *nameval_buf;
2049 	int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2050 	int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2051 
2052 	BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2053 	       name_size);
2054 
2055 	nameval_buf = ocfs2_xa_offset_pointer(loc,
2056 				le16_to_cpu(loc->xl_entry->xe_name_offset));
2057 	if (xe_local) {
2058 		memset(nameval_buf + name_size, 0,
2059 		       namevalue_size_xe(loc->xl_entry) - name_size);
2060 		if (!xi_local)
2061 			ocfs2_xa_install_value_root(loc);
2062 	} else {
2063 		orig_clusters = ocfs2_xa_value_clusters(loc);
2064 		if (xi_local) {
2065 			rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2066 			if (rc < 0)
2067 				mlog_errno(rc);
2068 			else
2069 				memset(nameval_buf + name_size, 0,
2070 				       namevalue_size_xe(loc->xl_entry) -
2071 				       name_size);
2072 		} else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2073 			   xi->xi_value_len) {
2074 			rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2075 						     ctxt);
2076 			if (rc < 0)
2077 				mlog_errno(rc);
2078 		}
2079 
2080 		if (rc) {
2081 			ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2082 							orig_clusters);
2083 			goto out;
2084 		}
2085 	}
2086 
2087 	loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2088 	ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2089 
2090 out:
2091 	return rc;
2092 }
2093 
2094 /*
2095  * Prepares loc->xl_entry to receive the new xattr.  This includes
2096  * properly setting up the name+value pair region.  If loc->xl_entry
2097  * already exists, it will take care of modifying it appropriately.
2098  *
2099  * Note that this modifies the data.  You did journal_access already,
2100  * right?
2101  */
2102 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2103 				  struct ocfs2_xattr_info *xi,
2104 				  u32 name_hash,
2105 				  struct ocfs2_xattr_set_ctxt *ctxt)
2106 {
2107 	int rc = 0;
2108 	unsigned int orig_clusters;
2109 	__le64 orig_value_size = 0;
2110 
2111 	rc = ocfs2_xa_check_space(loc, xi);
2112 	if (rc)
2113 		goto out;
2114 
2115 	if (loc->xl_entry) {
2116 		if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2117 			orig_value_size = loc->xl_entry->xe_value_size;
2118 			rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2119 			if (rc)
2120 				goto out;
2121 			goto alloc_value;
2122 		}
2123 
2124 		if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2125 			orig_clusters = ocfs2_xa_value_clusters(loc);
2126 			rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2127 			if (rc) {
2128 				mlog_errno(rc);
2129 				ocfs2_xa_cleanup_value_truncate(loc,
2130 								"overwriting",
2131 								orig_clusters);
2132 				goto out;
2133 			}
2134 		}
2135 		ocfs2_xa_wipe_namevalue(loc);
2136 	} else
2137 		ocfs2_xa_add_entry(loc, name_hash);
2138 
2139 	/*
2140 	 * If we get here, we have a blank entry.  Fill it.  We grow our
2141 	 * name+value pair back from the end.
2142 	 */
2143 	ocfs2_xa_add_namevalue(loc, xi);
2144 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2145 		ocfs2_xa_install_value_root(loc);
2146 
2147 alloc_value:
2148 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2149 		orig_clusters = ocfs2_xa_value_clusters(loc);
2150 		rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2151 		if (rc < 0) {
2152 			ctxt->set_abort = 1;
2153 			ocfs2_xa_cleanup_value_truncate(loc, "growing",
2154 							orig_clusters);
2155 			/*
2156 			 * If we were growing an existing value,
2157 			 * ocfs2_xa_cleanup_value_truncate() won't remove
2158 			 * the entry. We need to restore the original value
2159 			 * size.
2160 			 */
2161 			if (loc->xl_entry) {
2162 				BUG_ON(!orig_value_size);
2163 				loc->xl_entry->xe_value_size = orig_value_size;
2164 			}
2165 			mlog_errno(rc);
2166 		}
2167 	}
2168 
2169 out:
2170 	return rc;
2171 }
2172 
2173 /*
2174  * Store the value portion of the name+value pair.  This will skip
2175  * values that are stored externally.  Their tree roots were set up
2176  * by ocfs2_xa_prepare_entry().
2177  */
2178 static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2179 				struct ocfs2_xattr_info *xi,
2180 				struct ocfs2_xattr_set_ctxt *ctxt)
2181 {
2182 	int rc = 0;
2183 	int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2184 	int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2185 	char *nameval_buf;
2186 	struct ocfs2_xattr_value_buf vb;
2187 
2188 	nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2189 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2190 		ocfs2_xa_fill_value_buf(loc, &vb);
2191 		rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2192 						     ctxt->handle, &vb,
2193 						     xi->xi_value,
2194 						     xi->xi_value_len);
2195 	} else
2196 		memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2197 
2198 	return rc;
2199 }
2200 
2201 static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2202 			struct ocfs2_xattr_info *xi,
2203 			struct ocfs2_xattr_set_ctxt *ctxt)
2204 {
2205 	int ret;
2206 	u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2207 					      xi->xi_name_len);
2208 
2209 	ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2210 				      OCFS2_JOURNAL_ACCESS_WRITE);
2211 	if (ret) {
2212 		mlog_errno(ret);
2213 		goto out;
2214 	}
2215 
2216 	/*
2217 	 * From here on out, everything is going to modify the buffer a
2218 	 * little.  Errors are going to leave the xattr header in a
2219 	 * sane state.  Thus, even with errors we dirty the sucker.
2220 	 */
2221 
2222 	/* Don't worry, we are never called with !xi_value and !xl_entry */
2223 	if (!xi->xi_value) {
2224 		ret = ocfs2_xa_remove(loc, ctxt);
2225 		goto out_dirty;
2226 	}
2227 
2228 	ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2229 	if (ret) {
2230 		if (ret != -ENOSPC)
2231 			mlog_errno(ret);
2232 		goto out_dirty;
2233 	}
2234 
2235 	ret = ocfs2_xa_store_value(loc, xi, ctxt);
2236 	if (ret)
2237 		mlog_errno(ret);
2238 
2239 out_dirty:
2240 	ocfs2_xa_journal_dirty(ctxt->handle, loc);
2241 
2242 out:
2243 	return ret;
2244 }
2245 
2246 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2247 				     struct inode *inode,
2248 				     struct buffer_head *bh,
2249 				     struct ocfs2_xattr_entry *entry)
2250 {
2251 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2252 
2253 	BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2254 
2255 	loc->xl_inode = inode;
2256 	loc->xl_ops = &ocfs2_xa_block_loc_ops;
2257 	loc->xl_storage = bh;
2258 	loc->xl_entry = entry;
2259 	loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2260 	loc->xl_header =
2261 		(struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2262 					      loc->xl_size);
2263 }
2264 
2265 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2266 					  struct inode *inode,
2267 					  struct buffer_head *bh,
2268 					  struct ocfs2_xattr_entry *entry)
2269 {
2270 	struct ocfs2_xattr_block *xb =
2271 		(struct ocfs2_xattr_block *)bh->b_data;
2272 
2273 	BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2274 
2275 	loc->xl_inode = inode;
2276 	loc->xl_ops = &ocfs2_xa_block_loc_ops;
2277 	loc->xl_storage = bh;
2278 	loc->xl_header = &(xb->xb_attrs.xb_header);
2279 	loc->xl_entry = entry;
2280 	loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2281 					     xb_attrs.xb_header);
2282 }
2283 
2284 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2285 					   struct ocfs2_xattr_bucket *bucket,
2286 					   struct ocfs2_xattr_entry *entry)
2287 {
2288 	loc->xl_inode = bucket->bu_inode;
2289 	loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2290 	loc->xl_storage = bucket;
2291 	loc->xl_header = bucket_xh(bucket);
2292 	loc->xl_entry = entry;
2293 	loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2294 }
2295 
2296 /*
2297  * In xattr remove, if it is stored outside and refcounted, we may have
2298  * the chance to split the refcount tree. So need the allocators.
2299  */
2300 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2301 					struct ocfs2_xattr_value_root *xv,
2302 					struct ocfs2_caching_info *ref_ci,
2303 					struct buffer_head *ref_root_bh,
2304 					struct ocfs2_alloc_context **meta_ac,
2305 					int *ref_credits)
2306 {
2307 	int ret, meta_add = 0;
2308 	u32 p_cluster, num_clusters;
2309 	unsigned int ext_flags;
2310 
2311 	*ref_credits = 0;
2312 	ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2313 				       &num_clusters,
2314 				       &xv->xr_list,
2315 				       &ext_flags);
2316 	if (ret) {
2317 		mlog_errno(ret);
2318 		goto out;
2319 	}
2320 
2321 	if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2322 		goto out;
2323 
2324 	ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2325 						 ref_root_bh, xv,
2326 						 &meta_add, ref_credits);
2327 	if (ret) {
2328 		mlog_errno(ret);
2329 		goto out;
2330 	}
2331 
2332 	ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2333 						meta_add, meta_ac);
2334 	if (ret)
2335 		mlog_errno(ret);
2336 
2337 out:
2338 	return ret;
2339 }
2340 
2341 static int ocfs2_remove_value_outside(struct inode*inode,
2342 				      struct ocfs2_xattr_value_buf *vb,
2343 				      struct ocfs2_xattr_header *header,
2344 				      struct ocfs2_caching_info *ref_ci,
2345 				      struct buffer_head *ref_root_bh)
2346 {
2347 	int ret = 0, i, ref_credits;
2348 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2349 	struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2350 	void *val;
2351 
2352 	ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2353 
2354 	for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2355 		struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2356 
2357 		if (ocfs2_xattr_is_local(entry))
2358 			continue;
2359 
2360 		val = (void *)header +
2361 			le16_to_cpu(entry->xe_name_offset);
2362 		vb->vb_xv = (struct ocfs2_xattr_value_root *)
2363 			(val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2364 
2365 		ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2366 							 ref_ci, ref_root_bh,
2367 							 &ctxt.meta_ac,
2368 							 &ref_credits);
2369 
2370 		ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2371 					ocfs2_remove_extent_credits(osb->sb));
2372 		if (IS_ERR(ctxt.handle)) {
2373 			ret = PTR_ERR(ctxt.handle);
2374 			mlog_errno(ret);
2375 			break;
2376 		}
2377 
2378 		ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2379 
2380 		ocfs2_commit_trans(osb, ctxt.handle);
2381 		if (ctxt.meta_ac) {
2382 			ocfs2_free_alloc_context(ctxt.meta_ac);
2383 			ctxt.meta_ac = NULL;
2384 		}
2385 
2386 		if (ret < 0) {
2387 			mlog_errno(ret);
2388 			break;
2389 		}
2390 
2391 	}
2392 
2393 	if (ctxt.meta_ac)
2394 		ocfs2_free_alloc_context(ctxt.meta_ac);
2395 	ocfs2_schedule_truncate_log_flush(osb, 1);
2396 	ocfs2_run_deallocs(osb, &ctxt.dealloc);
2397 	return ret;
2398 }
2399 
2400 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2401 				    struct buffer_head *di_bh,
2402 				    struct ocfs2_caching_info *ref_ci,
2403 				    struct buffer_head *ref_root_bh)
2404 {
2405 
2406 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2407 	struct ocfs2_xattr_header *header;
2408 	int ret;
2409 	struct ocfs2_xattr_value_buf vb = {
2410 		.vb_bh = di_bh,
2411 		.vb_access = ocfs2_journal_access_di,
2412 	};
2413 
2414 	header = (struct ocfs2_xattr_header *)
2415 		 ((void *)di + inode->i_sb->s_blocksize -
2416 		 le16_to_cpu(di->i_xattr_inline_size));
2417 
2418 	ret = ocfs2_remove_value_outside(inode, &vb, header,
2419 					 ref_ci, ref_root_bh);
2420 
2421 	return ret;
2422 }
2423 
2424 struct ocfs2_rm_xattr_bucket_para {
2425 	struct ocfs2_caching_info *ref_ci;
2426 	struct buffer_head *ref_root_bh;
2427 };
2428 
2429 static int ocfs2_xattr_block_remove(struct inode *inode,
2430 				    struct buffer_head *blk_bh,
2431 				    struct ocfs2_caching_info *ref_ci,
2432 				    struct buffer_head *ref_root_bh)
2433 {
2434 	struct ocfs2_xattr_block *xb;
2435 	int ret = 0;
2436 	struct ocfs2_xattr_value_buf vb = {
2437 		.vb_bh = blk_bh,
2438 		.vb_access = ocfs2_journal_access_xb,
2439 	};
2440 	struct ocfs2_rm_xattr_bucket_para args = {
2441 		.ref_ci = ref_ci,
2442 		.ref_root_bh = ref_root_bh,
2443 	};
2444 
2445 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2446 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2447 		struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2448 		ret = ocfs2_remove_value_outside(inode, &vb, header,
2449 						 ref_ci, ref_root_bh);
2450 	} else
2451 		ret = ocfs2_iterate_xattr_index_block(inode,
2452 						blk_bh,
2453 						ocfs2_rm_xattr_cluster,
2454 						&args);
2455 
2456 	return ret;
2457 }
2458 
2459 static int ocfs2_xattr_free_block(struct inode *inode,
2460 				  u64 block,
2461 				  struct ocfs2_caching_info *ref_ci,
2462 				  struct buffer_head *ref_root_bh)
2463 {
2464 	struct inode *xb_alloc_inode;
2465 	struct buffer_head *xb_alloc_bh = NULL;
2466 	struct buffer_head *blk_bh = NULL;
2467 	struct ocfs2_xattr_block *xb;
2468 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2469 	handle_t *handle;
2470 	int ret = 0;
2471 	u64 blk, bg_blkno;
2472 	u16 bit;
2473 
2474 	ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2475 	if (ret < 0) {
2476 		mlog_errno(ret);
2477 		goto out;
2478 	}
2479 
2480 	ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2481 	if (ret < 0) {
2482 		mlog_errno(ret);
2483 		goto out;
2484 	}
2485 
2486 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2487 	blk = le64_to_cpu(xb->xb_blkno);
2488 	bit = le16_to_cpu(xb->xb_suballoc_bit);
2489 	if (xb->xb_suballoc_loc)
2490 		bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2491 	else
2492 		bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2493 
2494 	xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2495 				EXTENT_ALLOC_SYSTEM_INODE,
2496 				le16_to_cpu(xb->xb_suballoc_slot));
2497 	if (!xb_alloc_inode) {
2498 		ret = -ENOMEM;
2499 		mlog_errno(ret);
2500 		goto out;
2501 	}
2502 	mutex_lock(&xb_alloc_inode->i_mutex);
2503 
2504 	ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2505 	if (ret < 0) {
2506 		mlog_errno(ret);
2507 		goto out_mutex;
2508 	}
2509 
2510 	handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2511 	if (IS_ERR(handle)) {
2512 		ret = PTR_ERR(handle);
2513 		mlog_errno(ret);
2514 		goto out_unlock;
2515 	}
2516 
2517 	ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2518 				       bit, bg_blkno, 1);
2519 	if (ret < 0)
2520 		mlog_errno(ret);
2521 
2522 	ocfs2_commit_trans(osb, handle);
2523 out_unlock:
2524 	ocfs2_inode_unlock(xb_alloc_inode, 1);
2525 	brelse(xb_alloc_bh);
2526 out_mutex:
2527 	mutex_unlock(&xb_alloc_inode->i_mutex);
2528 	iput(xb_alloc_inode);
2529 out:
2530 	brelse(blk_bh);
2531 	return ret;
2532 }
2533 
2534 /*
2535  * ocfs2_xattr_remove()
2536  *
2537  * Free extended attribute resources associated with this inode.
2538  */
2539 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2540 {
2541 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2542 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2543 	struct ocfs2_refcount_tree *ref_tree = NULL;
2544 	struct buffer_head *ref_root_bh = NULL;
2545 	struct ocfs2_caching_info *ref_ci = NULL;
2546 	handle_t *handle;
2547 	int ret;
2548 
2549 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2550 		return 0;
2551 
2552 	if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2553 		return 0;
2554 
2555 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2556 		ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2557 					       le64_to_cpu(di->i_refcount_loc),
2558 					       1, &ref_tree, &ref_root_bh);
2559 		if (ret) {
2560 			mlog_errno(ret);
2561 			goto out;
2562 		}
2563 		ref_ci = &ref_tree->rf_ci;
2564 
2565 	}
2566 
2567 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2568 		ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2569 					       ref_ci, ref_root_bh);
2570 		if (ret < 0) {
2571 			mlog_errno(ret);
2572 			goto out;
2573 		}
2574 	}
2575 
2576 	if (di->i_xattr_loc) {
2577 		ret = ocfs2_xattr_free_block(inode,
2578 					     le64_to_cpu(di->i_xattr_loc),
2579 					     ref_ci, ref_root_bh);
2580 		if (ret < 0) {
2581 			mlog_errno(ret);
2582 			goto out;
2583 		}
2584 	}
2585 
2586 	handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2587 				   OCFS2_INODE_UPDATE_CREDITS);
2588 	if (IS_ERR(handle)) {
2589 		ret = PTR_ERR(handle);
2590 		mlog_errno(ret);
2591 		goto out;
2592 	}
2593 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2594 				      OCFS2_JOURNAL_ACCESS_WRITE);
2595 	if (ret) {
2596 		mlog_errno(ret);
2597 		goto out_commit;
2598 	}
2599 
2600 	di->i_xattr_loc = 0;
2601 
2602 	spin_lock(&oi->ip_lock);
2603 	oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2604 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2605 	spin_unlock(&oi->ip_lock);
2606 
2607 	ocfs2_journal_dirty(handle, di_bh);
2608 out_commit:
2609 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2610 out:
2611 	if (ref_tree)
2612 		ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2613 	brelse(ref_root_bh);
2614 	return ret;
2615 }
2616 
2617 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2618 					struct ocfs2_dinode *di)
2619 {
2620 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2621 	unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2622 	int free;
2623 
2624 	if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2625 		return 0;
2626 
2627 	if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2628 		struct ocfs2_inline_data *idata = &di->id2.i_data;
2629 		free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2630 	} else if (ocfs2_inode_is_fast_symlink(inode)) {
2631 		free = ocfs2_fast_symlink_chars(inode->i_sb) -
2632 			le64_to_cpu(di->i_size);
2633 	} else {
2634 		struct ocfs2_extent_list *el = &di->id2.i_list;
2635 		free = (le16_to_cpu(el->l_count) -
2636 			le16_to_cpu(el->l_next_free_rec)) *
2637 			sizeof(struct ocfs2_extent_rec);
2638 	}
2639 	if (free >= xattrsize)
2640 		return 1;
2641 
2642 	return 0;
2643 }
2644 
2645 /*
2646  * ocfs2_xattr_ibody_find()
2647  *
2648  * Find extended attribute in inode block and
2649  * fill search info into struct ocfs2_xattr_search.
2650  */
2651 static int ocfs2_xattr_ibody_find(struct inode *inode,
2652 				  int name_index,
2653 				  const char *name,
2654 				  struct ocfs2_xattr_search *xs)
2655 {
2656 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2657 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2658 	int ret;
2659 	int has_space = 0;
2660 
2661 	if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2662 		return 0;
2663 
2664 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2665 		down_read(&oi->ip_alloc_sem);
2666 		has_space = ocfs2_xattr_has_space_inline(inode, di);
2667 		up_read(&oi->ip_alloc_sem);
2668 		if (!has_space)
2669 			return 0;
2670 	}
2671 
2672 	xs->xattr_bh = xs->inode_bh;
2673 	xs->end = (void *)di + inode->i_sb->s_blocksize;
2674 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2675 		xs->header = (struct ocfs2_xattr_header *)
2676 			(xs->end - le16_to_cpu(di->i_xattr_inline_size));
2677 	else
2678 		xs->header = (struct ocfs2_xattr_header *)
2679 			(xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2680 	xs->base = (void *)xs->header;
2681 	xs->here = xs->header->xh_entries;
2682 
2683 	/* Find the named attribute. */
2684 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2685 		ret = ocfs2_xattr_find_entry(name_index, name, xs);
2686 		if (ret && ret != -ENODATA)
2687 			return ret;
2688 		xs->not_found = ret;
2689 	}
2690 
2691 	return 0;
2692 }
2693 
2694 static int ocfs2_xattr_ibody_init(struct inode *inode,
2695 				  struct buffer_head *di_bh,
2696 				  struct ocfs2_xattr_set_ctxt *ctxt)
2697 {
2698 	int ret;
2699 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2700 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2701 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2702 	unsigned int xattrsize = osb->s_xattr_inline_size;
2703 
2704 	if (!ocfs2_xattr_has_space_inline(inode, di)) {
2705 		ret = -ENOSPC;
2706 		goto out;
2707 	}
2708 
2709 	ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2710 				      OCFS2_JOURNAL_ACCESS_WRITE);
2711 	if (ret) {
2712 		mlog_errno(ret);
2713 		goto out;
2714 	}
2715 
2716 	/*
2717 	 * Adjust extent record count or inline data size
2718 	 * to reserve space for extended attribute.
2719 	 */
2720 	if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2721 		struct ocfs2_inline_data *idata = &di->id2.i_data;
2722 		le16_add_cpu(&idata->id_count, -xattrsize);
2723 	} else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2724 		struct ocfs2_extent_list *el = &di->id2.i_list;
2725 		le16_add_cpu(&el->l_count, -(xattrsize /
2726 					     sizeof(struct ocfs2_extent_rec)));
2727 	}
2728 	di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2729 
2730 	spin_lock(&oi->ip_lock);
2731 	oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2732 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2733 	spin_unlock(&oi->ip_lock);
2734 
2735 	ocfs2_journal_dirty(ctxt->handle, di_bh);
2736 
2737 out:
2738 	return ret;
2739 }
2740 
2741 /*
2742  * ocfs2_xattr_ibody_set()
2743  *
2744  * Set, replace or remove an extended attribute into inode block.
2745  *
2746  */
2747 static int ocfs2_xattr_ibody_set(struct inode *inode,
2748 				 struct ocfs2_xattr_info *xi,
2749 				 struct ocfs2_xattr_search *xs,
2750 				 struct ocfs2_xattr_set_ctxt *ctxt)
2751 {
2752 	int ret;
2753 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2754 	struct ocfs2_xa_loc loc;
2755 
2756 	if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2757 		return -ENOSPC;
2758 
2759 	down_write(&oi->ip_alloc_sem);
2760 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2761 		ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2762 		if (ret) {
2763 			if (ret != -ENOSPC)
2764 				mlog_errno(ret);
2765 			goto out;
2766 		}
2767 	}
2768 
2769 	ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2770 				 xs->not_found ? NULL : xs->here);
2771 	ret = ocfs2_xa_set(&loc, xi, ctxt);
2772 	if (ret) {
2773 		if (ret != -ENOSPC)
2774 			mlog_errno(ret);
2775 		goto out;
2776 	}
2777 	xs->here = loc.xl_entry;
2778 
2779 out:
2780 	up_write(&oi->ip_alloc_sem);
2781 
2782 	return ret;
2783 }
2784 
2785 /*
2786  * ocfs2_xattr_block_find()
2787  *
2788  * Find extended attribute in external block and
2789  * fill search info into struct ocfs2_xattr_search.
2790  */
2791 static int ocfs2_xattr_block_find(struct inode *inode,
2792 				  int name_index,
2793 				  const char *name,
2794 				  struct ocfs2_xattr_search *xs)
2795 {
2796 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2797 	struct buffer_head *blk_bh = NULL;
2798 	struct ocfs2_xattr_block *xb;
2799 	int ret = 0;
2800 
2801 	if (!di->i_xattr_loc)
2802 		return ret;
2803 
2804 	ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2805 				     &blk_bh);
2806 	if (ret < 0) {
2807 		mlog_errno(ret);
2808 		return ret;
2809 	}
2810 
2811 	xs->xattr_bh = blk_bh;
2812 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2813 
2814 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2815 		xs->header = &xb->xb_attrs.xb_header;
2816 		xs->base = (void *)xs->header;
2817 		xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2818 		xs->here = xs->header->xh_entries;
2819 
2820 		ret = ocfs2_xattr_find_entry(name_index, name, xs);
2821 	} else
2822 		ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2823 						   name_index,
2824 						   name, xs);
2825 
2826 	if (ret && ret != -ENODATA) {
2827 		xs->xattr_bh = NULL;
2828 		goto cleanup;
2829 	}
2830 	xs->not_found = ret;
2831 	return 0;
2832 cleanup:
2833 	brelse(blk_bh);
2834 
2835 	return ret;
2836 }
2837 
2838 static int ocfs2_create_xattr_block(struct inode *inode,
2839 				    struct buffer_head *inode_bh,
2840 				    struct ocfs2_xattr_set_ctxt *ctxt,
2841 				    int indexed,
2842 				    struct buffer_head **ret_bh)
2843 {
2844 	int ret;
2845 	u16 suballoc_bit_start;
2846 	u32 num_got;
2847 	u64 suballoc_loc, first_blkno;
2848 	struct ocfs2_dinode *di =  (struct ocfs2_dinode *)inode_bh->b_data;
2849 	struct buffer_head *new_bh = NULL;
2850 	struct ocfs2_xattr_block *xblk;
2851 
2852 	ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2853 				      inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2854 	if (ret < 0) {
2855 		mlog_errno(ret);
2856 		goto end;
2857 	}
2858 
2859 	ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
2860 				   &suballoc_loc, &suballoc_bit_start,
2861 				   &num_got, &first_blkno);
2862 	if (ret < 0) {
2863 		mlog_errno(ret);
2864 		goto end;
2865 	}
2866 
2867 	new_bh = sb_getblk(inode->i_sb, first_blkno);
2868 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2869 
2870 	ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2871 				      new_bh,
2872 				      OCFS2_JOURNAL_ACCESS_CREATE);
2873 	if (ret < 0) {
2874 		mlog_errno(ret);
2875 		goto end;
2876 	}
2877 
2878 	/* Initialize ocfs2_xattr_block */
2879 	xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2880 	memset(xblk, 0, inode->i_sb->s_blocksize);
2881 	strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2882 	xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2883 	xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2884 	xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2885 	xblk->xb_fs_generation =
2886 		cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2887 	xblk->xb_blkno = cpu_to_le64(first_blkno);
2888 	if (indexed) {
2889 		struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2890 		xr->xt_clusters = cpu_to_le32(1);
2891 		xr->xt_last_eb_blk = 0;
2892 		xr->xt_list.l_tree_depth = 0;
2893 		xr->xt_list.l_count = cpu_to_le16(
2894 					ocfs2_xattr_recs_per_xb(inode->i_sb));
2895 		xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2896 		xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2897 	}
2898 	ocfs2_journal_dirty(ctxt->handle, new_bh);
2899 
2900 	/* Add it to the inode */
2901 	di->i_xattr_loc = cpu_to_le64(first_blkno);
2902 
2903 	spin_lock(&OCFS2_I(inode)->ip_lock);
2904 	OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2905 	di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2906 	spin_unlock(&OCFS2_I(inode)->ip_lock);
2907 
2908 	ocfs2_journal_dirty(ctxt->handle, inode_bh);
2909 
2910 	*ret_bh = new_bh;
2911 	new_bh = NULL;
2912 
2913 end:
2914 	brelse(new_bh);
2915 	return ret;
2916 }
2917 
2918 /*
2919  * ocfs2_xattr_block_set()
2920  *
2921  * Set, replace or remove an extended attribute into external block.
2922  *
2923  */
2924 static int ocfs2_xattr_block_set(struct inode *inode,
2925 				 struct ocfs2_xattr_info *xi,
2926 				 struct ocfs2_xattr_search *xs,
2927 				 struct ocfs2_xattr_set_ctxt *ctxt)
2928 {
2929 	struct buffer_head *new_bh = NULL;
2930 	struct ocfs2_xattr_block *xblk = NULL;
2931 	int ret;
2932 	struct ocfs2_xa_loc loc;
2933 
2934 	if (!xs->xattr_bh) {
2935 		ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2936 					       0, &new_bh);
2937 		if (ret) {
2938 			mlog_errno(ret);
2939 			goto end;
2940 		}
2941 
2942 		xs->xattr_bh = new_bh;
2943 		xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2944 		xs->header = &xblk->xb_attrs.xb_header;
2945 		xs->base = (void *)xs->header;
2946 		xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2947 		xs->here = xs->header->xh_entries;
2948 	} else
2949 		xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2950 
2951 	if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2952 		ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2953 					      xs->not_found ? NULL : xs->here);
2954 
2955 		ret = ocfs2_xa_set(&loc, xi, ctxt);
2956 		if (!ret)
2957 			xs->here = loc.xl_entry;
2958 		else if ((ret != -ENOSPC) || ctxt->set_abort)
2959 			goto end;
2960 		else {
2961 			ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2962 			if (ret)
2963 				goto end;
2964 		}
2965 	}
2966 
2967 	if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2968 		ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2969 
2970 end:
2971 	return ret;
2972 }
2973 
2974 /* Check whether the new xattr can be inserted into the inode. */
2975 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2976 				       struct ocfs2_xattr_info *xi,
2977 				       struct ocfs2_xattr_search *xs)
2978 {
2979 	struct ocfs2_xattr_entry *last;
2980 	int free, i;
2981 	size_t min_offs = xs->end - xs->base;
2982 
2983 	if (!xs->header)
2984 		return 0;
2985 
2986 	last = xs->header->xh_entries;
2987 
2988 	for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2989 		size_t offs = le16_to_cpu(last->xe_name_offset);
2990 		if (offs < min_offs)
2991 			min_offs = offs;
2992 		last += 1;
2993 	}
2994 
2995 	free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2996 	if (free < 0)
2997 		return 0;
2998 
2999 	BUG_ON(!xs->not_found);
3000 
3001 	if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
3002 		return 1;
3003 
3004 	return 0;
3005 }
3006 
3007 static int ocfs2_calc_xattr_set_need(struct inode *inode,
3008 				     struct ocfs2_dinode *di,
3009 				     struct ocfs2_xattr_info *xi,
3010 				     struct ocfs2_xattr_search *xis,
3011 				     struct ocfs2_xattr_search *xbs,
3012 				     int *clusters_need,
3013 				     int *meta_need,
3014 				     int *credits_need)
3015 {
3016 	int ret = 0, old_in_xb = 0;
3017 	int clusters_add = 0, meta_add = 0, credits = 0;
3018 	struct buffer_head *bh = NULL;
3019 	struct ocfs2_xattr_block *xb = NULL;
3020 	struct ocfs2_xattr_entry *xe = NULL;
3021 	struct ocfs2_xattr_value_root *xv = NULL;
3022 	char *base = NULL;
3023 	int name_offset, name_len = 0;
3024 	u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3025 						    xi->xi_value_len);
3026 	u64 value_size;
3027 
3028 	/*
3029 	 * Calculate the clusters we need to write.
3030 	 * No matter whether we replace an old one or add a new one,
3031 	 * we need this for writing.
3032 	 */
3033 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
3034 		credits += new_clusters *
3035 			   ocfs2_clusters_to_blocks(inode->i_sb, 1);
3036 
3037 	if (xis->not_found && xbs->not_found) {
3038 		credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3039 
3040 		if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3041 			clusters_add += new_clusters;
3042 			credits += ocfs2_calc_extend_credits(inode->i_sb,
3043 							&def_xv.xv.xr_list,
3044 							new_clusters);
3045 		}
3046 
3047 		goto meta_guess;
3048 	}
3049 
3050 	if (!xis->not_found) {
3051 		xe = xis->here;
3052 		name_offset = le16_to_cpu(xe->xe_name_offset);
3053 		name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3054 		base = xis->base;
3055 		credits += OCFS2_INODE_UPDATE_CREDITS;
3056 	} else {
3057 		int i, block_off = 0;
3058 		xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3059 		xe = xbs->here;
3060 		name_offset = le16_to_cpu(xe->xe_name_offset);
3061 		name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3062 		i = xbs->here - xbs->header->xh_entries;
3063 		old_in_xb = 1;
3064 
3065 		if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3066 			ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3067 							bucket_xh(xbs->bucket),
3068 							i, &block_off,
3069 							&name_offset);
3070 			base = bucket_block(xbs->bucket, block_off);
3071 			credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3072 		} else {
3073 			base = xbs->base;
3074 			credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3075 		}
3076 	}
3077 
3078 	/*
3079 	 * delete a xattr doesn't need metadata and cluster allocation.
3080 	 * so just calculate the credits and return.
3081 	 *
3082 	 * The credits for removing the value tree will be extended
3083 	 * by ocfs2_remove_extent itself.
3084 	 */
3085 	if (!xi->xi_value) {
3086 		if (!ocfs2_xattr_is_local(xe))
3087 			credits += ocfs2_remove_extent_credits(inode->i_sb);
3088 
3089 		goto out;
3090 	}
3091 
3092 	/* do cluster allocation guess first. */
3093 	value_size = le64_to_cpu(xe->xe_value_size);
3094 
3095 	if (old_in_xb) {
3096 		/*
3097 		 * In xattr set, we always try to set the xe in inode first,
3098 		 * so if it can be inserted into inode successfully, the old
3099 		 * one will be removed from the xattr block, and this xattr
3100 		 * will be inserted into inode as a new xattr in inode.
3101 		 */
3102 		if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3103 			clusters_add += new_clusters;
3104 			credits += ocfs2_remove_extent_credits(inode->i_sb) +
3105 				    OCFS2_INODE_UPDATE_CREDITS;
3106 			if (!ocfs2_xattr_is_local(xe))
3107 				credits += ocfs2_calc_extend_credits(
3108 							inode->i_sb,
3109 							&def_xv.xv.xr_list,
3110 							new_clusters);
3111 			goto out;
3112 		}
3113 	}
3114 
3115 	if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3116 		/* the new values will be stored outside. */
3117 		u32 old_clusters = 0;
3118 
3119 		if (!ocfs2_xattr_is_local(xe)) {
3120 			old_clusters =	ocfs2_clusters_for_bytes(inode->i_sb,
3121 								 value_size);
3122 			xv = (struct ocfs2_xattr_value_root *)
3123 			     (base + name_offset + name_len);
3124 			value_size = OCFS2_XATTR_ROOT_SIZE;
3125 		} else
3126 			xv = &def_xv.xv;
3127 
3128 		if (old_clusters >= new_clusters) {
3129 			credits += ocfs2_remove_extent_credits(inode->i_sb);
3130 			goto out;
3131 		} else {
3132 			meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3133 			clusters_add += new_clusters - old_clusters;
3134 			credits += ocfs2_calc_extend_credits(inode->i_sb,
3135 							     &xv->xr_list,
3136 							     new_clusters -
3137 							     old_clusters);
3138 			if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3139 				goto out;
3140 		}
3141 	} else {
3142 		/*
3143 		 * Now the new value will be stored inside. So if the new
3144 		 * value is smaller than the size of value root or the old
3145 		 * value, we don't need any allocation, otherwise we have
3146 		 * to guess metadata allocation.
3147 		 */
3148 		if ((ocfs2_xattr_is_local(xe) &&
3149 		     (value_size >= xi->xi_value_len)) ||
3150 		    (!ocfs2_xattr_is_local(xe) &&
3151 		     OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3152 			goto out;
3153 	}
3154 
3155 meta_guess:
3156 	/* calculate metadata allocation. */
3157 	if (di->i_xattr_loc) {
3158 		if (!xbs->xattr_bh) {
3159 			ret = ocfs2_read_xattr_block(inode,
3160 						     le64_to_cpu(di->i_xattr_loc),
3161 						     &bh);
3162 			if (ret) {
3163 				mlog_errno(ret);
3164 				goto out;
3165 			}
3166 
3167 			xb = (struct ocfs2_xattr_block *)bh->b_data;
3168 		} else
3169 			xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3170 
3171 		/*
3172 		 * If there is already an xattr tree, good, we can calculate
3173 		 * like other b-trees. Otherwise we may have the chance of
3174 		 * create a tree, the credit calculation is borrowed from
3175 		 * ocfs2_calc_extend_credits with root_el = NULL. And the
3176 		 * new tree will be cluster based, so no meta is needed.
3177 		 */
3178 		if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3179 			struct ocfs2_extent_list *el =
3180 				 &xb->xb_attrs.xb_root.xt_list;
3181 			meta_add += ocfs2_extend_meta_needed(el);
3182 			credits += ocfs2_calc_extend_credits(inode->i_sb,
3183 							     el, 1);
3184 		} else
3185 			credits += OCFS2_SUBALLOC_ALLOC + 1;
3186 
3187 		/*
3188 		 * This cluster will be used either for new bucket or for
3189 		 * new xattr block.
3190 		 * If the cluster size is the same as the bucket size, one
3191 		 * more is needed since we may need to extend the bucket
3192 		 * also.
3193 		 */
3194 		clusters_add += 1;
3195 		credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3196 		if (OCFS2_XATTR_BUCKET_SIZE ==
3197 			OCFS2_SB(inode->i_sb)->s_clustersize) {
3198 			credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3199 			clusters_add += 1;
3200 		}
3201 	} else {
3202 		meta_add += 1;
3203 		credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3204 	}
3205 out:
3206 	if (clusters_need)
3207 		*clusters_need = clusters_add;
3208 	if (meta_need)
3209 		*meta_need = meta_add;
3210 	if (credits_need)
3211 		*credits_need = credits;
3212 	brelse(bh);
3213 	return ret;
3214 }
3215 
3216 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3217 				     struct ocfs2_dinode *di,
3218 				     struct ocfs2_xattr_info *xi,
3219 				     struct ocfs2_xattr_search *xis,
3220 				     struct ocfs2_xattr_search *xbs,
3221 				     struct ocfs2_xattr_set_ctxt *ctxt,
3222 				     int extra_meta,
3223 				     int *credits)
3224 {
3225 	int clusters_add, meta_add, ret;
3226 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3227 
3228 	memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3229 
3230 	ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3231 
3232 	ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3233 					&clusters_add, &meta_add, credits);
3234 	if (ret) {
3235 		mlog_errno(ret);
3236 		return ret;
3237 	}
3238 
3239 	meta_add += extra_meta;
3240 	trace_ocfs2_init_xattr_set_ctxt(xi->xi_name, meta_add,
3241 					clusters_add, *credits);
3242 
3243 	if (meta_add) {
3244 		ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3245 							&ctxt->meta_ac);
3246 		if (ret) {
3247 			mlog_errno(ret);
3248 			goto out;
3249 		}
3250 	}
3251 
3252 	if (clusters_add) {
3253 		ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3254 		if (ret)
3255 			mlog_errno(ret);
3256 	}
3257 out:
3258 	if (ret) {
3259 		if (ctxt->meta_ac) {
3260 			ocfs2_free_alloc_context(ctxt->meta_ac);
3261 			ctxt->meta_ac = NULL;
3262 		}
3263 
3264 		/*
3265 		 * We cannot have an error and a non null ctxt->data_ac.
3266 		 */
3267 	}
3268 
3269 	return ret;
3270 }
3271 
3272 static int __ocfs2_xattr_set_handle(struct inode *inode,
3273 				    struct ocfs2_dinode *di,
3274 				    struct ocfs2_xattr_info *xi,
3275 				    struct ocfs2_xattr_search *xis,
3276 				    struct ocfs2_xattr_search *xbs,
3277 				    struct ocfs2_xattr_set_ctxt *ctxt)
3278 {
3279 	int ret = 0, credits, old_found;
3280 
3281 	if (!xi->xi_value) {
3282 		/* Remove existing extended attribute */
3283 		if (!xis->not_found)
3284 			ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3285 		else if (!xbs->not_found)
3286 			ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3287 	} else {
3288 		/* We always try to set extended attribute into inode first*/
3289 		ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3290 		if (!ret && !xbs->not_found) {
3291 			/*
3292 			 * If succeed and that extended attribute existing in
3293 			 * external block, then we will remove it.
3294 			 */
3295 			xi->xi_value = NULL;
3296 			xi->xi_value_len = 0;
3297 
3298 			old_found = xis->not_found;
3299 			xis->not_found = -ENODATA;
3300 			ret = ocfs2_calc_xattr_set_need(inode,
3301 							di,
3302 							xi,
3303 							xis,
3304 							xbs,
3305 							NULL,
3306 							NULL,
3307 							&credits);
3308 			xis->not_found = old_found;
3309 			if (ret) {
3310 				mlog_errno(ret);
3311 				goto out;
3312 			}
3313 
3314 			ret = ocfs2_extend_trans(ctxt->handle, credits);
3315 			if (ret) {
3316 				mlog_errno(ret);
3317 				goto out;
3318 			}
3319 			ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3320 		} else if ((ret == -ENOSPC) && !ctxt->set_abort) {
3321 			if (di->i_xattr_loc && !xbs->xattr_bh) {
3322 				ret = ocfs2_xattr_block_find(inode,
3323 							     xi->xi_name_index,
3324 							     xi->xi_name, xbs);
3325 				if (ret)
3326 					goto out;
3327 
3328 				old_found = xis->not_found;
3329 				xis->not_found = -ENODATA;
3330 				ret = ocfs2_calc_xattr_set_need(inode,
3331 								di,
3332 								xi,
3333 								xis,
3334 								xbs,
3335 								NULL,
3336 								NULL,
3337 								&credits);
3338 				xis->not_found = old_found;
3339 				if (ret) {
3340 					mlog_errno(ret);
3341 					goto out;
3342 				}
3343 
3344 				ret = ocfs2_extend_trans(ctxt->handle, credits);
3345 				if (ret) {
3346 					mlog_errno(ret);
3347 					goto out;
3348 				}
3349 			}
3350 			/*
3351 			 * If no space in inode, we will set extended attribute
3352 			 * into external block.
3353 			 */
3354 			ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3355 			if (ret)
3356 				goto out;
3357 			if (!xis->not_found) {
3358 				/*
3359 				 * If succeed and that extended attribute
3360 				 * existing in inode, we will remove it.
3361 				 */
3362 				xi->xi_value = NULL;
3363 				xi->xi_value_len = 0;
3364 				xbs->not_found = -ENODATA;
3365 				ret = ocfs2_calc_xattr_set_need(inode,
3366 								di,
3367 								xi,
3368 								xis,
3369 								xbs,
3370 								NULL,
3371 								NULL,
3372 								&credits);
3373 				if (ret) {
3374 					mlog_errno(ret);
3375 					goto out;
3376 				}
3377 
3378 				ret = ocfs2_extend_trans(ctxt->handle, credits);
3379 				if (ret) {
3380 					mlog_errno(ret);
3381 					goto out;
3382 				}
3383 				ret = ocfs2_xattr_ibody_set(inode, xi,
3384 							    xis, ctxt);
3385 			}
3386 		}
3387 	}
3388 
3389 	if (!ret) {
3390 		/* Update inode ctime. */
3391 		ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3392 					      xis->inode_bh,
3393 					      OCFS2_JOURNAL_ACCESS_WRITE);
3394 		if (ret) {
3395 			mlog_errno(ret);
3396 			goto out;
3397 		}
3398 
3399 		inode->i_ctime = CURRENT_TIME;
3400 		di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3401 		di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3402 		ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3403 	}
3404 out:
3405 	return ret;
3406 }
3407 
3408 /*
3409  * This function only called duing creating inode
3410  * for init security/acl xattrs of the new inode.
3411  * All transanction credits have been reserved in mknod.
3412  */
3413 int ocfs2_xattr_set_handle(handle_t *handle,
3414 			   struct inode *inode,
3415 			   struct buffer_head *di_bh,
3416 			   int name_index,
3417 			   const char *name,
3418 			   const void *value,
3419 			   size_t value_len,
3420 			   int flags,
3421 			   struct ocfs2_alloc_context *meta_ac,
3422 			   struct ocfs2_alloc_context *data_ac)
3423 {
3424 	struct ocfs2_dinode *di;
3425 	int ret;
3426 
3427 	struct ocfs2_xattr_info xi = {
3428 		.xi_name_index = name_index,
3429 		.xi_name = name,
3430 		.xi_name_len = strlen(name),
3431 		.xi_value = value,
3432 		.xi_value_len = value_len,
3433 	};
3434 
3435 	struct ocfs2_xattr_search xis = {
3436 		.not_found = -ENODATA,
3437 	};
3438 
3439 	struct ocfs2_xattr_search xbs = {
3440 		.not_found = -ENODATA,
3441 	};
3442 
3443 	struct ocfs2_xattr_set_ctxt ctxt = {
3444 		.handle = handle,
3445 		.meta_ac = meta_ac,
3446 		.data_ac = data_ac,
3447 	};
3448 
3449 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3450 		return -EOPNOTSUPP;
3451 
3452 	/*
3453 	 * In extreme situation, may need xattr bucket when
3454 	 * block size is too small. And we have already reserved
3455 	 * the credits for bucket in mknod.
3456 	 */
3457 	if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3458 		xbs.bucket = ocfs2_xattr_bucket_new(inode);
3459 		if (!xbs.bucket) {
3460 			mlog_errno(-ENOMEM);
3461 			return -ENOMEM;
3462 		}
3463 	}
3464 
3465 	xis.inode_bh = xbs.inode_bh = di_bh;
3466 	di = (struct ocfs2_dinode *)di_bh->b_data;
3467 
3468 	down_write(&OCFS2_I(inode)->ip_xattr_sem);
3469 
3470 	ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3471 	if (ret)
3472 		goto cleanup;
3473 	if (xis.not_found) {
3474 		ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3475 		if (ret)
3476 			goto cleanup;
3477 	}
3478 
3479 	ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3480 
3481 cleanup:
3482 	up_write(&OCFS2_I(inode)->ip_xattr_sem);
3483 	brelse(xbs.xattr_bh);
3484 	ocfs2_xattr_bucket_free(xbs.bucket);
3485 
3486 	return ret;
3487 }
3488 
3489 /*
3490  * ocfs2_xattr_set()
3491  *
3492  * Set, replace or remove an extended attribute for this inode.
3493  * value is NULL to remove an existing extended attribute, else either
3494  * create or replace an extended attribute.
3495  */
3496 int ocfs2_xattr_set(struct inode *inode,
3497 		    int name_index,
3498 		    const char *name,
3499 		    const void *value,
3500 		    size_t value_len,
3501 		    int flags)
3502 {
3503 	struct buffer_head *di_bh = NULL;
3504 	struct ocfs2_dinode *di;
3505 	int ret, credits, ref_meta = 0, ref_credits = 0;
3506 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3507 	struct inode *tl_inode = osb->osb_tl_inode;
3508 	struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, NULL, };
3509 	struct ocfs2_refcount_tree *ref_tree = NULL;
3510 
3511 	struct ocfs2_xattr_info xi = {
3512 		.xi_name_index = name_index,
3513 		.xi_name = name,
3514 		.xi_name_len = strlen(name),
3515 		.xi_value = value,
3516 		.xi_value_len = value_len,
3517 	};
3518 
3519 	struct ocfs2_xattr_search xis = {
3520 		.not_found = -ENODATA,
3521 	};
3522 
3523 	struct ocfs2_xattr_search xbs = {
3524 		.not_found = -ENODATA,
3525 	};
3526 
3527 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3528 		return -EOPNOTSUPP;
3529 
3530 	/*
3531 	 * Only xbs will be used on indexed trees.  xis doesn't need a
3532 	 * bucket.
3533 	 */
3534 	xbs.bucket = ocfs2_xattr_bucket_new(inode);
3535 	if (!xbs.bucket) {
3536 		mlog_errno(-ENOMEM);
3537 		return -ENOMEM;
3538 	}
3539 
3540 	ret = ocfs2_inode_lock(inode, &di_bh, 1);
3541 	if (ret < 0) {
3542 		mlog_errno(ret);
3543 		goto cleanup_nolock;
3544 	}
3545 	xis.inode_bh = xbs.inode_bh = di_bh;
3546 	di = (struct ocfs2_dinode *)di_bh->b_data;
3547 
3548 	down_write(&OCFS2_I(inode)->ip_xattr_sem);
3549 	/*
3550 	 * Scan inode and external block to find the same name
3551 	 * extended attribute and collect search information.
3552 	 */
3553 	ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3554 	if (ret)
3555 		goto cleanup;
3556 	if (xis.not_found) {
3557 		ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3558 		if (ret)
3559 			goto cleanup;
3560 	}
3561 
3562 	if (xis.not_found && xbs.not_found) {
3563 		ret = -ENODATA;
3564 		if (flags & XATTR_REPLACE)
3565 			goto cleanup;
3566 		ret = 0;
3567 		if (!value)
3568 			goto cleanup;
3569 	} else {
3570 		ret = -EEXIST;
3571 		if (flags & XATTR_CREATE)
3572 			goto cleanup;
3573 	}
3574 
3575 	/* Check whether the value is refcounted and do some preparation. */
3576 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3577 	    (!xis.not_found || !xbs.not_found)) {
3578 		ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3579 						   &xis, &xbs, &ref_tree,
3580 						   &ref_meta, &ref_credits);
3581 		if (ret) {
3582 			mlog_errno(ret);
3583 			goto cleanup;
3584 		}
3585 	}
3586 
3587 	mutex_lock(&tl_inode->i_mutex);
3588 
3589 	if (ocfs2_truncate_log_needs_flush(osb)) {
3590 		ret = __ocfs2_flush_truncate_log(osb);
3591 		if (ret < 0) {
3592 			mutex_unlock(&tl_inode->i_mutex);
3593 			mlog_errno(ret);
3594 			goto cleanup;
3595 		}
3596 	}
3597 	mutex_unlock(&tl_inode->i_mutex);
3598 
3599 	ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3600 					&xbs, &ctxt, ref_meta, &credits);
3601 	if (ret) {
3602 		mlog_errno(ret);
3603 		goto cleanup;
3604 	}
3605 
3606 	/* we need to update inode's ctime field, so add credit for it. */
3607 	credits += OCFS2_INODE_UPDATE_CREDITS;
3608 	ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3609 	if (IS_ERR(ctxt.handle)) {
3610 		ret = PTR_ERR(ctxt.handle);
3611 		mlog_errno(ret);
3612 		goto out_free_ac;
3613 	}
3614 
3615 	ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3616 
3617 	ocfs2_commit_trans(osb, ctxt.handle);
3618 
3619 out_free_ac:
3620 	if (ctxt.data_ac)
3621 		ocfs2_free_alloc_context(ctxt.data_ac);
3622 	if (ctxt.meta_ac)
3623 		ocfs2_free_alloc_context(ctxt.meta_ac);
3624 	if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3625 		ocfs2_schedule_truncate_log_flush(osb, 1);
3626 	ocfs2_run_deallocs(osb, &ctxt.dealloc);
3627 
3628 cleanup:
3629 	if (ref_tree)
3630 		ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3631 	up_write(&OCFS2_I(inode)->ip_xattr_sem);
3632 	if (!value && !ret) {
3633 		ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3634 		if (ret)
3635 			mlog_errno(ret);
3636 	}
3637 	ocfs2_inode_unlock(inode, 1);
3638 cleanup_nolock:
3639 	brelse(di_bh);
3640 	brelse(xbs.xattr_bh);
3641 	ocfs2_xattr_bucket_free(xbs.bucket);
3642 
3643 	return ret;
3644 }
3645 
3646 /*
3647  * Find the xattr extent rec which may contains name_hash.
3648  * e_cpos will be the first name hash of the xattr rec.
3649  * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3650  */
3651 static int ocfs2_xattr_get_rec(struct inode *inode,
3652 			       u32 name_hash,
3653 			       u64 *p_blkno,
3654 			       u32 *e_cpos,
3655 			       u32 *num_clusters,
3656 			       struct ocfs2_extent_list *el)
3657 {
3658 	int ret = 0, i;
3659 	struct buffer_head *eb_bh = NULL;
3660 	struct ocfs2_extent_block *eb;
3661 	struct ocfs2_extent_rec *rec = NULL;
3662 	u64 e_blkno = 0;
3663 
3664 	if (el->l_tree_depth) {
3665 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3666 				      &eb_bh);
3667 		if (ret) {
3668 			mlog_errno(ret);
3669 			goto out;
3670 		}
3671 
3672 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3673 		el = &eb->h_list;
3674 
3675 		if (el->l_tree_depth) {
3676 			ocfs2_error(inode->i_sb,
3677 				    "Inode %lu has non zero tree depth in "
3678 				    "xattr tree block %llu\n", inode->i_ino,
3679 				    (unsigned long long)eb_bh->b_blocknr);
3680 			ret = -EROFS;
3681 			goto out;
3682 		}
3683 	}
3684 
3685 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3686 		rec = &el->l_recs[i];
3687 
3688 		if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3689 			e_blkno = le64_to_cpu(rec->e_blkno);
3690 			break;
3691 		}
3692 	}
3693 
3694 	if (!e_blkno) {
3695 		ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3696 			    "record (%u, %u, 0) in xattr", inode->i_ino,
3697 			    le32_to_cpu(rec->e_cpos),
3698 			    ocfs2_rec_clusters(el, rec));
3699 		ret = -EROFS;
3700 		goto out;
3701 	}
3702 
3703 	*p_blkno = le64_to_cpu(rec->e_blkno);
3704 	*num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3705 	if (e_cpos)
3706 		*e_cpos = le32_to_cpu(rec->e_cpos);
3707 out:
3708 	brelse(eb_bh);
3709 	return ret;
3710 }
3711 
3712 typedef int (xattr_bucket_func)(struct inode *inode,
3713 				struct ocfs2_xattr_bucket *bucket,
3714 				void *para);
3715 
3716 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3717 				   struct ocfs2_xattr_bucket *bucket,
3718 				   int name_index,
3719 				   const char *name,
3720 				   u32 name_hash,
3721 				   u16 *xe_index,
3722 				   int *found)
3723 {
3724 	int i, ret = 0, cmp = 1, block_off, new_offset;
3725 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3726 	size_t name_len = strlen(name);
3727 	struct ocfs2_xattr_entry *xe = NULL;
3728 	char *xe_name;
3729 
3730 	/*
3731 	 * We don't use binary search in the bucket because there
3732 	 * may be multiple entries with the same name hash.
3733 	 */
3734 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3735 		xe = &xh->xh_entries[i];
3736 
3737 		if (name_hash > le32_to_cpu(xe->xe_name_hash))
3738 			continue;
3739 		else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3740 			break;
3741 
3742 		cmp = name_index - ocfs2_xattr_get_type(xe);
3743 		if (!cmp)
3744 			cmp = name_len - xe->xe_name_len;
3745 		if (cmp)
3746 			continue;
3747 
3748 		ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3749 							xh,
3750 							i,
3751 							&block_off,
3752 							&new_offset);
3753 		if (ret) {
3754 			mlog_errno(ret);
3755 			break;
3756 		}
3757 
3758 
3759 		xe_name = bucket_block(bucket, block_off) + new_offset;
3760 		if (!memcmp(name, xe_name, name_len)) {
3761 			*xe_index = i;
3762 			*found = 1;
3763 			ret = 0;
3764 			break;
3765 		}
3766 	}
3767 
3768 	return ret;
3769 }
3770 
3771 /*
3772  * Find the specified xattr entry in a series of buckets.
3773  * This series start from p_blkno and last for num_clusters.
3774  * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3775  * the num of the valid buckets.
3776  *
3777  * Return the buffer_head this xattr should reside in. And if the xattr's
3778  * hash is in the gap of 2 buckets, return the lower bucket.
3779  */
3780 static int ocfs2_xattr_bucket_find(struct inode *inode,
3781 				   int name_index,
3782 				   const char *name,
3783 				   u32 name_hash,
3784 				   u64 p_blkno,
3785 				   u32 first_hash,
3786 				   u32 num_clusters,
3787 				   struct ocfs2_xattr_search *xs)
3788 {
3789 	int ret, found = 0;
3790 	struct ocfs2_xattr_header *xh = NULL;
3791 	struct ocfs2_xattr_entry *xe = NULL;
3792 	u16 index = 0;
3793 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3794 	int low_bucket = 0, bucket, high_bucket;
3795 	struct ocfs2_xattr_bucket *search;
3796 	u32 last_hash;
3797 	u64 blkno, lower_blkno = 0;
3798 
3799 	search = ocfs2_xattr_bucket_new(inode);
3800 	if (!search) {
3801 		ret = -ENOMEM;
3802 		mlog_errno(ret);
3803 		goto out;
3804 	}
3805 
3806 	ret = ocfs2_read_xattr_bucket(search, p_blkno);
3807 	if (ret) {
3808 		mlog_errno(ret);
3809 		goto out;
3810 	}
3811 
3812 	xh = bucket_xh(search);
3813 	high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3814 	while (low_bucket <= high_bucket) {
3815 		ocfs2_xattr_bucket_relse(search);
3816 
3817 		bucket = (low_bucket + high_bucket) / 2;
3818 		blkno = p_blkno + bucket * blk_per_bucket;
3819 		ret = ocfs2_read_xattr_bucket(search, blkno);
3820 		if (ret) {
3821 			mlog_errno(ret);
3822 			goto out;
3823 		}
3824 
3825 		xh = bucket_xh(search);
3826 		xe = &xh->xh_entries[0];
3827 		if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3828 			high_bucket = bucket - 1;
3829 			continue;
3830 		}
3831 
3832 		/*
3833 		 * Check whether the hash of the last entry in our
3834 		 * bucket is larger than the search one. for an empty
3835 		 * bucket, the last one is also the first one.
3836 		 */
3837 		if (xh->xh_count)
3838 			xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3839 
3840 		last_hash = le32_to_cpu(xe->xe_name_hash);
3841 
3842 		/* record lower_blkno which may be the insert place. */
3843 		lower_blkno = blkno;
3844 
3845 		if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3846 			low_bucket = bucket + 1;
3847 			continue;
3848 		}
3849 
3850 		/* the searched xattr should reside in this bucket if exists. */
3851 		ret = ocfs2_find_xe_in_bucket(inode, search,
3852 					      name_index, name, name_hash,
3853 					      &index, &found);
3854 		if (ret) {
3855 			mlog_errno(ret);
3856 			goto out;
3857 		}
3858 		break;
3859 	}
3860 
3861 	/*
3862 	 * Record the bucket we have found.
3863 	 * When the xattr's hash value is in the gap of 2 buckets, we will
3864 	 * always set it to the previous bucket.
3865 	 */
3866 	if (!lower_blkno)
3867 		lower_blkno = p_blkno;
3868 
3869 	/* This should be in cache - we just read it during the search */
3870 	ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3871 	if (ret) {
3872 		mlog_errno(ret);
3873 		goto out;
3874 	}
3875 
3876 	xs->header = bucket_xh(xs->bucket);
3877 	xs->base = bucket_block(xs->bucket, 0);
3878 	xs->end = xs->base + inode->i_sb->s_blocksize;
3879 
3880 	if (found) {
3881 		xs->here = &xs->header->xh_entries[index];
3882 		trace_ocfs2_xattr_bucket_find(OCFS2_I(inode)->ip_blkno,
3883 			name, name_index, name_hash,
3884 			(unsigned long long)bucket_blkno(xs->bucket),
3885 			index);
3886 	} else
3887 		ret = -ENODATA;
3888 
3889 out:
3890 	ocfs2_xattr_bucket_free(search);
3891 	return ret;
3892 }
3893 
3894 static int ocfs2_xattr_index_block_find(struct inode *inode,
3895 					struct buffer_head *root_bh,
3896 					int name_index,
3897 					const char *name,
3898 					struct ocfs2_xattr_search *xs)
3899 {
3900 	int ret;
3901 	struct ocfs2_xattr_block *xb =
3902 			(struct ocfs2_xattr_block *)root_bh->b_data;
3903 	struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3904 	struct ocfs2_extent_list *el = &xb_root->xt_list;
3905 	u64 p_blkno = 0;
3906 	u32 first_hash, num_clusters = 0;
3907 	u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3908 
3909 	if (le16_to_cpu(el->l_next_free_rec) == 0)
3910 		return -ENODATA;
3911 
3912 	trace_ocfs2_xattr_index_block_find(OCFS2_I(inode)->ip_blkno,
3913 					name, name_index, name_hash,
3914 					(unsigned long long)root_bh->b_blocknr,
3915 					-1);
3916 
3917 	ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3918 				  &num_clusters, el);
3919 	if (ret) {
3920 		mlog_errno(ret);
3921 		goto out;
3922 	}
3923 
3924 	BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3925 
3926 	trace_ocfs2_xattr_index_block_find_rec(OCFS2_I(inode)->ip_blkno,
3927 					name, name_index, first_hash,
3928 					(unsigned long long)p_blkno,
3929 					num_clusters);
3930 
3931 	ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3932 				      p_blkno, first_hash, num_clusters, xs);
3933 
3934 out:
3935 	return ret;
3936 }
3937 
3938 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3939 				       u64 blkno,
3940 				       u32 clusters,
3941 				       xattr_bucket_func *func,
3942 				       void *para)
3943 {
3944 	int i, ret = 0;
3945 	u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3946 	u32 num_buckets = clusters * bpc;
3947 	struct ocfs2_xattr_bucket *bucket;
3948 
3949 	bucket = ocfs2_xattr_bucket_new(inode);
3950 	if (!bucket) {
3951 		mlog_errno(-ENOMEM);
3952 		return -ENOMEM;
3953 	}
3954 
3955 	trace_ocfs2_iterate_xattr_buckets(
3956 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
3957 		(unsigned long long)blkno, clusters);
3958 
3959 	for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3960 		ret = ocfs2_read_xattr_bucket(bucket, blkno);
3961 		if (ret) {
3962 			mlog_errno(ret);
3963 			break;
3964 		}
3965 
3966 		/*
3967 		 * The real bucket num in this series of blocks is stored
3968 		 * in the 1st bucket.
3969 		 */
3970 		if (i == 0)
3971 			num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3972 
3973 		trace_ocfs2_iterate_xattr_bucket((unsigned long long)blkno,
3974 		     le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3975 		if (func) {
3976 			ret = func(inode, bucket, para);
3977 			if (ret && ret != -ERANGE)
3978 				mlog_errno(ret);
3979 			/* Fall through to bucket_relse() */
3980 		}
3981 
3982 		ocfs2_xattr_bucket_relse(bucket);
3983 		if (ret)
3984 			break;
3985 	}
3986 
3987 	ocfs2_xattr_bucket_free(bucket);
3988 	return ret;
3989 }
3990 
3991 struct ocfs2_xattr_tree_list {
3992 	char *buffer;
3993 	size_t buffer_size;
3994 	size_t result;
3995 };
3996 
3997 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3998 					     struct ocfs2_xattr_header *xh,
3999 					     int index,
4000 					     int *block_off,
4001 					     int *new_offset)
4002 {
4003 	u16 name_offset;
4004 
4005 	if (index < 0 || index >= le16_to_cpu(xh->xh_count))
4006 		return -EINVAL;
4007 
4008 	name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
4009 
4010 	*block_off = name_offset >> sb->s_blocksize_bits;
4011 	*new_offset = name_offset % sb->s_blocksize;
4012 
4013 	return 0;
4014 }
4015 
4016 static int ocfs2_list_xattr_bucket(struct inode *inode,
4017 				   struct ocfs2_xattr_bucket *bucket,
4018 				   void *para)
4019 {
4020 	int ret = 0, type;
4021 	struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
4022 	int i, block_off, new_offset;
4023 	const char *prefix, *name;
4024 
4025 	for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4026 		struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4027 		type = ocfs2_xattr_get_type(entry);
4028 		prefix = ocfs2_xattr_prefix(type);
4029 
4030 		if (prefix) {
4031 			ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
4032 								bucket_xh(bucket),
4033 								i,
4034 								&block_off,
4035 								&new_offset);
4036 			if (ret)
4037 				break;
4038 
4039 			name = (const char *)bucket_block(bucket, block_off) +
4040 				new_offset;
4041 			ret = ocfs2_xattr_list_entry(xl->buffer,
4042 						     xl->buffer_size,
4043 						     &xl->result,
4044 						     prefix, name,
4045 						     entry->xe_name_len);
4046 			if (ret)
4047 				break;
4048 		}
4049 	}
4050 
4051 	return ret;
4052 }
4053 
4054 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4055 					   struct buffer_head *blk_bh,
4056 					   xattr_tree_rec_func *rec_func,
4057 					   void *para)
4058 {
4059 	struct ocfs2_xattr_block *xb =
4060 			(struct ocfs2_xattr_block *)blk_bh->b_data;
4061 	struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4062 	int ret = 0;
4063 	u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4064 	u64 p_blkno = 0;
4065 
4066 	if (!el->l_next_free_rec || !rec_func)
4067 		return 0;
4068 
4069 	while (name_hash > 0) {
4070 		ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4071 					  &e_cpos, &num_clusters, el);
4072 		if (ret) {
4073 			mlog_errno(ret);
4074 			break;
4075 		}
4076 
4077 		ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4078 			       num_clusters, para);
4079 		if (ret) {
4080 			if (ret != -ERANGE)
4081 				mlog_errno(ret);
4082 			break;
4083 		}
4084 
4085 		if (e_cpos == 0)
4086 			break;
4087 
4088 		name_hash = e_cpos - 1;
4089 	}
4090 
4091 	return ret;
4092 
4093 }
4094 
4095 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4096 				     struct buffer_head *root_bh,
4097 				     u64 blkno, u32 cpos, u32 len, void *para)
4098 {
4099 	return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4100 					   ocfs2_list_xattr_bucket, para);
4101 }
4102 
4103 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4104 					     struct buffer_head *blk_bh,
4105 					     char *buffer,
4106 					     size_t buffer_size)
4107 {
4108 	int ret;
4109 	struct ocfs2_xattr_tree_list xl = {
4110 		.buffer = buffer,
4111 		.buffer_size = buffer_size,
4112 		.result = 0,
4113 	};
4114 
4115 	ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4116 					      ocfs2_list_xattr_tree_rec, &xl);
4117 	if (ret) {
4118 		mlog_errno(ret);
4119 		goto out;
4120 	}
4121 
4122 	ret = xl.result;
4123 out:
4124 	return ret;
4125 }
4126 
4127 static int cmp_xe(const void *a, const void *b)
4128 {
4129 	const struct ocfs2_xattr_entry *l = a, *r = b;
4130 	u32 l_hash = le32_to_cpu(l->xe_name_hash);
4131 	u32 r_hash = le32_to_cpu(r->xe_name_hash);
4132 
4133 	if (l_hash > r_hash)
4134 		return 1;
4135 	if (l_hash < r_hash)
4136 		return -1;
4137 	return 0;
4138 }
4139 
4140 static void swap_xe(void *a, void *b, int size)
4141 {
4142 	struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4143 
4144 	tmp = *l;
4145 	memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4146 	memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4147 }
4148 
4149 /*
4150  * When the ocfs2_xattr_block is filled up, new bucket will be created
4151  * and all the xattr entries will be moved to the new bucket.
4152  * The header goes at the start of the bucket, and the names+values are
4153  * filled from the end.  This is why *target starts as the last buffer.
4154  * Note: we need to sort the entries since they are not saved in order
4155  * in the ocfs2_xattr_block.
4156  */
4157 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4158 					   struct buffer_head *xb_bh,
4159 					   struct ocfs2_xattr_bucket *bucket)
4160 {
4161 	int i, blocksize = inode->i_sb->s_blocksize;
4162 	int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4163 	u16 offset, size, off_change;
4164 	struct ocfs2_xattr_entry *xe;
4165 	struct ocfs2_xattr_block *xb =
4166 				(struct ocfs2_xattr_block *)xb_bh->b_data;
4167 	struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4168 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4169 	u16 count = le16_to_cpu(xb_xh->xh_count);
4170 	char *src = xb_bh->b_data;
4171 	char *target = bucket_block(bucket, blks - 1);
4172 
4173 	trace_ocfs2_cp_xattr_block_to_bucket_begin(
4174 				(unsigned long long)xb_bh->b_blocknr,
4175 				(unsigned long long)bucket_blkno(bucket));
4176 
4177 	for (i = 0; i < blks; i++)
4178 		memset(bucket_block(bucket, i), 0, blocksize);
4179 
4180 	/*
4181 	 * Since the xe_name_offset is based on ocfs2_xattr_header,
4182 	 * there is a offset change corresponding to the change of
4183 	 * ocfs2_xattr_header's position.
4184 	 */
4185 	off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4186 	xe = &xb_xh->xh_entries[count - 1];
4187 	offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4188 	size = blocksize - offset;
4189 
4190 	/* copy all the names and values. */
4191 	memcpy(target + offset, src + offset, size);
4192 
4193 	/* Init new header now. */
4194 	xh->xh_count = xb_xh->xh_count;
4195 	xh->xh_num_buckets = cpu_to_le16(1);
4196 	xh->xh_name_value_len = cpu_to_le16(size);
4197 	xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4198 
4199 	/* copy all the entries. */
4200 	target = bucket_block(bucket, 0);
4201 	offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4202 	size = count * sizeof(struct ocfs2_xattr_entry);
4203 	memcpy(target + offset, (char *)xb_xh + offset, size);
4204 
4205 	/* Change the xe offset for all the xe because of the move. */
4206 	off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4207 		 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4208 	for (i = 0; i < count; i++)
4209 		le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4210 
4211 	trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
4212 
4213 	sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4214 	     cmp_xe, swap_xe);
4215 }
4216 
4217 /*
4218  * After we move xattr from block to index btree, we have to
4219  * update ocfs2_xattr_search to the new xe and base.
4220  *
4221  * When the entry is in xattr block, xattr_bh indicates the storage place.
4222  * While if the entry is in index b-tree, "bucket" indicates the
4223  * real place of the xattr.
4224  */
4225 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4226 					    struct ocfs2_xattr_search *xs,
4227 					    struct buffer_head *old_bh)
4228 {
4229 	char *buf = old_bh->b_data;
4230 	struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4231 	struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4232 	int i;
4233 
4234 	xs->header = bucket_xh(xs->bucket);
4235 	xs->base = bucket_block(xs->bucket, 0);
4236 	xs->end = xs->base + inode->i_sb->s_blocksize;
4237 
4238 	if (xs->not_found)
4239 		return;
4240 
4241 	i = xs->here - old_xh->xh_entries;
4242 	xs->here = &xs->header->xh_entries[i];
4243 }
4244 
4245 static int ocfs2_xattr_create_index_block(struct inode *inode,
4246 					  struct ocfs2_xattr_search *xs,
4247 					  struct ocfs2_xattr_set_ctxt *ctxt)
4248 {
4249 	int ret;
4250 	u32 bit_off, len;
4251 	u64 blkno;
4252 	handle_t *handle = ctxt->handle;
4253 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
4254 	struct buffer_head *xb_bh = xs->xattr_bh;
4255 	struct ocfs2_xattr_block *xb =
4256 			(struct ocfs2_xattr_block *)xb_bh->b_data;
4257 	struct ocfs2_xattr_tree_root *xr;
4258 	u16 xb_flags = le16_to_cpu(xb->xb_flags);
4259 
4260 	trace_ocfs2_xattr_create_index_block_begin(
4261 				(unsigned long long)xb_bh->b_blocknr);
4262 
4263 	BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4264 	BUG_ON(!xs->bucket);
4265 
4266 	/*
4267 	 * XXX:
4268 	 * We can use this lock for now, and maybe move to a dedicated mutex
4269 	 * if performance becomes a problem later.
4270 	 */
4271 	down_write(&oi->ip_alloc_sem);
4272 
4273 	ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4274 				      OCFS2_JOURNAL_ACCESS_WRITE);
4275 	if (ret) {
4276 		mlog_errno(ret);
4277 		goto out;
4278 	}
4279 
4280 	ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
4281 				     1, 1, &bit_off, &len);
4282 	if (ret) {
4283 		mlog_errno(ret);
4284 		goto out;
4285 	}
4286 
4287 	/*
4288 	 * The bucket may spread in many blocks, and
4289 	 * we will only touch the 1st block and the last block
4290 	 * in the whole bucket(one for entry and one for data).
4291 	 */
4292 	blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4293 
4294 	trace_ocfs2_xattr_create_index_block((unsigned long long)blkno);
4295 
4296 	ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4297 	if (ret) {
4298 		mlog_errno(ret);
4299 		goto out;
4300 	}
4301 
4302 	ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4303 						OCFS2_JOURNAL_ACCESS_CREATE);
4304 	if (ret) {
4305 		mlog_errno(ret);
4306 		goto out;
4307 	}
4308 
4309 	ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4310 	ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4311 
4312 	ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4313 
4314 	/* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4315 	memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4316 	       offsetof(struct ocfs2_xattr_block, xb_attrs));
4317 
4318 	xr = &xb->xb_attrs.xb_root;
4319 	xr->xt_clusters = cpu_to_le32(1);
4320 	xr->xt_last_eb_blk = 0;
4321 	xr->xt_list.l_tree_depth = 0;
4322 	xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4323 	xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4324 
4325 	xr->xt_list.l_recs[0].e_cpos = 0;
4326 	xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4327 	xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4328 
4329 	xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4330 
4331 	ocfs2_journal_dirty(handle, xb_bh);
4332 
4333 out:
4334 	up_write(&oi->ip_alloc_sem);
4335 
4336 	return ret;
4337 }
4338 
4339 static int cmp_xe_offset(const void *a, const void *b)
4340 {
4341 	const struct ocfs2_xattr_entry *l = a, *r = b;
4342 	u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4343 	u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4344 
4345 	if (l_name_offset < r_name_offset)
4346 		return 1;
4347 	if (l_name_offset > r_name_offset)
4348 		return -1;
4349 	return 0;
4350 }
4351 
4352 /*
4353  * defrag a xattr bucket if we find that the bucket has some
4354  * holes beteen name/value pairs.
4355  * We will move all the name/value pairs to the end of the bucket
4356  * so that we can spare some space for insertion.
4357  */
4358 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4359 				     handle_t *handle,
4360 				     struct ocfs2_xattr_bucket *bucket)
4361 {
4362 	int ret, i;
4363 	size_t end, offset, len;
4364 	struct ocfs2_xattr_header *xh;
4365 	char *entries, *buf, *bucket_buf = NULL;
4366 	u64 blkno = bucket_blkno(bucket);
4367 	u16 xh_free_start;
4368 	size_t blocksize = inode->i_sb->s_blocksize;
4369 	struct ocfs2_xattr_entry *xe;
4370 
4371 	/*
4372 	 * In order to make the operation more efficient and generic,
4373 	 * we copy all the blocks into a contiguous memory and do the
4374 	 * defragment there, so if anything is error, we will not touch
4375 	 * the real block.
4376 	 */
4377 	bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4378 	if (!bucket_buf) {
4379 		ret = -EIO;
4380 		goto out;
4381 	}
4382 
4383 	buf = bucket_buf;
4384 	for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4385 		memcpy(buf, bucket_block(bucket, i), blocksize);
4386 
4387 	ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4388 						OCFS2_JOURNAL_ACCESS_WRITE);
4389 	if (ret < 0) {
4390 		mlog_errno(ret);
4391 		goto out;
4392 	}
4393 
4394 	xh = (struct ocfs2_xattr_header *)bucket_buf;
4395 	entries = (char *)xh->xh_entries;
4396 	xh_free_start = le16_to_cpu(xh->xh_free_start);
4397 
4398 	trace_ocfs2_defrag_xattr_bucket(
4399 	     (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4400 	     xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4401 
4402 	/*
4403 	 * sort all the entries by their offset.
4404 	 * the largest will be the first, so that we can
4405 	 * move them to the end one by one.
4406 	 */
4407 	sort(entries, le16_to_cpu(xh->xh_count),
4408 	     sizeof(struct ocfs2_xattr_entry),
4409 	     cmp_xe_offset, swap_xe);
4410 
4411 	/* Move all name/values to the end of the bucket. */
4412 	xe = xh->xh_entries;
4413 	end = OCFS2_XATTR_BUCKET_SIZE;
4414 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4415 		offset = le16_to_cpu(xe->xe_name_offset);
4416 		len = namevalue_size_xe(xe);
4417 
4418 		/*
4419 		 * We must make sure that the name/value pair
4420 		 * exist in the same block. So adjust end to
4421 		 * the previous block end if needed.
4422 		 */
4423 		if (((end - len) / blocksize !=
4424 			(end - 1) / blocksize))
4425 			end = end - end % blocksize;
4426 
4427 		if (end > offset + len) {
4428 			memmove(bucket_buf + end - len,
4429 				bucket_buf + offset, len);
4430 			xe->xe_name_offset = cpu_to_le16(end - len);
4431 		}
4432 
4433 		mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4434 				"bucket %llu\n", (unsigned long long)blkno);
4435 
4436 		end -= len;
4437 	}
4438 
4439 	mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4440 			"bucket %llu\n", (unsigned long long)blkno);
4441 
4442 	if (xh_free_start == end)
4443 		goto out;
4444 
4445 	memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4446 	xh->xh_free_start = cpu_to_le16(end);
4447 
4448 	/* sort the entries by their name_hash. */
4449 	sort(entries, le16_to_cpu(xh->xh_count),
4450 	     sizeof(struct ocfs2_xattr_entry),
4451 	     cmp_xe, swap_xe);
4452 
4453 	buf = bucket_buf;
4454 	for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4455 		memcpy(bucket_block(bucket, i), buf, blocksize);
4456 	ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4457 
4458 out:
4459 	kfree(bucket_buf);
4460 	return ret;
4461 }
4462 
4463 /*
4464  * prev_blkno points to the start of an existing extent.  new_blkno
4465  * points to a newly allocated extent.  Because we know each of our
4466  * clusters contains more than bucket, we can easily split one cluster
4467  * at a bucket boundary.  So we take the last cluster of the existing
4468  * extent and split it down the middle.  We move the last half of the
4469  * buckets in the last cluster of the existing extent over to the new
4470  * extent.
4471  *
4472  * first_bh is the buffer at prev_blkno so we can update the existing
4473  * extent's bucket count.  header_bh is the bucket were we were hoping
4474  * to insert our xattr.  If the bucket move places the target in the new
4475  * extent, we'll update first_bh and header_bh after modifying the old
4476  * extent.
4477  *
4478  * first_hash will be set as the 1st xe's name_hash in the new extent.
4479  */
4480 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4481 					       handle_t *handle,
4482 					       struct ocfs2_xattr_bucket *first,
4483 					       struct ocfs2_xattr_bucket *target,
4484 					       u64 new_blkno,
4485 					       u32 num_clusters,
4486 					       u32 *first_hash)
4487 {
4488 	int ret;
4489 	struct super_block *sb = inode->i_sb;
4490 	int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4491 	int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4492 	int to_move = num_buckets / 2;
4493 	u64 src_blkno;
4494 	u64 last_cluster_blkno = bucket_blkno(first) +
4495 		((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4496 
4497 	BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4498 	BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4499 
4500 	trace_ocfs2_mv_xattr_bucket_cross_cluster(
4501 				(unsigned long long)last_cluster_blkno,
4502 				(unsigned long long)new_blkno);
4503 
4504 	ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4505 				     last_cluster_blkno, new_blkno,
4506 				     to_move, first_hash);
4507 	if (ret) {
4508 		mlog_errno(ret);
4509 		goto out;
4510 	}
4511 
4512 	/* This is the first bucket that got moved */
4513 	src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4514 
4515 	/*
4516 	 * If the target bucket was part of the moved buckets, we need to
4517 	 * update first and target.
4518 	 */
4519 	if (bucket_blkno(target) >= src_blkno) {
4520 		/* Find the block for the new target bucket */
4521 		src_blkno = new_blkno +
4522 			(bucket_blkno(target) - src_blkno);
4523 
4524 		ocfs2_xattr_bucket_relse(first);
4525 		ocfs2_xattr_bucket_relse(target);
4526 
4527 		/*
4528 		 * These shouldn't fail - the buffers are in the
4529 		 * journal from ocfs2_cp_xattr_bucket().
4530 		 */
4531 		ret = ocfs2_read_xattr_bucket(first, new_blkno);
4532 		if (ret) {
4533 			mlog_errno(ret);
4534 			goto out;
4535 		}
4536 		ret = ocfs2_read_xattr_bucket(target, src_blkno);
4537 		if (ret)
4538 			mlog_errno(ret);
4539 
4540 	}
4541 
4542 out:
4543 	return ret;
4544 }
4545 
4546 /*
4547  * Find the suitable pos when we divide a bucket into 2.
4548  * We have to make sure the xattrs with the same hash value exist
4549  * in the same bucket.
4550  *
4551  * If this ocfs2_xattr_header covers more than one hash value, find a
4552  * place where the hash value changes.  Try to find the most even split.
4553  * The most common case is that all entries have different hash values,
4554  * and the first check we make will find a place to split.
4555  */
4556 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4557 {
4558 	struct ocfs2_xattr_entry *entries = xh->xh_entries;
4559 	int count = le16_to_cpu(xh->xh_count);
4560 	int delta, middle = count / 2;
4561 
4562 	/*
4563 	 * We start at the middle.  Each step gets farther away in both
4564 	 * directions.  We therefore hit the change in hash value
4565 	 * nearest to the middle.  Note that this loop does not execute for
4566 	 * count < 2.
4567 	 */
4568 	for (delta = 0; delta < middle; delta++) {
4569 		/* Let's check delta earlier than middle */
4570 		if (cmp_xe(&entries[middle - delta - 1],
4571 			   &entries[middle - delta]))
4572 			return middle - delta;
4573 
4574 		/* For even counts, don't walk off the end */
4575 		if ((middle + delta + 1) == count)
4576 			continue;
4577 
4578 		/* Now try delta past middle */
4579 		if (cmp_xe(&entries[middle + delta],
4580 			   &entries[middle + delta + 1]))
4581 			return middle + delta + 1;
4582 	}
4583 
4584 	/* Every entry had the same hash */
4585 	return count;
4586 }
4587 
4588 /*
4589  * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4590  * first_hash will record the 1st hash of the new bucket.
4591  *
4592  * Normally half of the xattrs will be moved.  But we have to make
4593  * sure that the xattrs with the same hash value are stored in the
4594  * same bucket. If all the xattrs in this bucket have the same hash
4595  * value, the new bucket will be initialized as an empty one and the
4596  * first_hash will be initialized as (hash_value+1).
4597  */
4598 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4599 				    handle_t *handle,
4600 				    u64 blk,
4601 				    u64 new_blk,
4602 				    u32 *first_hash,
4603 				    int new_bucket_head)
4604 {
4605 	int ret, i;
4606 	int count, start, len, name_value_len = 0, name_offset = 0;
4607 	struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4608 	struct ocfs2_xattr_header *xh;
4609 	struct ocfs2_xattr_entry *xe;
4610 	int blocksize = inode->i_sb->s_blocksize;
4611 
4612 	trace_ocfs2_divide_xattr_bucket_begin((unsigned long long)blk,
4613 					      (unsigned long long)new_blk);
4614 
4615 	s_bucket = ocfs2_xattr_bucket_new(inode);
4616 	t_bucket = ocfs2_xattr_bucket_new(inode);
4617 	if (!s_bucket || !t_bucket) {
4618 		ret = -ENOMEM;
4619 		mlog_errno(ret);
4620 		goto out;
4621 	}
4622 
4623 	ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4624 	if (ret) {
4625 		mlog_errno(ret);
4626 		goto out;
4627 	}
4628 
4629 	ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4630 						OCFS2_JOURNAL_ACCESS_WRITE);
4631 	if (ret) {
4632 		mlog_errno(ret);
4633 		goto out;
4634 	}
4635 
4636 	/*
4637 	 * Even if !new_bucket_head, we're overwriting t_bucket.  Thus,
4638 	 * there's no need to read it.
4639 	 */
4640 	ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4641 	if (ret) {
4642 		mlog_errno(ret);
4643 		goto out;
4644 	}
4645 
4646 	/*
4647 	 * Hey, if we're overwriting t_bucket, what difference does
4648 	 * ACCESS_CREATE vs ACCESS_WRITE make?  See the comment in the
4649 	 * same part of ocfs2_cp_xattr_bucket().
4650 	 */
4651 	ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4652 						new_bucket_head ?
4653 						OCFS2_JOURNAL_ACCESS_CREATE :
4654 						OCFS2_JOURNAL_ACCESS_WRITE);
4655 	if (ret) {
4656 		mlog_errno(ret);
4657 		goto out;
4658 	}
4659 
4660 	xh = bucket_xh(s_bucket);
4661 	count = le16_to_cpu(xh->xh_count);
4662 	start = ocfs2_xattr_find_divide_pos(xh);
4663 
4664 	if (start == count) {
4665 		xe = &xh->xh_entries[start-1];
4666 
4667 		/*
4668 		 * initialized a new empty bucket here.
4669 		 * The hash value is set as one larger than
4670 		 * that of the last entry in the previous bucket.
4671 		 */
4672 		for (i = 0; i < t_bucket->bu_blocks; i++)
4673 			memset(bucket_block(t_bucket, i), 0, blocksize);
4674 
4675 		xh = bucket_xh(t_bucket);
4676 		xh->xh_free_start = cpu_to_le16(blocksize);
4677 		xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4678 		le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4679 
4680 		goto set_num_buckets;
4681 	}
4682 
4683 	/* copy the whole bucket to the new first. */
4684 	ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4685 
4686 	/* update the new bucket. */
4687 	xh = bucket_xh(t_bucket);
4688 
4689 	/*
4690 	 * Calculate the total name/value len and xh_free_start for
4691 	 * the old bucket first.
4692 	 */
4693 	name_offset = OCFS2_XATTR_BUCKET_SIZE;
4694 	name_value_len = 0;
4695 	for (i = 0; i < start; i++) {
4696 		xe = &xh->xh_entries[i];
4697 		name_value_len += namevalue_size_xe(xe);
4698 		if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4699 			name_offset = le16_to_cpu(xe->xe_name_offset);
4700 	}
4701 
4702 	/*
4703 	 * Now begin the modification to the new bucket.
4704 	 *
4705 	 * In the new bucket, We just move the xattr entry to the beginning
4706 	 * and don't touch the name/value. So there will be some holes in the
4707 	 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4708 	 * called.
4709 	 */
4710 	xe = &xh->xh_entries[start];
4711 	len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4712 	trace_ocfs2_divide_xattr_bucket_move(len,
4713 			(int)((char *)xe - (char *)xh),
4714 			(int)((char *)xh->xh_entries - (char *)xh));
4715 	memmove((char *)xh->xh_entries, (char *)xe, len);
4716 	xe = &xh->xh_entries[count - start];
4717 	len = sizeof(struct ocfs2_xattr_entry) * start;
4718 	memset((char *)xe, 0, len);
4719 
4720 	le16_add_cpu(&xh->xh_count, -start);
4721 	le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4722 
4723 	/* Calculate xh_free_start for the new bucket. */
4724 	xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4725 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4726 		xe = &xh->xh_entries[i];
4727 		if (le16_to_cpu(xe->xe_name_offset) <
4728 		    le16_to_cpu(xh->xh_free_start))
4729 			xh->xh_free_start = xe->xe_name_offset;
4730 	}
4731 
4732 set_num_buckets:
4733 	/* set xh->xh_num_buckets for the new xh. */
4734 	if (new_bucket_head)
4735 		xh->xh_num_buckets = cpu_to_le16(1);
4736 	else
4737 		xh->xh_num_buckets = 0;
4738 
4739 	ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4740 
4741 	/* store the first_hash of the new bucket. */
4742 	if (first_hash)
4743 		*first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4744 
4745 	/*
4746 	 * Now only update the 1st block of the old bucket.  If we
4747 	 * just added a new empty bucket, there is no need to modify
4748 	 * it.
4749 	 */
4750 	if (start == count)
4751 		goto out;
4752 
4753 	xh = bucket_xh(s_bucket);
4754 	memset(&xh->xh_entries[start], 0,
4755 	       sizeof(struct ocfs2_xattr_entry) * (count - start));
4756 	xh->xh_count = cpu_to_le16(start);
4757 	xh->xh_free_start = cpu_to_le16(name_offset);
4758 	xh->xh_name_value_len = cpu_to_le16(name_value_len);
4759 
4760 	ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4761 
4762 out:
4763 	ocfs2_xattr_bucket_free(s_bucket);
4764 	ocfs2_xattr_bucket_free(t_bucket);
4765 
4766 	return ret;
4767 }
4768 
4769 /*
4770  * Copy xattr from one bucket to another bucket.
4771  *
4772  * The caller must make sure that the journal transaction
4773  * has enough space for journaling.
4774  */
4775 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4776 				 handle_t *handle,
4777 				 u64 s_blkno,
4778 				 u64 t_blkno,
4779 				 int t_is_new)
4780 {
4781 	int ret;
4782 	struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4783 
4784 	BUG_ON(s_blkno == t_blkno);
4785 
4786 	trace_ocfs2_cp_xattr_bucket((unsigned long long)s_blkno,
4787 				    (unsigned long long)t_blkno,
4788 				    t_is_new);
4789 
4790 	s_bucket = ocfs2_xattr_bucket_new(inode);
4791 	t_bucket = ocfs2_xattr_bucket_new(inode);
4792 	if (!s_bucket || !t_bucket) {
4793 		ret = -ENOMEM;
4794 		mlog_errno(ret);
4795 		goto out;
4796 	}
4797 
4798 	ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4799 	if (ret)
4800 		goto out;
4801 
4802 	/*
4803 	 * Even if !t_is_new, we're overwriting t_bucket.  Thus,
4804 	 * there's no need to read it.
4805 	 */
4806 	ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4807 	if (ret)
4808 		goto out;
4809 
4810 	/*
4811 	 * Hey, if we're overwriting t_bucket, what difference does
4812 	 * ACCESS_CREATE vs ACCESS_WRITE make?  Well, if we allocated a new
4813 	 * cluster to fill, we came here from
4814 	 * ocfs2_mv_xattr_buckets(), and it is really new -
4815 	 * ACCESS_CREATE is required.  But we also might have moved data
4816 	 * out of t_bucket before extending back into it.
4817 	 * ocfs2_add_new_xattr_bucket() can do this - its call to
4818 	 * ocfs2_add_new_xattr_cluster() may have created a new extent
4819 	 * and copied out the end of the old extent.  Then it re-extends
4820 	 * the old extent back to create space for new xattrs.  That's
4821 	 * how we get here, and the bucket isn't really new.
4822 	 */
4823 	ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4824 						t_is_new ?
4825 						OCFS2_JOURNAL_ACCESS_CREATE :
4826 						OCFS2_JOURNAL_ACCESS_WRITE);
4827 	if (ret)
4828 		goto out;
4829 
4830 	ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4831 	ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4832 
4833 out:
4834 	ocfs2_xattr_bucket_free(t_bucket);
4835 	ocfs2_xattr_bucket_free(s_bucket);
4836 
4837 	return ret;
4838 }
4839 
4840 /*
4841  * src_blk points to the start of an existing extent.  last_blk points to
4842  * last cluster in that extent.  to_blk points to a newly allocated
4843  * extent.  We copy the buckets from the cluster at last_blk to the new
4844  * extent.  If start_bucket is non-zero, we skip that many buckets before
4845  * we start copying.  The new extent's xh_num_buckets gets set to the
4846  * number of buckets we copied.  The old extent's xh_num_buckets shrinks
4847  * by the same amount.
4848  */
4849 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4850 				  u64 src_blk, u64 last_blk, u64 to_blk,
4851 				  unsigned int start_bucket,
4852 				  u32 *first_hash)
4853 {
4854 	int i, ret, credits;
4855 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4856 	int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4857 	int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4858 	struct ocfs2_xattr_bucket *old_first, *new_first;
4859 
4860 	trace_ocfs2_mv_xattr_buckets((unsigned long long)last_blk,
4861 				     (unsigned long long)to_blk);
4862 
4863 	BUG_ON(start_bucket >= num_buckets);
4864 	if (start_bucket) {
4865 		num_buckets -= start_bucket;
4866 		last_blk += (start_bucket * blks_per_bucket);
4867 	}
4868 
4869 	/* The first bucket of the original extent */
4870 	old_first = ocfs2_xattr_bucket_new(inode);
4871 	/* The first bucket of the new extent */
4872 	new_first = ocfs2_xattr_bucket_new(inode);
4873 	if (!old_first || !new_first) {
4874 		ret = -ENOMEM;
4875 		mlog_errno(ret);
4876 		goto out;
4877 	}
4878 
4879 	ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4880 	if (ret) {
4881 		mlog_errno(ret);
4882 		goto out;
4883 	}
4884 
4885 	/*
4886 	 * We need to update the first bucket of the old extent and all
4887 	 * the buckets going to the new extent.
4888 	 */
4889 	credits = ((num_buckets + 1) * blks_per_bucket);
4890 	ret = ocfs2_extend_trans(handle, credits);
4891 	if (ret) {
4892 		mlog_errno(ret);
4893 		goto out;
4894 	}
4895 
4896 	ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4897 						OCFS2_JOURNAL_ACCESS_WRITE);
4898 	if (ret) {
4899 		mlog_errno(ret);
4900 		goto out;
4901 	}
4902 
4903 	for (i = 0; i < num_buckets; i++) {
4904 		ret = ocfs2_cp_xattr_bucket(inode, handle,
4905 					    last_blk + (i * blks_per_bucket),
4906 					    to_blk + (i * blks_per_bucket),
4907 					    1);
4908 		if (ret) {
4909 			mlog_errno(ret);
4910 			goto out;
4911 		}
4912 	}
4913 
4914 	/*
4915 	 * Get the new bucket ready before we dirty anything
4916 	 * (This actually shouldn't fail, because we already dirtied
4917 	 * it once in ocfs2_cp_xattr_bucket()).
4918 	 */
4919 	ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4920 	if (ret) {
4921 		mlog_errno(ret);
4922 		goto out;
4923 	}
4924 	ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4925 						OCFS2_JOURNAL_ACCESS_WRITE);
4926 	if (ret) {
4927 		mlog_errno(ret);
4928 		goto out;
4929 	}
4930 
4931 	/* Now update the headers */
4932 	le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4933 	ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4934 
4935 	bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4936 	ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4937 
4938 	if (first_hash)
4939 		*first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4940 
4941 out:
4942 	ocfs2_xattr_bucket_free(new_first);
4943 	ocfs2_xattr_bucket_free(old_first);
4944 	return ret;
4945 }
4946 
4947 /*
4948  * Move some xattrs in this cluster to the new cluster.
4949  * This function should only be called when bucket size == cluster size.
4950  * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4951  */
4952 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4953 				      handle_t *handle,
4954 				      u64 prev_blk,
4955 				      u64 new_blk,
4956 				      u32 *first_hash)
4957 {
4958 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4959 	int ret, credits = 2 * blk_per_bucket;
4960 
4961 	BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4962 
4963 	ret = ocfs2_extend_trans(handle, credits);
4964 	if (ret) {
4965 		mlog_errno(ret);
4966 		return ret;
4967 	}
4968 
4969 	/* Move half of the xattr in start_blk to the next bucket. */
4970 	return  ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4971 					  new_blk, first_hash, 1);
4972 }
4973 
4974 /*
4975  * Move some xattrs from the old cluster to the new one since they are not
4976  * contiguous in ocfs2 xattr tree.
4977  *
4978  * new_blk starts a new separate cluster, and we will move some xattrs from
4979  * prev_blk to it. v_start will be set as the first name hash value in this
4980  * new cluster so that it can be used as e_cpos during tree insertion and
4981  * don't collide with our original b-tree operations. first_bh and header_bh
4982  * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4983  * to extend the insert bucket.
4984  *
4985  * The problem is how much xattr should we move to the new one and when should
4986  * we update first_bh and header_bh?
4987  * 1. If cluster size > bucket size, that means the previous cluster has more
4988  *    than 1 bucket, so just move half nums of bucket into the new cluster and
4989  *    update the first_bh and header_bh if the insert bucket has been moved
4990  *    to the new cluster.
4991  * 2. If cluster_size == bucket_size:
4992  *    a) If the previous extent rec has more than one cluster and the insert
4993  *       place isn't in the last cluster, copy the entire last cluster to the
4994  *       new one. This time, we don't need to upate the first_bh and header_bh
4995  *       since they will not be moved into the new cluster.
4996  *    b) Otherwise, move the bottom half of the xattrs in the last cluster into
4997  *       the new one. And we set the extend flag to zero if the insert place is
4998  *       moved into the new allocated cluster since no extend is needed.
4999  */
5000 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
5001 					    handle_t *handle,
5002 					    struct ocfs2_xattr_bucket *first,
5003 					    struct ocfs2_xattr_bucket *target,
5004 					    u64 new_blk,
5005 					    u32 prev_clusters,
5006 					    u32 *v_start,
5007 					    int *extend)
5008 {
5009 	int ret;
5010 
5011 	trace_ocfs2_adjust_xattr_cross_cluster(
5012 			(unsigned long long)bucket_blkno(first),
5013 			(unsigned long long)new_blk, prev_clusters);
5014 
5015 	if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
5016 		ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5017 							  handle,
5018 							  first, target,
5019 							  new_blk,
5020 							  prev_clusters,
5021 							  v_start);
5022 		if (ret)
5023 			mlog_errno(ret);
5024 	} else {
5025 		/* The start of the last cluster in the first extent */
5026 		u64 last_blk = bucket_blkno(first) +
5027 			((prev_clusters - 1) *
5028 			 ocfs2_clusters_to_blocks(inode->i_sb, 1));
5029 
5030 		if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
5031 			ret = ocfs2_mv_xattr_buckets(inode, handle,
5032 						     bucket_blkno(first),
5033 						     last_blk, new_blk, 0,
5034 						     v_start);
5035 			if (ret)
5036 				mlog_errno(ret);
5037 		} else {
5038 			ret = ocfs2_divide_xattr_cluster(inode, handle,
5039 							 last_blk, new_blk,
5040 							 v_start);
5041 			if (ret)
5042 				mlog_errno(ret);
5043 
5044 			if ((bucket_blkno(target) == last_blk) && extend)
5045 				*extend = 0;
5046 		}
5047 	}
5048 
5049 	return ret;
5050 }
5051 
5052 /*
5053  * Add a new cluster for xattr storage.
5054  *
5055  * If the new cluster is contiguous with the previous one, it will be
5056  * appended to the same extent record, and num_clusters will be updated.
5057  * If not, we will insert a new extent for it and move some xattrs in
5058  * the last cluster into the new allocated one.
5059  * We also need to limit the maximum size of a btree leaf, otherwise we'll
5060  * lose the benefits of hashing because we'll have to search large leaves.
5061  * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5062  * if it's bigger).
5063  *
5064  * first_bh is the first block of the previous extent rec and header_bh
5065  * indicates the bucket we will insert the new xattrs. They will be updated
5066  * when the header_bh is moved into the new cluster.
5067  */
5068 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5069 				       struct buffer_head *root_bh,
5070 				       struct ocfs2_xattr_bucket *first,
5071 				       struct ocfs2_xattr_bucket *target,
5072 				       u32 *num_clusters,
5073 				       u32 prev_cpos,
5074 				       int *extend,
5075 				       struct ocfs2_xattr_set_ctxt *ctxt)
5076 {
5077 	int ret;
5078 	u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5079 	u32 prev_clusters = *num_clusters;
5080 	u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5081 	u64 block;
5082 	handle_t *handle = ctxt->handle;
5083 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5084 	struct ocfs2_extent_tree et;
5085 
5086 	trace_ocfs2_add_new_xattr_cluster_begin(
5087 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
5088 		(unsigned long long)bucket_blkno(first),
5089 		prev_cpos, prev_clusters);
5090 
5091 	ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5092 
5093 	ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5094 				      OCFS2_JOURNAL_ACCESS_WRITE);
5095 	if (ret < 0) {
5096 		mlog_errno(ret);
5097 		goto leave;
5098 	}
5099 
5100 	ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
5101 				     clusters_to_add, &bit_off, &num_bits);
5102 	if (ret < 0) {
5103 		if (ret != -ENOSPC)
5104 			mlog_errno(ret);
5105 		goto leave;
5106 	}
5107 
5108 	BUG_ON(num_bits > clusters_to_add);
5109 
5110 	block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5111 	trace_ocfs2_add_new_xattr_cluster((unsigned long long)block, num_bits);
5112 
5113 	if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5114 	    (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5115 	     OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5116 		/*
5117 		 * If this cluster is contiguous with the old one and
5118 		 * adding this new cluster, we don't surpass the limit of
5119 		 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5120 		 * initialized and used like other buckets in the previous
5121 		 * cluster.
5122 		 * So add it as a contiguous one. The caller will handle
5123 		 * its init process.
5124 		 */
5125 		v_start = prev_cpos + prev_clusters;
5126 		*num_clusters = prev_clusters + num_bits;
5127 	} else {
5128 		ret = ocfs2_adjust_xattr_cross_cluster(inode,
5129 						       handle,
5130 						       first,
5131 						       target,
5132 						       block,
5133 						       prev_clusters,
5134 						       &v_start,
5135 						       extend);
5136 		if (ret) {
5137 			mlog_errno(ret);
5138 			goto leave;
5139 		}
5140 	}
5141 
5142 	trace_ocfs2_add_new_xattr_cluster_insert((unsigned long long)block,
5143 						 v_start, num_bits);
5144 	ret = ocfs2_insert_extent(handle, &et, v_start, block,
5145 				  num_bits, 0, ctxt->meta_ac);
5146 	if (ret < 0) {
5147 		mlog_errno(ret);
5148 		goto leave;
5149 	}
5150 
5151 	ocfs2_journal_dirty(handle, root_bh);
5152 
5153 leave:
5154 	return ret;
5155 }
5156 
5157 /*
5158  * We are given an extent.  'first' is the bucket at the very front of
5159  * the extent.  The extent has space for an additional bucket past
5160  * bucket_xh(first)->xh_num_buckets.  'target_blkno' is the block number
5161  * of the target bucket.  We wish to shift every bucket past the target
5162  * down one, filling in that additional space.  When we get back to the
5163  * target, we split the target between itself and the now-empty bucket
5164  * at target+1 (aka, target_blkno + blks_per_bucket).
5165  */
5166 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5167 				     handle_t *handle,
5168 				     struct ocfs2_xattr_bucket *first,
5169 				     u64 target_blk,
5170 				     u32 num_clusters)
5171 {
5172 	int ret, credits;
5173 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5174 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5175 	u64 end_blk;
5176 	u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5177 
5178 	trace_ocfs2_extend_xattr_bucket((unsigned long long)target_blk,
5179 					(unsigned long long)bucket_blkno(first),
5180 					num_clusters, new_bucket);
5181 
5182 	/* The extent must have room for an additional bucket */
5183 	BUG_ON(new_bucket >=
5184 	       (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5185 
5186 	/* end_blk points to the last existing bucket */
5187 	end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5188 
5189 	/*
5190 	 * end_blk is the start of the last existing bucket.
5191 	 * Thus, (end_blk - target_blk) covers the target bucket and
5192 	 * every bucket after it up to, but not including, the last
5193 	 * existing bucket.  Then we add the last existing bucket, the
5194 	 * new bucket, and the first bucket (3 * blk_per_bucket).
5195 	 */
5196 	credits = (end_blk - target_blk) + (3 * blk_per_bucket);
5197 	ret = ocfs2_extend_trans(handle, credits);
5198 	if (ret) {
5199 		mlog_errno(ret);
5200 		goto out;
5201 	}
5202 
5203 	ret = ocfs2_xattr_bucket_journal_access(handle, first,
5204 						OCFS2_JOURNAL_ACCESS_WRITE);
5205 	if (ret) {
5206 		mlog_errno(ret);
5207 		goto out;
5208 	}
5209 
5210 	while (end_blk != target_blk) {
5211 		ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5212 					    end_blk + blk_per_bucket, 0);
5213 		if (ret)
5214 			goto out;
5215 		end_blk -= blk_per_bucket;
5216 	}
5217 
5218 	/* Move half of the xattr in target_blkno to the next bucket. */
5219 	ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5220 					target_blk + blk_per_bucket, NULL, 0);
5221 
5222 	le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5223 	ocfs2_xattr_bucket_journal_dirty(handle, first);
5224 
5225 out:
5226 	return ret;
5227 }
5228 
5229 /*
5230  * Add new xattr bucket in an extent record and adjust the buckets
5231  * accordingly.  xb_bh is the ocfs2_xattr_block, and target is the
5232  * bucket we want to insert into.
5233  *
5234  * In the easy case, we will move all the buckets after target down by
5235  * one. Half of target's xattrs will be moved to the next bucket.
5236  *
5237  * If current cluster is full, we'll allocate a new one.  This may not
5238  * be contiguous.  The underlying calls will make sure that there is
5239  * space for the insert, shifting buckets around if necessary.
5240  * 'target' may be moved by those calls.
5241  */
5242 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5243 				      struct buffer_head *xb_bh,
5244 				      struct ocfs2_xattr_bucket *target,
5245 				      struct ocfs2_xattr_set_ctxt *ctxt)
5246 {
5247 	struct ocfs2_xattr_block *xb =
5248 			(struct ocfs2_xattr_block *)xb_bh->b_data;
5249 	struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5250 	struct ocfs2_extent_list *el = &xb_root->xt_list;
5251 	u32 name_hash =
5252 		le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5253 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5254 	int ret, num_buckets, extend = 1;
5255 	u64 p_blkno;
5256 	u32 e_cpos, num_clusters;
5257 	/* The bucket at the front of the extent */
5258 	struct ocfs2_xattr_bucket *first;
5259 
5260 	trace_ocfs2_add_new_xattr_bucket(
5261 				(unsigned long long)bucket_blkno(target));
5262 
5263 	/* The first bucket of the original extent */
5264 	first = ocfs2_xattr_bucket_new(inode);
5265 	if (!first) {
5266 		ret = -ENOMEM;
5267 		mlog_errno(ret);
5268 		goto out;
5269 	}
5270 
5271 	ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5272 				  &num_clusters, el);
5273 	if (ret) {
5274 		mlog_errno(ret);
5275 		goto out;
5276 	}
5277 
5278 	ret = ocfs2_read_xattr_bucket(first, p_blkno);
5279 	if (ret) {
5280 		mlog_errno(ret);
5281 		goto out;
5282 	}
5283 
5284 	num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5285 	if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5286 		/*
5287 		 * This can move first+target if the target bucket moves
5288 		 * to the new extent.
5289 		 */
5290 		ret = ocfs2_add_new_xattr_cluster(inode,
5291 						  xb_bh,
5292 						  first,
5293 						  target,
5294 						  &num_clusters,
5295 						  e_cpos,
5296 						  &extend,
5297 						  ctxt);
5298 		if (ret) {
5299 			mlog_errno(ret);
5300 			goto out;
5301 		}
5302 	}
5303 
5304 	if (extend) {
5305 		ret = ocfs2_extend_xattr_bucket(inode,
5306 						ctxt->handle,
5307 						first,
5308 						bucket_blkno(target),
5309 						num_clusters);
5310 		if (ret)
5311 			mlog_errno(ret);
5312 	}
5313 
5314 out:
5315 	ocfs2_xattr_bucket_free(first);
5316 
5317 	return ret;
5318 }
5319 
5320 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5321 					struct ocfs2_xattr_bucket *bucket,
5322 					int offs)
5323 {
5324 	int block_off = offs >> inode->i_sb->s_blocksize_bits;
5325 
5326 	offs = offs % inode->i_sb->s_blocksize;
5327 	return bucket_block(bucket, block_off) + offs;
5328 }
5329 
5330 /*
5331  * Truncate the specified xe_off entry in xattr bucket.
5332  * bucket is indicated by header_bh and len is the new length.
5333  * Both the ocfs2_xattr_value_root and the entry will be updated here.
5334  *
5335  * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5336  */
5337 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5338 					     struct ocfs2_xattr_bucket *bucket,
5339 					     int xe_off,
5340 					     int len,
5341 					     struct ocfs2_xattr_set_ctxt *ctxt)
5342 {
5343 	int ret, offset;
5344 	u64 value_blk;
5345 	struct ocfs2_xattr_entry *xe;
5346 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5347 	size_t blocksize = inode->i_sb->s_blocksize;
5348 	struct ocfs2_xattr_value_buf vb = {
5349 		.vb_access = ocfs2_journal_access,
5350 	};
5351 
5352 	xe = &xh->xh_entries[xe_off];
5353 
5354 	BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5355 
5356 	offset = le16_to_cpu(xe->xe_name_offset) +
5357 		 OCFS2_XATTR_SIZE(xe->xe_name_len);
5358 
5359 	value_blk = offset / blocksize;
5360 
5361 	/* We don't allow ocfs2_xattr_value to be stored in different block. */
5362 	BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5363 
5364 	vb.vb_bh = bucket->bu_bhs[value_blk];
5365 	BUG_ON(!vb.vb_bh);
5366 
5367 	vb.vb_xv = (struct ocfs2_xattr_value_root *)
5368 		(vb.vb_bh->b_data + offset % blocksize);
5369 
5370 	/*
5371 	 * From here on out we have to dirty the bucket.  The generic
5372 	 * value calls only modify one of the bucket's bhs, but we need
5373 	 * to send the bucket at once.  So if they error, they *could* have
5374 	 * modified something.  We have to assume they did, and dirty
5375 	 * the whole bucket.  This leaves us in a consistent state.
5376 	 */
5377 	trace_ocfs2_xattr_bucket_value_truncate(
5378 			(unsigned long long)bucket_blkno(bucket), xe_off, len);
5379 	ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5380 	if (ret) {
5381 		mlog_errno(ret);
5382 		goto out;
5383 	}
5384 
5385 	ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5386 						OCFS2_JOURNAL_ACCESS_WRITE);
5387 	if (ret) {
5388 		mlog_errno(ret);
5389 		goto out;
5390 	}
5391 
5392 	xe->xe_value_size = cpu_to_le64(len);
5393 
5394 	ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5395 
5396 out:
5397 	return ret;
5398 }
5399 
5400 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5401 				  struct buffer_head *root_bh,
5402 				  u64 blkno,
5403 				  u32 cpos,
5404 				  u32 len,
5405 				  void *para)
5406 {
5407 	int ret;
5408 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5409 	struct inode *tl_inode = osb->osb_tl_inode;
5410 	handle_t *handle;
5411 	struct ocfs2_xattr_block *xb =
5412 			(struct ocfs2_xattr_block *)root_bh->b_data;
5413 	struct ocfs2_alloc_context *meta_ac = NULL;
5414 	struct ocfs2_cached_dealloc_ctxt dealloc;
5415 	struct ocfs2_extent_tree et;
5416 
5417 	ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5418 					  ocfs2_delete_xattr_in_bucket, para);
5419 	if (ret) {
5420 		mlog_errno(ret);
5421 		return ret;
5422 	}
5423 
5424 	ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5425 
5426 	ocfs2_init_dealloc_ctxt(&dealloc);
5427 
5428 	trace_ocfs2_rm_xattr_cluster(
5429 			(unsigned long long)OCFS2_I(inode)->ip_blkno,
5430 			(unsigned long long)blkno, cpos, len);
5431 
5432 	ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5433 					       len);
5434 
5435 	ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5436 	if (ret) {
5437 		mlog_errno(ret);
5438 		return ret;
5439 	}
5440 
5441 	mutex_lock(&tl_inode->i_mutex);
5442 
5443 	if (ocfs2_truncate_log_needs_flush(osb)) {
5444 		ret = __ocfs2_flush_truncate_log(osb);
5445 		if (ret < 0) {
5446 			mlog_errno(ret);
5447 			goto out;
5448 		}
5449 	}
5450 
5451 	handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5452 	if (IS_ERR(handle)) {
5453 		ret = -ENOMEM;
5454 		mlog_errno(ret);
5455 		goto out;
5456 	}
5457 
5458 	ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5459 				      OCFS2_JOURNAL_ACCESS_WRITE);
5460 	if (ret) {
5461 		mlog_errno(ret);
5462 		goto out_commit;
5463 	}
5464 
5465 	ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5466 				  &dealloc);
5467 	if (ret) {
5468 		mlog_errno(ret);
5469 		goto out_commit;
5470 	}
5471 
5472 	le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5473 	ocfs2_journal_dirty(handle, root_bh);
5474 
5475 	ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5476 	if (ret)
5477 		mlog_errno(ret);
5478 
5479 out_commit:
5480 	ocfs2_commit_trans(osb, handle);
5481 out:
5482 	ocfs2_schedule_truncate_log_flush(osb, 1);
5483 
5484 	mutex_unlock(&tl_inode->i_mutex);
5485 
5486 	if (meta_ac)
5487 		ocfs2_free_alloc_context(meta_ac);
5488 
5489 	ocfs2_run_deallocs(osb, &dealloc);
5490 
5491 	return ret;
5492 }
5493 
5494 /*
5495  * check whether the xattr bucket is filled up with the same hash value.
5496  * If we want to insert the xattr with the same hash, return -ENOSPC.
5497  * If we want to insert a xattr with different hash value, go ahead
5498  * and ocfs2_divide_xattr_bucket will handle this.
5499  */
5500 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5501 					      struct ocfs2_xattr_bucket *bucket,
5502 					      const char *name)
5503 {
5504 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5505 	u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5506 
5507 	if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5508 		return 0;
5509 
5510 	if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5511 	    xh->xh_entries[0].xe_name_hash) {
5512 		mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5513 		     "hash = %u\n",
5514 		     (unsigned long long)bucket_blkno(bucket),
5515 		     le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5516 		return -ENOSPC;
5517 	}
5518 
5519 	return 0;
5520 }
5521 
5522 /*
5523  * Try to set the entry in the current bucket.  If we fail, the caller
5524  * will handle getting us another bucket.
5525  */
5526 static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5527 					struct ocfs2_xattr_info *xi,
5528 					struct ocfs2_xattr_search *xs,
5529 					struct ocfs2_xattr_set_ctxt *ctxt)
5530 {
5531 	int ret;
5532 	struct ocfs2_xa_loc loc;
5533 
5534 	trace_ocfs2_xattr_set_entry_bucket(xi->xi_name);
5535 
5536 	ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5537 				       xs->not_found ? NULL : xs->here);
5538 	ret = ocfs2_xa_set(&loc, xi, ctxt);
5539 	if (!ret) {
5540 		xs->here = loc.xl_entry;
5541 		goto out;
5542 	}
5543 	if (ret != -ENOSPC) {
5544 		mlog_errno(ret);
5545 		goto out;
5546 	}
5547 
5548 	/* Ok, we need space.  Let's try defragmenting the bucket. */
5549 	ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5550 					xs->bucket);
5551 	if (ret) {
5552 		mlog_errno(ret);
5553 		goto out;
5554 	}
5555 
5556 	ret = ocfs2_xa_set(&loc, xi, ctxt);
5557 	if (!ret) {
5558 		xs->here = loc.xl_entry;
5559 		goto out;
5560 	}
5561 	if (ret != -ENOSPC)
5562 		mlog_errno(ret);
5563 
5564 
5565 out:
5566 	return ret;
5567 }
5568 
5569 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5570 					     struct ocfs2_xattr_info *xi,
5571 					     struct ocfs2_xattr_search *xs,
5572 					     struct ocfs2_xattr_set_ctxt *ctxt)
5573 {
5574 	int ret;
5575 
5576 	trace_ocfs2_xattr_set_entry_index_block(xi->xi_name);
5577 
5578 	ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5579 	if (!ret)
5580 		goto out;
5581 	if (ret != -ENOSPC) {
5582 		mlog_errno(ret);
5583 		goto out;
5584 	}
5585 
5586 	/* Ack, need more space.  Let's try to get another bucket! */
5587 
5588 	/*
5589 	 * We do not allow for overlapping ranges between buckets. And
5590 	 * the maximum number of collisions we will allow for then is
5591 	 * one bucket's worth, so check it here whether we need to
5592 	 * add a new bucket for the insert.
5593 	 */
5594 	ret = ocfs2_check_xattr_bucket_collision(inode,
5595 						 xs->bucket,
5596 						 xi->xi_name);
5597 	if (ret) {
5598 		mlog_errno(ret);
5599 		goto out;
5600 	}
5601 
5602 	ret = ocfs2_add_new_xattr_bucket(inode,
5603 					 xs->xattr_bh,
5604 					 xs->bucket,
5605 					 ctxt);
5606 	if (ret) {
5607 		mlog_errno(ret);
5608 		goto out;
5609 	}
5610 
5611 	/*
5612 	 * ocfs2_add_new_xattr_bucket() will have updated
5613 	 * xs->bucket if it moved, but it will not have updated
5614 	 * any of the other search fields.  Thus, we drop it and
5615 	 * re-search.  Everything should be cached, so it'll be
5616 	 * quick.
5617 	 */
5618 	ocfs2_xattr_bucket_relse(xs->bucket);
5619 	ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5620 					   xi->xi_name_index,
5621 					   xi->xi_name, xs);
5622 	if (ret && ret != -ENODATA)
5623 		goto out;
5624 	xs->not_found = ret;
5625 
5626 	/* Ok, we have a new bucket, let's try again */
5627 	ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5628 	if (ret && (ret != -ENOSPC))
5629 		mlog_errno(ret);
5630 
5631 out:
5632 	return ret;
5633 }
5634 
5635 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5636 					struct ocfs2_xattr_bucket *bucket,
5637 					void *para)
5638 {
5639 	int ret = 0, ref_credits;
5640 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5641 	u16 i;
5642 	struct ocfs2_xattr_entry *xe;
5643 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5644 	struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5645 	int credits = ocfs2_remove_extent_credits(osb->sb) +
5646 		ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5647 	struct ocfs2_xattr_value_root *xv;
5648 	struct ocfs2_rm_xattr_bucket_para *args =
5649 			(struct ocfs2_rm_xattr_bucket_para *)para;
5650 
5651 	ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5652 
5653 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5654 		xe = &xh->xh_entries[i];
5655 		if (ocfs2_xattr_is_local(xe))
5656 			continue;
5657 
5658 		ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5659 						      i, &xv, NULL);
5660 
5661 		ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5662 							 args->ref_ci,
5663 							 args->ref_root_bh,
5664 							 &ctxt.meta_ac,
5665 							 &ref_credits);
5666 
5667 		ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5668 		if (IS_ERR(ctxt.handle)) {
5669 			ret = PTR_ERR(ctxt.handle);
5670 			mlog_errno(ret);
5671 			break;
5672 		}
5673 
5674 		ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5675 							i, 0, &ctxt);
5676 
5677 		ocfs2_commit_trans(osb, ctxt.handle);
5678 		if (ctxt.meta_ac) {
5679 			ocfs2_free_alloc_context(ctxt.meta_ac);
5680 			ctxt.meta_ac = NULL;
5681 		}
5682 		if (ret) {
5683 			mlog_errno(ret);
5684 			break;
5685 		}
5686 	}
5687 
5688 	if (ctxt.meta_ac)
5689 		ocfs2_free_alloc_context(ctxt.meta_ac);
5690 	ocfs2_schedule_truncate_log_flush(osb, 1);
5691 	ocfs2_run_deallocs(osb, &ctxt.dealloc);
5692 	return ret;
5693 }
5694 
5695 /*
5696  * Whenever we modify a xattr value root in the bucket(e.g, CoW
5697  * or change the extent record flag), we need to recalculate
5698  * the metaecc for the whole bucket. So it is done here.
5699  *
5700  * Note:
5701  * We have to give the extra credits for the caller.
5702  */
5703 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5704 					    handle_t *handle,
5705 					    void *para)
5706 {
5707 	int ret;
5708 	struct ocfs2_xattr_bucket *bucket =
5709 			(struct ocfs2_xattr_bucket *)para;
5710 
5711 	ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5712 						OCFS2_JOURNAL_ACCESS_WRITE);
5713 	if (ret) {
5714 		mlog_errno(ret);
5715 		return ret;
5716 	}
5717 
5718 	ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5719 
5720 	return 0;
5721 }
5722 
5723 /*
5724  * Special action we need if the xattr value is refcounted.
5725  *
5726  * 1. If the xattr is refcounted, lock the tree.
5727  * 2. CoW the xattr if we are setting the new value and the value
5728  *    will be stored outside.
5729  * 3. In other case, decrease_refcount will work for us, so just
5730  *    lock the refcount tree, calculate the meta and credits is OK.
5731  *
5732  * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5733  * currently CoW is a completed transaction, while this function
5734  * will also lock the allocators and let us deadlock. So we will
5735  * CoW the whole xattr value.
5736  */
5737 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5738 					struct ocfs2_dinode *di,
5739 					struct ocfs2_xattr_info *xi,
5740 					struct ocfs2_xattr_search *xis,
5741 					struct ocfs2_xattr_search *xbs,
5742 					struct ocfs2_refcount_tree **ref_tree,
5743 					int *meta_add,
5744 					int *credits)
5745 {
5746 	int ret = 0;
5747 	struct ocfs2_xattr_block *xb;
5748 	struct ocfs2_xattr_entry *xe;
5749 	char *base;
5750 	u32 p_cluster, num_clusters;
5751 	unsigned int ext_flags;
5752 	int name_offset, name_len;
5753 	struct ocfs2_xattr_value_buf vb;
5754 	struct ocfs2_xattr_bucket *bucket = NULL;
5755 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5756 	struct ocfs2_post_refcount refcount;
5757 	struct ocfs2_post_refcount *p = NULL;
5758 	struct buffer_head *ref_root_bh = NULL;
5759 
5760 	if (!xis->not_found) {
5761 		xe = xis->here;
5762 		name_offset = le16_to_cpu(xe->xe_name_offset);
5763 		name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5764 		base = xis->base;
5765 		vb.vb_bh = xis->inode_bh;
5766 		vb.vb_access = ocfs2_journal_access_di;
5767 	} else {
5768 		int i, block_off = 0;
5769 		xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5770 		xe = xbs->here;
5771 		name_offset = le16_to_cpu(xe->xe_name_offset);
5772 		name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5773 		i = xbs->here - xbs->header->xh_entries;
5774 
5775 		if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5776 			ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5777 							bucket_xh(xbs->bucket),
5778 							i, &block_off,
5779 							&name_offset);
5780 			if (ret) {
5781 				mlog_errno(ret);
5782 				goto out;
5783 			}
5784 			base = bucket_block(xbs->bucket, block_off);
5785 			vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5786 			vb.vb_access = ocfs2_journal_access;
5787 
5788 			if (ocfs2_meta_ecc(osb)) {
5789 				/*create parameters for ocfs2_post_refcount. */
5790 				bucket = xbs->bucket;
5791 				refcount.credits = bucket->bu_blocks;
5792 				refcount.para = bucket;
5793 				refcount.func =
5794 					ocfs2_xattr_bucket_post_refcount;
5795 				p = &refcount;
5796 			}
5797 		} else {
5798 			base = xbs->base;
5799 			vb.vb_bh = xbs->xattr_bh;
5800 			vb.vb_access = ocfs2_journal_access_xb;
5801 		}
5802 	}
5803 
5804 	if (ocfs2_xattr_is_local(xe))
5805 		goto out;
5806 
5807 	vb.vb_xv = (struct ocfs2_xattr_value_root *)
5808 				(base + name_offset + name_len);
5809 
5810 	ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5811 				       &num_clusters, &vb.vb_xv->xr_list,
5812 				       &ext_flags);
5813 	if (ret) {
5814 		mlog_errno(ret);
5815 		goto out;
5816 	}
5817 
5818 	/*
5819 	 * We just need to check the 1st extent record, since we always
5820 	 * CoW the whole xattr. So there shouldn't be a xattr with
5821 	 * some REFCOUNT extent recs after the 1st one.
5822 	 */
5823 	if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5824 		goto out;
5825 
5826 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5827 				       1, ref_tree, &ref_root_bh);
5828 	if (ret) {
5829 		mlog_errno(ret);
5830 		goto out;
5831 	}
5832 
5833 	/*
5834 	 * If we are deleting the xattr or the new size will be stored inside,
5835 	 * cool, leave it there, the xattr truncate process will remove them
5836 	 * for us(it still needs the refcount tree lock and the meta, credits).
5837 	 * And the worse case is that every cluster truncate will split the
5838 	 * refcount tree, and make the original extent become 3. So we will need
5839 	 * 2 * cluster more extent recs at most.
5840 	 */
5841 	if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5842 
5843 		ret = ocfs2_refcounted_xattr_delete_need(inode,
5844 							 &(*ref_tree)->rf_ci,
5845 							 ref_root_bh, vb.vb_xv,
5846 							 meta_add, credits);
5847 		if (ret)
5848 			mlog_errno(ret);
5849 		goto out;
5850 	}
5851 
5852 	ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5853 				       *ref_tree, ref_root_bh, 0,
5854 				       le32_to_cpu(vb.vb_xv->xr_clusters), p);
5855 	if (ret)
5856 		mlog_errno(ret);
5857 
5858 out:
5859 	brelse(ref_root_bh);
5860 	return ret;
5861 }
5862 
5863 /*
5864  * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5865  * The physical clusters will be added to refcount tree.
5866  */
5867 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5868 				struct ocfs2_xattr_value_root *xv,
5869 				struct ocfs2_extent_tree *value_et,
5870 				struct ocfs2_caching_info *ref_ci,
5871 				struct buffer_head *ref_root_bh,
5872 				struct ocfs2_cached_dealloc_ctxt *dealloc,
5873 				struct ocfs2_post_refcount *refcount)
5874 {
5875 	int ret = 0;
5876 	u32 clusters = le32_to_cpu(xv->xr_clusters);
5877 	u32 cpos, p_cluster, num_clusters;
5878 	struct ocfs2_extent_list *el = &xv->xr_list;
5879 	unsigned int ext_flags;
5880 
5881 	cpos = 0;
5882 	while (cpos < clusters) {
5883 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5884 					       &num_clusters, el, &ext_flags);
5885 		if (ret) {
5886 			mlog_errno(ret);
5887 			break;
5888 		}
5889 
5890 		cpos += num_clusters;
5891 		if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5892 			continue;
5893 
5894 		BUG_ON(!p_cluster);
5895 
5896 		ret = ocfs2_add_refcount_flag(inode, value_et,
5897 					      ref_ci, ref_root_bh,
5898 					      cpos - num_clusters,
5899 					      p_cluster, num_clusters,
5900 					      dealloc, refcount);
5901 		if (ret) {
5902 			mlog_errno(ret);
5903 			break;
5904 		}
5905 	}
5906 
5907 	return ret;
5908 }
5909 
5910 /*
5911  * Given a normal ocfs2_xattr_header, refcount all the entries which
5912  * have value stored outside.
5913  * Used for xattrs stored in inode and ocfs2_xattr_block.
5914  */
5915 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5916 				struct ocfs2_xattr_value_buf *vb,
5917 				struct ocfs2_xattr_header *header,
5918 				struct ocfs2_caching_info *ref_ci,
5919 				struct buffer_head *ref_root_bh,
5920 				struct ocfs2_cached_dealloc_ctxt *dealloc)
5921 {
5922 
5923 	struct ocfs2_xattr_entry *xe;
5924 	struct ocfs2_xattr_value_root *xv;
5925 	struct ocfs2_extent_tree et;
5926 	int i, ret = 0;
5927 
5928 	for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5929 		xe = &header->xh_entries[i];
5930 
5931 		if (ocfs2_xattr_is_local(xe))
5932 			continue;
5933 
5934 		xv = (struct ocfs2_xattr_value_root *)((void *)header +
5935 			le16_to_cpu(xe->xe_name_offset) +
5936 			OCFS2_XATTR_SIZE(xe->xe_name_len));
5937 
5938 		vb->vb_xv = xv;
5939 		ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5940 
5941 		ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5942 							ref_ci, ref_root_bh,
5943 							dealloc, NULL);
5944 		if (ret) {
5945 			mlog_errno(ret);
5946 			break;
5947 		}
5948 	}
5949 
5950 	return ret;
5951 }
5952 
5953 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5954 				struct buffer_head *fe_bh,
5955 				struct ocfs2_caching_info *ref_ci,
5956 				struct buffer_head *ref_root_bh,
5957 				struct ocfs2_cached_dealloc_ctxt *dealloc)
5958 {
5959 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5960 	struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5961 				(fe_bh->b_data + inode->i_sb->s_blocksize -
5962 				le16_to_cpu(di->i_xattr_inline_size));
5963 	struct ocfs2_xattr_value_buf vb = {
5964 		.vb_bh = fe_bh,
5965 		.vb_access = ocfs2_journal_access_di,
5966 	};
5967 
5968 	return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5969 						  ref_ci, ref_root_bh, dealloc);
5970 }
5971 
5972 struct ocfs2_xattr_tree_value_refcount_para {
5973 	struct ocfs2_caching_info *ref_ci;
5974 	struct buffer_head *ref_root_bh;
5975 	struct ocfs2_cached_dealloc_ctxt *dealloc;
5976 };
5977 
5978 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5979 					   struct ocfs2_xattr_bucket *bucket,
5980 					   int offset,
5981 					   struct ocfs2_xattr_value_root **xv,
5982 					   struct buffer_head **bh)
5983 {
5984 	int ret, block_off, name_offset;
5985 	struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5986 	struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5987 	void *base;
5988 
5989 	ret = ocfs2_xattr_bucket_get_name_value(sb,
5990 						bucket_xh(bucket),
5991 						offset,
5992 						&block_off,
5993 						&name_offset);
5994 	if (ret) {
5995 		mlog_errno(ret);
5996 		goto out;
5997 	}
5998 
5999 	base = bucket_block(bucket, block_off);
6000 
6001 	*xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6002 			 OCFS2_XATTR_SIZE(xe->xe_name_len));
6003 
6004 	if (bh)
6005 		*bh = bucket->bu_bhs[block_off];
6006 out:
6007 	return ret;
6008 }
6009 
6010 /*
6011  * For a given xattr bucket, refcount all the entries which
6012  * have value stored outside.
6013  */
6014 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6015 					     struct ocfs2_xattr_bucket *bucket,
6016 					     void *para)
6017 {
6018 	int i, ret = 0;
6019 	struct ocfs2_extent_tree et;
6020 	struct ocfs2_xattr_tree_value_refcount_para *ref =
6021 			(struct ocfs2_xattr_tree_value_refcount_para *)para;
6022 	struct ocfs2_xattr_header *xh =
6023 			(struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6024 	struct ocfs2_xattr_entry *xe;
6025 	struct ocfs2_xattr_value_buf vb = {
6026 		.vb_access = ocfs2_journal_access,
6027 	};
6028 	struct ocfs2_post_refcount refcount = {
6029 		.credits = bucket->bu_blocks,
6030 		.para = bucket,
6031 		.func = ocfs2_xattr_bucket_post_refcount,
6032 	};
6033 	struct ocfs2_post_refcount *p = NULL;
6034 
6035 	/* We only need post_refcount if we support metaecc. */
6036 	if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6037 		p = &refcount;
6038 
6039 	trace_ocfs2_xattr_bucket_value_refcount(
6040 				(unsigned long long)bucket_blkno(bucket),
6041 				le16_to_cpu(xh->xh_count));
6042 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6043 		xe = &xh->xh_entries[i];
6044 
6045 		if (ocfs2_xattr_is_local(xe))
6046 			continue;
6047 
6048 		ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6049 						      &vb.vb_xv, &vb.vb_bh);
6050 		if (ret) {
6051 			mlog_errno(ret);
6052 			break;
6053 		}
6054 
6055 		ocfs2_init_xattr_value_extent_tree(&et,
6056 						   INODE_CACHE(inode), &vb);
6057 
6058 		ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6059 							&et, ref->ref_ci,
6060 							ref->ref_root_bh,
6061 							ref->dealloc, p);
6062 		if (ret) {
6063 			mlog_errno(ret);
6064 			break;
6065 		}
6066 	}
6067 
6068 	return ret;
6069 
6070 }
6071 
6072 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6073 				     struct buffer_head *root_bh,
6074 				     u64 blkno, u32 cpos, u32 len, void *para)
6075 {
6076 	return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6077 					   ocfs2_xattr_bucket_value_refcount,
6078 					   para);
6079 }
6080 
6081 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6082 				struct buffer_head *blk_bh,
6083 				struct ocfs2_caching_info *ref_ci,
6084 				struct buffer_head *ref_root_bh,
6085 				struct ocfs2_cached_dealloc_ctxt *dealloc)
6086 {
6087 	int ret = 0;
6088 	struct ocfs2_xattr_block *xb =
6089 				(struct ocfs2_xattr_block *)blk_bh->b_data;
6090 
6091 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6092 		struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6093 		struct ocfs2_xattr_value_buf vb = {
6094 			.vb_bh = blk_bh,
6095 			.vb_access = ocfs2_journal_access_xb,
6096 		};
6097 
6098 		ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6099 							 ref_ci, ref_root_bh,
6100 							 dealloc);
6101 	} else {
6102 		struct ocfs2_xattr_tree_value_refcount_para para = {
6103 			.ref_ci = ref_ci,
6104 			.ref_root_bh = ref_root_bh,
6105 			.dealloc = dealloc,
6106 		};
6107 
6108 		ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6109 						ocfs2_refcount_xattr_tree_rec,
6110 						&para);
6111 	}
6112 
6113 	return ret;
6114 }
6115 
6116 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6117 				     struct buffer_head *fe_bh,
6118 				     struct ocfs2_caching_info *ref_ci,
6119 				     struct buffer_head *ref_root_bh,
6120 				     struct ocfs2_cached_dealloc_ctxt *dealloc)
6121 {
6122 	int ret = 0;
6123 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
6124 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6125 	struct buffer_head *blk_bh = NULL;
6126 
6127 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6128 		ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6129 							 ref_ci, ref_root_bh,
6130 							 dealloc);
6131 		if (ret) {
6132 			mlog_errno(ret);
6133 			goto out;
6134 		}
6135 	}
6136 
6137 	if (!di->i_xattr_loc)
6138 		goto out;
6139 
6140 	ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6141 				     &blk_bh);
6142 	if (ret < 0) {
6143 		mlog_errno(ret);
6144 		goto out;
6145 	}
6146 
6147 	ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6148 						ref_root_bh, dealloc);
6149 	if (ret)
6150 		mlog_errno(ret);
6151 
6152 	brelse(blk_bh);
6153 out:
6154 
6155 	return ret;
6156 }
6157 
6158 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6159 /*
6160  * Store the information we need in xattr reflink.
6161  * old_bh and new_bh are inode bh for the old and new inode.
6162  */
6163 struct ocfs2_xattr_reflink {
6164 	struct inode *old_inode;
6165 	struct inode *new_inode;
6166 	struct buffer_head *old_bh;
6167 	struct buffer_head *new_bh;
6168 	struct ocfs2_caching_info *ref_ci;
6169 	struct buffer_head *ref_root_bh;
6170 	struct ocfs2_cached_dealloc_ctxt *dealloc;
6171 	should_xattr_reflinked *xattr_reflinked;
6172 };
6173 
6174 /*
6175  * Given a xattr header and xe offset,
6176  * return the proper xv and the corresponding bh.
6177  * xattr in inode, block and xattr tree have different implementaions.
6178  */
6179 typedef int (get_xattr_value_root)(struct super_block *sb,
6180 				   struct buffer_head *bh,
6181 				   struct ocfs2_xattr_header *xh,
6182 				   int offset,
6183 				   struct ocfs2_xattr_value_root **xv,
6184 				   struct buffer_head **ret_bh,
6185 				   void *para);
6186 
6187 /*
6188  * Calculate all the xattr value root metadata stored in this xattr header and
6189  * credits we need if we create them from the scratch.
6190  * We use get_xattr_value_root so that all types of xattr container can use it.
6191  */
6192 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6193 					     struct buffer_head *bh,
6194 					     struct ocfs2_xattr_header *xh,
6195 					     int *metas, int *credits,
6196 					     int *num_recs,
6197 					     get_xattr_value_root *func,
6198 					     void *para)
6199 {
6200 	int i, ret = 0;
6201 	struct ocfs2_xattr_value_root *xv;
6202 	struct ocfs2_xattr_entry *xe;
6203 
6204 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6205 		xe = &xh->xh_entries[i];
6206 		if (ocfs2_xattr_is_local(xe))
6207 			continue;
6208 
6209 		ret = func(sb, bh, xh, i, &xv, NULL, para);
6210 		if (ret) {
6211 			mlog_errno(ret);
6212 			break;
6213 		}
6214 
6215 		*metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6216 			  le16_to_cpu(xv->xr_list.l_next_free_rec);
6217 
6218 		*credits += ocfs2_calc_extend_credits(sb,
6219 						&def_xv.xv.xr_list,
6220 						le32_to_cpu(xv->xr_clusters));
6221 
6222 		/*
6223 		 * If the value is a tree with depth > 1, We don't go deep
6224 		 * to the extent block, so just calculate a maximum record num.
6225 		 */
6226 		if (!xv->xr_list.l_tree_depth)
6227 			*num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6228 		else
6229 			*num_recs += ocfs2_clusters_for_bytes(sb,
6230 							      XATTR_SIZE_MAX);
6231 	}
6232 
6233 	return ret;
6234 }
6235 
6236 /* Used by xattr inode and block to return the right xv and buffer_head. */
6237 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6238 				      struct buffer_head *bh,
6239 				      struct ocfs2_xattr_header *xh,
6240 				      int offset,
6241 				      struct ocfs2_xattr_value_root **xv,
6242 				      struct buffer_head **ret_bh,
6243 				      void *para)
6244 {
6245 	struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6246 
6247 	*xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6248 		le16_to_cpu(xe->xe_name_offset) +
6249 		OCFS2_XATTR_SIZE(xe->xe_name_len));
6250 
6251 	if (ret_bh)
6252 		*ret_bh = bh;
6253 
6254 	return 0;
6255 }
6256 
6257 /*
6258  * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6259  * It is only used for inline xattr and xattr block.
6260  */
6261 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6262 					struct ocfs2_xattr_header *xh,
6263 					struct buffer_head *ref_root_bh,
6264 					int *credits,
6265 					struct ocfs2_alloc_context **meta_ac)
6266 {
6267 	int ret, meta_add = 0, num_recs = 0;
6268 	struct ocfs2_refcount_block *rb =
6269 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
6270 
6271 	*credits = 0;
6272 
6273 	ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6274 						&meta_add, credits, &num_recs,
6275 						ocfs2_get_xattr_value_root,
6276 						NULL);
6277 	if (ret) {
6278 		mlog_errno(ret);
6279 		goto out;
6280 	}
6281 
6282 	/*
6283 	 * We need to add/modify num_recs in refcount tree, so just calculate
6284 	 * an approximate number we need for refcount tree change.
6285 	 * Sometimes we need to split the tree, and after split,  half recs
6286 	 * will be moved to the new block, and a new block can only provide
6287 	 * half number of recs. So we multiple new blocks by 2.
6288 	 */
6289 	num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6290 	meta_add += num_recs;
6291 	*credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6292 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6293 		*credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6294 			    le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6295 	else
6296 		*credits += 1;
6297 
6298 	ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6299 	if (ret)
6300 		mlog_errno(ret);
6301 
6302 out:
6303 	return ret;
6304 }
6305 
6306 /*
6307  * Given a xattr header, reflink all the xattrs in this container.
6308  * It can be used for inode, block and bucket.
6309  *
6310  * NOTE:
6311  * Before we call this function, the caller has memcpy the xattr in
6312  * old_xh to the new_xh.
6313  *
6314  * If args.xattr_reflinked is set, call it to decide whether the xe should
6315  * be reflinked or not. If not, remove it from the new xattr header.
6316  */
6317 static int ocfs2_reflink_xattr_header(handle_t *handle,
6318 				      struct ocfs2_xattr_reflink *args,
6319 				      struct buffer_head *old_bh,
6320 				      struct ocfs2_xattr_header *xh,
6321 				      struct buffer_head *new_bh,
6322 				      struct ocfs2_xattr_header *new_xh,
6323 				      struct ocfs2_xattr_value_buf *vb,
6324 				      struct ocfs2_alloc_context *meta_ac,
6325 				      get_xattr_value_root *func,
6326 				      void *para)
6327 {
6328 	int ret = 0, i, j;
6329 	struct super_block *sb = args->old_inode->i_sb;
6330 	struct buffer_head *value_bh;
6331 	struct ocfs2_xattr_entry *xe, *last;
6332 	struct ocfs2_xattr_value_root *xv, *new_xv;
6333 	struct ocfs2_extent_tree data_et;
6334 	u32 clusters, cpos, p_cluster, num_clusters;
6335 	unsigned int ext_flags = 0;
6336 
6337 	trace_ocfs2_reflink_xattr_header((unsigned long long)old_bh->b_blocknr,
6338 					 le16_to_cpu(xh->xh_count));
6339 
6340 	last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6341 	for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6342 		xe = &xh->xh_entries[i];
6343 
6344 		if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6345 			xe = &new_xh->xh_entries[j];
6346 
6347 			le16_add_cpu(&new_xh->xh_count, -1);
6348 			if (new_xh->xh_count) {
6349 				memmove(xe, xe + 1,
6350 					(void *)last - (void *)xe);
6351 				memset(last, 0,
6352 				       sizeof(struct ocfs2_xattr_entry));
6353 			}
6354 
6355 			/*
6356 			 * We don't want j to increase in the next round since
6357 			 * it is already moved ahead.
6358 			 */
6359 			j--;
6360 			continue;
6361 		}
6362 
6363 		if (ocfs2_xattr_is_local(xe))
6364 			continue;
6365 
6366 		ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6367 		if (ret) {
6368 			mlog_errno(ret);
6369 			break;
6370 		}
6371 
6372 		ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6373 		if (ret) {
6374 			mlog_errno(ret);
6375 			break;
6376 		}
6377 
6378 		/*
6379 		 * For the xattr which has l_tree_depth = 0, all the extent
6380 		 * recs have already be copied to the new xh with the
6381 		 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6382 		 * increase the refount count int the refcount tree.
6383 		 *
6384 		 * For the xattr which has l_tree_depth > 0, we need
6385 		 * to initialize it to the empty default value root,
6386 		 * and then insert the extents one by one.
6387 		 */
6388 		if (xv->xr_list.l_tree_depth) {
6389 			memcpy(new_xv, &def_xv, sizeof(def_xv));
6390 			vb->vb_xv = new_xv;
6391 			vb->vb_bh = value_bh;
6392 			ocfs2_init_xattr_value_extent_tree(&data_et,
6393 					INODE_CACHE(args->new_inode), vb);
6394 		}
6395 
6396 		clusters = le32_to_cpu(xv->xr_clusters);
6397 		cpos = 0;
6398 		while (cpos < clusters) {
6399 			ret = ocfs2_xattr_get_clusters(args->old_inode,
6400 						       cpos,
6401 						       &p_cluster,
6402 						       &num_clusters,
6403 						       &xv->xr_list,
6404 						       &ext_flags);
6405 			if (ret) {
6406 				mlog_errno(ret);
6407 				goto out;
6408 			}
6409 
6410 			BUG_ON(!p_cluster);
6411 
6412 			if (xv->xr_list.l_tree_depth) {
6413 				ret = ocfs2_insert_extent(handle,
6414 						&data_et, cpos,
6415 						ocfs2_clusters_to_blocks(
6416 							args->old_inode->i_sb,
6417 							p_cluster),
6418 						num_clusters, ext_flags,
6419 						meta_ac);
6420 				if (ret) {
6421 					mlog_errno(ret);
6422 					goto out;
6423 				}
6424 			}
6425 
6426 			ret = ocfs2_increase_refcount(handle, args->ref_ci,
6427 						      args->ref_root_bh,
6428 						      p_cluster, num_clusters,
6429 						      meta_ac, args->dealloc);
6430 			if (ret) {
6431 				mlog_errno(ret);
6432 				goto out;
6433 			}
6434 
6435 			cpos += num_clusters;
6436 		}
6437 	}
6438 
6439 out:
6440 	return ret;
6441 }
6442 
6443 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6444 {
6445 	int ret = 0, credits = 0;
6446 	handle_t *handle;
6447 	struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6448 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6449 	int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6450 	int header_off = osb->sb->s_blocksize - inline_size;
6451 	struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6452 					(args->old_bh->b_data + header_off);
6453 	struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6454 					(args->new_bh->b_data + header_off);
6455 	struct ocfs2_alloc_context *meta_ac = NULL;
6456 	struct ocfs2_inode_info *new_oi;
6457 	struct ocfs2_dinode *new_di;
6458 	struct ocfs2_xattr_value_buf vb = {
6459 		.vb_bh = args->new_bh,
6460 		.vb_access = ocfs2_journal_access_di,
6461 	};
6462 
6463 	ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6464 						  &credits, &meta_ac);
6465 	if (ret) {
6466 		mlog_errno(ret);
6467 		goto out;
6468 	}
6469 
6470 	handle = ocfs2_start_trans(osb, credits);
6471 	if (IS_ERR(handle)) {
6472 		ret = PTR_ERR(handle);
6473 		mlog_errno(ret);
6474 		goto out;
6475 	}
6476 
6477 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6478 				      args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6479 	if (ret) {
6480 		mlog_errno(ret);
6481 		goto out_commit;
6482 	}
6483 
6484 	memcpy(args->new_bh->b_data + header_off,
6485 	       args->old_bh->b_data + header_off, inline_size);
6486 
6487 	new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6488 	new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6489 
6490 	ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6491 					 args->new_bh, new_xh, &vb, meta_ac,
6492 					 ocfs2_get_xattr_value_root, NULL);
6493 	if (ret) {
6494 		mlog_errno(ret);
6495 		goto out_commit;
6496 	}
6497 
6498 	new_oi = OCFS2_I(args->new_inode);
6499 	/*
6500 	 * Adjust extent record count to reserve space for extended attribute.
6501 	 * Inline data count had been adjusted in ocfs2_duplicate_inline_data().
6502 	 */
6503 	if (!(new_oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) &&
6504 	    !(ocfs2_inode_is_fast_symlink(args->new_inode))) {
6505 		struct ocfs2_extent_list *el = &new_di->id2.i_list;
6506 		le16_add_cpu(&el->l_count, -(inline_size /
6507 					sizeof(struct ocfs2_extent_rec)));
6508 	}
6509 	spin_lock(&new_oi->ip_lock);
6510 	new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6511 	new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6512 	spin_unlock(&new_oi->ip_lock);
6513 
6514 	ocfs2_journal_dirty(handle, args->new_bh);
6515 
6516 out_commit:
6517 	ocfs2_commit_trans(osb, handle);
6518 
6519 out:
6520 	if (meta_ac)
6521 		ocfs2_free_alloc_context(meta_ac);
6522 	return ret;
6523 }
6524 
6525 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6526 					  struct buffer_head *fe_bh,
6527 					  struct buffer_head **ret_bh,
6528 					  int indexed)
6529 {
6530 	int ret;
6531 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6532 	struct ocfs2_xattr_set_ctxt ctxt;
6533 
6534 	memset(&ctxt, 0, sizeof(ctxt));
6535 	ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
6536 	if (ret < 0) {
6537 		mlog_errno(ret);
6538 		return ret;
6539 	}
6540 
6541 	ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6542 	if (IS_ERR(ctxt.handle)) {
6543 		ret = PTR_ERR(ctxt.handle);
6544 		mlog_errno(ret);
6545 		goto out;
6546 	}
6547 
6548 	trace_ocfs2_create_empty_xattr_block(
6549 				(unsigned long long)fe_bh->b_blocknr, indexed);
6550 	ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6551 				       ret_bh);
6552 	if (ret)
6553 		mlog_errno(ret);
6554 
6555 	ocfs2_commit_trans(osb, ctxt.handle);
6556 out:
6557 	ocfs2_free_alloc_context(ctxt.meta_ac);
6558 	return ret;
6559 }
6560 
6561 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6562 				     struct buffer_head *blk_bh,
6563 				     struct buffer_head *new_blk_bh)
6564 {
6565 	int ret = 0, credits = 0;
6566 	handle_t *handle;
6567 	struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6568 	struct ocfs2_dinode *new_di;
6569 	struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6570 	int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6571 	struct ocfs2_xattr_block *xb =
6572 			(struct ocfs2_xattr_block *)blk_bh->b_data;
6573 	struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6574 	struct ocfs2_xattr_block *new_xb =
6575 			(struct ocfs2_xattr_block *)new_blk_bh->b_data;
6576 	struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6577 	struct ocfs2_alloc_context *meta_ac;
6578 	struct ocfs2_xattr_value_buf vb = {
6579 		.vb_bh = new_blk_bh,
6580 		.vb_access = ocfs2_journal_access_xb,
6581 	};
6582 
6583 	ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6584 						  &credits, &meta_ac);
6585 	if (ret) {
6586 		mlog_errno(ret);
6587 		return ret;
6588 	}
6589 
6590 	/* One more credits in case we need to add xattr flags in new inode. */
6591 	handle = ocfs2_start_trans(osb, credits + 1);
6592 	if (IS_ERR(handle)) {
6593 		ret = PTR_ERR(handle);
6594 		mlog_errno(ret);
6595 		goto out;
6596 	}
6597 
6598 	if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6599 		ret = ocfs2_journal_access_di(handle,
6600 					      INODE_CACHE(args->new_inode),
6601 					      args->new_bh,
6602 					      OCFS2_JOURNAL_ACCESS_WRITE);
6603 		if (ret) {
6604 			mlog_errno(ret);
6605 			goto out_commit;
6606 		}
6607 	}
6608 
6609 	ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6610 				      new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6611 	if (ret) {
6612 		mlog_errno(ret);
6613 		goto out_commit;
6614 	}
6615 
6616 	memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6617 	       osb->sb->s_blocksize - header_off);
6618 
6619 	ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6620 					 new_blk_bh, new_xh, &vb, meta_ac,
6621 					 ocfs2_get_xattr_value_root, NULL);
6622 	if (ret) {
6623 		mlog_errno(ret);
6624 		goto out_commit;
6625 	}
6626 
6627 	ocfs2_journal_dirty(handle, new_blk_bh);
6628 
6629 	if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6630 		new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6631 		spin_lock(&new_oi->ip_lock);
6632 		new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6633 		new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6634 		spin_unlock(&new_oi->ip_lock);
6635 
6636 		ocfs2_journal_dirty(handle, args->new_bh);
6637 	}
6638 
6639 out_commit:
6640 	ocfs2_commit_trans(osb, handle);
6641 
6642 out:
6643 	ocfs2_free_alloc_context(meta_ac);
6644 	return ret;
6645 }
6646 
6647 struct ocfs2_reflink_xattr_tree_args {
6648 	struct ocfs2_xattr_reflink *reflink;
6649 	struct buffer_head *old_blk_bh;
6650 	struct buffer_head *new_blk_bh;
6651 	struct ocfs2_xattr_bucket *old_bucket;
6652 	struct ocfs2_xattr_bucket *new_bucket;
6653 };
6654 
6655 /*
6656  * NOTE:
6657  * We have to handle the case that both old bucket and new bucket
6658  * will call this function to get the right ret_bh.
6659  * So The caller must give us the right bh.
6660  */
6661 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6662 					struct buffer_head *bh,
6663 					struct ocfs2_xattr_header *xh,
6664 					int offset,
6665 					struct ocfs2_xattr_value_root **xv,
6666 					struct buffer_head **ret_bh,
6667 					void *para)
6668 {
6669 	struct ocfs2_reflink_xattr_tree_args *args =
6670 			(struct ocfs2_reflink_xattr_tree_args *)para;
6671 	struct ocfs2_xattr_bucket *bucket;
6672 
6673 	if (bh == args->old_bucket->bu_bhs[0])
6674 		bucket = args->old_bucket;
6675 	else
6676 		bucket = args->new_bucket;
6677 
6678 	return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6679 					       xv, ret_bh);
6680 }
6681 
6682 struct ocfs2_value_tree_metas {
6683 	int num_metas;
6684 	int credits;
6685 	int num_recs;
6686 };
6687 
6688 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6689 					struct buffer_head *bh,
6690 					struct ocfs2_xattr_header *xh,
6691 					int offset,
6692 					struct ocfs2_xattr_value_root **xv,
6693 					struct buffer_head **ret_bh,
6694 					void *para)
6695 {
6696 	struct ocfs2_xattr_bucket *bucket =
6697 				(struct ocfs2_xattr_bucket *)para;
6698 
6699 	return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6700 					       xv, ret_bh);
6701 }
6702 
6703 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6704 				      struct ocfs2_xattr_bucket *bucket,
6705 				      void *para)
6706 {
6707 	struct ocfs2_value_tree_metas *metas =
6708 			(struct ocfs2_value_tree_metas *)para;
6709 	struct ocfs2_xattr_header *xh =
6710 			(struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6711 
6712 	/* Add the credits for this bucket first. */
6713 	metas->credits += bucket->bu_blocks;
6714 	return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6715 					xh, &metas->num_metas,
6716 					&metas->credits, &metas->num_recs,
6717 					ocfs2_value_tree_metas_in_bucket,
6718 					bucket);
6719 }
6720 
6721 /*
6722  * Given a xattr extent rec starting from blkno and having len clusters,
6723  * iterate all the buckets calculate how much metadata we need for reflinking
6724  * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6725  */
6726 static int ocfs2_lock_reflink_xattr_rec_allocators(
6727 				struct ocfs2_reflink_xattr_tree_args *args,
6728 				struct ocfs2_extent_tree *xt_et,
6729 				u64 blkno, u32 len, int *credits,
6730 				struct ocfs2_alloc_context **meta_ac,
6731 				struct ocfs2_alloc_context **data_ac)
6732 {
6733 	int ret, num_free_extents;
6734 	struct ocfs2_value_tree_metas metas;
6735 	struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6736 	struct ocfs2_refcount_block *rb;
6737 
6738 	memset(&metas, 0, sizeof(metas));
6739 
6740 	ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6741 					  ocfs2_calc_value_tree_metas, &metas);
6742 	if (ret) {
6743 		mlog_errno(ret);
6744 		goto out;
6745 	}
6746 
6747 	*credits = metas.credits;
6748 
6749 	/*
6750 	 * Calculate we need for refcount tree change.
6751 	 *
6752 	 * We need to add/modify num_recs in refcount tree, so just calculate
6753 	 * an approximate number we need for refcount tree change.
6754 	 * Sometimes we need to split the tree, and after split,  half recs
6755 	 * will be moved to the new block, and a new block can only provide
6756 	 * half number of recs. So we multiple new blocks by 2.
6757 	 * In the end, we have to add credits for modifying the already
6758 	 * existed refcount block.
6759 	 */
6760 	rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6761 	metas.num_recs =
6762 		(metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6763 		 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6764 	metas.num_metas += metas.num_recs;
6765 	*credits += metas.num_recs +
6766 		    metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6767 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6768 		*credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6769 			    le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6770 	else
6771 		*credits += 1;
6772 
6773 	/* count in the xattr tree change. */
6774 	num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6775 	if (num_free_extents < 0) {
6776 		ret = num_free_extents;
6777 		mlog_errno(ret);
6778 		goto out;
6779 	}
6780 
6781 	if (num_free_extents < len)
6782 		metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6783 
6784 	*credits += ocfs2_calc_extend_credits(osb->sb,
6785 					      xt_et->et_root_el, len);
6786 
6787 	if (metas.num_metas) {
6788 		ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6789 							meta_ac);
6790 		if (ret) {
6791 			mlog_errno(ret);
6792 			goto out;
6793 		}
6794 	}
6795 
6796 	if (len) {
6797 		ret = ocfs2_reserve_clusters(osb, len, data_ac);
6798 		if (ret)
6799 			mlog_errno(ret);
6800 	}
6801 out:
6802 	if (ret) {
6803 		if (*meta_ac) {
6804 			ocfs2_free_alloc_context(*meta_ac);
6805 			*meta_ac = NULL;
6806 		}
6807 	}
6808 
6809 	return ret;
6810 }
6811 
6812 static int ocfs2_reflink_xattr_bucket(handle_t *handle,
6813 				u64 blkno, u64 new_blkno, u32 clusters,
6814 				u32 *cpos, int num_buckets,
6815 				struct ocfs2_alloc_context *meta_ac,
6816 				struct ocfs2_alloc_context *data_ac,
6817 				struct ocfs2_reflink_xattr_tree_args *args)
6818 {
6819 	int i, j, ret = 0;
6820 	struct super_block *sb = args->reflink->old_inode->i_sb;
6821 	int bpb = args->old_bucket->bu_blocks;
6822 	struct ocfs2_xattr_value_buf vb = {
6823 		.vb_access = ocfs2_journal_access,
6824 	};
6825 
6826 	for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6827 		ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6828 		if (ret) {
6829 			mlog_errno(ret);
6830 			break;
6831 		}
6832 
6833 		ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6834 		if (ret) {
6835 			mlog_errno(ret);
6836 			break;
6837 		}
6838 
6839 		ret = ocfs2_xattr_bucket_journal_access(handle,
6840 						args->new_bucket,
6841 						OCFS2_JOURNAL_ACCESS_CREATE);
6842 		if (ret) {
6843 			mlog_errno(ret);
6844 			break;
6845 		}
6846 
6847 		for (j = 0; j < bpb; j++)
6848 			memcpy(bucket_block(args->new_bucket, j),
6849 			       bucket_block(args->old_bucket, j),
6850 			       sb->s_blocksize);
6851 
6852 		/*
6853 		 * Record the start cpos so that we can use it to initialize
6854 		 * our xattr tree we also set the xh_num_bucket for the new
6855 		 * bucket.
6856 		 */
6857 		if (i == 0) {
6858 			*cpos = le32_to_cpu(bucket_xh(args->new_bucket)->
6859 					    xh_entries[0].xe_name_hash);
6860 			bucket_xh(args->new_bucket)->xh_num_buckets =
6861 				cpu_to_le16(num_buckets);
6862 		}
6863 
6864 		ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6865 
6866 		ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6867 					args->old_bucket->bu_bhs[0],
6868 					bucket_xh(args->old_bucket),
6869 					args->new_bucket->bu_bhs[0],
6870 					bucket_xh(args->new_bucket),
6871 					&vb, meta_ac,
6872 					ocfs2_get_reflink_xattr_value_root,
6873 					args);
6874 		if (ret) {
6875 			mlog_errno(ret);
6876 			break;
6877 		}
6878 
6879 		/*
6880 		 * Re-access and dirty the bucket to calculate metaecc.
6881 		 * Because we may extend the transaction in reflink_xattr_header
6882 		 * which will let the already accessed block gone.
6883 		 */
6884 		ret = ocfs2_xattr_bucket_journal_access(handle,
6885 						args->new_bucket,
6886 						OCFS2_JOURNAL_ACCESS_WRITE);
6887 		if (ret) {
6888 			mlog_errno(ret);
6889 			break;
6890 		}
6891 
6892 		ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6893 
6894 		ocfs2_xattr_bucket_relse(args->old_bucket);
6895 		ocfs2_xattr_bucket_relse(args->new_bucket);
6896 	}
6897 
6898 	ocfs2_xattr_bucket_relse(args->old_bucket);
6899 	ocfs2_xattr_bucket_relse(args->new_bucket);
6900 	return ret;
6901 }
6902 
6903 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6904 				struct inode *inode,
6905 				struct ocfs2_reflink_xattr_tree_args *args,
6906 				struct ocfs2_extent_tree *et,
6907 				struct ocfs2_alloc_context *meta_ac,
6908 				struct ocfs2_alloc_context *data_ac,
6909 				u64 blkno, u32 cpos, u32 len)
6910 {
6911 	int ret, first_inserted = 0;
6912 	u32 p_cluster, num_clusters, reflink_cpos = 0;
6913 	u64 new_blkno;
6914 	unsigned int num_buckets, reflink_buckets;
6915 	unsigned int bpc =
6916 		ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
6917 
6918 	ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6919 	if (ret) {
6920 		mlog_errno(ret);
6921 		goto out;
6922 	}
6923 	num_buckets = le16_to_cpu(bucket_xh(args->old_bucket)->xh_num_buckets);
6924 	ocfs2_xattr_bucket_relse(args->old_bucket);
6925 
6926 	while (len && num_buckets) {
6927 		ret = ocfs2_claim_clusters(handle, data_ac,
6928 					   1, &p_cluster, &num_clusters);
6929 		if (ret) {
6930 			mlog_errno(ret);
6931 			goto out;
6932 		}
6933 
6934 		new_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
6935 		reflink_buckets = min(num_buckets, bpc * num_clusters);
6936 
6937 		ret = ocfs2_reflink_xattr_bucket(handle, blkno,
6938 						 new_blkno, num_clusters,
6939 						 &reflink_cpos, reflink_buckets,
6940 						 meta_ac, data_ac, args);
6941 		if (ret) {
6942 			mlog_errno(ret);
6943 			goto out;
6944 		}
6945 
6946 		/*
6947 		 * For the 1st allocated cluster, we make it use the same cpos
6948 		 * so that the xattr tree looks the same as the original one
6949 		 * in the most case.
6950 		 */
6951 		if (!first_inserted) {
6952 			reflink_cpos = cpos;
6953 			first_inserted = 1;
6954 		}
6955 		ret = ocfs2_insert_extent(handle, et, reflink_cpos, new_blkno,
6956 					  num_clusters, 0, meta_ac);
6957 		if (ret)
6958 			mlog_errno(ret);
6959 
6960 		trace_ocfs2_reflink_xattr_buckets((unsigned long long)new_blkno,
6961 						  num_clusters, reflink_cpos);
6962 
6963 		len -= num_clusters;
6964 		blkno += ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
6965 		num_buckets -= reflink_buckets;
6966 	}
6967 out:
6968 	return ret;
6969 }
6970 
6971 /*
6972  * Create the same xattr extent record in the new inode's xattr tree.
6973  */
6974 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6975 				   struct buffer_head *root_bh,
6976 				   u64 blkno,
6977 				   u32 cpos,
6978 				   u32 len,
6979 				   void *para)
6980 {
6981 	int ret, credits = 0;
6982 	handle_t *handle;
6983 	struct ocfs2_reflink_xattr_tree_args *args =
6984 			(struct ocfs2_reflink_xattr_tree_args *)para;
6985 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6986 	struct ocfs2_alloc_context *meta_ac = NULL;
6987 	struct ocfs2_alloc_context *data_ac = NULL;
6988 	struct ocfs2_extent_tree et;
6989 
6990 	trace_ocfs2_reflink_xattr_rec((unsigned long long)blkno, len);
6991 
6992 	ocfs2_init_xattr_tree_extent_tree(&et,
6993 					  INODE_CACHE(args->reflink->new_inode),
6994 					  args->new_blk_bh);
6995 
6996 	ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6997 						      len, &credits,
6998 						      &meta_ac, &data_ac);
6999 	if (ret) {
7000 		mlog_errno(ret);
7001 		goto out;
7002 	}
7003 
7004 	handle = ocfs2_start_trans(osb, credits);
7005 	if (IS_ERR(handle)) {
7006 		ret = PTR_ERR(handle);
7007 		mlog_errno(ret);
7008 		goto out;
7009 	}
7010 
7011 	ret = ocfs2_reflink_xattr_buckets(handle, inode, args, &et,
7012 					  meta_ac, data_ac,
7013 					  blkno, cpos, len);
7014 	if (ret)
7015 		mlog_errno(ret);
7016 
7017 	ocfs2_commit_trans(osb, handle);
7018 
7019 out:
7020 	if (meta_ac)
7021 		ocfs2_free_alloc_context(meta_ac);
7022 	if (data_ac)
7023 		ocfs2_free_alloc_context(data_ac);
7024 	return ret;
7025 }
7026 
7027 /*
7028  * Create reflinked xattr buckets.
7029  * We will add bucket one by one, and refcount all the xattrs in the bucket
7030  * if they are stored outside.
7031  */
7032 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7033 				    struct buffer_head *blk_bh,
7034 				    struct buffer_head *new_blk_bh)
7035 {
7036 	int ret;
7037 	struct ocfs2_reflink_xattr_tree_args para;
7038 
7039 	memset(&para, 0, sizeof(para));
7040 	para.reflink = args;
7041 	para.old_blk_bh = blk_bh;
7042 	para.new_blk_bh = new_blk_bh;
7043 
7044 	para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7045 	if (!para.old_bucket) {
7046 		mlog_errno(-ENOMEM);
7047 		return -ENOMEM;
7048 	}
7049 
7050 	para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7051 	if (!para.new_bucket) {
7052 		ret = -ENOMEM;
7053 		mlog_errno(ret);
7054 		goto out;
7055 	}
7056 
7057 	ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7058 					      ocfs2_reflink_xattr_rec,
7059 					      &para);
7060 	if (ret)
7061 		mlog_errno(ret);
7062 
7063 out:
7064 	ocfs2_xattr_bucket_free(para.old_bucket);
7065 	ocfs2_xattr_bucket_free(para.new_bucket);
7066 	return ret;
7067 }
7068 
7069 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7070 					struct buffer_head *blk_bh)
7071 {
7072 	int ret, indexed = 0;
7073 	struct buffer_head *new_blk_bh = NULL;
7074 	struct ocfs2_xattr_block *xb =
7075 			(struct ocfs2_xattr_block *)blk_bh->b_data;
7076 
7077 
7078 	if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7079 		indexed = 1;
7080 
7081 	ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7082 					     &new_blk_bh, indexed);
7083 	if (ret) {
7084 		mlog_errno(ret);
7085 		goto out;
7086 	}
7087 
7088 	if (!indexed)
7089 		ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7090 	else
7091 		ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7092 	if (ret)
7093 		mlog_errno(ret);
7094 
7095 out:
7096 	brelse(new_blk_bh);
7097 	return ret;
7098 }
7099 
7100 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7101 {
7102 	int type = ocfs2_xattr_get_type(xe);
7103 
7104 	return type != OCFS2_XATTR_INDEX_SECURITY &&
7105 	       type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7106 	       type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7107 }
7108 
7109 int ocfs2_reflink_xattrs(struct inode *old_inode,
7110 			 struct buffer_head *old_bh,
7111 			 struct inode *new_inode,
7112 			 struct buffer_head *new_bh,
7113 			 bool preserve_security)
7114 {
7115 	int ret;
7116 	struct ocfs2_xattr_reflink args;
7117 	struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7118 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7119 	struct buffer_head *blk_bh = NULL;
7120 	struct ocfs2_cached_dealloc_ctxt dealloc;
7121 	struct ocfs2_refcount_tree *ref_tree;
7122 	struct buffer_head *ref_root_bh = NULL;
7123 
7124 	ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7125 				       le64_to_cpu(di->i_refcount_loc),
7126 				       1, &ref_tree, &ref_root_bh);
7127 	if (ret) {
7128 		mlog_errno(ret);
7129 		goto out;
7130 	}
7131 
7132 	ocfs2_init_dealloc_ctxt(&dealloc);
7133 
7134 	args.old_inode = old_inode;
7135 	args.new_inode = new_inode;
7136 	args.old_bh = old_bh;
7137 	args.new_bh = new_bh;
7138 	args.ref_ci = &ref_tree->rf_ci;
7139 	args.ref_root_bh = ref_root_bh;
7140 	args.dealloc = &dealloc;
7141 	if (preserve_security)
7142 		args.xattr_reflinked = NULL;
7143 	else
7144 		args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7145 
7146 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7147 		ret = ocfs2_reflink_xattr_inline(&args);
7148 		if (ret) {
7149 			mlog_errno(ret);
7150 			goto out_unlock;
7151 		}
7152 	}
7153 
7154 	if (!di->i_xattr_loc)
7155 		goto out_unlock;
7156 
7157 	ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7158 				     &blk_bh);
7159 	if (ret < 0) {
7160 		mlog_errno(ret);
7161 		goto out_unlock;
7162 	}
7163 
7164 	ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7165 	if (ret)
7166 		mlog_errno(ret);
7167 
7168 	brelse(blk_bh);
7169 
7170 out_unlock:
7171 	ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7172 				   ref_tree, 1);
7173 	brelse(ref_root_bh);
7174 
7175 	if (ocfs2_dealloc_has_cluster(&dealloc)) {
7176 		ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7177 		ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7178 	}
7179 
7180 out:
7181 	return ret;
7182 }
7183 
7184 /*
7185  * Initialize security and acl for a already created inode.
7186  * Used for reflink a non-preserve-security file.
7187  *
7188  * It uses common api like ocfs2_xattr_set, so the caller
7189  * must not hold any lock expect i_mutex.
7190  */
7191 int ocfs2_init_security_and_acl(struct inode *dir,
7192 				struct inode *inode,
7193 				const struct qstr *qstr)
7194 {
7195 	int ret = 0;
7196 	struct buffer_head *dir_bh = NULL;
7197 
7198 	ret = ocfs2_init_security_get(inode, dir, qstr, NULL);
7199 	if (ret) {
7200 		mlog_errno(ret);
7201 		goto leave;
7202 	}
7203 
7204 	ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7205 	if (ret) {
7206 		mlog_errno(ret);
7207 		goto leave;
7208 	}
7209 
7210 	ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7211 	if (ret)
7212 		mlog_errno(ret);
7213 
7214 	ocfs2_inode_unlock(dir, 0);
7215 	brelse(dir_bh);
7216 leave:
7217 	return ret;
7218 }
7219 /*
7220  * 'security' attributes support
7221  */
7222 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7223 					size_t list_size, const char *name,
7224 					size_t name_len, int type)
7225 {
7226 	const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7227 	const size_t total_len = prefix_len + name_len + 1;
7228 
7229 	if (list && total_len <= list_size) {
7230 		memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7231 		memcpy(list + prefix_len, name, name_len);
7232 		list[prefix_len + name_len] = '\0';
7233 	}
7234 	return total_len;
7235 }
7236 
7237 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7238 				    void *buffer, size_t size, int type)
7239 {
7240 	if (strcmp(name, "") == 0)
7241 		return -EINVAL;
7242 	return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7243 			       name, buffer, size);
7244 }
7245 
7246 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7247 		const void *value, size_t size, int flags, int type)
7248 {
7249 	if (strcmp(name, "") == 0)
7250 		return -EINVAL;
7251 
7252 	return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7253 			       name, value, size, flags);
7254 }
7255 
7256 int ocfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
7257 		     void *fs_info)
7258 {
7259 	const struct xattr *xattr;
7260 	int err = 0;
7261 
7262 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
7263 		err = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7264 				      xattr->name, xattr->value,
7265 				      xattr->value_len, XATTR_CREATE);
7266 		if (err)
7267 			break;
7268 	}
7269 	return err;
7270 }
7271 
7272 int ocfs2_init_security_get(struct inode *inode,
7273 			    struct inode *dir,
7274 			    const struct qstr *qstr,
7275 			    struct ocfs2_security_xattr_info *si)
7276 {
7277 	/* check whether ocfs2 support feature xattr */
7278 	if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7279 		return -EOPNOTSUPP;
7280 	if (si)
7281 		return security_old_inode_init_security(inode, dir, qstr,
7282 							&si->name, &si->value,
7283 							&si->value_len);
7284 
7285 	return security_inode_init_security(inode, dir, qstr,
7286 					    &ocfs2_initxattrs, NULL);
7287 }
7288 
7289 int ocfs2_init_security_set(handle_t *handle,
7290 			    struct inode *inode,
7291 			    struct buffer_head *di_bh,
7292 			    struct ocfs2_security_xattr_info *si,
7293 			    struct ocfs2_alloc_context *xattr_ac,
7294 			    struct ocfs2_alloc_context *data_ac)
7295 {
7296 	return ocfs2_xattr_set_handle(handle, inode, di_bh,
7297 				     OCFS2_XATTR_INDEX_SECURITY,
7298 				     si->name, si->value, si->value_len, 0,
7299 				     xattr_ac, data_ac);
7300 }
7301 
7302 const struct xattr_handler ocfs2_xattr_security_handler = {
7303 	.prefix	= XATTR_SECURITY_PREFIX,
7304 	.list	= ocfs2_xattr_security_list,
7305 	.get	= ocfs2_xattr_security_get,
7306 	.set	= ocfs2_xattr_security_set,
7307 };
7308 
7309 /*
7310  * 'trusted' attributes support
7311  */
7312 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7313 				       size_t list_size, const char *name,
7314 				       size_t name_len, int type)
7315 {
7316 	const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7317 	const size_t total_len = prefix_len + name_len + 1;
7318 
7319 	if (list && total_len <= list_size) {
7320 		memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7321 		memcpy(list + prefix_len, name, name_len);
7322 		list[prefix_len + name_len] = '\0';
7323 	}
7324 	return total_len;
7325 }
7326 
7327 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7328 		void *buffer, size_t size, int type)
7329 {
7330 	if (strcmp(name, "") == 0)
7331 		return -EINVAL;
7332 	return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7333 			       name, buffer, size);
7334 }
7335 
7336 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7337 		const void *value, size_t size, int flags, int type)
7338 {
7339 	if (strcmp(name, "") == 0)
7340 		return -EINVAL;
7341 
7342 	return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7343 			       name, value, size, flags);
7344 }
7345 
7346 const struct xattr_handler ocfs2_xattr_trusted_handler = {
7347 	.prefix	= XATTR_TRUSTED_PREFIX,
7348 	.list	= ocfs2_xattr_trusted_list,
7349 	.get	= ocfs2_xattr_trusted_get,
7350 	.set	= ocfs2_xattr_trusted_set,
7351 };
7352 
7353 /*
7354  * 'user' attributes support
7355  */
7356 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7357 				    size_t list_size, const char *name,
7358 				    size_t name_len, int type)
7359 {
7360 	const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7361 	const size_t total_len = prefix_len + name_len + 1;
7362 	struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7363 
7364 	if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7365 		return 0;
7366 
7367 	if (list && total_len <= list_size) {
7368 		memcpy(list, XATTR_USER_PREFIX, prefix_len);
7369 		memcpy(list + prefix_len, name, name_len);
7370 		list[prefix_len + name_len] = '\0';
7371 	}
7372 	return total_len;
7373 }
7374 
7375 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7376 		void *buffer, size_t size, int type)
7377 {
7378 	struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7379 
7380 	if (strcmp(name, "") == 0)
7381 		return -EINVAL;
7382 	if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7383 		return -EOPNOTSUPP;
7384 	return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7385 			       buffer, size);
7386 }
7387 
7388 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7389 		const void *value, size_t size, int flags, int type)
7390 {
7391 	struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7392 
7393 	if (strcmp(name, "") == 0)
7394 		return -EINVAL;
7395 	if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7396 		return -EOPNOTSUPP;
7397 
7398 	return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7399 			       name, value, size, flags);
7400 }
7401 
7402 const struct xattr_handler ocfs2_xattr_user_handler = {
7403 	.prefix	= XATTR_USER_PREFIX,
7404 	.list	= ocfs2_xattr_user_list,
7405 	.get	= ocfs2_xattr_user_get,
7406 	.set	= ocfs2_xattr_user_set,
7407 };
7408