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