xref: /openbmc/linux/fs/ocfs2/xattr.c (revision 06b240d8)
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * xattr.c
5  *
6  * Copyright (C) 2008 Oracle.  All rights reserved.
7  *
8  * CREDITS:
9  * Lots of code in this file is taken from ext3.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
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  * You should have received a copy of the GNU General Public
22  * License along with this program; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 021110-1307, USA.
25  */
26 
27 #include <linux/capability.h>
28 #include <linux/fs.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/highmem.h>
32 #include <linux/pagemap.h>
33 #include <linux/uio.h>
34 #include <linux/sched.h>
35 #include <linux/splice.h>
36 #include <linux/mount.h>
37 #include <linux/writeback.h>
38 #include <linux/falloc.h>
39 #include <linux/sort.h>
40 
41 #define MLOG_MASK_PREFIX ML_XATTR
42 #include <cluster/masklog.h>
43 
44 #include "ocfs2.h"
45 #include "alloc.h"
46 #include "dlmglue.h"
47 #include "file.h"
48 #include "symlink.h"
49 #include "sysfile.h"
50 #include "inode.h"
51 #include "journal.h"
52 #include "ocfs2_fs.h"
53 #include "suballoc.h"
54 #include "uptodate.h"
55 #include "buffer_head_io.h"
56 #include "super.h"
57 #include "xattr.h"
58 
59 
60 struct ocfs2_xattr_def_value_root {
61 	struct ocfs2_xattr_value_root	xv;
62 	struct ocfs2_extent_rec		er;
63 };
64 
65 struct ocfs2_xattr_bucket {
66 	struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
67 	struct ocfs2_xattr_header *xh;
68 };
69 
70 #define OCFS2_XATTR_ROOT_SIZE	(sizeof(struct ocfs2_xattr_def_value_root))
71 #define OCFS2_XATTR_INLINE_SIZE	80
72 
73 static struct ocfs2_xattr_def_value_root def_xv = {
74 	.xv.xr_list.l_count = cpu_to_le16(1),
75 };
76 
77 struct xattr_handler *ocfs2_xattr_handlers[] = {
78 	&ocfs2_xattr_user_handler,
79 	&ocfs2_xattr_trusted_handler,
80 	NULL
81 };
82 
83 static struct xattr_handler *ocfs2_xattr_handler_map[] = {
84 	[OCFS2_XATTR_INDEX_USER]	= &ocfs2_xattr_user_handler,
85 	[OCFS2_XATTR_INDEX_TRUSTED]	= &ocfs2_xattr_trusted_handler,
86 };
87 
88 struct ocfs2_xattr_info {
89 	int name_index;
90 	const char *name;
91 	const void *value;
92 	size_t value_len;
93 };
94 
95 struct ocfs2_xattr_search {
96 	struct buffer_head *inode_bh;
97 	/*
98 	 * xattr_bh point to the block buffer head which has extended attribute
99 	 * when extended attribute in inode, xattr_bh is equal to inode_bh.
100 	 */
101 	struct buffer_head *xattr_bh;
102 	struct ocfs2_xattr_header *header;
103 	struct ocfs2_xattr_bucket bucket;
104 	void *base;
105 	void *end;
106 	struct ocfs2_xattr_entry *here;
107 	int not_found;
108 };
109 
110 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
111 					     struct ocfs2_xattr_header *xh,
112 					     int index,
113 					     int *block_off,
114 					     int *new_offset);
115 
116 static int ocfs2_xattr_index_block_find(struct inode *inode,
117 					struct buffer_head *root_bh,
118 					int name_index,
119 					const char *name,
120 					struct ocfs2_xattr_search *xs);
121 
122 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
123 					struct ocfs2_xattr_tree_root *xt,
124 					char *buffer,
125 					size_t buffer_size);
126 
127 static int ocfs2_xattr_create_index_block(struct inode *inode,
128 					  struct ocfs2_xattr_search *xs);
129 
130 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
131 					     struct ocfs2_xattr_info *xi,
132 					     struct ocfs2_xattr_search *xs);
133 
134 static int ocfs2_delete_xattr_index_block(struct inode *inode,
135 					  struct buffer_head *xb_bh);
136 
137 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
138 {
139 	struct xattr_handler *handler = NULL;
140 
141 	if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
142 		handler = ocfs2_xattr_handler_map[name_index];
143 
144 	return handler;
145 }
146 
147 static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
148 					char *prefix,
149 					int prefix_len,
150 					char *name,
151 					int name_len)
152 {
153 	/* Get hash value of uuid from super block */
154 	u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
155 	int i;
156 
157 	/* hash extended attribute prefix */
158 	for (i = 0; i < prefix_len; i++) {
159 		hash = (hash << OCFS2_HASH_SHIFT) ^
160 		       (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
161 		       *prefix++;
162 	}
163 	/* hash extended attribute name */
164 	for (i = 0; i < name_len; i++) {
165 		hash = (hash << OCFS2_HASH_SHIFT) ^
166 		       (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
167 		       *name++;
168 	}
169 
170 	return hash;
171 }
172 
173 /*
174  * ocfs2_xattr_hash_entry()
175  *
176  * Compute the hash of an extended attribute.
177  */
178 static void ocfs2_xattr_hash_entry(struct inode *inode,
179 				   struct ocfs2_xattr_header *header,
180 				   struct ocfs2_xattr_entry *entry)
181 {
182 	u32 hash = 0;
183 	struct xattr_handler *handler =
184 			ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
185 	char *prefix = handler->prefix;
186 	char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
187 	int prefix_len = strlen(handler->prefix);
188 
189 	hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
190 				     entry->xe_name_len);
191 	entry->xe_name_hash = cpu_to_le32(hash);
192 
193 	return;
194 }
195 
196 static int ocfs2_xattr_extend_allocation(struct inode *inode,
197 					 u32 clusters_to_add,
198 					 struct buffer_head *xattr_bh,
199 					 struct ocfs2_xattr_value_root *xv)
200 {
201 	int status = 0;
202 	int restart_func = 0;
203 	int credits = 0;
204 	handle_t *handle = NULL;
205 	struct ocfs2_alloc_context *data_ac = NULL;
206 	struct ocfs2_alloc_context *meta_ac = NULL;
207 	enum ocfs2_alloc_restarted why;
208 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
209 	u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
210 	struct ocfs2_extent_tree et;
211 
212 	mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
213 
214 	ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
215 
216 restart_all:
217 
218 	status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
219 				       &data_ac, &meta_ac);
220 	if (status) {
221 		mlog_errno(status);
222 		goto leave;
223 	}
224 
225 	credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
226 					    clusters_to_add);
227 	handle = ocfs2_start_trans(osb, credits);
228 	if (IS_ERR(handle)) {
229 		status = PTR_ERR(handle);
230 		handle = NULL;
231 		mlog_errno(status);
232 		goto leave;
233 	}
234 
235 restarted_transaction:
236 	status = ocfs2_journal_access(handle, inode, xattr_bh,
237 				      OCFS2_JOURNAL_ACCESS_WRITE);
238 	if (status < 0) {
239 		mlog_errno(status);
240 		goto leave;
241 	}
242 
243 	prev_clusters = le32_to_cpu(xv->xr_clusters);
244 	status = ocfs2_add_clusters_in_btree(osb,
245 					     inode,
246 					     &logical_start,
247 					     clusters_to_add,
248 					     0,
249 					     &et,
250 					     handle,
251 					     data_ac,
252 					     meta_ac,
253 					     &why);
254 	if ((status < 0) && (status != -EAGAIN)) {
255 		if (status != -ENOSPC)
256 			mlog_errno(status);
257 		goto leave;
258 	}
259 
260 	status = ocfs2_journal_dirty(handle, xattr_bh);
261 	if (status < 0) {
262 		mlog_errno(status);
263 		goto leave;
264 	}
265 
266 	clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
267 
268 	if (why != RESTART_NONE && clusters_to_add) {
269 		if (why == RESTART_META) {
270 			mlog(0, "restarting function.\n");
271 			restart_func = 1;
272 		} else {
273 			BUG_ON(why != RESTART_TRANS);
274 
275 			mlog(0, "restarting transaction.\n");
276 			/* TODO: This can be more intelligent. */
277 			credits = ocfs2_calc_extend_credits(osb->sb,
278 							    et.et_root_el,
279 							    clusters_to_add);
280 			status = ocfs2_extend_trans(handle, credits);
281 			if (status < 0) {
282 				/* handle still has to be committed at
283 				 * this point. */
284 				status = -ENOMEM;
285 				mlog_errno(status);
286 				goto leave;
287 			}
288 			goto restarted_transaction;
289 		}
290 	}
291 
292 leave:
293 	if (handle) {
294 		ocfs2_commit_trans(osb, handle);
295 		handle = NULL;
296 	}
297 	if (data_ac) {
298 		ocfs2_free_alloc_context(data_ac);
299 		data_ac = NULL;
300 	}
301 	if (meta_ac) {
302 		ocfs2_free_alloc_context(meta_ac);
303 		meta_ac = NULL;
304 	}
305 	if ((!status) && restart_func) {
306 		restart_func = 0;
307 		goto restart_all;
308 	}
309 
310 	return status;
311 }
312 
313 static int __ocfs2_remove_xattr_range(struct inode *inode,
314 				      struct buffer_head *root_bh,
315 				      struct ocfs2_xattr_value_root *xv,
316 				      u32 cpos, u32 phys_cpos, u32 len,
317 				      struct ocfs2_cached_dealloc_ctxt *dealloc)
318 {
319 	int ret;
320 	u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
321 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
322 	struct inode *tl_inode = osb->osb_tl_inode;
323 	handle_t *handle;
324 	struct ocfs2_alloc_context *meta_ac = NULL;
325 	struct ocfs2_extent_tree et;
326 
327 	ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
328 
329 	ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
330 	if (ret) {
331 		mlog_errno(ret);
332 		return ret;
333 	}
334 
335 	mutex_lock(&tl_inode->i_mutex);
336 
337 	if (ocfs2_truncate_log_needs_flush(osb)) {
338 		ret = __ocfs2_flush_truncate_log(osb);
339 		if (ret < 0) {
340 			mlog_errno(ret);
341 			goto out;
342 		}
343 	}
344 
345 	handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
346 	if (IS_ERR(handle)) {
347 		ret = PTR_ERR(handle);
348 		mlog_errno(ret);
349 		goto out;
350 	}
351 
352 	ret = ocfs2_journal_access(handle, inode, root_bh,
353 				   OCFS2_JOURNAL_ACCESS_WRITE);
354 	if (ret) {
355 		mlog_errno(ret);
356 		goto out_commit;
357 	}
358 
359 	ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
360 				  dealloc);
361 	if (ret) {
362 		mlog_errno(ret);
363 		goto out_commit;
364 	}
365 
366 	le32_add_cpu(&xv->xr_clusters, -len);
367 
368 	ret = ocfs2_journal_dirty(handle, root_bh);
369 	if (ret) {
370 		mlog_errno(ret);
371 		goto out_commit;
372 	}
373 
374 	ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
375 	if (ret)
376 		mlog_errno(ret);
377 
378 out_commit:
379 	ocfs2_commit_trans(osb, handle);
380 out:
381 	mutex_unlock(&tl_inode->i_mutex);
382 
383 	if (meta_ac)
384 		ocfs2_free_alloc_context(meta_ac);
385 
386 	return ret;
387 }
388 
389 static int ocfs2_xattr_shrink_size(struct inode *inode,
390 				   u32 old_clusters,
391 				   u32 new_clusters,
392 				   struct buffer_head *root_bh,
393 				   struct ocfs2_xattr_value_root *xv)
394 {
395 	int ret = 0;
396 	u32 trunc_len, cpos, phys_cpos, alloc_size;
397 	u64 block;
398 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
399 	struct ocfs2_cached_dealloc_ctxt dealloc;
400 
401 	ocfs2_init_dealloc_ctxt(&dealloc);
402 
403 	if (old_clusters <= new_clusters)
404 		return 0;
405 
406 	cpos = new_clusters;
407 	trunc_len = old_clusters - new_clusters;
408 	while (trunc_len) {
409 		ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
410 					       &alloc_size, &xv->xr_list);
411 		if (ret) {
412 			mlog_errno(ret);
413 			goto out;
414 		}
415 
416 		if (alloc_size > trunc_len)
417 			alloc_size = trunc_len;
418 
419 		ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
420 						 phys_cpos, alloc_size,
421 						 &dealloc);
422 		if (ret) {
423 			mlog_errno(ret);
424 			goto out;
425 		}
426 
427 		block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
428 		ocfs2_remove_xattr_clusters_from_cache(inode, block,
429 						       alloc_size);
430 		cpos += alloc_size;
431 		trunc_len -= alloc_size;
432 	}
433 
434 out:
435 	ocfs2_schedule_truncate_log_flush(osb, 1);
436 	ocfs2_run_deallocs(osb, &dealloc);
437 
438 	return ret;
439 }
440 
441 static int ocfs2_xattr_value_truncate(struct inode *inode,
442 				      struct buffer_head *root_bh,
443 				      struct ocfs2_xattr_value_root *xv,
444 				      int len)
445 {
446 	int ret;
447 	u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
448 	u32 old_clusters = le32_to_cpu(xv->xr_clusters);
449 
450 	if (new_clusters == old_clusters)
451 		return 0;
452 
453 	if (new_clusters > old_clusters)
454 		ret = ocfs2_xattr_extend_allocation(inode,
455 						    new_clusters - old_clusters,
456 						    root_bh, xv);
457 	else
458 		ret = ocfs2_xattr_shrink_size(inode,
459 					      old_clusters, new_clusters,
460 					      root_bh, xv);
461 
462 	return ret;
463 }
464 
465 static int ocfs2_xattr_list_entries(struct inode *inode,
466 				    struct ocfs2_xattr_header *header,
467 				    char *buffer, size_t buffer_size)
468 {
469 	size_t rest = buffer_size;
470 	int i;
471 
472 	for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
473 		struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
474 		struct xattr_handler *handler =
475 			ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
476 
477 		if (handler) {
478 			size_t size = handler->list(inode, buffer, rest,
479 					((char *)header +
480 					le16_to_cpu(entry->xe_name_offset)),
481 					entry->xe_name_len);
482 			if (buffer) {
483 				if (size > rest)
484 					return -ERANGE;
485 				buffer += size;
486 			}
487 			rest -= size;
488 		}
489 	}
490 
491 	return buffer_size - rest;
492 }
493 
494 static int ocfs2_xattr_ibody_list(struct inode *inode,
495 				  struct ocfs2_dinode *di,
496 				  char *buffer,
497 				  size_t buffer_size)
498 {
499 	struct ocfs2_xattr_header *header = NULL;
500 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
501 	int ret = 0;
502 
503 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
504 		return ret;
505 
506 	header = (struct ocfs2_xattr_header *)
507 		 ((void *)di + inode->i_sb->s_blocksize -
508 		 le16_to_cpu(di->i_xattr_inline_size));
509 
510 	ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
511 
512 	return ret;
513 }
514 
515 static int ocfs2_xattr_block_list(struct inode *inode,
516 				  struct ocfs2_dinode *di,
517 				  char *buffer,
518 				  size_t buffer_size)
519 {
520 	struct buffer_head *blk_bh = NULL;
521 	struct ocfs2_xattr_block *xb;
522 	int ret = 0;
523 
524 	if (!di->i_xattr_loc)
525 		return ret;
526 
527 	ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
528 			       le64_to_cpu(di->i_xattr_loc),
529 			       &blk_bh, OCFS2_BH_CACHED, inode);
530 	if (ret < 0) {
531 		mlog_errno(ret);
532 		return ret;
533 	}
534 	/*Verify the signature of xattr block*/
535 	if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
536 		   strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
537 		ret = -EFAULT;
538 		goto cleanup;
539 	}
540 
541 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
542 
543 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
544 		struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
545 		ret = ocfs2_xattr_list_entries(inode, header,
546 					       buffer, buffer_size);
547 	} else {
548 		struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
549 		ret = ocfs2_xattr_tree_list_index_block(inode, xt,
550 						   buffer, buffer_size);
551 	}
552 cleanup:
553 	brelse(blk_bh);
554 
555 	return ret;
556 }
557 
558 ssize_t ocfs2_listxattr(struct dentry *dentry,
559 			char *buffer,
560 			size_t size)
561 {
562 	int ret = 0, i_ret = 0, b_ret = 0;
563 	struct buffer_head *di_bh = NULL;
564 	struct ocfs2_dinode *di = NULL;
565 	struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
566 
567 	if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
568 		return -EOPNOTSUPP;
569 
570 	if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
571 		return ret;
572 
573 	ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
574 	if (ret < 0) {
575 		mlog_errno(ret);
576 		return ret;
577 	}
578 
579 	di = (struct ocfs2_dinode *)di_bh->b_data;
580 
581 	down_read(&oi->ip_xattr_sem);
582 	i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
583 	if (i_ret < 0)
584 		b_ret = 0;
585 	else {
586 		if (buffer) {
587 			buffer += i_ret;
588 			size -= i_ret;
589 		}
590 		b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
591 					       buffer, size);
592 		if (b_ret < 0)
593 			i_ret = 0;
594 	}
595 	up_read(&oi->ip_xattr_sem);
596 	ocfs2_inode_unlock(dentry->d_inode, 0);
597 
598 	brelse(di_bh);
599 
600 	return i_ret + b_ret;
601 }
602 
603 static int ocfs2_xattr_find_entry(int name_index,
604 				  const char *name,
605 				  struct ocfs2_xattr_search *xs)
606 {
607 	struct ocfs2_xattr_entry *entry;
608 	size_t name_len;
609 	int i, cmp = 1;
610 
611 	if (name == NULL)
612 		return -EINVAL;
613 
614 	name_len = strlen(name);
615 	entry = xs->here;
616 	for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
617 		cmp = name_index - ocfs2_xattr_get_type(entry);
618 		if (!cmp)
619 			cmp = name_len - entry->xe_name_len;
620 		if (!cmp)
621 			cmp = memcmp(name, (xs->base +
622 				     le16_to_cpu(entry->xe_name_offset)),
623 				     name_len);
624 		if (cmp == 0)
625 			break;
626 		entry += 1;
627 	}
628 	xs->here = entry;
629 
630 	return cmp ? -ENODATA : 0;
631 }
632 
633 static int ocfs2_xattr_get_value_outside(struct inode *inode,
634 					 struct ocfs2_xattr_value_root *xv,
635 					 void *buffer,
636 					 size_t len)
637 {
638 	u32 cpos, p_cluster, num_clusters, bpc, clusters;
639 	u64 blkno;
640 	int i, ret = 0;
641 	size_t cplen, blocksize;
642 	struct buffer_head *bh = NULL;
643 	struct ocfs2_extent_list *el;
644 
645 	el = &xv->xr_list;
646 	clusters = le32_to_cpu(xv->xr_clusters);
647 	bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
648 	blocksize = inode->i_sb->s_blocksize;
649 
650 	cpos = 0;
651 	while (cpos < clusters) {
652 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
653 					       &num_clusters, el);
654 		if (ret) {
655 			mlog_errno(ret);
656 			goto out;
657 		}
658 
659 		blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
660 		/* Copy ocfs2_xattr_value */
661 		for (i = 0; i < num_clusters * bpc; i++, blkno++) {
662 			ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
663 					       &bh, OCFS2_BH_CACHED, inode);
664 			if (ret) {
665 				mlog_errno(ret);
666 				goto out;
667 			}
668 
669 			cplen = len >= blocksize ? blocksize : len;
670 			memcpy(buffer, bh->b_data, cplen);
671 			len -= cplen;
672 			buffer += cplen;
673 
674 			brelse(bh);
675 			bh = NULL;
676 			if (len == 0)
677 				break;
678 		}
679 		cpos += num_clusters;
680 	}
681 out:
682 	return ret;
683 }
684 
685 static int ocfs2_xattr_ibody_get(struct inode *inode,
686 				 int name_index,
687 				 const char *name,
688 				 void *buffer,
689 				 size_t buffer_size,
690 				 struct ocfs2_xattr_search *xs)
691 {
692 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
693 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
694 	struct ocfs2_xattr_value_root *xv;
695 	size_t size;
696 	int ret = 0;
697 
698 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
699 		return -ENODATA;
700 
701 	xs->end = (void *)di + inode->i_sb->s_blocksize;
702 	xs->header = (struct ocfs2_xattr_header *)
703 			(xs->end - le16_to_cpu(di->i_xattr_inline_size));
704 	xs->base = (void *)xs->header;
705 	xs->here = xs->header->xh_entries;
706 
707 	ret = ocfs2_xattr_find_entry(name_index, name, xs);
708 	if (ret)
709 		return ret;
710 	size = le64_to_cpu(xs->here->xe_value_size);
711 	if (buffer) {
712 		if (size > buffer_size)
713 			return -ERANGE;
714 		if (ocfs2_xattr_is_local(xs->here)) {
715 			memcpy(buffer, (void *)xs->base +
716 			       le16_to_cpu(xs->here->xe_name_offset) +
717 			       OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
718 		} else {
719 			xv = (struct ocfs2_xattr_value_root *)
720 				(xs->base + le16_to_cpu(
721 				 xs->here->xe_name_offset) +
722 				OCFS2_XATTR_SIZE(xs->here->xe_name_len));
723 			ret = ocfs2_xattr_get_value_outside(inode, xv,
724 							    buffer, size);
725 			if (ret < 0) {
726 				mlog_errno(ret);
727 				return ret;
728 			}
729 		}
730 	}
731 
732 	return size;
733 }
734 
735 static int ocfs2_xattr_block_get(struct inode *inode,
736 				 int name_index,
737 				 const char *name,
738 				 void *buffer,
739 				 size_t buffer_size,
740 				 struct ocfs2_xattr_search *xs)
741 {
742 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
743 	struct buffer_head *blk_bh = NULL;
744 	struct ocfs2_xattr_block *xb;
745 	struct ocfs2_xattr_value_root *xv;
746 	size_t size;
747 	int ret = -ENODATA, name_offset, name_len, block_off, i;
748 
749 	if (!di->i_xattr_loc)
750 		return ret;
751 
752 	memset(&xs->bucket, 0, sizeof(xs->bucket));
753 
754 	ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
755 			       le64_to_cpu(di->i_xattr_loc),
756 			       &blk_bh, OCFS2_BH_CACHED, inode);
757 	if (ret < 0) {
758 		mlog_errno(ret);
759 		return ret;
760 	}
761 	/*Verify the signature of xattr block*/
762 	if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
763 		   strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
764 		ret = -EFAULT;
765 		goto cleanup;
766 	}
767 
768 	xs->xattr_bh = blk_bh;
769 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
770 
771 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
772 		xs->header = &xb->xb_attrs.xb_header;
773 		xs->base = (void *)xs->header;
774 		xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
775 		xs->here = xs->header->xh_entries;
776 
777 		ret = ocfs2_xattr_find_entry(name_index, name, xs);
778 	} else
779 		ret = ocfs2_xattr_index_block_find(inode, blk_bh,
780 						   name_index,
781 						   name, xs);
782 
783 	if (ret)
784 		goto cleanup;
785 	size = le64_to_cpu(xs->here->xe_value_size);
786 	if (buffer) {
787 		ret = -ERANGE;
788 		if (size > buffer_size)
789 			goto cleanup;
790 
791 		name_offset = le16_to_cpu(xs->here->xe_name_offset);
792 		name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
793 		i = xs->here - xs->header->xh_entries;
794 
795 		if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
796 			ret = ocfs2_xattr_bucket_get_name_value(inode,
797 								xs->bucket.xh,
798 								i,
799 								&block_off,
800 								&name_offset);
801 			xs->base = xs->bucket.bhs[block_off]->b_data;
802 		}
803 		if (ocfs2_xattr_is_local(xs->here)) {
804 			memcpy(buffer, (void *)xs->base +
805 			       name_offset + name_len, size);
806 		} else {
807 			xv = (struct ocfs2_xattr_value_root *)
808 				(xs->base + name_offset + name_len);
809 			ret = ocfs2_xattr_get_value_outside(inode, xv,
810 							    buffer, size);
811 			if (ret < 0) {
812 				mlog_errno(ret);
813 				goto cleanup;
814 			}
815 		}
816 	}
817 	ret = size;
818 cleanup:
819 	for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
820 		brelse(xs->bucket.bhs[i]);
821 	memset(&xs->bucket, 0, sizeof(xs->bucket));
822 
823 	brelse(blk_bh);
824 	return ret;
825 }
826 
827 /* ocfs2_xattr_get()
828  *
829  * Copy an extended attribute into the buffer provided.
830  * Buffer is NULL to compute the size of buffer required.
831  */
832 int ocfs2_xattr_get(struct inode *inode,
833 		    int name_index,
834 		    const char *name,
835 		    void *buffer,
836 		    size_t buffer_size)
837 {
838 	int ret;
839 	struct ocfs2_dinode *di = NULL;
840 	struct buffer_head *di_bh = NULL;
841 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
842 	struct ocfs2_xattr_search xis = {
843 		.not_found = -ENODATA,
844 	};
845 	struct ocfs2_xattr_search xbs = {
846 		.not_found = -ENODATA,
847 	};
848 
849 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
850 		return -EOPNOTSUPP;
851 
852 	if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
853 		ret = -ENODATA;
854 
855 	ret = ocfs2_inode_lock(inode, &di_bh, 0);
856 	if (ret < 0) {
857 		mlog_errno(ret);
858 		return ret;
859 	}
860 	xis.inode_bh = xbs.inode_bh = di_bh;
861 	di = (struct ocfs2_dinode *)di_bh->b_data;
862 
863 	down_read(&oi->ip_xattr_sem);
864 	ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
865 				    buffer_size, &xis);
866 	if (ret == -ENODATA)
867 		ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
868 					    buffer_size, &xbs);
869 	up_read(&oi->ip_xattr_sem);
870 	ocfs2_inode_unlock(inode, 0);
871 
872 	brelse(di_bh);
873 
874 	return ret;
875 }
876 
877 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
878 					   struct ocfs2_xattr_value_root *xv,
879 					   const void *value,
880 					   int value_len)
881 {
882 	int ret = 0, i, cp_len, credits;
883 	u16 blocksize = inode->i_sb->s_blocksize;
884 	u32 p_cluster, num_clusters;
885 	u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
886 	u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
887 	u64 blkno;
888 	struct buffer_head *bh = NULL;
889 	handle_t *handle;
890 
891 	BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
892 
893 	credits = clusters * bpc;
894 	handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
895 	if (IS_ERR(handle)) {
896 		ret = PTR_ERR(handle);
897 		mlog_errno(ret);
898 		goto out;
899 	}
900 
901 	while (cpos < clusters) {
902 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
903 					       &num_clusters, &xv->xr_list);
904 		if (ret) {
905 			mlog_errno(ret);
906 			goto out_commit;
907 		}
908 
909 		blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
910 
911 		for (i = 0; i < num_clusters * bpc; i++, blkno++) {
912 			ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
913 					       &bh, OCFS2_BH_CACHED, inode);
914 			if (ret) {
915 				mlog_errno(ret);
916 				goto out_commit;
917 			}
918 
919 			ret = ocfs2_journal_access(handle,
920 						   inode,
921 						   bh,
922 						   OCFS2_JOURNAL_ACCESS_WRITE);
923 			if (ret < 0) {
924 				mlog_errno(ret);
925 				goto out_commit;
926 			}
927 
928 			cp_len = value_len > blocksize ? blocksize : value_len;
929 			memcpy(bh->b_data, value, cp_len);
930 			value_len -= cp_len;
931 			value += cp_len;
932 			if (cp_len < blocksize)
933 				memset(bh->b_data + cp_len, 0,
934 				       blocksize - cp_len);
935 
936 			ret = ocfs2_journal_dirty(handle, bh);
937 			if (ret < 0) {
938 				mlog_errno(ret);
939 				goto out_commit;
940 			}
941 			brelse(bh);
942 			bh = NULL;
943 
944 			/*
945 			 * XXX: do we need to empty all the following
946 			 * blocks in this cluster?
947 			 */
948 			if (!value_len)
949 				break;
950 		}
951 		cpos += num_clusters;
952 	}
953 out_commit:
954 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
955 out:
956 	brelse(bh);
957 
958 	return ret;
959 }
960 
961 static int ocfs2_xattr_cleanup(struct inode *inode,
962 			       struct ocfs2_xattr_info *xi,
963 			       struct ocfs2_xattr_search *xs,
964 			       size_t offs)
965 {
966 	handle_t *handle = NULL;
967 	int ret = 0;
968 	size_t name_len = strlen(xi->name);
969 	void *val = xs->base + offs;
970 	size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
971 
972 	handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
973 				   OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
974 	if (IS_ERR(handle)) {
975 		ret = PTR_ERR(handle);
976 		mlog_errno(ret);
977 		goto out;
978 	}
979 	ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
980 				   OCFS2_JOURNAL_ACCESS_WRITE);
981 	if (ret) {
982 		mlog_errno(ret);
983 		goto out_commit;
984 	}
985 	/* Decrease xattr count */
986 	le16_add_cpu(&xs->header->xh_count, -1);
987 	/* Remove the xattr entry and tree root which has already be set*/
988 	memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
989 	memset(val, 0, size);
990 
991 	ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
992 	if (ret < 0)
993 		mlog_errno(ret);
994 out_commit:
995 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
996 out:
997 	return ret;
998 }
999 
1000 static int ocfs2_xattr_update_entry(struct inode *inode,
1001 				    struct ocfs2_xattr_info *xi,
1002 				    struct ocfs2_xattr_search *xs,
1003 				    size_t offs)
1004 {
1005 	handle_t *handle = NULL;
1006 	int ret = 0;
1007 
1008 	handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1009 				   OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1010 	if (IS_ERR(handle)) {
1011 		ret = PTR_ERR(handle);
1012 		mlog_errno(ret);
1013 		goto out;
1014 	}
1015 	ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1016 				   OCFS2_JOURNAL_ACCESS_WRITE);
1017 	if (ret) {
1018 		mlog_errno(ret);
1019 		goto out_commit;
1020 	}
1021 
1022 	xs->here->xe_name_offset = cpu_to_le16(offs);
1023 	xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1024 	if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1025 		ocfs2_xattr_set_local(xs->here, 1);
1026 	else
1027 		ocfs2_xattr_set_local(xs->here, 0);
1028 	ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1029 
1030 	ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1031 	if (ret < 0)
1032 		mlog_errno(ret);
1033 out_commit:
1034 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1035 out:
1036 	return ret;
1037 }
1038 
1039 /*
1040  * ocfs2_xattr_set_value_outside()
1041  *
1042  * Set large size value in B tree.
1043  */
1044 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1045 					 struct ocfs2_xattr_info *xi,
1046 					 struct ocfs2_xattr_search *xs,
1047 					 size_t offs)
1048 {
1049 	size_t name_len = strlen(xi->name);
1050 	void *val = xs->base + offs;
1051 	struct ocfs2_xattr_value_root *xv = NULL;
1052 	size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1053 	int ret = 0;
1054 
1055 	memset(val, 0, size);
1056 	memcpy(val, xi->name, name_len);
1057 	xv = (struct ocfs2_xattr_value_root *)
1058 		(val + OCFS2_XATTR_SIZE(name_len));
1059 	xv->xr_clusters = 0;
1060 	xv->xr_last_eb_blk = 0;
1061 	xv->xr_list.l_tree_depth = 0;
1062 	xv->xr_list.l_count = cpu_to_le16(1);
1063 	xv->xr_list.l_next_free_rec = 0;
1064 
1065 	ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1066 					 xi->value_len);
1067 	if (ret < 0) {
1068 		mlog_errno(ret);
1069 		return ret;
1070 	}
1071 	ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1072 					      xi->value_len);
1073 	if (ret < 0) {
1074 		mlog_errno(ret);
1075 		return ret;
1076 	}
1077 	ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1078 	if (ret < 0)
1079 		mlog_errno(ret);
1080 
1081 	return ret;
1082 }
1083 
1084 /*
1085  * ocfs2_xattr_set_entry_local()
1086  *
1087  * Set, replace or remove extended attribute in local.
1088  */
1089 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1090 					struct ocfs2_xattr_info *xi,
1091 					struct ocfs2_xattr_search *xs,
1092 					struct ocfs2_xattr_entry *last,
1093 					size_t min_offs)
1094 {
1095 	size_t name_len = strlen(xi->name);
1096 	int i;
1097 
1098 	if (xi->value && xs->not_found) {
1099 		/* Insert the new xattr entry. */
1100 		le16_add_cpu(&xs->header->xh_count, 1);
1101 		ocfs2_xattr_set_type(last, xi->name_index);
1102 		ocfs2_xattr_set_local(last, 1);
1103 		last->xe_name_len = name_len;
1104 	} else {
1105 		void *first_val;
1106 		void *val;
1107 		size_t offs, size;
1108 
1109 		first_val = xs->base + min_offs;
1110 		offs = le16_to_cpu(xs->here->xe_name_offset);
1111 		val = xs->base + offs;
1112 
1113 		if (le64_to_cpu(xs->here->xe_value_size) >
1114 		    OCFS2_XATTR_INLINE_SIZE)
1115 			size = OCFS2_XATTR_SIZE(name_len) +
1116 				OCFS2_XATTR_ROOT_SIZE;
1117 		else
1118 			size = OCFS2_XATTR_SIZE(name_len) +
1119 			OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1120 
1121 		if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1122 				OCFS2_XATTR_SIZE(xi->value_len)) {
1123 			/* The old and the new value have the
1124 			   same size. Just replace the value. */
1125 			ocfs2_xattr_set_local(xs->here, 1);
1126 			xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1127 			/* Clear value bytes. */
1128 			memset(val + OCFS2_XATTR_SIZE(name_len),
1129 			       0,
1130 			       OCFS2_XATTR_SIZE(xi->value_len));
1131 			memcpy(val + OCFS2_XATTR_SIZE(name_len),
1132 			       xi->value,
1133 			       xi->value_len);
1134 			return;
1135 		}
1136 		/* Remove the old name+value. */
1137 		memmove(first_val + size, first_val, val - first_val);
1138 		memset(first_val, 0, size);
1139 		xs->here->xe_name_hash = 0;
1140 		xs->here->xe_name_offset = 0;
1141 		ocfs2_xattr_set_local(xs->here, 1);
1142 		xs->here->xe_value_size = 0;
1143 
1144 		min_offs += size;
1145 
1146 		/* Adjust all value offsets. */
1147 		last = xs->header->xh_entries;
1148 		for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1149 			size_t o = le16_to_cpu(last->xe_name_offset);
1150 
1151 			if (o < offs)
1152 				last->xe_name_offset = cpu_to_le16(o + size);
1153 			last += 1;
1154 		}
1155 
1156 		if (!xi->value) {
1157 			/* Remove the old entry. */
1158 			last -= 1;
1159 			memmove(xs->here, xs->here + 1,
1160 				(void *)last - (void *)xs->here);
1161 			memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1162 			le16_add_cpu(&xs->header->xh_count, -1);
1163 		}
1164 	}
1165 	if (xi->value) {
1166 		/* Insert the new name+value. */
1167 		size_t size = OCFS2_XATTR_SIZE(name_len) +
1168 				OCFS2_XATTR_SIZE(xi->value_len);
1169 		void *val = xs->base + min_offs - size;
1170 
1171 		xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1172 		memset(val, 0, size);
1173 		memcpy(val, xi->name, name_len);
1174 		memcpy(val + OCFS2_XATTR_SIZE(name_len),
1175 		       xi->value,
1176 		       xi->value_len);
1177 		xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1178 		ocfs2_xattr_set_local(xs->here, 1);
1179 		ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1180 	}
1181 
1182 	return;
1183 }
1184 
1185 /*
1186  * ocfs2_xattr_set_entry()
1187  *
1188  * Set extended attribute entry into inode or block.
1189  *
1190  * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1191  * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1192  * then set value in B tree with set_value_outside().
1193  */
1194 static int ocfs2_xattr_set_entry(struct inode *inode,
1195 				 struct ocfs2_xattr_info *xi,
1196 				 struct ocfs2_xattr_search *xs,
1197 				 int flag)
1198 {
1199 	struct ocfs2_xattr_entry *last;
1200 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1201 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1202 	size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1203 	size_t size_l = 0;
1204 	handle_t *handle = NULL;
1205 	int free, i, ret;
1206 	struct ocfs2_xattr_info xi_l = {
1207 		.name_index = xi->name_index,
1208 		.name = xi->name,
1209 		.value = xi->value,
1210 		.value_len = xi->value_len,
1211 	};
1212 
1213 	/* Compute min_offs, last and free space. */
1214 	last = xs->header->xh_entries;
1215 
1216 	for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1217 		size_t offs = le16_to_cpu(last->xe_name_offset);
1218 		if (offs < min_offs)
1219 			min_offs = offs;
1220 		last += 1;
1221 	}
1222 
1223 	free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1224 	if (free < 0)
1225 		return -EFAULT;
1226 
1227 	if (!xs->not_found) {
1228 		size_t size = 0;
1229 		if (ocfs2_xattr_is_local(xs->here))
1230 			size = OCFS2_XATTR_SIZE(name_len) +
1231 			OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1232 		else
1233 			size = OCFS2_XATTR_SIZE(name_len) +
1234 				OCFS2_XATTR_ROOT_SIZE;
1235 		free += (size + sizeof(struct ocfs2_xattr_entry));
1236 	}
1237 	/* Check free space in inode or block */
1238 	if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1239 		if (free < sizeof(struct ocfs2_xattr_entry) +
1240 			   OCFS2_XATTR_SIZE(name_len) +
1241 			   OCFS2_XATTR_ROOT_SIZE) {
1242 			ret = -ENOSPC;
1243 			goto out;
1244 		}
1245 		size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1246 		xi_l.value = (void *)&def_xv;
1247 		xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1248 	} else if (xi->value) {
1249 		if (free < sizeof(struct ocfs2_xattr_entry) +
1250 			   OCFS2_XATTR_SIZE(name_len) +
1251 			   OCFS2_XATTR_SIZE(xi->value_len)) {
1252 			ret = -ENOSPC;
1253 			goto out;
1254 		}
1255 	}
1256 
1257 	if (!xs->not_found) {
1258 		/* For existing extended attribute */
1259 		size_t size = OCFS2_XATTR_SIZE(name_len) +
1260 			OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1261 		size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1262 		void *val = xs->base + offs;
1263 
1264 		if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1265 			/* Replace existing local xattr with tree root */
1266 			ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1267 							    offs);
1268 			if (ret < 0)
1269 				mlog_errno(ret);
1270 			goto out;
1271 		} else if (!ocfs2_xattr_is_local(xs->here)) {
1272 			/* For existing xattr which has value outside */
1273 			struct ocfs2_xattr_value_root *xv = NULL;
1274 			xv = (struct ocfs2_xattr_value_root *)(val +
1275 				OCFS2_XATTR_SIZE(name_len));
1276 
1277 			if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1278 				/*
1279 				 * If new value need set outside also,
1280 				 * first truncate old value to new value,
1281 				 * then set new value with set_value_outside().
1282 				 */
1283 				ret = ocfs2_xattr_value_truncate(inode,
1284 								 xs->xattr_bh,
1285 								 xv,
1286 								 xi->value_len);
1287 				if (ret < 0) {
1288 					mlog_errno(ret);
1289 					goto out;
1290 				}
1291 
1292 				ret = __ocfs2_xattr_set_value_outside(inode,
1293 								xv,
1294 								xi->value,
1295 								xi->value_len);
1296 				if (ret < 0) {
1297 					mlog_errno(ret);
1298 					goto out;
1299 				}
1300 
1301 				ret = ocfs2_xattr_update_entry(inode,
1302 							       xi,
1303 							       xs,
1304 							       offs);
1305 				if (ret < 0)
1306 					mlog_errno(ret);
1307 				goto out;
1308 			} else {
1309 				/*
1310 				 * If new value need set in local,
1311 				 * just trucate old value to zero.
1312 				 */
1313 				 ret = ocfs2_xattr_value_truncate(inode,
1314 								 xs->xattr_bh,
1315 								 xv,
1316 								 0);
1317 				if (ret < 0)
1318 					mlog_errno(ret);
1319 			}
1320 		}
1321 	}
1322 
1323 	handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1324 				   OCFS2_INODE_UPDATE_CREDITS);
1325 	if (IS_ERR(handle)) {
1326 		ret = PTR_ERR(handle);
1327 		mlog_errno(ret);
1328 		goto out;
1329 	}
1330 
1331 	ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1332 				   OCFS2_JOURNAL_ACCESS_WRITE);
1333 	if (ret) {
1334 		mlog_errno(ret);
1335 		goto out_commit;
1336 	}
1337 
1338 	if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1339 		/* set extended attribute in external block. */
1340 		ret = ocfs2_extend_trans(handle,
1341 					 OCFS2_INODE_UPDATE_CREDITS +
1342 					 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1343 		if (ret) {
1344 			mlog_errno(ret);
1345 			goto out_commit;
1346 		}
1347 		ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1348 					   OCFS2_JOURNAL_ACCESS_WRITE);
1349 		if (ret) {
1350 			mlog_errno(ret);
1351 			goto out_commit;
1352 		}
1353 	}
1354 
1355 	/*
1356 	 * Set value in local, include set tree root in local.
1357 	 * This is the first step for value size >INLINE_SIZE.
1358 	 */
1359 	ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1360 
1361 	if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1362 		ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1363 		if (ret < 0) {
1364 			mlog_errno(ret);
1365 			goto out_commit;
1366 		}
1367 	}
1368 
1369 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1370 	    (flag & OCFS2_INLINE_XATTR_FL)) {
1371 		struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1372 		unsigned int xattrsize = osb->s_xattr_inline_size;
1373 
1374 		/*
1375 		 * Adjust extent record count or inline data size
1376 		 * to reserve space for extended attribute.
1377 		 */
1378 		if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1379 			struct ocfs2_inline_data *idata = &di->id2.i_data;
1380 			le16_add_cpu(&idata->id_count, -xattrsize);
1381 		} else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1382 			struct ocfs2_extent_list *el = &di->id2.i_list;
1383 			le16_add_cpu(&el->l_count, -(xattrsize /
1384 					sizeof(struct ocfs2_extent_rec)));
1385 		}
1386 		di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1387 	}
1388 	/* Update xattr flag */
1389 	spin_lock(&oi->ip_lock);
1390 	oi->ip_dyn_features |= flag;
1391 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1392 	spin_unlock(&oi->ip_lock);
1393 	/* Update inode ctime */
1394 	inode->i_ctime = CURRENT_TIME;
1395 	di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1396 	di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1397 
1398 	ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1399 	if (ret < 0)
1400 		mlog_errno(ret);
1401 
1402 out_commit:
1403 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1404 
1405 	if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1406 		/*
1407 		 * Set value outside in B tree.
1408 		 * This is the second step for value size > INLINE_SIZE.
1409 		 */
1410 		size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1411 		ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1412 		if (ret < 0) {
1413 			int ret2;
1414 
1415 			mlog_errno(ret);
1416 			/*
1417 			 * If set value outside failed, we have to clean
1418 			 * the junk tree root we have already set in local.
1419 			 */
1420 			ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1421 			if (ret2 < 0)
1422 				mlog_errno(ret2);
1423 		}
1424 	}
1425 out:
1426 	return ret;
1427 
1428 }
1429 
1430 static int ocfs2_remove_value_outside(struct inode*inode,
1431 				      struct buffer_head *bh,
1432 				      struct ocfs2_xattr_header *header)
1433 {
1434 	int ret = 0, i;
1435 
1436 	for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1437 		struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1438 
1439 		if (!ocfs2_xattr_is_local(entry)) {
1440 			struct ocfs2_xattr_value_root *xv;
1441 			void *val;
1442 
1443 			val = (void *)header +
1444 				le16_to_cpu(entry->xe_name_offset);
1445 			xv = (struct ocfs2_xattr_value_root *)
1446 				(val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1447 			ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1448 			if (ret < 0) {
1449 				mlog_errno(ret);
1450 				return ret;
1451 			}
1452 		}
1453 	}
1454 
1455 	return ret;
1456 }
1457 
1458 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1459 				    struct buffer_head *di_bh)
1460 {
1461 
1462 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1463 	struct ocfs2_xattr_header *header;
1464 	int ret;
1465 
1466 	header = (struct ocfs2_xattr_header *)
1467 		 ((void *)di + inode->i_sb->s_blocksize -
1468 		 le16_to_cpu(di->i_xattr_inline_size));
1469 
1470 	ret = ocfs2_remove_value_outside(inode, di_bh, header);
1471 
1472 	return ret;
1473 }
1474 
1475 static int ocfs2_xattr_block_remove(struct inode *inode,
1476 				    struct buffer_head *blk_bh)
1477 {
1478 	struct ocfs2_xattr_block *xb;
1479 	int ret = 0;
1480 
1481 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1482 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1483 		struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1484 		ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1485 	} else
1486 		ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1487 
1488 	return ret;
1489 }
1490 
1491 static int ocfs2_xattr_free_block(struct inode *inode,
1492 				  u64 block)
1493 {
1494 	struct inode *xb_alloc_inode;
1495 	struct buffer_head *xb_alloc_bh = NULL;
1496 	struct buffer_head *blk_bh = NULL;
1497 	struct ocfs2_xattr_block *xb;
1498 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1499 	handle_t *handle;
1500 	int ret = 0;
1501 	u64 blk, bg_blkno;
1502 	u16 bit;
1503 
1504 	ret = ocfs2_read_block(osb, block, &blk_bh,
1505 			       OCFS2_BH_CACHED, inode);
1506 	if (ret < 0) {
1507 		mlog_errno(ret);
1508 		goto out;
1509 	}
1510 
1511 	/*Verify the signature of xattr block*/
1512 	if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1513 		   strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1514 		ret = -EFAULT;
1515 		goto out;
1516 	}
1517 
1518 	ret = ocfs2_xattr_block_remove(inode, blk_bh);
1519 	if (ret < 0) {
1520 		mlog_errno(ret);
1521 		goto out;
1522 	}
1523 
1524 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1525 	blk = le64_to_cpu(xb->xb_blkno);
1526 	bit = le16_to_cpu(xb->xb_suballoc_bit);
1527 	bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1528 
1529 	xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1530 				EXTENT_ALLOC_SYSTEM_INODE,
1531 				le16_to_cpu(xb->xb_suballoc_slot));
1532 	if (!xb_alloc_inode) {
1533 		ret = -ENOMEM;
1534 		mlog_errno(ret);
1535 		goto out;
1536 	}
1537 	mutex_lock(&xb_alloc_inode->i_mutex);
1538 
1539 	ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1540 	if (ret < 0) {
1541 		mlog_errno(ret);
1542 		goto out_mutex;
1543 	}
1544 
1545 	handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1546 	if (IS_ERR(handle)) {
1547 		ret = PTR_ERR(handle);
1548 		mlog_errno(ret);
1549 		goto out_unlock;
1550 	}
1551 
1552 	ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1553 				       bit, bg_blkno, 1);
1554 	if (ret < 0)
1555 		mlog_errno(ret);
1556 
1557 	ocfs2_commit_trans(osb, handle);
1558 out_unlock:
1559 	ocfs2_inode_unlock(xb_alloc_inode, 1);
1560 	brelse(xb_alloc_bh);
1561 out_mutex:
1562 	mutex_unlock(&xb_alloc_inode->i_mutex);
1563 	iput(xb_alloc_inode);
1564 out:
1565 	brelse(blk_bh);
1566 	return ret;
1567 }
1568 
1569 /*
1570  * ocfs2_xattr_remove()
1571  *
1572  * Free extended attribute resources associated with this inode.
1573  */
1574 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1575 {
1576 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1577 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1578 	handle_t *handle;
1579 	int ret;
1580 
1581 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1582 		return 0;
1583 
1584 	if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1585 		return 0;
1586 
1587 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1588 		ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1589 		if (ret < 0) {
1590 			mlog_errno(ret);
1591 			goto out;
1592 		}
1593 	}
1594 
1595 	if (di->i_xattr_loc) {
1596 		ret = ocfs2_xattr_free_block(inode,
1597 					     le64_to_cpu(di->i_xattr_loc));
1598 		if (ret < 0) {
1599 			mlog_errno(ret);
1600 			goto out;
1601 		}
1602 	}
1603 
1604 	handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1605 				   OCFS2_INODE_UPDATE_CREDITS);
1606 	if (IS_ERR(handle)) {
1607 		ret = PTR_ERR(handle);
1608 		mlog_errno(ret);
1609 		goto out;
1610 	}
1611 	ret = ocfs2_journal_access(handle, inode, di_bh,
1612 				   OCFS2_JOURNAL_ACCESS_WRITE);
1613 	if (ret) {
1614 		mlog_errno(ret);
1615 		goto out_commit;
1616 	}
1617 
1618 	di->i_xattr_loc = 0;
1619 
1620 	spin_lock(&oi->ip_lock);
1621 	oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1622 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1623 	spin_unlock(&oi->ip_lock);
1624 
1625 	ret = ocfs2_journal_dirty(handle, di_bh);
1626 	if (ret < 0)
1627 		mlog_errno(ret);
1628 out_commit:
1629 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1630 out:
1631 	return ret;
1632 }
1633 
1634 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1635 					struct ocfs2_dinode *di)
1636 {
1637 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1638 	unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1639 	int free;
1640 
1641 	if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1642 		return 0;
1643 
1644 	if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1645 		struct ocfs2_inline_data *idata = &di->id2.i_data;
1646 		free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1647 	} else if (ocfs2_inode_is_fast_symlink(inode)) {
1648 		free = ocfs2_fast_symlink_chars(inode->i_sb) -
1649 			le64_to_cpu(di->i_size);
1650 	} else {
1651 		struct ocfs2_extent_list *el = &di->id2.i_list;
1652 		free = (le16_to_cpu(el->l_count) -
1653 			le16_to_cpu(el->l_next_free_rec)) *
1654 			sizeof(struct ocfs2_extent_rec);
1655 	}
1656 	if (free >= xattrsize)
1657 		return 1;
1658 
1659 	return 0;
1660 }
1661 
1662 /*
1663  * ocfs2_xattr_ibody_find()
1664  *
1665  * Find extended attribute in inode block and
1666  * fill search info into struct ocfs2_xattr_search.
1667  */
1668 static int ocfs2_xattr_ibody_find(struct inode *inode,
1669 				  int name_index,
1670 				  const char *name,
1671 				  struct ocfs2_xattr_search *xs)
1672 {
1673 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1674 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1675 	int ret;
1676 	int has_space = 0;
1677 
1678 	if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1679 		return 0;
1680 
1681 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1682 		down_read(&oi->ip_alloc_sem);
1683 		has_space = ocfs2_xattr_has_space_inline(inode, di);
1684 		up_read(&oi->ip_alloc_sem);
1685 		if (!has_space)
1686 			return 0;
1687 	}
1688 
1689 	xs->xattr_bh = xs->inode_bh;
1690 	xs->end = (void *)di + inode->i_sb->s_blocksize;
1691 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1692 		xs->header = (struct ocfs2_xattr_header *)
1693 			(xs->end - le16_to_cpu(di->i_xattr_inline_size));
1694 	else
1695 		xs->header = (struct ocfs2_xattr_header *)
1696 			(xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1697 	xs->base = (void *)xs->header;
1698 	xs->here = xs->header->xh_entries;
1699 
1700 	/* Find the named attribute. */
1701 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1702 		ret = ocfs2_xattr_find_entry(name_index, name, xs);
1703 		if (ret && ret != -ENODATA)
1704 			return ret;
1705 		xs->not_found = ret;
1706 	}
1707 
1708 	return 0;
1709 }
1710 
1711 /*
1712  * ocfs2_xattr_ibody_set()
1713  *
1714  * Set, replace or remove an extended attribute into inode block.
1715  *
1716  */
1717 static int ocfs2_xattr_ibody_set(struct inode *inode,
1718 				 struct ocfs2_xattr_info *xi,
1719 				 struct ocfs2_xattr_search *xs)
1720 {
1721 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1722 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1723 	int ret;
1724 
1725 	if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1726 		return -ENOSPC;
1727 
1728 	down_write(&oi->ip_alloc_sem);
1729 	if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1730 		if (!ocfs2_xattr_has_space_inline(inode, di)) {
1731 			ret = -ENOSPC;
1732 			goto out;
1733 		}
1734 	}
1735 
1736 	ret = ocfs2_xattr_set_entry(inode, xi, xs,
1737 				(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1738 out:
1739 	up_write(&oi->ip_alloc_sem);
1740 
1741 	return ret;
1742 }
1743 
1744 /*
1745  * ocfs2_xattr_block_find()
1746  *
1747  * Find extended attribute in external block and
1748  * fill search info into struct ocfs2_xattr_search.
1749  */
1750 static int ocfs2_xattr_block_find(struct inode *inode,
1751 				  int name_index,
1752 				  const char *name,
1753 				  struct ocfs2_xattr_search *xs)
1754 {
1755 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1756 	struct buffer_head *blk_bh = NULL;
1757 	struct ocfs2_xattr_block *xb;
1758 	int ret = 0;
1759 
1760 	if (!di->i_xattr_loc)
1761 		return ret;
1762 
1763 	ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1764 			       le64_to_cpu(di->i_xattr_loc),
1765 			       &blk_bh, OCFS2_BH_CACHED, inode);
1766 	if (ret < 0) {
1767 		mlog_errno(ret);
1768 		return ret;
1769 	}
1770 	/*Verify the signature of xattr block*/
1771 	if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1772 		   strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1773 			ret = -EFAULT;
1774 			goto cleanup;
1775 	}
1776 
1777 	xs->xattr_bh = blk_bh;
1778 	xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1779 
1780 	if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1781 		xs->header = &xb->xb_attrs.xb_header;
1782 		xs->base = (void *)xs->header;
1783 		xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1784 		xs->here = xs->header->xh_entries;
1785 
1786 		ret = ocfs2_xattr_find_entry(name_index, name, xs);
1787 	} else
1788 		ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1789 						   name_index,
1790 						   name, xs);
1791 
1792 	if (ret && ret != -ENODATA) {
1793 		xs->xattr_bh = NULL;
1794 		goto cleanup;
1795 	}
1796 	xs->not_found = ret;
1797 	return 0;
1798 cleanup:
1799 	brelse(blk_bh);
1800 
1801 	return ret;
1802 }
1803 
1804 /*
1805  * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1806  * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1807  * re-initialized.
1808  */
1809 static int ocfs2_restore_xattr_block(struct inode *inode,
1810 				     struct ocfs2_xattr_search *xs)
1811 {
1812 	int ret;
1813 	handle_t *handle;
1814 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1815 	struct ocfs2_xattr_block *xb =
1816 		(struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1817 	struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1818 	u16 xb_flags = le16_to_cpu(xb->xb_flags);
1819 
1820 	BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1821 		le16_to_cpu(el->l_next_free_rec) != 0);
1822 
1823 	handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1824 	if (IS_ERR(handle)) {
1825 		ret = PTR_ERR(handle);
1826 		handle = NULL;
1827 		goto out;
1828 	}
1829 
1830 	ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1831 				   OCFS2_JOURNAL_ACCESS_WRITE);
1832 	if (ret < 0) {
1833 		mlog_errno(ret);
1834 		goto out_commit;
1835 	}
1836 
1837 	memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1838 	       offsetof(struct ocfs2_xattr_block, xb_attrs));
1839 
1840 	xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1841 
1842 	ocfs2_journal_dirty(handle, xs->xattr_bh);
1843 
1844 out_commit:
1845 	ocfs2_commit_trans(osb, handle);
1846 out:
1847 	return ret;
1848 }
1849 
1850 /*
1851  * ocfs2_xattr_block_set()
1852  *
1853  * Set, replace or remove an extended attribute into external block.
1854  *
1855  */
1856 static int ocfs2_xattr_block_set(struct inode *inode,
1857 				 struct ocfs2_xattr_info *xi,
1858 				 struct ocfs2_xattr_search *xs)
1859 {
1860 	struct buffer_head *new_bh = NULL;
1861 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1862 	struct ocfs2_dinode *di =  (struct ocfs2_dinode *)xs->inode_bh->b_data;
1863 	struct ocfs2_alloc_context *meta_ac = NULL;
1864 	handle_t *handle = NULL;
1865 	struct ocfs2_xattr_block *xblk = NULL;
1866 	u16 suballoc_bit_start;
1867 	u32 num_got;
1868 	u64 first_blkno;
1869 	int ret;
1870 
1871 	if (!xs->xattr_bh) {
1872 		/*
1873 		 * Alloc one external block for extended attribute
1874 		 * outside of inode.
1875 		 */
1876 		ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1877 		if (ret < 0) {
1878 			mlog_errno(ret);
1879 			goto out;
1880 		}
1881 		handle = ocfs2_start_trans(osb,
1882 					   OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1883 		if (IS_ERR(handle)) {
1884 			ret = PTR_ERR(handle);
1885 			mlog_errno(ret);
1886 			goto out;
1887 		}
1888 		ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1889 					   OCFS2_JOURNAL_ACCESS_CREATE);
1890 		if (ret < 0) {
1891 			mlog_errno(ret);
1892 			goto out_commit;
1893 		}
1894 
1895 		ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1896 					   &suballoc_bit_start, &num_got,
1897 					   &first_blkno);
1898 		if (ret < 0) {
1899 			mlog_errno(ret);
1900 			goto out_commit;
1901 		}
1902 
1903 		new_bh = sb_getblk(inode->i_sb, first_blkno);
1904 		ocfs2_set_new_buffer_uptodate(inode, new_bh);
1905 
1906 		ret = ocfs2_journal_access(handle, inode, new_bh,
1907 					   OCFS2_JOURNAL_ACCESS_CREATE);
1908 		if (ret < 0) {
1909 			mlog_errno(ret);
1910 			goto out_commit;
1911 		}
1912 
1913 		/* Initialize ocfs2_xattr_block */
1914 		xs->xattr_bh = new_bh;
1915 		xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1916 		memset(xblk, 0, inode->i_sb->s_blocksize);
1917 		strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1918 		xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1919 		xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1920 		xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1921 		xblk->xb_blkno = cpu_to_le64(first_blkno);
1922 
1923 		xs->header = &xblk->xb_attrs.xb_header;
1924 		xs->base = (void *)xs->header;
1925 		xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1926 		xs->here = xs->header->xh_entries;
1927 
1928 
1929 		ret = ocfs2_journal_dirty(handle, new_bh);
1930 		if (ret < 0) {
1931 			mlog_errno(ret);
1932 			goto out_commit;
1933 		}
1934 		di->i_xattr_loc = cpu_to_le64(first_blkno);
1935 		ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1936 		if (ret < 0)
1937 			mlog_errno(ret);
1938 out_commit:
1939 		ocfs2_commit_trans(osb, handle);
1940 out:
1941 		if (meta_ac)
1942 			ocfs2_free_alloc_context(meta_ac);
1943 		if (ret < 0)
1944 			return ret;
1945 	} else
1946 		xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1947 
1948 	if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1949 		/* Set extended attribute into external block */
1950 		ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1951 		if (!ret || ret != -ENOSPC)
1952 			goto end;
1953 
1954 		ret = ocfs2_xattr_create_index_block(inode, xs);
1955 		if (ret)
1956 			goto end;
1957 	}
1958 
1959 	ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1960 	if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1961 		ret = ocfs2_restore_xattr_block(inode, xs);
1962 
1963 end:
1964 
1965 	return ret;
1966 }
1967 
1968 /*
1969  * ocfs2_xattr_set()
1970  *
1971  * Set, replace or remove an extended attribute for this inode.
1972  * value is NULL to remove an existing extended attribute, else either
1973  * create or replace an extended attribute.
1974  */
1975 int ocfs2_xattr_set(struct inode *inode,
1976 		    int name_index,
1977 		    const char *name,
1978 		    const void *value,
1979 		    size_t value_len,
1980 		    int flags)
1981 {
1982 	struct buffer_head *di_bh = NULL;
1983 	struct ocfs2_dinode *di;
1984 	int ret;
1985 	u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1986 
1987 	struct ocfs2_xattr_info xi = {
1988 		.name_index = name_index,
1989 		.name = name,
1990 		.value = value,
1991 		.value_len = value_len,
1992 	};
1993 
1994 	struct ocfs2_xattr_search xis = {
1995 		.not_found = -ENODATA,
1996 	};
1997 
1998 	struct ocfs2_xattr_search xbs = {
1999 		.not_found = -ENODATA,
2000 	};
2001 
2002 	if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2003 		return -EOPNOTSUPP;
2004 
2005 	ret = ocfs2_inode_lock(inode, &di_bh, 1);
2006 	if (ret < 0) {
2007 		mlog_errno(ret);
2008 		return ret;
2009 	}
2010 	xis.inode_bh = xbs.inode_bh = di_bh;
2011 	di = (struct ocfs2_dinode *)di_bh->b_data;
2012 
2013 	down_write(&OCFS2_I(inode)->ip_xattr_sem);
2014 	/*
2015 	 * Scan inode and external block to find the same name
2016 	 * extended attribute and collect search infomation.
2017 	 */
2018 	ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2019 	if (ret)
2020 		goto cleanup;
2021 	if (xis.not_found) {
2022 		ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2023 		if (ret)
2024 			goto cleanup;
2025 	}
2026 
2027 	if (xis.not_found && xbs.not_found) {
2028 		ret = -ENODATA;
2029 		if (flags & XATTR_REPLACE)
2030 			goto cleanup;
2031 		ret = 0;
2032 		if (!value)
2033 			goto cleanup;
2034 	} else {
2035 		ret = -EEXIST;
2036 		if (flags & XATTR_CREATE)
2037 			goto cleanup;
2038 	}
2039 
2040 	if (!value) {
2041 		/* Remove existing extended attribute */
2042 		if (!xis.not_found)
2043 			ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2044 		else if (!xbs.not_found)
2045 			ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2046 	} else {
2047 		/* We always try to set extended attribute into inode first*/
2048 		ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2049 		if (!ret && !xbs.not_found) {
2050 			/*
2051 			 * If succeed and that extended attribute existing in
2052 			 * external block, then we will remove it.
2053 			 */
2054 			xi.value = NULL;
2055 			xi.value_len = 0;
2056 			ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2057 		} else if (ret == -ENOSPC) {
2058 			if (di->i_xattr_loc && !xbs.xattr_bh) {
2059 				ret = ocfs2_xattr_block_find(inode, name_index,
2060 							     name, &xbs);
2061 				if (ret)
2062 					goto cleanup;
2063 			}
2064 			/*
2065 			 * If no space in inode, we will set extended attribute
2066 			 * into external block.
2067 			 */
2068 			ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2069 			if (ret)
2070 				goto cleanup;
2071 			if (!xis.not_found) {
2072 				/*
2073 				 * If succeed and that extended attribute
2074 				 * existing in inode, we will remove it.
2075 				 */
2076 				xi.value = NULL;
2077 				xi.value_len = 0;
2078 				ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2079 			}
2080 		}
2081 	}
2082 cleanup:
2083 	up_write(&OCFS2_I(inode)->ip_xattr_sem);
2084 	ocfs2_inode_unlock(inode, 1);
2085 	brelse(di_bh);
2086 	brelse(xbs.xattr_bh);
2087 	for (i = 0; i < blk_per_bucket; i++)
2088 		brelse(xbs.bucket.bhs[i]);
2089 
2090 	return ret;
2091 }
2092 
2093 static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode,
2094 					   int name_index,
2095 					   const char *suffix_name)
2096 {
2097 	struct xattr_handler *handler = ocfs2_xattr_handler(name_index);
2098 	char *prefix = handler->prefix;
2099 	int prefix_len = strlen(handler->prefix);
2100 
2101 	return ocfs2_xattr_name_hash(inode, prefix, prefix_len,
2102 				     (char *)suffix_name, strlen(suffix_name));
2103 }
2104 
2105 /*
2106  * Find the xattr extent rec which may contains name_hash.
2107  * e_cpos will be the first name hash of the xattr rec.
2108  * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2109  */
2110 static int ocfs2_xattr_get_rec(struct inode *inode,
2111 			       u32 name_hash,
2112 			       u64 *p_blkno,
2113 			       u32 *e_cpos,
2114 			       u32 *num_clusters,
2115 			       struct ocfs2_extent_list *el)
2116 {
2117 	int ret = 0, i;
2118 	struct buffer_head *eb_bh = NULL;
2119 	struct ocfs2_extent_block *eb;
2120 	struct ocfs2_extent_rec *rec = NULL;
2121 	u64 e_blkno = 0;
2122 
2123 	if (el->l_tree_depth) {
2124 		ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2125 		if (ret) {
2126 			mlog_errno(ret);
2127 			goto out;
2128 		}
2129 
2130 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2131 		el = &eb->h_list;
2132 
2133 		if (el->l_tree_depth) {
2134 			ocfs2_error(inode->i_sb,
2135 				    "Inode %lu has non zero tree depth in "
2136 				    "xattr tree block %llu\n", inode->i_ino,
2137 				    (unsigned long long)eb_bh->b_blocknr);
2138 			ret = -EROFS;
2139 			goto out;
2140 		}
2141 	}
2142 
2143 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2144 		rec = &el->l_recs[i];
2145 
2146 		if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2147 			e_blkno = le64_to_cpu(rec->e_blkno);
2148 			break;
2149 		}
2150 	}
2151 
2152 	if (!e_blkno) {
2153 		ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2154 			    "record (%u, %u, 0) in xattr", inode->i_ino,
2155 			    le32_to_cpu(rec->e_cpos),
2156 			    ocfs2_rec_clusters(el, rec));
2157 		ret = -EROFS;
2158 		goto out;
2159 	}
2160 
2161 	*p_blkno = le64_to_cpu(rec->e_blkno);
2162 	*num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2163 	if (e_cpos)
2164 		*e_cpos = le32_to_cpu(rec->e_cpos);
2165 out:
2166 	brelse(eb_bh);
2167 	return ret;
2168 }
2169 
2170 typedef int (xattr_bucket_func)(struct inode *inode,
2171 				struct ocfs2_xattr_bucket *bucket,
2172 				void *para);
2173 
2174 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2175 				   struct buffer_head *header_bh,
2176 				   int name_index,
2177 				   const char *name,
2178 				   u32 name_hash,
2179 				   u16 *xe_index,
2180 				   int *found)
2181 {
2182 	int i, ret = 0, cmp = 1, block_off, new_offset;
2183 	struct ocfs2_xattr_header *xh =
2184 			(struct ocfs2_xattr_header *)header_bh->b_data;
2185 	size_t name_len = strlen(name);
2186 	struct ocfs2_xattr_entry *xe = NULL;
2187 	struct buffer_head *name_bh = NULL;
2188 	char *xe_name;
2189 
2190 	/*
2191 	 * We don't use binary search in the bucket because there
2192 	 * may be multiple entries with the same name hash.
2193 	 */
2194 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2195 		xe = &xh->xh_entries[i];
2196 
2197 		if (name_hash > le32_to_cpu(xe->xe_name_hash))
2198 			continue;
2199 		else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2200 			break;
2201 
2202 		cmp = name_index - ocfs2_xattr_get_type(xe);
2203 		if (!cmp)
2204 			cmp = name_len - xe->xe_name_len;
2205 		if (cmp)
2206 			continue;
2207 
2208 		ret = ocfs2_xattr_bucket_get_name_value(inode,
2209 							xh,
2210 							i,
2211 							&block_off,
2212 							&new_offset);
2213 		if (ret) {
2214 			mlog_errno(ret);
2215 			break;
2216 		}
2217 
2218 		ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2219 				       header_bh->b_blocknr + block_off,
2220 				       &name_bh, OCFS2_BH_CACHED, inode);
2221 		if (ret) {
2222 			mlog_errno(ret);
2223 			break;
2224 		}
2225 		xe_name = name_bh->b_data + new_offset;
2226 
2227 		cmp = memcmp(name, xe_name, name_len);
2228 		brelse(name_bh);
2229 		name_bh = NULL;
2230 
2231 		if (cmp == 0) {
2232 			*xe_index = i;
2233 			*found = 1;
2234 			ret = 0;
2235 			break;
2236 		}
2237 	}
2238 
2239 	return ret;
2240 }
2241 
2242 /*
2243  * Find the specified xattr entry in a series of buckets.
2244  * This series start from p_blkno and last for num_clusters.
2245  * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2246  * the num of the valid buckets.
2247  *
2248  * Return the buffer_head this xattr should reside in. And if the xattr's
2249  * hash is in the gap of 2 buckets, return the lower bucket.
2250  */
2251 static int ocfs2_xattr_bucket_find(struct inode *inode,
2252 				   int name_index,
2253 				   const char *name,
2254 				   u32 name_hash,
2255 				   u64 p_blkno,
2256 				   u32 first_hash,
2257 				   u32 num_clusters,
2258 				   struct ocfs2_xattr_search *xs)
2259 {
2260 	int ret, found = 0;
2261 	struct buffer_head *bh = NULL;
2262 	struct buffer_head *lower_bh = NULL;
2263 	struct ocfs2_xattr_header *xh = NULL;
2264 	struct ocfs2_xattr_entry *xe = NULL;
2265 	u16 index = 0;
2266 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2267 	int low_bucket = 0, bucket, high_bucket;
2268 	u32 last_hash;
2269 	u64 blkno;
2270 
2271 	ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2272 			       &bh, OCFS2_BH_CACHED, inode);
2273 	if (ret) {
2274 		mlog_errno(ret);
2275 		goto out;
2276 	}
2277 
2278 	xh = (struct ocfs2_xattr_header *)bh->b_data;
2279 	high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2280 
2281 	while (low_bucket <= high_bucket) {
2282 		brelse(bh);
2283 		bh = NULL;
2284 		bucket = (low_bucket + high_bucket) / 2;
2285 
2286 		blkno = p_blkno + bucket * blk_per_bucket;
2287 
2288 		ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
2289 				       &bh, OCFS2_BH_CACHED, inode);
2290 		if (ret) {
2291 			mlog_errno(ret);
2292 			goto out;
2293 		}
2294 
2295 		xh = (struct ocfs2_xattr_header *)bh->b_data;
2296 		xe = &xh->xh_entries[0];
2297 		if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2298 			high_bucket = bucket - 1;
2299 			continue;
2300 		}
2301 
2302 		/*
2303 		 * Check whether the hash of the last entry in our
2304 		 * bucket is larger than the search one.
2305 		 */
2306 		xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2307 		last_hash = le32_to_cpu(xe->xe_name_hash);
2308 
2309 		/* record lower_bh which may be the insert place. */
2310 		brelse(lower_bh);
2311 		lower_bh = bh;
2312 		bh = NULL;
2313 
2314 		if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2315 			low_bucket = bucket + 1;
2316 			continue;
2317 		}
2318 
2319 		/* the searched xattr should reside in this bucket if exists. */
2320 		ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2321 					      name_index, name, name_hash,
2322 					      &index, &found);
2323 		if (ret) {
2324 			mlog_errno(ret);
2325 			goto out;
2326 		}
2327 		break;
2328 	}
2329 
2330 	/*
2331 	 * Record the bucket we have found.
2332 	 * When the xattr's hash value is in the gap of 2 buckets, we will
2333 	 * always set it to the previous bucket.
2334 	 */
2335 	if (!lower_bh) {
2336 		/*
2337 		 * We can't find any bucket whose first name_hash is less
2338 		 * than the find name_hash.
2339 		 */
2340 		BUG_ON(bh->b_blocknr != p_blkno);
2341 		lower_bh = bh;
2342 		bh = NULL;
2343 	}
2344 	xs->bucket.bhs[0] = lower_bh;
2345 	xs->bucket.xh = (struct ocfs2_xattr_header *)
2346 					xs->bucket.bhs[0]->b_data;
2347 	lower_bh = NULL;
2348 
2349 	xs->header = xs->bucket.xh;
2350 	xs->base = xs->bucket.bhs[0]->b_data;
2351 	xs->end = xs->base + inode->i_sb->s_blocksize;
2352 
2353 	if (found) {
2354 		/*
2355 		 * If we have found the xattr enty, read all the blocks in
2356 		 * this bucket.
2357 		 */
2358 		ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2359 					xs->bucket.bhs[0]->b_blocknr + 1,
2360 					blk_per_bucket - 1, &xs->bucket.bhs[1],
2361 					OCFS2_BH_CACHED, inode);
2362 		if (ret) {
2363 			mlog_errno(ret);
2364 			goto out;
2365 		}
2366 
2367 		xs->here = &xs->header->xh_entries[index];
2368 		mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2369 		     (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2370 	} else
2371 		ret = -ENODATA;
2372 
2373 out:
2374 	brelse(bh);
2375 	brelse(lower_bh);
2376 	return ret;
2377 }
2378 
2379 static int ocfs2_xattr_index_block_find(struct inode *inode,
2380 					struct buffer_head *root_bh,
2381 					int name_index,
2382 					const char *name,
2383 					struct ocfs2_xattr_search *xs)
2384 {
2385 	int ret;
2386 	struct ocfs2_xattr_block *xb =
2387 			(struct ocfs2_xattr_block *)root_bh->b_data;
2388 	struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2389 	struct ocfs2_extent_list *el = &xb_root->xt_list;
2390 	u64 p_blkno = 0;
2391 	u32 first_hash, num_clusters = 0;
2392 	u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name);
2393 
2394 	if (le16_to_cpu(el->l_next_free_rec) == 0)
2395 		return -ENODATA;
2396 
2397 	mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2398 	     name, name_hash, name_index);
2399 
2400 	ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2401 				  &num_clusters, el);
2402 	if (ret) {
2403 		mlog_errno(ret);
2404 		goto out;
2405 	}
2406 
2407 	BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2408 
2409 	mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2410 	     "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2411 
2412 	ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2413 				      p_blkno, first_hash, num_clusters, xs);
2414 
2415 out:
2416 	return ret;
2417 }
2418 
2419 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2420 				       u64 blkno,
2421 				       u32 clusters,
2422 				       xattr_bucket_func *func,
2423 				       void *para)
2424 {
2425 	int i, j, ret = 0;
2426 	int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2427 	u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2428 	u32 num_buckets = clusters * bpc;
2429 	struct ocfs2_xattr_bucket bucket;
2430 
2431 	memset(&bucket, 0, sizeof(bucket));
2432 
2433 	mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2434 	     clusters, blkno);
2435 
2436 	for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2437 		ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2438 					blkno, blk_per_bucket,
2439 					bucket.bhs, OCFS2_BH_CACHED, inode);
2440 		if (ret) {
2441 			mlog_errno(ret);
2442 			goto out;
2443 		}
2444 
2445 		bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2446 		/*
2447 		 * The real bucket num in this series of blocks is stored
2448 		 * in the 1st bucket.
2449 		 */
2450 		if (i == 0)
2451 			num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2452 
2453 		mlog(0, "iterating xattr bucket %llu\n", blkno);
2454 		if (func) {
2455 			ret = func(inode, &bucket, para);
2456 			if (ret) {
2457 				mlog_errno(ret);
2458 				break;
2459 			}
2460 		}
2461 
2462 		for (j = 0; j < blk_per_bucket; j++)
2463 			brelse(bucket.bhs[j]);
2464 		memset(&bucket, 0, sizeof(bucket));
2465 	}
2466 
2467 out:
2468 	for (j = 0; j < blk_per_bucket; j++)
2469 		brelse(bucket.bhs[j]);
2470 
2471 	return ret;
2472 }
2473 
2474 struct ocfs2_xattr_tree_list {
2475 	char *buffer;
2476 	size_t buffer_size;
2477 };
2478 
2479 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2480 					     struct ocfs2_xattr_header *xh,
2481 					     int index,
2482 					     int *block_off,
2483 					     int *new_offset)
2484 {
2485 	u16 name_offset;
2486 
2487 	if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2488 		return -EINVAL;
2489 
2490 	name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2491 
2492 	*block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2493 	*new_offset = name_offset % inode->i_sb->s_blocksize;
2494 
2495 	return 0;
2496 }
2497 
2498 static int ocfs2_list_xattr_bucket(struct inode *inode,
2499 				   struct ocfs2_xattr_bucket *bucket,
2500 				   void *para)
2501 {
2502 	int ret = 0;
2503 	struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2504 	size_t size;
2505 	int i, block_off, new_offset;
2506 
2507 	for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2508 		struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2509 		struct xattr_handler *handler =
2510 			ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2511 
2512 		if (handler) {
2513 			ret = ocfs2_xattr_bucket_get_name_value(inode,
2514 								bucket->xh,
2515 								i,
2516 								&block_off,
2517 								&new_offset);
2518 			if (ret)
2519 				break;
2520 			size = handler->list(inode, xl->buffer, xl->buffer_size,
2521 					     bucket->bhs[block_off]->b_data +
2522 					     new_offset,
2523 					     entry->xe_name_len);
2524 			if (xl->buffer) {
2525 				if (size > xl->buffer_size)
2526 					return -ERANGE;
2527 				xl->buffer += size;
2528 			}
2529 			xl->buffer_size -= size;
2530 		}
2531 	}
2532 
2533 	return ret;
2534 }
2535 
2536 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2537 					     struct ocfs2_xattr_tree_root *xt,
2538 					     char *buffer,
2539 					     size_t buffer_size)
2540 {
2541 	struct ocfs2_extent_list *el = &xt->xt_list;
2542 	int ret = 0;
2543 	u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2544 	u64 p_blkno = 0;
2545 	struct ocfs2_xattr_tree_list xl = {
2546 		.buffer = buffer,
2547 		.buffer_size = buffer_size,
2548 	};
2549 
2550 	if (le16_to_cpu(el->l_next_free_rec) == 0)
2551 		return 0;
2552 
2553 	while (name_hash > 0) {
2554 		ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2555 					  &e_cpos, &num_clusters, el);
2556 		if (ret) {
2557 			mlog_errno(ret);
2558 			goto out;
2559 		}
2560 
2561 		ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2562 						  ocfs2_list_xattr_bucket,
2563 						  &xl);
2564 		if (ret) {
2565 			mlog_errno(ret);
2566 			goto out;
2567 		}
2568 
2569 		if (e_cpos == 0)
2570 			break;
2571 
2572 		name_hash = e_cpos - 1;
2573 	}
2574 
2575 	ret = buffer_size - xl.buffer_size;
2576 out:
2577 	return ret;
2578 }
2579 
2580 static int cmp_xe(const void *a, const void *b)
2581 {
2582 	const struct ocfs2_xattr_entry *l = a, *r = b;
2583 	u32 l_hash = le32_to_cpu(l->xe_name_hash);
2584 	u32 r_hash = le32_to_cpu(r->xe_name_hash);
2585 
2586 	if (l_hash > r_hash)
2587 		return 1;
2588 	if (l_hash < r_hash)
2589 		return -1;
2590 	return 0;
2591 }
2592 
2593 static void swap_xe(void *a, void *b, int size)
2594 {
2595 	struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2596 
2597 	tmp = *l;
2598 	memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2599 	memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2600 }
2601 
2602 /*
2603  * When the ocfs2_xattr_block is filled up, new bucket will be created
2604  * and all the xattr entries will be moved to the new bucket.
2605  * Note: we need to sort the entries since they are not saved in order
2606  * in the ocfs2_xattr_block.
2607  */
2608 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2609 					   struct buffer_head *xb_bh,
2610 					   struct buffer_head *xh_bh,
2611 					   struct buffer_head *data_bh)
2612 {
2613 	int i, blocksize = inode->i_sb->s_blocksize;
2614 	u16 offset, size, off_change;
2615 	struct ocfs2_xattr_entry *xe;
2616 	struct ocfs2_xattr_block *xb =
2617 				(struct ocfs2_xattr_block *)xb_bh->b_data;
2618 	struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2619 	struct ocfs2_xattr_header *xh =
2620 				(struct ocfs2_xattr_header *)xh_bh->b_data;
2621 	u16 count = le16_to_cpu(xb_xh->xh_count);
2622 	char *target = xh_bh->b_data, *src = xb_bh->b_data;
2623 
2624 	mlog(0, "cp xattr from block %llu to bucket %llu\n",
2625 	     (unsigned long long)xb_bh->b_blocknr,
2626 	     (unsigned long long)xh_bh->b_blocknr);
2627 
2628 	memset(xh_bh->b_data, 0, blocksize);
2629 	if (data_bh)
2630 		memset(data_bh->b_data, 0, blocksize);
2631 	/*
2632 	 * Since the xe_name_offset is based on ocfs2_xattr_header,
2633 	 * there is a offset change corresponding to the change of
2634 	 * ocfs2_xattr_header's position.
2635 	 */
2636 	off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2637 	xe = &xb_xh->xh_entries[count - 1];
2638 	offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2639 	size = blocksize - offset;
2640 
2641 	/* copy all the names and values. */
2642 	if (data_bh)
2643 		target = data_bh->b_data;
2644 	memcpy(target + offset, src + offset, size);
2645 
2646 	/* Init new header now. */
2647 	xh->xh_count = xb_xh->xh_count;
2648 	xh->xh_num_buckets = cpu_to_le16(1);
2649 	xh->xh_name_value_len = cpu_to_le16(size);
2650 	xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2651 
2652 	/* copy all the entries. */
2653 	target = xh_bh->b_data;
2654 	offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2655 	size = count * sizeof(struct ocfs2_xattr_entry);
2656 	memcpy(target + offset, (char *)xb_xh + offset, size);
2657 
2658 	/* Change the xe offset for all the xe because of the move. */
2659 	off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2660 		 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2661 	for (i = 0; i < count; i++)
2662 		le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2663 
2664 	mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2665 	     offset, size, off_change);
2666 
2667 	sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2668 	     cmp_xe, swap_xe);
2669 }
2670 
2671 /*
2672  * After we move xattr from block to index btree, we have to
2673  * update ocfs2_xattr_search to the new xe and base.
2674  *
2675  * When the entry is in xattr block, xattr_bh indicates the storage place.
2676  * While if the entry is in index b-tree, "bucket" indicates the
2677  * real place of the xattr.
2678  */
2679 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2680 					   struct ocfs2_xattr_search *xs,
2681 					   struct buffer_head *old_bh,
2682 					   struct buffer_head *new_bh)
2683 {
2684 	int ret = 0;
2685 	char *buf = old_bh->b_data;
2686 	struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2687 	struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2688 	int i, blocksize = inode->i_sb->s_blocksize;
2689 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2690 
2691 	xs->bucket.bhs[0] = new_bh;
2692 	get_bh(new_bh);
2693 	xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2694 	xs->header = xs->bucket.xh;
2695 
2696 	xs->base = new_bh->b_data;
2697 	xs->end = xs->base + inode->i_sb->s_blocksize;
2698 
2699 	if (!xs->not_found) {
2700 		if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2701 			ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2702 					xs->bucket.bhs[0]->b_blocknr + 1,
2703 					blk_per_bucket - 1, &xs->bucket.bhs[1],
2704 					OCFS2_BH_CACHED, inode);
2705 			if (ret) {
2706 				mlog_errno(ret);
2707 				return ret;
2708 			}
2709 
2710 			i = xs->here - old_xh->xh_entries;
2711 			xs->here = &xs->header->xh_entries[i];
2712 		}
2713 	}
2714 
2715 	return ret;
2716 }
2717 
2718 static int ocfs2_xattr_create_index_block(struct inode *inode,
2719 					  struct ocfs2_xattr_search *xs)
2720 {
2721 	int ret, credits = OCFS2_SUBALLOC_ALLOC;
2722 	u32 bit_off, len;
2723 	u64 blkno;
2724 	handle_t *handle;
2725 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2726 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2727 	struct ocfs2_alloc_context *data_ac;
2728 	struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2729 	struct buffer_head *xb_bh = xs->xattr_bh;
2730 	struct ocfs2_xattr_block *xb =
2731 			(struct ocfs2_xattr_block *)xb_bh->b_data;
2732 	struct ocfs2_xattr_tree_root *xr;
2733 	u16 xb_flags = le16_to_cpu(xb->xb_flags);
2734 	u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2735 
2736 	mlog(0, "create xattr index block for %llu\n",
2737 	     (unsigned long long)xb_bh->b_blocknr);
2738 
2739 	BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2740 
2741 	ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2742 	if (ret) {
2743 		mlog_errno(ret);
2744 		goto out;
2745 	}
2746 
2747 	/*
2748 	 * XXX:
2749 	 * We can use this lock for now, and maybe move to a dedicated mutex
2750 	 * if performance becomes a problem later.
2751 	 */
2752 	down_write(&oi->ip_alloc_sem);
2753 
2754 	/*
2755 	 * 3 more credits, one for xattr block update, one for the 1st block
2756 	 * of the new xattr bucket and one for the value/data.
2757 	 */
2758 	credits += 3;
2759 	handle = ocfs2_start_trans(osb, credits);
2760 	if (IS_ERR(handle)) {
2761 		ret = PTR_ERR(handle);
2762 		mlog_errno(ret);
2763 		goto out_sem;
2764 	}
2765 
2766 	ret = ocfs2_journal_access(handle, inode, xb_bh,
2767 				   OCFS2_JOURNAL_ACCESS_WRITE);
2768 	if (ret) {
2769 		mlog_errno(ret);
2770 		goto out_commit;
2771 	}
2772 
2773 	ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2774 	if (ret) {
2775 		mlog_errno(ret);
2776 		goto out_commit;
2777 	}
2778 
2779 	/*
2780 	 * The bucket may spread in many blocks, and
2781 	 * we will only touch the 1st block and the last block
2782 	 * in the whole bucket(one for entry and one for data).
2783 	 */
2784 	blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2785 
2786 	mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2787 
2788 	xh_bh = sb_getblk(inode->i_sb, blkno);
2789 	if (!xh_bh) {
2790 		ret = -EIO;
2791 		mlog_errno(ret);
2792 		goto out_commit;
2793 	}
2794 
2795 	ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2796 
2797 	ret = ocfs2_journal_access(handle, inode, xh_bh,
2798 				   OCFS2_JOURNAL_ACCESS_CREATE);
2799 	if (ret) {
2800 		mlog_errno(ret);
2801 		goto out_commit;
2802 	}
2803 
2804 	if (bpb > 1) {
2805 		data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2806 		if (!data_bh) {
2807 			ret = -EIO;
2808 			mlog_errno(ret);
2809 			goto out_commit;
2810 		}
2811 
2812 		ocfs2_set_new_buffer_uptodate(inode, data_bh);
2813 
2814 		ret = ocfs2_journal_access(handle, inode, data_bh,
2815 					   OCFS2_JOURNAL_ACCESS_CREATE);
2816 		if (ret) {
2817 			mlog_errno(ret);
2818 			goto out_commit;
2819 		}
2820 	}
2821 
2822 	ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2823 
2824 	ocfs2_journal_dirty(handle, xh_bh);
2825 	if (data_bh)
2826 		ocfs2_journal_dirty(handle, data_bh);
2827 
2828 	ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2829 
2830 	/* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2831 	memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2832 	       offsetof(struct ocfs2_xattr_block, xb_attrs));
2833 
2834 	xr = &xb->xb_attrs.xb_root;
2835 	xr->xt_clusters = cpu_to_le32(1);
2836 	xr->xt_last_eb_blk = 0;
2837 	xr->xt_list.l_tree_depth = 0;
2838 	xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2839 	xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2840 
2841 	xr->xt_list.l_recs[0].e_cpos = 0;
2842 	xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2843 	xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2844 
2845 	xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2846 
2847 	ret = ocfs2_journal_dirty(handle, xb_bh);
2848 	if (ret) {
2849 		mlog_errno(ret);
2850 		goto out_commit;
2851 	}
2852 
2853 out_commit:
2854 	ocfs2_commit_trans(osb, handle);
2855 
2856 out_sem:
2857 	up_write(&oi->ip_alloc_sem);
2858 
2859 out:
2860 	if (data_ac)
2861 		ocfs2_free_alloc_context(data_ac);
2862 
2863 	brelse(xh_bh);
2864 	brelse(data_bh);
2865 
2866 	return ret;
2867 }
2868 
2869 static int cmp_xe_offset(const void *a, const void *b)
2870 {
2871 	const struct ocfs2_xattr_entry *l = a, *r = b;
2872 	u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2873 	u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2874 
2875 	if (l_name_offset < r_name_offset)
2876 		return 1;
2877 	if (l_name_offset > r_name_offset)
2878 		return -1;
2879 	return 0;
2880 }
2881 
2882 /*
2883  * defrag a xattr bucket if we find that the bucket has some
2884  * holes beteen name/value pairs.
2885  * We will move all the name/value pairs to the end of the bucket
2886  * so that we can spare some space for insertion.
2887  */
2888 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2889 				     struct ocfs2_xattr_bucket *bucket)
2890 {
2891 	int ret, i;
2892 	size_t end, offset, len, value_len;
2893 	struct ocfs2_xattr_header *xh;
2894 	char *entries, *buf, *bucket_buf = NULL;
2895 	u64 blkno = bucket->bhs[0]->b_blocknr;
2896 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2897 	u16 xh_free_start;
2898 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2899 	size_t blocksize = inode->i_sb->s_blocksize;
2900 	handle_t *handle;
2901 	struct buffer_head **bhs;
2902 	struct ocfs2_xattr_entry *xe;
2903 
2904 	bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2905 			GFP_NOFS);
2906 	if (!bhs)
2907 		return -ENOMEM;
2908 
2909 	ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
2910 				OCFS2_BH_CACHED, inode);
2911 	if (ret)
2912 		goto out;
2913 
2914 	/*
2915 	 * In order to make the operation more efficient and generic,
2916 	 * we copy all the blocks into a contiguous memory and do the
2917 	 * defragment there, so if anything is error, we will not touch
2918 	 * the real block.
2919 	 */
2920 	bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2921 	if (!bucket_buf) {
2922 		ret = -EIO;
2923 		goto out;
2924 	}
2925 
2926 	buf = bucket_buf;
2927 	for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2928 		memcpy(buf, bhs[i]->b_data, blocksize);
2929 
2930 	handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2931 	if (IS_ERR(handle)) {
2932 		ret = PTR_ERR(handle);
2933 		handle = NULL;
2934 		mlog_errno(ret);
2935 		goto out;
2936 	}
2937 
2938 	for (i = 0; i < blk_per_bucket; i++) {
2939 		ret = ocfs2_journal_access(handle, inode, bhs[i],
2940 					   OCFS2_JOURNAL_ACCESS_WRITE);
2941 		if (ret < 0) {
2942 			mlog_errno(ret);
2943 			goto commit;
2944 		}
2945 	}
2946 
2947 	xh = (struct ocfs2_xattr_header *)bucket_buf;
2948 	entries = (char *)xh->xh_entries;
2949 	xh_free_start = le16_to_cpu(xh->xh_free_start);
2950 
2951 	mlog(0, "adjust xattr bucket in %llu, count = %u, "
2952 	     "xh_free_start = %u, xh_name_value_len = %u.\n",
2953 	     blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2954 	     le16_to_cpu(xh->xh_name_value_len));
2955 
2956 	/*
2957 	 * sort all the entries by their offset.
2958 	 * the largest will be the first, so that we can
2959 	 * move them to the end one by one.
2960 	 */
2961 	sort(entries, le16_to_cpu(xh->xh_count),
2962 	     sizeof(struct ocfs2_xattr_entry),
2963 	     cmp_xe_offset, swap_xe);
2964 
2965 	/* Move all name/values to the end of the bucket. */
2966 	xe = xh->xh_entries;
2967 	end = OCFS2_XATTR_BUCKET_SIZE;
2968 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2969 		offset = le16_to_cpu(xe->xe_name_offset);
2970 		if (ocfs2_xattr_is_local(xe))
2971 			value_len = OCFS2_XATTR_SIZE(
2972 					le64_to_cpu(xe->xe_value_size));
2973 		else
2974 			value_len = OCFS2_XATTR_ROOT_SIZE;
2975 		len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2976 
2977 		/*
2978 		 * We must make sure that the name/value pair
2979 		 * exist in the same block. So adjust end to
2980 		 * the previous block end if needed.
2981 		 */
2982 		if (((end - len) / blocksize !=
2983 			(end - 1) / blocksize))
2984 			end = end - end % blocksize;
2985 
2986 		if (end > offset + len) {
2987 			memmove(bucket_buf + end - len,
2988 				bucket_buf + offset, len);
2989 			xe->xe_name_offset = cpu_to_le16(end - len);
2990 		}
2991 
2992 		mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2993 				"bucket %llu\n", (unsigned long long)blkno);
2994 
2995 		end -= len;
2996 	}
2997 
2998 	mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2999 			"bucket %llu\n", (unsigned long long)blkno);
3000 
3001 	if (xh_free_start == end)
3002 		goto commit;
3003 
3004 	memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3005 	xh->xh_free_start = cpu_to_le16(end);
3006 
3007 	/* sort the entries by their name_hash. */
3008 	sort(entries, le16_to_cpu(xh->xh_count),
3009 	     sizeof(struct ocfs2_xattr_entry),
3010 	     cmp_xe, swap_xe);
3011 
3012 	buf = bucket_buf;
3013 	for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3014 		memcpy(bhs[i]->b_data, buf, blocksize);
3015 		ocfs2_journal_dirty(handle, bhs[i]);
3016 	}
3017 
3018 commit:
3019 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3020 out:
3021 
3022 	if (bhs) {
3023 		for (i = 0; i < blk_per_bucket; i++)
3024 			brelse(bhs[i]);
3025 	}
3026 	kfree(bhs);
3027 
3028 	kfree(bucket_buf);
3029 	return ret;
3030 }
3031 
3032 /*
3033  * Move half nums of the xattr bucket in the previous cluster to this new
3034  * cluster. We only touch the last cluster of the previous extend record.
3035  *
3036  * first_bh is the first buffer_head of a series of bucket in the same
3037  * extent rec and header_bh is the header of one bucket in this cluster.
3038  * They will be updated if we move the data header_bh contains to the new
3039  * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3040  */
3041 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3042 					       handle_t *handle,
3043 					       struct buffer_head **first_bh,
3044 					       struct buffer_head **header_bh,
3045 					       u64 new_blkno,
3046 					       u64 prev_blkno,
3047 					       u32 num_clusters,
3048 					       u32 *first_hash)
3049 {
3050 	int i, ret, credits;
3051 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3052 	int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3053 	int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3054 	int blocksize = inode->i_sb->s_blocksize;
3055 	struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3056 	struct ocfs2_xattr_header *new_xh;
3057 	struct ocfs2_xattr_header *xh =
3058 			(struct ocfs2_xattr_header *)((*first_bh)->b_data);
3059 
3060 	BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3061 	BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3062 
3063 	prev_bh = *first_bh;
3064 	get_bh(prev_bh);
3065 	xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3066 
3067 	prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3068 
3069 	mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3070 	     prev_blkno, new_blkno);
3071 
3072 	/*
3073 	 * We need to update the 1st half of the new cluster and
3074 	 * 1 more for the update of the 1st bucket of the previous
3075 	 * extent record.
3076 	 */
3077 	credits = bpc / 2 + 1;
3078 	ret = ocfs2_extend_trans(handle, credits);
3079 	if (ret) {
3080 		mlog_errno(ret);
3081 		goto out;
3082 	}
3083 
3084 	ret = ocfs2_journal_access(handle, inode, prev_bh,
3085 				   OCFS2_JOURNAL_ACCESS_WRITE);
3086 	if (ret) {
3087 		mlog_errno(ret);
3088 		goto out;
3089 	}
3090 
3091 	for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3092 		old_bh = new_bh = NULL;
3093 		new_bh = sb_getblk(inode->i_sb, new_blkno);
3094 		if (!new_bh) {
3095 			ret = -EIO;
3096 			mlog_errno(ret);
3097 			goto out;
3098 		}
3099 
3100 		ocfs2_set_new_buffer_uptodate(inode, new_bh);
3101 
3102 		ret = ocfs2_journal_access(handle, inode, new_bh,
3103 					   OCFS2_JOURNAL_ACCESS_CREATE);
3104 		if (ret < 0) {
3105 			mlog_errno(ret);
3106 			brelse(new_bh);
3107 			goto out;
3108 		}
3109 
3110 		ret = ocfs2_read_block(osb, prev_blkno,
3111 				       &old_bh, OCFS2_BH_CACHED, inode);
3112 		if (ret < 0) {
3113 			mlog_errno(ret);
3114 			brelse(new_bh);
3115 			goto out;
3116 		}
3117 
3118 		memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3119 
3120 		if (i == 0) {
3121 			new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3122 			new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3123 
3124 			if (first_hash)
3125 				*first_hash = le32_to_cpu(
3126 					new_xh->xh_entries[0].xe_name_hash);
3127 			new_first_bh = new_bh;
3128 			get_bh(new_first_bh);
3129 		}
3130 
3131 		ocfs2_journal_dirty(handle, new_bh);
3132 
3133 		if (*header_bh == old_bh) {
3134 			brelse(*header_bh);
3135 			*header_bh = new_bh;
3136 			get_bh(*header_bh);
3137 
3138 			brelse(*first_bh);
3139 			*first_bh = new_first_bh;
3140 			get_bh(*first_bh);
3141 		}
3142 		brelse(new_bh);
3143 		brelse(old_bh);
3144 	}
3145 
3146 	le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3147 
3148 	ocfs2_journal_dirty(handle, prev_bh);
3149 out:
3150 	brelse(prev_bh);
3151 	brelse(new_first_bh);
3152 	return ret;
3153 }
3154 
3155 static int ocfs2_read_xattr_bucket(struct inode *inode,
3156 				   u64 blkno,
3157 				   struct buffer_head **bhs,
3158 				   int new)
3159 {
3160 	int ret = 0;
3161 	u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3162 
3163 	if (!new)
3164 		return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
3165 					 blk_per_bucket, bhs,
3166 					 OCFS2_BH_CACHED, inode);
3167 
3168 	for (i = 0; i < blk_per_bucket; i++) {
3169 		bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3170 		if (bhs[i] == NULL) {
3171 			ret = -EIO;
3172 			mlog_errno(ret);
3173 			break;
3174 		}
3175 		ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3176 	}
3177 
3178 	return ret;
3179 }
3180 
3181 /*
3182  * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3183  * first_hash will record the 1st hash of the new bucket.
3184  */
3185 static int ocfs2_half_xattr_bucket(struct inode *inode,
3186 				   handle_t *handle,
3187 				   u64 blk,
3188 				   u64 new_blk,
3189 				   u32 *first_hash,
3190 				   int new_bucket_head)
3191 {
3192 	int ret, i;
3193 	u16 count, start, len, name_value_len, xe_len, name_offset;
3194 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3195 	struct buffer_head **s_bhs, **t_bhs = NULL;
3196 	struct ocfs2_xattr_header *xh;
3197 	struct ocfs2_xattr_entry *xe;
3198 	int blocksize = inode->i_sb->s_blocksize;
3199 
3200 	mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3201 	     blk, new_blk);
3202 
3203 	s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3204 	if (!s_bhs)
3205 		return -ENOMEM;
3206 
3207 	ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3208 	if (ret) {
3209 		mlog_errno(ret);
3210 		goto out;
3211 	}
3212 
3213 	ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3214 				   OCFS2_JOURNAL_ACCESS_WRITE);
3215 	if (ret) {
3216 		mlog_errno(ret);
3217 		goto out;
3218 	}
3219 
3220 	t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3221 	if (!t_bhs) {
3222 		ret = -ENOMEM;
3223 		goto out;
3224 	}
3225 
3226 	ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3227 	if (ret) {
3228 		mlog_errno(ret);
3229 		goto out;
3230 	}
3231 
3232 	for (i = 0; i < blk_per_bucket; i++) {
3233 		ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3234 					   OCFS2_JOURNAL_ACCESS_CREATE);
3235 		if (ret) {
3236 			mlog_errno(ret);
3237 			goto out;
3238 		}
3239 	}
3240 
3241 	/* copy the whole bucket to the new first. */
3242 	for (i = 0; i < blk_per_bucket; i++)
3243 		memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3244 
3245 	/* update the new bucket. */
3246 	xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3247 	count = le16_to_cpu(xh->xh_count);
3248 	start = count / 2;
3249 
3250 	/*
3251 	 * Calculate the total name/value len and xh_free_start for
3252 	 * the old bucket first.
3253 	 */
3254 	name_offset = OCFS2_XATTR_BUCKET_SIZE;
3255 	name_value_len = 0;
3256 	for (i = 0; i < start; i++) {
3257 		xe = &xh->xh_entries[i];
3258 		xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3259 		if (ocfs2_xattr_is_local(xe))
3260 			xe_len +=
3261 			   OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3262 		else
3263 			xe_len += OCFS2_XATTR_ROOT_SIZE;
3264 		name_value_len += xe_len;
3265 		if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3266 			name_offset = le16_to_cpu(xe->xe_name_offset);
3267 	}
3268 
3269 	/*
3270 	 * Now begin the modification to the new bucket.
3271 	 *
3272 	 * In the new bucket, We just move the xattr entry to the beginning
3273 	 * and don't touch the name/value. So there will be some holes in the
3274 	 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3275 	 * called.
3276 	 */
3277 	xe = &xh->xh_entries[start];
3278 	len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3279 	mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3280 	     (int)((char *)xe - (char *)xh),
3281 	     (int)((char *)xh->xh_entries - (char *)xh));
3282 	memmove((char *)xh->xh_entries, (char *)xe, len);
3283 	xe = &xh->xh_entries[count - start];
3284 	len = sizeof(struct ocfs2_xattr_entry) * start;
3285 	memset((char *)xe, 0, len);
3286 
3287 	le16_add_cpu(&xh->xh_count, -start);
3288 	le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3289 
3290 	/* Calculate xh_free_start for the new bucket. */
3291 	xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3292 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3293 		xe = &xh->xh_entries[i];
3294 		xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3295 		if (ocfs2_xattr_is_local(xe))
3296 			xe_len +=
3297 			   OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3298 		else
3299 			xe_len += OCFS2_XATTR_ROOT_SIZE;
3300 		if (le16_to_cpu(xe->xe_name_offset) <
3301 		    le16_to_cpu(xh->xh_free_start))
3302 			xh->xh_free_start = xe->xe_name_offset;
3303 	}
3304 
3305 	/* set xh->xh_num_buckets for the new xh. */
3306 	if (new_bucket_head)
3307 		xh->xh_num_buckets = cpu_to_le16(1);
3308 	else
3309 		xh->xh_num_buckets = 0;
3310 
3311 	for (i = 0; i < blk_per_bucket; i++) {
3312 		ocfs2_journal_dirty(handle, t_bhs[i]);
3313 		if (ret)
3314 			mlog_errno(ret);
3315 	}
3316 
3317 	/* store the first_hash of the new bucket. */
3318 	if (first_hash)
3319 		*first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3320 
3321 	/*
3322 	 * Now only update the 1st block of the old bucket.
3323 	 * Please note that the entry has been sorted already above.
3324 	 */
3325 	xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3326 	memset(&xh->xh_entries[start], 0,
3327 	       sizeof(struct ocfs2_xattr_entry) * (count - start));
3328 	xh->xh_count = cpu_to_le16(start);
3329 	xh->xh_free_start = cpu_to_le16(name_offset);
3330 	xh->xh_name_value_len = cpu_to_le16(name_value_len);
3331 
3332 	ocfs2_journal_dirty(handle, s_bhs[0]);
3333 	if (ret)
3334 		mlog_errno(ret);
3335 
3336 out:
3337 	if (s_bhs) {
3338 		for (i = 0; i < blk_per_bucket; i++)
3339 			brelse(s_bhs[i]);
3340 	}
3341 	kfree(s_bhs);
3342 
3343 	if (t_bhs) {
3344 		for (i = 0; i < blk_per_bucket; i++)
3345 			brelse(t_bhs[i]);
3346 	}
3347 	kfree(t_bhs);
3348 
3349 	return ret;
3350 }
3351 
3352 /*
3353  * Copy xattr from one bucket to another bucket.
3354  *
3355  * The caller must make sure that the journal transaction
3356  * has enough space for journaling.
3357  */
3358 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3359 				 handle_t *handle,
3360 				 u64 s_blkno,
3361 				 u64 t_blkno,
3362 				 int t_is_new)
3363 {
3364 	int ret, i;
3365 	int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3366 	int blocksize = inode->i_sb->s_blocksize;
3367 	struct buffer_head **s_bhs, **t_bhs = NULL;
3368 
3369 	BUG_ON(s_blkno == t_blkno);
3370 
3371 	mlog(0, "cp bucket %llu to %llu, target is %d\n",
3372 	     s_blkno, t_blkno, t_is_new);
3373 
3374 	s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3375 			GFP_NOFS);
3376 	if (!s_bhs)
3377 		return -ENOMEM;
3378 
3379 	ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3380 	if (ret)
3381 		goto out;
3382 
3383 	t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3384 			GFP_NOFS);
3385 	if (!t_bhs) {
3386 		ret = -ENOMEM;
3387 		goto out;
3388 	}
3389 
3390 	ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3391 	if (ret)
3392 		goto out;
3393 
3394 	for (i = 0; i < blk_per_bucket; i++) {
3395 		ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3396 					   OCFS2_JOURNAL_ACCESS_WRITE);
3397 		if (ret)
3398 			goto out;
3399 	}
3400 
3401 	for (i = 0; i < blk_per_bucket; i++) {
3402 		memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3403 		ocfs2_journal_dirty(handle, t_bhs[i]);
3404 	}
3405 
3406 out:
3407 	if (s_bhs) {
3408 		for (i = 0; i < blk_per_bucket; i++)
3409 			brelse(s_bhs[i]);
3410 	}
3411 	kfree(s_bhs);
3412 
3413 	if (t_bhs) {
3414 		for (i = 0; i < blk_per_bucket; i++)
3415 			brelse(t_bhs[i]);
3416 	}
3417 	kfree(t_bhs);
3418 
3419 	return ret;
3420 }
3421 
3422 /*
3423  * Copy one xattr cluster from src_blk to to_blk.
3424  * The to_blk will become the first bucket header of the cluster, so its
3425  * xh_num_buckets will be initialized as the bucket num in the cluster.
3426  */
3427 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3428 				  handle_t *handle,
3429 				  struct buffer_head *first_bh,
3430 				  u64 src_blk,
3431 				  u64 to_blk,
3432 				  u32 *first_hash)
3433 {
3434 	int i, ret, credits;
3435 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3436 	int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3437 	int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3438 	struct buffer_head *bh = NULL;
3439 	struct ocfs2_xattr_header *xh;
3440 	u64 to_blk_start = to_blk;
3441 
3442 	mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3443 
3444 	/*
3445 	 * We need to update the new cluster and 1 more for the update of
3446 	 * the 1st bucket of the previous extent rec.
3447 	 */
3448 	credits = bpc + 1;
3449 	ret = ocfs2_extend_trans(handle, credits);
3450 	if (ret) {
3451 		mlog_errno(ret);
3452 		goto out;
3453 	}
3454 
3455 	ret = ocfs2_journal_access(handle, inode, first_bh,
3456 				   OCFS2_JOURNAL_ACCESS_WRITE);
3457 	if (ret) {
3458 		mlog_errno(ret);
3459 		goto out;
3460 	}
3461 
3462 	for (i = 0; i < num_buckets; i++) {
3463 		ret = ocfs2_cp_xattr_bucket(inode, handle,
3464 					    src_blk, to_blk, 1);
3465 		if (ret) {
3466 			mlog_errno(ret);
3467 			goto out;
3468 		}
3469 
3470 		src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3471 		to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3472 	}
3473 
3474 	/* update the old bucket header. */
3475 	xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3476 	le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3477 
3478 	ocfs2_journal_dirty(handle, first_bh);
3479 
3480 	/* update the new bucket header. */
3481 	ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
3482 	if (ret < 0) {
3483 		mlog_errno(ret);
3484 		goto out;
3485 	}
3486 
3487 	ret = ocfs2_journal_access(handle, inode, bh,
3488 				   OCFS2_JOURNAL_ACCESS_WRITE);
3489 	if (ret) {
3490 		mlog_errno(ret);
3491 		goto out;
3492 	}
3493 
3494 	xh = (struct ocfs2_xattr_header *)bh->b_data;
3495 	xh->xh_num_buckets = cpu_to_le16(num_buckets);
3496 
3497 	ocfs2_journal_dirty(handle, bh);
3498 
3499 	if (first_hash)
3500 		*first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3501 out:
3502 	brelse(bh);
3503 	return ret;
3504 }
3505 
3506 /*
3507  * Move half of the xattrs in this cluster to the new cluster.
3508  * This function should only be called when bucket size == cluster size.
3509  * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3510  */
3511 static int ocfs2_half_xattr_cluster(struct inode *inode,
3512 				    handle_t *handle,
3513 				    u64 prev_blk,
3514 				    u64 new_blk,
3515 				    u32 *first_hash)
3516 {
3517 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3518 	int ret, credits = 2 * blk_per_bucket;
3519 
3520 	BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3521 
3522 	ret = ocfs2_extend_trans(handle, credits);
3523 	if (ret) {
3524 		mlog_errno(ret);
3525 		return ret;
3526 	}
3527 
3528 	/* Move half of the xattr in start_blk to the next bucket. */
3529 	return  ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3530 					new_blk, first_hash, 1);
3531 }
3532 
3533 /*
3534  * Move some xattrs from the old cluster to the new one since they are not
3535  * contiguous in ocfs2 xattr tree.
3536  *
3537  * new_blk starts a new separate cluster, and we will move some xattrs from
3538  * prev_blk to it. v_start will be set as the first name hash value in this
3539  * new cluster so that it can be used as e_cpos during tree insertion and
3540  * don't collide with our original b-tree operations. first_bh and header_bh
3541  * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3542  * to extend the insert bucket.
3543  *
3544  * The problem is how much xattr should we move to the new one and when should
3545  * we update first_bh and header_bh?
3546  * 1. If cluster size > bucket size, that means the previous cluster has more
3547  *    than 1 bucket, so just move half nums of bucket into the new cluster and
3548  *    update the first_bh and header_bh if the insert bucket has been moved
3549  *    to the new cluster.
3550  * 2. If cluster_size == bucket_size:
3551  *    a) If the previous extent rec has more than one cluster and the insert
3552  *       place isn't in the last cluster, copy the entire last cluster to the
3553  *       new one. This time, we don't need to upate the first_bh and header_bh
3554  *       since they will not be moved into the new cluster.
3555  *    b) Otherwise, move the bottom half of the xattrs in the last cluster into
3556  *       the new one. And we set the extend flag to zero if the insert place is
3557  *       moved into the new allocated cluster since no extend is needed.
3558  */
3559 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3560 					    handle_t *handle,
3561 					    struct buffer_head **first_bh,
3562 					    struct buffer_head **header_bh,
3563 					    u64 new_blk,
3564 					    u64 prev_blk,
3565 					    u32 prev_clusters,
3566 					    u32 *v_start,
3567 					    int *extend)
3568 {
3569 	int ret = 0;
3570 	int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3571 
3572 	mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3573 	     prev_blk, prev_clusters, new_blk);
3574 
3575 	if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3576 		ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3577 							  handle,
3578 							  first_bh,
3579 							  header_bh,
3580 							  new_blk,
3581 							  prev_blk,
3582 							  prev_clusters,
3583 							  v_start);
3584 	else {
3585 		u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3586 
3587 		if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3588 			ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3589 						     last_blk, new_blk,
3590 						     v_start);
3591 		else {
3592 			ret = ocfs2_half_xattr_cluster(inode, handle,
3593 						       last_blk, new_blk,
3594 						       v_start);
3595 
3596 			if ((*header_bh)->b_blocknr == last_blk && extend)
3597 				*extend = 0;
3598 		}
3599 	}
3600 
3601 	return ret;
3602 }
3603 
3604 /*
3605  * Add a new cluster for xattr storage.
3606  *
3607  * If the new cluster is contiguous with the previous one, it will be
3608  * appended to the same extent record, and num_clusters will be updated.
3609  * If not, we will insert a new extent for it and move some xattrs in
3610  * the last cluster into the new allocated one.
3611  * We also need to limit the maximum size of a btree leaf, otherwise we'll
3612  * lose the benefits of hashing because we'll have to search large leaves.
3613  * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3614  * if it's bigger).
3615  *
3616  * first_bh is the first block of the previous extent rec and header_bh
3617  * indicates the bucket we will insert the new xattrs. They will be updated
3618  * when the header_bh is moved into the new cluster.
3619  */
3620 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3621 				       struct buffer_head *root_bh,
3622 				       struct buffer_head **first_bh,
3623 				       struct buffer_head **header_bh,
3624 				       u32 *num_clusters,
3625 				       u32 prev_cpos,
3626 				       u64 prev_blkno,
3627 				       int *extend)
3628 {
3629 	int ret, credits;
3630 	u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3631 	u32 prev_clusters = *num_clusters;
3632 	u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3633 	u64 block;
3634 	handle_t *handle = NULL;
3635 	struct ocfs2_alloc_context *data_ac = NULL;
3636 	struct ocfs2_alloc_context *meta_ac = NULL;
3637 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3638 	struct ocfs2_extent_tree et;
3639 
3640 	mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3641 	     "previous xattr blkno = %llu\n",
3642 	     (unsigned long long)OCFS2_I(inode)->ip_blkno,
3643 	     prev_cpos, prev_blkno);
3644 
3645 	ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3646 
3647 	ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3648 				    &data_ac, &meta_ac);
3649 	if (ret) {
3650 		mlog_errno(ret);
3651 		goto leave;
3652 	}
3653 
3654 	credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3655 					    clusters_to_add);
3656 	handle = ocfs2_start_trans(osb, credits);
3657 	if (IS_ERR(handle)) {
3658 		ret = PTR_ERR(handle);
3659 		handle = NULL;
3660 		mlog_errno(ret);
3661 		goto leave;
3662 	}
3663 
3664 	ret = ocfs2_journal_access(handle, inode, root_bh,
3665 				   OCFS2_JOURNAL_ACCESS_WRITE);
3666 	if (ret < 0) {
3667 		mlog_errno(ret);
3668 		goto leave;
3669 	}
3670 
3671 	ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3672 				     clusters_to_add, &bit_off, &num_bits);
3673 	if (ret < 0) {
3674 		if (ret != -ENOSPC)
3675 			mlog_errno(ret);
3676 		goto leave;
3677 	}
3678 
3679 	BUG_ON(num_bits > clusters_to_add);
3680 
3681 	block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3682 	mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3683 	     num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3684 
3685 	if (prev_blkno + prev_clusters * bpc == block &&
3686 	    (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3687 	     OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3688 		/*
3689 		 * If this cluster is contiguous with the old one and
3690 		 * adding this new cluster, we don't surpass the limit of
3691 		 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3692 		 * initialized and used like other buckets in the previous
3693 		 * cluster.
3694 		 * So add it as a contiguous one. The caller will handle
3695 		 * its init process.
3696 		 */
3697 		v_start = prev_cpos + prev_clusters;
3698 		*num_clusters = prev_clusters + num_bits;
3699 		mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3700 		     num_bits);
3701 	} else {
3702 		ret = ocfs2_adjust_xattr_cross_cluster(inode,
3703 						       handle,
3704 						       first_bh,
3705 						       header_bh,
3706 						       block,
3707 						       prev_blkno,
3708 						       prev_clusters,
3709 						       &v_start,
3710 						       extend);
3711 		if (ret) {
3712 			mlog_errno(ret);
3713 			goto leave;
3714 		}
3715 	}
3716 
3717 	if (handle->h_buffer_credits < credits) {
3718 		/*
3719 		 * The journal has been restarted before, and don't
3720 		 * have enough space for the insertion, so extend it
3721 		 * here.
3722 		 */
3723 		ret = ocfs2_extend_trans(handle, credits);
3724 		if (ret) {
3725 			mlog_errno(ret);
3726 			goto leave;
3727 		}
3728 	}
3729 	mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3730 	     num_bits, block, v_start);
3731 	ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3732 				  num_bits, 0, meta_ac);
3733 	if (ret < 0) {
3734 		mlog_errno(ret);
3735 		goto leave;
3736 	}
3737 
3738 	ret = ocfs2_journal_dirty(handle, root_bh);
3739 	if (ret < 0) {
3740 		mlog_errno(ret);
3741 		goto leave;
3742 	}
3743 
3744 leave:
3745 	if (handle)
3746 		ocfs2_commit_trans(osb, handle);
3747 	if (data_ac)
3748 		ocfs2_free_alloc_context(data_ac);
3749 	if (meta_ac)
3750 		ocfs2_free_alloc_context(meta_ac);
3751 
3752 	return ret;
3753 }
3754 
3755 /*
3756  * Extend a new xattr bucket and move xattrs to the end one by one until
3757  * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3758  */
3759 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3760 				     struct buffer_head *first_bh,
3761 				     struct buffer_head *start_bh,
3762 				     u32 num_clusters)
3763 {
3764 	int ret, credits;
3765 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3766 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3767 	u64 start_blk = start_bh->b_blocknr, end_blk;
3768 	u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3769 	handle_t *handle;
3770 	struct ocfs2_xattr_header *first_xh =
3771 				(struct ocfs2_xattr_header *)first_bh->b_data;
3772 	u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3773 
3774 	mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3775 	     "from %llu, len = %u\n", start_blk,
3776 	     (unsigned long long)first_bh->b_blocknr, num_clusters);
3777 
3778 	BUG_ON(bucket >= num_buckets);
3779 
3780 	end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3781 
3782 	/*
3783 	 * We will touch all the buckets after the start_bh(include it).
3784 	 * Add one more bucket and modify the first_bh.
3785 	 */
3786 	credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3787 	handle = ocfs2_start_trans(osb, credits);
3788 	if (IS_ERR(handle)) {
3789 		ret = PTR_ERR(handle);
3790 		handle = NULL;
3791 		mlog_errno(ret);
3792 		goto out;
3793 	}
3794 
3795 	ret = ocfs2_journal_access(handle, inode, first_bh,
3796 				   OCFS2_JOURNAL_ACCESS_WRITE);
3797 	if (ret) {
3798 		mlog_errno(ret);
3799 		goto commit;
3800 	}
3801 
3802 	while (end_blk != start_blk) {
3803 		ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3804 					    end_blk + blk_per_bucket, 0);
3805 		if (ret)
3806 			goto commit;
3807 		end_blk -= blk_per_bucket;
3808 	}
3809 
3810 	/* Move half of the xattr in start_blk to the next bucket. */
3811 	ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3812 				      start_blk + blk_per_bucket, NULL, 0);
3813 
3814 	le16_add_cpu(&first_xh->xh_num_buckets, 1);
3815 	ocfs2_journal_dirty(handle, first_bh);
3816 
3817 commit:
3818 	ocfs2_commit_trans(osb, handle);
3819 out:
3820 	return ret;
3821 }
3822 
3823 /*
3824  * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3825  * xb_bh is the ocfs2_xattr_block.
3826  * We will move all the buckets starting from header_bh to the next place. As
3827  * for this one, half num of its xattrs will be moved to the next one.
3828  *
3829  * We will allocate a new cluster if current cluster is full and adjust
3830  * header_bh and first_bh if the insert place is moved to the new cluster.
3831  */
3832 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3833 				      struct buffer_head *xb_bh,
3834 				      struct buffer_head *header_bh)
3835 {
3836 	struct ocfs2_xattr_header *first_xh = NULL;
3837 	struct buffer_head *first_bh = NULL;
3838 	struct ocfs2_xattr_block *xb =
3839 			(struct ocfs2_xattr_block *)xb_bh->b_data;
3840 	struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3841 	struct ocfs2_extent_list *el = &xb_root->xt_list;
3842 	struct ocfs2_xattr_header *xh =
3843 			(struct ocfs2_xattr_header *)header_bh->b_data;
3844 	u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3845 	struct super_block *sb = inode->i_sb;
3846 	struct ocfs2_super *osb = OCFS2_SB(sb);
3847 	int ret, num_buckets, extend = 1;
3848 	u64 p_blkno;
3849 	u32 e_cpos, num_clusters;
3850 
3851 	mlog(0, "Add new xattr bucket starting form %llu\n",
3852 	     (unsigned long long)header_bh->b_blocknr);
3853 
3854 	/*
3855 	 * Add refrence for header_bh here because it may be
3856 	 * changed in ocfs2_add_new_xattr_cluster and we need
3857 	 * to free it in the end.
3858 	 */
3859 	get_bh(header_bh);
3860 
3861 	ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3862 				  &num_clusters, el);
3863 	if (ret) {
3864 		mlog_errno(ret);
3865 		goto out;
3866 	}
3867 
3868 	ret = ocfs2_read_block(osb, p_blkno,
3869 			       &first_bh, OCFS2_BH_CACHED, inode);
3870 	if (ret) {
3871 		mlog_errno(ret);
3872 		goto out;
3873 	}
3874 
3875 	num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3876 	first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3877 
3878 	if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3879 		ret = ocfs2_add_new_xattr_cluster(inode,
3880 						  xb_bh,
3881 						  &first_bh,
3882 						  &header_bh,
3883 						  &num_clusters,
3884 						  e_cpos,
3885 						  p_blkno,
3886 						  &extend);
3887 		if (ret) {
3888 			mlog_errno(ret);
3889 			goto out;
3890 		}
3891 	}
3892 
3893 	if (extend)
3894 		ret = ocfs2_extend_xattr_bucket(inode,
3895 						first_bh,
3896 						header_bh,
3897 						num_clusters);
3898 	if (ret)
3899 		mlog_errno(ret);
3900 out:
3901 	brelse(first_bh);
3902 	brelse(header_bh);
3903 	return ret;
3904 }
3905 
3906 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3907 					struct ocfs2_xattr_bucket *bucket,
3908 					int offs)
3909 {
3910 	int block_off = offs >> inode->i_sb->s_blocksize_bits;
3911 
3912 	offs = offs % inode->i_sb->s_blocksize;
3913 	return bucket->bhs[block_off]->b_data + offs;
3914 }
3915 
3916 /*
3917  * Handle the normal xattr set, including replace, delete and new.
3918  * When the bucket is empty, "is_empty" is set and the caller can
3919  * free this bucket.
3920  *
3921  * Note: "local" indicates the real data's locality. So we can't
3922  * just its bucket locality by its length.
3923  */
3924 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3925 					 struct ocfs2_xattr_info *xi,
3926 					 struct ocfs2_xattr_search *xs,
3927 					 u32 name_hash,
3928 					 int local,
3929 					 int *is_empty)
3930 {
3931 	struct ocfs2_xattr_entry *last, *xe;
3932 	int name_len = strlen(xi->name);
3933 	struct ocfs2_xattr_header *xh = xs->header;
3934 	u16 count = le16_to_cpu(xh->xh_count), start;
3935 	size_t blocksize = inode->i_sb->s_blocksize;
3936 	char *val;
3937 	size_t offs, size, new_size;
3938 
3939 	last = &xh->xh_entries[count];
3940 	if (!xs->not_found) {
3941 		xe = xs->here;
3942 		offs = le16_to_cpu(xe->xe_name_offset);
3943 		if (ocfs2_xattr_is_local(xe))
3944 			size = OCFS2_XATTR_SIZE(name_len) +
3945 			OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3946 		else
3947 			size = OCFS2_XATTR_SIZE(name_len) +
3948 			OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3949 
3950 		/*
3951 		 * If the new value will be stored outside, xi->value has been
3952 		 * initalized as an empty ocfs2_xattr_value_root, and the same
3953 		 * goes with xi->value_len, so we can set new_size safely here.
3954 		 * See ocfs2_xattr_set_in_bucket.
3955 		 */
3956 		new_size = OCFS2_XATTR_SIZE(name_len) +
3957 			   OCFS2_XATTR_SIZE(xi->value_len);
3958 
3959 		le16_add_cpu(&xh->xh_name_value_len, -size);
3960 		if (xi->value) {
3961 			if (new_size > size)
3962 				goto set_new_name_value;
3963 
3964 			/* Now replace the old value with new one. */
3965 			if (local)
3966 				xe->xe_value_size = cpu_to_le64(xi->value_len);
3967 			else
3968 				xe->xe_value_size = 0;
3969 
3970 			val = ocfs2_xattr_bucket_get_val(inode,
3971 							 &xs->bucket, offs);
3972 			memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3973 			       size - OCFS2_XATTR_SIZE(name_len));
3974 			if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3975 				memcpy(val + OCFS2_XATTR_SIZE(name_len),
3976 				       xi->value, xi->value_len);
3977 
3978 			le16_add_cpu(&xh->xh_name_value_len, new_size);
3979 			ocfs2_xattr_set_local(xe, local);
3980 			return;
3981 		} else {
3982 			/* Remove the old entry. */
3983 			last -= 1;
3984 			memmove(xe, xe + 1,
3985 				(void *)last - (void *)xe);
3986 			memset(last, 0, sizeof(struct ocfs2_xattr_entry));
3987 			le16_add_cpu(&xh->xh_count, -1);
3988 			if (xh->xh_count == 0 && is_empty)
3989 				*is_empty = 1;
3990 			return;
3991 		}
3992 	} else {
3993 		/* find a new entry for insert. */
3994 		int low = 0, high = count - 1, tmp;
3995 		struct ocfs2_xattr_entry *tmp_xe;
3996 
3997 		while (low <= high) {
3998 			tmp = (low + high) / 2;
3999 			tmp_xe = &xh->xh_entries[tmp];
4000 
4001 			if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4002 				low = tmp + 1;
4003 			else if (name_hash <
4004 				 le32_to_cpu(tmp_xe->xe_name_hash))
4005 				high = tmp - 1;
4006 			else {
4007 				low = tmp;
4008 				break;
4009 			}
4010 		}
4011 
4012 		xe = &xh->xh_entries[low];
4013 		if (low != count)
4014 			memmove(xe + 1, xe, (void *)last - (void *)xe);
4015 
4016 		le16_add_cpu(&xh->xh_count, 1);
4017 		memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4018 		xe->xe_name_hash = cpu_to_le32(name_hash);
4019 		xe->xe_name_len = name_len;
4020 		ocfs2_xattr_set_type(xe, xi->name_index);
4021 	}
4022 
4023 set_new_name_value:
4024 	/* Insert the new name+value. */
4025 	size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4026 
4027 	/*
4028 	 * We must make sure that the name/value pair
4029 	 * exists in the same block.
4030 	 */
4031 	offs = le16_to_cpu(xh->xh_free_start);
4032 	start = offs - size;
4033 
4034 	if (start >> inode->i_sb->s_blocksize_bits !=
4035 	    (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4036 		offs = offs - offs % blocksize;
4037 		xh->xh_free_start = cpu_to_le16(offs);
4038 	}
4039 
4040 	val = ocfs2_xattr_bucket_get_val(inode,
4041 					 &xs->bucket, offs - size);
4042 	xe->xe_name_offset = cpu_to_le16(offs - size);
4043 
4044 	memset(val, 0, size);
4045 	memcpy(val, xi->name, name_len);
4046 	memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4047 
4048 	xe->xe_value_size = cpu_to_le64(xi->value_len);
4049 	ocfs2_xattr_set_local(xe, local);
4050 	xs->here = xe;
4051 	le16_add_cpu(&xh->xh_free_start, -size);
4052 	le16_add_cpu(&xh->xh_name_value_len, size);
4053 
4054 	return;
4055 }
4056 
4057 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4058 					     handle_t *handle,
4059 					     struct ocfs2_xattr_search *xs,
4060 					     struct buffer_head **bhs,
4061 					     u16 bh_num)
4062 {
4063 	int ret = 0, off, block_off;
4064 	struct ocfs2_xattr_entry *xe = xs->here;
4065 
4066 	/*
4067 	 * First calculate all the blocks we should journal_access
4068 	 * and journal_dirty. The first block should always be touched.
4069 	 */
4070 	ret = ocfs2_journal_dirty(handle, bhs[0]);
4071 	if (ret)
4072 		mlog_errno(ret);
4073 
4074 	/* calc the data. */
4075 	off = le16_to_cpu(xe->xe_name_offset);
4076 	block_off = off >> inode->i_sb->s_blocksize_bits;
4077 	ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4078 	if (ret)
4079 		mlog_errno(ret);
4080 
4081 	return ret;
4082 }
4083 
4084 /*
4085  * Set the xattr entry in the specified bucket.
4086  * The bucket is indicated by xs->bucket and it should have the enough
4087  * space for the xattr insertion.
4088  */
4089 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4090 					   struct ocfs2_xattr_info *xi,
4091 					   struct ocfs2_xattr_search *xs,
4092 					   u32 name_hash,
4093 					   int local,
4094 					   int *bucket_empty)
4095 {
4096 	int i, ret;
4097 	handle_t *handle = NULL;
4098 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4099 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4100 
4101 	mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4102 	     (unsigned long)xi->value_len, xi->name_index,
4103 	     (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4104 
4105 	if (!xs->bucket.bhs[1]) {
4106 		ret = ocfs2_read_blocks(osb,
4107 					xs->bucket.bhs[0]->b_blocknr + 1,
4108 					blk_per_bucket - 1, &xs->bucket.bhs[1],
4109 					OCFS2_BH_CACHED, inode);
4110 		if (ret) {
4111 			mlog_errno(ret);
4112 			goto out;
4113 		}
4114 	}
4115 
4116 	handle = ocfs2_start_trans(osb, blk_per_bucket);
4117 	if (IS_ERR(handle)) {
4118 		ret = PTR_ERR(handle);
4119 		handle = NULL;
4120 		mlog_errno(ret);
4121 		goto out;
4122 	}
4123 
4124 	for (i = 0; i < blk_per_bucket; i++) {
4125 		ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4126 					   OCFS2_JOURNAL_ACCESS_WRITE);
4127 		if (ret < 0) {
4128 			mlog_errno(ret);
4129 			goto out;
4130 		}
4131 	}
4132 
4133 	ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash,
4134 				     local, bucket_empty);
4135 
4136 	/*Only dirty the blocks we have touched in set xattr. */
4137 	ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4138 						xs->bucket.bhs, blk_per_bucket);
4139 	if (ret)
4140 		mlog_errno(ret);
4141 out:
4142 	ocfs2_commit_trans(osb, handle);
4143 
4144 	return ret;
4145 }
4146 
4147 static int ocfs2_xattr_value_update_size(struct inode *inode,
4148 					 struct buffer_head *xe_bh,
4149 					 struct ocfs2_xattr_entry *xe,
4150 					 u64 new_size)
4151 {
4152 	int ret;
4153 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4154 	handle_t *handle = NULL;
4155 
4156 	handle = ocfs2_start_trans(osb, 1);
4157 	if (handle == NULL) {
4158 		ret = -ENOMEM;
4159 		mlog_errno(ret);
4160 		goto out;
4161 	}
4162 
4163 	ret = ocfs2_journal_access(handle, inode, xe_bh,
4164 				   OCFS2_JOURNAL_ACCESS_WRITE);
4165 	if (ret < 0) {
4166 		mlog_errno(ret);
4167 		goto out_commit;
4168 	}
4169 
4170 	xe->xe_value_size = cpu_to_le64(new_size);
4171 
4172 	ret = ocfs2_journal_dirty(handle, xe_bh);
4173 	if (ret < 0)
4174 		mlog_errno(ret);
4175 
4176 out_commit:
4177 	ocfs2_commit_trans(osb, handle);
4178 out:
4179 	return ret;
4180 }
4181 
4182 /*
4183  * Truncate the specified xe_off entry in xattr bucket.
4184  * bucket is indicated by header_bh and len is the new length.
4185  * Both the ocfs2_xattr_value_root and the entry will be updated here.
4186  *
4187  * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4188  */
4189 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4190 					     struct buffer_head *header_bh,
4191 					     int xe_off,
4192 					     int len)
4193 {
4194 	int ret, offset;
4195 	u64 value_blk;
4196 	struct buffer_head *value_bh = NULL;
4197 	struct ocfs2_xattr_value_root *xv;
4198 	struct ocfs2_xattr_entry *xe;
4199 	struct ocfs2_xattr_header *xh =
4200 			(struct ocfs2_xattr_header *)header_bh->b_data;
4201 	size_t blocksize = inode->i_sb->s_blocksize;
4202 
4203 	xe = &xh->xh_entries[xe_off];
4204 
4205 	BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4206 
4207 	offset = le16_to_cpu(xe->xe_name_offset) +
4208 		 OCFS2_XATTR_SIZE(xe->xe_name_len);
4209 
4210 	value_blk = offset / blocksize;
4211 
4212 	/* We don't allow ocfs2_xattr_value to be stored in different block. */
4213 	BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4214 	value_blk += header_bh->b_blocknr;
4215 
4216 	ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
4217 			       &value_bh, OCFS2_BH_CACHED, inode);
4218 	if (ret) {
4219 		mlog_errno(ret);
4220 		goto out;
4221 	}
4222 
4223 	xv = (struct ocfs2_xattr_value_root *)
4224 		(value_bh->b_data + offset % blocksize);
4225 
4226 	mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4227 	     xe_off, (unsigned long long)header_bh->b_blocknr, len);
4228 	ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4229 	if (ret) {
4230 		mlog_errno(ret);
4231 		goto out;
4232 	}
4233 
4234 	ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4235 	if (ret) {
4236 		mlog_errno(ret);
4237 		goto out;
4238 	}
4239 
4240 out:
4241 	brelse(value_bh);
4242 	return ret;
4243 }
4244 
4245 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4246 						struct ocfs2_xattr_search *xs,
4247 						int len)
4248 {
4249 	int ret, offset;
4250 	struct ocfs2_xattr_entry *xe = xs->here;
4251 	struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4252 
4253 	BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4254 
4255 	offset = xe - xh->xh_entries;
4256 	ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4257 						offset, len);
4258 	if (ret)
4259 		mlog_errno(ret);
4260 
4261 	return ret;
4262 }
4263 
4264 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4265 						struct ocfs2_xattr_search *xs,
4266 						char *val,
4267 						int value_len)
4268 {
4269 	int offset;
4270 	struct ocfs2_xattr_value_root *xv;
4271 	struct ocfs2_xattr_entry *xe = xs->here;
4272 
4273 	BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4274 
4275 	offset = le16_to_cpu(xe->xe_name_offset) +
4276 		 OCFS2_XATTR_SIZE(xe->xe_name_len);
4277 
4278 	xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4279 
4280 	return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4281 }
4282 
4283 /*
4284  * Remove the xattr bucket pointed by bucket_bh.
4285  * All the buckets after it in the same xattr extent rec will be
4286  * move forward one by one.
4287  */
4288 static int ocfs2_rm_xattr_bucket(struct inode *inode,
4289 				 struct buffer_head *first_bh,
4290 				 struct ocfs2_xattr_bucket *bucket)
4291 {
4292 	int ret = 0, credits;
4293 	struct ocfs2_xattr_header *xh =
4294 				(struct ocfs2_xattr_header *)first_bh->b_data;
4295 	u16 bucket_num = le16_to_cpu(xh->xh_num_buckets);
4296 	u64 end, start = bucket->bhs[0]->b_blocknr;
4297 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4298 	handle_t *handle;
4299 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4300 
4301 	end = first_bh->b_blocknr + (bucket_num - 1) * blk_per_bucket;
4302 
4303 	mlog(0, "rm xattr bucket %llu\n", start);
4304 	/*
4305 	 * We need to update the first xattr_header and all the buckets starting
4306 	 * from start in this xattr rec.
4307 	 *
4308 	 * XXX: Should we empty the old last bucket here?
4309 	 */
4310 	credits = 1 + end - start;
4311 	handle = ocfs2_start_trans(osb, credits);
4312 	if (IS_ERR(handle)) {
4313 		ret = PTR_ERR(handle);
4314 		mlog_errno(ret);
4315 		return ret;
4316 	}
4317 
4318 	ret = ocfs2_journal_access(handle, inode, first_bh,
4319 				   OCFS2_JOURNAL_ACCESS_WRITE);
4320 	if (ret) {
4321 		mlog_errno(ret);
4322 		goto out_commit;
4323 	}
4324 
4325 
4326 	while (start < end) {
4327 		ret = ocfs2_cp_xattr_bucket(inode, handle,
4328 					    start + blk_per_bucket,
4329 					    start, 0);
4330 		if (ret) {
4331 			mlog_errno(ret);
4332 			goto out_commit;
4333 		}
4334 		start += blk_per_bucket;
4335 	}
4336 
4337 	/* update the first_bh. */
4338 	xh->xh_num_buckets = cpu_to_le16(bucket_num - 1);
4339 	ocfs2_journal_dirty(handle, first_bh);
4340 
4341 out_commit:
4342 	ocfs2_commit_trans(osb, handle);
4343 	return ret;
4344 }
4345 
4346 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4347 				  struct buffer_head *root_bh,
4348 				  u64 blkno,
4349 				  u32 cpos,
4350 				  u32 len)
4351 {
4352 	int ret;
4353 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4354 	struct inode *tl_inode = osb->osb_tl_inode;
4355 	handle_t *handle;
4356 	struct ocfs2_xattr_block *xb =
4357 			(struct ocfs2_xattr_block *)root_bh->b_data;
4358 	struct ocfs2_alloc_context *meta_ac = NULL;
4359 	struct ocfs2_cached_dealloc_ctxt dealloc;
4360 	struct ocfs2_extent_tree et;
4361 
4362 	ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4363 
4364 	ocfs2_init_dealloc_ctxt(&dealloc);
4365 
4366 	mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4367 	     cpos, len, (unsigned long long)blkno);
4368 
4369 	ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4370 
4371 	ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4372 	if (ret) {
4373 		mlog_errno(ret);
4374 		return ret;
4375 	}
4376 
4377 	mutex_lock(&tl_inode->i_mutex);
4378 
4379 	if (ocfs2_truncate_log_needs_flush(osb)) {
4380 		ret = __ocfs2_flush_truncate_log(osb);
4381 		if (ret < 0) {
4382 			mlog_errno(ret);
4383 			goto out;
4384 		}
4385 	}
4386 
4387 	handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4388 	if (handle == NULL) {
4389 		ret = -ENOMEM;
4390 		mlog_errno(ret);
4391 		goto out;
4392 	}
4393 
4394 	ret = ocfs2_journal_access(handle, inode, root_bh,
4395 				   OCFS2_JOURNAL_ACCESS_WRITE);
4396 	if (ret) {
4397 		mlog_errno(ret);
4398 		goto out_commit;
4399 	}
4400 
4401 	ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4402 				  &dealloc);
4403 	if (ret) {
4404 		mlog_errno(ret);
4405 		goto out_commit;
4406 	}
4407 
4408 	le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4409 
4410 	ret = ocfs2_journal_dirty(handle, root_bh);
4411 	if (ret) {
4412 		mlog_errno(ret);
4413 		goto out_commit;
4414 	}
4415 
4416 	ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4417 	if (ret)
4418 		mlog_errno(ret);
4419 
4420 out_commit:
4421 	ocfs2_commit_trans(osb, handle);
4422 out:
4423 	ocfs2_schedule_truncate_log_flush(osb, 1);
4424 
4425 	mutex_unlock(&tl_inode->i_mutex);
4426 
4427 	if (meta_ac)
4428 		ocfs2_free_alloc_context(meta_ac);
4429 
4430 	ocfs2_run_deallocs(osb, &dealloc);
4431 
4432 	return ret;
4433 }
4434 
4435 /*
4436  * Free the xattr bucket indicated by xs->bucket and if all the buckets
4437  * in the clusters is free, free the clusters also.
4438  */
4439 static int ocfs2_xattr_bucket_shrink(struct inode *inode,
4440 				     struct ocfs2_xattr_info *xi,
4441 				     struct ocfs2_xattr_search *xs,
4442 				     u32 name_hash)
4443 {
4444 	int ret;
4445 	u32 e_cpos, num_clusters;
4446 	u64 p_blkno;
4447 	struct buffer_head *first_bh = NULL;
4448 	struct ocfs2_xattr_header *first_xh;
4449 	struct ocfs2_xattr_block *xb =
4450 			(struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
4451 
4452 	BUG_ON(xs->header->xh_count != 0);
4453 
4454 	ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4455 				  &e_cpos, &num_clusters,
4456 				  &xb->xb_attrs.xb_root.xt_list);
4457 	if (ret) {
4458 		mlog_errno(ret);
4459 		return ret;
4460 	}
4461 
4462 	ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
4463 			       &first_bh, OCFS2_BH_CACHED, inode);
4464 	if (ret) {
4465 		mlog_errno(ret);
4466 		return ret;
4467 	}
4468 
4469 	ret = ocfs2_rm_xattr_bucket(inode, first_bh, &xs->bucket);
4470 	if (ret) {
4471 		mlog_errno(ret);
4472 		goto out;
4473 	}
4474 
4475 	first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4476 	if (first_xh->xh_num_buckets == 0)
4477 		ret = ocfs2_rm_xattr_cluster(inode, xs->xattr_bh,
4478 					     p_blkno, e_cpos,
4479 					     num_clusters);
4480 
4481 out:
4482 	brelse(first_bh);
4483 	return ret;
4484 }
4485 
4486 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4487 					 struct ocfs2_xattr_search *xs)
4488 {
4489 	handle_t *handle = NULL;
4490 	struct ocfs2_xattr_header *xh = xs->bucket.xh;
4491 	struct ocfs2_xattr_entry *last = &xh->xh_entries[
4492 						le16_to_cpu(xh->xh_count) - 1];
4493 	int ret = 0;
4494 
4495 	handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4496 	if (IS_ERR(handle)) {
4497 		ret = PTR_ERR(handle);
4498 		mlog_errno(ret);
4499 		return;
4500 	}
4501 
4502 	ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4503 				   OCFS2_JOURNAL_ACCESS_WRITE);
4504 	if (ret) {
4505 		mlog_errno(ret);
4506 		goto out_commit;
4507 	}
4508 
4509 	/* Remove the old entry. */
4510 	memmove(xs->here, xs->here + 1,
4511 		(void *)last - (void *)xs->here);
4512 	memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4513 	le16_add_cpu(&xh->xh_count, -1);
4514 
4515 	ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4516 	if (ret < 0)
4517 		mlog_errno(ret);
4518 out_commit:
4519 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4520 }
4521 
4522 /*
4523  * Set the xattr name/value in the bucket specified in xs.
4524  *
4525  * As the new value in xi may be stored in the bucket or in an outside cluster,
4526  * we divide the whole process into 3 steps:
4527  * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4528  * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4529  * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4530  * 4. If the clusters for the new outside value can't be allocated, we need
4531  *    to free the xattr we allocated in set.
4532  */
4533 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4534 				     struct ocfs2_xattr_info *xi,
4535 				     struct ocfs2_xattr_search *xs)
4536 {
4537 	int ret, local = 1, bucket_empty = 0;
4538 	size_t value_len;
4539 	char *val = (char *)xi->value;
4540 	struct ocfs2_xattr_entry *xe = xs->here;
4541 	u32 name_hash = ocfs2_xattr_hash_by_name(inode,
4542 						 xi->name_index, xi->name);
4543 
4544 	if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4545 		/*
4546 		 * We need to truncate the xattr storage first.
4547 		 *
4548 		 * If both the old and new value are stored to
4549 		 * outside block, we only need to truncate
4550 		 * the storage and then set the value outside.
4551 		 *
4552 		 * If the new value should be stored within block,
4553 		 * we should free all the outside block first and
4554 		 * the modification to the xattr block will be done
4555 		 * by following steps.
4556 		 */
4557 		if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4558 			value_len = xi->value_len;
4559 		else
4560 			value_len = 0;
4561 
4562 		ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4563 							   value_len);
4564 		if (ret)
4565 			goto out;
4566 
4567 		if (value_len)
4568 			goto set_value_outside;
4569 	}
4570 
4571 	value_len = xi->value_len;
4572 	/* So we have to handle the inside block change now. */
4573 	if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4574 		/*
4575 		 * If the new value will be stored outside of block,
4576 		 * initalize a new empty value root and insert it first.
4577 		 */
4578 		local = 0;
4579 		xi->value = &def_xv;
4580 		xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4581 	}
4582 
4583 	ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash,
4584 					      local, &bucket_empty);
4585 	if (ret) {
4586 		mlog_errno(ret);
4587 		goto out;
4588 	}
4589 
4590 	if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4591 		/* allocate the space now for the outside block storage. */
4592 		ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4593 							   value_len);
4594 		if (ret) {
4595 			mlog_errno(ret);
4596 
4597 			if (xs->not_found) {
4598 				/*
4599 				 * We can't allocate enough clusters for outside
4600 				 * storage and we have allocated xattr already,
4601 				 * so need to remove it.
4602 				 */
4603 				ocfs2_xattr_bucket_remove_xs(inode, xs);
4604 			}
4605 			goto out;
4606 		}
4607 	} else {
4608 		if (bucket_empty)
4609 			ret = ocfs2_xattr_bucket_shrink(inode, xi,
4610 							xs, name_hash);
4611 		goto out;
4612 	}
4613 
4614 set_value_outside:
4615 	ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4616 out:
4617 	return ret;
4618 }
4619 
4620 /* check whether the xattr bucket is filled up with the same hash value. */
4621 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4622 					      struct ocfs2_xattr_bucket *bucket)
4623 {
4624 	struct ocfs2_xattr_header *xh = bucket->xh;
4625 
4626 	if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4627 	    xh->xh_entries[0].xe_name_hash) {
4628 		mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4629 		     "hash = %u\n",
4630 		     (unsigned long long)bucket->bhs[0]->b_blocknr,
4631 		     le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4632 		return -ENOSPC;
4633 	}
4634 
4635 	return 0;
4636 }
4637 
4638 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4639 					     struct ocfs2_xattr_info *xi,
4640 					     struct ocfs2_xattr_search *xs)
4641 {
4642 	struct ocfs2_xattr_header *xh;
4643 	struct ocfs2_xattr_entry *xe;
4644 	u16 count, header_size, xh_free_start;
4645 	int i, free, max_free, need, old;
4646 	size_t value_size = 0, name_len = strlen(xi->name);
4647 	size_t blocksize = inode->i_sb->s_blocksize;
4648 	int ret, allocation = 0;
4649 	u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4650 
4651 	mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4652 
4653 try_again:
4654 	xh = xs->header;
4655 	count = le16_to_cpu(xh->xh_count);
4656 	xh_free_start = le16_to_cpu(xh->xh_free_start);
4657 	header_size = sizeof(struct ocfs2_xattr_header) +
4658 			count * sizeof(struct ocfs2_xattr_entry);
4659 	max_free = OCFS2_XATTR_BUCKET_SIZE -
4660 		le16_to_cpu(xh->xh_name_value_len) - header_size;
4661 
4662 	mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4663 			"of %u which exceed block size\n",
4664 			(unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4665 			header_size);
4666 
4667 	if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4668 		value_size = OCFS2_XATTR_ROOT_SIZE;
4669 	else if (xi->value)
4670 		value_size = OCFS2_XATTR_SIZE(xi->value_len);
4671 
4672 	if (xs->not_found)
4673 		need = sizeof(struct ocfs2_xattr_entry) +
4674 			OCFS2_XATTR_SIZE(name_len) + value_size;
4675 	else {
4676 		need = value_size + OCFS2_XATTR_SIZE(name_len);
4677 
4678 		/*
4679 		 * We only replace the old value if the new length is smaller
4680 		 * than the old one. Otherwise we will allocate new space in the
4681 		 * bucket to store it.
4682 		 */
4683 		xe = xs->here;
4684 		if (ocfs2_xattr_is_local(xe))
4685 			old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4686 		else
4687 			old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4688 
4689 		if (old >= value_size)
4690 			need = 0;
4691 	}
4692 
4693 	free = xh_free_start - header_size;
4694 	/*
4695 	 * We need to make sure the new name/value pair
4696 	 * can exist in the same block.
4697 	 */
4698 	if (xh_free_start % blocksize < need)
4699 		free -= xh_free_start % blocksize;
4700 
4701 	mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4702 	     "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4703 	     " %u\n", xs->not_found,
4704 	     (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4705 	     free, need, max_free, le16_to_cpu(xh->xh_free_start),
4706 	     le16_to_cpu(xh->xh_name_value_len));
4707 
4708 	if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4709 		if (need <= max_free &&
4710 		    count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4711 			/*
4712 			 * We can create the space by defragment. Since only the
4713 			 * name/value will be moved, the xe shouldn't be changed
4714 			 * in xs.
4715 			 */
4716 			ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4717 			if (ret) {
4718 				mlog_errno(ret);
4719 				goto out;
4720 			}
4721 
4722 			xh_free_start = le16_to_cpu(xh->xh_free_start);
4723 			free = xh_free_start - header_size;
4724 			if (xh_free_start % blocksize < need)
4725 				free -= xh_free_start % blocksize;
4726 
4727 			if (free >= need)
4728 				goto xattr_set;
4729 
4730 			mlog(0, "Can't get enough space for xattr insert by "
4731 			     "defragment. Need %u bytes, but we have %d, so "
4732 			     "allocate new bucket for it.\n", need, free);
4733 		}
4734 
4735 		/*
4736 		 * We have to add new buckets or clusters and one
4737 		 * allocation should leave us enough space for insert.
4738 		 */
4739 		BUG_ON(allocation);
4740 
4741 		/*
4742 		 * We do not allow for overlapping ranges between buckets. And
4743 		 * the maximum number of collisions we will allow for then is
4744 		 * one bucket's worth, so check it here whether we need to
4745 		 * add a new bucket for the insert.
4746 		 */
4747 		ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4748 		if (ret) {
4749 			mlog_errno(ret);
4750 			goto out;
4751 		}
4752 
4753 		ret = ocfs2_add_new_xattr_bucket(inode,
4754 						 xs->xattr_bh,
4755 						 xs->bucket.bhs[0]);
4756 		if (ret) {
4757 			mlog_errno(ret);
4758 			goto out;
4759 		}
4760 
4761 		for (i = 0; i < blk_per_bucket; i++)
4762 			brelse(xs->bucket.bhs[i]);
4763 
4764 		memset(&xs->bucket, 0, sizeof(xs->bucket));
4765 
4766 		ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4767 						   xi->name_index,
4768 						   xi->name, xs);
4769 		if (ret && ret != -ENODATA)
4770 			goto out;
4771 		xs->not_found = ret;
4772 		allocation = 1;
4773 		goto try_again;
4774 	}
4775 
4776 xattr_set:
4777 	ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4778 out:
4779 	mlog_exit(ret);
4780 	return ret;
4781 }
4782 
4783 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4784 					struct ocfs2_xattr_bucket *bucket,
4785 					void *para)
4786 {
4787 	int ret = 0;
4788 	struct ocfs2_xattr_header *xh = bucket->xh;
4789 	u16 i;
4790 	struct ocfs2_xattr_entry *xe;
4791 
4792 	for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4793 		xe = &xh->xh_entries[i];
4794 		if (ocfs2_xattr_is_local(xe))
4795 			continue;
4796 
4797 		ret = ocfs2_xattr_bucket_value_truncate(inode,
4798 							bucket->bhs[0],
4799 							i, 0);
4800 		if (ret) {
4801 			mlog_errno(ret);
4802 			break;
4803 		}
4804 	}
4805 
4806 	return ret;
4807 }
4808 
4809 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4810 					  struct buffer_head *xb_bh)
4811 {
4812 	struct ocfs2_xattr_block *xb =
4813 			(struct ocfs2_xattr_block *)xb_bh->b_data;
4814 	struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4815 	int ret = 0;
4816 	u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4817 	u64 p_blkno;
4818 
4819 	if (le16_to_cpu(el->l_next_free_rec) == 0)
4820 		return 0;
4821 
4822 	while (name_hash > 0) {
4823 		ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4824 					  &e_cpos, &num_clusters, el);
4825 		if (ret) {
4826 			mlog_errno(ret);
4827 			goto out;
4828 		}
4829 
4830 		ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4831 						  ocfs2_delete_xattr_in_bucket,
4832 						  NULL);
4833 		if (ret) {
4834 			mlog_errno(ret);
4835 			goto out;
4836 		}
4837 
4838 		ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4839 					     p_blkno, e_cpos, num_clusters);
4840 		if (ret) {
4841 			mlog_errno(ret);
4842 			break;
4843 		}
4844 
4845 		if (e_cpos == 0)
4846 			break;
4847 
4848 		name_hash = e_cpos - 1;
4849 	}
4850 
4851 out:
4852 	return ret;
4853 }
4854