xref: /openbmc/linux/fs/ocfs2/refcounttree.c (revision 110a045a)
1f2c870e3STao Ma /* -*- mode: c; c-basic-offset: 8; -*-
2f2c870e3STao Ma  * vim: noexpandtab sw=8 ts=8 sts=0:
3f2c870e3STao Ma  *
4f2c870e3STao Ma  * refcounttree.c
5f2c870e3STao Ma  *
6f2c870e3STao Ma  * Copyright (C) 2009 Oracle.  All rights reserved.
7f2c870e3STao Ma  *
8f2c870e3STao Ma  * This program is free software; you can redistribute it and/or
9f2c870e3STao Ma  * modify it under the terms of the GNU General Public
10f2c870e3STao Ma  * License version 2 as published by the Free Software Foundation.
11f2c870e3STao Ma  *
12f2c870e3STao Ma  * This program is distributed in the hope that it will be useful,
13f2c870e3STao Ma  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14f2c870e3STao Ma  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15f2c870e3STao Ma  * General Public License for more details.
16f2c870e3STao Ma  */
17f2c870e3STao Ma 
18e73a819dSTao Ma #include <linux/sort.h>
19f2c870e3STao Ma #define MLOG_MASK_PREFIX ML_REFCOUNT
20f2c870e3STao Ma #include <cluster/masklog.h>
21f2c870e3STao Ma #include "ocfs2.h"
22f2c870e3STao Ma #include "inode.h"
23f2c870e3STao Ma #include "alloc.h"
24f2c870e3STao Ma #include "suballoc.h"
25f2c870e3STao Ma #include "journal.h"
26f2c870e3STao Ma #include "uptodate.h"
27f2c870e3STao Ma #include "super.h"
28f2c870e3STao Ma #include "buffer_head_io.h"
29f2c870e3STao Ma #include "blockcheck.h"
30c732eb16STao Ma #include "refcounttree.h"
318bf396deSTao Ma #include "sysfile.h"
32374a263eSTao Ma #include "dlmglue.h"
33e73a819dSTao Ma #include "extent_map.h"
346f70fa51STao Ma #include "aops.h"
356f70fa51STao Ma 
366f70fa51STao Ma #include <linux/bio.h>
376f70fa51STao Ma #include <linux/blkdev.h>
386f70fa51STao Ma #include <linux/gfp.h>
396f70fa51STao Ma #include <linux/slab.h>
406f70fa51STao Ma #include <linux/writeback.h>
416f70fa51STao Ma #include <linux/pagevec.h>
426f70fa51STao Ma #include <linux/swap.h>
436f70fa51STao Ma 
446f70fa51STao Ma struct ocfs2_cow_context {
456f70fa51STao Ma 	struct inode *inode;
466f70fa51STao Ma 	u32 cow_start;
476f70fa51STao Ma 	u32 cow_len;
486f70fa51STao Ma 	struct ocfs2_extent_tree di_et;
496f70fa51STao Ma 	struct ocfs2_caching_info *ref_ci;
506f70fa51STao Ma 	struct buffer_head *ref_root_bh;
516f70fa51STao Ma 	struct ocfs2_alloc_context *meta_ac;
526f70fa51STao Ma 	struct ocfs2_alloc_context *data_ac;
536f70fa51STao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
546f70fa51STao Ma };
55c732eb16STao Ma 
56c732eb16STao Ma static inline struct ocfs2_refcount_tree *
57c732eb16STao Ma cache_info_to_refcount(struct ocfs2_caching_info *ci)
58c732eb16STao Ma {
59c732eb16STao Ma 	return container_of(ci, struct ocfs2_refcount_tree, rf_ci);
60c732eb16STao Ma }
61f2c870e3STao Ma 
62f2c870e3STao Ma static int ocfs2_validate_refcount_block(struct super_block *sb,
63f2c870e3STao Ma 					 struct buffer_head *bh)
64f2c870e3STao Ma {
65f2c870e3STao Ma 	int rc;
66f2c870e3STao Ma 	struct ocfs2_refcount_block *rb =
67f2c870e3STao Ma 		(struct ocfs2_refcount_block *)bh->b_data;
68f2c870e3STao Ma 
69f2c870e3STao Ma 	mlog(0, "Validating refcount block %llu\n",
70f2c870e3STao Ma 	     (unsigned long long)bh->b_blocknr);
71f2c870e3STao Ma 
72f2c870e3STao Ma 	BUG_ON(!buffer_uptodate(bh));
73f2c870e3STao Ma 
74f2c870e3STao Ma 	/*
75f2c870e3STao Ma 	 * If the ecc fails, we return the error but otherwise
76f2c870e3STao Ma 	 * leave the filesystem running.  We know any error is
77f2c870e3STao Ma 	 * local to this block.
78f2c870e3STao Ma 	 */
79f2c870e3STao Ma 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check);
80f2c870e3STao Ma 	if (rc) {
81f2c870e3STao Ma 		mlog(ML_ERROR, "Checksum failed for refcount block %llu\n",
82f2c870e3STao Ma 		     (unsigned long long)bh->b_blocknr);
83f2c870e3STao Ma 		return rc;
84f2c870e3STao Ma 	}
85f2c870e3STao Ma 
86f2c870e3STao Ma 
87f2c870e3STao Ma 	if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) {
88f2c870e3STao Ma 		ocfs2_error(sb,
89f2c870e3STao Ma 			    "Refcount block #%llu has bad signature %.*s",
90f2c870e3STao Ma 			    (unsigned long long)bh->b_blocknr, 7,
91f2c870e3STao Ma 			    rb->rf_signature);
92f2c870e3STao Ma 		return -EINVAL;
93f2c870e3STao Ma 	}
94f2c870e3STao Ma 
95f2c870e3STao Ma 	if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) {
96f2c870e3STao Ma 		ocfs2_error(sb,
97f2c870e3STao Ma 			    "Refcount block #%llu has an invalid rf_blkno "
98f2c870e3STao Ma 			    "of %llu",
99f2c870e3STao Ma 			    (unsigned long long)bh->b_blocknr,
100f2c870e3STao Ma 			    (unsigned long long)le64_to_cpu(rb->rf_blkno));
101f2c870e3STao Ma 		return -EINVAL;
102f2c870e3STao Ma 	}
103f2c870e3STao Ma 
104f2c870e3STao Ma 	if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) {
105f2c870e3STao Ma 		ocfs2_error(sb,
106f2c870e3STao Ma 			    "Refcount block #%llu has an invalid "
107f2c870e3STao Ma 			    "rf_fs_generation of #%u",
108f2c870e3STao Ma 			    (unsigned long long)bh->b_blocknr,
109f2c870e3STao Ma 			    le32_to_cpu(rb->rf_fs_generation));
110f2c870e3STao Ma 		return -EINVAL;
111f2c870e3STao Ma 	}
112f2c870e3STao Ma 
113f2c870e3STao Ma 	return 0;
114f2c870e3STao Ma }
115f2c870e3STao Ma 
116f2c870e3STao Ma static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci,
117f2c870e3STao Ma 				     u64 rb_blkno,
118f2c870e3STao Ma 				     struct buffer_head **bh)
119f2c870e3STao Ma {
120f2c870e3STao Ma 	int rc;
121f2c870e3STao Ma 	struct buffer_head *tmp = *bh;
122f2c870e3STao Ma 
123f2c870e3STao Ma 	rc = ocfs2_read_block(ci, rb_blkno, &tmp,
124f2c870e3STao Ma 			      ocfs2_validate_refcount_block);
125f2c870e3STao Ma 
126f2c870e3STao Ma 	/* If ocfs2_read_block() got us a new bh, pass it up. */
127f2c870e3STao Ma 	if (!rc && !*bh)
128f2c870e3STao Ma 		*bh = tmp;
129f2c870e3STao Ma 
130f2c870e3STao Ma 	return rc;
131f2c870e3STao Ma }
132c732eb16STao Ma 
133c732eb16STao Ma static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci)
134c732eb16STao Ma {
135c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
136c732eb16STao Ma 
137c732eb16STao Ma 	return rf->rf_blkno;
138c732eb16STao Ma }
139c732eb16STao Ma 
140c732eb16STao Ma static struct super_block *
141c732eb16STao Ma ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci)
142c732eb16STao Ma {
143c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
144c732eb16STao Ma 
145c732eb16STao Ma 	return rf->rf_sb;
146c732eb16STao Ma }
147c732eb16STao Ma 
148c732eb16STao Ma static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci)
149c732eb16STao Ma {
150c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
151c732eb16STao Ma 
152c732eb16STao Ma 	spin_lock(&rf->rf_lock);
153c732eb16STao Ma }
154c732eb16STao Ma 
155c732eb16STao Ma static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci)
156c732eb16STao Ma {
157c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
158c732eb16STao Ma 
159c732eb16STao Ma 	spin_unlock(&rf->rf_lock);
160c732eb16STao Ma }
161c732eb16STao Ma 
162c732eb16STao Ma static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci)
163c732eb16STao Ma {
164c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
165c732eb16STao Ma 
166c732eb16STao Ma 	mutex_lock(&rf->rf_io_mutex);
167c732eb16STao Ma }
168c732eb16STao Ma 
169c732eb16STao Ma static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci)
170c732eb16STao Ma {
171c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
172c732eb16STao Ma 
173c732eb16STao Ma 	mutex_unlock(&rf->rf_io_mutex);
174c732eb16STao Ma }
175c732eb16STao Ma 
176c732eb16STao Ma static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = {
177c732eb16STao Ma 	.co_owner		= ocfs2_refcount_cache_owner,
178c732eb16STao Ma 	.co_get_super		= ocfs2_refcount_cache_get_super,
179c732eb16STao Ma 	.co_cache_lock		= ocfs2_refcount_cache_lock,
180c732eb16STao Ma 	.co_cache_unlock	= ocfs2_refcount_cache_unlock,
181c732eb16STao Ma 	.co_io_lock		= ocfs2_refcount_cache_io_lock,
182c732eb16STao Ma 	.co_io_unlock		= ocfs2_refcount_cache_io_unlock,
183c732eb16STao Ma };
184374a263eSTao Ma 
185374a263eSTao Ma static struct ocfs2_refcount_tree *
186374a263eSTao Ma ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno)
187374a263eSTao Ma {
188374a263eSTao Ma 	struct rb_node *n = osb->osb_rf_lock_tree.rb_node;
189374a263eSTao Ma 	struct ocfs2_refcount_tree *tree = NULL;
190374a263eSTao Ma 
191374a263eSTao Ma 	while (n) {
192374a263eSTao Ma 		tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node);
193374a263eSTao Ma 
194374a263eSTao Ma 		if (blkno < tree->rf_blkno)
195374a263eSTao Ma 			n = n->rb_left;
196374a263eSTao Ma 		else if (blkno > tree->rf_blkno)
197374a263eSTao Ma 			n = n->rb_right;
198374a263eSTao Ma 		else
199374a263eSTao Ma 			return tree;
200374a263eSTao Ma 	}
201374a263eSTao Ma 
202374a263eSTao Ma 	return NULL;
203374a263eSTao Ma }
204374a263eSTao Ma 
205374a263eSTao Ma /* osb_lock is already locked. */
206374a263eSTao Ma static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb,
207374a263eSTao Ma 				       struct ocfs2_refcount_tree *new)
208374a263eSTao Ma {
209374a263eSTao Ma 	u64 rf_blkno = new->rf_blkno;
210374a263eSTao Ma 	struct rb_node *parent = NULL;
211374a263eSTao Ma 	struct rb_node **p = &osb->osb_rf_lock_tree.rb_node;
212374a263eSTao Ma 	struct ocfs2_refcount_tree *tmp;
213374a263eSTao Ma 
214374a263eSTao Ma 	while (*p) {
215374a263eSTao Ma 		parent = *p;
216374a263eSTao Ma 
217374a263eSTao Ma 		tmp = rb_entry(parent, struct ocfs2_refcount_tree,
218374a263eSTao Ma 			       rf_node);
219374a263eSTao Ma 
220374a263eSTao Ma 		if (rf_blkno < tmp->rf_blkno)
221374a263eSTao Ma 			p = &(*p)->rb_left;
222374a263eSTao Ma 		else if (rf_blkno > tmp->rf_blkno)
223374a263eSTao Ma 			p = &(*p)->rb_right;
224374a263eSTao Ma 		else {
225374a263eSTao Ma 			/* This should never happen! */
226374a263eSTao Ma 			mlog(ML_ERROR, "Duplicate refcount block %llu found!\n",
227374a263eSTao Ma 			     (unsigned long long)rf_blkno);
228374a263eSTao Ma 			BUG();
229374a263eSTao Ma 		}
230374a263eSTao Ma 	}
231374a263eSTao Ma 
232374a263eSTao Ma 	rb_link_node(&new->rf_node, parent, p);
233374a263eSTao Ma 	rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree);
234374a263eSTao Ma }
235374a263eSTao Ma 
236374a263eSTao Ma static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree)
237374a263eSTao Ma {
238374a263eSTao Ma 	ocfs2_metadata_cache_exit(&tree->rf_ci);
239374a263eSTao Ma 	ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres);
240374a263eSTao Ma 	ocfs2_lock_res_free(&tree->rf_lockres);
241374a263eSTao Ma 	kfree(tree);
242374a263eSTao Ma }
243374a263eSTao Ma 
244374a263eSTao Ma static inline void
245374a263eSTao Ma ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb,
246374a263eSTao Ma 					struct ocfs2_refcount_tree *tree)
247374a263eSTao Ma {
248374a263eSTao Ma 	rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree);
249374a263eSTao Ma 	if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree)
250374a263eSTao Ma 		osb->osb_ref_tree_lru = NULL;
251374a263eSTao Ma }
252374a263eSTao Ma 
253374a263eSTao Ma static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
254374a263eSTao Ma 					struct ocfs2_refcount_tree *tree)
255374a263eSTao Ma {
256374a263eSTao Ma 	spin_lock(&osb->osb_lock);
257374a263eSTao Ma 	ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
258374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
259374a263eSTao Ma }
260374a263eSTao Ma 
261374a263eSTao Ma void ocfs2_kref_remove_refcount_tree(struct kref *kref)
262374a263eSTao Ma {
263374a263eSTao Ma 	struct ocfs2_refcount_tree *tree =
264374a263eSTao Ma 		container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
265374a263eSTao Ma 
266374a263eSTao Ma 	ocfs2_free_refcount_tree(tree);
267374a263eSTao Ma }
268374a263eSTao Ma 
269374a263eSTao Ma static inline void
270374a263eSTao Ma ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree)
271374a263eSTao Ma {
272374a263eSTao Ma 	kref_get(&tree->rf_getcnt);
273374a263eSTao Ma }
274374a263eSTao Ma 
275374a263eSTao Ma static inline void
276374a263eSTao Ma ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree)
277374a263eSTao Ma {
278374a263eSTao Ma 	kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree);
279374a263eSTao Ma }
280374a263eSTao Ma 
281374a263eSTao Ma static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new,
282374a263eSTao Ma 					       struct super_block *sb)
283374a263eSTao Ma {
284374a263eSTao Ma 	ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops);
285374a263eSTao Ma 	mutex_init(&new->rf_io_mutex);
286374a263eSTao Ma 	new->rf_sb = sb;
287374a263eSTao Ma 	spin_lock_init(&new->rf_lock);
288374a263eSTao Ma }
289374a263eSTao Ma 
290374a263eSTao Ma static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb,
291374a263eSTao Ma 					struct ocfs2_refcount_tree *new,
292374a263eSTao Ma 					u64 rf_blkno, u32 generation)
293374a263eSTao Ma {
294374a263eSTao Ma 	init_rwsem(&new->rf_sem);
295374a263eSTao Ma 	ocfs2_refcount_lock_res_init(&new->rf_lockres, osb,
296374a263eSTao Ma 				     rf_blkno, generation);
297374a263eSTao Ma }
298374a263eSTao Ma 
2998bf396deSTao Ma static struct ocfs2_refcount_tree*
3008bf396deSTao Ma ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno)
3018bf396deSTao Ma {
3028bf396deSTao Ma 	struct ocfs2_refcount_tree *new;
3038bf396deSTao Ma 
3048bf396deSTao Ma 	new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS);
3058bf396deSTao Ma 	if (!new)
3068bf396deSTao Ma 		return NULL;
3078bf396deSTao Ma 
3088bf396deSTao Ma 	new->rf_blkno = rf_blkno;
3098bf396deSTao Ma 	kref_init(&new->rf_getcnt);
3108bf396deSTao Ma 	ocfs2_init_refcount_tree_ci(new, osb->sb);
3118bf396deSTao Ma 
3128bf396deSTao Ma 	return new;
3138bf396deSTao Ma }
3148bf396deSTao Ma 
315374a263eSTao Ma static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno,
316374a263eSTao Ma 				   struct ocfs2_refcount_tree **ret_tree)
317374a263eSTao Ma {
318374a263eSTao Ma 	int ret = 0;
319374a263eSTao Ma 	struct ocfs2_refcount_tree *tree, *new = NULL;
320374a263eSTao Ma 	struct buffer_head *ref_root_bh = NULL;
321374a263eSTao Ma 	struct ocfs2_refcount_block *ref_rb;
322374a263eSTao Ma 
323374a263eSTao Ma 	spin_lock(&osb->osb_lock);
324374a263eSTao Ma 	if (osb->osb_ref_tree_lru &&
325374a263eSTao Ma 	    osb->osb_ref_tree_lru->rf_blkno == rf_blkno)
326374a263eSTao Ma 		tree = osb->osb_ref_tree_lru;
327374a263eSTao Ma 	else
328374a263eSTao Ma 		tree = ocfs2_find_refcount_tree(osb, rf_blkno);
329374a263eSTao Ma 	if (tree)
330374a263eSTao Ma 		goto out;
331374a263eSTao Ma 
332374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
333374a263eSTao Ma 
3348bf396deSTao Ma 	new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
335374a263eSTao Ma 	if (!new) {
336374a263eSTao Ma 		ret = -ENOMEM;
3378bf396deSTao Ma 		mlog_errno(ret);
338374a263eSTao Ma 		return ret;
339374a263eSTao Ma 	}
340374a263eSTao Ma 	/*
341374a263eSTao Ma 	 * We need the generation to create the refcount tree lock and since
342374a263eSTao Ma 	 * it isn't changed during the tree modification, we are safe here to
343374a263eSTao Ma 	 * read without protection.
344374a263eSTao Ma 	 * We also have to purge the cache after we create the lock since the
345374a263eSTao Ma 	 * refcount block may have the stale data. It can only be trusted when
346374a263eSTao Ma 	 * we hold the refcount lock.
347374a263eSTao Ma 	 */
348374a263eSTao Ma 	ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
349374a263eSTao Ma 	if (ret) {
350374a263eSTao Ma 		mlog_errno(ret);
351374a263eSTao Ma 		ocfs2_metadata_cache_exit(&new->rf_ci);
352374a263eSTao Ma 		kfree(new);
353374a263eSTao Ma 		return ret;
354374a263eSTao Ma 	}
355374a263eSTao Ma 
356374a263eSTao Ma 	ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
357374a263eSTao Ma 	new->rf_generation = le32_to_cpu(ref_rb->rf_generation);
358374a263eSTao Ma 	ocfs2_init_refcount_tree_lock(osb, new, rf_blkno,
359374a263eSTao Ma 				      new->rf_generation);
360374a263eSTao Ma 	ocfs2_metadata_cache_purge(&new->rf_ci);
361374a263eSTao Ma 
362374a263eSTao Ma 	spin_lock(&osb->osb_lock);
363374a263eSTao Ma 	tree = ocfs2_find_refcount_tree(osb, rf_blkno);
364374a263eSTao Ma 	if (tree)
365374a263eSTao Ma 		goto out;
366374a263eSTao Ma 
367374a263eSTao Ma 	ocfs2_insert_refcount_tree(osb, new);
368374a263eSTao Ma 
369374a263eSTao Ma 	tree = new;
370374a263eSTao Ma 	new = NULL;
371374a263eSTao Ma 
372374a263eSTao Ma out:
373374a263eSTao Ma 	*ret_tree = tree;
374374a263eSTao Ma 
375374a263eSTao Ma 	osb->osb_ref_tree_lru = tree;
376374a263eSTao Ma 
377374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
378374a263eSTao Ma 
379374a263eSTao Ma 	if (new)
380374a263eSTao Ma 		ocfs2_free_refcount_tree(new);
381374a263eSTao Ma 
382374a263eSTao Ma 	brelse(ref_root_bh);
383374a263eSTao Ma 	return ret;
384374a263eSTao Ma }
385374a263eSTao Ma 
386374a263eSTao Ma static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno)
387374a263eSTao Ma {
388374a263eSTao Ma 	int ret;
389374a263eSTao Ma 	struct buffer_head *di_bh = NULL;
390374a263eSTao Ma 	struct ocfs2_dinode *di;
391374a263eSTao Ma 
392374a263eSTao Ma 	ret = ocfs2_read_inode_block(inode, &di_bh);
393374a263eSTao Ma 	if (ret) {
394374a263eSTao Ma 		mlog_errno(ret);
395374a263eSTao Ma 		goto out;
396374a263eSTao Ma 	}
397374a263eSTao Ma 
398374a263eSTao Ma 	BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
399374a263eSTao Ma 
400374a263eSTao Ma 	di = (struct ocfs2_dinode *)di_bh->b_data;
401374a263eSTao Ma 	*ref_blkno = le64_to_cpu(di->i_refcount_loc);
402374a263eSTao Ma 	brelse(di_bh);
403374a263eSTao Ma out:
404374a263eSTao Ma 	return ret;
405374a263eSTao Ma }
406374a263eSTao Ma 
407374a263eSTao Ma static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
408374a263eSTao Ma 				      struct ocfs2_refcount_tree *tree, int rw)
409374a263eSTao Ma {
410374a263eSTao Ma 	int ret;
411374a263eSTao Ma 
412374a263eSTao Ma 	ret = ocfs2_refcount_lock(tree, rw);
413374a263eSTao Ma 	if (ret) {
414374a263eSTao Ma 		mlog_errno(ret);
415374a263eSTao Ma 		goto out;
416374a263eSTao Ma 	}
417374a263eSTao Ma 
418374a263eSTao Ma 	if (rw)
419374a263eSTao Ma 		down_write(&tree->rf_sem);
420374a263eSTao Ma 	else
421374a263eSTao Ma 		down_read(&tree->rf_sem);
422374a263eSTao Ma 
423374a263eSTao Ma out:
424374a263eSTao Ma 	return ret;
425374a263eSTao Ma }
426374a263eSTao Ma 
427374a263eSTao Ma /*
428374a263eSTao Ma  * Lock the refcount tree pointed by ref_blkno and return the tree.
429374a263eSTao Ma  * In most case, we lock the tree and read the refcount block.
430374a263eSTao Ma  * So read it here if the caller really needs it.
431374a263eSTao Ma  *
432374a263eSTao Ma  * If the tree has been re-created by other node, it will free the
433374a263eSTao Ma  * old one and re-create it.
434374a263eSTao Ma  */
435374a263eSTao Ma int ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
436374a263eSTao Ma 			     u64 ref_blkno, int rw,
437374a263eSTao Ma 			     struct ocfs2_refcount_tree **ret_tree,
438374a263eSTao Ma 			     struct buffer_head **ref_bh)
439374a263eSTao Ma {
440374a263eSTao Ma 	int ret, delete_tree = 0;
441374a263eSTao Ma 	struct ocfs2_refcount_tree *tree = NULL;
442374a263eSTao Ma 	struct buffer_head *ref_root_bh = NULL;
443374a263eSTao Ma 	struct ocfs2_refcount_block *rb;
444374a263eSTao Ma 
445374a263eSTao Ma again:
446374a263eSTao Ma 	ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
447374a263eSTao Ma 	if (ret) {
448374a263eSTao Ma 		mlog_errno(ret);
449374a263eSTao Ma 		return ret;
450374a263eSTao Ma 	}
451374a263eSTao Ma 
452374a263eSTao Ma 	ocfs2_refcount_tree_get(tree);
453374a263eSTao Ma 
454374a263eSTao Ma 	ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
455374a263eSTao Ma 	if (ret) {
456374a263eSTao Ma 		mlog_errno(ret);
457374a263eSTao Ma 		ocfs2_refcount_tree_put(tree);
458374a263eSTao Ma 		goto out;
459374a263eSTao Ma 	}
460374a263eSTao Ma 
461374a263eSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
462374a263eSTao Ma 					&ref_root_bh);
463374a263eSTao Ma 	if (ret) {
464374a263eSTao Ma 		mlog_errno(ret);
465374a263eSTao Ma 		ocfs2_unlock_refcount_tree(osb, tree, rw);
466374a263eSTao Ma 		ocfs2_refcount_tree_put(tree);
467374a263eSTao Ma 		goto out;
468374a263eSTao Ma 	}
469374a263eSTao Ma 
470374a263eSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
471374a263eSTao Ma 	/*
472374a263eSTao Ma 	 * If the refcount block has been freed and re-created, we may need
473374a263eSTao Ma 	 * to recreate the refcount tree also.
474374a263eSTao Ma 	 *
475374a263eSTao Ma 	 * Here we just remove the tree from the rb-tree, and the last
476374a263eSTao Ma 	 * kref holder will unlock and delete this refcount_tree.
477374a263eSTao Ma 	 * Then we goto "again" and ocfs2_get_refcount_tree will create
478374a263eSTao Ma 	 * the new refcount tree for us.
479374a263eSTao Ma 	 */
480374a263eSTao Ma 	if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) {
481374a263eSTao Ma 		if (!tree->rf_removed) {
482374a263eSTao Ma 			ocfs2_erase_refcount_tree_from_list(osb, tree);
483374a263eSTao Ma 			tree->rf_removed = 1;
484374a263eSTao Ma 			delete_tree = 1;
485374a263eSTao Ma 		}
486374a263eSTao Ma 
487374a263eSTao Ma 		ocfs2_unlock_refcount_tree(osb, tree, rw);
488374a263eSTao Ma 		/*
489374a263eSTao Ma 		 * We get an extra reference when we create the refcount
490374a263eSTao Ma 		 * tree, so another put will destroy it.
491374a263eSTao Ma 		 */
492374a263eSTao Ma 		if (delete_tree)
493374a263eSTao Ma 			ocfs2_refcount_tree_put(tree);
494374a263eSTao Ma 		brelse(ref_root_bh);
495374a263eSTao Ma 		ref_root_bh = NULL;
496374a263eSTao Ma 		goto again;
497374a263eSTao Ma 	}
498374a263eSTao Ma 
499374a263eSTao Ma 	*ret_tree = tree;
500374a263eSTao Ma 	if (ref_bh) {
501374a263eSTao Ma 		*ref_bh = ref_root_bh;
502374a263eSTao Ma 		ref_root_bh = NULL;
503374a263eSTao Ma 	}
504374a263eSTao Ma out:
505374a263eSTao Ma 	brelse(ref_root_bh);
506374a263eSTao Ma 	return ret;
507374a263eSTao Ma }
508374a263eSTao Ma 
509374a263eSTao Ma int ocfs2_lock_refcount_tree_by_inode(struct inode *inode, int rw,
510374a263eSTao Ma 				      struct ocfs2_refcount_tree **ret_tree,
511374a263eSTao Ma 				      struct buffer_head **ref_bh)
512374a263eSTao Ma {
513374a263eSTao Ma 	int ret;
514374a263eSTao Ma 	u64 ref_blkno;
515374a263eSTao Ma 
516374a263eSTao Ma 	ret = ocfs2_get_refcount_block(inode, &ref_blkno);
517374a263eSTao Ma 	if (ret) {
518374a263eSTao Ma 		mlog_errno(ret);
519374a263eSTao Ma 		return ret;
520374a263eSTao Ma 	}
521374a263eSTao Ma 
522374a263eSTao Ma 	return ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno,
523374a263eSTao Ma 					rw, ret_tree, ref_bh);
524374a263eSTao Ma }
525374a263eSTao Ma 
526374a263eSTao Ma void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
527374a263eSTao Ma 				struct ocfs2_refcount_tree *tree, int rw)
528374a263eSTao Ma {
529374a263eSTao Ma 	if (rw)
530374a263eSTao Ma 		up_write(&tree->rf_sem);
531374a263eSTao Ma 	else
532374a263eSTao Ma 		up_read(&tree->rf_sem);
533374a263eSTao Ma 
534374a263eSTao Ma 	ocfs2_refcount_unlock(tree, rw);
535374a263eSTao Ma 	ocfs2_refcount_tree_put(tree);
536374a263eSTao Ma }
537374a263eSTao Ma 
538374a263eSTao Ma void ocfs2_purge_refcount_trees(struct ocfs2_super *osb)
539374a263eSTao Ma {
540374a263eSTao Ma 	struct rb_node *node;
541374a263eSTao Ma 	struct ocfs2_refcount_tree *tree;
542374a263eSTao Ma 	struct rb_root *root = &osb->osb_rf_lock_tree;
543374a263eSTao Ma 
544374a263eSTao Ma 	while ((node = rb_last(root)) != NULL) {
545374a263eSTao Ma 		tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node);
546374a263eSTao Ma 
547374a263eSTao Ma 		mlog(0, "Purge tree %llu\n",
548374a263eSTao Ma 		     (unsigned long long) tree->rf_blkno);
549374a263eSTao Ma 
550374a263eSTao Ma 		rb_erase(&tree->rf_node, root);
551374a263eSTao Ma 		ocfs2_free_refcount_tree(tree);
552374a263eSTao Ma 	}
553374a263eSTao Ma }
5548bf396deSTao Ma 
5558bf396deSTao Ma /*
5568bf396deSTao Ma  * Create a refcount tree for an inode.
5578bf396deSTao Ma  * We take for granted that the inode is already locked.
5588bf396deSTao Ma  */
5598bf396deSTao Ma static int ocfs2_create_refcount_tree(struct inode *inode,
5608bf396deSTao Ma 				      struct buffer_head *di_bh)
5618bf396deSTao Ma {
5628bf396deSTao Ma 	int ret;
5638bf396deSTao Ma 	handle_t *handle = NULL;
5648bf396deSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
5658bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
5668bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
5678bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5688bf396deSTao Ma 	struct buffer_head *new_bh = NULL;
5698bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
5708bf396deSTao Ma 	struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
5718bf396deSTao Ma 	u16 suballoc_bit_start;
5728bf396deSTao Ma 	u32 num_got;
5738bf396deSTao Ma 	u64 first_blkno;
5748bf396deSTao Ma 
5758bf396deSTao Ma 	BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
5768bf396deSTao Ma 
5778bf396deSTao Ma 	mlog(0, "create tree for inode %lu\n", inode->i_ino);
5788bf396deSTao Ma 
5798bf396deSTao Ma 	ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
5808bf396deSTao Ma 	if (ret) {
5818bf396deSTao Ma 		mlog_errno(ret);
5828bf396deSTao Ma 		goto out;
5838bf396deSTao Ma 	}
5848bf396deSTao Ma 
5858bf396deSTao Ma 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS);
5868bf396deSTao Ma 	if (IS_ERR(handle)) {
5878bf396deSTao Ma 		ret = PTR_ERR(handle);
5888bf396deSTao Ma 		mlog_errno(ret);
5898bf396deSTao Ma 		goto out;
5908bf396deSTao Ma 	}
5918bf396deSTao Ma 
5928bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
5938bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
5948bf396deSTao Ma 	if (ret) {
5958bf396deSTao Ma 		mlog_errno(ret);
5968bf396deSTao Ma 		goto out_commit;
5978bf396deSTao Ma 	}
5988bf396deSTao Ma 
5998bf396deSTao Ma 	ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
6008bf396deSTao Ma 				   &suballoc_bit_start, &num_got,
6018bf396deSTao Ma 				   &first_blkno);
6028bf396deSTao Ma 	if (ret) {
6038bf396deSTao Ma 		mlog_errno(ret);
6048bf396deSTao Ma 		goto out_commit;
6058bf396deSTao Ma 	}
6068bf396deSTao Ma 
6078bf396deSTao Ma 	new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
6088bf396deSTao Ma 	if (!new_tree) {
6098bf396deSTao Ma 		ret = -ENOMEM;
6108bf396deSTao Ma 		mlog_errno(ret);
6118bf396deSTao Ma 		goto out_commit;
6128bf396deSTao Ma 	}
6138bf396deSTao Ma 
6148bf396deSTao Ma 	new_bh = sb_getblk(inode->i_sb, first_blkno);
6158bf396deSTao Ma 	ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh);
6168bf396deSTao Ma 
6178bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh,
6188bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
6198bf396deSTao Ma 	if (ret) {
6208bf396deSTao Ma 		mlog_errno(ret);
6218bf396deSTao Ma 		goto out_commit;
6228bf396deSTao Ma 	}
6238bf396deSTao Ma 
6248bf396deSTao Ma 	/* Initialize ocfs2_refcount_block. */
6258bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)new_bh->b_data;
6268bf396deSTao Ma 	memset(rb, 0, inode->i_sb->s_blocksize);
6278bf396deSTao Ma 	strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
6288bf396deSTao Ma 	rb->rf_suballoc_slot = cpu_to_le16(osb->slot_num);
6298bf396deSTao Ma 	rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
6308bf396deSTao Ma 	rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
6318bf396deSTao Ma 	rb->rf_blkno = cpu_to_le64(first_blkno);
6328bf396deSTao Ma 	rb->rf_count = cpu_to_le32(1);
6338bf396deSTao Ma 	rb->rf_records.rl_count =
6348bf396deSTao Ma 			cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb));
6358bf396deSTao Ma 	spin_lock(&osb->osb_lock);
6368bf396deSTao Ma 	rb->rf_generation = osb->s_next_generation++;
6378bf396deSTao Ma 	spin_unlock(&osb->osb_lock);
6388bf396deSTao Ma 
6398bf396deSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
6408bf396deSTao Ma 
6418bf396deSTao Ma 	spin_lock(&oi->ip_lock);
6428bf396deSTao Ma 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
6438bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6448bf396deSTao Ma 	di->i_refcount_loc = cpu_to_le64(first_blkno);
6458bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
6468bf396deSTao Ma 
6478bf396deSTao Ma 	mlog(0, "created tree for inode %lu, refblock %llu\n",
6488bf396deSTao Ma 	     inode->i_ino, (unsigned long long)first_blkno);
6498bf396deSTao Ma 
6508bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
6518bf396deSTao Ma 
6528bf396deSTao Ma 	/*
6538bf396deSTao Ma 	 * We have to init the tree lock here since it will use
6548bf396deSTao Ma 	 * the generation number to create it.
6558bf396deSTao Ma 	 */
6568bf396deSTao Ma 	new_tree->rf_generation = le32_to_cpu(rb->rf_generation);
6578bf396deSTao Ma 	ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
6588bf396deSTao Ma 				      new_tree->rf_generation);
6598bf396deSTao Ma 
6608bf396deSTao Ma 	spin_lock(&osb->osb_lock);
6618bf396deSTao Ma 	tree = ocfs2_find_refcount_tree(osb, first_blkno);
6628bf396deSTao Ma 
6638bf396deSTao Ma 	/*
6648bf396deSTao Ma 	 * We've just created a new refcount tree in this block.  If
6658bf396deSTao Ma 	 * we found a refcount tree on the ocfs2_super, it must be
6668bf396deSTao Ma 	 * one we just deleted.  We free the old tree before
6678bf396deSTao Ma 	 * inserting the new tree.
6688bf396deSTao Ma 	 */
6698bf396deSTao Ma 	BUG_ON(tree && tree->rf_generation == new_tree->rf_generation);
6708bf396deSTao Ma 	if (tree)
6718bf396deSTao Ma 		ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
6728bf396deSTao Ma 	ocfs2_insert_refcount_tree(osb, new_tree);
6738bf396deSTao Ma 	spin_unlock(&osb->osb_lock);
6748bf396deSTao Ma 	new_tree = NULL;
6758bf396deSTao Ma 	if (tree)
6768bf396deSTao Ma 		ocfs2_refcount_tree_put(tree);
6778bf396deSTao Ma 
6788bf396deSTao Ma out_commit:
6798bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
6808bf396deSTao Ma 
6818bf396deSTao Ma out:
6828bf396deSTao Ma 	if (new_tree) {
6838bf396deSTao Ma 		ocfs2_metadata_cache_exit(&new_tree->rf_ci);
6848bf396deSTao Ma 		kfree(new_tree);
6858bf396deSTao Ma 	}
6868bf396deSTao Ma 
6878bf396deSTao Ma 	brelse(new_bh);
6888bf396deSTao Ma 	if (meta_ac)
6898bf396deSTao Ma 		ocfs2_free_alloc_context(meta_ac);
6908bf396deSTao Ma 
6918bf396deSTao Ma 	return ret;
6928bf396deSTao Ma }
6938bf396deSTao Ma 
6948bf396deSTao Ma static int ocfs2_set_refcount_tree(struct inode *inode,
6958bf396deSTao Ma 				   struct buffer_head *di_bh,
6968bf396deSTao Ma 				   u64 refcount_loc)
6978bf396deSTao Ma {
6988bf396deSTao Ma 	int ret;
6998bf396deSTao Ma 	handle_t *handle = NULL;
7008bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7018bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
7028bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7038bf396deSTao Ma 	struct buffer_head *ref_root_bh = NULL;
7048bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
7058bf396deSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
7068bf396deSTao Ma 
7078bf396deSTao Ma 	BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
7088bf396deSTao Ma 
7098bf396deSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
7108bf396deSTao Ma 				       &ref_tree, &ref_root_bh);
7118bf396deSTao Ma 	if (ret) {
7128bf396deSTao Ma 		mlog_errno(ret);
7138bf396deSTao Ma 		return ret;
7148bf396deSTao Ma 	}
7158bf396deSTao Ma 
7168bf396deSTao Ma 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS);
7178bf396deSTao Ma 	if (IS_ERR(handle)) {
7188bf396deSTao Ma 		ret = PTR_ERR(handle);
7198bf396deSTao Ma 		mlog_errno(ret);
7208bf396deSTao Ma 		goto out;
7218bf396deSTao Ma 	}
7228bf396deSTao Ma 
7238bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7248bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
7258bf396deSTao Ma 	if (ret) {
7268bf396deSTao Ma 		mlog_errno(ret);
7278bf396deSTao Ma 		goto out_commit;
7288bf396deSTao Ma 	}
7298bf396deSTao Ma 
7308bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh,
7318bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
7328bf396deSTao Ma 	if (ret) {
7338bf396deSTao Ma 		mlog_errno(ret);
7348bf396deSTao Ma 		goto out_commit;
7358bf396deSTao Ma 	}
7368bf396deSTao Ma 
7378bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
7388bf396deSTao Ma 	le32_add_cpu(&rb->rf_count, 1);
7398bf396deSTao Ma 
7408bf396deSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
7418bf396deSTao Ma 
7428bf396deSTao Ma 	spin_lock(&oi->ip_lock);
7438bf396deSTao Ma 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
7448bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7458bf396deSTao Ma 	di->i_refcount_loc = cpu_to_le64(refcount_loc);
7468bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
7478bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
7488bf396deSTao Ma 
7498bf396deSTao Ma out_commit:
7508bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
7518bf396deSTao Ma out:
7528bf396deSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7538bf396deSTao Ma 	brelse(ref_root_bh);
7548bf396deSTao Ma 
7558bf396deSTao Ma 	return ret;
7568bf396deSTao Ma }
7578bf396deSTao Ma 
7588bf396deSTao Ma int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
7598bf396deSTao Ma {
7608bf396deSTao Ma 	int ret, delete_tree = 0;
7618bf396deSTao Ma 	handle_t *handle = NULL;
7628bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7638bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
7648bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7658bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
7668bf396deSTao Ma 	struct inode *alloc_inode = NULL;
7678bf396deSTao Ma 	struct buffer_head *alloc_bh = NULL;
7688bf396deSTao Ma 	struct buffer_head *blk_bh = NULL;
7698bf396deSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
7708bf396deSTao Ma 	int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS;
7718bf396deSTao Ma 	u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc);
7728bf396deSTao Ma 	u16 bit = 0;
7738bf396deSTao Ma 
7748bf396deSTao Ma 	if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL))
7758bf396deSTao Ma 		return 0;
7768bf396deSTao Ma 
7778bf396deSTao Ma 	BUG_ON(!ref_blkno);
7788bf396deSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh);
7798bf396deSTao Ma 	if (ret) {
7808bf396deSTao Ma 		mlog_errno(ret);
7818bf396deSTao Ma 		return ret;
7828bf396deSTao Ma 	}
7838bf396deSTao Ma 
7848bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)blk_bh->b_data;
7858bf396deSTao Ma 
7868bf396deSTao Ma 	/*
7878bf396deSTao Ma 	 * If we are the last user, we need to free the block.
7888bf396deSTao Ma 	 * So lock the allocator ahead.
7898bf396deSTao Ma 	 */
7908bf396deSTao Ma 	if (le32_to_cpu(rb->rf_count) == 1) {
7918bf396deSTao Ma 		blk = le64_to_cpu(rb->rf_blkno);
7928bf396deSTao Ma 		bit = le16_to_cpu(rb->rf_suballoc_bit);
7938bf396deSTao Ma 		bg_blkno = ocfs2_which_suballoc_group(blk, bit);
7948bf396deSTao Ma 
7958bf396deSTao Ma 		alloc_inode = ocfs2_get_system_file_inode(osb,
7968bf396deSTao Ma 					EXTENT_ALLOC_SYSTEM_INODE,
7978bf396deSTao Ma 					le16_to_cpu(rb->rf_suballoc_slot));
7988bf396deSTao Ma 		if (!alloc_inode) {
7998bf396deSTao Ma 			ret = -ENOMEM;
8008bf396deSTao Ma 			mlog_errno(ret);
8018bf396deSTao Ma 			goto out;
8028bf396deSTao Ma 		}
8038bf396deSTao Ma 		mutex_lock(&alloc_inode->i_mutex);
8048bf396deSTao Ma 
8058bf396deSTao Ma 		ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1);
8068bf396deSTao Ma 		if (ret) {
8078bf396deSTao Ma 			mlog_errno(ret);
8088bf396deSTao Ma 			goto out_mutex;
8098bf396deSTao Ma 		}
8108bf396deSTao Ma 
8118bf396deSTao Ma 		credits += OCFS2_SUBALLOC_FREE;
8128bf396deSTao Ma 	}
8138bf396deSTao Ma 
8148bf396deSTao Ma 	handle = ocfs2_start_trans(osb, credits);
8158bf396deSTao Ma 	if (IS_ERR(handle)) {
8168bf396deSTao Ma 		ret = PTR_ERR(handle);
8178bf396deSTao Ma 		mlog_errno(ret);
8188bf396deSTao Ma 		goto out_unlock;
8198bf396deSTao Ma 	}
8208bf396deSTao Ma 
8218bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
8228bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
8238bf396deSTao Ma 	if (ret) {
8248bf396deSTao Ma 		mlog_errno(ret);
8258bf396deSTao Ma 		goto out_commit;
8268bf396deSTao Ma 	}
8278bf396deSTao Ma 
8288bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh,
8298bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
8308bf396deSTao Ma 	if (ret) {
8318bf396deSTao Ma 		mlog_errno(ret);
8328bf396deSTao Ma 		goto out_commit;
8338bf396deSTao Ma 	}
8348bf396deSTao Ma 
8358bf396deSTao Ma 	spin_lock(&oi->ip_lock);
8368bf396deSTao Ma 	oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL;
8378bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
8388bf396deSTao Ma 	di->i_refcount_loc = 0;
8398bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
8408bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
8418bf396deSTao Ma 
8428bf396deSTao Ma 	le32_add_cpu(&rb->rf_count , -1);
8438bf396deSTao Ma 	ocfs2_journal_dirty(handle, blk_bh);
8448bf396deSTao Ma 
8458bf396deSTao Ma 	if (!rb->rf_count) {
8468bf396deSTao Ma 		delete_tree = 1;
8478bf396deSTao Ma 		ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
8488bf396deSTao Ma 		ret = ocfs2_free_suballoc_bits(handle, alloc_inode,
8498bf396deSTao Ma 					       alloc_bh, bit, bg_blkno, 1);
8508bf396deSTao Ma 		if (ret)
8518bf396deSTao Ma 			mlog_errno(ret);
8528bf396deSTao Ma 	}
8538bf396deSTao Ma 
8548bf396deSTao Ma out_commit:
8558bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
8568bf396deSTao Ma out_unlock:
8578bf396deSTao Ma 	if (alloc_inode) {
8588bf396deSTao Ma 		ocfs2_inode_unlock(alloc_inode, 1);
8598bf396deSTao Ma 		brelse(alloc_bh);
8608bf396deSTao Ma 	}
8618bf396deSTao Ma out_mutex:
8628bf396deSTao Ma 	if (alloc_inode) {
8638bf396deSTao Ma 		mutex_unlock(&alloc_inode->i_mutex);
8648bf396deSTao Ma 		iput(alloc_inode);
8658bf396deSTao Ma 	}
8668bf396deSTao Ma out:
8678bf396deSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
8688bf396deSTao Ma 	if (delete_tree)
8698bf396deSTao Ma 		ocfs2_refcount_tree_put(ref_tree);
8708bf396deSTao Ma 	brelse(blk_bh);
8718bf396deSTao Ma 
8728bf396deSTao Ma 	return ret;
8738bf396deSTao Ma }
874e73a819dSTao Ma 
875e73a819dSTao Ma static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci,
876e73a819dSTao Ma 					  struct buffer_head *ref_leaf_bh,
877e73a819dSTao Ma 					  u64 cpos, unsigned int len,
878e73a819dSTao Ma 					  struct ocfs2_refcount_rec *ret_rec,
879e73a819dSTao Ma 					  int *index)
880e73a819dSTao Ma {
881e73a819dSTao Ma 	int i = 0;
882e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
883e73a819dSTao Ma 		(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
884e73a819dSTao Ma 	struct ocfs2_refcount_rec *rec = NULL;
885e73a819dSTao Ma 
886e73a819dSTao Ma 	for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) {
887e73a819dSTao Ma 		rec = &rb->rf_records.rl_recs[i];
888e73a819dSTao Ma 
889e73a819dSTao Ma 		if (le64_to_cpu(rec->r_cpos) +
890e73a819dSTao Ma 		    le32_to_cpu(rec->r_clusters) <= cpos)
891e73a819dSTao Ma 			continue;
892e73a819dSTao Ma 		else if (le64_to_cpu(rec->r_cpos) > cpos)
893e73a819dSTao Ma 			break;
894e73a819dSTao Ma 
895e73a819dSTao Ma 		/* ok, cpos fail in this rec. Just return. */
896e73a819dSTao Ma 		if (ret_rec)
897e73a819dSTao Ma 			*ret_rec = *rec;
898e73a819dSTao Ma 		goto out;
899e73a819dSTao Ma 	}
900e73a819dSTao Ma 
901e73a819dSTao Ma 	if (ret_rec) {
902e73a819dSTao Ma 		/* We meet with a hole here, so fake the rec. */
903e73a819dSTao Ma 		ret_rec->r_cpos = cpu_to_le64(cpos);
904e73a819dSTao Ma 		ret_rec->r_refcount = 0;
905e73a819dSTao Ma 		if (i < le16_to_cpu(rb->rf_records.rl_used) &&
906e73a819dSTao Ma 		    le64_to_cpu(rec->r_cpos) < cpos + len)
907e73a819dSTao Ma 			ret_rec->r_clusters =
908e73a819dSTao Ma 				cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos);
909e73a819dSTao Ma 		else
910e73a819dSTao Ma 			ret_rec->r_clusters = cpu_to_le32(len);
911e73a819dSTao Ma 	}
912e73a819dSTao Ma 
913e73a819dSTao Ma out:
914e73a819dSTao Ma 	*index = i;
915e73a819dSTao Ma }
916e73a819dSTao Ma 
917e73a819dSTao Ma /*
918e73a819dSTao Ma  * Given a cpos and len, try to find the refcount record which contains cpos.
919e73a819dSTao Ma  * 1. If cpos can be found in one refcount record, return the record.
920e73a819dSTao Ma  * 2. If cpos can't be found, return a fake record which start from cpos
921e73a819dSTao Ma  *    and end at a small value between cpos+len and start of the next record.
922e73a819dSTao Ma  *    This fake record has r_refcount = 0.
923e73a819dSTao Ma  */
924e73a819dSTao Ma static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
925e73a819dSTao Ma 				  struct buffer_head *ref_root_bh,
926e73a819dSTao Ma 				  u64 cpos, unsigned int len,
927e73a819dSTao Ma 				  struct ocfs2_refcount_rec *ret_rec,
928e73a819dSTao Ma 				  int *index,
929e73a819dSTao Ma 				  struct buffer_head **ret_bh)
930e73a819dSTao Ma {
931e73a819dSTao Ma 	int ret = 0, i, found;
932e73a819dSTao Ma 	u32 low_cpos;
933e73a819dSTao Ma 	struct ocfs2_extent_list *el;
934e73a819dSTao Ma 	struct ocfs2_extent_rec *tmp, *rec = NULL;
935e73a819dSTao Ma 	struct ocfs2_extent_block *eb;
936e73a819dSTao Ma 	struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
937e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
938e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
939e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
940e73a819dSTao Ma 
941e73a819dSTao Ma 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) {
942e73a819dSTao Ma 		ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
943e73a819dSTao Ma 					      ret_rec, index);
944e73a819dSTao Ma 		*ret_bh = ref_root_bh;
945e73a819dSTao Ma 		get_bh(ref_root_bh);
946e73a819dSTao Ma 		return 0;
947e73a819dSTao Ma 	}
948e73a819dSTao Ma 
949e73a819dSTao Ma 	el = &rb->rf_list;
950e73a819dSTao Ma 	low_cpos = cpos & OCFS2_32BIT_POS_MASK;
951e73a819dSTao Ma 
952e73a819dSTao Ma 	if (el->l_tree_depth) {
953e73a819dSTao Ma 		ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh);
954e73a819dSTao Ma 		if (ret) {
955e73a819dSTao Ma 			mlog_errno(ret);
956e73a819dSTao Ma 			goto out;
957e73a819dSTao Ma 		}
958e73a819dSTao Ma 
959e73a819dSTao Ma 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
960e73a819dSTao Ma 		el = &eb->h_list;
961e73a819dSTao Ma 
962e73a819dSTao Ma 		if (el->l_tree_depth) {
963e73a819dSTao Ma 			ocfs2_error(sb,
964e73a819dSTao Ma 			"refcount tree %llu has non zero tree "
965e73a819dSTao Ma 			"depth in leaf btree tree block %llu\n",
966e73a819dSTao Ma 			(unsigned long long)ocfs2_metadata_cache_owner(ci),
967e73a819dSTao Ma 			(unsigned long long)eb_bh->b_blocknr);
968e73a819dSTao Ma 			ret = -EROFS;
969e73a819dSTao Ma 			goto out;
970e73a819dSTao Ma 		}
971e73a819dSTao Ma 	}
972e73a819dSTao Ma 
973e73a819dSTao Ma 	found = 0;
974e73a819dSTao Ma 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
975e73a819dSTao Ma 		rec = &el->l_recs[i];
976e73a819dSTao Ma 
977e73a819dSTao Ma 		if (le32_to_cpu(rec->e_cpos) <= low_cpos) {
978e73a819dSTao Ma 			found = 1;
979e73a819dSTao Ma 			break;
980e73a819dSTao Ma 		}
981e73a819dSTao Ma 	}
982e73a819dSTao Ma 
983e73a819dSTao Ma 	/* adjust len when we have ocfs2_extent_rec after it. */
984e73a819dSTao Ma 	if (found && i < le16_to_cpu(el->l_next_free_rec) - 1) {
985e73a819dSTao Ma 		tmp = &el->l_recs[i+1];
986e73a819dSTao Ma 
987e73a819dSTao Ma 		if (le32_to_cpu(tmp->e_cpos) < cpos + len)
988e73a819dSTao Ma 			len = le32_to_cpu(tmp->e_cpos) - cpos;
989e73a819dSTao Ma 	}
990e73a819dSTao Ma 
991e73a819dSTao Ma 	ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
992e73a819dSTao Ma 					&ref_leaf_bh);
993e73a819dSTao Ma 	if (ret) {
994e73a819dSTao Ma 		mlog_errno(ret);
995e73a819dSTao Ma 		goto out;
996e73a819dSTao Ma 	}
997e73a819dSTao Ma 
998e73a819dSTao Ma 	ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
999e73a819dSTao Ma 				      ret_rec, index);
1000e73a819dSTao Ma 	*ret_bh = ref_leaf_bh;
1001e73a819dSTao Ma out:
1002e73a819dSTao Ma 	brelse(eb_bh);
1003e73a819dSTao Ma 	return ret;
1004e73a819dSTao Ma }
1005e73a819dSTao Ma 
1006e73a819dSTao Ma enum ocfs2_ref_rec_contig {
1007e73a819dSTao Ma 	REF_CONTIG_NONE = 0,
1008e73a819dSTao Ma 	REF_CONTIG_LEFT,
1009e73a819dSTao Ma 	REF_CONTIG_RIGHT,
1010e73a819dSTao Ma 	REF_CONTIG_LEFTRIGHT,
1011e73a819dSTao Ma };
1012e73a819dSTao Ma 
1013e73a819dSTao Ma static enum ocfs2_ref_rec_contig
1014e73a819dSTao Ma 	ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb,
1015e73a819dSTao Ma 				    int index)
1016e73a819dSTao Ma {
1017e73a819dSTao Ma 	if ((rb->rf_records.rl_recs[index].r_refcount ==
1018e73a819dSTao Ma 	    rb->rf_records.rl_recs[index + 1].r_refcount) &&
1019e73a819dSTao Ma 	    (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) +
1020e73a819dSTao Ma 	    le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) ==
1021e73a819dSTao Ma 	    le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos)))
1022e73a819dSTao Ma 		return REF_CONTIG_RIGHT;
1023e73a819dSTao Ma 
1024e73a819dSTao Ma 	return REF_CONTIG_NONE;
1025e73a819dSTao Ma }
1026e73a819dSTao Ma 
1027e73a819dSTao Ma static enum ocfs2_ref_rec_contig
1028e73a819dSTao Ma 	ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb,
1029e73a819dSTao Ma 				  int index)
1030e73a819dSTao Ma {
1031e73a819dSTao Ma 	enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE;
1032e73a819dSTao Ma 
1033e73a819dSTao Ma 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 1)
1034e73a819dSTao Ma 		ret = ocfs2_refcount_rec_adjacent(rb, index);
1035e73a819dSTao Ma 
1036e73a819dSTao Ma 	if (index > 0) {
1037e73a819dSTao Ma 		enum ocfs2_ref_rec_contig tmp;
1038e73a819dSTao Ma 
1039e73a819dSTao Ma 		tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
1040e73a819dSTao Ma 
1041e73a819dSTao Ma 		if (tmp == REF_CONTIG_RIGHT) {
1042e73a819dSTao Ma 			if (ret == REF_CONTIG_RIGHT)
1043e73a819dSTao Ma 				ret = REF_CONTIG_LEFTRIGHT;
1044e73a819dSTao Ma 			else
1045e73a819dSTao Ma 				ret = REF_CONTIG_LEFT;
1046e73a819dSTao Ma 		}
1047e73a819dSTao Ma 	}
1048e73a819dSTao Ma 
1049e73a819dSTao Ma 	return ret;
1050e73a819dSTao Ma }
1051e73a819dSTao Ma 
1052e73a819dSTao Ma static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb,
1053e73a819dSTao Ma 					   int index)
1054e73a819dSTao Ma {
1055e73a819dSTao Ma 	BUG_ON(rb->rf_records.rl_recs[index].r_refcount !=
1056e73a819dSTao Ma 	       rb->rf_records.rl_recs[index+1].r_refcount);
1057e73a819dSTao Ma 
1058e73a819dSTao Ma 	le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters,
1059e73a819dSTao Ma 		     le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters));
1060e73a819dSTao Ma 
1061e73a819dSTao Ma 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 2)
1062e73a819dSTao Ma 		memmove(&rb->rf_records.rl_recs[index + 1],
1063e73a819dSTao Ma 			&rb->rf_records.rl_recs[index + 2],
1064e73a819dSTao Ma 			sizeof(struct ocfs2_refcount_rec) *
1065e73a819dSTao Ma 			(le16_to_cpu(rb->rf_records.rl_used) - index - 2));
1066e73a819dSTao Ma 
1067e73a819dSTao Ma 	memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1],
1068e73a819dSTao Ma 	       0, sizeof(struct ocfs2_refcount_rec));
1069e73a819dSTao Ma 	le16_add_cpu(&rb->rf_records.rl_used, -1);
1070e73a819dSTao Ma }
1071e73a819dSTao Ma 
1072e73a819dSTao Ma /*
1073e73a819dSTao Ma  * Merge the refcount rec if we are contiguous with the adjacent recs.
1074e73a819dSTao Ma  */
1075e73a819dSTao Ma static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb,
1076e73a819dSTao Ma 				     int index)
1077e73a819dSTao Ma {
1078e73a819dSTao Ma 	enum ocfs2_ref_rec_contig contig =
1079e73a819dSTao Ma 				ocfs2_refcount_rec_contig(rb, index);
1080e73a819dSTao Ma 
1081e73a819dSTao Ma 	if (contig == REF_CONTIG_NONE)
1082e73a819dSTao Ma 		return;
1083e73a819dSTao Ma 
1084e73a819dSTao Ma 	if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) {
1085e73a819dSTao Ma 		BUG_ON(index == 0);
1086e73a819dSTao Ma 		index--;
1087e73a819dSTao Ma 	}
1088e73a819dSTao Ma 
1089e73a819dSTao Ma 	ocfs2_rotate_refcount_rec_left(rb, index);
1090e73a819dSTao Ma 
1091e73a819dSTao Ma 	if (contig == REF_CONTIG_LEFTRIGHT)
1092e73a819dSTao Ma 		ocfs2_rotate_refcount_rec_left(rb, index);
1093e73a819dSTao Ma }
1094e73a819dSTao Ma 
10951823cb0bSTao Ma /*
10961823cb0bSTao Ma  * Change the refcount indexed by "index" in ref_bh.
10971823cb0bSTao Ma  * If refcount reaches 0, remove it.
10981823cb0bSTao Ma  */
1099e73a819dSTao Ma static int ocfs2_change_refcount_rec(handle_t *handle,
1100e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1101e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1102e73a819dSTao Ma 				     int index, int change)
1103e73a819dSTao Ma {
1104e73a819dSTao Ma 	int ret;
1105e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1106e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
11071823cb0bSTao Ma 	struct ocfs2_refcount_list *rl = &rb->rf_records;
11081823cb0bSTao Ma 	struct ocfs2_refcount_rec *rec = &rl->rl_recs[index];
1109e73a819dSTao Ma 
1110e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1111e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1112e73a819dSTao Ma 	if (ret) {
1113e73a819dSTao Ma 		mlog_errno(ret);
1114e73a819dSTao Ma 		goto out;
1115e73a819dSTao Ma 	}
1116e73a819dSTao Ma 
1117e73a819dSTao Ma 	mlog(0, "change index %d, old count %u, change %d\n", index,
1118e73a819dSTao Ma 	     le32_to_cpu(rec->r_refcount), change);
1119e73a819dSTao Ma 	le32_add_cpu(&rec->r_refcount, change);
1120e73a819dSTao Ma 
11211823cb0bSTao Ma 	if (!rec->r_refcount) {
11221823cb0bSTao Ma 		if (index != le16_to_cpu(rl->rl_used) - 1) {
11231823cb0bSTao Ma 			memmove(rec, rec + 1,
11241823cb0bSTao Ma 				(le16_to_cpu(rl->rl_used) - index - 1) *
11251823cb0bSTao Ma 				sizeof(struct ocfs2_refcount_rec));
11261823cb0bSTao Ma 			memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1],
11271823cb0bSTao Ma 			       0, sizeof(struct ocfs2_refcount_rec));
11281823cb0bSTao Ma 		}
11291823cb0bSTao Ma 
11301823cb0bSTao Ma 		le16_add_cpu(&rl->rl_used, -1);
11311823cb0bSTao Ma 	} else
1132e73a819dSTao Ma 		ocfs2_refcount_rec_merge(rb, index);
1133e73a819dSTao Ma 
1134e73a819dSTao Ma 	ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
1135e73a819dSTao Ma 	if (ret)
1136e73a819dSTao Ma 		mlog_errno(ret);
1137e73a819dSTao Ma out:
1138e73a819dSTao Ma 	return ret;
1139e73a819dSTao Ma }
1140e73a819dSTao Ma 
1141e73a819dSTao Ma static int ocfs2_expand_inline_ref_root(handle_t *handle,
1142e73a819dSTao Ma 					struct ocfs2_caching_info *ci,
1143e73a819dSTao Ma 					struct buffer_head *ref_root_bh,
1144e73a819dSTao Ma 					struct buffer_head **ref_leaf_bh,
1145e73a819dSTao Ma 					struct ocfs2_alloc_context *meta_ac)
1146e73a819dSTao Ma {
1147e73a819dSTao Ma 	int ret;
1148e73a819dSTao Ma 	u16 suballoc_bit_start;
1149e73a819dSTao Ma 	u32 num_got;
1150e73a819dSTao Ma 	u64 blkno;
1151e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1152e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1153e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb;
1154e73a819dSTao Ma 	struct ocfs2_refcount_block *root_rb =
1155e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1156e73a819dSTao Ma 
1157e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1158e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1159e73a819dSTao Ma 	if (ret) {
1160e73a819dSTao Ma 		mlog_errno(ret);
1161e73a819dSTao Ma 		goto out;
1162e73a819dSTao Ma 	}
1163e73a819dSTao Ma 
1164e73a819dSTao Ma 	ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
1165e73a819dSTao Ma 				   &suballoc_bit_start, &num_got,
1166e73a819dSTao Ma 				   &blkno);
1167e73a819dSTao Ma 	if (ret) {
1168e73a819dSTao Ma 		mlog_errno(ret);
1169e73a819dSTao Ma 		goto out;
1170e73a819dSTao Ma 	}
1171e73a819dSTao Ma 
1172e73a819dSTao Ma 	new_bh = sb_getblk(sb, blkno);
1173e73a819dSTao Ma 	if (new_bh == NULL) {
1174e73a819dSTao Ma 		ret = -EIO;
1175e73a819dSTao Ma 		mlog_errno(ret);
1176e73a819dSTao Ma 		goto out;
1177e73a819dSTao Ma 	}
1178e73a819dSTao Ma 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1179e73a819dSTao Ma 
1180e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1181e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
1182e73a819dSTao Ma 	if (ret) {
1183e73a819dSTao Ma 		mlog_errno(ret);
1184e73a819dSTao Ma 		goto out;
1185e73a819dSTao Ma 	}
1186e73a819dSTao Ma 
1187e73a819dSTao Ma 	/*
1188e73a819dSTao Ma 	 * Initialize ocfs2_refcount_block.
1189e73a819dSTao Ma 	 * It should contain the same information as the old root.
1190e73a819dSTao Ma 	 * so just memcpy it and change the corresponding field.
1191e73a819dSTao Ma 	 */
1192e73a819dSTao Ma 	memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
1193e73a819dSTao Ma 
1194e73a819dSTao Ma 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1195e73a819dSTao Ma 	new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num);
1196e73a819dSTao Ma 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1197e73a819dSTao Ma 	new_rb->rf_blkno = cpu_to_le64(blkno);
1198e73a819dSTao Ma 	new_rb->rf_cpos = cpu_to_le32(0);
1199e73a819dSTao Ma 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1200e73a819dSTao Ma 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1201e73a819dSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
1202e73a819dSTao Ma 
1203e73a819dSTao Ma 	/* Now change the root. */
1204e73a819dSTao Ma 	memset(&root_rb->rf_list, 0, sb->s_blocksize -
1205e73a819dSTao Ma 	       offsetof(struct ocfs2_refcount_block, rf_list));
1206e73a819dSTao Ma 	root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb));
1207e73a819dSTao Ma 	root_rb->rf_clusters = cpu_to_le32(1);
1208e73a819dSTao Ma 	root_rb->rf_list.l_next_free_rec = cpu_to_le16(1);
1209e73a819dSTao Ma 	root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
1210e73a819dSTao Ma 	root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
1211e73a819dSTao Ma 	root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL);
1212e73a819dSTao Ma 
1213e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
1214e73a819dSTao Ma 
1215e73a819dSTao Ma 	mlog(0, "new leaf block %llu, used %u\n", (unsigned long long)blkno,
1216e73a819dSTao Ma 	     le16_to_cpu(new_rb->rf_records.rl_used));
1217e73a819dSTao Ma 
1218e73a819dSTao Ma 	*ref_leaf_bh = new_bh;
1219e73a819dSTao Ma 	new_bh = NULL;
1220e73a819dSTao Ma out:
1221e73a819dSTao Ma 	brelse(new_bh);
1222e73a819dSTao Ma 	return ret;
1223e73a819dSTao Ma }
1224e73a819dSTao Ma 
1225e73a819dSTao Ma static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev,
1226e73a819dSTao Ma 					   struct ocfs2_refcount_rec *next)
1227e73a819dSTao Ma {
1228e73a819dSTao Ma 	if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <=
1229e73a819dSTao Ma 		ocfs2_get_ref_rec_low_cpos(next))
1230e73a819dSTao Ma 		return 1;
1231e73a819dSTao Ma 
1232e73a819dSTao Ma 	return 0;
1233e73a819dSTao Ma }
1234e73a819dSTao Ma 
1235e73a819dSTao Ma static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b)
1236e73a819dSTao Ma {
1237e73a819dSTao Ma 	const struct ocfs2_refcount_rec *l = a, *r = b;
1238e73a819dSTao Ma 	u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
1239e73a819dSTao Ma 	u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
1240e73a819dSTao Ma 
1241e73a819dSTao Ma 	if (l_cpos > r_cpos)
1242e73a819dSTao Ma 		return 1;
1243e73a819dSTao Ma 	if (l_cpos < r_cpos)
1244e73a819dSTao Ma 		return -1;
1245e73a819dSTao Ma 	return 0;
1246e73a819dSTao Ma }
1247e73a819dSTao Ma 
1248e73a819dSTao Ma static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
1249e73a819dSTao Ma {
1250e73a819dSTao Ma 	const struct ocfs2_refcount_rec *l = a, *r = b;
1251e73a819dSTao Ma 	u64 l_cpos = le64_to_cpu(l->r_cpos);
1252e73a819dSTao Ma 	u64 r_cpos = le64_to_cpu(r->r_cpos);
1253e73a819dSTao Ma 
1254e73a819dSTao Ma 	if (l_cpos > r_cpos)
1255e73a819dSTao Ma 		return 1;
1256e73a819dSTao Ma 	if (l_cpos < r_cpos)
1257e73a819dSTao Ma 		return -1;
1258e73a819dSTao Ma 	return 0;
1259e73a819dSTao Ma }
1260e73a819dSTao Ma 
1261e73a819dSTao Ma static void swap_refcount_rec(void *a, void *b, int size)
1262e73a819dSTao Ma {
1263e73a819dSTao Ma 	struct ocfs2_refcount_rec *l = a, *r = b, tmp;
1264e73a819dSTao Ma 
1265e73a819dSTao Ma 	tmp = *(struct ocfs2_refcount_rec *)l;
1266e73a819dSTao Ma 	*(struct ocfs2_refcount_rec *)l =
1267e73a819dSTao Ma 			*(struct ocfs2_refcount_rec *)r;
1268e73a819dSTao Ma 	*(struct ocfs2_refcount_rec *)r = tmp;
1269e73a819dSTao Ma }
1270e73a819dSTao Ma 
1271e73a819dSTao Ma /*
1272e73a819dSTao Ma  * The refcount cpos are ordered by their 64bit cpos,
1273e73a819dSTao Ma  * But we will use the low 32 bit to be the e_cpos in the b-tree.
1274e73a819dSTao Ma  * So we need to make sure that this pos isn't intersected with others.
1275e73a819dSTao Ma  *
1276e73a819dSTao Ma  * Note: The refcount block is already sorted by their low 32 bit cpos,
1277e73a819dSTao Ma  *       So just try the middle pos first, and we will exit when we find
1278e73a819dSTao Ma  *       the good position.
1279e73a819dSTao Ma  */
1280e73a819dSTao Ma static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl,
1281e73a819dSTao Ma 					 u32 *split_pos, int *split_index)
1282e73a819dSTao Ma {
1283e73a819dSTao Ma 	int num_used = le16_to_cpu(rl->rl_used);
1284e73a819dSTao Ma 	int delta, middle = num_used / 2;
1285e73a819dSTao Ma 
1286e73a819dSTao Ma 	for (delta = 0; delta < middle; delta++) {
1287e73a819dSTao Ma 		/* Let's check delta earlier than middle */
1288e73a819dSTao Ma 		if (ocfs2_refcount_rec_no_intersect(
1289e73a819dSTao Ma 					&rl->rl_recs[middle - delta - 1],
1290e73a819dSTao Ma 					&rl->rl_recs[middle - delta])) {
1291e73a819dSTao Ma 			*split_index = middle - delta;
1292e73a819dSTao Ma 			break;
1293e73a819dSTao Ma 		}
1294e73a819dSTao Ma 
1295e73a819dSTao Ma 		/* For even counts, don't walk off the end */
1296e73a819dSTao Ma 		if ((middle + delta + 1) == num_used)
1297e73a819dSTao Ma 			continue;
1298e73a819dSTao Ma 
1299e73a819dSTao Ma 		/* Now try delta past middle */
1300e73a819dSTao Ma 		if (ocfs2_refcount_rec_no_intersect(
1301e73a819dSTao Ma 					&rl->rl_recs[middle + delta],
1302e73a819dSTao Ma 					&rl->rl_recs[middle + delta + 1])) {
1303e73a819dSTao Ma 			*split_index = middle + delta + 1;
1304e73a819dSTao Ma 			break;
1305e73a819dSTao Ma 		}
1306e73a819dSTao Ma 	}
1307e73a819dSTao Ma 
1308e73a819dSTao Ma 	if (delta >= middle)
1309e73a819dSTao Ma 		return -ENOSPC;
1310e73a819dSTao Ma 
1311e73a819dSTao Ma 	*split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]);
1312e73a819dSTao Ma 	return 0;
1313e73a819dSTao Ma }
1314e73a819dSTao Ma 
1315e73a819dSTao Ma static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
1316e73a819dSTao Ma 					    struct buffer_head *new_bh,
1317e73a819dSTao Ma 					    u32 *split_cpos)
1318e73a819dSTao Ma {
1319e73a819dSTao Ma 	int split_index = 0, num_moved, ret;
1320e73a819dSTao Ma 	u32 cpos = 0;
1321e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1322e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1323e73a819dSTao Ma 	struct ocfs2_refcount_list *rl = &rb->rf_records;
1324e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb =
1325e73a819dSTao Ma 			(struct ocfs2_refcount_block *)new_bh->b_data;
1326e73a819dSTao Ma 	struct ocfs2_refcount_list *new_rl = &new_rb->rf_records;
1327e73a819dSTao Ma 
1328e73a819dSTao Ma 	mlog(0, "split old leaf refcount block %llu, count = %u, used = %u\n",
1329e73a819dSTao Ma 	     (unsigned long long)ref_leaf_bh->b_blocknr,
1330e73a819dSTao Ma 	     le32_to_cpu(rl->rl_count), le32_to_cpu(rl->rl_used));
1331e73a819dSTao Ma 
1332e73a819dSTao Ma 	/*
1333e73a819dSTao Ma 	 * XXX: Improvement later.
1334e73a819dSTao Ma 	 * If we know all the high 32 bit cpos is the same, no need to sort.
1335e73a819dSTao Ma 	 *
1336e73a819dSTao Ma 	 * In order to make the whole process safe, we do:
1337e73a819dSTao Ma 	 * 1. sort the entries by their low 32 bit cpos first so that we can
1338e73a819dSTao Ma 	 *    find the split cpos easily.
1339e73a819dSTao Ma 	 * 2. call ocfs2_insert_extent to insert the new refcount block.
1340e73a819dSTao Ma 	 * 3. move the refcount rec to the new block.
1341e73a819dSTao Ma 	 * 4. sort the entries by their 64 bit cpos.
1342e73a819dSTao Ma 	 * 5. dirty the new_rb and rb.
1343e73a819dSTao Ma 	 */
1344e73a819dSTao Ma 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1345e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1346e73a819dSTao Ma 	     cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
1347e73a819dSTao Ma 
1348e73a819dSTao Ma 	ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
1349e73a819dSTao Ma 	if (ret) {
1350e73a819dSTao Ma 		mlog_errno(ret);
1351e73a819dSTao Ma 		return ret;
1352e73a819dSTao Ma 	}
1353e73a819dSTao Ma 
1354e73a819dSTao Ma 	new_rb->rf_cpos = cpu_to_le32(cpos);
1355e73a819dSTao Ma 
1356e73a819dSTao Ma 	/* move refcount records starting from split_index to the new block. */
1357e73a819dSTao Ma 	num_moved = le16_to_cpu(rl->rl_used) - split_index;
1358e73a819dSTao Ma 	memcpy(new_rl->rl_recs, &rl->rl_recs[split_index],
1359e73a819dSTao Ma 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1360e73a819dSTao Ma 
1361e73a819dSTao Ma 	/*ok, remove the entries we just moved over to the other block. */
1362e73a819dSTao Ma 	memset(&rl->rl_recs[split_index], 0,
1363e73a819dSTao Ma 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1364e73a819dSTao Ma 
1365e73a819dSTao Ma 	/* change old and new rl_used accordingly. */
1366e73a819dSTao Ma 	le16_add_cpu(&rl->rl_used, -num_moved);
1367e73a819dSTao Ma 	new_rl->rl_used = cpu_to_le32(num_moved);
1368e73a819dSTao Ma 
1369e73a819dSTao Ma 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1370e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1371e73a819dSTao Ma 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1372e73a819dSTao Ma 
1373e73a819dSTao Ma 	sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
1374e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1375e73a819dSTao Ma 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1376e73a819dSTao Ma 
1377e73a819dSTao Ma 	*split_cpos = cpos;
1378e73a819dSTao Ma 	return 0;
1379e73a819dSTao Ma }
1380e73a819dSTao Ma 
1381e73a819dSTao Ma static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1382e73a819dSTao Ma 					 struct ocfs2_caching_info *ci,
1383e73a819dSTao Ma 					 struct buffer_head *ref_root_bh,
1384e73a819dSTao Ma 					 struct buffer_head *ref_leaf_bh,
1385e73a819dSTao Ma 					 struct ocfs2_alloc_context *meta_ac)
1386e73a819dSTao Ma {
1387e73a819dSTao Ma 	int ret;
1388e73a819dSTao Ma 	u16 suballoc_bit_start;
1389e73a819dSTao Ma 	u32 num_got, new_cpos;
1390e73a819dSTao Ma 	u64 blkno;
1391e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1392e73a819dSTao Ma 	struct ocfs2_refcount_block *root_rb =
1393e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1394e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1395e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb;
1396e73a819dSTao Ma 	struct ocfs2_extent_tree ref_et;
1397e73a819dSTao Ma 
1398e73a819dSTao Ma 	BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL));
1399e73a819dSTao Ma 
1400e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1401e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1402e73a819dSTao Ma 	if (ret) {
1403e73a819dSTao Ma 		mlog_errno(ret);
1404e73a819dSTao Ma 		goto out;
1405e73a819dSTao Ma 	}
1406e73a819dSTao Ma 
1407e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1408e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1409e73a819dSTao Ma 	if (ret) {
1410e73a819dSTao Ma 		mlog_errno(ret);
1411e73a819dSTao Ma 		goto out;
1412e73a819dSTao Ma 	}
1413e73a819dSTao Ma 
1414e73a819dSTao Ma 	ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
1415e73a819dSTao Ma 				   &suballoc_bit_start, &num_got,
1416e73a819dSTao Ma 				   &blkno);
1417e73a819dSTao Ma 	if (ret) {
1418e73a819dSTao Ma 		mlog_errno(ret);
1419e73a819dSTao Ma 		goto out;
1420e73a819dSTao Ma 	}
1421e73a819dSTao Ma 
1422e73a819dSTao Ma 	new_bh = sb_getblk(sb, blkno);
1423e73a819dSTao Ma 	if (new_bh == NULL) {
1424e73a819dSTao Ma 		ret = -EIO;
1425e73a819dSTao Ma 		mlog_errno(ret);
1426e73a819dSTao Ma 		goto out;
1427e73a819dSTao Ma 	}
1428e73a819dSTao Ma 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1429e73a819dSTao Ma 
1430e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1431e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
1432e73a819dSTao Ma 	if (ret) {
1433e73a819dSTao Ma 		mlog_errno(ret);
1434e73a819dSTao Ma 		goto out;
1435e73a819dSTao Ma 	}
1436e73a819dSTao Ma 
1437e73a819dSTao Ma 	/* Initialize ocfs2_refcount_block. */
1438e73a819dSTao Ma 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1439e73a819dSTao Ma 	memset(new_rb, 0, sb->s_blocksize);
1440e73a819dSTao Ma 	strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
1441e73a819dSTao Ma 	new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num);
1442e73a819dSTao Ma 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1443e73a819dSTao Ma 	new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1444e73a819dSTao Ma 	new_rb->rf_blkno = cpu_to_le64(blkno);
1445e73a819dSTao Ma 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1446e73a819dSTao Ma 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1447e73a819dSTao Ma 	new_rb->rf_records.rl_count =
1448e73a819dSTao Ma 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
1449e73a819dSTao Ma 	new_rb->rf_generation = root_rb->rf_generation;
1450e73a819dSTao Ma 
1451e73a819dSTao Ma 	ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
1452e73a819dSTao Ma 	if (ret) {
1453e73a819dSTao Ma 		mlog_errno(ret);
1454e73a819dSTao Ma 		goto out;
1455e73a819dSTao Ma 	}
1456e73a819dSTao Ma 
1457e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1458e73a819dSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
1459e73a819dSTao Ma 
1460e73a819dSTao Ma 	ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh);
1461e73a819dSTao Ma 
1462e73a819dSTao Ma 	mlog(0, "insert new leaf block %llu at %u\n",
1463e73a819dSTao Ma 	     (unsigned long long)new_bh->b_blocknr, new_cpos);
1464e73a819dSTao Ma 
1465e73a819dSTao Ma 	/* Insert the new leaf block with the specific offset cpos. */
1466e73a819dSTao Ma 	ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr,
1467e73a819dSTao Ma 				  1, 0, meta_ac);
1468e73a819dSTao Ma 	if (ret)
1469e73a819dSTao Ma 		mlog_errno(ret);
1470e73a819dSTao Ma 
1471e73a819dSTao Ma out:
1472e73a819dSTao Ma 	brelse(new_bh);
1473e73a819dSTao Ma 	return ret;
1474e73a819dSTao Ma }
1475e73a819dSTao Ma 
1476e73a819dSTao Ma static int ocfs2_expand_refcount_tree(handle_t *handle,
1477e73a819dSTao Ma 				      struct ocfs2_caching_info *ci,
1478e73a819dSTao Ma 				      struct buffer_head *ref_root_bh,
1479e73a819dSTao Ma 				      struct buffer_head *ref_leaf_bh,
1480e73a819dSTao Ma 				      struct ocfs2_alloc_context *meta_ac)
1481e73a819dSTao Ma {
1482e73a819dSTao Ma 	int ret;
1483e73a819dSTao Ma 	struct buffer_head *expand_bh = NULL;
1484e73a819dSTao Ma 
1485e73a819dSTao Ma 	if (ref_root_bh == ref_leaf_bh) {
1486e73a819dSTao Ma 		/*
1487e73a819dSTao Ma 		 * the old root bh hasn't been expanded to a b-tree,
1488e73a819dSTao Ma 		 * so expand it first.
1489e73a819dSTao Ma 		 */
1490e73a819dSTao Ma 		ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
1491e73a819dSTao Ma 						   &expand_bh, meta_ac);
1492e73a819dSTao Ma 		if (ret) {
1493e73a819dSTao Ma 			mlog_errno(ret);
1494e73a819dSTao Ma 			goto out;
1495e73a819dSTao Ma 		}
1496e73a819dSTao Ma 	} else {
1497e73a819dSTao Ma 		expand_bh = ref_leaf_bh;
1498e73a819dSTao Ma 		get_bh(expand_bh);
1499e73a819dSTao Ma 	}
1500e73a819dSTao Ma 
1501e73a819dSTao Ma 
1502e73a819dSTao Ma 	/* Now add a new refcount block into the tree.*/
1503e73a819dSTao Ma 	ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
1504e73a819dSTao Ma 					    expand_bh, meta_ac);
1505e73a819dSTao Ma 	if (ret)
1506e73a819dSTao Ma 		mlog_errno(ret);
1507e73a819dSTao Ma out:
1508e73a819dSTao Ma 	brelse(expand_bh);
1509e73a819dSTao Ma 	return ret;
1510e73a819dSTao Ma }
1511e73a819dSTao Ma 
1512e73a819dSTao Ma /*
1513e73a819dSTao Ma  * Adjust the extent rec in b-tree representing ref_leaf_bh.
1514e73a819dSTao Ma  *
1515e73a819dSTao Ma  * Only called when we have inserted a new refcount rec at index 0
1516e73a819dSTao Ma  * which means ocfs2_extent_rec.e_cpos may need some change.
1517e73a819dSTao Ma  */
1518e73a819dSTao Ma static int ocfs2_adjust_refcount_rec(handle_t *handle,
1519e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1520e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
1521e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1522e73a819dSTao Ma 				     struct ocfs2_refcount_rec *rec)
1523e73a819dSTao Ma {
1524e73a819dSTao Ma 	int ret = 0, i;
1525e73a819dSTao Ma 	u32 new_cpos, old_cpos;
1526e73a819dSTao Ma 	struct ocfs2_path *path = NULL;
1527e73a819dSTao Ma 	struct ocfs2_extent_tree et;
1528e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1529e73a819dSTao Ma 		(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1530e73a819dSTao Ma 	struct ocfs2_extent_list *el;
1531e73a819dSTao Ma 
1532e73a819dSTao Ma 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL))
1533e73a819dSTao Ma 		goto out;
1534e73a819dSTao Ma 
1535e73a819dSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1536e73a819dSTao Ma 	old_cpos = le32_to_cpu(rb->rf_cpos);
1537e73a819dSTao Ma 	new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK;
1538e73a819dSTao Ma 	if (old_cpos <= new_cpos)
1539e73a819dSTao Ma 		goto out;
1540e73a819dSTao Ma 
1541e73a819dSTao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
1542e73a819dSTao Ma 
1543e73a819dSTao Ma 	path = ocfs2_new_path_from_et(&et);
1544e73a819dSTao Ma 	if (!path) {
1545e73a819dSTao Ma 		ret = -ENOMEM;
1546e73a819dSTao Ma 		mlog_errno(ret);
1547e73a819dSTao Ma 		goto out;
1548e73a819dSTao Ma 	}
1549e73a819dSTao Ma 
1550e73a819dSTao Ma 	ret = ocfs2_find_path(ci, path, old_cpos);
1551e73a819dSTao Ma 	if (ret) {
1552e73a819dSTao Ma 		mlog_errno(ret);
1553e73a819dSTao Ma 		goto out;
1554e73a819dSTao Ma 	}
1555e73a819dSTao Ma 
1556e73a819dSTao Ma 	/*
1557e73a819dSTao Ma 	 * 2 more credits, one for the leaf refcount block, one for
1558e73a819dSTao Ma 	 * the extent block contains the extent rec.
1559e73a819dSTao Ma 	 */
1560e73a819dSTao Ma 	ret = ocfs2_extend_trans(handle, handle->h_buffer_credits + 2);
1561e73a819dSTao Ma 	if (ret < 0) {
1562e73a819dSTao Ma 		mlog_errno(ret);
1563e73a819dSTao Ma 		goto out;
1564e73a819dSTao Ma 	}
1565e73a819dSTao Ma 
1566e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1567e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1568e73a819dSTao Ma 	if (ret < 0) {
1569e73a819dSTao Ma 		mlog_errno(ret);
1570e73a819dSTao Ma 		goto out;
1571e73a819dSTao Ma 	}
1572e73a819dSTao Ma 
1573e73a819dSTao Ma 	ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path),
1574e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1575e73a819dSTao Ma 	if (ret < 0) {
1576e73a819dSTao Ma 		mlog_errno(ret);
1577e73a819dSTao Ma 		goto out;
1578e73a819dSTao Ma 	}
1579e73a819dSTao Ma 
1580e73a819dSTao Ma 	/* change the leaf extent block first. */
1581e73a819dSTao Ma 	el = path_leaf_el(path);
1582e73a819dSTao Ma 
1583e73a819dSTao Ma 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++)
1584e73a819dSTao Ma 		if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos)
1585e73a819dSTao Ma 			break;
1586e73a819dSTao Ma 
1587e73a819dSTao Ma 	BUG_ON(i == le16_to_cpu(el->l_next_free_rec));
1588e73a819dSTao Ma 
1589e73a819dSTao Ma 	el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1590e73a819dSTao Ma 
1591e73a819dSTao Ma 	/* change the r_cpos in the leaf block. */
1592e73a819dSTao Ma 	rb->rf_cpos = cpu_to_le32(new_cpos);
1593e73a819dSTao Ma 
1594e73a819dSTao Ma 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
1595e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1596e73a819dSTao Ma 
1597e73a819dSTao Ma out:
1598e73a819dSTao Ma 	ocfs2_free_path(path);
1599e73a819dSTao Ma 	return ret;
1600e73a819dSTao Ma }
1601e73a819dSTao Ma 
1602e73a819dSTao Ma static int ocfs2_insert_refcount_rec(handle_t *handle,
1603e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1604e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
1605e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1606e73a819dSTao Ma 				     struct ocfs2_refcount_rec *rec,
1607e73a819dSTao Ma 				     int index,
1608e73a819dSTao Ma 				     struct ocfs2_alloc_context *meta_ac)
1609e73a819dSTao Ma {
1610e73a819dSTao Ma 	int ret;
1611e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1612e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1613e73a819dSTao Ma 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1614e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1615e73a819dSTao Ma 
1616e73a819dSTao Ma 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1617e73a819dSTao Ma 
1618e73a819dSTao Ma 	if (rf_list->rl_used == rf_list->rl_count) {
1619e73a819dSTao Ma 		u64 cpos = le64_to_cpu(rec->r_cpos);
1620e73a819dSTao Ma 		u32 len = le32_to_cpu(rec->r_clusters);
1621e73a819dSTao Ma 
1622e73a819dSTao Ma 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1623e73a819dSTao Ma 						 ref_leaf_bh, meta_ac);
1624e73a819dSTao Ma 		if (ret) {
1625e73a819dSTao Ma 			mlog_errno(ret);
1626e73a819dSTao Ma 			goto out;
1627e73a819dSTao Ma 		}
1628e73a819dSTao Ma 
1629e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1630e73a819dSTao Ma 					     cpos, len, NULL, &index,
1631e73a819dSTao Ma 					     &new_bh);
1632e73a819dSTao Ma 		if (ret) {
1633e73a819dSTao Ma 			mlog_errno(ret);
1634e73a819dSTao Ma 			goto out;
1635e73a819dSTao Ma 		}
1636e73a819dSTao Ma 
1637e73a819dSTao Ma 		ref_leaf_bh = new_bh;
1638e73a819dSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1639e73a819dSTao Ma 		rf_list = &rb->rf_records;
1640e73a819dSTao Ma 	}
1641e73a819dSTao Ma 
1642e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1643e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1644e73a819dSTao Ma 	if (ret) {
1645e73a819dSTao Ma 		mlog_errno(ret);
1646e73a819dSTao Ma 		goto out;
1647e73a819dSTao Ma 	}
1648e73a819dSTao Ma 
1649e73a819dSTao Ma 	if (index < le16_to_cpu(rf_list->rl_used))
1650e73a819dSTao Ma 		memmove(&rf_list->rl_recs[index + 1],
1651e73a819dSTao Ma 			&rf_list->rl_recs[index],
1652e73a819dSTao Ma 			(le16_to_cpu(rf_list->rl_used) - index) *
1653e73a819dSTao Ma 			 sizeof(struct ocfs2_refcount_rec));
1654e73a819dSTao Ma 
1655e73a819dSTao Ma 	mlog(0, "insert refcount record start %llu, len %u, count %u "
1656e73a819dSTao Ma 	     "to leaf block %llu at index %d\n",
1657e73a819dSTao Ma 	     (unsigned long long)le64_to_cpu(rec->r_cpos),
1658e73a819dSTao Ma 	     le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount),
1659e73a819dSTao Ma 	     (unsigned long long)ref_leaf_bh->b_blocknr, index);
1660e73a819dSTao Ma 
1661e73a819dSTao Ma 	rf_list->rl_recs[index] = *rec;
1662e73a819dSTao Ma 
1663e73a819dSTao Ma 	le16_add_cpu(&rf_list->rl_used, 1);
1664e73a819dSTao Ma 
1665e73a819dSTao Ma 	ocfs2_refcount_rec_merge(rb, index);
1666e73a819dSTao Ma 
1667e73a819dSTao Ma 	ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
1668e73a819dSTao Ma 	if (ret) {
1669e73a819dSTao Ma 		mlog_errno(ret);
1670e73a819dSTao Ma 		goto out;
1671e73a819dSTao Ma 	}
1672e73a819dSTao Ma 
1673e73a819dSTao Ma 	if (index == 0) {
1674e73a819dSTao Ma 		ret = ocfs2_adjust_refcount_rec(handle, ci,
1675e73a819dSTao Ma 						ref_root_bh,
1676e73a819dSTao Ma 						ref_leaf_bh, rec);
1677e73a819dSTao Ma 		if (ret)
1678e73a819dSTao Ma 			mlog_errno(ret);
1679e73a819dSTao Ma 	}
1680e73a819dSTao Ma out:
1681e73a819dSTao Ma 	brelse(new_bh);
1682e73a819dSTao Ma 	return ret;
1683e73a819dSTao Ma }
1684e73a819dSTao Ma 
1685e73a819dSTao Ma /*
1686e73a819dSTao Ma  * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1687e73a819dSTao Ma  * This is much simple than our b-tree code.
1688e73a819dSTao Ma  * split_rec is the new refcount rec we want to insert.
1689e73a819dSTao Ma  * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1690e73a819dSTao Ma  * increase refcount or decrease a refcount to non-zero).
1691e73a819dSTao Ma  * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1692e73a819dSTao Ma  * rec( in case we decrease a refcount to zero).
1693e73a819dSTao Ma  */
1694e73a819dSTao Ma static int ocfs2_split_refcount_rec(handle_t *handle,
1695e73a819dSTao Ma 				    struct ocfs2_caching_info *ci,
1696e73a819dSTao Ma 				    struct buffer_head *ref_root_bh,
1697e73a819dSTao Ma 				    struct buffer_head *ref_leaf_bh,
1698e73a819dSTao Ma 				    struct ocfs2_refcount_rec *split_rec,
1699e73a819dSTao Ma 				    int index,
1700e73a819dSTao Ma 				    struct ocfs2_alloc_context *meta_ac,
1701e73a819dSTao Ma 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
1702e73a819dSTao Ma {
1703e73a819dSTao Ma 	int ret, recs_need;
1704e73a819dSTao Ma 	u32 len;
1705e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1706e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1707e73a819dSTao Ma 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1708e73a819dSTao Ma 	struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index];
1709e73a819dSTao Ma 	struct ocfs2_refcount_rec *tail_rec = NULL;
1710e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1711e73a819dSTao Ma 
1712e73a819dSTao Ma 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1713e73a819dSTao Ma 
1714e73a819dSTao Ma 	mlog(0, "original r_pos %llu, cluster %u, split %llu, cluster %u\n",
1715e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos), le32_to_cpu(orig_rec->r_clusters),
1716e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos),
1717e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters));
1718e73a819dSTao Ma 
1719e73a819dSTao Ma 	/*
1720e73a819dSTao Ma 	 * If we just need to split the header or tail clusters,
1721e73a819dSTao Ma 	 * no more recs are needed, just split is OK.
1722e73a819dSTao Ma 	 * Otherwise we at least need one new recs.
1723e73a819dSTao Ma 	 */
1724e73a819dSTao Ma 	if (!split_rec->r_refcount &&
1725e73a819dSTao Ma 	    (split_rec->r_cpos == orig_rec->r_cpos ||
1726e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos) +
1727e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters) ==
1728e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1729e73a819dSTao Ma 		recs_need = 0;
1730e73a819dSTao Ma 	else
1731e73a819dSTao Ma 		recs_need = 1;
1732e73a819dSTao Ma 
1733e73a819dSTao Ma 	/*
1734e73a819dSTao Ma 	 * We need one more rec if we split in the middle and the new rec have
1735e73a819dSTao Ma 	 * some refcount in it.
1736e73a819dSTao Ma 	 */
1737e73a819dSTao Ma 	if (split_rec->r_refcount &&
1738e73a819dSTao Ma 	    (split_rec->r_cpos != orig_rec->r_cpos &&
1739e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos) +
1740e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters) !=
1741e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1742e73a819dSTao Ma 		recs_need++;
1743e73a819dSTao Ma 
1744e73a819dSTao Ma 	/* If the leaf block don't have enough record, expand it. */
1745e73a819dSTao Ma 	if (le16_to_cpu(rf_list->rl_used) + recs_need > rf_list->rl_count) {
1746e73a819dSTao Ma 		struct ocfs2_refcount_rec tmp_rec;
1747e73a819dSTao Ma 		u64 cpos = le64_to_cpu(orig_rec->r_cpos);
1748e73a819dSTao Ma 		len = le32_to_cpu(orig_rec->r_clusters);
1749e73a819dSTao Ma 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1750e73a819dSTao Ma 						 ref_leaf_bh, meta_ac);
1751e73a819dSTao Ma 		if (ret) {
1752e73a819dSTao Ma 			mlog_errno(ret);
1753e73a819dSTao Ma 			goto out;
1754e73a819dSTao Ma 		}
1755e73a819dSTao Ma 
1756e73a819dSTao Ma 		/*
1757e73a819dSTao Ma 		 * We have to re-get it since now cpos may be moved to
1758e73a819dSTao Ma 		 * another leaf block.
1759e73a819dSTao Ma 		 */
1760e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1761e73a819dSTao Ma 					     cpos, len, &tmp_rec, &index,
1762e73a819dSTao Ma 					     &new_bh);
1763e73a819dSTao Ma 		if (ret) {
1764e73a819dSTao Ma 			mlog_errno(ret);
1765e73a819dSTao Ma 			goto out;
1766e73a819dSTao Ma 		}
1767e73a819dSTao Ma 
1768e73a819dSTao Ma 		ref_leaf_bh = new_bh;
1769e73a819dSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1770e73a819dSTao Ma 		rf_list = &rb->rf_records;
1771e73a819dSTao Ma 		orig_rec = &rf_list->rl_recs[index];
1772e73a819dSTao Ma 	}
1773e73a819dSTao Ma 
1774e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1775e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1776e73a819dSTao Ma 	if (ret) {
1777e73a819dSTao Ma 		mlog_errno(ret);
1778e73a819dSTao Ma 		goto out;
1779e73a819dSTao Ma 	}
1780e73a819dSTao Ma 
1781e73a819dSTao Ma 	/*
1782e73a819dSTao Ma 	 * We have calculated out how many new records we need and store
1783e73a819dSTao Ma 	 * in recs_need, so spare enough space first by moving the records
1784e73a819dSTao Ma 	 * after "index" to the end.
1785e73a819dSTao Ma 	 */
1786e73a819dSTao Ma 	if (index != le16_to_cpu(rf_list->rl_used) - 1)
1787e73a819dSTao Ma 		memmove(&rf_list->rl_recs[index + 1 + recs_need],
1788e73a819dSTao Ma 			&rf_list->rl_recs[index + 1],
1789e73a819dSTao Ma 			(le16_to_cpu(rf_list->rl_used) - index - 1) *
1790e73a819dSTao Ma 			 sizeof(struct ocfs2_refcount_rec));
1791e73a819dSTao Ma 
1792e73a819dSTao Ma 	len = (le64_to_cpu(orig_rec->r_cpos) +
1793e73a819dSTao Ma 	      le32_to_cpu(orig_rec->r_clusters)) -
1794e73a819dSTao Ma 	      (le64_to_cpu(split_rec->r_cpos) +
1795e73a819dSTao Ma 	      le32_to_cpu(split_rec->r_clusters));
1796e73a819dSTao Ma 
1797e73a819dSTao Ma 	/*
1798e73a819dSTao Ma 	 * If we have "len", the we will split in the tail and move it
1799e73a819dSTao Ma 	 * to the end of the space we have just spared.
1800e73a819dSTao Ma 	 */
1801e73a819dSTao Ma 	if (len) {
1802e73a819dSTao Ma 		tail_rec = &rf_list->rl_recs[index + recs_need];
1803e73a819dSTao Ma 
1804e73a819dSTao Ma 		memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
1805e73a819dSTao Ma 		le64_add_cpu(&tail_rec->r_cpos,
1806e73a819dSTao Ma 			     le32_to_cpu(tail_rec->r_clusters) - len);
1807e73a819dSTao Ma 		tail_rec->r_clusters = le32_to_cpu(len);
1808e73a819dSTao Ma 	}
1809e73a819dSTao Ma 
1810e73a819dSTao Ma 	/*
1811e73a819dSTao Ma 	 * If the split pos isn't the same as the original one, we need to
1812e73a819dSTao Ma 	 * split in the head.
1813e73a819dSTao Ma 	 *
1814e73a819dSTao Ma 	 * Note: We have the chance that split_rec.r_refcount = 0,
1815e73a819dSTao Ma 	 * recs_need = 0 and len > 0, which means we just cut the head from
1816e73a819dSTao Ma 	 * the orig_rec and in that case we have done some modification in
1817e73a819dSTao Ma 	 * orig_rec above, so the check for r_cpos is faked.
1818e73a819dSTao Ma 	 */
1819e73a819dSTao Ma 	if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
1820e73a819dSTao Ma 		len = le64_to_cpu(split_rec->r_cpos) -
1821e73a819dSTao Ma 		      le64_to_cpu(orig_rec->r_cpos);
1822e73a819dSTao Ma 		orig_rec->r_clusters = cpu_to_le32(len);
1823e73a819dSTao Ma 		index++;
1824e73a819dSTao Ma 	}
1825e73a819dSTao Ma 
1826e73a819dSTao Ma 	le16_add_cpu(&rf_list->rl_used, recs_need);
1827e73a819dSTao Ma 
1828e73a819dSTao Ma 	if (split_rec->r_refcount) {
1829e73a819dSTao Ma 		rf_list->rl_recs[index] = *split_rec;
1830e73a819dSTao Ma 		mlog(0, "insert refcount record start %llu, len %u, count %u "
1831e73a819dSTao Ma 		     "to leaf block %llu at index %d\n",
1832e73a819dSTao Ma 		     (unsigned long long)le64_to_cpu(split_rec->r_cpos),
1833e73a819dSTao Ma 		     le32_to_cpu(split_rec->r_clusters),
1834e73a819dSTao Ma 		     le32_to_cpu(split_rec->r_refcount),
1835e73a819dSTao Ma 		     (unsigned long long)ref_leaf_bh->b_blocknr, index);
1836e73a819dSTao Ma 
1837e73a819dSTao Ma 		ocfs2_refcount_rec_merge(rb, index);
1838e73a819dSTao Ma 	}
1839e73a819dSTao Ma 
1840e73a819dSTao Ma 	ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
1841e73a819dSTao Ma 	if (ret)
1842e73a819dSTao Ma 		mlog_errno(ret);
1843e73a819dSTao Ma 
1844e73a819dSTao Ma out:
1845e73a819dSTao Ma 	brelse(new_bh);
1846e73a819dSTao Ma 	return ret;
1847e73a819dSTao Ma }
1848e73a819dSTao Ma 
1849e73a819dSTao Ma static int __ocfs2_increase_refcount(handle_t *handle,
1850e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1851e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
1852e73a819dSTao Ma 				     u64 cpos, u32 len,
1853e73a819dSTao Ma 				     struct ocfs2_alloc_context *meta_ac,
1854e73a819dSTao Ma 				     struct ocfs2_cached_dealloc_ctxt *dealloc)
1855e73a819dSTao Ma {
1856e73a819dSTao Ma 	int ret = 0, index;
1857e73a819dSTao Ma 	struct buffer_head *ref_leaf_bh = NULL;
1858e73a819dSTao Ma 	struct ocfs2_refcount_rec rec;
1859e73a819dSTao Ma 	unsigned int set_len = 0;
1860e73a819dSTao Ma 
1861e73a819dSTao Ma 	mlog(0, "Tree owner %llu, add refcount start %llu, len %u\n",
1862e73a819dSTao Ma 	     (unsigned long long)ocfs2_metadata_cache_owner(ci),
1863e73a819dSTao Ma 	     (unsigned long long)cpos, len);
1864e73a819dSTao Ma 
1865e73a819dSTao Ma 	while (len) {
1866e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1867e73a819dSTao Ma 					     cpos, len, &rec, &index,
1868e73a819dSTao Ma 					     &ref_leaf_bh);
1869e73a819dSTao Ma 		if (ret) {
1870e73a819dSTao Ma 			mlog_errno(ret);
1871e73a819dSTao Ma 			goto out;
1872e73a819dSTao Ma 		}
1873e73a819dSTao Ma 
1874e73a819dSTao Ma 		set_len = le32_to_cpu(rec.r_clusters);
1875e73a819dSTao Ma 
1876e73a819dSTao Ma 		/*
1877e73a819dSTao Ma 		 * Here we may meet with 3 situations:
1878e73a819dSTao Ma 		 *
1879e73a819dSTao Ma 		 * 1. If we find an already existing record, and the length
1880e73a819dSTao Ma 		 *    is the same, cool, we just need to increase the r_refcount
1881e73a819dSTao Ma 		 *    and it is OK.
1882e73a819dSTao Ma 		 * 2. If we find a hole, just insert it with r_refcount = 1.
1883e73a819dSTao Ma 		 * 3. If we are in the middle of one extent record, split
1884e73a819dSTao Ma 		 *    it.
1885e73a819dSTao Ma 		 */
1886e73a819dSTao Ma 		if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos &&
1887e73a819dSTao Ma 		    set_len <= len) {
1888e73a819dSTao Ma 			mlog(0, "increase refcount rec, start %llu, len %u, "
1889e73a819dSTao Ma 			     "count %u\n", (unsigned long long)cpos, set_len,
1890e73a819dSTao Ma 			     le32_to_cpu(rec.r_refcount));
1891e73a819dSTao Ma 			ret = ocfs2_change_refcount_rec(handle, ci,
1892e73a819dSTao Ma 							ref_leaf_bh, index, 1);
1893e73a819dSTao Ma 			if (ret) {
1894e73a819dSTao Ma 				mlog_errno(ret);
1895e73a819dSTao Ma 				goto out;
1896e73a819dSTao Ma 			}
1897e73a819dSTao Ma 		} else if (!rec.r_refcount) {
1898e73a819dSTao Ma 			rec.r_refcount = cpu_to_le32(1);
1899e73a819dSTao Ma 
1900e73a819dSTao Ma 			mlog(0, "insert refcount rec, start %llu, len %u\n",
1901e73a819dSTao Ma 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
1902e73a819dSTao Ma 			     set_len);
1903e73a819dSTao Ma 			ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
1904e73a819dSTao Ma 							ref_leaf_bh,
1905e73a819dSTao Ma 							&rec, index, meta_ac);
1906e73a819dSTao Ma 			if (ret) {
1907e73a819dSTao Ma 				mlog_errno(ret);
1908e73a819dSTao Ma 				goto out;
1909e73a819dSTao Ma 			}
1910e73a819dSTao Ma 		} else  {
1911e73a819dSTao Ma 			set_len = min((u64)(cpos + len),
1912e73a819dSTao Ma 				      le64_to_cpu(rec.r_cpos) + set_len) - cpos;
1913e73a819dSTao Ma 			rec.r_cpos = cpu_to_le64(cpos);
1914e73a819dSTao Ma 			rec.r_clusters = cpu_to_le32(set_len);
1915e73a819dSTao Ma 			le32_add_cpu(&rec.r_refcount, 1);
1916e73a819dSTao Ma 
1917e73a819dSTao Ma 			mlog(0, "split refcount rec, start %llu, "
1918e73a819dSTao Ma 			     "len %u, count %u\n",
1919e73a819dSTao Ma 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
1920e73a819dSTao Ma 			     set_len, le32_to_cpu(rec.r_refcount));
1921e73a819dSTao Ma 			ret = ocfs2_split_refcount_rec(handle, ci,
1922e73a819dSTao Ma 						       ref_root_bh, ref_leaf_bh,
1923e73a819dSTao Ma 						       &rec, index,
1924e73a819dSTao Ma 						       meta_ac, dealloc);
1925e73a819dSTao Ma 			if (ret) {
1926e73a819dSTao Ma 				mlog_errno(ret);
1927e73a819dSTao Ma 				goto out;
1928e73a819dSTao Ma 			}
1929e73a819dSTao Ma 		}
1930e73a819dSTao Ma 
1931e73a819dSTao Ma 		cpos += set_len;
1932e73a819dSTao Ma 		len -= set_len;
1933e73a819dSTao Ma 		brelse(ref_leaf_bh);
1934e73a819dSTao Ma 		ref_leaf_bh = NULL;
1935e73a819dSTao Ma 	}
1936e73a819dSTao Ma 
1937e73a819dSTao Ma out:
1938e73a819dSTao Ma 	brelse(ref_leaf_bh);
1939e73a819dSTao Ma 	return ret;
1940e73a819dSTao Ma }
19411823cb0bSTao Ma 
19421823cb0bSTao Ma static int ocfs2_remove_refcount_extent(handle_t *handle,
19431823cb0bSTao Ma 				struct ocfs2_caching_info *ci,
19441823cb0bSTao Ma 				struct buffer_head *ref_root_bh,
19451823cb0bSTao Ma 				struct buffer_head *ref_leaf_bh,
19461823cb0bSTao Ma 				struct ocfs2_alloc_context *meta_ac,
19471823cb0bSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
19481823cb0bSTao Ma {
19491823cb0bSTao Ma 	int ret;
19501823cb0bSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
19511823cb0bSTao Ma 	struct ocfs2_refcount_block *rb =
19521823cb0bSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
19531823cb0bSTao Ma 	struct ocfs2_extent_tree et;
19541823cb0bSTao Ma 
19551823cb0bSTao Ma 	BUG_ON(rb->rf_records.rl_used);
19561823cb0bSTao Ma 
19571823cb0bSTao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
19581823cb0bSTao Ma 	ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos),
19591823cb0bSTao Ma 				  1, meta_ac, dealloc);
19601823cb0bSTao Ma 	if (ret) {
19611823cb0bSTao Ma 		mlog_errno(ret);
19621823cb0bSTao Ma 		goto out;
19631823cb0bSTao Ma 	}
19641823cb0bSTao Ma 
19651823cb0bSTao Ma 	ocfs2_remove_from_cache(ci, ref_leaf_bh);
19661823cb0bSTao Ma 
19671823cb0bSTao Ma 	/*
19681823cb0bSTao Ma 	 * add the freed block to the dealloc so that it will be freed
19691823cb0bSTao Ma 	 * when we run dealloc.
19701823cb0bSTao Ma 	 */
19711823cb0bSTao Ma 	ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
19721823cb0bSTao Ma 					le16_to_cpu(rb->rf_suballoc_slot),
19731823cb0bSTao Ma 					le64_to_cpu(rb->rf_blkno),
19741823cb0bSTao Ma 					le16_to_cpu(rb->rf_suballoc_bit));
19751823cb0bSTao Ma 	if (ret) {
19761823cb0bSTao Ma 		mlog_errno(ret);
19771823cb0bSTao Ma 		goto out;
19781823cb0bSTao Ma 	}
19791823cb0bSTao Ma 
19801823cb0bSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
19811823cb0bSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
19821823cb0bSTao Ma 	if (ret) {
19831823cb0bSTao Ma 		mlog_errno(ret);
19841823cb0bSTao Ma 		goto out;
19851823cb0bSTao Ma 	}
19861823cb0bSTao Ma 
19871823cb0bSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
19881823cb0bSTao Ma 
19891823cb0bSTao Ma 	le32_add_cpu(&rb->rf_clusters, -1);
19901823cb0bSTao Ma 
19911823cb0bSTao Ma 	/*
19921823cb0bSTao Ma 	 * check whether we need to restore the root refcount block if
19931823cb0bSTao Ma 	 * there is no leaf extent block at atll.
19941823cb0bSTao Ma 	 */
19951823cb0bSTao Ma 	if (!rb->rf_list.l_next_free_rec) {
19961823cb0bSTao Ma 		BUG_ON(rb->rf_clusters);
19971823cb0bSTao Ma 
19981823cb0bSTao Ma 		mlog(0, "reset refcount tree root %llu to be a record block.\n",
19991823cb0bSTao Ma 		     (unsigned long long)ref_root_bh->b_blocknr);
20001823cb0bSTao Ma 
20011823cb0bSTao Ma 		rb->rf_flags = 0;
20021823cb0bSTao Ma 		rb->rf_parent = 0;
20031823cb0bSTao Ma 		rb->rf_cpos = 0;
20041823cb0bSTao Ma 		memset(&rb->rf_records, 0, sb->s_blocksize -
20051823cb0bSTao Ma 		       offsetof(struct ocfs2_refcount_block, rf_records));
20061823cb0bSTao Ma 		rb->rf_records.rl_count =
20071823cb0bSTao Ma 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
20081823cb0bSTao Ma 	}
20091823cb0bSTao Ma 
20101823cb0bSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
20111823cb0bSTao Ma 
20121823cb0bSTao Ma out:
20131823cb0bSTao Ma 	return ret;
20141823cb0bSTao Ma }
20151823cb0bSTao Ma 
20161823cb0bSTao Ma static int ocfs2_decrease_refcount_rec(handle_t *handle,
20171823cb0bSTao Ma 				struct ocfs2_caching_info *ci,
20181823cb0bSTao Ma 				struct buffer_head *ref_root_bh,
20191823cb0bSTao Ma 				struct buffer_head *ref_leaf_bh,
20201823cb0bSTao Ma 				int index, u64 cpos, unsigned int len,
20211823cb0bSTao Ma 				struct ocfs2_alloc_context *meta_ac,
20221823cb0bSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
20231823cb0bSTao Ma {
20241823cb0bSTao Ma 	int ret;
20251823cb0bSTao Ma 	struct ocfs2_refcount_block *rb =
20261823cb0bSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
20271823cb0bSTao Ma 	struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index];
20281823cb0bSTao Ma 
20291823cb0bSTao Ma 	BUG_ON(cpos < le64_to_cpu(rec->r_cpos));
20301823cb0bSTao Ma 	BUG_ON(cpos + len >
20311823cb0bSTao Ma 	       le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters));
20321823cb0bSTao Ma 
20331823cb0bSTao Ma 	if (cpos == le64_to_cpu(rec->r_cpos) &&
20341823cb0bSTao Ma 	    len == le32_to_cpu(rec->r_clusters))
20351823cb0bSTao Ma 		ret = ocfs2_change_refcount_rec(handle, ci,
20361823cb0bSTao Ma 						ref_leaf_bh, index, -1);
20371823cb0bSTao Ma 	else {
20381823cb0bSTao Ma 		struct ocfs2_refcount_rec split = *rec;
20391823cb0bSTao Ma 		split.r_cpos = cpu_to_le64(cpos);
20401823cb0bSTao Ma 		split.r_clusters = cpu_to_le32(len);
20411823cb0bSTao Ma 
20421823cb0bSTao Ma 		le32_add_cpu(&split.r_refcount, -1);
20431823cb0bSTao Ma 
20441823cb0bSTao Ma 		mlog(0, "split refcount rec, start %llu, "
20451823cb0bSTao Ma 		     "len %u, count %u, original start %llu, len %u\n",
20461823cb0bSTao Ma 		     (unsigned long long)le64_to_cpu(split.r_cpos),
20471823cb0bSTao Ma 		     len, le32_to_cpu(split.r_refcount),
20481823cb0bSTao Ma 		     (unsigned long long)le64_to_cpu(rec->r_cpos),
20491823cb0bSTao Ma 		     le32_to_cpu(rec->r_clusters));
20501823cb0bSTao Ma 		ret = ocfs2_split_refcount_rec(handle, ci,
20511823cb0bSTao Ma 					       ref_root_bh, ref_leaf_bh,
20521823cb0bSTao Ma 					       &split, index,
20531823cb0bSTao Ma 					       meta_ac, dealloc);
20541823cb0bSTao Ma 	}
20551823cb0bSTao Ma 
20561823cb0bSTao Ma 	if (ret) {
20571823cb0bSTao Ma 		mlog_errno(ret);
20581823cb0bSTao Ma 		goto out;
20591823cb0bSTao Ma 	}
20601823cb0bSTao Ma 
20611823cb0bSTao Ma 	/* Remove the leaf refcount block if it contains no refcount record. */
20621823cb0bSTao Ma 	if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) {
20631823cb0bSTao Ma 		ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh,
20641823cb0bSTao Ma 						   ref_leaf_bh, meta_ac,
20651823cb0bSTao Ma 						   dealloc);
20661823cb0bSTao Ma 		if (ret)
20671823cb0bSTao Ma 			mlog_errno(ret);
20681823cb0bSTao Ma 	}
20691823cb0bSTao Ma 
20701823cb0bSTao Ma out:
20711823cb0bSTao Ma 	return ret;
20721823cb0bSTao Ma }
20731823cb0bSTao Ma 
20741823cb0bSTao Ma static int __ocfs2_decrease_refcount(handle_t *handle,
20751823cb0bSTao Ma 				     struct ocfs2_caching_info *ci,
20761823cb0bSTao Ma 				     struct buffer_head *ref_root_bh,
20771823cb0bSTao Ma 				     u64 cpos, u32 len,
20781823cb0bSTao Ma 				     struct ocfs2_alloc_context *meta_ac,
20796ae23c55STao Ma 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
20806ae23c55STao Ma 				     int delete)
20811823cb0bSTao Ma {
20821823cb0bSTao Ma 	int ret = 0, index = 0;
20831823cb0bSTao Ma 	struct ocfs2_refcount_rec rec;
20841823cb0bSTao Ma 	unsigned int r_count = 0, r_len;
20851823cb0bSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
20861823cb0bSTao Ma 	struct buffer_head *ref_leaf_bh = NULL;
20871823cb0bSTao Ma 
20886ae23c55STao Ma 	mlog(0, "Tree owner %llu, decrease refcount start %llu, "
20896ae23c55STao Ma 	     "len %u, delete %u\n",
20901823cb0bSTao Ma 	     (unsigned long long)ocfs2_metadata_cache_owner(ci),
20916ae23c55STao Ma 	     (unsigned long long)cpos, len, delete);
20921823cb0bSTao Ma 
20931823cb0bSTao Ma 	while (len) {
20941823cb0bSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
20951823cb0bSTao Ma 					     cpos, len, &rec, &index,
20961823cb0bSTao Ma 					     &ref_leaf_bh);
20971823cb0bSTao Ma 		if (ret) {
20981823cb0bSTao Ma 			mlog_errno(ret);
20991823cb0bSTao Ma 			goto out;
21001823cb0bSTao Ma 		}
21011823cb0bSTao Ma 
21021823cb0bSTao Ma 		r_count = le32_to_cpu(rec.r_refcount);
21031823cb0bSTao Ma 		BUG_ON(r_count == 0);
21046ae23c55STao Ma 		if (!delete)
21056ae23c55STao Ma 			BUG_ON(r_count > 1);
21061823cb0bSTao Ma 
21071823cb0bSTao Ma 		r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) +
21081823cb0bSTao Ma 			      le32_to_cpu(rec.r_clusters)) - cpos;
21091823cb0bSTao Ma 
21101823cb0bSTao Ma 		ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
21111823cb0bSTao Ma 						  ref_leaf_bh, index,
21121823cb0bSTao Ma 						  cpos, r_len,
21131823cb0bSTao Ma 						  meta_ac, dealloc);
21141823cb0bSTao Ma 		if (ret) {
21151823cb0bSTao Ma 			mlog_errno(ret);
21161823cb0bSTao Ma 			goto out;
21171823cb0bSTao Ma 		}
21181823cb0bSTao Ma 
21196ae23c55STao Ma 		if (le32_to_cpu(rec.r_refcount) == 1 && delete) {
21201823cb0bSTao Ma 			ret = ocfs2_cache_cluster_dealloc(dealloc,
21211823cb0bSTao Ma 					  ocfs2_clusters_to_blocks(sb, cpos),
21221823cb0bSTao Ma 							  r_len);
21231823cb0bSTao Ma 			if (ret) {
21241823cb0bSTao Ma 				mlog_errno(ret);
21251823cb0bSTao Ma 				goto out;
21261823cb0bSTao Ma 			}
21271823cb0bSTao Ma 		}
21281823cb0bSTao Ma 
21291823cb0bSTao Ma 		cpos += r_len;
21301823cb0bSTao Ma 		len -= r_len;
21311823cb0bSTao Ma 		brelse(ref_leaf_bh);
21321823cb0bSTao Ma 		ref_leaf_bh = NULL;
21331823cb0bSTao Ma 	}
21341823cb0bSTao Ma 
21351823cb0bSTao Ma out:
21361823cb0bSTao Ma 	brelse(ref_leaf_bh);
21371823cb0bSTao Ma 	return ret;
21381823cb0bSTao Ma }
21391823cb0bSTao Ma 
21401823cb0bSTao Ma /* Caller must hold refcount tree lock. */
21411823cb0bSTao Ma int ocfs2_decrease_refcount(struct inode *inode,
21421823cb0bSTao Ma 			    handle_t *handle, u32 cpos, u32 len,
21431823cb0bSTao Ma 			    struct ocfs2_alloc_context *meta_ac,
21446ae23c55STao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc,
21456ae23c55STao Ma 			    int delete)
21461823cb0bSTao Ma {
21471823cb0bSTao Ma 	int ret;
21481823cb0bSTao Ma 	u64 ref_blkno;
21491823cb0bSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
21501823cb0bSTao Ma 	struct buffer_head *ref_root_bh = NULL;
21511823cb0bSTao Ma 	struct ocfs2_refcount_tree *tree;
21521823cb0bSTao Ma 
21531823cb0bSTao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
21541823cb0bSTao Ma 
21551823cb0bSTao Ma 	ret = ocfs2_get_refcount_block(inode, &ref_blkno);
21561823cb0bSTao Ma 	if (ret) {
21571823cb0bSTao Ma 		mlog_errno(ret);
21581823cb0bSTao Ma 		goto out;
21591823cb0bSTao Ma 	}
21601823cb0bSTao Ma 
21611823cb0bSTao Ma 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree);
21621823cb0bSTao Ma 	if (ret) {
21631823cb0bSTao Ma 		mlog_errno(ret);
21641823cb0bSTao Ma 		goto out;
21651823cb0bSTao Ma 	}
21661823cb0bSTao Ma 
21671823cb0bSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
21681823cb0bSTao Ma 					&ref_root_bh);
21691823cb0bSTao Ma 	if (ret) {
21701823cb0bSTao Ma 		mlog_errno(ret);
21711823cb0bSTao Ma 		goto out;
21721823cb0bSTao Ma 	}
21731823cb0bSTao Ma 
21741823cb0bSTao Ma 	ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh,
21756ae23c55STao Ma 					cpos, len, meta_ac, dealloc, delete);
21761823cb0bSTao Ma 	if (ret)
21771823cb0bSTao Ma 		mlog_errno(ret);
21781823cb0bSTao Ma out:
21791823cb0bSTao Ma 	brelse(ref_root_bh);
21801823cb0bSTao Ma 	return ret;
21811823cb0bSTao Ma }
21821aa75feaSTao Ma 
21831aa75feaSTao Ma /*
21841aa75feaSTao Ma  * Mark the already-existing extent at cpos as refcounted for len clusters.
21851aa75feaSTao Ma  * This adds the refcount extent flag.
21861aa75feaSTao Ma  *
21871aa75feaSTao Ma  * If the existing extent is larger than the request, initiate a
21881aa75feaSTao Ma  * split. An attempt will be made at merging with adjacent extents.
21891aa75feaSTao Ma  *
21901aa75feaSTao Ma  * The caller is responsible for passing down meta_ac if we'll need it.
21911aa75feaSTao Ma  */
21921aa75feaSTao Ma static int ocfs2_mark_extent_refcounted(struct inode *inode,
21931aa75feaSTao Ma 				struct ocfs2_extent_tree *et,
21941aa75feaSTao Ma 				handle_t *handle, u32 cpos,
21951aa75feaSTao Ma 				u32 len, u32 phys,
21961aa75feaSTao Ma 				struct ocfs2_alloc_context *meta_ac,
21971aa75feaSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
21981aa75feaSTao Ma {
21991aa75feaSTao Ma 	int ret;
22001aa75feaSTao Ma 
22011aa75feaSTao Ma 	mlog(0, "Inode %lu refcount tree cpos %u, len %u, phys cluster %u\n",
22021aa75feaSTao Ma 	     inode->i_ino, cpos, len, phys);
22031aa75feaSTao Ma 
22041aa75feaSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
22051aa75feaSTao Ma 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
22061aa75feaSTao Ma 			    "tree, but the feature bit is not set in the "
22071aa75feaSTao Ma 			    "super block.", inode->i_ino);
22081aa75feaSTao Ma 		ret = -EROFS;
22091aa75feaSTao Ma 		goto out;
22101aa75feaSTao Ma 	}
22111aa75feaSTao Ma 
22121aa75feaSTao Ma 	ret = ocfs2_change_extent_flag(handle, et, cpos,
22131aa75feaSTao Ma 				       len, phys, meta_ac, dealloc,
22141aa75feaSTao Ma 				       OCFS2_EXT_REFCOUNTED, 0);
22151aa75feaSTao Ma 	if (ret)
22161aa75feaSTao Ma 		mlog_errno(ret);
22171aa75feaSTao Ma 
22181aa75feaSTao Ma out:
22191aa75feaSTao Ma 	return ret;
22201aa75feaSTao Ma }
2221bcbbb24aSTao Ma 
2222bcbbb24aSTao Ma /*
2223bcbbb24aSTao Ma  * Given some contiguous physical clusters, calculate what we need
2224bcbbb24aSTao Ma  * for modifying their refcount.
2225bcbbb24aSTao Ma  */
2226bcbbb24aSTao Ma static int ocfs2_calc_refcount_meta_credits(struct super_block *sb,
2227bcbbb24aSTao Ma 					    struct ocfs2_caching_info *ci,
2228bcbbb24aSTao Ma 					    struct buffer_head *ref_root_bh,
2229bcbbb24aSTao Ma 					    u64 start_cpos,
2230bcbbb24aSTao Ma 					    u32 clusters,
2231bcbbb24aSTao Ma 					    int *meta_add,
2232bcbbb24aSTao Ma 					    int *credits)
2233bcbbb24aSTao Ma {
2234bcbbb24aSTao Ma 	int ret = 0, index, ref_blocks = 0, recs_add = 0;
2235bcbbb24aSTao Ma 	u64 cpos = start_cpos;
2236bcbbb24aSTao Ma 	struct ocfs2_refcount_block *rb;
2237bcbbb24aSTao Ma 	struct ocfs2_refcount_rec rec;
2238bcbbb24aSTao Ma 	struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL;
2239bcbbb24aSTao Ma 	u32 len;
2240bcbbb24aSTao Ma 
2241bcbbb24aSTao Ma 	mlog(0, "start_cpos %llu, clusters %u\n",
2242bcbbb24aSTao Ma 	     (unsigned long long)start_cpos, clusters);
2243bcbbb24aSTao Ma 	while (clusters) {
2244bcbbb24aSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2245bcbbb24aSTao Ma 					     cpos, clusters, &rec,
2246bcbbb24aSTao Ma 					     &index, &ref_leaf_bh);
2247bcbbb24aSTao Ma 		if (ret) {
2248bcbbb24aSTao Ma 			mlog_errno(ret);
2249bcbbb24aSTao Ma 			goto out;
2250bcbbb24aSTao Ma 		}
2251bcbbb24aSTao Ma 
2252bcbbb24aSTao Ma 		if (ref_leaf_bh != prev_bh) {
2253bcbbb24aSTao Ma 			/*
2254bcbbb24aSTao Ma 			 * Now we encounter a new leaf block, so calculate
2255bcbbb24aSTao Ma 			 * whether we need to extend the old leaf.
2256bcbbb24aSTao Ma 			 */
2257bcbbb24aSTao Ma 			if (prev_bh) {
2258bcbbb24aSTao Ma 				rb = (struct ocfs2_refcount_block *)
2259bcbbb24aSTao Ma 							prev_bh->b_data;
2260bcbbb24aSTao Ma 
2261bcbbb24aSTao Ma 				if (le64_to_cpu(rb->rf_records.rl_used) +
2262bcbbb24aSTao Ma 				    recs_add >
2263bcbbb24aSTao Ma 				    le16_to_cpu(rb->rf_records.rl_count))
2264bcbbb24aSTao Ma 					ref_blocks++;
2265bcbbb24aSTao Ma 			}
2266bcbbb24aSTao Ma 
2267bcbbb24aSTao Ma 			recs_add = 0;
2268bcbbb24aSTao Ma 			*credits += 1;
2269bcbbb24aSTao Ma 			brelse(prev_bh);
2270bcbbb24aSTao Ma 			prev_bh = ref_leaf_bh;
2271bcbbb24aSTao Ma 			get_bh(prev_bh);
2272bcbbb24aSTao Ma 		}
2273bcbbb24aSTao Ma 
2274bcbbb24aSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2275bcbbb24aSTao Ma 
2276bcbbb24aSTao Ma 		mlog(0, "recs_add %d,cpos %llu, clusters %u, rec->r_cpos %llu,"
2277bcbbb24aSTao Ma 		     "rec->r_clusters %u, rec->r_refcount %u, index %d\n",
2278bcbbb24aSTao Ma 		     recs_add, (unsigned long long)cpos, clusters,
2279bcbbb24aSTao Ma 		     (unsigned long long)le64_to_cpu(rec.r_cpos),
2280bcbbb24aSTao Ma 		     le32_to_cpu(rec.r_clusters),
2281bcbbb24aSTao Ma 		     le32_to_cpu(rec.r_refcount), index);
2282bcbbb24aSTao Ma 
2283bcbbb24aSTao Ma 		len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) +
2284bcbbb24aSTao Ma 			  le32_to_cpu(rec.r_clusters)) - cpos;
2285bcbbb24aSTao Ma 		/*
2286bcbbb24aSTao Ma 		 * If the refcount rec already exist, cool. We just need
2287bcbbb24aSTao Ma 		 * to check whether there is a split. Otherwise we just need
2288bcbbb24aSTao Ma 		 * to increase the refcount.
2289bcbbb24aSTao Ma 		 * If we will insert one, increases recs_add.
2290bcbbb24aSTao Ma 		 *
2291bcbbb24aSTao Ma 		 * We record all the records which will be inserted to the
2292bcbbb24aSTao Ma 		 * same refcount block, so that we can tell exactly whether
2293bcbbb24aSTao Ma 		 * we need a new refcount block or not.
2294bcbbb24aSTao Ma 		 */
2295bcbbb24aSTao Ma 		if (rec.r_refcount) {
2296bcbbb24aSTao Ma 			/* Check whether we need a split at the beginning. */
2297bcbbb24aSTao Ma 			if (cpos == start_cpos &&
2298bcbbb24aSTao Ma 			    cpos != le64_to_cpu(rec.r_cpos))
2299bcbbb24aSTao Ma 				recs_add++;
2300bcbbb24aSTao Ma 
2301bcbbb24aSTao Ma 			/* Check whether we need a split in the end. */
2302bcbbb24aSTao Ma 			if (cpos + clusters < le64_to_cpu(rec.r_cpos) +
2303bcbbb24aSTao Ma 			    le32_to_cpu(rec.r_clusters))
2304bcbbb24aSTao Ma 				recs_add++;
2305bcbbb24aSTao Ma 		} else
2306bcbbb24aSTao Ma 			recs_add++;
2307bcbbb24aSTao Ma 
2308bcbbb24aSTao Ma 		brelse(ref_leaf_bh);
2309bcbbb24aSTao Ma 		ref_leaf_bh = NULL;
2310bcbbb24aSTao Ma 		clusters -= len;
2311bcbbb24aSTao Ma 		cpos += len;
2312bcbbb24aSTao Ma 	}
2313bcbbb24aSTao Ma 
2314bcbbb24aSTao Ma 	if (prev_bh) {
2315bcbbb24aSTao Ma 		rb = (struct ocfs2_refcount_block *)prev_bh->b_data;
2316bcbbb24aSTao Ma 
2317bcbbb24aSTao Ma 		if (le64_to_cpu(rb->rf_records.rl_used) + recs_add >
2318bcbbb24aSTao Ma 		    le16_to_cpu(rb->rf_records.rl_count))
2319bcbbb24aSTao Ma 			ref_blocks++;
2320bcbbb24aSTao Ma 
2321bcbbb24aSTao Ma 		*credits += 1;
2322bcbbb24aSTao Ma 	}
2323bcbbb24aSTao Ma 
2324bcbbb24aSTao Ma 	if (!ref_blocks)
2325bcbbb24aSTao Ma 		goto out;
2326bcbbb24aSTao Ma 
2327bcbbb24aSTao Ma 	mlog(0, "we need ref_blocks %d\n", ref_blocks);
2328bcbbb24aSTao Ma 	*meta_add += ref_blocks;
2329bcbbb24aSTao Ma 	*credits += ref_blocks;
2330bcbbb24aSTao Ma 
2331bcbbb24aSTao Ma 	/*
2332bcbbb24aSTao Ma 	 * So we may need ref_blocks to insert into the tree.
2333bcbbb24aSTao Ma 	 * That also means we need to change the b-tree and add that number
2334bcbbb24aSTao Ma 	 * of records since we never merge them.
2335bcbbb24aSTao Ma 	 * We need one more block for expansion since the new created leaf
2336bcbbb24aSTao Ma 	 * block is also full and needs split.
2337bcbbb24aSTao Ma 	 */
2338bcbbb24aSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
2339bcbbb24aSTao Ma 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) {
2340bcbbb24aSTao Ma 		struct ocfs2_extent_tree et;
2341bcbbb24aSTao Ma 
2342bcbbb24aSTao Ma 		ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
2343bcbbb24aSTao Ma 		*meta_add += ocfs2_extend_meta_needed(et.et_root_el);
2344bcbbb24aSTao Ma 		*credits += ocfs2_calc_extend_credits(sb,
2345bcbbb24aSTao Ma 						      et.et_root_el,
2346bcbbb24aSTao Ma 						      ref_blocks);
2347bcbbb24aSTao Ma 	} else {
2348bcbbb24aSTao Ma 		*credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
2349bcbbb24aSTao Ma 		*meta_add += 1;
2350bcbbb24aSTao Ma 	}
2351bcbbb24aSTao Ma 
2352bcbbb24aSTao Ma out:
2353bcbbb24aSTao Ma 	brelse(ref_leaf_bh);
2354bcbbb24aSTao Ma 	brelse(prev_bh);
2355bcbbb24aSTao Ma 	return ret;
2356bcbbb24aSTao Ma }
2357bcbbb24aSTao Ma 
2358bcbbb24aSTao Ma /*
2359bcbbb24aSTao Ma  * For refcount tree, we will decrease some contiguous clusters
2360bcbbb24aSTao Ma  * refcount count, so just go through it to see how many blocks
2361bcbbb24aSTao Ma  * we gonna touch and whether we need to create new blocks.
2362bcbbb24aSTao Ma  *
2363bcbbb24aSTao Ma  * Normally the refcount blocks store these refcount should be
2364bcbbb24aSTao Ma  * continguous also, so that we can get the number easily.
2365bcbbb24aSTao Ma  * As for meta_ac, we will at most add split 2 refcount record and
2366bcbbb24aSTao Ma  * 2 more refcount block, so just check it in a rough way.
2367bcbbb24aSTao Ma  *
2368bcbbb24aSTao Ma  * Caller must hold refcount tree lock.
2369bcbbb24aSTao Ma  */
2370bcbbb24aSTao Ma int ocfs2_prepare_refcount_change_for_del(struct inode *inode,
2371bcbbb24aSTao Ma 					  struct buffer_head *di_bh,
2372bcbbb24aSTao Ma 					  u64 phys_blkno,
2373bcbbb24aSTao Ma 					  u32 clusters,
2374bcbbb24aSTao Ma 					  int *credits,
2375bcbbb24aSTao Ma 					  struct ocfs2_alloc_context **meta_ac)
2376bcbbb24aSTao Ma {
2377bcbbb24aSTao Ma 	int ret, ref_blocks = 0;
2378bcbbb24aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2379bcbbb24aSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2380bcbbb24aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
2381bcbbb24aSTao Ma 	struct ocfs2_refcount_tree *tree;
2382bcbbb24aSTao Ma 	u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno);
2383bcbbb24aSTao Ma 
2384bcbbb24aSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
2385bcbbb24aSTao Ma 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
2386bcbbb24aSTao Ma 			    "tree, but the feature bit is not set in the "
2387bcbbb24aSTao Ma 			    "super block.", inode->i_ino);
2388bcbbb24aSTao Ma 		ret = -EROFS;
2389bcbbb24aSTao Ma 		goto out;
2390bcbbb24aSTao Ma 	}
2391bcbbb24aSTao Ma 
2392bcbbb24aSTao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
2393bcbbb24aSTao Ma 
2394bcbbb24aSTao Ma 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb),
2395bcbbb24aSTao Ma 				      le64_to_cpu(di->i_refcount_loc), &tree);
2396bcbbb24aSTao Ma 	if (ret) {
2397bcbbb24aSTao Ma 		mlog_errno(ret);
2398bcbbb24aSTao Ma 		goto out;
2399bcbbb24aSTao Ma 	}
2400bcbbb24aSTao Ma 
2401bcbbb24aSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci,
2402bcbbb24aSTao Ma 					le64_to_cpu(di->i_refcount_loc),
2403bcbbb24aSTao Ma 					&ref_root_bh);
2404bcbbb24aSTao Ma 	if (ret) {
2405bcbbb24aSTao Ma 		mlog_errno(ret);
2406bcbbb24aSTao Ma 		goto out;
2407bcbbb24aSTao Ma 	}
2408bcbbb24aSTao Ma 
2409bcbbb24aSTao Ma 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
2410bcbbb24aSTao Ma 					       &tree->rf_ci,
2411bcbbb24aSTao Ma 					       ref_root_bh,
2412bcbbb24aSTao Ma 					       start_cpos, clusters,
2413bcbbb24aSTao Ma 					       &ref_blocks, credits);
2414bcbbb24aSTao Ma 	if (ret) {
2415bcbbb24aSTao Ma 		mlog_errno(ret);
2416bcbbb24aSTao Ma 		goto out;
2417bcbbb24aSTao Ma 	}
2418bcbbb24aSTao Ma 
2419bcbbb24aSTao Ma 	mlog(0, "reserve new metadata %d, credits = %d\n",
2420bcbbb24aSTao Ma 	     ref_blocks, *credits);
2421bcbbb24aSTao Ma 
2422bcbbb24aSTao Ma 	if (ref_blocks) {
2423bcbbb24aSTao Ma 		ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2424bcbbb24aSTao Ma 							ref_blocks, meta_ac);
2425bcbbb24aSTao Ma 		if (ret)
2426bcbbb24aSTao Ma 			mlog_errno(ret);
2427bcbbb24aSTao Ma 	}
2428bcbbb24aSTao Ma 
2429bcbbb24aSTao Ma out:
2430bcbbb24aSTao Ma 	brelse(ref_root_bh);
2431bcbbb24aSTao Ma 	return ret;
2432bcbbb24aSTao Ma }
24336f70fa51STao Ma 
24346f70fa51STao Ma #define	MAX_CONTIG_BYTES	1048576
24356f70fa51STao Ma 
24366f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb)
24376f70fa51STao Ma {
24386f70fa51STao Ma 	return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES);
24396f70fa51STao Ma }
24406f70fa51STao Ma 
24416f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb)
24426f70fa51STao Ma {
24436f70fa51STao Ma 	return ~(ocfs2_cow_contig_clusters(sb) - 1);
24446f70fa51STao Ma }
24456f70fa51STao Ma 
24466f70fa51STao Ma /*
24476f70fa51STao Ma  * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
24486f70fa51STao Ma  * find an offset (start + (n * contig_clusters)) that is closest to cpos
24496f70fa51STao Ma  * while still being less than or equal to it.
24506f70fa51STao Ma  *
24516f70fa51STao Ma  * The goal is to break the extent at a multiple of contig_clusters.
24526f70fa51STao Ma  */
24536f70fa51STao Ma static inline unsigned int ocfs2_cow_align_start(struct super_block *sb,
24546f70fa51STao Ma 						 unsigned int start,
24556f70fa51STao Ma 						 unsigned int cpos)
24566f70fa51STao Ma {
24576f70fa51STao Ma 	BUG_ON(start > cpos);
24586f70fa51STao Ma 
24596f70fa51STao Ma 	return start + ((cpos - start) & ocfs2_cow_contig_mask(sb));
24606f70fa51STao Ma }
24616f70fa51STao Ma 
24626f70fa51STao Ma /*
24636f70fa51STao Ma  * Given a cluster count of len, pad it out so that it is a multiple
24646f70fa51STao Ma  * of contig_clusters.
24656f70fa51STao Ma  */
24666f70fa51STao Ma static inline unsigned int ocfs2_cow_align_length(struct super_block *sb,
24676f70fa51STao Ma 						  unsigned int len)
24686f70fa51STao Ma {
24696f70fa51STao Ma 	unsigned int padded =
24706f70fa51STao Ma 		(len + (ocfs2_cow_contig_clusters(sb) - 1)) &
24716f70fa51STao Ma 		ocfs2_cow_contig_mask(sb);
24726f70fa51STao Ma 
24736f70fa51STao Ma 	/* Did we wrap? */
24746f70fa51STao Ma 	if (padded < len)
24756f70fa51STao Ma 		padded = UINT_MAX;
24766f70fa51STao Ma 
24776f70fa51STao Ma 	return padded;
24786f70fa51STao Ma }
24796f70fa51STao Ma 
24806f70fa51STao Ma /*
24816f70fa51STao Ma  * Calculate out the start and number of virtual clusters we need to to CoW.
24826f70fa51STao Ma  *
24836f70fa51STao Ma  * cpos is vitual start cluster position we want to do CoW in a
24846f70fa51STao Ma  * file and write_len is the cluster length.
248537f8a2bfSTao Ma  * max_cpos is the place where we want to stop CoW intentionally.
24866f70fa51STao Ma  *
24876f70fa51STao Ma  * Normal we will start CoW from the beginning of extent record cotaining cpos.
24886f70fa51STao Ma  * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
24896f70fa51STao Ma  * get good I/O from the resulting extent tree.
24906f70fa51STao Ma  */
24916f70fa51STao Ma static int ocfs2_refcount_cal_cow_clusters(struct inode *inode,
24926f70fa51STao Ma 					   struct buffer_head *di_bh,
24936f70fa51STao Ma 					   u32 cpos,
24946f70fa51STao Ma 					   u32 write_len,
249537f8a2bfSTao Ma 					   u32 max_cpos,
24966f70fa51STao Ma 					   u32 *cow_start,
24976f70fa51STao Ma 					   u32 *cow_len)
24986f70fa51STao Ma {
24996f70fa51STao Ma 	int ret = 0;
25006f70fa51STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
25016f70fa51STao Ma 	struct ocfs2_extent_list *el = &di->id2.i_list;
25026f70fa51STao Ma 	int tree_height = le16_to_cpu(el->l_tree_depth), i;
25036f70fa51STao Ma 	struct buffer_head *eb_bh = NULL;
25046f70fa51STao Ma 	struct ocfs2_extent_block *eb = NULL;
25056f70fa51STao Ma 	struct ocfs2_extent_rec *rec;
25066f70fa51STao Ma 	unsigned int want_clusters, rec_end = 0;
25076f70fa51STao Ma 	int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb);
25086f70fa51STao Ma 	int leaf_clusters;
25096f70fa51STao Ma 
251037f8a2bfSTao Ma 	BUG_ON(cpos + write_len > max_cpos);
251137f8a2bfSTao Ma 
25126f70fa51STao Ma 	if (tree_height > 0) {
25136f70fa51STao Ma 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh);
25146f70fa51STao Ma 		if (ret) {
25156f70fa51STao Ma 			mlog_errno(ret);
25166f70fa51STao Ma 			goto out;
25176f70fa51STao Ma 		}
25186f70fa51STao Ma 
25196f70fa51STao Ma 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
25206f70fa51STao Ma 		el = &eb->h_list;
25216f70fa51STao Ma 
25226f70fa51STao Ma 		if (el->l_tree_depth) {
25236f70fa51STao Ma 			ocfs2_error(inode->i_sb,
25246f70fa51STao Ma 				    "Inode %lu has non zero tree depth in "
25256f70fa51STao Ma 				    "leaf block %llu\n", inode->i_ino,
25266f70fa51STao Ma 				    (unsigned long long)eb_bh->b_blocknr);
25276f70fa51STao Ma 			ret = -EROFS;
25286f70fa51STao Ma 			goto out;
25296f70fa51STao Ma 		}
25306f70fa51STao Ma 	}
25316f70fa51STao Ma 
25326f70fa51STao Ma 	*cow_len = 0;
25336f70fa51STao Ma 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
25346f70fa51STao Ma 		rec = &el->l_recs[i];
25356f70fa51STao Ma 
25366f70fa51STao Ma 		if (ocfs2_is_empty_extent(rec)) {
25376f70fa51STao Ma 			mlog_bug_on_msg(i != 0, "Inode %lu has empty record in "
25386f70fa51STao Ma 					"index %d\n", inode->i_ino, i);
25396f70fa51STao Ma 			continue;
25406f70fa51STao Ma 		}
25416f70fa51STao Ma 
25426f70fa51STao Ma 		if (le32_to_cpu(rec->e_cpos) +
25436f70fa51STao Ma 		    le16_to_cpu(rec->e_leaf_clusters) <= cpos)
25446f70fa51STao Ma 			continue;
25456f70fa51STao Ma 
25466f70fa51STao Ma 		if (*cow_len == 0) {
25476f70fa51STao Ma 			/*
25486f70fa51STao Ma 			 * We should find a refcounted record in the
25496f70fa51STao Ma 			 * first pass.
25506f70fa51STao Ma 			 */
25516f70fa51STao Ma 			BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED));
25526f70fa51STao Ma 			*cow_start = le32_to_cpu(rec->e_cpos);
25536f70fa51STao Ma 		}
25546f70fa51STao Ma 
25556f70fa51STao Ma 		/*
255637f8a2bfSTao Ma 		 * If we encounter a hole, a non-refcounted record or
255737f8a2bfSTao Ma 		 * pass the max_cpos, stop the search.
25586f70fa51STao Ma 		 */
25596f70fa51STao Ma 		if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) ||
256037f8a2bfSTao Ma 		    (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) ||
256137f8a2bfSTao Ma 		    (max_cpos <= le32_to_cpu(rec->e_cpos)))
25626f70fa51STao Ma 			break;
25636f70fa51STao Ma 
25646f70fa51STao Ma 		leaf_clusters = le16_to_cpu(rec->e_leaf_clusters);
25656f70fa51STao Ma 		rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters;
256637f8a2bfSTao Ma 		if (rec_end > max_cpos) {
256737f8a2bfSTao Ma 			rec_end = max_cpos;
256837f8a2bfSTao Ma 			leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos);
256937f8a2bfSTao Ma 		}
25706f70fa51STao Ma 
25716f70fa51STao Ma 		/*
25726f70fa51STao Ma 		 * How many clusters do we actually need from
25736f70fa51STao Ma 		 * this extent?  First we see how many we actually
25746f70fa51STao Ma 		 * need to complete the write.  If that's smaller
25756f70fa51STao Ma 		 * than contig_clusters, we try for contig_clusters.
25766f70fa51STao Ma 		 */
25776f70fa51STao Ma 		if (!*cow_len)
25786f70fa51STao Ma 			want_clusters = write_len;
25796f70fa51STao Ma 		else
25806f70fa51STao Ma 			want_clusters = (cpos + write_len) -
25816f70fa51STao Ma 				(*cow_start + *cow_len);
25826f70fa51STao Ma 		if (want_clusters < contig_clusters)
25836f70fa51STao Ma 			want_clusters = contig_clusters;
25846f70fa51STao Ma 
25856f70fa51STao Ma 		/*
25866f70fa51STao Ma 		 * If the write does not cover the whole extent, we
25876f70fa51STao Ma 		 * need to calculate how we're going to split the extent.
25886f70fa51STao Ma 		 * We try to do it on contig_clusters boundaries.
25896f70fa51STao Ma 		 *
25906f70fa51STao Ma 		 * Any extent smaller than contig_clusters will be
25916f70fa51STao Ma 		 * CoWed in its entirety.
25926f70fa51STao Ma 		 */
25936f70fa51STao Ma 		if (leaf_clusters <= contig_clusters)
25946f70fa51STao Ma 			*cow_len += leaf_clusters;
25956f70fa51STao Ma 		else if (*cow_len || (*cow_start == cpos)) {
25966f70fa51STao Ma 			/*
25976f70fa51STao Ma 			 * This extent needs to be CoW'd from its
25986f70fa51STao Ma 			 * beginning, so all we have to do is compute
25996f70fa51STao Ma 			 * how many clusters to grab.  We align
26006f70fa51STao Ma 			 * want_clusters to the edge of contig_clusters
26016f70fa51STao Ma 			 * to get better I/O.
26026f70fa51STao Ma 			 */
26036f70fa51STao Ma 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
26046f70fa51STao Ma 							       want_clusters);
26056f70fa51STao Ma 
26066f70fa51STao Ma 			if (leaf_clusters < want_clusters)
26076f70fa51STao Ma 				*cow_len += leaf_clusters;
26086f70fa51STao Ma 			else
26096f70fa51STao Ma 				*cow_len += want_clusters;
26106f70fa51STao Ma 		} else if ((*cow_start + contig_clusters) >=
26116f70fa51STao Ma 			   (cpos + write_len)) {
26126f70fa51STao Ma 			/*
26136f70fa51STao Ma 			 * Breaking off contig_clusters at the front
26146f70fa51STao Ma 			 * of the extent will cover our write.  That's
26156f70fa51STao Ma 			 * easy.
26166f70fa51STao Ma 			 */
26176f70fa51STao Ma 			*cow_len = contig_clusters;
26186f70fa51STao Ma 		} else if ((rec_end - cpos) <= contig_clusters) {
26196f70fa51STao Ma 			/*
26206f70fa51STao Ma 			 * Breaking off contig_clusters at the tail of
26216f70fa51STao Ma 			 * this extent will cover cpos.
26226f70fa51STao Ma 			 */
26236f70fa51STao Ma 			*cow_start = rec_end - contig_clusters;
26246f70fa51STao Ma 			*cow_len = contig_clusters;
26256f70fa51STao Ma 		} else if ((rec_end - cpos) <= want_clusters) {
26266f70fa51STao Ma 			/*
26276f70fa51STao Ma 			 * While we can't fit the entire write in this
26286f70fa51STao Ma 			 * extent, we know that the write goes from cpos
26296f70fa51STao Ma 			 * to the end of the extent.  Break that off.
26306f70fa51STao Ma 			 * We try to break it at some multiple of
26316f70fa51STao Ma 			 * contig_clusters from the front of the extent.
26326f70fa51STao Ma 			 * Failing that (ie, cpos is within
26336f70fa51STao Ma 			 * contig_clusters of the front), we'll CoW the
26346f70fa51STao Ma 			 * entire extent.
26356f70fa51STao Ma 			 */
26366f70fa51STao Ma 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
26376f70fa51STao Ma 							   *cow_start, cpos);
26386f70fa51STao Ma 			*cow_len = rec_end - *cow_start;
26396f70fa51STao Ma 		} else {
26406f70fa51STao Ma 			/*
26416f70fa51STao Ma 			 * Ok, the entire write lives in the middle of
26426f70fa51STao Ma 			 * this extent.  Let's try to slice the extent up
26436f70fa51STao Ma 			 * nicely.  Optimally, our CoW region starts at
26446f70fa51STao Ma 			 * m*contig_clusters from the beginning of the
26456f70fa51STao Ma 			 * extent and goes for n*contig_clusters,
26466f70fa51STao Ma 			 * covering the entire write.
26476f70fa51STao Ma 			 */
26486f70fa51STao Ma 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
26496f70fa51STao Ma 							   *cow_start, cpos);
26506f70fa51STao Ma 
26516f70fa51STao Ma 			want_clusters = (cpos + write_len) - *cow_start;
26526f70fa51STao Ma 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
26536f70fa51STao Ma 							       want_clusters);
26546f70fa51STao Ma 			if (*cow_start + want_clusters <= rec_end)
26556f70fa51STao Ma 				*cow_len = want_clusters;
26566f70fa51STao Ma 			else
26576f70fa51STao Ma 				*cow_len = rec_end - *cow_start;
26586f70fa51STao Ma 		}
26596f70fa51STao Ma 
26606f70fa51STao Ma 		/* Have we covered our entire write yet? */
26616f70fa51STao Ma 		if ((*cow_start + *cow_len) >= (cpos + write_len))
26626f70fa51STao Ma 			break;
26636f70fa51STao Ma 
26646f70fa51STao Ma 		/*
26656f70fa51STao Ma 		 * If we reach the end of the extent block and don't get enough
26666f70fa51STao Ma 		 * clusters, continue with the next extent block if possible.
26676f70fa51STao Ma 		 */
26686f70fa51STao Ma 		if (i + 1 == le16_to_cpu(el->l_next_free_rec) &&
26696f70fa51STao Ma 		    eb && eb->h_next_leaf_blk) {
26706f70fa51STao Ma 			brelse(eb_bh);
26716f70fa51STao Ma 			eb_bh = NULL;
26726f70fa51STao Ma 
26736f70fa51STao Ma 			ret = ocfs2_read_extent_block(INODE_CACHE(inode),
26746f70fa51STao Ma 					       le64_to_cpu(eb->h_next_leaf_blk),
26756f70fa51STao Ma 					       &eb_bh);
26766f70fa51STao Ma 			if (ret) {
26776f70fa51STao Ma 				mlog_errno(ret);
26786f70fa51STao Ma 				goto out;
26796f70fa51STao Ma 			}
26806f70fa51STao Ma 
26816f70fa51STao Ma 			eb = (struct ocfs2_extent_block *) eb_bh->b_data;
26826f70fa51STao Ma 			el = &eb->h_list;
26836f70fa51STao Ma 			i = -1;
26846f70fa51STao Ma 		}
26856f70fa51STao Ma 	}
26866f70fa51STao Ma 
26876f70fa51STao Ma out:
26886f70fa51STao Ma 	brelse(eb_bh);
26896f70fa51STao Ma 	return ret;
26906f70fa51STao Ma }
26916f70fa51STao Ma 
26926f70fa51STao Ma /*
26936f70fa51STao Ma  * Prepare meta_ac, data_ac and calculate credits when we want to add some
26946f70fa51STao Ma  * num_clusters in data_tree "et" and change the refcount for the old
26956f70fa51STao Ma  * clusters(starting form p_cluster) in the refcount tree.
26966f70fa51STao Ma  *
26976f70fa51STao Ma  * Note:
26986f70fa51STao Ma  * 1. since we may split the old tree, so we at most will need num_clusters + 2
26996f70fa51STao Ma  *    more new leaf records.
27006f70fa51STao Ma  * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
27016f70fa51STao Ma  *    just give data_ac = NULL.
27026f70fa51STao Ma  */
27036f70fa51STao Ma static int ocfs2_lock_refcount_allocators(struct super_block *sb,
27046f70fa51STao Ma 					u32 p_cluster, u32 num_clusters,
27056f70fa51STao Ma 					struct ocfs2_extent_tree *et,
27066f70fa51STao Ma 					struct ocfs2_caching_info *ref_ci,
27076f70fa51STao Ma 					struct buffer_head *ref_root_bh,
27086f70fa51STao Ma 					struct ocfs2_alloc_context **meta_ac,
27096f70fa51STao Ma 					struct ocfs2_alloc_context **data_ac,
27106f70fa51STao Ma 					int *credits)
27116f70fa51STao Ma {
27126f70fa51STao Ma 	int ret = 0, meta_add = 0;
27136f70fa51STao Ma 	int num_free_extents = ocfs2_num_free_extents(OCFS2_SB(sb), et);
27146f70fa51STao Ma 
27156f70fa51STao Ma 	if (num_free_extents < 0) {
27166f70fa51STao Ma 		ret = num_free_extents;
27176f70fa51STao Ma 		mlog_errno(ret);
27186f70fa51STao Ma 		goto out;
27196f70fa51STao Ma 	}
27206f70fa51STao Ma 
27216f70fa51STao Ma 	if (num_free_extents < num_clusters + 2)
27226f70fa51STao Ma 		meta_add =
27236f70fa51STao Ma 			ocfs2_extend_meta_needed(et->et_root_el);
27246f70fa51STao Ma 
27256f70fa51STao Ma 	*credits += ocfs2_calc_extend_credits(sb, et->et_root_el,
27266f70fa51STao Ma 					      num_clusters + 2);
27276f70fa51STao Ma 
27286f70fa51STao Ma 	ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
27296f70fa51STao Ma 					       p_cluster, num_clusters,
27306f70fa51STao Ma 					       &meta_add, credits);
27316f70fa51STao Ma 	if (ret) {
27326f70fa51STao Ma 		mlog_errno(ret);
27336f70fa51STao Ma 		goto out;
27346f70fa51STao Ma 	}
27356f70fa51STao Ma 
27366f70fa51STao Ma 	mlog(0, "reserve new metadata %d, clusters %u, credits = %d\n",
27376f70fa51STao Ma 	     meta_add, num_clusters, *credits);
27386f70fa51STao Ma 	ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add,
27396f70fa51STao Ma 						meta_ac);
27406f70fa51STao Ma 	if (ret) {
27416f70fa51STao Ma 		mlog_errno(ret);
27426f70fa51STao Ma 		goto out;
27436f70fa51STao Ma 	}
27446f70fa51STao Ma 
27456f70fa51STao Ma 	if (data_ac) {
27466f70fa51STao Ma 		ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters,
27476f70fa51STao Ma 					     data_ac);
27486f70fa51STao Ma 		if (ret)
27496f70fa51STao Ma 			mlog_errno(ret);
27506f70fa51STao Ma 	}
27516f70fa51STao Ma 
27526f70fa51STao Ma out:
27536f70fa51STao Ma 	if (ret) {
27546f70fa51STao Ma 		if (*meta_ac) {
27556f70fa51STao Ma 			ocfs2_free_alloc_context(*meta_ac);
27566f70fa51STao Ma 			*meta_ac = NULL;
27576f70fa51STao Ma 		}
27586f70fa51STao Ma 	}
27596f70fa51STao Ma 
27606f70fa51STao Ma 	return ret;
27616f70fa51STao Ma }
27626f70fa51STao Ma 
27636f70fa51STao Ma static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh)
27646f70fa51STao Ma {
27656f70fa51STao Ma 	BUG_ON(buffer_dirty(bh));
27666f70fa51STao Ma 
27676f70fa51STao Ma 	clear_buffer_mapped(bh);
27686f70fa51STao Ma 
27696f70fa51STao Ma 	return 0;
27706f70fa51STao Ma }
27716f70fa51STao Ma 
27726f70fa51STao Ma static int ocfs2_duplicate_clusters(handle_t *handle,
27736f70fa51STao Ma 				    struct ocfs2_cow_context *context,
27746f70fa51STao Ma 				    u32 cpos, u32 old_cluster,
27756f70fa51STao Ma 				    u32 new_cluster, u32 new_len)
27766f70fa51STao Ma {
27776f70fa51STao Ma 	int ret = 0, partial;
27786f70fa51STao Ma 	struct ocfs2_caching_info *ci = context->di_et.et_ci;
27796f70fa51STao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
27806f70fa51STao Ma 	u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
27816f70fa51STao Ma 	struct page *page;
27826f70fa51STao Ma 	pgoff_t page_index;
27836f70fa51STao Ma 	unsigned int from, to;
27846f70fa51STao Ma 	loff_t offset, end, map_end;
27856f70fa51STao Ma 	struct address_space *mapping = context->inode->i_mapping;
27866f70fa51STao Ma 
27876f70fa51STao Ma 	mlog(0, "old_cluster %u, new %u, len %u at offset %u\n", old_cluster,
27886f70fa51STao Ma 	     new_cluster, new_len, cpos);
27896f70fa51STao Ma 
27906f70fa51STao Ma 	offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
27916f70fa51STao Ma 	end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits);
27926f70fa51STao Ma 
27936f70fa51STao Ma 	while (offset < end) {
27946f70fa51STao Ma 		page_index = offset >> PAGE_CACHE_SHIFT;
27956f70fa51STao Ma 		map_end = (page_index + 1) << PAGE_CACHE_SHIFT;
27966f70fa51STao Ma 		if (map_end > end)
27976f70fa51STao Ma 			map_end = end;
27986f70fa51STao Ma 
27996f70fa51STao Ma 		/* from, to is the offset within the page. */
28006f70fa51STao Ma 		from = offset & (PAGE_CACHE_SIZE - 1);
28016f70fa51STao Ma 		to = PAGE_CACHE_SIZE;
28026f70fa51STao Ma 		if (map_end & (PAGE_CACHE_SIZE - 1))
28036f70fa51STao Ma 			to = map_end & (PAGE_CACHE_SIZE - 1);
28046f70fa51STao Ma 
28056f70fa51STao Ma 		page = grab_cache_page(mapping, page_index);
28066f70fa51STao Ma 
28076f70fa51STao Ma 		/* This page can't be dirtied before we CoW it out. */
28086f70fa51STao Ma 		BUG_ON(PageDirty(page));
28096f70fa51STao Ma 
28106f70fa51STao Ma 		if (!PageUptodate(page)) {
28116f70fa51STao Ma 			ret = block_read_full_page(page, ocfs2_get_block);
28126f70fa51STao Ma 			if (ret) {
28136f70fa51STao Ma 				mlog_errno(ret);
28146f70fa51STao Ma 				goto unlock;
28156f70fa51STao Ma 			}
28166f70fa51STao Ma 			lock_page(page);
28176f70fa51STao Ma 		}
28186f70fa51STao Ma 
28196f70fa51STao Ma 		if (page_has_buffers(page)) {
28206f70fa51STao Ma 			ret = walk_page_buffers(handle, page_buffers(page),
28216f70fa51STao Ma 						from, to, &partial,
28226f70fa51STao Ma 						ocfs2_clear_cow_buffer);
28236f70fa51STao Ma 			if (ret) {
28246f70fa51STao Ma 				mlog_errno(ret);
28256f70fa51STao Ma 				goto unlock;
28266f70fa51STao Ma 			}
28276f70fa51STao Ma 		}
28286f70fa51STao Ma 
28296f70fa51STao Ma 		ocfs2_map_and_dirty_page(context->inode,
28306f70fa51STao Ma 					 handle, from, to,
28316f70fa51STao Ma 					 page, 0, &new_block);
28326f70fa51STao Ma 		mark_page_accessed(page);
28336f70fa51STao Ma unlock:
28346f70fa51STao Ma 		unlock_page(page);
28356f70fa51STao Ma 		page_cache_release(page);
28366f70fa51STao Ma 		page = NULL;
28376f70fa51STao Ma 		offset = map_end;
28386f70fa51STao Ma 		if (ret)
28396f70fa51STao Ma 			break;
28406f70fa51STao Ma 	}
28416f70fa51STao Ma 
28426f70fa51STao Ma 	return ret;
28436f70fa51STao Ma }
28446f70fa51STao Ma 
28456f70fa51STao Ma static int ocfs2_clear_ext_refcount(handle_t *handle,
28466f70fa51STao Ma 				    struct ocfs2_extent_tree *et,
28476f70fa51STao Ma 				    u32 cpos, u32 p_cluster, u32 len,
28486f70fa51STao Ma 				    unsigned int ext_flags,
28496f70fa51STao Ma 				    struct ocfs2_alloc_context *meta_ac,
28506f70fa51STao Ma 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
28516f70fa51STao Ma {
28526f70fa51STao Ma 	int ret, index;
28536f70fa51STao Ma 	struct ocfs2_extent_rec replace_rec;
28546f70fa51STao Ma 	struct ocfs2_path *path = NULL;
28556f70fa51STao Ma 	struct ocfs2_extent_list *el;
28566f70fa51STao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
28576f70fa51STao Ma 	u64 ino = ocfs2_metadata_cache_owner(et->et_ci);
28586f70fa51STao Ma 
28596f70fa51STao Ma 	mlog(0, "inode %llu cpos %u, len %u, p_cluster %u, ext_flags %u\n",
28606f70fa51STao Ma 	     (unsigned long long)ino, cpos, len, p_cluster, ext_flags);
28616f70fa51STao Ma 
28626f70fa51STao Ma 	memset(&replace_rec, 0, sizeof(replace_rec));
28636f70fa51STao Ma 	replace_rec.e_cpos = cpu_to_le32(cpos);
28646f70fa51STao Ma 	replace_rec.e_leaf_clusters = cpu_to_le16(len);
28656f70fa51STao Ma 	replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb,
28666f70fa51STao Ma 								   p_cluster));
28676f70fa51STao Ma 	replace_rec.e_flags = ext_flags;
28686f70fa51STao Ma 	replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED;
28696f70fa51STao Ma 
28706f70fa51STao Ma 	path = ocfs2_new_path_from_et(et);
28716f70fa51STao Ma 	if (!path) {
28726f70fa51STao Ma 		ret = -ENOMEM;
28736f70fa51STao Ma 		mlog_errno(ret);
28746f70fa51STao Ma 		goto out;
28756f70fa51STao Ma 	}
28766f70fa51STao Ma 
28776f70fa51STao Ma 	ret = ocfs2_find_path(et->et_ci, path, cpos);
28786f70fa51STao Ma 	if (ret) {
28796f70fa51STao Ma 		mlog_errno(ret);
28806f70fa51STao Ma 		goto out;
28816f70fa51STao Ma 	}
28826f70fa51STao Ma 
28836f70fa51STao Ma 	el = path_leaf_el(path);
28846f70fa51STao Ma 
28856f70fa51STao Ma 	index = ocfs2_search_extent_list(el, cpos);
28866f70fa51STao Ma 	if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
28876f70fa51STao Ma 		ocfs2_error(sb,
28886f70fa51STao Ma 			    "Inode %llu has an extent at cpos %u which can no "
28896f70fa51STao Ma 			    "longer be found.\n",
28906f70fa51STao Ma 			    (unsigned long long)ino, cpos);
28916f70fa51STao Ma 		ret = -EROFS;
28926f70fa51STao Ma 		goto out;
28936f70fa51STao Ma 	}
28946f70fa51STao Ma 
28956f70fa51STao Ma 	ret = ocfs2_split_extent(handle, et, path, index,
28966f70fa51STao Ma 				 &replace_rec, meta_ac, dealloc);
28976f70fa51STao Ma 	if (ret)
28986f70fa51STao Ma 		mlog_errno(ret);
28996f70fa51STao Ma 
29006f70fa51STao Ma out:
29016f70fa51STao Ma 	ocfs2_free_path(path);
29026f70fa51STao Ma 	return ret;
29036f70fa51STao Ma }
29046f70fa51STao Ma 
29056f70fa51STao Ma static int ocfs2_replace_clusters(handle_t *handle,
29066f70fa51STao Ma 				  struct ocfs2_cow_context *context,
29076f70fa51STao Ma 				  u32 cpos, u32 old,
29086f70fa51STao Ma 				  u32 new, u32 len,
29096f70fa51STao Ma 				  unsigned int ext_flags)
29106f70fa51STao Ma {
29116f70fa51STao Ma 	int ret;
29126f70fa51STao Ma 	struct ocfs2_caching_info *ci = context->di_et.et_ci;
29136f70fa51STao Ma 	u64 ino = ocfs2_metadata_cache_owner(ci);
29146f70fa51STao Ma 
29156f70fa51STao Ma 	mlog(0, "inode %llu, cpos %u, old %u, new %u, len %u, ext_flags %u\n",
29166f70fa51STao Ma 	     (unsigned long long)ino, cpos, old, new, len, ext_flags);
29176f70fa51STao Ma 
29186f70fa51STao Ma 	/*If the old clusters is unwritten, no need to duplicate. */
29196f70fa51STao Ma 	if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) {
29206f70fa51STao Ma 		ret = ocfs2_duplicate_clusters(handle, context, cpos,
29216f70fa51STao Ma 					       old, new, len);
29226f70fa51STao Ma 		if (ret) {
29236f70fa51STao Ma 			mlog_errno(ret);
29246f70fa51STao Ma 			goto out;
29256f70fa51STao Ma 		}
29266f70fa51STao Ma 	}
29276f70fa51STao Ma 
29286f70fa51STao Ma 	ret = ocfs2_clear_ext_refcount(handle, &context->di_et,
29296f70fa51STao Ma 				       cpos, new, len, ext_flags,
29306f70fa51STao Ma 				       context->meta_ac, &context->dealloc);
29316f70fa51STao Ma 	if (ret)
29326f70fa51STao Ma 		mlog_errno(ret);
29336f70fa51STao Ma out:
29346f70fa51STao Ma 	return ret;
29356f70fa51STao Ma }
29366f70fa51STao Ma 
29376f70fa51STao Ma static int ocfs2_cow_sync_writeback(struct super_block *sb,
29386f70fa51STao Ma 				    struct ocfs2_cow_context *context,
29396f70fa51STao Ma 				    u32 cpos, u32 num_clusters)
29406f70fa51STao Ma {
29416f70fa51STao Ma 	int ret = 0;
29426f70fa51STao Ma 	loff_t offset, end, map_end;
29436f70fa51STao Ma 	pgoff_t page_index;
29446f70fa51STao Ma 	struct page *page;
29456f70fa51STao Ma 
29466f70fa51STao Ma 	if (ocfs2_should_order_data(context->inode))
29476f70fa51STao Ma 		return 0;
29486f70fa51STao Ma 
29496f70fa51STao Ma 	offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
29506f70fa51STao Ma 	end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits);
29516f70fa51STao Ma 
29526f70fa51STao Ma 	ret = filemap_fdatawrite_range(context->inode->i_mapping,
29536f70fa51STao Ma 				       offset, end - 1);
29546f70fa51STao Ma 	if (ret < 0) {
29556f70fa51STao Ma 		mlog_errno(ret);
29566f70fa51STao Ma 		return ret;
29576f70fa51STao Ma 	}
29586f70fa51STao Ma 
29596f70fa51STao Ma 	while (offset < end) {
29606f70fa51STao Ma 		page_index = offset >> PAGE_CACHE_SHIFT;
29616f70fa51STao Ma 		map_end = (page_index + 1) << PAGE_CACHE_SHIFT;
29626f70fa51STao Ma 		if (map_end > end)
29636f70fa51STao Ma 			map_end = end;
29646f70fa51STao Ma 
29656f70fa51STao Ma 		page = grab_cache_page(context->inode->i_mapping, page_index);
29666f70fa51STao Ma 		BUG_ON(!page);
29676f70fa51STao Ma 
29686f70fa51STao Ma 		wait_on_page_writeback(page);
29696f70fa51STao Ma 		if (PageError(page)) {
29706f70fa51STao Ma 			ret = -EIO;
29716f70fa51STao Ma 			mlog_errno(ret);
29726f70fa51STao Ma 		} else
29736f70fa51STao Ma 			mark_page_accessed(page);
29746f70fa51STao Ma 
29756f70fa51STao Ma 		unlock_page(page);
29766f70fa51STao Ma 		page_cache_release(page);
29776f70fa51STao Ma 		page = NULL;
29786f70fa51STao Ma 		offset = map_end;
29796f70fa51STao Ma 		if (ret)
29806f70fa51STao Ma 			break;
29816f70fa51STao Ma 	}
29826f70fa51STao Ma 
29836f70fa51STao Ma 	return ret;
29846f70fa51STao Ma }
29856f70fa51STao Ma 
29866f70fa51STao Ma static int ocfs2_make_clusters_writable(struct super_block *sb,
29876f70fa51STao Ma 					struct ocfs2_cow_context *context,
29886f70fa51STao Ma 					u32 cpos, u32 p_cluster,
29896f70fa51STao Ma 					u32 num_clusters, unsigned int e_flags)
29906f70fa51STao Ma {
29916ae23c55STao Ma 	int ret, delete, index, credits =  0;
29926f70fa51STao Ma 	u32 new_bit, new_len;
29936ae23c55STao Ma 	unsigned int set_len;
29946f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(sb);
29956f70fa51STao Ma 	handle_t *handle;
29966ae23c55STao Ma 	struct buffer_head *ref_leaf_bh = NULL;
29976ae23c55STao Ma 	struct ocfs2_refcount_rec rec;
29986ae23c55STao Ma 
29996ae23c55STao Ma 	mlog(0, "cpos %u, p_cluster %u, num_clusters %u, e_flags %u\n",
30006ae23c55STao Ma 	     cpos, p_cluster, num_clusters, e_flags);
30016f70fa51STao Ma 
30026f70fa51STao Ma 	ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
30036f70fa51STao Ma 					     &context->di_et,
30046f70fa51STao Ma 					     context->ref_ci,
30056f70fa51STao Ma 					     context->ref_root_bh,
30066f70fa51STao Ma 					     &context->meta_ac,
30076f70fa51STao Ma 					     &context->data_ac, &credits);
30086f70fa51STao Ma 	if (ret) {
30096f70fa51STao Ma 		mlog_errno(ret);
30106f70fa51STao Ma 		return ret;
30116f70fa51STao Ma 	}
30126f70fa51STao Ma 
30136f70fa51STao Ma 	handle = ocfs2_start_trans(osb, credits);
30146f70fa51STao Ma 	if (IS_ERR(handle)) {
30156f70fa51STao Ma 		ret = PTR_ERR(handle);
30166f70fa51STao Ma 		mlog_errno(ret);
30176f70fa51STao Ma 		goto out;
30186f70fa51STao Ma 	}
30196f70fa51STao Ma 
30206f70fa51STao Ma 	while (num_clusters) {
30216ae23c55STao Ma 		ret = ocfs2_get_refcount_rec(context->ref_ci,
30226ae23c55STao Ma 					     context->ref_root_bh,
30236ae23c55STao Ma 					     p_cluster, num_clusters,
30246ae23c55STao Ma 					     &rec, &index, &ref_leaf_bh);
30256ae23c55STao Ma 		if (ret) {
30266ae23c55STao Ma 			mlog_errno(ret);
30276ae23c55STao Ma 			goto out_commit;
30286ae23c55STao Ma 		}
30296ae23c55STao Ma 
30306ae23c55STao Ma 		BUG_ON(!rec.r_refcount);
30316ae23c55STao Ma 		set_len = min((u64)p_cluster + num_clusters,
30326ae23c55STao Ma 			      le64_to_cpu(rec.r_cpos) +
30336ae23c55STao Ma 			      le32_to_cpu(rec.r_clusters)) - p_cluster;
30346ae23c55STao Ma 
30356ae23c55STao Ma 		/*
30366ae23c55STao Ma 		 * There are many different situation here.
30376ae23c55STao Ma 		 * 1. If refcount == 1, remove the flag and don't COW.
30386ae23c55STao Ma 		 * 2. If refcount > 1, allocate clusters.
30396ae23c55STao Ma 		 *    Here we may not allocate r_len once at a time, so continue
30406ae23c55STao Ma 		 *    until we reach num_clusters.
30416ae23c55STao Ma 		 */
30426ae23c55STao Ma 		if (le32_to_cpu(rec.r_refcount) == 1) {
30436ae23c55STao Ma 			delete = 0;
30446ae23c55STao Ma 			ret = ocfs2_clear_ext_refcount(handle, &context->di_et,
30456ae23c55STao Ma 						       cpos, p_cluster,
30466ae23c55STao Ma 						       set_len, e_flags,
30476ae23c55STao Ma 						       context->meta_ac,
30486ae23c55STao Ma 						       &context->dealloc);
30496ae23c55STao Ma 			if (ret) {
30506ae23c55STao Ma 				mlog_errno(ret);
30516ae23c55STao Ma 				goto out_commit;
30526ae23c55STao Ma 			}
30536ae23c55STao Ma 		} else {
30546ae23c55STao Ma 			delete = 1;
30556ae23c55STao Ma 
30566ae23c55STao Ma 			ret = __ocfs2_claim_clusters(osb, handle,
30576ae23c55STao Ma 						     context->data_ac,
30586ae23c55STao Ma 						     1, set_len,
30596f70fa51STao Ma 						     &new_bit, &new_len);
30606f70fa51STao Ma 			if (ret) {
30616f70fa51STao Ma 				mlog_errno(ret);
30626f70fa51STao Ma 				goto out_commit;
30636f70fa51STao Ma 			}
30646f70fa51STao Ma 
30656f70fa51STao Ma 			ret = ocfs2_replace_clusters(handle, context,
30666f70fa51STao Ma 						     cpos, p_cluster, new_bit,
30676f70fa51STao Ma 						     new_len, e_flags);
30686f70fa51STao Ma 			if (ret) {
30696f70fa51STao Ma 				mlog_errno(ret);
30706f70fa51STao Ma 				goto out_commit;
30716f70fa51STao Ma 			}
30726ae23c55STao Ma 			set_len = new_len;
30736f70fa51STao Ma 		}
30746f70fa51STao Ma 
30756f70fa51STao Ma 		ret = __ocfs2_decrease_refcount(handle, context->ref_ci,
30766f70fa51STao Ma 						context->ref_root_bh,
30776ae23c55STao Ma 						p_cluster, set_len,
30786f70fa51STao Ma 						context->meta_ac,
30796ae23c55STao Ma 						&context->dealloc, delete);
30806f70fa51STao Ma 		if (ret) {
30816f70fa51STao Ma 			mlog_errno(ret);
30826f70fa51STao Ma 			goto out_commit;
30836f70fa51STao Ma 		}
30846f70fa51STao Ma 
30856ae23c55STao Ma 		cpos += set_len;
30866ae23c55STao Ma 		p_cluster += set_len;
30876ae23c55STao Ma 		num_clusters -= set_len;
30886ae23c55STao Ma 		brelse(ref_leaf_bh);
30896ae23c55STao Ma 		ref_leaf_bh = NULL;
30906ae23c55STao Ma 	}
30916ae23c55STao Ma 
30926f70fa51STao Ma 	/*
30936f70fa51STao Ma 	 * Here we should write the new page out first if we are
30946f70fa51STao Ma 	 * in write-back mode.
30956f70fa51STao Ma 	 */
30966f70fa51STao Ma 	ret = ocfs2_cow_sync_writeback(sb, context, cpos, num_clusters);
30976f70fa51STao Ma 	if (ret)
30986f70fa51STao Ma 		mlog_errno(ret);
30996f70fa51STao Ma 
31006f70fa51STao Ma out_commit:
31016f70fa51STao Ma 	ocfs2_commit_trans(osb, handle);
31026f70fa51STao Ma 
31036f70fa51STao Ma out:
31046f70fa51STao Ma 	if (context->data_ac) {
31056f70fa51STao Ma 		ocfs2_free_alloc_context(context->data_ac);
31066f70fa51STao Ma 		context->data_ac = NULL;
31076f70fa51STao Ma 	}
31086f70fa51STao Ma 	if (context->meta_ac) {
31096f70fa51STao Ma 		ocfs2_free_alloc_context(context->meta_ac);
31106f70fa51STao Ma 		context->meta_ac = NULL;
31116f70fa51STao Ma 	}
31126ae23c55STao Ma 	brelse(ref_leaf_bh);
31136f70fa51STao Ma 
31146f70fa51STao Ma 	return ret;
31156f70fa51STao Ma }
31166f70fa51STao Ma 
31176f70fa51STao Ma static int ocfs2_replace_cow(struct inode *inode,
31186f70fa51STao Ma 			     struct buffer_head *di_bh,
31196f70fa51STao Ma 			     struct buffer_head *ref_root_bh,
31206f70fa51STao Ma 			     struct ocfs2_caching_info *ref_ci,
31216f70fa51STao Ma 			     u32 cow_start, u32 cow_len)
31226f70fa51STao Ma {
31236f70fa51STao Ma 	int ret = 0;
31246f70fa51STao Ma 	u32 p_cluster, num_clusters, start = cow_start;
31256f70fa51STao Ma 	unsigned int ext_flags;
31266f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
31276f70fa51STao Ma 	struct ocfs2_cow_context *context;
31286f70fa51STao Ma 
31296f70fa51STao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
31306f70fa51STao Ma 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
31316f70fa51STao Ma 			    "tree, but the feature bit is not set in the "
31326f70fa51STao Ma 			    "super block.", inode->i_ino);
31336f70fa51STao Ma 		return -EROFS;
31346f70fa51STao Ma 	}
31356f70fa51STao Ma 
31366f70fa51STao Ma 	context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
31376f70fa51STao Ma 	if (!context) {
31386f70fa51STao Ma 		ret = -ENOMEM;
31396f70fa51STao Ma 		mlog_errno(ret);
31406f70fa51STao Ma 		return ret;
31416f70fa51STao Ma 	}
31426f70fa51STao Ma 
31436f70fa51STao Ma 	context->inode = inode;
31446f70fa51STao Ma 	context->cow_start = cow_start;
31456f70fa51STao Ma 	context->cow_len = cow_len;
31466f70fa51STao Ma 	context->ref_ci = ref_ci;
31476f70fa51STao Ma 	context->ref_root_bh = ref_root_bh;
31486f70fa51STao Ma 
31496f70fa51STao Ma 	ocfs2_init_dealloc_ctxt(&context->dealloc);
31506f70fa51STao Ma 	ocfs2_init_dinode_extent_tree(&context->di_et,
31516f70fa51STao Ma 				      INODE_CACHE(inode), di_bh);
31526f70fa51STao Ma 
31536f70fa51STao Ma 	while (cow_len) {
31546f70fa51STao Ma 		ret = ocfs2_get_clusters(inode, cow_start, &p_cluster,
31556f70fa51STao Ma 					 &num_clusters, &ext_flags);
31566f70fa51STao Ma 		if (ret) {
31576f70fa51STao Ma 			mlog_errno(ret);
31586f70fa51STao Ma 			break;
31596f70fa51STao Ma 		}
31606f70fa51STao Ma 
31616f70fa51STao Ma 		BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED));
31626f70fa51STao Ma 
31636f70fa51STao Ma 		if (cow_len < num_clusters)
31646f70fa51STao Ma 			num_clusters = cow_len;
31656f70fa51STao Ma 
31666f70fa51STao Ma 		ret = ocfs2_make_clusters_writable(inode->i_sb, context,
31676f70fa51STao Ma 						   cow_start, p_cluster,
31686f70fa51STao Ma 						   num_clusters, ext_flags);
31696f70fa51STao Ma 		if (ret) {
31706f70fa51STao Ma 			mlog_errno(ret);
31716f70fa51STao Ma 			break;
31726f70fa51STao Ma 		}
31736f70fa51STao Ma 
31746f70fa51STao Ma 		cow_len -= num_clusters;
31756f70fa51STao Ma 		cow_start += num_clusters;
31766f70fa51STao Ma 	}
31776f70fa51STao Ma 
31786f70fa51STao Ma 
31796f70fa51STao Ma 	/*
31806f70fa51STao Ma 	 * truncate the extent map here since no matter whether we meet with
31816f70fa51STao Ma 	 * any error during the action, we shouldn't trust cached extent map
31826f70fa51STao Ma 	 * any more.
31836f70fa51STao Ma 	 */
31846f70fa51STao Ma 	ocfs2_extent_map_trunc(inode, start);
31856f70fa51STao Ma 
31866f70fa51STao Ma 	if (ocfs2_dealloc_has_cluster(&context->dealloc)) {
31876f70fa51STao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
31886f70fa51STao Ma 		ocfs2_run_deallocs(osb, &context->dealloc);
31896f70fa51STao Ma 	}
31906f70fa51STao Ma 
31916f70fa51STao Ma 	kfree(context);
31926f70fa51STao Ma 	return ret;
31936f70fa51STao Ma }
31946f70fa51STao Ma 
31956f70fa51STao Ma /*
319637f8a2bfSTao Ma  * Starting at cpos, try to CoW write_len clusters.  Don't CoW
319737f8a2bfSTao Ma  * past max_cpos.  This will stop when it runs into a hole or an
319837f8a2bfSTao Ma  * unrefcounted extent.
31996f70fa51STao Ma  */
32006f70fa51STao Ma static int ocfs2_refcount_cow_hunk(struct inode *inode,
32016f70fa51STao Ma 				   struct buffer_head *di_bh,
320237f8a2bfSTao Ma 				   u32 cpos, u32 write_len, u32 max_cpos)
32036f70fa51STao Ma {
32046f70fa51STao Ma 	int ret;
32056f70fa51STao Ma 	u32 cow_start = 0, cow_len = 0;
32066f70fa51STao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
32076f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
32086f70fa51STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
32096f70fa51STao Ma 	struct buffer_head *ref_root_bh = NULL;
32106f70fa51STao Ma 	struct ocfs2_refcount_tree *ref_tree;
32116f70fa51STao Ma 
32126f70fa51STao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
32136f70fa51STao Ma 
321437f8a2bfSTao Ma 	ret = ocfs2_refcount_cal_cow_clusters(inode, di_bh,
321537f8a2bfSTao Ma 					      cpos, write_len, max_cpos,
32166f70fa51STao Ma 					      &cow_start, &cow_len);
32176f70fa51STao Ma 	if (ret) {
32186f70fa51STao Ma 		mlog_errno(ret);
32196f70fa51STao Ma 		goto out;
32206f70fa51STao Ma 	}
322137f8a2bfSTao Ma 
32226f70fa51STao Ma 	mlog(0, "CoW inode %lu, cpos %u, write_len %u, cow_start %u, "
32236f70fa51STao Ma 	     "cow_len %u\n", inode->i_ino,
32246f70fa51STao Ma 	     cpos, write_len, cow_start, cow_len);
32256f70fa51STao Ma 
32266f70fa51STao Ma 	BUG_ON(cow_len == 0);
32276f70fa51STao Ma 
32286f70fa51STao Ma 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
32296f70fa51STao Ma 				       1, &ref_tree, &ref_root_bh);
32306f70fa51STao Ma 	if (ret) {
32316f70fa51STao Ma 		mlog_errno(ret);
32326f70fa51STao Ma 		goto out;
32336f70fa51STao Ma 	}
32346f70fa51STao Ma 
32356f70fa51STao Ma 	ret = ocfs2_replace_cow(inode, di_bh, ref_root_bh, &ref_tree->rf_ci,
32366f70fa51STao Ma 				cow_start, cow_len);
32376f70fa51STao Ma 	if (ret)
32386f70fa51STao Ma 		mlog_errno(ret);
32396f70fa51STao Ma 
32406f70fa51STao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
32416f70fa51STao Ma 	brelse(ref_root_bh);
32426f70fa51STao Ma out:
32436f70fa51STao Ma 	return ret;
32446f70fa51STao Ma }
32456f70fa51STao Ma 
32466f70fa51STao Ma /*
32476f70fa51STao Ma  * CoW any and all clusters between cpos and cpos+write_len.
324837f8a2bfSTao Ma  * Don't CoW past max_cpos.  If this returns successfully, all
324937f8a2bfSTao Ma  * clusters between cpos and cpos+write_len are safe to modify.
32506f70fa51STao Ma  */
32516f70fa51STao Ma int ocfs2_refcount_cow(struct inode *inode,
32526f70fa51STao Ma 		       struct buffer_head *di_bh,
325337f8a2bfSTao Ma 		       u32 cpos, u32 write_len, u32 max_cpos)
32546f70fa51STao Ma {
32556f70fa51STao Ma 	int ret = 0;
32566f70fa51STao Ma 	u32 p_cluster, num_clusters;
32576f70fa51STao Ma 	unsigned int ext_flags;
32586f70fa51STao Ma 
32596f70fa51STao Ma 	while (write_len) {
32606f70fa51STao Ma 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
32616f70fa51STao Ma 					 &num_clusters, &ext_flags);
32626f70fa51STao Ma 		if (ret) {
32636f70fa51STao Ma 			mlog_errno(ret);
32646f70fa51STao Ma 			break;
32656f70fa51STao Ma 		}
32666f70fa51STao Ma 
32676f70fa51STao Ma 		if (write_len < num_clusters)
32686f70fa51STao Ma 			num_clusters = write_len;
32696f70fa51STao Ma 
32706f70fa51STao Ma 		if (ext_flags & OCFS2_EXT_REFCOUNTED) {
32716f70fa51STao Ma 			ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos,
327237f8a2bfSTao Ma 						      num_clusters, max_cpos);
32736f70fa51STao Ma 			if (ret) {
32746f70fa51STao Ma 				mlog_errno(ret);
32756f70fa51STao Ma 				break;
32766f70fa51STao Ma 			}
32776f70fa51STao Ma 		}
32786f70fa51STao Ma 
32796f70fa51STao Ma 		write_len -= num_clusters;
32806f70fa51STao Ma 		cpos += num_clusters;
32816f70fa51STao Ma 	}
32826f70fa51STao Ma 
32836f70fa51STao Ma 	return ret;
32846f70fa51STao Ma }
3285110a045aSTao Ma 
3286110a045aSTao Ma /*
3287110a045aSTao Ma  * Insert a new extent into refcount tree and mark a extent rec
3288110a045aSTao Ma  * as refcounted in the dinode tree.
3289110a045aSTao Ma  */
3290110a045aSTao Ma int ocfs2_add_refcount_flag(struct inode *inode,
3291110a045aSTao Ma 			    struct ocfs2_extent_tree *data_et,
3292110a045aSTao Ma 			    struct ocfs2_caching_info *ref_ci,
3293110a045aSTao Ma 			    struct buffer_head *ref_root_bh,
3294110a045aSTao Ma 			    u32 cpos, u32 p_cluster, u32 num_clusters,
3295110a045aSTao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc)
3296110a045aSTao Ma {
3297110a045aSTao Ma 	int ret;
3298110a045aSTao Ma 	handle_t *handle;
3299110a045aSTao Ma 	int credits = 1, ref_blocks = 0;
3300110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3301110a045aSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
3302110a045aSTao Ma 
3303110a045aSTao Ma 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
3304110a045aSTao Ma 					       ref_ci, ref_root_bh,
3305110a045aSTao Ma 					       p_cluster, num_clusters,
3306110a045aSTao Ma 					       &ref_blocks, &credits);
3307110a045aSTao Ma 	if (ret) {
3308110a045aSTao Ma 		mlog_errno(ret);
3309110a045aSTao Ma 		goto out;
3310110a045aSTao Ma 	}
3311110a045aSTao Ma 
3312110a045aSTao Ma 	mlog(0, "reserve new metadata %d, credits = %d\n",
3313110a045aSTao Ma 	     ref_blocks, credits);
3314110a045aSTao Ma 
3315110a045aSTao Ma 	if (ref_blocks) {
3316110a045aSTao Ma 		ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
3317110a045aSTao Ma 							ref_blocks, &meta_ac);
3318110a045aSTao Ma 		if (ret) {
3319110a045aSTao Ma 			mlog_errno(ret);
3320110a045aSTao Ma 			goto out;
3321110a045aSTao Ma 		}
3322110a045aSTao Ma 	}
3323110a045aSTao Ma 
3324110a045aSTao Ma 	handle = ocfs2_start_trans(osb, credits);
3325110a045aSTao Ma 	if (IS_ERR(handle)) {
3326110a045aSTao Ma 		ret = PTR_ERR(handle);
3327110a045aSTao Ma 		mlog_errno(ret);
3328110a045aSTao Ma 		goto out;
3329110a045aSTao Ma 	}
3330110a045aSTao Ma 
3331110a045aSTao Ma 	ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
3332110a045aSTao Ma 					   cpos, num_clusters, p_cluster,
3333110a045aSTao Ma 					   meta_ac, dealloc);
3334110a045aSTao Ma 	if (ret) {
3335110a045aSTao Ma 		mlog_errno(ret);
3336110a045aSTao Ma 		goto out_commit;
3337110a045aSTao Ma 	}
3338110a045aSTao Ma 
3339110a045aSTao Ma 	ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3340110a045aSTao Ma 					p_cluster, num_clusters,
3341110a045aSTao Ma 					meta_ac, dealloc);
3342110a045aSTao Ma 	if (ret)
3343110a045aSTao Ma 		mlog_errno(ret);
3344110a045aSTao Ma 
3345110a045aSTao Ma out_commit:
3346110a045aSTao Ma 	ocfs2_commit_trans(osb, handle);
3347110a045aSTao Ma out:
3348110a045aSTao Ma 	if (meta_ac)
3349110a045aSTao Ma 		ocfs2_free_alloc_context(meta_ac);
3350110a045aSTao Ma 	return ret;
3351110a045aSTao Ma }
3352110a045aSTao Ma 
3353110a045aSTao Ma static int ocfs2_attach_refcount_tree(struct inode *inode,
3354110a045aSTao Ma 				      struct buffer_head *di_bh)
3355110a045aSTao Ma {
3356110a045aSTao Ma 	int ret;
3357110a045aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
3358110a045aSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
3359110a045aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3360110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3361110a045aSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
3362110a045aSTao Ma 	unsigned int ext_flags;
3363110a045aSTao Ma 	loff_t size;
3364110a045aSTao Ma 	u32 cpos, num_clusters, clusters, p_cluster;
3365110a045aSTao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
3366110a045aSTao Ma 	struct ocfs2_extent_tree di_et;
3367110a045aSTao Ma 
3368110a045aSTao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
3369110a045aSTao Ma 
3370110a045aSTao Ma 	if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)) {
3371110a045aSTao Ma 		ret = ocfs2_create_refcount_tree(inode, di_bh);
3372110a045aSTao Ma 		if (ret) {
3373110a045aSTao Ma 			mlog_errno(ret);
3374110a045aSTao Ma 			goto out;
3375110a045aSTao Ma 		}
3376110a045aSTao Ma 	}
3377110a045aSTao Ma 
3378110a045aSTao Ma 	BUG_ON(!di->i_refcount_loc);
3379110a045aSTao Ma 	ret = ocfs2_lock_refcount_tree(osb,
3380110a045aSTao Ma 				       le64_to_cpu(di->i_refcount_loc), 1,
3381110a045aSTao Ma 				       &ref_tree, &ref_root_bh);
3382110a045aSTao Ma 	if (ret) {
3383110a045aSTao Ma 		mlog_errno(ret);
3384110a045aSTao Ma 		goto out;
3385110a045aSTao Ma 	}
3386110a045aSTao Ma 
3387110a045aSTao Ma 	ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh);
3388110a045aSTao Ma 
3389110a045aSTao Ma 	size = i_size_read(inode);
3390110a045aSTao Ma 	clusters = ocfs2_clusters_for_bytes(inode->i_sb, size);
3391110a045aSTao Ma 
3392110a045aSTao Ma 	cpos = 0;
3393110a045aSTao Ma 	while (cpos < clusters) {
3394110a045aSTao Ma 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3395110a045aSTao Ma 					 &num_clusters, &ext_flags);
3396110a045aSTao Ma 
3397110a045aSTao Ma 		if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) {
3398110a045aSTao Ma 			ret = ocfs2_add_refcount_flag(inode, &di_et,
3399110a045aSTao Ma 						      &ref_tree->rf_ci,
3400110a045aSTao Ma 						      ref_root_bh, cpos,
3401110a045aSTao Ma 						      p_cluster, num_clusters,
3402110a045aSTao Ma 						      &dealloc);
3403110a045aSTao Ma 			if (ret) {
3404110a045aSTao Ma 				mlog_errno(ret);
3405110a045aSTao Ma 				break;
3406110a045aSTao Ma 			}
3407110a045aSTao Ma 		}
3408110a045aSTao Ma 		cpos += num_clusters;
3409110a045aSTao Ma 	}
3410110a045aSTao Ma 
3411110a045aSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3412110a045aSTao Ma 	brelse(ref_root_bh);
3413110a045aSTao Ma 
3414110a045aSTao Ma 	if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
3415110a045aSTao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
3416110a045aSTao Ma 		ocfs2_run_deallocs(osb, &dealloc);
3417110a045aSTao Ma 	}
3418110a045aSTao Ma out:
3419110a045aSTao Ma 	/*
3420110a045aSTao Ma 	 * Empty the extent map so that we may get the right extent
3421110a045aSTao Ma 	 * record from the disk.
3422110a045aSTao Ma 	 */
3423110a045aSTao Ma 	ocfs2_extent_map_trunc(inode, 0);
3424110a045aSTao Ma 
3425110a045aSTao Ma 	return ret;
3426110a045aSTao Ma }
3427110a045aSTao Ma 
3428110a045aSTao Ma static int ocfs2_add_refcounted_extent(struct inode *inode,
3429110a045aSTao Ma 				   struct ocfs2_extent_tree *et,
3430110a045aSTao Ma 				   struct ocfs2_caching_info *ref_ci,
3431110a045aSTao Ma 				   struct buffer_head *ref_root_bh,
3432110a045aSTao Ma 				   u32 cpos, u32 p_cluster, u32 num_clusters,
3433110a045aSTao Ma 				   unsigned int ext_flags,
3434110a045aSTao Ma 				   struct ocfs2_cached_dealloc_ctxt *dealloc)
3435110a045aSTao Ma {
3436110a045aSTao Ma 	int ret;
3437110a045aSTao Ma 	handle_t *handle;
3438110a045aSTao Ma 	int credits = 0;
3439110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3440110a045aSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
3441110a045aSTao Ma 
3442110a045aSTao Ma 	ret = ocfs2_lock_refcount_allocators(inode->i_sb,
3443110a045aSTao Ma 					     p_cluster, num_clusters,
3444110a045aSTao Ma 					     et, ref_ci,
3445110a045aSTao Ma 					     ref_root_bh, &meta_ac,
3446110a045aSTao Ma 					     NULL, &credits);
3447110a045aSTao Ma 	if (ret) {
3448110a045aSTao Ma 		mlog_errno(ret);
3449110a045aSTao Ma 		goto out;
3450110a045aSTao Ma 	}
3451110a045aSTao Ma 
3452110a045aSTao Ma 	handle = ocfs2_start_trans(osb, credits);
3453110a045aSTao Ma 	if (IS_ERR(handle)) {
3454110a045aSTao Ma 		ret = PTR_ERR(handle);
3455110a045aSTao Ma 		mlog_errno(ret);
3456110a045aSTao Ma 		goto out;
3457110a045aSTao Ma 	}
3458110a045aSTao Ma 
3459110a045aSTao Ma 	ret = ocfs2_insert_extent(handle, et, cpos,
3460110a045aSTao Ma 			cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb,
3461110a045aSTao Ma 							     p_cluster)),
3462110a045aSTao Ma 			num_clusters, ext_flags, meta_ac);
3463110a045aSTao Ma 	if (ret) {
3464110a045aSTao Ma 		mlog_errno(ret);
3465110a045aSTao Ma 		goto out_commit;
3466110a045aSTao Ma 	}
3467110a045aSTao Ma 
3468110a045aSTao Ma 	ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3469110a045aSTao Ma 					p_cluster, num_clusters,
3470110a045aSTao Ma 					meta_ac, dealloc);
3471110a045aSTao Ma 	if (ret)
3472110a045aSTao Ma 		mlog_errno(ret);
3473110a045aSTao Ma 
3474110a045aSTao Ma out_commit:
3475110a045aSTao Ma 	ocfs2_commit_trans(osb, handle);
3476110a045aSTao Ma out:
3477110a045aSTao Ma 	if (meta_ac)
3478110a045aSTao Ma 		ocfs2_free_alloc_context(meta_ac);
3479110a045aSTao Ma 	return ret;
3480110a045aSTao Ma }
3481110a045aSTao Ma 
3482110a045aSTao Ma static int ocfs2_duplicate_extent_list(struct inode *s_inode,
3483110a045aSTao Ma 				struct inode *t_inode,
3484110a045aSTao Ma 				struct buffer_head *t_bh,
3485110a045aSTao Ma 				struct ocfs2_caching_info *ref_ci,
3486110a045aSTao Ma 				struct buffer_head *ref_root_bh,
3487110a045aSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
3488110a045aSTao Ma {
3489110a045aSTao Ma 	int ret = 0;
3490110a045aSTao Ma 	u32 p_cluster, num_clusters, clusters, cpos;
3491110a045aSTao Ma 	loff_t size;
3492110a045aSTao Ma 	unsigned int ext_flags;
3493110a045aSTao Ma 	struct ocfs2_extent_tree et;
3494110a045aSTao Ma 
3495110a045aSTao Ma 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh);
3496110a045aSTao Ma 
3497110a045aSTao Ma 	size = i_size_read(s_inode);
3498110a045aSTao Ma 	clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size);
3499110a045aSTao Ma 
3500110a045aSTao Ma 	cpos = 0;
3501110a045aSTao Ma 	while (cpos < clusters) {
3502110a045aSTao Ma 		ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster,
3503110a045aSTao Ma 					 &num_clusters, &ext_flags);
3504110a045aSTao Ma 
3505110a045aSTao Ma 		if (p_cluster) {
3506110a045aSTao Ma 			ret = ocfs2_add_refcounted_extent(t_inode, &et,
3507110a045aSTao Ma 							  ref_ci, ref_root_bh,
3508110a045aSTao Ma 							  cpos, p_cluster,
3509110a045aSTao Ma 							  num_clusters,
3510110a045aSTao Ma 							  ext_flags,
3511110a045aSTao Ma 							  dealloc);
3512110a045aSTao Ma 			if (ret) {
3513110a045aSTao Ma 				mlog_errno(ret);
3514110a045aSTao Ma 				goto out;
3515110a045aSTao Ma 			}
3516110a045aSTao Ma 		}
3517110a045aSTao Ma 
3518110a045aSTao Ma 		cpos += num_clusters;
3519110a045aSTao Ma 	}
3520110a045aSTao Ma 
3521110a045aSTao Ma out:
3522110a045aSTao Ma 	return ret;
3523110a045aSTao Ma }
3524110a045aSTao Ma 
3525110a045aSTao Ma static int ocfs2_create_reflink_node(struct inode *s_inode,
3526110a045aSTao Ma 				     struct buffer_head *s_bh,
3527110a045aSTao Ma 				     struct inode *t_inode,
3528110a045aSTao Ma 				     struct buffer_head *t_bh)
3529110a045aSTao Ma {
3530110a045aSTao Ma 	int ret;
3531110a045aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
3532110a045aSTao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
3533110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
3534110a045aSTao Ma 	struct ocfs2_refcount_block *rb;
3535110a045aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data;
3536110a045aSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
3537110a045aSTao Ma 
3538110a045aSTao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
3539110a045aSTao Ma 
3540110a045aSTao Ma 	ret = ocfs2_set_refcount_tree(t_inode, t_bh,
3541110a045aSTao Ma 				      le64_to_cpu(di->i_refcount_loc));
3542110a045aSTao Ma 	if (ret) {
3543110a045aSTao Ma 		mlog_errno(ret);
3544110a045aSTao Ma 		goto out;
3545110a045aSTao Ma 	}
3546110a045aSTao Ma 
3547110a045aSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
3548110a045aSTao Ma 				       1, &ref_tree, &ref_root_bh);
3549110a045aSTao Ma 	if (ret) {
3550110a045aSTao Ma 		mlog_errno(ret);
3551110a045aSTao Ma 		goto out;
3552110a045aSTao Ma 	}
3553110a045aSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
3554110a045aSTao Ma 
3555110a045aSTao Ma 	ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
3556110a045aSTao Ma 					  &ref_tree->rf_ci, ref_root_bh,
3557110a045aSTao Ma 					  &dealloc);
3558110a045aSTao Ma 	if (ret)
3559110a045aSTao Ma 		mlog_errno(ret);
3560110a045aSTao Ma 
3561110a045aSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3562110a045aSTao Ma 	brelse(ref_root_bh);
3563110a045aSTao Ma out:
3564110a045aSTao Ma 	if (ocfs2_dealloc_has_cluster(&dealloc)) {
3565110a045aSTao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
3566110a045aSTao Ma 		ocfs2_run_deallocs(osb, &dealloc);
3567110a045aSTao Ma 	}
3568110a045aSTao Ma 
3569110a045aSTao Ma 	return ret;
3570110a045aSTao Ma }
3571