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