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