xref: /openbmc/linux/fs/ocfs2/refcounttree.c (revision 7540c1a7)
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * refcounttree.c
5  *
6  * Copyright (C) 2009 Oracle.  All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public
10  * License version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  */
17 
18 #include <linux/sort.h>
19 #define MLOG_MASK_PREFIX ML_REFCOUNT
20 #include <cluster/masklog.h>
21 #include "ocfs2.h"
22 #include "inode.h"
23 #include "alloc.h"
24 #include "suballoc.h"
25 #include "journal.h"
26 #include "uptodate.h"
27 #include "super.h"
28 #include "buffer_head_io.h"
29 #include "blockcheck.h"
30 #include "refcounttree.h"
31 #include "sysfile.h"
32 #include "dlmglue.h"
33 #include "extent_map.h"
34 #include "aops.h"
35 #include "xattr.h"
36 
37 #include <linux/bio.h>
38 #include <linux/blkdev.h>
39 #include <linux/gfp.h>
40 #include <linux/slab.h>
41 #include <linux/writeback.h>
42 #include <linux/pagevec.h>
43 #include <linux/swap.h>
44 
45 struct ocfs2_cow_context {
46 	struct inode *inode;
47 	u32 cow_start;
48 	u32 cow_len;
49 	struct ocfs2_extent_tree data_et;
50 	struct ocfs2_refcount_tree *ref_tree;
51 	struct buffer_head *ref_root_bh;
52 	struct ocfs2_alloc_context *meta_ac;
53 	struct ocfs2_alloc_context *data_ac;
54 	struct ocfs2_cached_dealloc_ctxt dealloc;
55 	void *cow_object;
56 	struct ocfs2_post_refcount *post_refcount;
57 	int extra_credits;
58 	int (*get_clusters)(struct ocfs2_cow_context *context,
59 			    u32 v_cluster, u32 *p_cluster,
60 			    u32 *num_clusters,
61 			    unsigned int *extent_flags);
62 	int (*cow_duplicate_clusters)(handle_t *handle,
63 				      struct ocfs2_cow_context *context,
64 				      u32 cpos, u32 old_cluster,
65 				      u32 new_cluster, u32 new_len);
66 };
67 
68 static inline struct ocfs2_refcount_tree *
69 cache_info_to_refcount(struct ocfs2_caching_info *ci)
70 {
71 	return container_of(ci, struct ocfs2_refcount_tree, rf_ci);
72 }
73 
74 static int ocfs2_validate_refcount_block(struct super_block *sb,
75 					 struct buffer_head *bh)
76 {
77 	int rc;
78 	struct ocfs2_refcount_block *rb =
79 		(struct ocfs2_refcount_block *)bh->b_data;
80 
81 	mlog(0, "Validating refcount block %llu\n",
82 	     (unsigned long long)bh->b_blocknr);
83 
84 	BUG_ON(!buffer_uptodate(bh));
85 
86 	/*
87 	 * If the ecc fails, we return the error but otherwise
88 	 * leave the filesystem running.  We know any error is
89 	 * local to this block.
90 	 */
91 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check);
92 	if (rc) {
93 		mlog(ML_ERROR, "Checksum failed for refcount block %llu\n",
94 		     (unsigned long long)bh->b_blocknr);
95 		return rc;
96 	}
97 
98 
99 	if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) {
100 		ocfs2_error(sb,
101 			    "Refcount block #%llu has bad signature %.*s",
102 			    (unsigned long long)bh->b_blocknr, 7,
103 			    rb->rf_signature);
104 		return -EINVAL;
105 	}
106 
107 	if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) {
108 		ocfs2_error(sb,
109 			    "Refcount block #%llu has an invalid rf_blkno "
110 			    "of %llu",
111 			    (unsigned long long)bh->b_blocknr,
112 			    (unsigned long long)le64_to_cpu(rb->rf_blkno));
113 		return -EINVAL;
114 	}
115 
116 	if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) {
117 		ocfs2_error(sb,
118 			    "Refcount block #%llu has an invalid "
119 			    "rf_fs_generation of #%u",
120 			    (unsigned long long)bh->b_blocknr,
121 			    le32_to_cpu(rb->rf_fs_generation));
122 		return -EINVAL;
123 	}
124 
125 	return 0;
126 }
127 
128 static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci,
129 				     u64 rb_blkno,
130 				     struct buffer_head **bh)
131 {
132 	int rc;
133 	struct buffer_head *tmp = *bh;
134 
135 	rc = ocfs2_read_block(ci, rb_blkno, &tmp,
136 			      ocfs2_validate_refcount_block);
137 
138 	/* If ocfs2_read_block() got us a new bh, pass it up. */
139 	if (!rc && !*bh)
140 		*bh = tmp;
141 
142 	return rc;
143 }
144 
145 static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci)
146 {
147 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
148 
149 	return rf->rf_blkno;
150 }
151 
152 static struct super_block *
153 ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci)
154 {
155 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
156 
157 	return rf->rf_sb;
158 }
159 
160 static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci)
161 {
162 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
163 
164 	spin_lock(&rf->rf_lock);
165 }
166 
167 static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci)
168 {
169 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
170 
171 	spin_unlock(&rf->rf_lock);
172 }
173 
174 static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci)
175 {
176 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
177 
178 	mutex_lock(&rf->rf_io_mutex);
179 }
180 
181 static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci)
182 {
183 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
184 
185 	mutex_unlock(&rf->rf_io_mutex);
186 }
187 
188 static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = {
189 	.co_owner		= ocfs2_refcount_cache_owner,
190 	.co_get_super		= ocfs2_refcount_cache_get_super,
191 	.co_cache_lock		= ocfs2_refcount_cache_lock,
192 	.co_cache_unlock	= ocfs2_refcount_cache_unlock,
193 	.co_io_lock		= ocfs2_refcount_cache_io_lock,
194 	.co_io_unlock		= ocfs2_refcount_cache_io_unlock,
195 };
196 
197 static struct ocfs2_refcount_tree *
198 ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno)
199 {
200 	struct rb_node *n = osb->osb_rf_lock_tree.rb_node;
201 	struct ocfs2_refcount_tree *tree = NULL;
202 
203 	while (n) {
204 		tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node);
205 
206 		if (blkno < tree->rf_blkno)
207 			n = n->rb_left;
208 		else if (blkno > tree->rf_blkno)
209 			n = n->rb_right;
210 		else
211 			return tree;
212 	}
213 
214 	return NULL;
215 }
216 
217 /* osb_lock is already locked. */
218 static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb,
219 				       struct ocfs2_refcount_tree *new)
220 {
221 	u64 rf_blkno = new->rf_blkno;
222 	struct rb_node *parent = NULL;
223 	struct rb_node **p = &osb->osb_rf_lock_tree.rb_node;
224 	struct ocfs2_refcount_tree *tmp;
225 
226 	while (*p) {
227 		parent = *p;
228 
229 		tmp = rb_entry(parent, struct ocfs2_refcount_tree,
230 			       rf_node);
231 
232 		if (rf_blkno < tmp->rf_blkno)
233 			p = &(*p)->rb_left;
234 		else if (rf_blkno > tmp->rf_blkno)
235 			p = &(*p)->rb_right;
236 		else {
237 			/* This should never happen! */
238 			mlog(ML_ERROR, "Duplicate refcount block %llu found!\n",
239 			     (unsigned long long)rf_blkno);
240 			BUG();
241 		}
242 	}
243 
244 	rb_link_node(&new->rf_node, parent, p);
245 	rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree);
246 }
247 
248 static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree)
249 {
250 	ocfs2_metadata_cache_exit(&tree->rf_ci);
251 	ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres);
252 	ocfs2_lock_res_free(&tree->rf_lockres);
253 	kfree(tree);
254 }
255 
256 static inline void
257 ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb,
258 					struct ocfs2_refcount_tree *tree)
259 {
260 	rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree);
261 	if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree)
262 		osb->osb_ref_tree_lru = NULL;
263 }
264 
265 static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
266 					struct ocfs2_refcount_tree *tree)
267 {
268 	spin_lock(&osb->osb_lock);
269 	ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
270 	spin_unlock(&osb->osb_lock);
271 }
272 
273 void ocfs2_kref_remove_refcount_tree(struct kref *kref)
274 {
275 	struct ocfs2_refcount_tree *tree =
276 		container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
277 
278 	ocfs2_free_refcount_tree(tree);
279 }
280 
281 static inline void
282 ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree)
283 {
284 	kref_get(&tree->rf_getcnt);
285 }
286 
287 static inline void
288 ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree)
289 {
290 	kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree);
291 }
292 
293 static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new,
294 					       struct super_block *sb)
295 {
296 	ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops);
297 	mutex_init(&new->rf_io_mutex);
298 	new->rf_sb = sb;
299 	spin_lock_init(&new->rf_lock);
300 }
301 
302 static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb,
303 					struct ocfs2_refcount_tree *new,
304 					u64 rf_blkno, u32 generation)
305 {
306 	init_rwsem(&new->rf_sem);
307 	ocfs2_refcount_lock_res_init(&new->rf_lockres, osb,
308 				     rf_blkno, generation);
309 }
310 
311 static struct ocfs2_refcount_tree*
312 ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno)
313 {
314 	struct ocfs2_refcount_tree *new;
315 
316 	new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS);
317 	if (!new)
318 		return NULL;
319 
320 	new->rf_blkno = rf_blkno;
321 	kref_init(&new->rf_getcnt);
322 	ocfs2_init_refcount_tree_ci(new, osb->sb);
323 
324 	return new;
325 }
326 
327 static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno,
328 				   struct ocfs2_refcount_tree **ret_tree)
329 {
330 	int ret = 0;
331 	struct ocfs2_refcount_tree *tree, *new = NULL;
332 	struct buffer_head *ref_root_bh = NULL;
333 	struct ocfs2_refcount_block *ref_rb;
334 
335 	spin_lock(&osb->osb_lock);
336 	if (osb->osb_ref_tree_lru &&
337 	    osb->osb_ref_tree_lru->rf_blkno == rf_blkno)
338 		tree = osb->osb_ref_tree_lru;
339 	else
340 		tree = ocfs2_find_refcount_tree(osb, rf_blkno);
341 	if (tree)
342 		goto out;
343 
344 	spin_unlock(&osb->osb_lock);
345 
346 	new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
347 	if (!new) {
348 		ret = -ENOMEM;
349 		mlog_errno(ret);
350 		return ret;
351 	}
352 	/*
353 	 * We need the generation to create the refcount tree lock and since
354 	 * it isn't changed during the tree modification, we are safe here to
355 	 * read without protection.
356 	 * We also have to purge the cache after we create the lock since the
357 	 * refcount block may have the stale data. It can only be trusted when
358 	 * we hold the refcount lock.
359 	 */
360 	ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
361 	if (ret) {
362 		mlog_errno(ret);
363 		ocfs2_metadata_cache_exit(&new->rf_ci);
364 		kfree(new);
365 		return ret;
366 	}
367 
368 	ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
369 	new->rf_generation = le32_to_cpu(ref_rb->rf_generation);
370 	ocfs2_init_refcount_tree_lock(osb, new, rf_blkno,
371 				      new->rf_generation);
372 	ocfs2_metadata_cache_purge(&new->rf_ci);
373 
374 	spin_lock(&osb->osb_lock);
375 	tree = ocfs2_find_refcount_tree(osb, rf_blkno);
376 	if (tree)
377 		goto out;
378 
379 	ocfs2_insert_refcount_tree(osb, new);
380 
381 	tree = new;
382 	new = NULL;
383 
384 out:
385 	*ret_tree = tree;
386 
387 	osb->osb_ref_tree_lru = tree;
388 
389 	spin_unlock(&osb->osb_lock);
390 
391 	if (new)
392 		ocfs2_free_refcount_tree(new);
393 
394 	brelse(ref_root_bh);
395 	return ret;
396 }
397 
398 static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno)
399 {
400 	int ret;
401 	struct buffer_head *di_bh = NULL;
402 	struct ocfs2_dinode *di;
403 
404 	ret = ocfs2_read_inode_block(inode, &di_bh);
405 	if (ret) {
406 		mlog_errno(ret);
407 		goto out;
408 	}
409 
410 	BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
411 
412 	di = (struct ocfs2_dinode *)di_bh->b_data;
413 	*ref_blkno = le64_to_cpu(di->i_refcount_loc);
414 	brelse(di_bh);
415 out:
416 	return ret;
417 }
418 
419 static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
420 				      struct ocfs2_refcount_tree *tree, int rw)
421 {
422 	int ret;
423 
424 	ret = ocfs2_refcount_lock(tree, rw);
425 	if (ret) {
426 		mlog_errno(ret);
427 		goto out;
428 	}
429 
430 	if (rw)
431 		down_write(&tree->rf_sem);
432 	else
433 		down_read(&tree->rf_sem);
434 
435 out:
436 	return ret;
437 }
438 
439 /*
440  * Lock the refcount tree pointed by ref_blkno and return the tree.
441  * In most case, we lock the tree and read the refcount block.
442  * So read it here if the caller really needs it.
443  *
444  * If the tree has been re-created by other node, it will free the
445  * old one and re-create it.
446  */
447 int ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
448 			     u64 ref_blkno, int rw,
449 			     struct ocfs2_refcount_tree **ret_tree,
450 			     struct buffer_head **ref_bh)
451 {
452 	int ret, delete_tree = 0;
453 	struct ocfs2_refcount_tree *tree = NULL;
454 	struct buffer_head *ref_root_bh = NULL;
455 	struct ocfs2_refcount_block *rb;
456 
457 again:
458 	ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
459 	if (ret) {
460 		mlog_errno(ret);
461 		return ret;
462 	}
463 
464 	ocfs2_refcount_tree_get(tree);
465 
466 	ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
467 	if (ret) {
468 		mlog_errno(ret);
469 		ocfs2_refcount_tree_put(tree);
470 		goto out;
471 	}
472 
473 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
474 					&ref_root_bh);
475 	if (ret) {
476 		mlog_errno(ret);
477 		ocfs2_unlock_refcount_tree(osb, tree, rw);
478 		ocfs2_refcount_tree_put(tree);
479 		goto out;
480 	}
481 
482 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
483 	/*
484 	 * If the refcount block has been freed and re-created, we may need
485 	 * to recreate the refcount tree also.
486 	 *
487 	 * Here we just remove the tree from the rb-tree, and the last
488 	 * kref holder will unlock and delete this refcount_tree.
489 	 * Then we goto "again" and ocfs2_get_refcount_tree will create
490 	 * the new refcount tree for us.
491 	 */
492 	if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) {
493 		if (!tree->rf_removed) {
494 			ocfs2_erase_refcount_tree_from_list(osb, tree);
495 			tree->rf_removed = 1;
496 			delete_tree = 1;
497 		}
498 
499 		ocfs2_unlock_refcount_tree(osb, tree, rw);
500 		/*
501 		 * We get an extra reference when we create the refcount
502 		 * tree, so another put will destroy it.
503 		 */
504 		if (delete_tree)
505 			ocfs2_refcount_tree_put(tree);
506 		brelse(ref_root_bh);
507 		ref_root_bh = NULL;
508 		goto again;
509 	}
510 
511 	*ret_tree = tree;
512 	if (ref_bh) {
513 		*ref_bh = ref_root_bh;
514 		ref_root_bh = NULL;
515 	}
516 out:
517 	brelse(ref_root_bh);
518 	return ret;
519 }
520 
521 int ocfs2_lock_refcount_tree_by_inode(struct inode *inode, int rw,
522 				      struct ocfs2_refcount_tree **ret_tree,
523 				      struct buffer_head **ref_bh)
524 {
525 	int ret;
526 	u64 ref_blkno;
527 
528 	ret = ocfs2_get_refcount_block(inode, &ref_blkno);
529 	if (ret) {
530 		mlog_errno(ret);
531 		return ret;
532 	}
533 
534 	return ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno,
535 					rw, ret_tree, ref_bh);
536 }
537 
538 void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
539 				struct ocfs2_refcount_tree *tree, int rw)
540 {
541 	if (rw)
542 		up_write(&tree->rf_sem);
543 	else
544 		up_read(&tree->rf_sem);
545 
546 	ocfs2_refcount_unlock(tree, rw);
547 	ocfs2_refcount_tree_put(tree);
548 }
549 
550 void ocfs2_purge_refcount_trees(struct ocfs2_super *osb)
551 {
552 	struct rb_node *node;
553 	struct ocfs2_refcount_tree *tree;
554 	struct rb_root *root = &osb->osb_rf_lock_tree;
555 
556 	while ((node = rb_last(root)) != NULL) {
557 		tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node);
558 
559 		mlog(0, "Purge tree %llu\n",
560 		     (unsigned long long) tree->rf_blkno);
561 
562 		rb_erase(&tree->rf_node, root);
563 		ocfs2_free_refcount_tree(tree);
564 	}
565 }
566 
567 /*
568  * Create a refcount tree for an inode.
569  * We take for granted that the inode is already locked.
570  */
571 static int ocfs2_create_refcount_tree(struct inode *inode,
572 				      struct buffer_head *di_bh)
573 {
574 	int ret;
575 	handle_t *handle = NULL;
576 	struct ocfs2_alloc_context *meta_ac = NULL;
577 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
578 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
579 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
580 	struct buffer_head *new_bh = NULL;
581 	struct ocfs2_refcount_block *rb;
582 	struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
583 	u16 suballoc_bit_start;
584 	u32 num_got;
585 	u64 first_blkno;
586 
587 	BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
588 
589 	mlog(0, "create tree for inode %lu\n", inode->i_ino);
590 
591 	ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
592 	if (ret) {
593 		mlog_errno(ret);
594 		goto out;
595 	}
596 
597 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS);
598 	if (IS_ERR(handle)) {
599 		ret = PTR_ERR(handle);
600 		mlog_errno(ret);
601 		goto out;
602 	}
603 
604 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
605 				      OCFS2_JOURNAL_ACCESS_WRITE);
606 	if (ret) {
607 		mlog_errno(ret);
608 		goto out_commit;
609 	}
610 
611 	ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
612 				   &suballoc_bit_start, &num_got,
613 				   &first_blkno);
614 	if (ret) {
615 		mlog_errno(ret);
616 		goto out_commit;
617 	}
618 
619 	new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
620 	if (!new_tree) {
621 		ret = -ENOMEM;
622 		mlog_errno(ret);
623 		goto out_commit;
624 	}
625 
626 	new_bh = sb_getblk(inode->i_sb, first_blkno);
627 	ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh);
628 
629 	ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh,
630 				      OCFS2_JOURNAL_ACCESS_CREATE);
631 	if (ret) {
632 		mlog_errno(ret);
633 		goto out_commit;
634 	}
635 
636 	/* Initialize ocfs2_refcount_block. */
637 	rb = (struct ocfs2_refcount_block *)new_bh->b_data;
638 	memset(rb, 0, inode->i_sb->s_blocksize);
639 	strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
640 	rb->rf_suballoc_slot = cpu_to_le16(osb->slot_num);
641 	rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
642 	rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
643 	rb->rf_blkno = cpu_to_le64(first_blkno);
644 	rb->rf_count = cpu_to_le32(1);
645 	rb->rf_records.rl_count =
646 			cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb));
647 	spin_lock(&osb->osb_lock);
648 	rb->rf_generation = osb->s_next_generation++;
649 	spin_unlock(&osb->osb_lock);
650 
651 	ocfs2_journal_dirty(handle, new_bh);
652 
653 	spin_lock(&oi->ip_lock);
654 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
655 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
656 	di->i_refcount_loc = cpu_to_le64(first_blkno);
657 	spin_unlock(&oi->ip_lock);
658 
659 	mlog(0, "created tree for inode %lu, refblock %llu\n",
660 	     inode->i_ino, (unsigned long long)first_blkno);
661 
662 	ocfs2_journal_dirty(handle, di_bh);
663 
664 	/*
665 	 * We have to init the tree lock here since it will use
666 	 * the generation number to create it.
667 	 */
668 	new_tree->rf_generation = le32_to_cpu(rb->rf_generation);
669 	ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
670 				      new_tree->rf_generation);
671 
672 	spin_lock(&osb->osb_lock);
673 	tree = ocfs2_find_refcount_tree(osb, first_blkno);
674 
675 	/*
676 	 * We've just created a new refcount tree in this block.  If
677 	 * we found a refcount tree on the ocfs2_super, it must be
678 	 * one we just deleted.  We free the old tree before
679 	 * inserting the new tree.
680 	 */
681 	BUG_ON(tree && tree->rf_generation == new_tree->rf_generation);
682 	if (tree)
683 		ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
684 	ocfs2_insert_refcount_tree(osb, new_tree);
685 	spin_unlock(&osb->osb_lock);
686 	new_tree = NULL;
687 	if (tree)
688 		ocfs2_refcount_tree_put(tree);
689 
690 out_commit:
691 	ocfs2_commit_trans(osb, handle);
692 
693 out:
694 	if (new_tree) {
695 		ocfs2_metadata_cache_exit(&new_tree->rf_ci);
696 		kfree(new_tree);
697 	}
698 
699 	brelse(new_bh);
700 	if (meta_ac)
701 		ocfs2_free_alloc_context(meta_ac);
702 
703 	return ret;
704 }
705 
706 static int ocfs2_set_refcount_tree(struct inode *inode,
707 				   struct buffer_head *di_bh,
708 				   u64 refcount_loc)
709 {
710 	int ret;
711 	handle_t *handle = NULL;
712 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
713 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
714 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
715 	struct buffer_head *ref_root_bh = NULL;
716 	struct ocfs2_refcount_block *rb;
717 	struct ocfs2_refcount_tree *ref_tree;
718 
719 	BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
720 
721 	ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
722 				       &ref_tree, &ref_root_bh);
723 	if (ret) {
724 		mlog_errno(ret);
725 		return ret;
726 	}
727 
728 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS);
729 	if (IS_ERR(handle)) {
730 		ret = PTR_ERR(handle);
731 		mlog_errno(ret);
732 		goto out;
733 	}
734 
735 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
736 				      OCFS2_JOURNAL_ACCESS_WRITE);
737 	if (ret) {
738 		mlog_errno(ret);
739 		goto out_commit;
740 	}
741 
742 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh,
743 				      OCFS2_JOURNAL_ACCESS_WRITE);
744 	if (ret) {
745 		mlog_errno(ret);
746 		goto out_commit;
747 	}
748 
749 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
750 	le32_add_cpu(&rb->rf_count, 1);
751 
752 	ocfs2_journal_dirty(handle, ref_root_bh);
753 
754 	spin_lock(&oi->ip_lock);
755 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
756 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
757 	di->i_refcount_loc = cpu_to_le64(refcount_loc);
758 	spin_unlock(&oi->ip_lock);
759 	ocfs2_journal_dirty(handle, di_bh);
760 
761 out_commit:
762 	ocfs2_commit_trans(osb, handle);
763 out:
764 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
765 	brelse(ref_root_bh);
766 
767 	return ret;
768 }
769 
770 int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
771 {
772 	int ret, delete_tree = 0;
773 	handle_t *handle = NULL;
774 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
775 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
776 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
777 	struct ocfs2_refcount_block *rb;
778 	struct inode *alloc_inode = NULL;
779 	struct buffer_head *alloc_bh = NULL;
780 	struct buffer_head *blk_bh = NULL;
781 	struct ocfs2_refcount_tree *ref_tree;
782 	int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS;
783 	u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc);
784 	u16 bit = 0;
785 
786 	if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL))
787 		return 0;
788 
789 	BUG_ON(!ref_blkno);
790 	ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh);
791 	if (ret) {
792 		mlog_errno(ret);
793 		return ret;
794 	}
795 
796 	rb = (struct ocfs2_refcount_block *)blk_bh->b_data;
797 
798 	/*
799 	 * If we are the last user, we need to free the block.
800 	 * So lock the allocator ahead.
801 	 */
802 	if (le32_to_cpu(rb->rf_count) == 1) {
803 		blk = le64_to_cpu(rb->rf_blkno);
804 		bit = le16_to_cpu(rb->rf_suballoc_bit);
805 		bg_blkno = ocfs2_which_suballoc_group(blk, bit);
806 
807 		alloc_inode = ocfs2_get_system_file_inode(osb,
808 					EXTENT_ALLOC_SYSTEM_INODE,
809 					le16_to_cpu(rb->rf_suballoc_slot));
810 		if (!alloc_inode) {
811 			ret = -ENOMEM;
812 			mlog_errno(ret);
813 			goto out;
814 		}
815 		mutex_lock(&alloc_inode->i_mutex);
816 
817 		ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1);
818 		if (ret) {
819 			mlog_errno(ret);
820 			goto out_mutex;
821 		}
822 
823 		credits += OCFS2_SUBALLOC_FREE;
824 	}
825 
826 	handle = ocfs2_start_trans(osb, credits);
827 	if (IS_ERR(handle)) {
828 		ret = PTR_ERR(handle);
829 		mlog_errno(ret);
830 		goto out_unlock;
831 	}
832 
833 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
834 				      OCFS2_JOURNAL_ACCESS_WRITE);
835 	if (ret) {
836 		mlog_errno(ret);
837 		goto out_commit;
838 	}
839 
840 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh,
841 				      OCFS2_JOURNAL_ACCESS_WRITE);
842 	if (ret) {
843 		mlog_errno(ret);
844 		goto out_commit;
845 	}
846 
847 	spin_lock(&oi->ip_lock);
848 	oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL;
849 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
850 	di->i_refcount_loc = 0;
851 	spin_unlock(&oi->ip_lock);
852 	ocfs2_journal_dirty(handle, di_bh);
853 
854 	le32_add_cpu(&rb->rf_count , -1);
855 	ocfs2_journal_dirty(handle, blk_bh);
856 
857 	if (!rb->rf_count) {
858 		delete_tree = 1;
859 		ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
860 		ret = ocfs2_free_suballoc_bits(handle, alloc_inode,
861 					       alloc_bh, bit, bg_blkno, 1);
862 		if (ret)
863 			mlog_errno(ret);
864 	}
865 
866 out_commit:
867 	ocfs2_commit_trans(osb, handle);
868 out_unlock:
869 	if (alloc_inode) {
870 		ocfs2_inode_unlock(alloc_inode, 1);
871 		brelse(alloc_bh);
872 	}
873 out_mutex:
874 	if (alloc_inode) {
875 		mutex_unlock(&alloc_inode->i_mutex);
876 		iput(alloc_inode);
877 	}
878 out:
879 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
880 	if (delete_tree)
881 		ocfs2_refcount_tree_put(ref_tree);
882 	brelse(blk_bh);
883 
884 	return ret;
885 }
886 
887 static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci,
888 					  struct buffer_head *ref_leaf_bh,
889 					  u64 cpos, unsigned int len,
890 					  struct ocfs2_refcount_rec *ret_rec,
891 					  int *index)
892 {
893 	int i = 0;
894 	struct ocfs2_refcount_block *rb =
895 		(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
896 	struct ocfs2_refcount_rec *rec = NULL;
897 
898 	for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) {
899 		rec = &rb->rf_records.rl_recs[i];
900 
901 		if (le64_to_cpu(rec->r_cpos) +
902 		    le32_to_cpu(rec->r_clusters) <= cpos)
903 			continue;
904 		else if (le64_to_cpu(rec->r_cpos) > cpos)
905 			break;
906 
907 		/* ok, cpos fail in this rec. Just return. */
908 		if (ret_rec)
909 			*ret_rec = *rec;
910 		goto out;
911 	}
912 
913 	if (ret_rec) {
914 		/* We meet with a hole here, so fake the rec. */
915 		ret_rec->r_cpos = cpu_to_le64(cpos);
916 		ret_rec->r_refcount = 0;
917 		if (i < le16_to_cpu(rb->rf_records.rl_used) &&
918 		    le64_to_cpu(rec->r_cpos) < cpos + len)
919 			ret_rec->r_clusters =
920 				cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos);
921 		else
922 			ret_rec->r_clusters = cpu_to_le32(len);
923 	}
924 
925 out:
926 	*index = i;
927 }
928 
929 /*
930  * Try to remove refcount tree. The mechanism is:
931  * 1) Check whether i_clusters == 0, if no, exit.
932  * 2) check whether we have i_xattr_loc in dinode. if yes, exit.
933  * 3) Check whether we have inline xattr stored outside, if yes, exit.
934  * 4) Remove the tree.
935  */
936 int ocfs2_try_remove_refcount_tree(struct inode *inode,
937 				   struct buffer_head *di_bh)
938 {
939 	int ret;
940 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
941 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
942 
943 	down_write(&oi->ip_xattr_sem);
944 	down_write(&oi->ip_alloc_sem);
945 
946 	if (oi->ip_clusters)
947 		goto out;
948 
949 	if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc)
950 		goto out;
951 
952 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL &&
953 	    ocfs2_has_inline_xattr_value_outside(inode, di))
954 		goto out;
955 
956 	ret = ocfs2_remove_refcount_tree(inode, di_bh);
957 	if (ret)
958 		mlog_errno(ret);
959 out:
960 	up_write(&oi->ip_alloc_sem);
961 	up_write(&oi->ip_xattr_sem);
962 	return 0;
963 }
964 
965 /*
966  * Given a cpos and len, try to find the refcount record which contains cpos.
967  * 1. If cpos can be found in one refcount record, return the record.
968  * 2. If cpos can't be found, return a fake record which start from cpos
969  *    and end at a small value between cpos+len and start of the next record.
970  *    This fake record has r_refcount = 0.
971  */
972 static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
973 				  struct buffer_head *ref_root_bh,
974 				  u64 cpos, unsigned int len,
975 				  struct ocfs2_refcount_rec *ret_rec,
976 				  int *index,
977 				  struct buffer_head **ret_bh)
978 {
979 	int ret = 0, i, found;
980 	u32 low_cpos;
981 	struct ocfs2_extent_list *el;
982 	struct ocfs2_extent_rec *tmp, *rec = NULL;
983 	struct ocfs2_extent_block *eb;
984 	struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
985 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
986 	struct ocfs2_refcount_block *rb =
987 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
988 
989 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) {
990 		ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
991 					      ret_rec, index);
992 		*ret_bh = ref_root_bh;
993 		get_bh(ref_root_bh);
994 		return 0;
995 	}
996 
997 	el = &rb->rf_list;
998 	low_cpos = cpos & OCFS2_32BIT_POS_MASK;
999 
1000 	if (el->l_tree_depth) {
1001 		ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh);
1002 		if (ret) {
1003 			mlog_errno(ret);
1004 			goto out;
1005 		}
1006 
1007 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1008 		el = &eb->h_list;
1009 
1010 		if (el->l_tree_depth) {
1011 			ocfs2_error(sb,
1012 			"refcount tree %llu has non zero tree "
1013 			"depth in leaf btree tree block %llu\n",
1014 			(unsigned long long)ocfs2_metadata_cache_owner(ci),
1015 			(unsigned long long)eb_bh->b_blocknr);
1016 			ret = -EROFS;
1017 			goto out;
1018 		}
1019 	}
1020 
1021 	found = 0;
1022 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1023 		rec = &el->l_recs[i];
1024 
1025 		if (le32_to_cpu(rec->e_cpos) <= low_cpos) {
1026 			found = 1;
1027 			break;
1028 		}
1029 	}
1030 
1031 	/* adjust len when we have ocfs2_extent_rec after it. */
1032 	if (found && i < le16_to_cpu(el->l_next_free_rec) - 1) {
1033 		tmp = &el->l_recs[i+1];
1034 
1035 		if (le32_to_cpu(tmp->e_cpos) < cpos + len)
1036 			len = le32_to_cpu(tmp->e_cpos) - cpos;
1037 	}
1038 
1039 	ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
1040 					&ref_leaf_bh);
1041 	if (ret) {
1042 		mlog_errno(ret);
1043 		goto out;
1044 	}
1045 
1046 	ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
1047 				      ret_rec, index);
1048 	*ret_bh = ref_leaf_bh;
1049 out:
1050 	brelse(eb_bh);
1051 	return ret;
1052 }
1053 
1054 enum ocfs2_ref_rec_contig {
1055 	REF_CONTIG_NONE = 0,
1056 	REF_CONTIG_LEFT,
1057 	REF_CONTIG_RIGHT,
1058 	REF_CONTIG_LEFTRIGHT,
1059 };
1060 
1061 static enum ocfs2_ref_rec_contig
1062 	ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb,
1063 				    int index)
1064 {
1065 	if ((rb->rf_records.rl_recs[index].r_refcount ==
1066 	    rb->rf_records.rl_recs[index + 1].r_refcount) &&
1067 	    (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) +
1068 	    le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) ==
1069 	    le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos)))
1070 		return REF_CONTIG_RIGHT;
1071 
1072 	return REF_CONTIG_NONE;
1073 }
1074 
1075 static enum ocfs2_ref_rec_contig
1076 	ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb,
1077 				  int index)
1078 {
1079 	enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE;
1080 
1081 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 1)
1082 		ret = ocfs2_refcount_rec_adjacent(rb, index);
1083 
1084 	if (index > 0) {
1085 		enum ocfs2_ref_rec_contig tmp;
1086 
1087 		tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
1088 
1089 		if (tmp == REF_CONTIG_RIGHT) {
1090 			if (ret == REF_CONTIG_RIGHT)
1091 				ret = REF_CONTIG_LEFTRIGHT;
1092 			else
1093 				ret = REF_CONTIG_LEFT;
1094 		}
1095 	}
1096 
1097 	return ret;
1098 }
1099 
1100 static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb,
1101 					   int index)
1102 {
1103 	BUG_ON(rb->rf_records.rl_recs[index].r_refcount !=
1104 	       rb->rf_records.rl_recs[index+1].r_refcount);
1105 
1106 	le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters,
1107 		     le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters));
1108 
1109 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 2)
1110 		memmove(&rb->rf_records.rl_recs[index + 1],
1111 			&rb->rf_records.rl_recs[index + 2],
1112 			sizeof(struct ocfs2_refcount_rec) *
1113 			(le16_to_cpu(rb->rf_records.rl_used) - index - 2));
1114 
1115 	memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1],
1116 	       0, sizeof(struct ocfs2_refcount_rec));
1117 	le16_add_cpu(&rb->rf_records.rl_used, -1);
1118 }
1119 
1120 /*
1121  * Merge the refcount rec if we are contiguous with the adjacent recs.
1122  */
1123 static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb,
1124 				     int index)
1125 {
1126 	enum ocfs2_ref_rec_contig contig =
1127 				ocfs2_refcount_rec_contig(rb, index);
1128 
1129 	if (contig == REF_CONTIG_NONE)
1130 		return;
1131 
1132 	if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) {
1133 		BUG_ON(index == 0);
1134 		index--;
1135 	}
1136 
1137 	ocfs2_rotate_refcount_rec_left(rb, index);
1138 
1139 	if (contig == REF_CONTIG_LEFTRIGHT)
1140 		ocfs2_rotate_refcount_rec_left(rb, index);
1141 }
1142 
1143 /*
1144  * Change the refcount indexed by "index" in ref_bh.
1145  * If refcount reaches 0, remove it.
1146  */
1147 static int ocfs2_change_refcount_rec(handle_t *handle,
1148 				     struct ocfs2_caching_info *ci,
1149 				     struct buffer_head *ref_leaf_bh,
1150 				     int index, int merge, int change)
1151 {
1152 	int ret;
1153 	struct ocfs2_refcount_block *rb =
1154 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1155 	struct ocfs2_refcount_list *rl = &rb->rf_records;
1156 	struct ocfs2_refcount_rec *rec = &rl->rl_recs[index];
1157 
1158 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1159 				      OCFS2_JOURNAL_ACCESS_WRITE);
1160 	if (ret) {
1161 		mlog_errno(ret);
1162 		goto out;
1163 	}
1164 
1165 	mlog(0, "change index %d, old count %u, change %d\n", index,
1166 	     le32_to_cpu(rec->r_refcount), change);
1167 	le32_add_cpu(&rec->r_refcount, change);
1168 
1169 	if (!rec->r_refcount) {
1170 		if (index != le16_to_cpu(rl->rl_used) - 1) {
1171 			memmove(rec, rec + 1,
1172 				(le16_to_cpu(rl->rl_used) - index - 1) *
1173 				sizeof(struct ocfs2_refcount_rec));
1174 			memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1],
1175 			       0, sizeof(struct ocfs2_refcount_rec));
1176 		}
1177 
1178 		le16_add_cpu(&rl->rl_used, -1);
1179 	} else if (merge)
1180 		ocfs2_refcount_rec_merge(rb, index);
1181 
1182 	ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
1183 	if (ret)
1184 		mlog_errno(ret);
1185 out:
1186 	return ret;
1187 }
1188 
1189 static int ocfs2_expand_inline_ref_root(handle_t *handle,
1190 					struct ocfs2_caching_info *ci,
1191 					struct buffer_head *ref_root_bh,
1192 					struct buffer_head **ref_leaf_bh,
1193 					struct ocfs2_alloc_context *meta_ac)
1194 {
1195 	int ret;
1196 	u16 suballoc_bit_start;
1197 	u32 num_got;
1198 	u64 blkno;
1199 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1200 	struct buffer_head *new_bh = NULL;
1201 	struct ocfs2_refcount_block *new_rb;
1202 	struct ocfs2_refcount_block *root_rb =
1203 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1204 
1205 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1206 				      OCFS2_JOURNAL_ACCESS_WRITE);
1207 	if (ret) {
1208 		mlog_errno(ret);
1209 		goto out;
1210 	}
1211 
1212 	ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
1213 				   &suballoc_bit_start, &num_got,
1214 				   &blkno);
1215 	if (ret) {
1216 		mlog_errno(ret);
1217 		goto out;
1218 	}
1219 
1220 	new_bh = sb_getblk(sb, blkno);
1221 	if (new_bh == NULL) {
1222 		ret = -EIO;
1223 		mlog_errno(ret);
1224 		goto out;
1225 	}
1226 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1227 
1228 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1229 				      OCFS2_JOURNAL_ACCESS_CREATE);
1230 	if (ret) {
1231 		mlog_errno(ret);
1232 		goto out;
1233 	}
1234 
1235 	/*
1236 	 * Initialize ocfs2_refcount_block.
1237 	 * It should contain the same information as the old root.
1238 	 * so just memcpy it and change the corresponding field.
1239 	 */
1240 	memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
1241 
1242 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1243 	new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num);
1244 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1245 	new_rb->rf_blkno = cpu_to_le64(blkno);
1246 	new_rb->rf_cpos = cpu_to_le32(0);
1247 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1248 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1249 	ocfs2_journal_dirty(handle, new_bh);
1250 
1251 	/* Now change the root. */
1252 	memset(&root_rb->rf_list, 0, sb->s_blocksize -
1253 	       offsetof(struct ocfs2_refcount_block, rf_list));
1254 	root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb));
1255 	root_rb->rf_clusters = cpu_to_le32(1);
1256 	root_rb->rf_list.l_next_free_rec = cpu_to_le16(1);
1257 	root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
1258 	root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
1259 	root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL);
1260 
1261 	ocfs2_journal_dirty(handle, ref_root_bh);
1262 
1263 	mlog(0, "new leaf block %llu, used %u\n", (unsigned long long)blkno,
1264 	     le16_to_cpu(new_rb->rf_records.rl_used));
1265 
1266 	*ref_leaf_bh = new_bh;
1267 	new_bh = NULL;
1268 out:
1269 	brelse(new_bh);
1270 	return ret;
1271 }
1272 
1273 static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev,
1274 					   struct ocfs2_refcount_rec *next)
1275 {
1276 	if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <=
1277 		ocfs2_get_ref_rec_low_cpos(next))
1278 		return 1;
1279 
1280 	return 0;
1281 }
1282 
1283 static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b)
1284 {
1285 	const struct ocfs2_refcount_rec *l = a, *r = b;
1286 	u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
1287 	u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
1288 
1289 	if (l_cpos > r_cpos)
1290 		return 1;
1291 	if (l_cpos < r_cpos)
1292 		return -1;
1293 	return 0;
1294 }
1295 
1296 static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
1297 {
1298 	const struct ocfs2_refcount_rec *l = a, *r = b;
1299 	u64 l_cpos = le64_to_cpu(l->r_cpos);
1300 	u64 r_cpos = le64_to_cpu(r->r_cpos);
1301 
1302 	if (l_cpos > r_cpos)
1303 		return 1;
1304 	if (l_cpos < r_cpos)
1305 		return -1;
1306 	return 0;
1307 }
1308 
1309 static void swap_refcount_rec(void *a, void *b, int size)
1310 {
1311 	struct ocfs2_refcount_rec *l = a, *r = b, tmp;
1312 
1313 	tmp = *(struct ocfs2_refcount_rec *)l;
1314 	*(struct ocfs2_refcount_rec *)l =
1315 			*(struct ocfs2_refcount_rec *)r;
1316 	*(struct ocfs2_refcount_rec *)r = tmp;
1317 }
1318 
1319 /*
1320  * The refcount cpos are ordered by their 64bit cpos,
1321  * But we will use the low 32 bit to be the e_cpos in the b-tree.
1322  * So we need to make sure that this pos isn't intersected with others.
1323  *
1324  * Note: The refcount block is already sorted by their low 32 bit cpos,
1325  *       So just try the middle pos first, and we will exit when we find
1326  *       the good position.
1327  */
1328 static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl,
1329 					 u32 *split_pos, int *split_index)
1330 {
1331 	int num_used = le16_to_cpu(rl->rl_used);
1332 	int delta, middle = num_used / 2;
1333 
1334 	for (delta = 0; delta < middle; delta++) {
1335 		/* Let's check delta earlier than middle */
1336 		if (ocfs2_refcount_rec_no_intersect(
1337 					&rl->rl_recs[middle - delta - 1],
1338 					&rl->rl_recs[middle - delta])) {
1339 			*split_index = middle - delta;
1340 			break;
1341 		}
1342 
1343 		/* For even counts, don't walk off the end */
1344 		if ((middle + delta + 1) == num_used)
1345 			continue;
1346 
1347 		/* Now try delta past middle */
1348 		if (ocfs2_refcount_rec_no_intersect(
1349 					&rl->rl_recs[middle + delta],
1350 					&rl->rl_recs[middle + delta + 1])) {
1351 			*split_index = middle + delta + 1;
1352 			break;
1353 		}
1354 	}
1355 
1356 	if (delta >= middle)
1357 		return -ENOSPC;
1358 
1359 	*split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]);
1360 	return 0;
1361 }
1362 
1363 static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
1364 					    struct buffer_head *new_bh,
1365 					    u32 *split_cpos)
1366 {
1367 	int split_index = 0, num_moved, ret;
1368 	u32 cpos = 0;
1369 	struct ocfs2_refcount_block *rb =
1370 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1371 	struct ocfs2_refcount_list *rl = &rb->rf_records;
1372 	struct ocfs2_refcount_block *new_rb =
1373 			(struct ocfs2_refcount_block *)new_bh->b_data;
1374 	struct ocfs2_refcount_list *new_rl = &new_rb->rf_records;
1375 
1376 	mlog(0, "split old leaf refcount block %llu, count = %u, used = %u\n",
1377 	     (unsigned long long)ref_leaf_bh->b_blocknr,
1378 	     le32_to_cpu(rl->rl_count), le32_to_cpu(rl->rl_used));
1379 
1380 	/*
1381 	 * XXX: Improvement later.
1382 	 * If we know all the high 32 bit cpos is the same, no need to sort.
1383 	 *
1384 	 * In order to make the whole process safe, we do:
1385 	 * 1. sort the entries by their low 32 bit cpos first so that we can
1386 	 *    find the split cpos easily.
1387 	 * 2. call ocfs2_insert_extent to insert the new refcount block.
1388 	 * 3. move the refcount rec to the new block.
1389 	 * 4. sort the entries by their 64 bit cpos.
1390 	 * 5. dirty the new_rb and rb.
1391 	 */
1392 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1393 	     sizeof(struct ocfs2_refcount_rec),
1394 	     cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
1395 
1396 	ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
1397 	if (ret) {
1398 		mlog_errno(ret);
1399 		return ret;
1400 	}
1401 
1402 	new_rb->rf_cpos = cpu_to_le32(cpos);
1403 
1404 	/* move refcount records starting from split_index to the new block. */
1405 	num_moved = le16_to_cpu(rl->rl_used) - split_index;
1406 	memcpy(new_rl->rl_recs, &rl->rl_recs[split_index],
1407 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1408 
1409 	/*ok, remove the entries we just moved over to the other block. */
1410 	memset(&rl->rl_recs[split_index], 0,
1411 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1412 
1413 	/* change old and new rl_used accordingly. */
1414 	le16_add_cpu(&rl->rl_used, -num_moved);
1415 	new_rl->rl_used = cpu_to_le32(num_moved);
1416 
1417 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1418 	     sizeof(struct ocfs2_refcount_rec),
1419 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1420 
1421 	sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
1422 	     sizeof(struct ocfs2_refcount_rec),
1423 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1424 
1425 	*split_cpos = cpos;
1426 	return 0;
1427 }
1428 
1429 static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1430 					 struct ocfs2_caching_info *ci,
1431 					 struct buffer_head *ref_root_bh,
1432 					 struct buffer_head *ref_leaf_bh,
1433 					 struct ocfs2_alloc_context *meta_ac)
1434 {
1435 	int ret;
1436 	u16 suballoc_bit_start;
1437 	u32 num_got, new_cpos;
1438 	u64 blkno;
1439 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1440 	struct ocfs2_refcount_block *root_rb =
1441 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1442 	struct buffer_head *new_bh = NULL;
1443 	struct ocfs2_refcount_block *new_rb;
1444 	struct ocfs2_extent_tree ref_et;
1445 
1446 	BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL));
1447 
1448 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1449 				      OCFS2_JOURNAL_ACCESS_WRITE);
1450 	if (ret) {
1451 		mlog_errno(ret);
1452 		goto out;
1453 	}
1454 
1455 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1456 				      OCFS2_JOURNAL_ACCESS_WRITE);
1457 	if (ret) {
1458 		mlog_errno(ret);
1459 		goto out;
1460 	}
1461 
1462 	ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
1463 				   &suballoc_bit_start, &num_got,
1464 				   &blkno);
1465 	if (ret) {
1466 		mlog_errno(ret);
1467 		goto out;
1468 	}
1469 
1470 	new_bh = sb_getblk(sb, blkno);
1471 	if (new_bh == NULL) {
1472 		ret = -EIO;
1473 		mlog_errno(ret);
1474 		goto out;
1475 	}
1476 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1477 
1478 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1479 				      OCFS2_JOURNAL_ACCESS_CREATE);
1480 	if (ret) {
1481 		mlog_errno(ret);
1482 		goto out;
1483 	}
1484 
1485 	/* Initialize ocfs2_refcount_block. */
1486 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1487 	memset(new_rb, 0, sb->s_blocksize);
1488 	strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
1489 	new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num);
1490 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1491 	new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1492 	new_rb->rf_blkno = cpu_to_le64(blkno);
1493 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1494 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1495 	new_rb->rf_records.rl_count =
1496 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
1497 	new_rb->rf_generation = root_rb->rf_generation;
1498 
1499 	ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
1500 	if (ret) {
1501 		mlog_errno(ret);
1502 		goto out;
1503 	}
1504 
1505 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1506 	ocfs2_journal_dirty(handle, new_bh);
1507 
1508 	ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh);
1509 
1510 	mlog(0, "insert new leaf block %llu at %u\n",
1511 	     (unsigned long long)new_bh->b_blocknr, new_cpos);
1512 
1513 	/* Insert the new leaf block with the specific offset cpos. */
1514 	ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr,
1515 				  1, 0, meta_ac);
1516 	if (ret)
1517 		mlog_errno(ret);
1518 
1519 out:
1520 	brelse(new_bh);
1521 	return ret;
1522 }
1523 
1524 static int ocfs2_expand_refcount_tree(handle_t *handle,
1525 				      struct ocfs2_caching_info *ci,
1526 				      struct buffer_head *ref_root_bh,
1527 				      struct buffer_head *ref_leaf_bh,
1528 				      struct ocfs2_alloc_context *meta_ac)
1529 {
1530 	int ret;
1531 	struct buffer_head *expand_bh = NULL;
1532 
1533 	if (ref_root_bh == ref_leaf_bh) {
1534 		/*
1535 		 * the old root bh hasn't been expanded to a b-tree,
1536 		 * so expand it first.
1537 		 */
1538 		ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
1539 						   &expand_bh, meta_ac);
1540 		if (ret) {
1541 			mlog_errno(ret);
1542 			goto out;
1543 		}
1544 	} else {
1545 		expand_bh = ref_leaf_bh;
1546 		get_bh(expand_bh);
1547 	}
1548 
1549 
1550 	/* Now add a new refcount block into the tree.*/
1551 	ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
1552 					    expand_bh, meta_ac);
1553 	if (ret)
1554 		mlog_errno(ret);
1555 out:
1556 	brelse(expand_bh);
1557 	return ret;
1558 }
1559 
1560 /*
1561  * Adjust the extent rec in b-tree representing ref_leaf_bh.
1562  *
1563  * Only called when we have inserted a new refcount rec at index 0
1564  * which means ocfs2_extent_rec.e_cpos may need some change.
1565  */
1566 static int ocfs2_adjust_refcount_rec(handle_t *handle,
1567 				     struct ocfs2_caching_info *ci,
1568 				     struct buffer_head *ref_root_bh,
1569 				     struct buffer_head *ref_leaf_bh,
1570 				     struct ocfs2_refcount_rec *rec)
1571 {
1572 	int ret = 0, i;
1573 	u32 new_cpos, old_cpos;
1574 	struct ocfs2_path *path = NULL;
1575 	struct ocfs2_extent_tree et;
1576 	struct ocfs2_refcount_block *rb =
1577 		(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1578 	struct ocfs2_extent_list *el;
1579 
1580 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL))
1581 		goto out;
1582 
1583 	rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1584 	old_cpos = le32_to_cpu(rb->rf_cpos);
1585 	new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK;
1586 	if (old_cpos <= new_cpos)
1587 		goto out;
1588 
1589 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
1590 
1591 	path = ocfs2_new_path_from_et(&et);
1592 	if (!path) {
1593 		ret = -ENOMEM;
1594 		mlog_errno(ret);
1595 		goto out;
1596 	}
1597 
1598 	ret = ocfs2_find_path(ci, path, old_cpos);
1599 	if (ret) {
1600 		mlog_errno(ret);
1601 		goto out;
1602 	}
1603 
1604 	/*
1605 	 * 2 more credits, one for the leaf refcount block, one for
1606 	 * the extent block contains the extent rec.
1607 	 */
1608 	ret = ocfs2_extend_trans(handle, handle->h_buffer_credits + 2);
1609 	if (ret < 0) {
1610 		mlog_errno(ret);
1611 		goto out;
1612 	}
1613 
1614 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1615 				      OCFS2_JOURNAL_ACCESS_WRITE);
1616 	if (ret < 0) {
1617 		mlog_errno(ret);
1618 		goto out;
1619 	}
1620 
1621 	ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path),
1622 				      OCFS2_JOURNAL_ACCESS_WRITE);
1623 	if (ret < 0) {
1624 		mlog_errno(ret);
1625 		goto out;
1626 	}
1627 
1628 	/* change the leaf extent block first. */
1629 	el = path_leaf_el(path);
1630 
1631 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++)
1632 		if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos)
1633 			break;
1634 
1635 	BUG_ON(i == le16_to_cpu(el->l_next_free_rec));
1636 
1637 	el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1638 
1639 	/* change the r_cpos in the leaf block. */
1640 	rb->rf_cpos = cpu_to_le32(new_cpos);
1641 
1642 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
1643 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1644 
1645 out:
1646 	ocfs2_free_path(path);
1647 	return ret;
1648 }
1649 
1650 static int ocfs2_insert_refcount_rec(handle_t *handle,
1651 				     struct ocfs2_caching_info *ci,
1652 				     struct buffer_head *ref_root_bh,
1653 				     struct buffer_head *ref_leaf_bh,
1654 				     struct ocfs2_refcount_rec *rec,
1655 				     int index, int merge,
1656 				     struct ocfs2_alloc_context *meta_ac)
1657 {
1658 	int ret;
1659 	struct ocfs2_refcount_block *rb =
1660 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1661 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1662 	struct buffer_head *new_bh = NULL;
1663 
1664 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1665 
1666 	if (rf_list->rl_used == rf_list->rl_count) {
1667 		u64 cpos = le64_to_cpu(rec->r_cpos);
1668 		u32 len = le32_to_cpu(rec->r_clusters);
1669 
1670 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1671 						 ref_leaf_bh, meta_ac);
1672 		if (ret) {
1673 			mlog_errno(ret);
1674 			goto out;
1675 		}
1676 
1677 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1678 					     cpos, len, NULL, &index,
1679 					     &new_bh);
1680 		if (ret) {
1681 			mlog_errno(ret);
1682 			goto out;
1683 		}
1684 
1685 		ref_leaf_bh = new_bh;
1686 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1687 		rf_list = &rb->rf_records;
1688 	}
1689 
1690 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1691 				      OCFS2_JOURNAL_ACCESS_WRITE);
1692 	if (ret) {
1693 		mlog_errno(ret);
1694 		goto out;
1695 	}
1696 
1697 	if (index < le16_to_cpu(rf_list->rl_used))
1698 		memmove(&rf_list->rl_recs[index + 1],
1699 			&rf_list->rl_recs[index],
1700 			(le16_to_cpu(rf_list->rl_used) - index) *
1701 			 sizeof(struct ocfs2_refcount_rec));
1702 
1703 	mlog(0, "insert refcount record start %llu, len %u, count %u "
1704 	     "to leaf block %llu at index %d\n",
1705 	     (unsigned long long)le64_to_cpu(rec->r_cpos),
1706 	     le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount),
1707 	     (unsigned long long)ref_leaf_bh->b_blocknr, index);
1708 
1709 	rf_list->rl_recs[index] = *rec;
1710 
1711 	le16_add_cpu(&rf_list->rl_used, 1);
1712 
1713 	if (merge)
1714 		ocfs2_refcount_rec_merge(rb, index);
1715 
1716 	ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
1717 	if (ret) {
1718 		mlog_errno(ret);
1719 		goto out;
1720 	}
1721 
1722 	if (index == 0) {
1723 		ret = ocfs2_adjust_refcount_rec(handle, ci,
1724 						ref_root_bh,
1725 						ref_leaf_bh, rec);
1726 		if (ret)
1727 			mlog_errno(ret);
1728 	}
1729 out:
1730 	brelse(new_bh);
1731 	return ret;
1732 }
1733 
1734 /*
1735  * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1736  * This is much simple than our b-tree code.
1737  * split_rec is the new refcount rec we want to insert.
1738  * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1739  * increase refcount or decrease a refcount to non-zero).
1740  * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1741  * rec( in case we decrease a refcount to zero).
1742  */
1743 static int ocfs2_split_refcount_rec(handle_t *handle,
1744 				    struct ocfs2_caching_info *ci,
1745 				    struct buffer_head *ref_root_bh,
1746 				    struct buffer_head *ref_leaf_bh,
1747 				    struct ocfs2_refcount_rec *split_rec,
1748 				    int index, int merge,
1749 				    struct ocfs2_alloc_context *meta_ac,
1750 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
1751 {
1752 	int ret, recs_need;
1753 	u32 len;
1754 	struct ocfs2_refcount_block *rb =
1755 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1756 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1757 	struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index];
1758 	struct ocfs2_refcount_rec *tail_rec = NULL;
1759 	struct buffer_head *new_bh = NULL;
1760 
1761 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1762 
1763 	mlog(0, "original r_pos %llu, cluster %u, split %llu, cluster %u\n",
1764 	     le64_to_cpu(orig_rec->r_cpos), le32_to_cpu(orig_rec->r_clusters),
1765 	     le64_to_cpu(split_rec->r_cpos),
1766 	     le32_to_cpu(split_rec->r_clusters));
1767 
1768 	/*
1769 	 * If we just need to split the header or tail clusters,
1770 	 * no more recs are needed, just split is OK.
1771 	 * Otherwise we at least need one new recs.
1772 	 */
1773 	if (!split_rec->r_refcount &&
1774 	    (split_rec->r_cpos == orig_rec->r_cpos ||
1775 	     le64_to_cpu(split_rec->r_cpos) +
1776 	     le32_to_cpu(split_rec->r_clusters) ==
1777 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1778 		recs_need = 0;
1779 	else
1780 		recs_need = 1;
1781 
1782 	/*
1783 	 * We need one more rec if we split in the middle and the new rec have
1784 	 * some refcount in it.
1785 	 */
1786 	if (split_rec->r_refcount &&
1787 	    (split_rec->r_cpos != orig_rec->r_cpos &&
1788 	     le64_to_cpu(split_rec->r_cpos) +
1789 	     le32_to_cpu(split_rec->r_clusters) !=
1790 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1791 		recs_need++;
1792 
1793 	/* If the leaf block don't have enough record, expand it. */
1794 	if (le16_to_cpu(rf_list->rl_used) + recs_need > rf_list->rl_count) {
1795 		struct ocfs2_refcount_rec tmp_rec;
1796 		u64 cpos = le64_to_cpu(orig_rec->r_cpos);
1797 		len = le32_to_cpu(orig_rec->r_clusters);
1798 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1799 						 ref_leaf_bh, meta_ac);
1800 		if (ret) {
1801 			mlog_errno(ret);
1802 			goto out;
1803 		}
1804 
1805 		/*
1806 		 * We have to re-get it since now cpos may be moved to
1807 		 * another leaf block.
1808 		 */
1809 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1810 					     cpos, len, &tmp_rec, &index,
1811 					     &new_bh);
1812 		if (ret) {
1813 			mlog_errno(ret);
1814 			goto out;
1815 		}
1816 
1817 		ref_leaf_bh = new_bh;
1818 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1819 		rf_list = &rb->rf_records;
1820 		orig_rec = &rf_list->rl_recs[index];
1821 	}
1822 
1823 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1824 				      OCFS2_JOURNAL_ACCESS_WRITE);
1825 	if (ret) {
1826 		mlog_errno(ret);
1827 		goto out;
1828 	}
1829 
1830 	/*
1831 	 * We have calculated out how many new records we need and store
1832 	 * in recs_need, so spare enough space first by moving the records
1833 	 * after "index" to the end.
1834 	 */
1835 	if (index != le16_to_cpu(rf_list->rl_used) - 1)
1836 		memmove(&rf_list->rl_recs[index + 1 + recs_need],
1837 			&rf_list->rl_recs[index + 1],
1838 			(le16_to_cpu(rf_list->rl_used) - index - 1) *
1839 			 sizeof(struct ocfs2_refcount_rec));
1840 
1841 	len = (le64_to_cpu(orig_rec->r_cpos) +
1842 	      le32_to_cpu(orig_rec->r_clusters)) -
1843 	      (le64_to_cpu(split_rec->r_cpos) +
1844 	      le32_to_cpu(split_rec->r_clusters));
1845 
1846 	/*
1847 	 * If we have "len", the we will split in the tail and move it
1848 	 * to the end of the space we have just spared.
1849 	 */
1850 	if (len) {
1851 		tail_rec = &rf_list->rl_recs[index + recs_need];
1852 
1853 		memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
1854 		le64_add_cpu(&tail_rec->r_cpos,
1855 			     le32_to_cpu(tail_rec->r_clusters) - len);
1856 		tail_rec->r_clusters = le32_to_cpu(len);
1857 	}
1858 
1859 	/*
1860 	 * If the split pos isn't the same as the original one, we need to
1861 	 * split in the head.
1862 	 *
1863 	 * Note: We have the chance that split_rec.r_refcount = 0,
1864 	 * recs_need = 0 and len > 0, which means we just cut the head from
1865 	 * the orig_rec and in that case we have done some modification in
1866 	 * orig_rec above, so the check for r_cpos is faked.
1867 	 */
1868 	if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
1869 		len = le64_to_cpu(split_rec->r_cpos) -
1870 		      le64_to_cpu(orig_rec->r_cpos);
1871 		orig_rec->r_clusters = cpu_to_le32(len);
1872 		index++;
1873 	}
1874 
1875 	le16_add_cpu(&rf_list->rl_used, recs_need);
1876 
1877 	if (split_rec->r_refcount) {
1878 		rf_list->rl_recs[index] = *split_rec;
1879 		mlog(0, "insert refcount record start %llu, len %u, count %u "
1880 		     "to leaf block %llu at index %d\n",
1881 		     (unsigned long long)le64_to_cpu(split_rec->r_cpos),
1882 		     le32_to_cpu(split_rec->r_clusters),
1883 		     le32_to_cpu(split_rec->r_refcount),
1884 		     (unsigned long long)ref_leaf_bh->b_blocknr, index);
1885 
1886 		if (merge)
1887 			ocfs2_refcount_rec_merge(rb, index);
1888 	}
1889 
1890 	ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
1891 	if (ret)
1892 		mlog_errno(ret);
1893 
1894 out:
1895 	brelse(new_bh);
1896 	return ret;
1897 }
1898 
1899 static int __ocfs2_increase_refcount(handle_t *handle,
1900 				     struct ocfs2_caching_info *ci,
1901 				     struct buffer_head *ref_root_bh,
1902 				     u64 cpos, u32 len, int merge,
1903 				     struct ocfs2_alloc_context *meta_ac,
1904 				     struct ocfs2_cached_dealloc_ctxt *dealloc)
1905 {
1906 	int ret = 0, index;
1907 	struct buffer_head *ref_leaf_bh = NULL;
1908 	struct ocfs2_refcount_rec rec;
1909 	unsigned int set_len = 0;
1910 
1911 	mlog(0, "Tree owner %llu, add refcount start %llu, len %u\n",
1912 	     (unsigned long long)ocfs2_metadata_cache_owner(ci),
1913 	     (unsigned long long)cpos, len);
1914 
1915 	while (len) {
1916 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1917 					     cpos, len, &rec, &index,
1918 					     &ref_leaf_bh);
1919 		if (ret) {
1920 			mlog_errno(ret);
1921 			goto out;
1922 		}
1923 
1924 		set_len = le32_to_cpu(rec.r_clusters);
1925 
1926 		/*
1927 		 * Here we may meet with 3 situations:
1928 		 *
1929 		 * 1. If we find an already existing record, and the length
1930 		 *    is the same, cool, we just need to increase the r_refcount
1931 		 *    and it is OK.
1932 		 * 2. If we find a hole, just insert it with r_refcount = 1.
1933 		 * 3. If we are in the middle of one extent record, split
1934 		 *    it.
1935 		 */
1936 		if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos &&
1937 		    set_len <= len) {
1938 			mlog(0, "increase refcount rec, start %llu, len %u, "
1939 			     "count %u\n", (unsigned long long)cpos, set_len,
1940 			     le32_to_cpu(rec.r_refcount));
1941 			ret = ocfs2_change_refcount_rec(handle, ci,
1942 							ref_leaf_bh, index,
1943 							merge, 1);
1944 			if (ret) {
1945 				mlog_errno(ret);
1946 				goto out;
1947 			}
1948 		} else if (!rec.r_refcount) {
1949 			rec.r_refcount = cpu_to_le32(1);
1950 
1951 			mlog(0, "insert refcount rec, start %llu, len %u\n",
1952 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
1953 			     set_len);
1954 			ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
1955 							ref_leaf_bh,
1956 							&rec, index,
1957 							merge, meta_ac);
1958 			if (ret) {
1959 				mlog_errno(ret);
1960 				goto out;
1961 			}
1962 		} else  {
1963 			set_len = min((u64)(cpos + len),
1964 				      le64_to_cpu(rec.r_cpos) + set_len) - cpos;
1965 			rec.r_cpos = cpu_to_le64(cpos);
1966 			rec.r_clusters = cpu_to_le32(set_len);
1967 			le32_add_cpu(&rec.r_refcount, 1);
1968 
1969 			mlog(0, "split refcount rec, start %llu, "
1970 			     "len %u, count %u\n",
1971 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
1972 			     set_len, le32_to_cpu(rec.r_refcount));
1973 			ret = ocfs2_split_refcount_rec(handle, ci,
1974 						       ref_root_bh, ref_leaf_bh,
1975 						       &rec, index, merge,
1976 						       meta_ac, dealloc);
1977 			if (ret) {
1978 				mlog_errno(ret);
1979 				goto out;
1980 			}
1981 		}
1982 
1983 		cpos += set_len;
1984 		len -= set_len;
1985 		brelse(ref_leaf_bh);
1986 		ref_leaf_bh = NULL;
1987 	}
1988 
1989 out:
1990 	brelse(ref_leaf_bh);
1991 	return ret;
1992 }
1993 
1994 static int ocfs2_remove_refcount_extent(handle_t *handle,
1995 				struct ocfs2_caching_info *ci,
1996 				struct buffer_head *ref_root_bh,
1997 				struct buffer_head *ref_leaf_bh,
1998 				struct ocfs2_alloc_context *meta_ac,
1999 				struct ocfs2_cached_dealloc_ctxt *dealloc)
2000 {
2001 	int ret;
2002 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
2003 	struct ocfs2_refcount_block *rb =
2004 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2005 	struct ocfs2_extent_tree et;
2006 
2007 	BUG_ON(rb->rf_records.rl_used);
2008 
2009 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
2010 	ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos),
2011 				  1, meta_ac, dealloc);
2012 	if (ret) {
2013 		mlog_errno(ret);
2014 		goto out;
2015 	}
2016 
2017 	ocfs2_remove_from_cache(ci, ref_leaf_bh);
2018 
2019 	/*
2020 	 * add the freed block to the dealloc so that it will be freed
2021 	 * when we run dealloc.
2022 	 */
2023 	ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
2024 					le16_to_cpu(rb->rf_suballoc_slot),
2025 					le64_to_cpu(rb->rf_blkno),
2026 					le16_to_cpu(rb->rf_suballoc_bit));
2027 	if (ret) {
2028 		mlog_errno(ret);
2029 		goto out;
2030 	}
2031 
2032 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
2033 				      OCFS2_JOURNAL_ACCESS_WRITE);
2034 	if (ret) {
2035 		mlog_errno(ret);
2036 		goto out;
2037 	}
2038 
2039 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
2040 
2041 	le32_add_cpu(&rb->rf_clusters, -1);
2042 
2043 	/*
2044 	 * check whether we need to restore the root refcount block if
2045 	 * there is no leaf extent block at atll.
2046 	 */
2047 	if (!rb->rf_list.l_next_free_rec) {
2048 		BUG_ON(rb->rf_clusters);
2049 
2050 		mlog(0, "reset refcount tree root %llu to be a record block.\n",
2051 		     (unsigned long long)ref_root_bh->b_blocknr);
2052 
2053 		rb->rf_flags = 0;
2054 		rb->rf_parent = 0;
2055 		rb->rf_cpos = 0;
2056 		memset(&rb->rf_records, 0, sb->s_blocksize -
2057 		       offsetof(struct ocfs2_refcount_block, rf_records));
2058 		rb->rf_records.rl_count =
2059 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
2060 	}
2061 
2062 	ocfs2_journal_dirty(handle, ref_root_bh);
2063 
2064 out:
2065 	return ret;
2066 }
2067 
2068 int ocfs2_increase_refcount(handle_t *handle,
2069 			    struct ocfs2_caching_info *ci,
2070 			    struct buffer_head *ref_root_bh,
2071 			    u64 cpos, u32 len,
2072 			    struct ocfs2_alloc_context *meta_ac,
2073 			    struct ocfs2_cached_dealloc_ctxt *dealloc)
2074 {
2075 	return __ocfs2_increase_refcount(handle, ci, ref_root_bh,
2076 					 cpos, len, 1,
2077 					 meta_ac, dealloc);
2078 }
2079 
2080 static int ocfs2_decrease_refcount_rec(handle_t *handle,
2081 				struct ocfs2_caching_info *ci,
2082 				struct buffer_head *ref_root_bh,
2083 				struct buffer_head *ref_leaf_bh,
2084 				int index, u64 cpos, unsigned int len,
2085 				struct ocfs2_alloc_context *meta_ac,
2086 				struct ocfs2_cached_dealloc_ctxt *dealloc)
2087 {
2088 	int ret;
2089 	struct ocfs2_refcount_block *rb =
2090 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2091 	struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index];
2092 
2093 	BUG_ON(cpos < le64_to_cpu(rec->r_cpos));
2094 	BUG_ON(cpos + len >
2095 	       le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters));
2096 
2097 	if (cpos == le64_to_cpu(rec->r_cpos) &&
2098 	    len == le32_to_cpu(rec->r_clusters))
2099 		ret = ocfs2_change_refcount_rec(handle, ci,
2100 						ref_leaf_bh, index, 1, -1);
2101 	else {
2102 		struct ocfs2_refcount_rec split = *rec;
2103 		split.r_cpos = cpu_to_le64(cpos);
2104 		split.r_clusters = cpu_to_le32(len);
2105 
2106 		le32_add_cpu(&split.r_refcount, -1);
2107 
2108 		mlog(0, "split refcount rec, start %llu, "
2109 		     "len %u, count %u, original start %llu, len %u\n",
2110 		     (unsigned long long)le64_to_cpu(split.r_cpos),
2111 		     len, le32_to_cpu(split.r_refcount),
2112 		     (unsigned long long)le64_to_cpu(rec->r_cpos),
2113 		     le32_to_cpu(rec->r_clusters));
2114 		ret = ocfs2_split_refcount_rec(handle, ci,
2115 					       ref_root_bh, ref_leaf_bh,
2116 					       &split, index, 1,
2117 					       meta_ac, dealloc);
2118 	}
2119 
2120 	if (ret) {
2121 		mlog_errno(ret);
2122 		goto out;
2123 	}
2124 
2125 	/* Remove the leaf refcount block if it contains no refcount record. */
2126 	if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) {
2127 		ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh,
2128 						   ref_leaf_bh, meta_ac,
2129 						   dealloc);
2130 		if (ret)
2131 			mlog_errno(ret);
2132 	}
2133 
2134 out:
2135 	return ret;
2136 }
2137 
2138 static int __ocfs2_decrease_refcount(handle_t *handle,
2139 				     struct ocfs2_caching_info *ci,
2140 				     struct buffer_head *ref_root_bh,
2141 				     u64 cpos, u32 len,
2142 				     struct ocfs2_alloc_context *meta_ac,
2143 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
2144 				     int delete)
2145 {
2146 	int ret = 0, index = 0;
2147 	struct ocfs2_refcount_rec rec;
2148 	unsigned int r_count = 0, r_len;
2149 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
2150 	struct buffer_head *ref_leaf_bh = NULL;
2151 
2152 	mlog(0, "Tree owner %llu, decrease refcount start %llu, "
2153 	     "len %u, delete %u\n",
2154 	     (unsigned long long)ocfs2_metadata_cache_owner(ci),
2155 	     (unsigned long long)cpos, len, delete);
2156 
2157 	while (len) {
2158 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2159 					     cpos, len, &rec, &index,
2160 					     &ref_leaf_bh);
2161 		if (ret) {
2162 			mlog_errno(ret);
2163 			goto out;
2164 		}
2165 
2166 		r_count = le32_to_cpu(rec.r_refcount);
2167 		BUG_ON(r_count == 0);
2168 		if (!delete)
2169 			BUG_ON(r_count > 1);
2170 
2171 		r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) +
2172 			      le32_to_cpu(rec.r_clusters)) - cpos;
2173 
2174 		ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
2175 						  ref_leaf_bh, index,
2176 						  cpos, r_len,
2177 						  meta_ac, dealloc);
2178 		if (ret) {
2179 			mlog_errno(ret);
2180 			goto out;
2181 		}
2182 
2183 		if (le32_to_cpu(rec.r_refcount) == 1 && delete) {
2184 			ret = ocfs2_cache_cluster_dealloc(dealloc,
2185 					  ocfs2_clusters_to_blocks(sb, cpos),
2186 							  r_len);
2187 			if (ret) {
2188 				mlog_errno(ret);
2189 				goto out;
2190 			}
2191 		}
2192 
2193 		cpos += r_len;
2194 		len -= r_len;
2195 		brelse(ref_leaf_bh);
2196 		ref_leaf_bh = NULL;
2197 	}
2198 
2199 out:
2200 	brelse(ref_leaf_bh);
2201 	return ret;
2202 }
2203 
2204 /* Caller must hold refcount tree lock. */
2205 int ocfs2_decrease_refcount(struct inode *inode,
2206 			    handle_t *handle, u32 cpos, u32 len,
2207 			    struct ocfs2_alloc_context *meta_ac,
2208 			    struct ocfs2_cached_dealloc_ctxt *dealloc,
2209 			    int delete)
2210 {
2211 	int ret;
2212 	u64 ref_blkno;
2213 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2214 	struct buffer_head *ref_root_bh = NULL;
2215 	struct ocfs2_refcount_tree *tree;
2216 
2217 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
2218 
2219 	ret = ocfs2_get_refcount_block(inode, &ref_blkno);
2220 	if (ret) {
2221 		mlog_errno(ret);
2222 		goto out;
2223 	}
2224 
2225 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree);
2226 	if (ret) {
2227 		mlog_errno(ret);
2228 		goto out;
2229 	}
2230 
2231 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
2232 					&ref_root_bh);
2233 	if (ret) {
2234 		mlog_errno(ret);
2235 		goto out;
2236 	}
2237 
2238 	ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh,
2239 					cpos, len, meta_ac, dealloc, delete);
2240 	if (ret)
2241 		mlog_errno(ret);
2242 out:
2243 	brelse(ref_root_bh);
2244 	return ret;
2245 }
2246 
2247 /*
2248  * Mark the already-existing extent at cpos as refcounted for len clusters.
2249  * This adds the refcount extent flag.
2250  *
2251  * If the existing extent is larger than the request, initiate a
2252  * split. An attempt will be made at merging with adjacent extents.
2253  *
2254  * The caller is responsible for passing down meta_ac if we'll need it.
2255  */
2256 static int ocfs2_mark_extent_refcounted(struct inode *inode,
2257 				struct ocfs2_extent_tree *et,
2258 				handle_t *handle, u32 cpos,
2259 				u32 len, u32 phys,
2260 				struct ocfs2_alloc_context *meta_ac,
2261 				struct ocfs2_cached_dealloc_ctxt *dealloc)
2262 {
2263 	int ret;
2264 
2265 	mlog(0, "Inode %lu refcount tree cpos %u, len %u, phys cluster %u\n",
2266 	     inode->i_ino, cpos, len, phys);
2267 
2268 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
2269 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
2270 			    "tree, but the feature bit is not set in the "
2271 			    "super block.", inode->i_ino);
2272 		ret = -EROFS;
2273 		goto out;
2274 	}
2275 
2276 	ret = ocfs2_change_extent_flag(handle, et, cpos,
2277 				       len, phys, meta_ac, dealloc,
2278 				       OCFS2_EXT_REFCOUNTED, 0);
2279 	if (ret)
2280 		mlog_errno(ret);
2281 
2282 out:
2283 	return ret;
2284 }
2285 
2286 /*
2287  * Given some contiguous physical clusters, calculate what we need
2288  * for modifying their refcount.
2289  */
2290 static int ocfs2_calc_refcount_meta_credits(struct super_block *sb,
2291 					    struct ocfs2_caching_info *ci,
2292 					    struct buffer_head *ref_root_bh,
2293 					    u64 start_cpos,
2294 					    u32 clusters,
2295 					    int *meta_add,
2296 					    int *credits)
2297 {
2298 	int ret = 0, index, ref_blocks = 0, recs_add = 0;
2299 	u64 cpos = start_cpos;
2300 	struct ocfs2_refcount_block *rb;
2301 	struct ocfs2_refcount_rec rec;
2302 	struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL;
2303 	u32 len;
2304 
2305 	mlog(0, "start_cpos %llu, clusters %u\n",
2306 	     (unsigned long long)start_cpos, clusters);
2307 	while (clusters) {
2308 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2309 					     cpos, clusters, &rec,
2310 					     &index, &ref_leaf_bh);
2311 		if (ret) {
2312 			mlog_errno(ret);
2313 			goto out;
2314 		}
2315 
2316 		if (ref_leaf_bh != prev_bh) {
2317 			/*
2318 			 * Now we encounter a new leaf block, so calculate
2319 			 * whether we need to extend the old leaf.
2320 			 */
2321 			if (prev_bh) {
2322 				rb = (struct ocfs2_refcount_block *)
2323 							prev_bh->b_data;
2324 
2325 				if (le64_to_cpu(rb->rf_records.rl_used) +
2326 				    recs_add >
2327 				    le16_to_cpu(rb->rf_records.rl_count))
2328 					ref_blocks++;
2329 			}
2330 
2331 			recs_add = 0;
2332 			*credits += 1;
2333 			brelse(prev_bh);
2334 			prev_bh = ref_leaf_bh;
2335 			get_bh(prev_bh);
2336 		}
2337 
2338 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2339 
2340 		mlog(0, "recs_add %d,cpos %llu, clusters %u, rec->r_cpos %llu,"
2341 		     "rec->r_clusters %u, rec->r_refcount %u, index %d\n",
2342 		     recs_add, (unsigned long long)cpos, clusters,
2343 		     (unsigned long long)le64_to_cpu(rec.r_cpos),
2344 		     le32_to_cpu(rec.r_clusters),
2345 		     le32_to_cpu(rec.r_refcount), index);
2346 
2347 		len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) +
2348 			  le32_to_cpu(rec.r_clusters)) - cpos;
2349 		/*
2350 		 * If the refcount rec already exist, cool. We just need
2351 		 * to check whether there is a split. Otherwise we just need
2352 		 * to increase the refcount.
2353 		 * If we will insert one, increases recs_add.
2354 		 *
2355 		 * We record all the records which will be inserted to the
2356 		 * same refcount block, so that we can tell exactly whether
2357 		 * we need a new refcount block or not.
2358 		 */
2359 		if (rec.r_refcount) {
2360 			/* Check whether we need a split at the beginning. */
2361 			if (cpos == start_cpos &&
2362 			    cpos != le64_to_cpu(rec.r_cpos))
2363 				recs_add++;
2364 
2365 			/* Check whether we need a split in the end. */
2366 			if (cpos + clusters < le64_to_cpu(rec.r_cpos) +
2367 			    le32_to_cpu(rec.r_clusters))
2368 				recs_add++;
2369 		} else
2370 			recs_add++;
2371 
2372 		brelse(ref_leaf_bh);
2373 		ref_leaf_bh = NULL;
2374 		clusters -= len;
2375 		cpos += len;
2376 	}
2377 
2378 	if (prev_bh) {
2379 		rb = (struct ocfs2_refcount_block *)prev_bh->b_data;
2380 
2381 		if (le64_to_cpu(rb->rf_records.rl_used) + recs_add >
2382 		    le16_to_cpu(rb->rf_records.rl_count))
2383 			ref_blocks++;
2384 
2385 		*credits += 1;
2386 	}
2387 
2388 	if (!ref_blocks)
2389 		goto out;
2390 
2391 	mlog(0, "we need ref_blocks %d\n", ref_blocks);
2392 	*meta_add += ref_blocks;
2393 	*credits += ref_blocks;
2394 
2395 	/*
2396 	 * So we may need ref_blocks to insert into the tree.
2397 	 * That also means we need to change the b-tree and add that number
2398 	 * of records since we never merge them.
2399 	 * We need one more block for expansion since the new created leaf
2400 	 * block is also full and needs split.
2401 	 */
2402 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
2403 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) {
2404 		struct ocfs2_extent_tree et;
2405 
2406 		ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
2407 		*meta_add += ocfs2_extend_meta_needed(et.et_root_el);
2408 		*credits += ocfs2_calc_extend_credits(sb,
2409 						      et.et_root_el,
2410 						      ref_blocks);
2411 	} else {
2412 		*credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
2413 		*meta_add += 1;
2414 	}
2415 
2416 out:
2417 	brelse(ref_leaf_bh);
2418 	brelse(prev_bh);
2419 	return ret;
2420 }
2421 
2422 /*
2423  * For refcount tree, we will decrease some contiguous clusters
2424  * refcount count, so just go through it to see how many blocks
2425  * we gonna touch and whether we need to create new blocks.
2426  *
2427  * Normally the refcount blocks store these refcount should be
2428  * continguous also, so that we can get the number easily.
2429  * As for meta_ac, we will at most add split 2 refcount record and
2430  * 2 more refcount block, so just check it in a rough way.
2431  *
2432  * Caller must hold refcount tree lock.
2433  */
2434 int ocfs2_prepare_refcount_change_for_del(struct inode *inode,
2435 					  struct buffer_head *di_bh,
2436 					  u64 phys_blkno,
2437 					  u32 clusters,
2438 					  int *credits,
2439 					  struct ocfs2_alloc_context **meta_ac)
2440 {
2441 	int ret, ref_blocks = 0;
2442 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2443 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2444 	struct buffer_head *ref_root_bh = NULL;
2445 	struct ocfs2_refcount_tree *tree;
2446 	u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno);
2447 
2448 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
2449 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
2450 			    "tree, but the feature bit is not set in the "
2451 			    "super block.", inode->i_ino);
2452 		ret = -EROFS;
2453 		goto out;
2454 	}
2455 
2456 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
2457 
2458 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb),
2459 				      le64_to_cpu(di->i_refcount_loc), &tree);
2460 	if (ret) {
2461 		mlog_errno(ret);
2462 		goto out;
2463 	}
2464 
2465 	ret = ocfs2_read_refcount_block(&tree->rf_ci,
2466 					le64_to_cpu(di->i_refcount_loc),
2467 					&ref_root_bh);
2468 	if (ret) {
2469 		mlog_errno(ret);
2470 		goto out;
2471 	}
2472 
2473 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
2474 					       &tree->rf_ci,
2475 					       ref_root_bh,
2476 					       start_cpos, clusters,
2477 					       &ref_blocks, credits);
2478 	if (ret) {
2479 		mlog_errno(ret);
2480 		goto out;
2481 	}
2482 
2483 	mlog(0, "reserve new metadata %d, credits = %d\n",
2484 	     ref_blocks, *credits);
2485 
2486 	if (ref_blocks) {
2487 		ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2488 							ref_blocks, meta_ac);
2489 		if (ret)
2490 			mlog_errno(ret);
2491 	}
2492 
2493 out:
2494 	brelse(ref_root_bh);
2495 	return ret;
2496 }
2497 
2498 #define	MAX_CONTIG_BYTES	1048576
2499 
2500 static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb)
2501 {
2502 	return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES);
2503 }
2504 
2505 static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb)
2506 {
2507 	return ~(ocfs2_cow_contig_clusters(sb) - 1);
2508 }
2509 
2510 /*
2511  * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
2512  * find an offset (start + (n * contig_clusters)) that is closest to cpos
2513  * while still being less than or equal to it.
2514  *
2515  * The goal is to break the extent at a multiple of contig_clusters.
2516  */
2517 static inline unsigned int ocfs2_cow_align_start(struct super_block *sb,
2518 						 unsigned int start,
2519 						 unsigned int cpos)
2520 {
2521 	BUG_ON(start > cpos);
2522 
2523 	return start + ((cpos - start) & ocfs2_cow_contig_mask(sb));
2524 }
2525 
2526 /*
2527  * Given a cluster count of len, pad it out so that it is a multiple
2528  * of contig_clusters.
2529  */
2530 static inline unsigned int ocfs2_cow_align_length(struct super_block *sb,
2531 						  unsigned int len)
2532 {
2533 	unsigned int padded =
2534 		(len + (ocfs2_cow_contig_clusters(sb) - 1)) &
2535 		ocfs2_cow_contig_mask(sb);
2536 
2537 	/* Did we wrap? */
2538 	if (padded < len)
2539 		padded = UINT_MAX;
2540 
2541 	return padded;
2542 }
2543 
2544 /*
2545  * Calculate out the start and number of virtual clusters we need to to CoW.
2546  *
2547  * cpos is vitual start cluster position we want to do CoW in a
2548  * file and write_len is the cluster length.
2549  * max_cpos is the place where we want to stop CoW intentionally.
2550  *
2551  * Normal we will start CoW from the beginning of extent record cotaining cpos.
2552  * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
2553  * get good I/O from the resulting extent tree.
2554  */
2555 static int ocfs2_refcount_cal_cow_clusters(struct inode *inode,
2556 					   struct ocfs2_extent_list *el,
2557 					   u32 cpos,
2558 					   u32 write_len,
2559 					   u32 max_cpos,
2560 					   u32 *cow_start,
2561 					   u32 *cow_len)
2562 {
2563 	int ret = 0;
2564 	int tree_height = le16_to_cpu(el->l_tree_depth), i;
2565 	struct buffer_head *eb_bh = NULL;
2566 	struct ocfs2_extent_block *eb = NULL;
2567 	struct ocfs2_extent_rec *rec;
2568 	unsigned int want_clusters, rec_end = 0;
2569 	int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb);
2570 	int leaf_clusters;
2571 
2572 	BUG_ON(cpos + write_len > max_cpos);
2573 
2574 	if (tree_height > 0) {
2575 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh);
2576 		if (ret) {
2577 			mlog_errno(ret);
2578 			goto out;
2579 		}
2580 
2581 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2582 		el = &eb->h_list;
2583 
2584 		if (el->l_tree_depth) {
2585 			ocfs2_error(inode->i_sb,
2586 				    "Inode %lu has non zero tree depth in "
2587 				    "leaf block %llu\n", inode->i_ino,
2588 				    (unsigned long long)eb_bh->b_blocknr);
2589 			ret = -EROFS;
2590 			goto out;
2591 		}
2592 	}
2593 
2594 	*cow_len = 0;
2595 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
2596 		rec = &el->l_recs[i];
2597 
2598 		if (ocfs2_is_empty_extent(rec)) {
2599 			mlog_bug_on_msg(i != 0, "Inode %lu has empty record in "
2600 					"index %d\n", inode->i_ino, i);
2601 			continue;
2602 		}
2603 
2604 		if (le32_to_cpu(rec->e_cpos) +
2605 		    le16_to_cpu(rec->e_leaf_clusters) <= cpos)
2606 			continue;
2607 
2608 		if (*cow_len == 0) {
2609 			/*
2610 			 * We should find a refcounted record in the
2611 			 * first pass.
2612 			 */
2613 			BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED));
2614 			*cow_start = le32_to_cpu(rec->e_cpos);
2615 		}
2616 
2617 		/*
2618 		 * If we encounter a hole, a non-refcounted record or
2619 		 * pass the max_cpos, stop the search.
2620 		 */
2621 		if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) ||
2622 		    (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) ||
2623 		    (max_cpos <= le32_to_cpu(rec->e_cpos)))
2624 			break;
2625 
2626 		leaf_clusters = le16_to_cpu(rec->e_leaf_clusters);
2627 		rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters;
2628 		if (rec_end > max_cpos) {
2629 			rec_end = max_cpos;
2630 			leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos);
2631 		}
2632 
2633 		/*
2634 		 * How many clusters do we actually need from
2635 		 * this extent?  First we see how many we actually
2636 		 * need to complete the write.  If that's smaller
2637 		 * than contig_clusters, we try for contig_clusters.
2638 		 */
2639 		if (!*cow_len)
2640 			want_clusters = write_len;
2641 		else
2642 			want_clusters = (cpos + write_len) -
2643 				(*cow_start + *cow_len);
2644 		if (want_clusters < contig_clusters)
2645 			want_clusters = contig_clusters;
2646 
2647 		/*
2648 		 * If the write does not cover the whole extent, we
2649 		 * need to calculate how we're going to split the extent.
2650 		 * We try to do it on contig_clusters boundaries.
2651 		 *
2652 		 * Any extent smaller than contig_clusters will be
2653 		 * CoWed in its entirety.
2654 		 */
2655 		if (leaf_clusters <= contig_clusters)
2656 			*cow_len += leaf_clusters;
2657 		else if (*cow_len || (*cow_start == cpos)) {
2658 			/*
2659 			 * This extent needs to be CoW'd from its
2660 			 * beginning, so all we have to do is compute
2661 			 * how many clusters to grab.  We align
2662 			 * want_clusters to the edge of contig_clusters
2663 			 * to get better I/O.
2664 			 */
2665 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
2666 							       want_clusters);
2667 
2668 			if (leaf_clusters < want_clusters)
2669 				*cow_len += leaf_clusters;
2670 			else
2671 				*cow_len += want_clusters;
2672 		} else if ((*cow_start + contig_clusters) >=
2673 			   (cpos + write_len)) {
2674 			/*
2675 			 * Breaking off contig_clusters at the front
2676 			 * of the extent will cover our write.  That's
2677 			 * easy.
2678 			 */
2679 			*cow_len = contig_clusters;
2680 		} else if ((rec_end - cpos) <= contig_clusters) {
2681 			/*
2682 			 * Breaking off contig_clusters at the tail of
2683 			 * this extent will cover cpos.
2684 			 */
2685 			*cow_start = rec_end - contig_clusters;
2686 			*cow_len = contig_clusters;
2687 		} else if ((rec_end - cpos) <= want_clusters) {
2688 			/*
2689 			 * While we can't fit the entire write in this
2690 			 * extent, we know that the write goes from cpos
2691 			 * to the end of the extent.  Break that off.
2692 			 * We try to break it at some multiple of
2693 			 * contig_clusters from the front of the extent.
2694 			 * Failing that (ie, cpos is within
2695 			 * contig_clusters of the front), we'll CoW the
2696 			 * entire extent.
2697 			 */
2698 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
2699 							   *cow_start, cpos);
2700 			*cow_len = rec_end - *cow_start;
2701 		} else {
2702 			/*
2703 			 * Ok, the entire write lives in the middle of
2704 			 * this extent.  Let's try to slice the extent up
2705 			 * nicely.  Optimally, our CoW region starts at
2706 			 * m*contig_clusters from the beginning of the
2707 			 * extent and goes for n*contig_clusters,
2708 			 * covering the entire write.
2709 			 */
2710 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
2711 							   *cow_start, cpos);
2712 
2713 			want_clusters = (cpos + write_len) - *cow_start;
2714 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
2715 							       want_clusters);
2716 			if (*cow_start + want_clusters <= rec_end)
2717 				*cow_len = want_clusters;
2718 			else
2719 				*cow_len = rec_end - *cow_start;
2720 		}
2721 
2722 		/* Have we covered our entire write yet? */
2723 		if ((*cow_start + *cow_len) >= (cpos + write_len))
2724 			break;
2725 
2726 		/*
2727 		 * If we reach the end of the extent block and don't get enough
2728 		 * clusters, continue with the next extent block if possible.
2729 		 */
2730 		if (i + 1 == le16_to_cpu(el->l_next_free_rec) &&
2731 		    eb && eb->h_next_leaf_blk) {
2732 			brelse(eb_bh);
2733 			eb_bh = NULL;
2734 
2735 			ret = ocfs2_read_extent_block(INODE_CACHE(inode),
2736 					       le64_to_cpu(eb->h_next_leaf_blk),
2737 					       &eb_bh);
2738 			if (ret) {
2739 				mlog_errno(ret);
2740 				goto out;
2741 			}
2742 
2743 			eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2744 			el = &eb->h_list;
2745 			i = -1;
2746 		}
2747 	}
2748 
2749 out:
2750 	brelse(eb_bh);
2751 	return ret;
2752 }
2753 
2754 /*
2755  * Prepare meta_ac, data_ac and calculate credits when we want to add some
2756  * num_clusters in data_tree "et" and change the refcount for the old
2757  * clusters(starting form p_cluster) in the refcount tree.
2758  *
2759  * Note:
2760  * 1. since we may split the old tree, so we at most will need num_clusters + 2
2761  *    more new leaf records.
2762  * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
2763  *    just give data_ac = NULL.
2764  */
2765 static int ocfs2_lock_refcount_allocators(struct super_block *sb,
2766 					u32 p_cluster, u32 num_clusters,
2767 					struct ocfs2_extent_tree *et,
2768 					struct ocfs2_caching_info *ref_ci,
2769 					struct buffer_head *ref_root_bh,
2770 					struct ocfs2_alloc_context **meta_ac,
2771 					struct ocfs2_alloc_context **data_ac,
2772 					int *credits)
2773 {
2774 	int ret = 0, meta_add = 0;
2775 	int num_free_extents = ocfs2_num_free_extents(OCFS2_SB(sb), et);
2776 
2777 	if (num_free_extents < 0) {
2778 		ret = num_free_extents;
2779 		mlog_errno(ret);
2780 		goto out;
2781 	}
2782 
2783 	if (num_free_extents < num_clusters + 2)
2784 		meta_add =
2785 			ocfs2_extend_meta_needed(et->et_root_el);
2786 
2787 	*credits += ocfs2_calc_extend_credits(sb, et->et_root_el,
2788 					      num_clusters + 2);
2789 
2790 	ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
2791 					       p_cluster, num_clusters,
2792 					       &meta_add, credits);
2793 	if (ret) {
2794 		mlog_errno(ret);
2795 		goto out;
2796 	}
2797 
2798 	mlog(0, "reserve new metadata %d, clusters %u, credits = %d\n",
2799 	     meta_add, num_clusters, *credits);
2800 	ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add,
2801 						meta_ac);
2802 	if (ret) {
2803 		mlog_errno(ret);
2804 		goto out;
2805 	}
2806 
2807 	if (data_ac) {
2808 		ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters,
2809 					     data_ac);
2810 		if (ret)
2811 			mlog_errno(ret);
2812 	}
2813 
2814 out:
2815 	if (ret) {
2816 		if (*meta_ac) {
2817 			ocfs2_free_alloc_context(*meta_ac);
2818 			*meta_ac = NULL;
2819 		}
2820 	}
2821 
2822 	return ret;
2823 }
2824 
2825 static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh)
2826 {
2827 	BUG_ON(buffer_dirty(bh));
2828 
2829 	clear_buffer_mapped(bh);
2830 
2831 	return 0;
2832 }
2833 
2834 static int ocfs2_duplicate_clusters_by_page(handle_t *handle,
2835 					    struct ocfs2_cow_context *context,
2836 					    u32 cpos, u32 old_cluster,
2837 					    u32 new_cluster, u32 new_len)
2838 {
2839 	int ret = 0, partial;
2840 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
2841 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
2842 	u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
2843 	struct page *page;
2844 	pgoff_t page_index;
2845 	unsigned int from, to;
2846 	loff_t offset, end, map_end;
2847 	struct address_space *mapping = context->inode->i_mapping;
2848 
2849 	mlog(0, "old_cluster %u, new %u, len %u at offset %u\n", old_cluster,
2850 	     new_cluster, new_len, cpos);
2851 
2852 	offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
2853 	end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits);
2854 
2855 	while (offset < end) {
2856 		page_index = offset >> PAGE_CACHE_SHIFT;
2857 		map_end = (page_index + 1) << PAGE_CACHE_SHIFT;
2858 		if (map_end > end)
2859 			map_end = end;
2860 
2861 		/* from, to is the offset within the page. */
2862 		from = offset & (PAGE_CACHE_SIZE - 1);
2863 		to = PAGE_CACHE_SIZE;
2864 		if (map_end & (PAGE_CACHE_SIZE - 1))
2865 			to = map_end & (PAGE_CACHE_SIZE - 1);
2866 
2867 		page = grab_cache_page(mapping, page_index);
2868 
2869 		/* This page can't be dirtied before we CoW it out. */
2870 		BUG_ON(PageDirty(page));
2871 
2872 		if (!PageUptodate(page)) {
2873 			ret = block_read_full_page(page, ocfs2_get_block);
2874 			if (ret) {
2875 				mlog_errno(ret);
2876 				goto unlock;
2877 			}
2878 			lock_page(page);
2879 		}
2880 
2881 		if (page_has_buffers(page)) {
2882 			ret = walk_page_buffers(handle, page_buffers(page),
2883 						from, to, &partial,
2884 						ocfs2_clear_cow_buffer);
2885 			if (ret) {
2886 				mlog_errno(ret);
2887 				goto unlock;
2888 			}
2889 		}
2890 
2891 		ocfs2_map_and_dirty_page(context->inode,
2892 					 handle, from, to,
2893 					 page, 0, &new_block);
2894 		mark_page_accessed(page);
2895 unlock:
2896 		unlock_page(page);
2897 		page_cache_release(page);
2898 		page = NULL;
2899 		offset = map_end;
2900 		if (ret)
2901 			break;
2902 	}
2903 
2904 	return ret;
2905 }
2906 
2907 static int ocfs2_duplicate_clusters_by_jbd(handle_t *handle,
2908 					   struct ocfs2_cow_context *context,
2909 					   u32 cpos, u32 old_cluster,
2910 					   u32 new_cluster, u32 new_len)
2911 {
2912 	int ret = 0;
2913 	struct super_block *sb = context->inode->i_sb;
2914 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
2915 	int i, blocks = ocfs2_clusters_to_blocks(sb, new_len);
2916 	u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster);
2917 	u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
2918 	struct ocfs2_super *osb = OCFS2_SB(sb);
2919 	struct buffer_head *old_bh = NULL;
2920 	struct buffer_head *new_bh = NULL;
2921 
2922 	mlog(0, "old_cluster %u, new %u, len %u\n", old_cluster,
2923 	     new_cluster, new_len);
2924 
2925 	for (i = 0; i < blocks; i++, old_block++, new_block++) {
2926 		new_bh = sb_getblk(osb->sb, new_block);
2927 		if (new_bh == NULL) {
2928 			ret = -EIO;
2929 			mlog_errno(ret);
2930 			break;
2931 		}
2932 
2933 		ocfs2_set_new_buffer_uptodate(ci, new_bh);
2934 
2935 		ret = ocfs2_read_block(ci, old_block, &old_bh, NULL);
2936 		if (ret) {
2937 			mlog_errno(ret);
2938 			break;
2939 		}
2940 
2941 		ret = ocfs2_journal_access(handle, ci, new_bh,
2942 					   OCFS2_JOURNAL_ACCESS_CREATE);
2943 		if (ret) {
2944 			mlog_errno(ret);
2945 			break;
2946 		}
2947 
2948 		memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize);
2949 		ret = ocfs2_journal_dirty(handle, new_bh);
2950 		if (ret) {
2951 			mlog_errno(ret);
2952 			break;
2953 		}
2954 
2955 		brelse(new_bh);
2956 		brelse(old_bh);
2957 		new_bh = NULL;
2958 		old_bh = NULL;
2959 	}
2960 
2961 	brelse(new_bh);
2962 	brelse(old_bh);
2963 	return ret;
2964 }
2965 
2966 static int ocfs2_clear_ext_refcount(handle_t *handle,
2967 				    struct ocfs2_extent_tree *et,
2968 				    u32 cpos, u32 p_cluster, u32 len,
2969 				    unsigned int ext_flags,
2970 				    struct ocfs2_alloc_context *meta_ac,
2971 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
2972 {
2973 	int ret, index;
2974 	struct ocfs2_extent_rec replace_rec;
2975 	struct ocfs2_path *path = NULL;
2976 	struct ocfs2_extent_list *el;
2977 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2978 	u64 ino = ocfs2_metadata_cache_owner(et->et_ci);
2979 
2980 	mlog(0, "inode %llu cpos %u, len %u, p_cluster %u, ext_flags %u\n",
2981 	     (unsigned long long)ino, cpos, len, p_cluster, ext_flags);
2982 
2983 	memset(&replace_rec, 0, sizeof(replace_rec));
2984 	replace_rec.e_cpos = cpu_to_le32(cpos);
2985 	replace_rec.e_leaf_clusters = cpu_to_le16(len);
2986 	replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb,
2987 								   p_cluster));
2988 	replace_rec.e_flags = ext_flags;
2989 	replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED;
2990 
2991 	path = ocfs2_new_path_from_et(et);
2992 	if (!path) {
2993 		ret = -ENOMEM;
2994 		mlog_errno(ret);
2995 		goto out;
2996 	}
2997 
2998 	ret = ocfs2_find_path(et->et_ci, path, cpos);
2999 	if (ret) {
3000 		mlog_errno(ret);
3001 		goto out;
3002 	}
3003 
3004 	el = path_leaf_el(path);
3005 
3006 	index = ocfs2_search_extent_list(el, cpos);
3007 	if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
3008 		ocfs2_error(sb,
3009 			    "Inode %llu has an extent at cpos %u which can no "
3010 			    "longer be found.\n",
3011 			    (unsigned long long)ino, cpos);
3012 		ret = -EROFS;
3013 		goto out;
3014 	}
3015 
3016 	ret = ocfs2_split_extent(handle, et, path, index,
3017 				 &replace_rec, meta_ac, dealloc);
3018 	if (ret)
3019 		mlog_errno(ret);
3020 
3021 out:
3022 	ocfs2_free_path(path);
3023 	return ret;
3024 }
3025 
3026 static int ocfs2_replace_clusters(handle_t *handle,
3027 				  struct ocfs2_cow_context *context,
3028 				  u32 cpos, u32 old,
3029 				  u32 new, u32 len,
3030 				  unsigned int ext_flags)
3031 {
3032 	int ret;
3033 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
3034 	u64 ino = ocfs2_metadata_cache_owner(ci);
3035 
3036 	mlog(0, "inode %llu, cpos %u, old %u, new %u, len %u, ext_flags %u\n",
3037 	     (unsigned long long)ino, cpos, old, new, len, ext_flags);
3038 
3039 	/*If the old clusters is unwritten, no need to duplicate. */
3040 	if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) {
3041 		ret = context->cow_duplicate_clusters(handle, context, cpos,
3042 						      old, new, len);
3043 		if (ret) {
3044 			mlog_errno(ret);
3045 			goto out;
3046 		}
3047 	}
3048 
3049 	ret = ocfs2_clear_ext_refcount(handle, &context->data_et,
3050 				       cpos, new, len, ext_flags,
3051 				       context->meta_ac, &context->dealloc);
3052 	if (ret)
3053 		mlog_errno(ret);
3054 out:
3055 	return ret;
3056 }
3057 
3058 static int ocfs2_cow_sync_writeback(struct super_block *sb,
3059 				    struct ocfs2_cow_context *context,
3060 				    u32 cpos, u32 num_clusters)
3061 {
3062 	int ret = 0;
3063 	loff_t offset, end, map_end;
3064 	pgoff_t page_index;
3065 	struct page *page;
3066 
3067 	if (ocfs2_should_order_data(context->inode))
3068 		return 0;
3069 
3070 	offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
3071 	end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits);
3072 
3073 	ret = filemap_fdatawrite_range(context->inode->i_mapping,
3074 				       offset, end - 1);
3075 	if (ret < 0) {
3076 		mlog_errno(ret);
3077 		return ret;
3078 	}
3079 
3080 	while (offset < end) {
3081 		page_index = offset >> PAGE_CACHE_SHIFT;
3082 		map_end = (page_index + 1) << PAGE_CACHE_SHIFT;
3083 		if (map_end > end)
3084 			map_end = end;
3085 
3086 		page = grab_cache_page(context->inode->i_mapping, page_index);
3087 		BUG_ON(!page);
3088 
3089 		wait_on_page_writeback(page);
3090 		if (PageError(page)) {
3091 			ret = -EIO;
3092 			mlog_errno(ret);
3093 		} else
3094 			mark_page_accessed(page);
3095 
3096 		unlock_page(page);
3097 		page_cache_release(page);
3098 		page = NULL;
3099 		offset = map_end;
3100 		if (ret)
3101 			break;
3102 	}
3103 
3104 	return ret;
3105 }
3106 
3107 static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context,
3108 				 u32 v_cluster, u32 *p_cluster,
3109 				 u32 *num_clusters,
3110 				 unsigned int *extent_flags)
3111 {
3112 	return ocfs2_get_clusters(context->inode, v_cluster, p_cluster,
3113 				  num_clusters, extent_flags);
3114 }
3115 
3116 static int ocfs2_make_clusters_writable(struct super_block *sb,
3117 					struct ocfs2_cow_context *context,
3118 					u32 cpos, u32 p_cluster,
3119 					u32 num_clusters, unsigned int e_flags)
3120 {
3121 	int ret, delete, index, credits =  0;
3122 	u32 new_bit, new_len;
3123 	unsigned int set_len;
3124 	struct ocfs2_super *osb = OCFS2_SB(sb);
3125 	handle_t *handle;
3126 	struct buffer_head *ref_leaf_bh = NULL;
3127 	struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci;
3128 	struct ocfs2_refcount_rec rec;
3129 
3130 	mlog(0, "cpos %u, p_cluster %u, num_clusters %u, e_flags %u\n",
3131 	     cpos, p_cluster, num_clusters, e_flags);
3132 
3133 	ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
3134 					     &context->data_et,
3135 					     ref_ci,
3136 					     context->ref_root_bh,
3137 					     &context->meta_ac,
3138 					     &context->data_ac, &credits);
3139 	if (ret) {
3140 		mlog_errno(ret);
3141 		return ret;
3142 	}
3143 
3144 	if (context->post_refcount)
3145 		credits += context->post_refcount->credits;
3146 
3147 	credits += context->extra_credits;
3148 	handle = ocfs2_start_trans(osb, credits);
3149 	if (IS_ERR(handle)) {
3150 		ret = PTR_ERR(handle);
3151 		mlog_errno(ret);
3152 		goto out;
3153 	}
3154 
3155 	while (num_clusters) {
3156 		ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh,
3157 					     p_cluster, num_clusters,
3158 					     &rec, &index, &ref_leaf_bh);
3159 		if (ret) {
3160 			mlog_errno(ret);
3161 			goto out_commit;
3162 		}
3163 
3164 		BUG_ON(!rec.r_refcount);
3165 		set_len = min((u64)p_cluster + num_clusters,
3166 			      le64_to_cpu(rec.r_cpos) +
3167 			      le32_to_cpu(rec.r_clusters)) - p_cluster;
3168 
3169 		/*
3170 		 * There are many different situation here.
3171 		 * 1. If refcount == 1, remove the flag and don't COW.
3172 		 * 2. If refcount > 1, allocate clusters.
3173 		 *    Here we may not allocate r_len once at a time, so continue
3174 		 *    until we reach num_clusters.
3175 		 */
3176 		if (le32_to_cpu(rec.r_refcount) == 1) {
3177 			delete = 0;
3178 			ret = ocfs2_clear_ext_refcount(handle,
3179 						       &context->data_et,
3180 						       cpos, p_cluster,
3181 						       set_len, e_flags,
3182 						       context->meta_ac,
3183 						       &context->dealloc);
3184 			if (ret) {
3185 				mlog_errno(ret);
3186 				goto out_commit;
3187 			}
3188 		} else {
3189 			delete = 1;
3190 
3191 			ret = __ocfs2_claim_clusters(osb, handle,
3192 						     context->data_ac,
3193 						     1, set_len,
3194 						     &new_bit, &new_len);
3195 			if (ret) {
3196 				mlog_errno(ret);
3197 				goto out_commit;
3198 			}
3199 
3200 			ret = ocfs2_replace_clusters(handle, context,
3201 						     cpos, p_cluster, new_bit,
3202 						     new_len, e_flags);
3203 			if (ret) {
3204 				mlog_errno(ret);
3205 				goto out_commit;
3206 			}
3207 			set_len = new_len;
3208 		}
3209 
3210 		ret = __ocfs2_decrease_refcount(handle, ref_ci,
3211 						context->ref_root_bh,
3212 						p_cluster, set_len,
3213 						context->meta_ac,
3214 						&context->dealloc, delete);
3215 		if (ret) {
3216 			mlog_errno(ret);
3217 			goto out_commit;
3218 		}
3219 
3220 		cpos += set_len;
3221 		p_cluster += set_len;
3222 		num_clusters -= set_len;
3223 		brelse(ref_leaf_bh);
3224 		ref_leaf_bh = NULL;
3225 	}
3226 
3227 	/* handle any post_cow action. */
3228 	if (context->post_refcount && context->post_refcount->func) {
3229 		ret = context->post_refcount->func(context->inode, handle,
3230 						context->post_refcount->para);
3231 		if (ret) {
3232 			mlog_errno(ret);
3233 			goto out_commit;
3234 		}
3235 	}
3236 
3237 	/*
3238 	 * Here we should write the new page out first if we are
3239 	 * in write-back mode.
3240 	 */
3241 	if (context->get_clusters == ocfs2_di_get_clusters) {
3242 		ret = ocfs2_cow_sync_writeback(sb, context, cpos, num_clusters);
3243 		if (ret)
3244 			mlog_errno(ret);
3245 	}
3246 
3247 out_commit:
3248 	ocfs2_commit_trans(osb, handle);
3249 
3250 out:
3251 	if (context->data_ac) {
3252 		ocfs2_free_alloc_context(context->data_ac);
3253 		context->data_ac = NULL;
3254 	}
3255 	if (context->meta_ac) {
3256 		ocfs2_free_alloc_context(context->meta_ac);
3257 		context->meta_ac = NULL;
3258 	}
3259 	brelse(ref_leaf_bh);
3260 
3261 	return ret;
3262 }
3263 
3264 static int ocfs2_replace_cow(struct ocfs2_cow_context *context)
3265 {
3266 	int ret = 0;
3267 	struct inode *inode = context->inode;
3268 	u32 cow_start = context->cow_start, cow_len = context->cow_len;
3269 	u32 p_cluster, num_clusters;
3270 	unsigned int ext_flags;
3271 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3272 
3273 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
3274 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
3275 			    "tree, but the feature bit is not set in the "
3276 			    "super block.", inode->i_ino);
3277 		return -EROFS;
3278 	}
3279 
3280 	ocfs2_init_dealloc_ctxt(&context->dealloc);
3281 
3282 	while (cow_len) {
3283 		ret = context->get_clusters(context, cow_start, &p_cluster,
3284 					    &num_clusters, &ext_flags);
3285 		if (ret) {
3286 			mlog_errno(ret);
3287 			break;
3288 		}
3289 
3290 		BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED));
3291 
3292 		if (cow_len < num_clusters)
3293 			num_clusters = cow_len;
3294 
3295 		ret = ocfs2_make_clusters_writable(inode->i_sb, context,
3296 						   cow_start, p_cluster,
3297 						   num_clusters, ext_flags);
3298 		if (ret) {
3299 			mlog_errno(ret);
3300 			break;
3301 		}
3302 
3303 		cow_len -= num_clusters;
3304 		cow_start += num_clusters;
3305 	}
3306 
3307 	if (ocfs2_dealloc_has_cluster(&context->dealloc)) {
3308 		ocfs2_schedule_truncate_log_flush(osb, 1);
3309 		ocfs2_run_deallocs(osb, &context->dealloc);
3310 	}
3311 
3312 	return ret;
3313 }
3314 
3315 /*
3316  * Starting at cpos, try to CoW write_len clusters.  Don't CoW
3317  * past max_cpos.  This will stop when it runs into a hole or an
3318  * unrefcounted extent.
3319  */
3320 static int ocfs2_refcount_cow_hunk(struct inode *inode,
3321 				   struct buffer_head *di_bh,
3322 				   u32 cpos, u32 write_len, u32 max_cpos)
3323 {
3324 	int ret;
3325 	u32 cow_start = 0, cow_len = 0;
3326 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
3327 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3328 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3329 	struct buffer_head *ref_root_bh = NULL;
3330 	struct ocfs2_refcount_tree *ref_tree;
3331 	struct ocfs2_cow_context *context = NULL;
3332 
3333 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
3334 
3335 	ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list,
3336 					      cpos, write_len, max_cpos,
3337 					      &cow_start, &cow_len);
3338 	if (ret) {
3339 		mlog_errno(ret);
3340 		goto out;
3341 	}
3342 
3343 	mlog(0, "CoW inode %lu, cpos %u, write_len %u, cow_start %u, "
3344 	     "cow_len %u\n", inode->i_ino,
3345 	     cpos, write_len, cow_start, cow_len);
3346 
3347 	BUG_ON(cow_len == 0);
3348 
3349 	context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3350 	if (!context) {
3351 		ret = -ENOMEM;
3352 		mlog_errno(ret);
3353 		goto out;
3354 	}
3355 
3356 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
3357 				       1, &ref_tree, &ref_root_bh);
3358 	if (ret) {
3359 		mlog_errno(ret);
3360 		goto out;
3361 	}
3362 
3363 	context->inode = inode;
3364 	context->cow_start = cow_start;
3365 	context->cow_len = cow_len;
3366 	context->ref_tree = ref_tree;
3367 	context->ref_root_bh = ref_root_bh;
3368 	context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page;
3369 	context->get_clusters = ocfs2_di_get_clusters;
3370 
3371 	ocfs2_init_dinode_extent_tree(&context->data_et,
3372 				      INODE_CACHE(inode), di_bh);
3373 
3374 	ret = ocfs2_replace_cow(context);
3375 	if (ret)
3376 		mlog_errno(ret);
3377 
3378 	/*
3379 	 * truncate the extent map here since no matter whether we meet with
3380 	 * any error during the action, we shouldn't trust cached extent map
3381 	 * any more.
3382 	 */
3383 	ocfs2_extent_map_trunc(inode, cow_start);
3384 
3385 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3386 	brelse(ref_root_bh);
3387 out:
3388 	kfree(context);
3389 	return ret;
3390 }
3391 
3392 /*
3393  * CoW any and all clusters between cpos and cpos+write_len.
3394  * Don't CoW past max_cpos.  If this returns successfully, all
3395  * clusters between cpos and cpos+write_len are safe to modify.
3396  */
3397 int ocfs2_refcount_cow(struct inode *inode,
3398 		       struct buffer_head *di_bh,
3399 		       u32 cpos, u32 write_len, u32 max_cpos)
3400 {
3401 	int ret = 0;
3402 	u32 p_cluster, num_clusters;
3403 	unsigned int ext_flags;
3404 
3405 	while (write_len) {
3406 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3407 					 &num_clusters, &ext_flags);
3408 		if (ret) {
3409 			mlog_errno(ret);
3410 			break;
3411 		}
3412 
3413 		if (write_len < num_clusters)
3414 			num_clusters = write_len;
3415 
3416 		if (ext_flags & OCFS2_EXT_REFCOUNTED) {
3417 			ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos,
3418 						      num_clusters, max_cpos);
3419 			if (ret) {
3420 				mlog_errno(ret);
3421 				break;
3422 			}
3423 		}
3424 
3425 		write_len -= num_clusters;
3426 		cpos += num_clusters;
3427 	}
3428 
3429 	return ret;
3430 }
3431 
3432 static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context,
3433 					  u32 v_cluster, u32 *p_cluster,
3434 					  u32 *num_clusters,
3435 					  unsigned int *extent_flags)
3436 {
3437 	struct inode *inode = context->inode;
3438 	struct ocfs2_xattr_value_root *xv = context->cow_object;
3439 
3440 	return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster,
3441 					num_clusters, &xv->xr_list,
3442 					extent_flags);
3443 }
3444 
3445 /*
3446  * Given a xattr value root, calculate the most meta/credits we need for
3447  * refcount tree change if we truncate it to 0.
3448  */
3449 int ocfs2_refcounted_xattr_delete_need(struct inode *inode,
3450 				       struct ocfs2_caching_info *ref_ci,
3451 				       struct buffer_head *ref_root_bh,
3452 				       struct ocfs2_xattr_value_root *xv,
3453 				       int *meta_add, int *credits)
3454 {
3455 	int ret = 0, index, ref_blocks = 0;
3456 	u32 p_cluster, num_clusters;
3457 	u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters);
3458 	struct ocfs2_refcount_block *rb;
3459 	struct ocfs2_refcount_rec rec;
3460 	struct buffer_head *ref_leaf_bh = NULL;
3461 
3462 	while (cpos < clusters) {
3463 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
3464 					       &num_clusters, &xv->xr_list,
3465 					       NULL);
3466 		if (ret) {
3467 			mlog_errno(ret);
3468 			goto out;
3469 		}
3470 
3471 		cpos += num_clusters;
3472 
3473 		while (num_clusters) {
3474 			ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh,
3475 						     p_cluster, num_clusters,
3476 						     &rec, &index,
3477 						     &ref_leaf_bh);
3478 			if (ret) {
3479 				mlog_errno(ret);
3480 				goto out;
3481 			}
3482 
3483 			BUG_ON(!rec.r_refcount);
3484 
3485 			rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
3486 
3487 			/*
3488 			 * We really don't know whether the other clusters is in
3489 			 * this refcount block or not, so just take the worst
3490 			 * case that all the clusters are in this block and each
3491 			 * one will split a refcount rec, so totally we need
3492 			 * clusters * 2 new refcount rec.
3493 			 */
3494 			if (le64_to_cpu(rb->rf_records.rl_used) + clusters * 2 >
3495 			    le16_to_cpu(rb->rf_records.rl_count))
3496 				ref_blocks++;
3497 
3498 			*credits += 1;
3499 			brelse(ref_leaf_bh);
3500 			ref_leaf_bh = NULL;
3501 
3502 			if (num_clusters <= le32_to_cpu(rec.r_clusters))
3503 				break;
3504 			else
3505 				num_clusters -= le32_to_cpu(rec.r_clusters);
3506 			p_cluster += num_clusters;
3507 		}
3508 	}
3509 
3510 	*meta_add += ref_blocks;
3511 	if (!ref_blocks)
3512 		goto out;
3513 
3514 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
3515 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
3516 		*credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
3517 	else {
3518 		struct ocfs2_extent_tree et;
3519 
3520 		ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh);
3521 		*credits += ocfs2_calc_extend_credits(inode->i_sb,
3522 						      et.et_root_el,
3523 						      ref_blocks);
3524 	}
3525 
3526 out:
3527 	brelse(ref_leaf_bh);
3528 	return ret;
3529 }
3530 
3531 /*
3532  * Do CoW for xattr.
3533  */
3534 int ocfs2_refcount_cow_xattr(struct inode *inode,
3535 			     struct ocfs2_dinode *di,
3536 			     struct ocfs2_xattr_value_buf *vb,
3537 			     struct ocfs2_refcount_tree *ref_tree,
3538 			     struct buffer_head *ref_root_bh,
3539 			     u32 cpos, u32 write_len,
3540 			     struct ocfs2_post_refcount *post)
3541 {
3542 	int ret;
3543 	struct ocfs2_xattr_value_root *xv = vb->vb_xv;
3544 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
3545 	struct ocfs2_cow_context *context = NULL;
3546 	u32 cow_start, cow_len;
3547 
3548 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
3549 
3550 	ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list,
3551 					      cpos, write_len, UINT_MAX,
3552 					      &cow_start, &cow_len);
3553 	if (ret) {
3554 		mlog_errno(ret);
3555 		goto out;
3556 	}
3557 
3558 	BUG_ON(cow_len == 0);
3559 
3560 	context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3561 	if (!context) {
3562 		ret = -ENOMEM;
3563 		mlog_errno(ret);
3564 		goto out;
3565 	}
3566 
3567 	context->inode = inode;
3568 	context->cow_start = cow_start;
3569 	context->cow_len = cow_len;
3570 	context->ref_tree = ref_tree;
3571 	context->ref_root_bh = ref_root_bh;;
3572 	context->cow_object = xv;
3573 
3574 	context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd;
3575 	/* We need the extra credits for duplicate_clusters by jbd. */
3576 	context->extra_credits =
3577 		ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len;
3578 	context->get_clusters = ocfs2_xattr_value_get_clusters;
3579 	context->post_refcount = post;
3580 
3581 	ocfs2_init_xattr_value_extent_tree(&context->data_et,
3582 					   INODE_CACHE(inode), vb);
3583 
3584 	ret = ocfs2_replace_cow(context);
3585 	if (ret)
3586 		mlog_errno(ret);
3587 
3588 out:
3589 	kfree(context);
3590 	return ret;
3591 }
3592 
3593 /*
3594  * Insert a new extent into refcount tree and mark a extent rec
3595  * as refcounted in the dinode tree.
3596  */
3597 int ocfs2_add_refcount_flag(struct inode *inode,
3598 			    struct ocfs2_extent_tree *data_et,
3599 			    struct ocfs2_caching_info *ref_ci,
3600 			    struct buffer_head *ref_root_bh,
3601 			    u32 cpos, u32 p_cluster, u32 num_clusters,
3602 			    struct ocfs2_cached_dealloc_ctxt *dealloc,
3603 			    struct ocfs2_post_refcount *post)
3604 {
3605 	int ret;
3606 	handle_t *handle;
3607 	int credits = 1, ref_blocks = 0;
3608 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3609 	struct ocfs2_alloc_context *meta_ac = NULL;
3610 
3611 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
3612 					       ref_ci, ref_root_bh,
3613 					       p_cluster, num_clusters,
3614 					       &ref_blocks, &credits);
3615 	if (ret) {
3616 		mlog_errno(ret);
3617 		goto out;
3618 	}
3619 
3620 	mlog(0, "reserve new metadata %d, credits = %d\n",
3621 	     ref_blocks, credits);
3622 
3623 	if (ref_blocks) {
3624 		ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
3625 							ref_blocks, &meta_ac);
3626 		if (ret) {
3627 			mlog_errno(ret);
3628 			goto out;
3629 		}
3630 	}
3631 
3632 	if (post)
3633 		credits += post->credits;
3634 
3635 	handle = ocfs2_start_trans(osb, credits);
3636 	if (IS_ERR(handle)) {
3637 		ret = PTR_ERR(handle);
3638 		mlog_errno(ret);
3639 		goto out;
3640 	}
3641 
3642 	ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
3643 					   cpos, num_clusters, p_cluster,
3644 					   meta_ac, dealloc);
3645 	if (ret) {
3646 		mlog_errno(ret);
3647 		goto out_commit;
3648 	}
3649 
3650 	ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3651 					p_cluster, num_clusters, 0,
3652 					meta_ac, dealloc);
3653 	if (ret) {
3654 		mlog_errno(ret);
3655 		goto out_commit;
3656 	}
3657 
3658 	if (post && post->func) {
3659 		ret = post->func(inode, handle, post->para);
3660 		if (ret)
3661 			mlog_errno(ret);
3662 	}
3663 
3664 out_commit:
3665 	ocfs2_commit_trans(osb, handle);
3666 out:
3667 	if (meta_ac)
3668 		ocfs2_free_alloc_context(meta_ac);
3669 	return ret;
3670 }
3671 
3672 static int ocfs2_change_ctime(struct inode *inode,
3673 			      struct buffer_head *di_bh)
3674 {
3675 	int ret;
3676 	handle_t *handle;
3677 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3678 
3679 	handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
3680 				   OCFS2_INODE_UPDATE_CREDITS);
3681 	if (IS_ERR(handle)) {
3682 		ret = PTR_ERR(handle);
3683 		mlog_errno(ret);
3684 		goto out;
3685 	}
3686 
3687 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
3688 				      OCFS2_JOURNAL_ACCESS_WRITE);
3689 	if (ret) {
3690 		mlog_errno(ret);
3691 		goto out_commit;
3692 	}
3693 
3694 	inode->i_ctime = CURRENT_TIME;
3695 	di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3696 	di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3697 
3698 	ocfs2_journal_dirty(handle, di_bh);
3699 
3700 out_commit:
3701 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3702 out:
3703 	return ret;
3704 }
3705 
3706 static int ocfs2_attach_refcount_tree(struct inode *inode,
3707 				      struct buffer_head *di_bh)
3708 {
3709 	int ret, data_changed = 0;
3710 	struct buffer_head *ref_root_bh = NULL;
3711 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
3712 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3713 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3714 	struct ocfs2_refcount_tree *ref_tree;
3715 	unsigned int ext_flags;
3716 	loff_t size;
3717 	u32 cpos, num_clusters, clusters, p_cluster;
3718 	struct ocfs2_cached_dealloc_ctxt dealloc;
3719 	struct ocfs2_extent_tree di_et;
3720 
3721 	ocfs2_init_dealloc_ctxt(&dealloc);
3722 
3723 	if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)) {
3724 		ret = ocfs2_create_refcount_tree(inode, di_bh);
3725 		if (ret) {
3726 			mlog_errno(ret);
3727 			goto out;
3728 		}
3729 	}
3730 
3731 	BUG_ON(!di->i_refcount_loc);
3732 	ret = ocfs2_lock_refcount_tree(osb,
3733 				       le64_to_cpu(di->i_refcount_loc), 1,
3734 				       &ref_tree, &ref_root_bh);
3735 	if (ret) {
3736 		mlog_errno(ret);
3737 		goto out;
3738 	}
3739 
3740 	ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh);
3741 
3742 	size = i_size_read(inode);
3743 	clusters = ocfs2_clusters_for_bytes(inode->i_sb, size);
3744 
3745 	cpos = 0;
3746 	while (cpos < clusters) {
3747 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3748 					 &num_clusters, &ext_flags);
3749 
3750 		if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) {
3751 			ret = ocfs2_add_refcount_flag(inode, &di_et,
3752 						      &ref_tree->rf_ci,
3753 						      ref_root_bh, cpos,
3754 						      p_cluster, num_clusters,
3755 						      &dealloc, NULL);
3756 			if (ret) {
3757 				mlog_errno(ret);
3758 				goto unlock;
3759 			}
3760 
3761 			data_changed = 1;
3762 		}
3763 		cpos += num_clusters;
3764 	}
3765 
3766 	if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
3767 		ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh,
3768 						       &ref_tree->rf_ci,
3769 						       ref_root_bh,
3770 						       &dealloc);
3771 		if (ret) {
3772 			mlog_errno(ret);
3773 			goto unlock;
3774 		}
3775 	}
3776 
3777 	if (data_changed) {
3778 		ret = ocfs2_change_ctime(inode, di_bh);
3779 		if (ret)
3780 			mlog_errno(ret);
3781 	}
3782 
3783 unlock:
3784 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3785 	brelse(ref_root_bh);
3786 
3787 	if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
3788 		ocfs2_schedule_truncate_log_flush(osb, 1);
3789 		ocfs2_run_deallocs(osb, &dealloc);
3790 	}
3791 out:
3792 	/*
3793 	 * Empty the extent map so that we may get the right extent
3794 	 * record from the disk.
3795 	 */
3796 	ocfs2_extent_map_trunc(inode, 0);
3797 
3798 	return ret;
3799 }
3800 
3801 static int ocfs2_add_refcounted_extent(struct inode *inode,
3802 				   struct ocfs2_extent_tree *et,
3803 				   struct ocfs2_caching_info *ref_ci,
3804 				   struct buffer_head *ref_root_bh,
3805 				   u32 cpos, u32 p_cluster, u32 num_clusters,
3806 				   unsigned int ext_flags,
3807 				   struct ocfs2_cached_dealloc_ctxt *dealloc)
3808 {
3809 	int ret;
3810 	handle_t *handle;
3811 	int credits = 0;
3812 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3813 	struct ocfs2_alloc_context *meta_ac = NULL;
3814 
3815 	ret = ocfs2_lock_refcount_allocators(inode->i_sb,
3816 					     p_cluster, num_clusters,
3817 					     et, ref_ci,
3818 					     ref_root_bh, &meta_ac,
3819 					     NULL, &credits);
3820 	if (ret) {
3821 		mlog_errno(ret);
3822 		goto out;
3823 	}
3824 
3825 	handle = ocfs2_start_trans(osb, credits);
3826 	if (IS_ERR(handle)) {
3827 		ret = PTR_ERR(handle);
3828 		mlog_errno(ret);
3829 		goto out;
3830 	}
3831 
3832 	ret = ocfs2_insert_extent(handle, et, cpos,
3833 			cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb,
3834 							     p_cluster)),
3835 			num_clusters, ext_flags, meta_ac);
3836 	if (ret) {
3837 		mlog_errno(ret);
3838 		goto out_commit;
3839 	}
3840 
3841 	ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3842 				      p_cluster, num_clusters,
3843 				      meta_ac, dealloc);
3844 	if (ret)
3845 		mlog_errno(ret);
3846 
3847 out_commit:
3848 	ocfs2_commit_trans(osb, handle);
3849 out:
3850 	if (meta_ac)
3851 		ocfs2_free_alloc_context(meta_ac);
3852 	return ret;
3853 }
3854 
3855 static int ocfs2_duplicate_extent_list(struct inode *s_inode,
3856 				struct inode *t_inode,
3857 				struct buffer_head *t_bh,
3858 				struct ocfs2_caching_info *ref_ci,
3859 				struct buffer_head *ref_root_bh,
3860 				struct ocfs2_cached_dealloc_ctxt *dealloc)
3861 {
3862 	int ret = 0;
3863 	u32 p_cluster, num_clusters, clusters, cpos;
3864 	loff_t size;
3865 	unsigned int ext_flags;
3866 	struct ocfs2_extent_tree et;
3867 
3868 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh);
3869 
3870 	size = i_size_read(s_inode);
3871 	clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size);
3872 
3873 	cpos = 0;
3874 	while (cpos < clusters) {
3875 		ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster,
3876 					 &num_clusters, &ext_flags);
3877 
3878 		if (p_cluster) {
3879 			ret = ocfs2_add_refcounted_extent(t_inode, &et,
3880 							  ref_ci, ref_root_bh,
3881 							  cpos, p_cluster,
3882 							  num_clusters,
3883 							  ext_flags,
3884 							  dealloc);
3885 			if (ret) {
3886 				mlog_errno(ret);
3887 				goto out;
3888 			}
3889 		}
3890 
3891 		cpos += num_clusters;
3892 	}
3893 
3894 out:
3895 	return ret;
3896 }
3897 
3898 /*
3899  * change the new file's attributes to the src.
3900  *
3901  * reflink creates a snapshot of a file, that means the attributes
3902  * must be identical except for three exceptions - nlink, ino, and ctime.
3903  */
3904 static int ocfs2_complete_reflink(struct inode *s_inode,
3905 				  struct buffer_head *s_bh,
3906 				  struct inode *t_inode,
3907 				  struct buffer_head *t_bh)
3908 {
3909 	int ret;
3910 	handle_t *handle;
3911 	struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
3912 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data;
3913 	loff_t size = i_size_read(s_inode);
3914 
3915 	handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb),
3916 				   OCFS2_INODE_UPDATE_CREDITS);
3917 	if (IS_ERR(handle)) {
3918 		ret = PTR_ERR(handle);
3919 		mlog_errno(ret);
3920 		return ret;
3921 	}
3922 
3923 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
3924 				      OCFS2_JOURNAL_ACCESS_WRITE);
3925 	if (ret) {
3926 		mlog_errno(ret);
3927 		goto out_commit;
3928 	}
3929 
3930 	spin_lock(&OCFS2_I(t_inode)->ip_lock);
3931 	OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters;
3932 	OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr;
3933 	OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
3934 	spin_unlock(&OCFS2_I(t_inode)->ip_lock);
3935 	i_size_write(t_inode, size);
3936 
3937 	di->i_xattr_inline_size = s_di->i_xattr_inline_size;
3938 	di->i_clusters = s_di->i_clusters;
3939 	di->i_size = s_di->i_size;
3940 	di->i_dyn_features = s_di->i_dyn_features;
3941 	di->i_attr = s_di->i_attr;
3942 	di->i_uid = s_di->i_uid;
3943 	di->i_gid = s_di->i_gid;
3944 	di->i_mode = s_di->i_mode;
3945 
3946 	/*
3947 	 * update time.
3948 	 * we want mtime to appear identical to the source and update ctime.
3949 	 */
3950 	t_inode->i_ctime = CURRENT_TIME;
3951 
3952 	di->i_ctime = cpu_to_le64(t_inode->i_ctime.tv_sec);
3953 	di->i_ctime_nsec = cpu_to_le32(t_inode->i_ctime.tv_nsec);
3954 
3955 	t_inode->i_mtime = s_inode->i_mtime;
3956 	di->i_mtime = s_di->i_mtime;
3957 	di->i_mtime_nsec = s_di->i_mtime_nsec;
3958 
3959 	ocfs2_journal_dirty(handle, t_bh);
3960 
3961 out_commit:
3962 	ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle);
3963 	return ret;
3964 }
3965 
3966 static int ocfs2_create_reflink_node(struct inode *s_inode,
3967 				     struct buffer_head *s_bh,
3968 				     struct inode *t_inode,
3969 				     struct buffer_head *t_bh)
3970 {
3971 	int ret;
3972 	struct buffer_head *ref_root_bh = NULL;
3973 	struct ocfs2_cached_dealloc_ctxt dealloc;
3974 	struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
3975 	struct ocfs2_refcount_block *rb;
3976 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data;
3977 	struct ocfs2_refcount_tree *ref_tree;
3978 
3979 	ocfs2_init_dealloc_ctxt(&dealloc);
3980 
3981 	ret = ocfs2_set_refcount_tree(t_inode, t_bh,
3982 				      le64_to_cpu(di->i_refcount_loc));
3983 	if (ret) {
3984 		mlog_errno(ret);
3985 		goto out;
3986 	}
3987 
3988 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
3989 				       1, &ref_tree, &ref_root_bh);
3990 	if (ret) {
3991 		mlog_errno(ret);
3992 		goto out;
3993 	}
3994 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
3995 
3996 	ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
3997 					  &ref_tree->rf_ci, ref_root_bh,
3998 					  &dealloc);
3999 	if (ret) {
4000 		mlog_errno(ret);
4001 		goto out_unlock_refcount;
4002 	}
4003 
4004 	ret = ocfs2_complete_reflink(s_inode, s_bh, t_inode, t_bh);
4005 	if (ret)
4006 		mlog_errno(ret);
4007 
4008 out_unlock_refcount:
4009 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
4010 	brelse(ref_root_bh);
4011 out:
4012 	if (ocfs2_dealloc_has_cluster(&dealloc)) {
4013 		ocfs2_schedule_truncate_log_flush(osb, 1);
4014 		ocfs2_run_deallocs(osb, &dealloc);
4015 	}
4016 
4017 	return ret;
4018 }
4019