xref: /OK3568_Linux_fs/kernel/fs/jfs/jfs_dmap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *   Copyright (C) International Business Machines Corp., 2000-2004
4*4882a593Smuzhiyun  *   Portions Copyright (C) Tino Reichardt, 2012
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/fs.h>
8*4882a593Smuzhiyun #include <linux/slab.h>
9*4882a593Smuzhiyun #include "jfs_incore.h"
10*4882a593Smuzhiyun #include "jfs_superblock.h"
11*4882a593Smuzhiyun #include "jfs_dmap.h"
12*4882a593Smuzhiyun #include "jfs_imap.h"
13*4882a593Smuzhiyun #include "jfs_lock.h"
14*4882a593Smuzhiyun #include "jfs_metapage.h"
15*4882a593Smuzhiyun #include "jfs_debug.h"
16*4882a593Smuzhiyun #include "jfs_discard.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  *	SERIALIZATION of the Block Allocation Map.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  *	the working state of the block allocation map is accessed in
22*4882a593Smuzhiyun  *	two directions:
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *	1) allocation and free requests that start at the dmap
25*4882a593Smuzhiyun  *	   level and move up through the dmap control pages (i.e.
26*4882a593Smuzhiyun  *	   the vast majority of requests).
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  *	2) allocation requests that start at dmap control page
29*4882a593Smuzhiyun  *	   level and work down towards the dmaps.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  *	the serialization scheme used here is as follows.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  *	requests which start at the bottom are serialized against each
34*4882a593Smuzhiyun  *	other through buffers and each requests holds onto its buffers
35*4882a593Smuzhiyun  *	as it works it way up from a single dmap to the required level
36*4882a593Smuzhiyun  *	of dmap control page.
37*4882a593Smuzhiyun  *	requests that start at the top are serialized against each other
38*4882a593Smuzhiyun  *	and request that start from the bottom by the multiple read/single
39*4882a593Smuzhiyun  *	write inode lock of the bmap inode. requests starting at the top
40*4882a593Smuzhiyun  *	take this lock in write mode while request starting at the bottom
41*4882a593Smuzhiyun  *	take the lock in read mode.  a single top-down request may proceed
42*4882a593Smuzhiyun  *	exclusively while multiple bottoms-up requests may proceed
43*4882a593Smuzhiyun  *	simultaneously (under the protection of busy buffers).
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  *	in addition to information found in dmaps and dmap control pages,
46*4882a593Smuzhiyun  *	the working state of the block allocation map also includes read/
47*4882a593Smuzhiyun  *	write information maintained in the bmap descriptor (i.e. total
48*4882a593Smuzhiyun  *	free block count, allocation group level free block counts).
49*4882a593Smuzhiyun  *	a single exclusive lock (BMAP_LOCK) is used to guard this information
50*4882a593Smuzhiyun  *	in the face of multiple-bottoms up requests.
51*4882a593Smuzhiyun  *	(lock ordering: IREAD_LOCK, BMAP_LOCK);
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  *	accesses to the persistent state of the block allocation map (limited
54*4882a593Smuzhiyun  *	to the persistent bitmaps in dmaps) is guarded by (busy) buffers.
55*4882a593Smuzhiyun  */
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #define BMAP_LOCK_INIT(bmp)	mutex_init(&bmp->db_bmaplock)
58*4882a593Smuzhiyun #define BMAP_LOCK(bmp)		mutex_lock(&bmp->db_bmaplock)
59*4882a593Smuzhiyun #define BMAP_UNLOCK(bmp)	mutex_unlock(&bmp->db_bmaplock)
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * forward references
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
65*4882a593Smuzhiyun 			int nblocks);
66*4882a593Smuzhiyun static void dbSplit(dmtree_t * tp, int leafno, int splitsz, int newval);
67*4882a593Smuzhiyun static int dbBackSplit(dmtree_t * tp, int leafno);
68*4882a593Smuzhiyun static int dbJoin(dmtree_t * tp, int leafno, int newval);
69*4882a593Smuzhiyun static void dbAdjTree(dmtree_t * tp, int leafno, int newval);
70*4882a593Smuzhiyun static int dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc,
71*4882a593Smuzhiyun 		    int level);
72*4882a593Smuzhiyun static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results);
73*4882a593Smuzhiyun static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
74*4882a593Smuzhiyun 		       int nblocks);
75*4882a593Smuzhiyun static int dbAllocNear(struct bmap * bmp, struct dmap * dp, s64 blkno,
76*4882a593Smuzhiyun 		       int nblocks,
77*4882a593Smuzhiyun 		       int l2nb, s64 * results);
78*4882a593Smuzhiyun static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
79*4882a593Smuzhiyun 		       int nblocks);
80*4882a593Smuzhiyun static int dbAllocDmapLev(struct bmap * bmp, struct dmap * dp, int nblocks,
81*4882a593Smuzhiyun 			  int l2nb,
82*4882a593Smuzhiyun 			  s64 * results);
83*4882a593Smuzhiyun static int dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb,
84*4882a593Smuzhiyun 		     s64 * results);
85*4882a593Smuzhiyun static int dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno,
86*4882a593Smuzhiyun 		      s64 * results);
87*4882a593Smuzhiyun static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks);
88*4882a593Smuzhiyun static int dbFindBits(u32 word, int l2nb);
89*4882a593Smuzhiyun static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno);
90*4882a593Smuzhiyun static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx);
91*4882a593Smuzhiyun static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
92*4882a593Smuzhiyun 		      int nblocks);
93*4882a593Smuzhiyun static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
94*4882a593Smuzhiyun 		      int nblocks);
95*4882a593Smuzhiyun static int dbMaxBud(u8 * cp);
96*4882a593Smuzhiyun static int blkstol2(s64 nb);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun static int cntlz(u32 value);
99*4882a593Smuzhiyun static int cnttz(u32 word);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
102*4882a593Smuzhiyun 			 int nblocks);
103*4882a593Smuzhiyun static int dbInitDmap(struct dmap * dp, s64 blkno, int nblocks);
104*4882a593Smuzhiyun static int dbInitDmapTree(struct dmap * dp);
105*4882a593Smuzhiyun static int dbInitTree(struct dmaptree * dtp);
106*4882a593Smuzhiyun static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i);
107*4882a593Smuzhiyun static int dbGetL2AGSize(s64 nblocks);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun  *	buddy table
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * table used for determining buddy sizes within characters of
113*4882a593Smuzhiyun  * dmap bitmap words.  the characters themselves serve as indexes
114*4882a593Smuzhiyun  * into the table, with the table elements yielding the maximum
115*4882a593Smuzhiyun  * binary buddy of free bits within the character.
116*4882a593Smuzhiyun  */
117*4882a593Smuzhiyun static const s8 budtab[256] = {
118*4882a593Smuzhiyun 	3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
119*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
120*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
121*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
122*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
123*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
124*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
125*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
126*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
127*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
128*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
129*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
130*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
131*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
132*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
133*4882a593Smuzhiyun 	2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun  * NAME:	dbMount()
138*4882a593Smuzhiyun  *
139*4882a593Smuzhiyun  * FUNCTION:	initializate the block allocation map.
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  *		memory is allocated for the in-core bmap descriptor and
142*4882a593Smuzhiyun  *		the in-core descriptor is initialized from disk.
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  * PARAMETERS:
145*4882a593Smuzhiyun  *	ipbmap	- pointer to in-core inode for the block map.
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * RETURN VALUES:
148*4882a593Smuzhiyun  *	0	- success
149*4882a593Smuzhiyun  *	-ENOMEM	- insufficient memory
150*4882a593Smuzhiyun  *	-EIO	- i/o error
151*4882a593Smuzhiyun  *	-EINVAL - wrong bmap data
152*4882a593Smuzhiyun  */
dbMount(struct inode * ipbmap)153*4882a593Smuzhiyun int dbMount(struct inode *ipbmap)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	struct bmap *bmp;
156*4882a593Smuzhiyun 	struct dbmap_disk *dbmp_le;
157*4882a593Smuzhiyun 	struct metapage *mp;
158*4882a593Smuzhiyun 	int i;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/*
161*4882a593Smuzhiyun 	 * allocate/initialize the in-memory bmap descriptor
162*4882a593Smuzhiyun 	 */
163*4882a593Smuzhiyun 	/* allocate memory for the in-memory bmap descriptor */
164*4882a593Smuzhiyun 	bmp = kmalloc(sizeof(struct bmap), GFP_KERNEL);
165*4882a593Smuzhiyun 	if (bmp == NULL)
166*4882a593Smuzhiyun 		return -ENOMEM;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/* read the on-disk bmap descriptor. */
169*4882a593Smuzhiyun 	mp = read_metapage(ipbmap,
170*4882a593Smuzhiyun 			   BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
171*4882a593Smuzhiyun 			   PSIZE, 0);
172*4882a593Smuzhiyun 	if (mp == NULL) {
173*4882a593Smuzhiyun 		kfree(bmp);
174*4882a593Smuzhiyun 		return -EIO;
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/* copy the on-disk bmap descriptor to its in-memory version. */
178*4882a593Smuzhiyun 	dbmp_le = (struct dbmap_disk *) mp->data;
179*4882a593Smuzhiyun 	bmp->db_mapsize = le64_to_cpu(dbmp_le->dn_mapsize);
180*4882a593Smuzhiyun 	bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree);
181*4882a593Smuzhiyun 	bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
182*4882a593Smuzhiyun 	bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
183*4882a593Smuzhiyun 	if (!bmp->db_numag) {
184*4882a593Smuzhiyun 		release_metapage(mp);
185*4882a593Smuzhiyun 		kfree(bmp);
186*4882a593Smuzhiyun 		return -EINVAL;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel);
190*4882a593Smuzhiyun 	bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag);
191*4882a593Smuzhiyun 	bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref);
192*4882a593Smuzhiyun 	bmp->db_aglevel = le32_to_cpu(dbmp_le->dn_aglevel);
193*4882a593Smuzhiyun 	bmp->db_agheight = le32_to_cpu(dbmp_le->dn_agheight);
194*4882a593Smuzhiyun 	bmp->db_agwidth = le32_to_cpu(dbmp_le->dn_agwidth);
195*4882a593Smuzhiyun 	bmp->db_agstart = le32_to_cpu(dbmp_le->dn_agstart);
196*4882a593Smuzhiyun 	bmp->db_agl2size = le32_to_cpu(dbmp_le->dn_agl2size);
197*4882a593Smuzhiyun 	for (i = 0; i < MAXAG; i++)
198*4882a593Smuzhiyun 		bmp->db_agfree[i] = le64_to_cpu(dbmp_le->dn_agfree[i]);
199*4882a593Smuzhiyun 	bmp->db_agsize = le64_to_cpu(dbmp_le->dn_agsize);
200*4882a593Smuzhiyun 	bmp->db_maxfreebud = dbmp_le->dn_maxfreebud;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/* release the buffer. */
203*4882a593Smuzhiyun 	release_metapage(mp);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/* bind the bmap inode and the bmap descriptor to each other. */
206*4882a593Smuzhiyun 	bmp->db_ipbmap = ipbmap;
207*4882a593Smuzhiyun 	JFS_SBI(ipbmap->i_sb)->bmap = bmp;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	memset(bmp->db_active, 0, sizeof(bmp->db_active));
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	/*
212*4882a593Smuzhiyun 	 * allocate/initialize the bmap lock
213*4882a593Smuzhiyun 	 */
214*4882a593Smuzhiyun 	BMAP_LOCK_INIT(bmp);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	return (0);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun /*
221*4882a593Smuzhiyun  * NAME:	dbUnmount()
222*4882a593Smuzhiyun  *
223*4882a593Smuzhiyun  * FUNCTION:	terminate the block allocation map in preparation for
224*4882a593Smuzhiyun  *		file system unmount.
225*4882a593Smuzhiyun  *
226*4882a593Smuzhiyun  *		the in-core bmap descriptor is written to disk and
227*4882a593Smuzhiyun  *		the memory for this descriptor is freed.
228*4882a593Smuzhiyun  *
229*4882a593Smuzhiyun  * PARAMETERS:
230*4882a593Smuzhiyun  *	ipbmap	- pointer to in-core inode for the block map.
231*4882a593Smuzhiyun  *
232*4882a593Smuzhiyun  * RETURN VALUES:
233*4882a593Smuzhiyun  *	0	- success
234*4882a593Smuzhiyun  *	-EIO	- i/o error
235*4882a593Smuzhiyun  */
dbUnmount(struct inode * ipbmap,int mounterror)236*4882a593Smuzhiyun int dbUnmount(struct inode *ipbmap, int mounterror)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	if (!(mounterror || isReadOnly(ipbmap)))
241*4882a593Smuzhiyun 		dbSync(ipbmap);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/*
244*4882a593Smuzhiyun 	 * Invalidate the page cache buffers
245*4882a593Smuzhiyun 	 */
246*4882a593Smuzhiyun 	truncate_inode_pages(ipbmap->i_mapping, 0);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	/* free the memory for the in-memory bmap. */
249*4882a593Smuzhiyun 	kfree(bmp);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	return (0);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun /*
255*4882a593Smuzhiyun  *	dbSync()
256*4882a593Smuzhiyun  */
dbSync(struct inode * ipbmap)257*4882a593Smuzhiyun int dbSync(struct inode *ipbmap)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	struct dbmap_disk *dbmp_le;
260*4882a593Smuzhiyun 	struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
261*4882a593Smuzhiyun 	struct metapage *mp;
262*4882a593Smuzhiyun 	int i;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	/*
265*4882a593Smuzhiyun 	 * write bmap global control page
266*4882a593Smuzhiyun 	 */
267*4882a593Smuzhiyun 	/* get the buffer for the on-disk bmap descriptor. */
268*4882a593Smuzhiyun 	mp = read_metapage(ipbmap,
269*4882a593Smuzhiyun 			   BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
270*4882a593Smuzhiyun 			   PSIZE, 0);
271*4882a593Smuzhiyun 	if (mp == NULL) {
272*4882a593Smuzhiyun 		jfs_err("dbSync: read_metapage failed!");
273*4882a593Smuzhiyun 		return -EIO;
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 	/* copy the in-memory version of the bmap to the on-disk version */
276*4882a593Smuzhiyun 	dbmp_le = (struct dbmap_disk *) mp->data;
277*4882a593Smuzhiyun 	dbmp_le->dn_mapsize = cpu_to_le64(bmp->db_mapsize);
278*4882a593Smuzhiyun 	dbmp_le->dn_nfree = cpu_to_le64(bmp->db_nfree);
279*4882a593Smuzhiyun 	dbmp_le->dn_l2nbperpage = cpu_to_le32(bmp->db_l2nbperpage);
280*4882a593Smuzhiyun 	dbmp_le->dn_numag = cpu_to_le32(bmp->db_numag);
281*4882a593Smuzhiyun 	dbmp_le->dn_maxlevel = cpu_to_le32(bmp->db_maxlevel);
282*4882a593Smuzhiyun 	dbmp_le->dn_maxag = cpu_to_le32(bmp->db_maxag);
283*4882a593Smuzhiyun 	dbmp_le->dn_agpref = cpu_to_le32(bmp->db_agpref);
284*4882a593Smuzhiyun 	dbmp_le->dn_aglevel = cpu_to_le32(bmp->db_aglevel);
285*4882a593Smuzhiyun 	dbmp_le->dn_agheight = cpu_to_le32(bmp->db_agheight);
286*4882a593Smuzhiyun 	dbmp_le->dn_agwidth = cpu_to_le32(bmp->db_agwidth);
287*4882a593Smuzhiyun 	dbmp_le->dn_agstart = cpu_to_le32(bmp->db_agstart);
288*4882a593Smuzhiyun 	dbmp_le->dn_agl2size = cpu_to_le32(bmp->db_agl2size);
289*4882a593Smuzhiyun 	for (i = 0; i < MAXAG; i++)
290*4882a593Smuzhiyun 		dbmp_le->dn_agfree[i] = cpu_to_le64(bmp->db_agfree[i]);
291*4882a593Smuzhiyun 	dbmp_le->dn_agsize = cpu_to_le64(bmp->db_agsize);
292*4882a593Smuzhiyun 	dbmp_le->dn_maxfreebud = bmp->db_maxfreebud;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	/* write the buffer */
295*4882a593Smuzhiyun 	write_metapage(mp);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	/*
298*4882a593Smuzhiyun 	 * write out dirty pages of bmap
299*4882a593Smuzhiyun 	 */
300*4882a593Smuzhiyun 	filemap_write_and_wait(ipbmap->i_mapping);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	diWriteSpecial(ipbmap, 0);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	return (0);
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun /*
308*4882a593Smuzhiyun  * NAME:	dbFree()
309*4882a593Smuzhiyun  *
310*4882a593Smuzhiyun  * FUNCTION:	free the specified block range from the working block
311*4882a593Smuzhiyun  *		allocation map.
312*4882a593Smuzhiyun  *
313*4882a593Smuzhiyun  *		the blocks will be free from the working map one dmap
314*4882a593Smuzhiyun  *		at a time.
315*4882a593Smuzhiyun  *
316*4882a593Smuzhiyun  * PARAMETERS:
317*4882a593Smuzhiyun  *	ip	- pointer to in-core inode;
318*4882a593Smuzhiyun  *	blkno	- starting block number to be freed.
319*4882a593Smuzhiyun  *	nblocks	- number of blocks to be freed.
320*4882a593Smuzhiyun  *
321*4882a593Smuzhiyun  * RETURN VALUES:
322*4882a593Smuzhiyun  *	0	- success
323*4882a593Smuzhiyun  *	-EIO	- i/o error
324*4882a593Smuzhiyun  */
dbFree(struct inode * ip,s64 blkno,s64 nblocks)325*4882a593Smuzhiyun int dbFree(struct inode *ip, s64 blkno, s64 nblocks)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	struct metapage *mp;
328*4882a593Smuzhiyun 	struct dmap *dp;
329*4882a593Smuzhiyun 	int nb, rc;
330*4882a593Smuzhiyun 	s64 lblkno, rem;
331*4882a593Smuzhiyun 	struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
332*4882a593Smuzhiyun 	struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
333*4882a593Smuzhiyun 	struct super_block *sb = ipbmap->i_sb;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	/* block to be freed better be within the mapsize. */
338*4882a593Smuzhiyun 	if (unlikely((blkno == 0) || (blkno + nblocks > bmp->db_mapsize))) {
339*4882a593Smuzhiyun 		IREAD_UNLOCK(ipbmap);
340*4882a593Smuzhiyun 		printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
341*4882a593Smuzhiyun 		       (unsigned long long) blkno,
342*4882a593Smuzhiyun 		       (unsigned long long) nblocks);
343*4882a593Smuzhiyun 		jfs_error(ip->i_sb, "block to be freed is outside the map\n");
344*4882a593Smuzhiyun 		return -EIO;
345*4882a593Smuzhiyun 	}
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	/**
348*4882a593Smuzhiyun 	 * TRIM the blocks, when mounted with discard option
349*4882a593Smuzhiyun 	 */
350*4882a593Smuzhiyun 	if (JFS_SBI(sb)->flag & JFS_DISCARD)
351*4882a593Smuzhiyun 		if (JFS_SBI(sb)->minblks_trim <= nblocks)
352*4882a593Smuzhiyun 			jfs_issue_discard(ipbmap, blkno, nblocks);
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/*
355*4882a593Smuzhiyun 	 * free the blocks a dmap at a time.
356*4882a593Smuzhiyun 	 */
357*4882a593Smuzhiyun 	mp = NULL;
358*4882a593Smuzhiyun 	for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
359*4882a593Smuzhiyun 		/* release previous dmap if any */
360*4882a593Smuzhiyun 		if (mp) {
361*4882a593Smuzhiyun 			write_metapage(mp);
362*4882a593Smuzhiyun 		}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 		/* get the buffer for the current dmap. */
365*4882a593Smuzhiyun 		lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
366*4882a593Smuzhiyun 		mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
367*4882a593Smuzhiyun 		if (mp == NULL) {
368*4882a593Smuzhiyun 			IREAD_UNLOCK(ipbmap);
369*4882a593Smuzhiyun 			return -EIO;
370*4882a593Smuzhiyun 		}
371*4882a593Smuzhiyun 		dp = (struct dmap *) mp->data;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 		/* determine the number of blocks to be freed from
374*4882a593Smuzhiyun 		 * this dmap.
375*4882a593Smuzhiyun 		 */
376*4882a593Smuzhiyun 		nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 		/* free the blocks. */
379*4882a593Smuzhiyun 		if ((rc = dbFreeDmap(bmp, dp, blkno, nb))) {
380*4882a593Smuzhiyun 			jfs_error(ip->i_sb, "error in block map\n");
381*4882a593Smuzhiyun 			release_metapage(mp);
382*4882a593Smuzhiyun 			IREAD_UNLOCK(ipbmap);
383*4882a593Smuzhiyun 			return (rc);
384*4882a593Smuzhiyun 		}
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* write the last buffer. */
388*4882a593Smuzhiyun 	if (mp)
389*4882a593Smuzhiyun 		write_metapage(mp);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	IREAD_UNLOCK(ipbmap);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	return (0);
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun /*
398*4882a593Smuzhiyun  * NAME:	dbUpdatePMap()
399*4882a593Smuzhiyun  *
400*4882a593Smuzhiyun  * FUNCTION:	update the allocation state (free or allocate) of the
401*4882a593Smuzhiyun  *		specified block range in the persistent block allocation map.
402*4882a593Smuzhiyun  *
403*4882a593Smuzhiyun  *		the blocks will be updated in the persistent map one
404*4882a593Smuzhiyun  *		dmap at a time.
405*4882a593Smuzhiyun  *
406*4882a593Smuzhiyun  * PARAMETERS:
407*4882a593Smuzhiyun  *	ipbmap	- pointer to in-core inode for the block map.
408*4882a593Smuzhiyun  *	free	- 'true' if block range is to be freed from the persistent
409*4882a593Smuzhiyun  *		  map; 'false' if it is to be allocated.
410*4882a593Smuzhiyun  *	blkno	- starting block number of the range.
411*4882a593Smuzhiyun  *	nblocks	- number of contiguous blocks in the range.
412*4882a593Smuzhiyun  *	tblk	- transaction block;
413*4882a593Smuzhiyun  *
414*4882a593Smuzhiyun  * RETURN VALUES:
415*4882a593Smuzhiyun  *	0	- success
416*4882a593Smuzhiyun  *	-EIO	- i/o error
417*4882a593Smuzhiyun  */
418*4882a593Smuzhiyun int
dbUpdatePMap(struct inode * ipbmap,int free,s64 blkno,s64 nblocks,struct tblock * tblk)419*4882a593Smuzhiyun dbUpdatePMap(struct inode *ipbmap,
420*4882a593Smuzhiyun 	     int free, s64 blkno, s64 nblocks, struct tblock * tblk)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	int nblks, dbitno, wbitno, rbits;
423*4882a593Smuzhiyun 	int word, nbits, nwords;
424*4882a593Smuzhiyun 	struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
425*4882a593Smuzhiyun 	s64 lblkno, rem, lastlblkno;
426*4882a593Smuzhiyun 	u32 mask;
427*4882a593Smuzhiyun 	struct dmap *dp;
428*4882a593Smuzhiyun 	struct metapage *mp;
429*4882a593Smuzhiyun 	struct jfs_log *log;
430*4882a593Smuzhiyun 	int lsn, difft, diffp;
431*4882a593Smuzhiyun 	unsigned long flags;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	/* the blocks better be within the mapsize. */
434*4882a593Smuzhiyun 	if (blkno + nblocks > bmp->db_mapsize) {
435*4882a593Smuzhiyun 		printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
436*4882a593Smuzhiyun 		       (unsigned long long) blkno,
437*4882a593Smuzhiyun 		       (unsigned long long) nblocks);
438*4882a593Smuzhiyun 		jfs_error(ipbmap->i_sb, "blocks are outside the map\n");
439*4882a593Smuzhiyun 		return -EIO;
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	/* compute delta of transaction lsn from log syncpt */
443*4882a593Smuzhiyun 	lsn = tblk->lsn;
444*4882a593Smuzhiyun 	log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
445*4882a593Smuzhiyun 	logdiff(difft, lsn, log);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	/*
448*4882a593Smuzhiyun 	 * update the block state a dmap at a time.
449*4882a593Smuzhiyun 	 */
450*4882a593Smuzhiyun 	mp = NULL;
451*4882a593Smuzhiyun 	lastlblkno = 0;
452*4882a593Smuzhiyun 	for (rem = nblocks; rem > 0; rem -= nblks, blkno += nblks) {
453*4882a593Smuzhiyun 		/* get the buffer for the current dmap. */
454*4882a593Smuzhiyun 		lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
455*4882a593Smuzhiyun 		if (lblkno != lastlblkno) {
456*4882a593Smuzhiyun 			if (mp) {
457*4882a593Smuzhiyun 				write_metapage(mp);
458*4882a593Smuzhiyun 			}
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 			mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE,
461*4882a593Smuzhiyun 					   0);
462*4882a593Smuzhiyun 			if (mp == NULL)
463*4882a593Smuzhiyun 				return -EIO;
464*4882a593Smuzhiyun 			metapage_wait_for_io(mp);
465*4882a593Smuzhiyun 		}
466*4882a593Smuzhiyun 		dp = (struct dmap *) mp->data;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 		/* determine the bit number and word within the dmap of
469*4882a593Smuzhiyun 		 * the starting block.  also determine how many blocks
470*4882a593Smuzhiyun 		 * are to be updated within this dmap.
471*4882a593Smuzhiyun 		 */
472*4882a593Smuzhiyun 		dbitno = blkno & (BPERDMAP - 1);
473*4882a593Smuzhiyun 		word = dbitno >> L2DBWORD;
474*4882a593Smuzhiyun 		nblks = min(rem, (s64)BPERDMAP - dbitno);
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 		/* update the bits of the dmap words. the first and last
477*4882a593Smuzhiyun 		 * words may only have a subset of their bits updated. if
478*4882a593Smuzhiyun 		 * this is the case, we'll work against that word (i.e.
479*4882a593Smuzhiyun 		 * partial first and/or last) only in a single pass.  a
480*4882a593Smuzhiyun 		 * single pass will also be used to update all words that
481*4882a593Smuzhiyun 		 * are to have all their bits updated.
482*4882a593Smuzhiyun 		 */
483*4882a593Smuzhiyun 		for (rbits = nblks; rbits > 0;
484*4882a593Smuzhiyun 		     rbits -= nbits, dbitno += nbits) {
485*4882a593Smuzhiyun 			/* determine the bit number within the word and
486*4882a593Smuzhiyun 			 * the number of bits within the word.
487*4882a593Smuzhiyun 			 */
488*4882a593Smuzhiyun 			wbitno = dbitno & (DBWORD - 1);
489*4882a593Smuzhiyun 			nbits = min(rbits, DBWORD - wbitno);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 			/* check if only part of the word is to be updated. */
492*4882a593Smuzhiyun 			if (nbits < DBWORD) {
493*4882a593Smuzhiyun 				/* update (free or allocate) the bits
494*4882a593Smuzhiyun 				 * in this word.
495*4882a593Smuzhiyun 				 */
496*4882a593Smuzhiyun 				mask =
497*4882a593Smuzhiyun 				    (ONES << (DBWORD - nbits) >> wbitno);
498*4882a593Smuzhiyun 				if (free)
499*4882a593Smuzhiyun 					dp->pmap[word] &=
500*4882a593Smuzhiyun 					    cpu_to_le32(~mask);
501*4882a593Smuzhiyun 				else
502*4882a593Smuzhiyun 					dp->pmap[word] |=
503*4882a593Smuzhiyun 					    cpu_to_le32(mask);
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 				word += 1;
506*4882a593Smuzhiyun 			} else {
507*4882a593Smuzhiyun 				/* one or more words are to have all
508*4882a593Smuzhiyun 				 * their bits updated.  determine how
509*4882a593Smuzhiyun 				 * many words and how many bits.
510*4882a593Smuzhiyun 				 */
511*4882a593Smuzhiyun 				nwords = rbits >> L2DBWORD;
512*4882a593Smuzhiyun 				nbits = nwords << L2DBWORD;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 				/* update (free or allocate) the bits
515*4882a593Smuzhiyun 				 * in these words.
516*4882a593Smuzhiyun 				 */
517*4882a593Smuzhiyun 				if (free)
518*4882a593Smuzhiyun 					memset(&dp->pmap[word], 0,
519*4882a593Smuzhiyun 					       nwords * 4);
520*4882a593Smuzhiyun 				else
521*4882a593Smuzhiyun 					memset(&dp->pmap[word], (int) ONES,
522*4882a593Smuzhiyun 					       nwords * 4);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 				word += nwords;
525*4882a593Smuzhiyun 			}
526*4882a593Smuzhiyun 		}
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 		/*
529*4882a593Smuzhiyun 		 * update dmap lsn
530*4882a593Smuzhiyun 		 */
531*4882a593Smuzhiyun 		if (lblkno == lastlblkno)
532*4882a593Smuzhiyun 			continue;
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 		lastlblkno = lblkno;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 		LOGSYNC_LOCK(log, flags);
537*4882a593Smuzhiyun 		if (mp->lsn != 0) {
538*4882a593Smuzhiyun 			/* inherit older/smaller lsn */
539*4882a593Smuzhiyun 			logdiff(diffp, mp->lsn, log);
540*4882a593Smuzhiyun 			if (difft < diffp) {
541*4882a593Smuzhiyun 				mp->lsn = lsn;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 				/* move bp after tblock in logsync list */
544*4882a593Smuzhiyun 				list_move(&mp->synclist, &tblk->synclist);
545*4882a593Smuzhiyun 			}
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 			/* inherit younger/larger clsn */
548*4882a593Smuzhiyun 			logdiff(difft, tblk->clsn, log);
549*4882a593Smuzhiyun 			logdiff(diffp, mp->clsn, log);
550*4882a593Smuzhiyun 			if (difft > diffp)
551*4882a593Smuzhiyun 				mp->clsn = tblk->clsn;
552*4882a593Smuzhiyun 		} else {
553*4882a593Smuzhiyun 			mp->log = log;
554*4882a593Smuzhiyun 			mp->lsn = lsn;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 			/* insert bp after tblock in logsync list */
557*4882a593Smuzhiyun 			log->count++;
558*4882a593Smuzhiyun 			list_add(&mp->synclist, &tblk->synclist);
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 			mp->clsn = tblk->clsn;
561*4882a593Smuzhiyun 		}
562*4882a593Smuzhiyun 		LOGSYNC_UNLOCK(log, flags);
563*4882a593Smuzhiyun 	}
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	/* write the last buffer. */
566*4882a593Smuzhiyun 	if (mp) {
567*4882a593Smuzhiyun 		write_metapage(mp);
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	return (0);
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun /*
575*4882a593Smuzhiyun  * NAME:	dbNextAG()
576*4882a593Smuzhiyun  *
577*4882a593Smuzhiyun  * FUNCTION:	find the preferred allocation group for new allocations.
578*4882a593Smuzhiyun  *
579*4882a593Smuzhiyun  *		Within the allocation groups, we maintain a preferred
580*4882a593Smuzhiyun  *		allocation group which consists of a group with at least
581*4882a593Smuzhiyun  *		average free space.  It is the preferred group that we target
582*4882a593Smuzhiyun  *		new inode allocation towards.  The tie-in between inode
583*4882a593Smuzhiyun  *		allocation and block allocation occurs as we allocate the
584*4882a593Smuzhiyun  *		first (data) block of an inode and specify the inode (block)
585*4882a593Smuzhiyun  *		as the allocation hint for this block.
586*4882a593Smuzhiyun  *
587*4882a593Smuzhiyun  *		We try to avoid having more than one open file growing in
588*4882a593Smuzhiyun  *		an allocation group, as this will lead to fragmentation.
589*4882a593Smuzhiyun  *		This differs from the old OS/2 method of trying to keep
590*4882a593Smuzhiyun  *		empty ags around for large allocations.
591*4882a593Smuzhiyun  *
592*4882a593Smuzhiyun  * PARAMETERS:
593*4882a593Smuzhiyun  *	ipbmap	- pointer to in-core inode for the block map.
594*4882a593Smuzhiyun  *
595*4882a593Smuzhiyun  * RETURN VALUES:
596*4882a593Smuzhiyun  *	the preferred allocation group number.
597*4882a593Smuzhiyun  */
dbNextAG(struct inode * ipbmap)598*4882a593Smuzhiyun int dbNextAG(struct inode *ipbmap)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun 	s64 avgfree;
601*4882a593Smuzhiyun 	int agpref;
602*4882a593Smuzhiyun 	s64 hwm = 0;
603*4882a593Smuzhiyun 	int i;
604*4882a593Smuzhiyun 	int next_best = -1;
605*4882a593Smuzhiyun 	struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	BMAP_LOCK(bmp);
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	/* determine the average number of free blocks within the ags. */
610*4882a593Smuzhiyun 	avgfree = (u32)bmp->db_nfree / bmp->db_numag;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	/*
613*4882a593Smuzhiyun 	 * if the current preferred ag does not have an active allocator
614*4882a593Smuzhiyun 	 * and has at least average freespace, return it
615*4882a593Smuzhiyun 	 */
616*4882a593Smuzhiyun 	agpref = bmp->db_agpref;
617*4882a593Smuzhiyun 	if ((atomic_read(&bmp->db_active[agpref]) == 0) &&
618*4882a593Smuzhiyun 	    (bmp->db_agfree[agpref] >= avgfree))
619*4882a593Smuzhiyun 		goto unlock;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	/* From the last preferred ag, find the next one with at least
622*4882a593Smuzhiyun 	 * average free space.
623*4882a593Smuzhiyun 	 */
624*4882a593Smuzhiyun 	for (i = 0 ; i < bmp->db_numag; i++, agpref++) {
625*4882a593Smuzhiyun 		if (agpref == bmp->db_numag)
626*4882a593Smuzhiyun 			agpref = 0;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 		if (atomic_read(&bmp->db_active[agpref]))
629*4882a593Smuzhiyun 			/* open file is currently growing in this ag */
630*4882a593Smuzhiyun 			continue;
631*4882a593Smuzhiyun 		if (bmp->db_agfree[agpref] >= avgfree) {
632*4882a593Smuzhiyun 			/* Return this one */
633*4882a593Smuzhiyun 			bmp->db_agpref = agpref;
634*4882a593Smuzhiyun 			goto unlock;
635*4882a593Smuzhiyun 		} else if (bmp->db_agfree[agpref] > hwm) {
636*4882a593Smuzhiyun 			/* Less than avg. freespace, but best so far */
637*4882a593Smuzhiyun 			hwm = bmp->db_agfree[agpref];
638*4882a593Smuzhiyun 			next_best = agpref;
639*4882a593Smuzhiyun 		}
640*4882a593Smuzhiyun 	}
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	/*
643*4882a593Smuzhiyun 	 * If no inactive ag was found with average freespace, use the
644*4882a593Smuzhiyun 	 * next best
645*4882a593Smuzhiyun 	 */
646*4882a593Smuzhiyun 	if (next_best != -1)
647*4882a593Smuzhiyun 		bmp->db_agpref = next_best;
648*4882a593Smuzhiyun 	/* else leave db_agpref unchanged */
649*4882a593Smuzhiyun unlock:
650*4882a593Smuzhiyun 	BMAP_UNLOCK(bmp);
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	/* return the preferred group.
653*4882a593Smuzhiyun 	 */
654*4882a593Smuzhiyun 	return (bmp->db_agpref);
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun /*
658*4882a593Smuzhiyun  * NAME:	dbAlloc()
659*4882a593Smuzhiyun  *
660*4882a593Smuzhiyun  * FUNCTION:	attempt to allocate a specified number of contiguous free
661*4882a593Smuzhiyun  *		blocks from the working allocation block map.
662*4882a593Smuzhiyun  *
663*4882a593Smuzhiyun  *		the block allocation policy uses hints and a multi-step
664*4882a593Smuzhiyun  *		approach.
665*4882a593Smuzhiyun  *
666*4882a593Smuzhiyun  *		for allocation requests smaller than the number of blocks
667*4882a593Smuzhiyun  *		per dmap, we first try to allocate the new blocks
668*4882a593Smuzhiyun  *		immediately following the hint.  if these blocks are not
669*4882a593Smuzhiyun  *		available, we try to allocate blocks near the hint.  if
670*4882a593Smuzhiyun  *		no blocks near the hint are available, we next try to
671*4882a593Smuzhiyun  *		allocate within the same dmap as contains the hint.
672*4882a593Smuzhiyun  *
673*4882a593Smuzhiyun  *		if no blocks are available in the dmap or the allocation
674*4882a593Smuzhiyun  *		request is larger than the dmap size, we try to allocate
675*4882a593Smuzhiyun  *		within the same allocation group as contains the hint. if
676*4882a593Smuzhiyun  *		this does not succeed, we finally try to allocate anywhere
677*4882a593Smuzhiyun  *		within the aggregate.
678*4882a593Smuzhiyun  *
679*4882a593Smuzhiyun  *		we also try to allocate anywhere within the aggregate for
680*4882a593Smuzhiyun  *		for allocation requests larger than the allocation group
681*4882a593Smuzhiyun  *		size or requests that specify no hint value.
682*4882a593Smuzhiyun  *
683*4882a593Smuzhiyun  * PARAMETERS:
684*4882a593Smuzhiyun  *	ip	- pointer to in-core inode;
685*4882a593Smuzhiyun  *	hint	- allocation hint.
686*4882a593Smuzhiyun  *	nblocks	- number of contiguous blocks in the range.
687*4882a593Smuzhiyun  *	results	- on successful return, set to the starting block number
688*4882a593Smuzhiyun  *		  of the newly allocated contiguous range.
689*4882a593Smuzhiyun  *
690*4882a593Smuzhiyun  * RETURN VALUES:
691*4882a593Smuzhiyun  *	0	- success
692*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
693*4882a593Smuzhiyun  *	-EIO	- i/o error
694*4882a593Smuzhiyun  */
dbAlloc(struct inode * ip,s64 hint,s64 nblocks,s64 * results)695*4882a593Smuzhiyun int dbAlloc(struct inode *ip, s64 hint, s64 nblocks, s64 * results)
696*4882a593Smuzhiyun {
697*4882a593Smuzhiyun 	int rc, agno;
698*4882a593Smuzhiyun 	struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
699*4882a593Smuzhiyun 	struct bmap *bmp;
700*4882a593Smuzhiyun 	struct metapage *mp;
701*4882a593Smuzhiyun 	s64 lblkno, blkno;
702*4882a593Smuzhiyun 	struct dmap *dp;
703*4882a593Smuzhiyun 	int l2nb;
704*4882a593Smuzhiyun 	s64 mapSize;
705*4882a593Smuzhiyun 	int writers;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	/* assert that nblocks is valid */
708*4882a593Smuzhiyun 	assert(nblocks > 0);
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	/* get the log2 number of blocks to be allocated.
711*4882a593Smuzhiyun 	 * if the number of blocks is not a log2 multiple,
712*4882a593Smuzhiyun 	 * it will be rounded up to the next log2 multiple.
713*4882a593Smuzhiyun 	 */
714*4882a593Smuzhiyun 	l2nb = BLKSTOL2(nblocks);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	bmp = JFS_SBI(ip->i_sb)->bmap;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	mapSize = bmp->db_mapsize;
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	/* the hint should be within the map */
721*4882a593Smuzhiyun 	if (hint >= mapSize) {
722*4882a593Smuzhiyun 		jfs_error(ip->i_sb, "the hint is outside the map\n");
723*4882a593Smuzhiyun 		return -EIO;
724*4882a593Smuzhiyun 	}
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	/* if the number of blocks to be allocated is greater than the
727*4882a593Smuzhiyun 	 * allocation group size, try to allocate anywhere.
728*4882a593Smuzhiyun 	 */
729*4882a593Smuzhiyun 	if (l2nb > bmp->db_agl2size) {
730*4882a593Smuzhiyun 		IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 		rc = dbAllocAny(bmp, nblocks, l2nb, results);
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 		goto write_unlock;
735*4882a593Smuzhiyun 	}
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	/*
738*4882a593Smuzhiyun 	 * If no hint, let dbNextAG recommend an allocation group
739*4882a593Smuzhiyun 	 */
740*4882a593Smuzhiyun 	if (hint == 0)
741*4882a593Smuzhiyun 		goto pref_ag;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* we would like to allocate close to the hint.  adjust the
744*4882a593Smuzhiyun 	 * hint to the block following the hint since the allocators
745*4882a593Smuzhiyun 	 * will start looking for free space starting at this point.
746*4882a593Smuzhiyun 	 */
747*4882a593Smuzhiyun 	blkno = hint + 1;
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	if (blkno >= bmp->db_mapsize)
750*4882a593Smuzhiyun 		goto pref_ag;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	agno = blkno >> bmp->db_agl2size;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	/* check if blkno crosses over into a new allocation group.
755*4882a593Smuzhiyun 	 * if so, check if we should allow allocations within this
756*4882a593Smuzhiyun 	 * allocation group.
757*4882a593Smuzhiyun 	 */
758*4882a593Smuzhiyun 	if ((blkno & (bmp->db_agsize - 1)) == 0)
759*4882a593Smuzhiyun 		/* check if the AG is currently being written to.
760*4882a593Smuzhiyun 		 * if so, call dbNextAG() to find a non-busy
761*4882a593Smuzhiyun 		 * AG with sufficient free space.
762*4882a593Smuzhiyun 		 */
763*4882a593Smuzhiyun 		if (atomic_read(&bmp->db_active[agno]))
764*4882a593Smuzhiyun 			goto pref_ag;
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	/* check if the allocation request size can be satisfied from a
767*4882a593Smuzhiyun 	 * single dmap.  if so, try to allocate from the dmap containing
768*4882a593Smuzhiyun 	 * the hint using a tiered strategy.
769*4882a593Smuzhiyun 	 */
770*4882a593Smuzhiyun 	if (nblocks <= BPERDMAP) {
771*4882a593Smuzhiyun 		IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 		/* get the buffer for the dmap containing the hint.
774*4882a593Smuzhiyun 		 */
775*4882a593Smuzhiyun 		rc = -EIO;
776*4882a593Smuzhiyun 		lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
777*4882a593Smuzhiyun 		mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
778*4882a593Smuzhiyun 		if (mp == NULL)
779*4882a593Smuzhiyun 			goto read_unlock;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 		dp = (struct dmap *) mp->data;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 		/* first, try to satisfy the allocation request with the
784*4882a593Smuzhiyun 		 * blocks beginning at the hint.
785*4882a593Smuzhiyun 		 */
786*4882a593Smuzhiyun 		if ((rc = dbAllocNext(bmp, dp, blkno, (int) nblocks))
787*4882a593Smuzhiyun 		    != -ENOSPC) {
788*4882a593Smuzhiyun 			if (rc == 0) {
789*4882a593Smuzhiyun 				*results = blkno;
790*4882a593Smuzhiyun 				mark_metapage_dirty(mp);
791*4882a593Smuzhiyun 			}
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 			release_metapage(mp);
794*4882a593Smuzhiyun 			goto read_unlock;
795*4882a593Smuzhiyun 		}
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 		writers = atomic_read(&bmp->db_active[agno]);
798*4882a593Smuzhiyun 		if ((writers > 1) ||
799*4882a593Smuzhiyun 		    ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) {
800*4882a593Smuzhiyun 			/*
801*4882a593Smuzhiyun 			 * Someone else is writing in this allocation
802*4882a593Smuzhiyun 			 * group.  To avoid fragmenting, try another ag
803*4882a593Smuzhiyun 			 */
804*4882a593Smuzhiyun 			release_metapage(mp);
805*4882a593Smuzhiyun 			IREAD_UNLOCK(ipbmap);
806*4882a593Smuzhiyun 			goto pref_ag;
807*4882a593Smuzhiyun 		}
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 		/* next, try to satisfy the allocation request with blocks
810*4882a593Smuzhiyun 		 * near the hint.
811*4882a593Smuzhiyun 		 */
812*4882a593Smuzhiyun 		if ((rc =
813*4882a593Smuzhiyun 		     dbAllocNear(bmp, dp, blkno, (int) nblocks, l2nb, results))
814*4882a593Smuzhiyun 		    != -ENOSPC) {
815*4882a593Smuzhiyun 			if (rc == 0)
816*4882a593Smuzhiyun 				mark_metapage_dirty(mp);
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 			release_metapage(mp);
819*4882a593Smuzhiyun 			goto read_unlock;
820*4882a593Smuzhiyun 		}
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 		/* try to satisfy the allocation request with blocks within
823*4882a593Smuzhiyun 		 * the same dmap as the hint.
824*4882a593Smuzhiyun 		 */
825*4882a593Smuzhiyun 		if ((rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results))
826*4882a593Smuzhiyun 		    != -ENOSPC) {
827*4882a593Smuzhiyun 			if (rc == 0)
828*4882a593Smuzhiyun 				mark_metapage_dirty(mp);
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 			release_metapage(mp);
831*4882a593Smuzhiyun 			goto read_unlock;
832*4882a593Smuzhiyun 		}
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 		release_metapage(mp);
835*4882a593Smuzhiyun 		IREAD_UNLOCK(ipbmap);
836*4882a593Smuzhiyun 	}
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	/* try to satisfy the allocation request with blocks within
839*4882a593Smuzhiyun 	 * the same allocation group as the hint.
840*4882a593Smuzhiyun 	 */
841*4882a593Smuzhiyun 	IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
842*4882a593Smuzhiyun 	if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC)
843*4882a593Smuzhiyun 		goto write_unlock;
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	IWRITE_UNLOCK(ipbmap);
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun       pref_ag:
849*4882a593Smuzhiyun 	/*
850*4882a593Smuzhiyun 	 * Let dbNextAG recommend a preferred allocation group
851*4882a593Smuzhiyun 	 */
852*4882a593Smuzhiyun 	agno = dbNextAG(ipbmap);
853*4882a593Smuzhiyun 	IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	/* Try to allocate within this allocation group.  if that fails, try to
856*4882a593Smuzhiyun 	 * allocate anywhere in the map.
857*4882a593Smuzhiyun 	 */
858*4882a593Smuzhiyun 	if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC)
859*4882a593Smuzhiyun 		rc = dbAllocAny(bmp, nblocks, l2nb, results);
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun       write_unlock:
862*4882a593Smuzhiyun 	IWRITE_UNLOCK(ipbmap);
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	return (rc);
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun       read_unlock:
867*4882a593Smuzhiyun 	IREAD_UNLOCK(ipbmap);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	return (rc);
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun #ifdef _NOTYET
873*4882a593Smuzhiyun /*
874*4882a593Smuzhiyun  * NAME:	dbAllocExact()
875*4882a593Smuzhiyun  *
876*4882a593Smuzhiyun  * FUNCTION:	try to allocate the requested extent;
877*4882a593Smuzhiyun  *
878*4882a593Smuzhiyun  * PARAMETERS:
879*4882a593Smuzhiyun  *	ip	- pointer to in-core inode;
880*4882a593Smuzhiyun  *	blkno	- extent address;
881*4882a593Smuzhiyun  *	nblocks	- extent length;
882*4882a593Smuzhiyun  *
883*4882a593Smuzhiyun  * RETURN VALUES:
884*4882a593Smuzhiyun  *	0	- success
885*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
886*4882a593Smuzhiyun  *	-EIO	- i/o error
887*4882a593Smuzhiyun  */
dbAllocExact(struct inode * ip,s64 blkno,int nblocks)888*4882a593Smuzhiyun int dbAllocExact(struct inode *ip, s64 blkno, int nblocks)
889*4882a593Smuzhiyun {
890*4882a593Smuzhiyun 	int rc;
891*4882a593Smuzhiyun 	struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
892*4882a593Smuzhiyun 	struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
893*4882a593Smuzhiyun 	struct dmap *dp;
894*4882a593Smuzhiyun 	s64 lblkno;
895*4882a593Smuzhiyun 	struct metapage *mp;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	/*
900*4882a593Smuzhiyun 	 * validate extent request:
901*4882a593Smuzhiyun 	 *
902*4882a593Smuzhiyun 	 * note: defragfs policy:
903*4882a593Smuzhiyun 	 *  max 64 blocks will be moved.
904*4882a593Smuzhiyun 	 *  allocation request size must be satisfied from a single dmap.
905*4882a593Smuzhiyun 	 */
906*4882a593Smuzhiyun 	if (nblocks <= 0 || nblocks > BPERDMAP || blkno >= bmp->db_mapsize) {
907*4882a593Smuzhiyun 		IREAD_UNLOCK(ipbmap);
908*4882a593Smuzhiyun 		return -EINVAL;
909*4882a593Smuzhiyun 	}
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	if (nblocks > ((s64) 1 << bmp->db_maxfreebud)) {
912*4882a593Smuzhiyun 		/* the free space is no longer available */
913*4882a593Smuzhiyun 		IREAD_UNLOCK(ipbmap);
914*4882a593Smuzhiyun 		return -ENOSPC;
915*4882a593Smuzhiyun 	}
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	/* read in the dmap covering the extent */
918*4882a593Smuzhiyun 	lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
919*4882a593Smuzhiyun 	mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
920*4882a593Smuzhiyun 	if (mp == NULL) {
921*4882a593Smuzhiyun 		IREAD_UNLOCK(ipbmap);
922*4882a593Smuzhiyun 		return -EIO;
923*4882a593Smuzhiyun 	}
924*4882a593Smuzhiyun 	dp = (struct dmap *) mp->data;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	/* try to allocate the requested extent */
927*4882a593Smuzhiyun 	rc = dbAllocNext(bmp, dp, blkno, nblocks);
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	IREAD_UNLOCK(ipbmap);
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	if (rc == 0)
932*4882a593Smuzhiyun 		mark_metapage_dirty(mp);
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	release_metapage(mp);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	return (rc);
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun #endif /* _NOTYET */
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun /*
941*4882a593Smuzhiyun  * NAME:	dbReAlloc()
942*4882a593Smuzhiyun  *
943*4882a593Smuzhiyun  * FUNCTION:	attempt to extend a current allocation by a specified
944*4882a593Smuzhiyun  *		number of blocks.
945*4882a593Smuzhiyun  *
946*4882a593Smuzhiyun  *		this routine attempts to satisfy the allocation request
947*4882a593Smuzhiyun  *		by first trying to extend the existing allocation in
948*4882a593Smuzhiyun  *		place by allocating the additional blocks as the blocks
949*4882a593Smuzhiyun  *		immediately following the current allocation.  if these
950*4882a593Smuzhiyun  *		blocks are not available, this routine will attempt to
951*4882a593Smuzhiyun  *		allocate a new set of contiguous blocks large enough
952*4882a593Smuzhiyun  *		to cover the existing allocation plus the additional
953*4882a593Smuzhiyun  *		number of blocks required.
954*4882a593Smuzhiyun  *
955*4882a593Smuzhiyun  * PARAMETERS:
956*4882a593Smuzhiyun  *	ip	    -  pointer to in-core inode requiring allocation.
957*4882a593Smuzhiyun  *	blkno	    -  starting block of the current allocation.
958*4882a593Smuzhiyun  *	nblocks	    -  number of contiguous blocks within the current
959*4882a593Smuzhiyun  *		       allocation.
960*4882a593Smuzhiyun  *	addnblocks  -  number of blocks to add to the allocation.
961*4882a593Smuzhiyun  *	results	-      on successful return, set to the starting block number
962*4882a593Smuzhiyun  *		       of the existing allocation if the existing allocation
963*4882a593Smuzhiyun  *		       was extended in place or to a newly allocated contiguous
964*4882a593Smuzhiyun  *		       range if the existing allocation could not be extended
965*4882a593Smuzhiyun  *		       in place.
966*4882a593Smuzhiyun  *
967*4882a593Smuzhiyun  * RETURN VALUES:
968*4882a593Smuzhiyun  *	0	- success
969*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
970*4882a593Smuzhiyun  *	-EIO	- i/o error
971*4882a593Smuzhiyun  */
972*4882a593Smuzhiyun int
dbReAlloc(struct inode * ip,s64 blkno,s64 nblocks,s64 addnblocks,s64 * results)973*4882a593Smuzhiyun dbReAlloc(struct inode *ip,
974*4882a593Smuzhiyun 	  s64 blkno, s64 nblocks, s64 addnblocks, s64 * results)
975*4882a593Smuzhiyun {
976*4882a593Smuzhiyun 	int rc;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	/* try to extend the allocation in place.
979*4882a593Smuzhiyun 	 */
980*4882a593Smuzhiyun 	if ((rc = dbExtend(ip, blkno, nblocks, addnblocks)) == 0) {
981*4882a593Smuzhiyun 		*results = blkno;
982*4882a593Smuzhiyun 		return (0);
983*4882a593Smuzhiyun 	} else {
984*4882a593Smuzhiyun 		if (rc != -ENOSPC)
985*4882a593Smuzhiyun 			return (rc);
986*4882a593Smuzhiyun 	}
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	/* could not extend the allocation in place, so allocate a
989*4882a593Smuzhiyun 	 * new set of blocks for the entire request (i.e. try to get
990*4882a593Smuzhiyun 	 * a range of contiguous blocks large enough to cover the
991*4882a593Smuzhiyun 	 * existing allocation plus the additional blocks.)
992*4882a593Smuzhiyun 	 */
993*4882a593Smuzhiyun 	return (dbAlloc
994*4882a593Smuzhiyun 		(ip, blkno + nblocks - 1, addnblocks + nblocks, results));
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun /*
999*4882a593Smuzhiyun  * NAME:	dbExtend()
1000*4882a593Smuzhiyun  *
1001*4882a593Smuzhiyun  * FUNCTION:	attempt to extend a current allocation by a specified
1002*4882a593Smuzhiyun  *		number of blocks.
1003*4882a593Smuzhiyun  *
1004*4882a593Smuzhiyun  *		this routine attempts to satisfy the allocation request
1005*4882a593Smuzhiyun  *		by first trying to extend the existing allocation in
1006*4882a593Smuzhiyun  *		place by allocating the additional blocks as the blocks
1007*4882a593Smuzhiyun  *		immediately following the current allocation.
1008*4882a593Smuzhiyun  *
1009*4882a593Smuzhiyun  * PARAMETERS:
1010*4882a593Smuzhiyun  *	ip	    -  pointer to in-core inode requiring allocation.
1011*4882a593Smuzhiyun  *	blkno	    -  starting block of the current allocation.
1012*4882a593Smuzhiyun  *	nblocks	    -  number of contiguous blocks within the current
1013*4882a593Smuzhiyun  *		       allocation.
1014*4882a593Smuzhiyun  *	addnblocks  -  number of blocks to add to the allocation.
1015*4882a593Smuzhiyun  *
1016*4882a593Smuzhiyun  * RETURN VALUES:
1017*4882a593Smuzhiyun  *	0	- success
1018*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
1019*4882a593Smuzhiyun  *	-EIO	- i/o error
1020*4882a593Smuzhiyun  */
dbExtend(struct inode * ip,s64 blkno,s64 nblocks,s64 addnblocks)1021*4882a593Smuzhiyun static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks)
1022*4882a593Smuzhiyun {
1023*4882a593Smuzhiyun 	struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1024*4882a593Smuzhiyun 	s64 lblkno, lastblkno, extblkno;
1025*4882a593Smuzhiyun 	uint rel_block;
1026*4882a593Smuzhiyun 	struct metapage *mp;
1027*4882a593Smuzhiyun 	struct dmap *dp;
1028*4882a593Smuzhiyun 	int rc;
1029*4882a593Smuzhiyun 	struct inode *ipbmap = sbi->ipbmap;
1030*4882a593Smuzhiyun 	struct bmap *bmp;
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun 	/*
1033*4882a593Smuzhiyun 	 * We don't want a non-aligned extent to cross a page boundary
1034*4882a593Smuzhiyun 	 */
1035*4882a593Smuzhiyun 	if (((rel_block = blkno & (sbi->nbperpage - 1))) &&
1036*4882a593Smuzhiyun 	    (rel_block + nblocks + addnblocks > sbi->nbperpage))
1037*4882a593Smuzhiyun 		return -ENOSPC;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	/* get the last block of the current allocation */
1040*4882a593Smuzhiyun 	lastblkno = blkno + nblocks - 1;
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	/* determine the block number of the block following
1043*4882a593Smuzhiyun 	 * the existing allocation.
1044*4882a593Smuzhiyun 	 */
1045*4882a593Smuzhiyun 	extblkno = lastblkno + 1;
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	/* better be within the file system */
1050*4882a593Smuzhiyun 	bmp = sbi->bmap;
1051*4882a593Smuzhiyun 	if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) {
1052*4882a593Smuzhiyun 		IREAD_UNLOCK(ipbmap);
1053*4882a593Smuzhiyun 		jfs_error(ip->i_sb, "the block is outside the filesystem\n");
1054*4882a593Smuzhiyun 		return -EIO;
1055*4882a593Smuzhiyun 	}
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	/* we'll attempt to extend the current allocation in place by
1058*4882a593Smuzhiyun 	 * allocating the additional blocks as the blocks immediately
1059*4882a593Smuzhiyun 	 * following the current allocation.  we only try to extend the
1060*4882a593Smuzhiyun 	 * current allocation in place if the number of additional blocks
1061*4882a593Smuzhiyun 	 * can fit into a dmap, the last block of the current allocation
1062*4882a593Smuzhiyun 	 * is not the last block of the file system, and the start of the
1063*4882a593Smuzhiyun 	 * inplace extension is not on an allocation group boundary.
1064*4882a593Smuzhiyun 	 */
1065*4882a593Smuzhiyun 	if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize ||
1066*4882a593Smuzhiyun 	    (extblkno & (bmp->db_agsize - 1)) == 0) {
1067*4882a593Smuzhiyun 		IREAD_UNLOCK(ipbmap);
1068*4882a593Smuzhiyun 		return -ENOSPC;
1069*4882a593Smuzhiyun 	}
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	/* get the buffer for the dmap containing the first block
1072*4882a593Smuzhiyun 	 * of the extension.
1073*4882a593Smuzhiyun 	 */
1074*4882a593Smuzhiyun 	lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage);
1075*4882a593Smuzhiyun 	mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
1076*4882a593Smuzhiyun 	if (mp == NULL) {
1077*4882a593Smuzhiyun 		IREAD_UNLOCK(ipbmap);
1078*4882a593Smuzhiyun 		return -EIO;
1079*4882a593Smuzhiyun 	}
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	dp = (struct dmap *) mp->data;
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	/* try to allocate the blocks immediately following the
1084*4882a593Smuzhiyun 	 * current allocation.
1085*4882a593Smuzhiyun 	 */
1086*4882a593Smuzhiyun 	rc = dbAllocNext(bmp, dp, extblkno, (int) addnblocks);
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	IREAD_UNLOCK(ipbmap);
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	/* were we successful ? */
1091*4882a593Smuzhiyun 	if (rc == 0)
1092*4882a593Smuzhiyun 		write_metapage(mp);
1093*4882a593Smuzhiyun 	else
1094*4882a593Smuzhiyun 		/* we were not successful */
1095*4882a593Smuzhiyun 		release_metapage(mp);
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	return (rc);
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun /*
1102*4882a593Smuzhiyun  * NAME:	dbAllocNext()
1103*4882a593Smuzhiyun  *
1104*4882a593Smuzhiyun  * FUNCTION:	attempt to allocate the blocks of the specified block
1105*4882a593Smuzhiyun  *		range within a dmap.
1106*4882a593Smuzhiyun  *
1107*4882a593Smuzhiyun  * PARAMETERS:
1108*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
1109*4882a593Smuzhiyun  *	dp	-  pointer to dmap.
1110*4882a593Smuzhiyun  *	blkno	-  starting block number of the range.
1111*4882a593Smuzhiyun  *	nblocks	-  number of contiguous free blocks of the range.
1112*4882a593Smuzhiyun  *
1113*4882a593Smuzhiyun  * RETURN VALUES:
1114*4882a593Smuzhiyun  *	0	- success
1115*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
1116*4882a593Smuzhiyun  *	-EIO	- i/o error
1117*4882a593Smuzhiyun  *
1118*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1119*4882a593Smuzhiyun  */
dbAllocNext(struct bmap * bmp,struct dmap * dp,s64 blkno,int nblocks)1120*4882a593Smuzhiyun static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
1121*4882a593Smuzhiyun 		       int nblocks)
1122*4882a593Smuzhiyun {
1123*4882a593Smuzhiyun 	int dbitno, word, rembits, nb, nwords, wbitno, nw;
1124*4882a593Smuzhiyun 	int l2size;
1125*4882a593Smuzhiyun 	s8 *leaf;
1126*4882a593Smuzhiyun 	u32 mask;
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1129*4882a593Smuzhiyun 		jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n");
1130*4882a593Smuzhiyun 		return -EIO;
1131*4882a593Smuzhiyun 	}
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	/* pick up a pointer to the leaves of the dmap tree.
1134*4882a593Smuzhiyun 	 */
1135*4882a593Smuzhiyun 	leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	/* determine the bit number and word within the dmap of the
1138*4882a593Smuzhiyun 	 * starting block.
1139*4882a593Smuzhiyun 	 */
1140*4882a593Smuzhiyun 	dbitno = blkno & (BPERDMAP - 1);
1141*4882a593Smuzhiyun 	word = dbitno >> L2DBWORD;
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	/* check if the specified block range is contained within
1144*4882a593Smuzhiyun 	 * this dmap.
1145*4882a593Smuzhiyun 	 */
1146*4882a593Smuzhiyun 	if (dbitno + nblocks > BPERDMAP)
1147*4882a593Smuzhiyun 		return -ENOSPC;
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun 	/* check if the starting leaf indicates that anything
1150*4882a593Smuzhiyun 	 * is free.
1151*4882a593Smuzhiyun 	 */
1152*4882a593Smuzhiyun 	if (leaf[word] == NOFREE)
1153*4882a593Smuzhiyun 		return -ENOSPC;
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	/* check the dmaps words corresponding to block range to see
1156*4882a593Smuzhiyun 	 * if the block range is free.  not all bits of the first and
1157*4882a593Smuzhiyun 	 * last words may be contained within the block range.  if this
1158*4882a593Smuzhiyun 	 * is the case, we'll work against those words (i.e. partial first
1159*4882a593Smuzhiyun 	 * and/or last) on an individual basis (a single pass) and examine
1160*4882a593Smuzhiyun 	 * the actual bits to determine if they are free.  a single pass
1161*4882a593Smuzhiyun 	 * will be used for all dmap words fully contained within the
1162*4882a593Smuzhiyun 	 * specified range.  within this pass, the leaves of the dmap
1163*4882a593Smuzhiyun 	 * tree will be examined to determine if the blocks are free. a
1164*4882a593Smuzhiyun 	 * single leaf may describe the free space of multiple dmap
1165*4882a593Smuzhiyun 	 * words, so we may visit only a subset of the actual leaves
1166*4882a593Smuzhiyun 	 * corresponding to the dmap words of the block range.
1167*4882a593Smuzhiyun 	 */
1168*4882a593Smuzhiyun 	for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
1169*4882a593Smuzhiyun 		/* determine the bit number within the word and
1170*4882a593Smuzhiyun 		 * the number of bits within the word.
1171*4882a593Smuzhiyun 		 */
1172*4882a593Smuzhiyun 		wbitno = dbitno & (DBWORD - 1);
1173*4882a593Smuzhiyun 		nb = min(rembits, DBWORD - wbitno);
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 		/* check if only part of the word is to be examined.
1176*4882a593Smuzhiyun 		 */
1177*4882a593Smuzhiyun 		if (nb < DBWORD) {
1178*4882a593Smuzhiyun 			/* check if the bits are free.
1179*4882a593Smuzhiyun 			 */
1180*4882a593Smuzhiyun 			mask = (ONES << (DBWORD - nb) >> wbitno);
1181*4882a593Smuzhiyun 			if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask)
1182*4882a593Smuzhiyun 				return -ENOSPC;
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 			word += 1;
1185*4882a593Smuzhiyun 		} else {
1186*4882a593Smuzhiyun 			/* one or more dmap words are fully contained
1187*4882a593Smuzhiyun 			 * within the block range.  determine how many
1188*4882a593Smuzhiyun 			 * words and how many bits.
1189*4882a593Smuzhiyun 			 */
1190*4882a593Smuzhiyun 			nwords = rembits >> L2DBWORD;
1191*4882a593Smuzhiyun 			nb = nwords << L2DBWORD;
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 			/* now examine the appropriate leaves to determine
1194*4882a593Smuzhiyun 			 * if the blocks are free.
1195*4882a593Smuzhiyun 			 */
1196*4882a593Smuzhiyun 			while (nwords > 0) {
1197*4882a593Smuzhiyun 				/* does the leaf describe any free space ?
1198*4882a593Smuzhiyun 				 */
1199*4882a593Smuzhiyun 				if (leaf[word] < BUDMIN)
1200*4882a593Smuzhiyun 					return -ENOSPC;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 				/* determine the l2 number of bits provided
1203*4882a593Smuzhiyun 				 * by this leaf.
1204*4882a593Smuzhiyun 				 */
1205*4882a593Smuzhiyun 				l2size =
1206*4882a593Smuzhiyun 				    min_t(int, leaf[word], NLSTOL2BSZ(nwords));
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun 				/* determine how many words were handled.
1209*4882a593Smuzhiyun 				 */
1210*4882a593Smuzhiyun 				nw = BUDSIZE(l2size, BUDMIN);
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 				nwords -= nw;
1213*4882a593Smuzhiyun 				word += nw;
1214*4882a593Smuzhiyun 			}
1215*4882a593Smuzhiyun 		}
1216*4882a593Smuzhiyun 	}
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	/* allocate the blocks.
1219*4882a593Smuzhiyun 	 */
1220*4882a593Smuzhiyun 	return (dbAllocDmap(bmp, dp, blkno, nblocks));
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun /*
1225*4882a593Smuzhiyun  * NAME:	dbAllocNear()
1226*4882a593Smuzhiyun  *
1227*4882a593Smuzhiyun  * FUNCTION:	attempt to allocate a number of contiguous free blocks near
1228*4882a593Smuzhiyun  *		a specified block (hint) within a dmap.
1229*4882a593Smuzhiyun  *
1230*4882a593Smuzhiyun  *		starting with the dmap leaf that covers the hint, we'll
1231*4882a593Smuzhiyun  *		check the next four contiguous leaves for sufficient free
1232*4882a593Smuzhiyun  *		space.  if sufficient free space is found, we'll allocate
1233*4882a593Smuzhiyun  *		the desired free space.
1234*4882a593Smuzhiyun  *
1235*4882a593Smuzhiyun  * PARAMETERS:
1236*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
1237*4882a593Smuzhiyun  *	dp	-  pointer to dmap.
1238*4882a593Smuzhiyun  *	blkno	-  block number to allocate near.
1239*4882a593Smuzhiyun  *	nblocks	-  actual number of contiguous free blocks desired.
1240*4882a593Smuzhiyun  *	l2nb	-  log2 number of contiguous free blocks desired.
1241*4882a593Smuzhiyun  *	results	-  on successful return, set to the starting block number
1242*4882a593Smuzhiyun  *		   of the newly allocated range.
1243*4882a593Smuzhiyun  *
1244*4882a593Smuzhiyun  * RETURN VALUES:
1245*4882a593Smuzhiyun  *	0	- success
1246*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
1247*4882a593Smuzhiyun  *	-EIO	- i/o error
1248*4882a593Smuzhiyun  *
1249*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1250*4882a593Smuzhiyun  */
1251*4882a593Smuzhiyun static int
dbAllocNear(struct bmap * bmp,struct dmap * dp,s64 blkno,int nblocks,int l2nb,s64 * results)1252*4882a593Smuzhiyun dbAllocNear(struct bmap * bmp,
1253*4882a593Smuzhiyun 	    struct dmap * dp, s64 blkno, int nblocks, int l2nb, s64 * results)
1254*4882a593Smuzhiyun {
1255*4882a593Smuzhiyun 	int word, lword, rc;
1256*4882a593Smuzhiyun 	s8 *leaf;
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun 	if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1259*4882a593Smuzhiyun 		jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n");
1260*4882a593Smuzhiyun 		return -EIO;
1261*4882a593Smuzhiyun 	}
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 	leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	/* determine the word within the dmap that holds the hint
1266*4882a593Smuzhiyun 	 * (i.e. blkno).  also, determine the last word in the dmap
1267*4882a593Smuzhiyun 	 * that we'll include in our examination.
1268*4882a593Smuzhiyun 	 */
1269*4882a593Smuzhiyun 	word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
1270*4882a593Smuzhiyun 	lword = min(word + 4, LPERDMAP);
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	/* examine the leaves for sufficient free space.
1273*4882a593Smuzhiyun 	 */
1274*4882a593Smuzhiyun 	for (; word < lword; word++) {
1275*4882a593Smuzhiyun 		/* does the leaf describe sufficient free space ?
1276*4882a593Smuzhiyun 		 */
1277*4882a593Smuzhiyun 		if (leaf[word] < l2nb)
1278*4882a593Smuzhiyun 			continue;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 		/* determine the block number within the file system
1281*4882a593Smuzhiyun 		 * of the first block described by this dmap word.
1282*4882a593Smuzhiyun 		 */
1283*4882a593Smuzhiyun 		blkno = le64_to_cpu(dp->start) + (word << L2DBWORD);
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 		/* if not all bits of the dmap word are free, get the
1286*4882a593Smuzhiyun 		 * starting bit number within the dmap word of the required
1287*4882a593Smuzhiyun 		 * string of free bits and adjust the block number with the
1288*4882a593Smuzhiyun 		 * value.
1289*4882a593Smuzhiyun 		 */
1290*4882a593Smuzhiyun 		if (leaf[word] < BUDMIN)
1291*4882a593Smuzhiyun 			blkno +=
1292*4882a593Smuzhiyun 			    dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb);
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 		/* allocate the blocks.
1295*4882a593Smuzhiyun 		 */
1296*4882a593Smuzhiyun 		if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
1297*4882a593Smuzhiyun 			*results = blkno;
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 		return (rc);
1300*4882a593Smuzhiyun 	}
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	return -ENOSPC;
1303*4882a593Smuzhiyun }
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun /*
1307*4882a593Smuzhiyun  * NAME:	dbAllocAG()
1308*4882a593Smuzhiyun  *
1309*4882a593Smuzhiyun  * FUNCTION:	attempt to allocate the specified number of contiguous
1310*4882a593Smuzhiyun  *		free blocks within the specified allocation group.
1311*4882a593Smuzhiyun  *
1312*4882a593Smuzhiyun  *		unless the allocation group size is equal to the number
1313*4882a593Smuzhiyun  *		of blocks per dmap, the dmap control pages will be used to
1314*4882a593Smuzhiyun  *		find the required free space, if available.  we start the
1315*4882a593Smuzhiyun  *		search at the highest dmap control page level which
1316*4882a593Smuzhiyun  *		distinctly describes the allocation group's free space
1317*4882a593Smuzhiyun  *		(i.e. the highest level at which the allocation group's
1318*4882a593Smuzhiyun  *		free space is not mixed in with that of any other group).
1319*4882a593Smuzhiyun  *		in addition, we start the search within this level at a
1320*4882a593Smuzhiyun  *		height of the dmapctl dmtree at which the nodes distinctly
1321*4882a593Smuzhiyun  *		describe the allocation group's free space.  at this height,
1322*4882a593Smuzhiyun  *		the allocation group's free space may be represented by 1
1323*4882a593Smuzhiyun  *		or two sub-trees, depending on the allocation group size.
1324*4882a593Smuzhiyun  *		we search the top nodes of these subtrees left to right for
1325*4882a593Smuzhiyun  *		sufficient free space.  if sufficient free space is found,
1326*4882a593Smuzhiyun  *		the subtree is searched to find the leftmost leaf that
1327*4882a593Smuzhiyun  *		has free space.  once we have made it to the leaf, we
1328*4882a593Smuzhiyun  *		move the search to the next lower level dmap control page
1329*4882a593Smuzhiyun  *		corresponding to this leaf.  we continue down the dmap control
1330*4882a593Smuzhiyun  *		pages until we find the dmap that contains or starts the
1331*4882a593Smuzhiyun  *		sufficient free space and we allocate at this dmap.
1332*4882a593Smuzhiyun  *
1333*4882a593Smuzhiyun  *		if the allocation group size is equal to the dmap size,
1334*4882a593Smuzhiyun  *		we'll start at the dmap corresponding to the allocation
1335*4882a593Smuzhiyun  *		group and attempt the allocation at this level.
1336*4882a593Smuzhiyun  *
1337*4882a593Smuzhiyun  *		the dmap control page search is also not performed if the
1338*4882a593Smuzhiyun  *		allocation group is completely free and we go to the first
1339*4882a593Smuzhiyun  *		dmap of the allocation group to do the allocation.  this is
1340*4882a593Smuzhiyun  *		done because the allocation group may be part (not the first
1341*4882a593Smuzhiyun  *		part) of a larger binary buddy system, causing the dmap
1342*4882a593Smuzhiyun  *		control pages to indicate no free space (NOFREE) within
1343*4882a593Smuzhiyun  *		the allocation group.
1344*4882a593Smuzhiyun  *
1345*4882a593Smuzhiyun  * PARAMETERS:
1346*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
1347*4882a593Smuzhiyun  *	agno	- allocation group number.
1348*4882a593Smuzhiyun  *	nblocks	-  actual number of contiguous free blocks desired.
1349*4882a593Smuzhiyun  *	l2nb	-  log2 number of contiguous free blocks desired.
1350*4882a593Smuzhiyun  *	results	-  on successful return, set to the starting block number
1351*4882a593Smuzhiyun  *		   of the newly allocated range.
1352*4882a593Smuzhiyun  *
1353*4882a593Smuzhiyun  * RETURN VALUES:
1354*4882a593Smuzhiyun  *	0	- success
1355*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
1356*4882a593Smuzhiyun  *	-EIO	- i/o error
1357*4882a593Smuzhiyun  *
1358*4882a593Smuzhiyun  * note: IWRITE_LOCK(ipmap) held on entry/exit;
1359*4882a593Smuzhiyun  */
1360*4882a593Smuzhiyun static int
dbAllocAG(struct bmap * bmp,int agno,s64 nblocks,int l2nb,s64 * results)1361*4882a593Smuzhiyun dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb, s64 * results)
1362*4882a593Smuzhiyun {
1363*4882a593Smuzhiyun 	struct metapage *mp;
1364*4882a593Smuzhiyun 	struct dmapctl *dcp;
1365*4882a593Smuzhiyun 	int rc, ti, i, k, m, n, agperlev;
1366*4882a593Smuzhiyun 	s64 blkno, lblkno;
1367*4882a593Smuzhiyun 	int budmin;
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	/* allocation request should not be for more than the
1370*4882a593Smuzhiyun 	 * allocation group size.
1371*4882a593Smuzhiyun 	 */
1372*4882a593Smuzhiyun 	if (l2nb > bmp->db_agl2size) {
1373*4882a593Smuzhiyun 		jfs_error(bmp->db_ipbmap->i_sb,
1374*4882a593Smuzhiyun 			  "allocation request is larger than the allocation group size\n");
1375*4882a593Smuzhiyun 		return -EIO;
1376*4882a593Smuzhiyun 	}
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 	/* determine the starting block number of the allocation
1379*4882a593Smuzhiyun 	 * group.
1380*4882a593Smuzhiyun 	 */
1381*4882a593Smuzhiyun 	blkno = (s64) agno << bmp->db_agl2size;
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	/* check if the allocation group size is the minimum allocation
1384*4882a593Smuzhiyun 	 * group size or if the allocation group is completely free. if
1385*4882a593Smuzhiyun 	 * the allocation group size is the minimum size of BPERDMAP (i.e.
1386*4882a593Smuzhiyun 	 * 1 dmap), there is no need to search the dmap control page (below)
1387*4882a593Smuzhiyun 	 * that fully describes the allocation group since the allocation
1388*4882a593Smuzhiyun 	 * group is already fully described by a dmap.  in this case, we
1389*4882a593Smuzhiyun 	 * just call dbAllocCtl() to search the dmap tree and allocate the
1390*4882a593Smuzhiyun 	 * required space if available.
1391*4882a593Smuzhiyun 	 *
1392*4882a593Smuzhiyun 	 * if the allocation group is completely free, dbAllocCtl() is
1393*4882a593Smuzhiyun 	 * also called to allocate the required space.  this is done for
1394*4882a593Smuzhiyun 	 * two reasons.  first, it makes no sense searching the dmap control
1395*4882a593Smuzhiyun 	 * pages for free space when we know that free space exists.  second,
1396*4882a593Smuzhiyun 	 * the dmap control pages may indicate that the allocation group
1397*4882a593Smuzhiyun 	 * has no free space if the allocation group is part (not the first
1398*4882a593Smuzhiyun 	 * part) of a larger binary buddy system.
1399*4882a593Smuzhiyun 	 */
1400*4882a593Smuzhiyun 	if (bmp->db_agsize == BPERDMAP
1401*4882a593Smuzhiyun 	    || bmp->db_agfree[agno] == bmp->db_agsize) {
1402*4882a593Smuzhiyun 		rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1403*4882a593Smuzhiyun 		if ((rc == -ENOSPC) &&
1404*4882a593Smuzhiyun 		    (bmp->db_agfree[agno] == bmp->db_agsize)) {
1405*4882a593Smuzhiyun 			printk(KERN_ERR "blkno = %Lx, blocks = %Lx\n",
1406*4882a593Smuzhiyun 			       (unsigned long long) blkno,
1407*4882a593Smuzhiyun 			       (unsigned long long) nblocks);
1408*4882a593Smuzhiyun 			jfs_error(bmp->db_ipbmap->i_sb,
1409*4882a593Smuzhiyun 				  "dbAllocCtl failed in free AG\n");
1410*4882a593Smuzhiyun 		}
1411*4882a593Smuzhiyun 		return (rc);
1412*4882a593Smuzhiyun 	}
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun 	/* the buffer for the dmap control page that fully describes the
1415*4882a593Smuzhiyun 	 * allocation group.
1416*4882a593Smuzhiyun 	 */
1417*4882a593Smuzhiyun 	lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel);
1418*4882a593Smuzhiyun 	mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1419*4882a593Smuzhiyun 	if (mp == NULL)
1420*4882a593Smuzhiyun 		return -EIO;
1421*4882a593Smuzhiyun 	dcp = (struct dmapctl *) mp->data;
1422*4882a593Smuzhiyun 	budmin = dcp->budmin;
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1425*4882a593Smuzhiyun 		jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n");
1426*4882a593Smuzhiyun 		release_metapage(mp);
1427*4882a593Smuzhiyun 		return -EIO;
1428*4882a593Smuzhiyun 	}
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	/* search the subtree(s) of the dmap control page that describes
1431*4882a593Smuzhiyun 	 * the allocation group, looking for sufficient free space.  to begin,
1432*4882a593Smuzhiyun 	 * determine how many allocation groups are represented in a dmap
1433*4882a593Smuzhiyun 	 * control page at the control page level (i.e. L0, L1, L2) that
1434*4882a593Smuzhiyun 	 * fully describes an allocation group. next, determine the starting
1435*4882a593Smuzhiyun 	 * tree index of this allocation group within the control page.
1436*4882a593Smuzhiyun 	 */
1437*4882a593Smuzhiyun 	agperlev =
1438*4882a593Smuzhiyun 	    (1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth;
1439*4882a593Smuzhiyun 	ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1));
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	/* dmap control page trees fan-out by 4 and a single allocation
1442*4882a593Smuzhiyun 	 * group may be described by 1 or 2 subtrees within the ag level
1443*4882a593Smuzhiyun 	 * dmap control page, depending upon the ag size. examine the ag's
1444*4882a593Smuzhiyun 	 * subtrees for sufficient free space, starting with the leftmost
1445*4882a593Smuzhiyun 	 * subtree.
1446*4882a593Smuzhiyun 	 */
1447*4882a593Smuzhiyun 	for (i = 0; i < bmp->db_agwidth; i++, ti++) {
1448*4882a593Smuzhiyun 		/* is there sufficient free space ?
1449*4882a593Smuzhiyun 		 */
1450*4882a593Smuzhiyun 		if (l2nb > dcp->stree[ti])
1451*4882a593Smuzhiyun 			continue;
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 		/* sufficient free space found in a subtree. now search down
1454*4882a593Smuzhiyun 		 * the subtree to find the leftmost leaf that describes this
1455*4882a593Smuzhiyun 		 * free space.
1456*4882a593Smuzhiyun 		 */
1457*4882a593Smuzhiyun 		for (k = bmp->db_agheight; k > 0; k--) {
1458*4882a593Smuzhiyun 			for (n = 0, m = (ti << 2) + 1; n < 4; n++) {
1459*4882a593Smuzhiyun 				if (l2nb <= dcp->stree[m + n]) {
1460*4882a593Smuzhiyun 					ti = m + n;
1461*4882a593Smuzhiyun 					break;
1462*4882a593Smuzhiyun 				}
1463*4882a593Smuzhiyun 			}
1464*4882a593Smuzhiyun 			if (n == 4) {
1465*4882a593Smuzhiyun 				jfs_error(bmp->db_ipbmap->i_sb,
1466*4882a593Smuzhiyun 					  "failed descending stree\n");
1467*4882a593Smuzhiyun 				release_metapage(mp);
1468*4882a593Smuzhiyun 				return -EIO;
1469*4882a593Smuzhiyun 			}
1470*4882a593Smuzhiyun 		}
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 		/* determine the block number within the file system
1473*4882a593Smuzhiyun 		 * that corresponds to this leaf.
1474*4882a593Smuzhiyun 		 */
1475*4882a593Smuzhiyun 		if (bmp->db_aglevel == 2)
1476*4882a593Smuzhiyun 			blkno = 0;
1477*4882a593Smuzhiyun 		else if (bmp->db_aglevel == 1)
1478*4882a593Smuzhiyun 			blkno &= ~(MAXL1SIZE - 1);
1479*4882a593Smuzhiyun 		else		/* bmp->db_aglevel == 0 */
1480*4882a593Smuzhiyun 			blkno &= ~(MAXL0SIZE - 1);
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 		blkno +=
1483*4882a593Smuzhiyun 		    ((s64) (ti - le32_to_cpu(dcp->leafidx))) << budmin;
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun 		/* release the buffer in preparation for going down
1486*4882a593Smuzhiyun 		 * the next level of dmap control pages.
1487*4882a593Smuzhiyun 		 */
1488*4882a593Smuzhiyun 		release_metapage(mp);
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 		/* check if we need to continue to search down the lower
1491*4882a593Smuzhiyun 		 * level dmap control pages.  we need to if the number of
1492*4882a593Smuzhiyun 		 * blocks required is less than maximum number of blocks
1493*4882a593Smuzhiyun 		 * described at the next lower level.
1494*4882a593Smuzhiyun 		 */
1495*4882a593Smuzhiyun 		if (l2nb < budmin) {
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 			/* search the lower level dmap control pages to get
1498*4882a593Smuzhiyun 			 * the starting block number of the dmap that
1499*4882a593Smuzhiyun 			 * contains or starts off the free space.
1500*4882a593Smuzhiyun 			 */
1501*4882a593Smuzhiyun 			if ((rc =
1502*4882a593Smuzhiyun 			     dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1,
1503*4882a593Smuzhiyun 				       &blkno))) {
1504*4882a593Smuzhiyun 				if (rc == -ENOSPC) {
1505*4882a593Smuzhiyun 					jfs_error(bmp->db_ipbmap->i_sb,
1506*4882a593Smuzhiyun 						  "control page inconsistent\n");
1507*4882a593Smuzhiyun 					return -EIO;
1508*4882a593Smuzhiyun 				}
1509*4882a593Smuzhiyun 				return (rc);
1510*4882a593Smuzhiyun 			}
1511*4882a593Smuzhiyun 		}
1512*4882a593Smuzhiyun 
1513*4882a593Smuzhiyun 		/* allocate the blocks.
1514*4882a593Smuzhiyun 		 */
1515*4882a593Smuzhiyun 		rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1516*4882a593Smuzhiyun 		if (rc == -ENOSPC) {
1517*4882a593Smuzhiyun 			jfs_error(bmp->db_ipbmap->i_sb,
1518*4882a593Smuzhiyun 				  "unable to allocate blocks\n");
1519*4882a593Smuzhiyun 			rc = -EIO;
1520*4882a593Smuzhiyun 		}
1521*4882a593Smuzhiyun 		return (rc);
1522*4882a593Smuzhiyun 	}
1523*4882a593Smuzhiyun 
1524*4882a593Smuzhiyun 	/* no space in the allocation group.  release the buffer and
1525*4882a593Smuzhiyun 	 * return -ENOSPC.
1526*4882a593Smuzhiyun 	 */
1527*4882a593Smuzhiyun 	release_metapage(mp);
1528*4882a593Smuzhiyun 
1529*4882a593Smuzhiyun 	return -ENOSPC;
1530*4882a593Smuzhiyun }
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun /*
1534*4882a593Smuzhiyun  * NAME:	dbAllocAny()
1535*4882a593Smuzhiyun  *
1536*4882a593Smuzhiyun  * FUNCTION:	attempt to allocate the specified number of contiguous
1537*4882a593Smuzhiyun  *		free blocks anywhere in the file system.
1538*4882a593Smuzhiyun  *
1539*4882a593Smuzhiyun  *		dbAllocAny() attempts to find the sufficient free space by
1540*4882a593Smuzhiyun  *		searching down the dmap control pages, starting with the
1541*4882a593Smuzhiyun  *		highest level (i.e. L0, L1, L2) control page.  if free space
1542*4882a593Smuzhiyun  *		large enough to satisfy the desired free space is found, the
1543*4882a593Smuzhiyun  *		desired free space is allocated.
1544*4882a593Smuzhiyun  *
1545*4882a593Smuzhiyun  * PARAMETERS:
1546*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
1547*4882a593Smuzhiyun  *	nblocks	 -  actual number of contiguous free blocks desired.
1548*4882a593Smuzhiyun  *	l2nb	 -  log2 number of contiguous free blocks desired.
1549*4882a593Smuzhiyun  *	results	-  on successful return, set to the starting block number
1550*4882a593Smuzhiyun  *		   of the newly allocated range.
1551*4882a593Smuzhiyun  *
1552*4882a593Smuzhiyun  * RETURN VALUES:
1553*4882a593Smuzhiyun  *	0	- success
1554*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
1555*4882a593Smuzhiyun  *	-EIO	- i/o error
1556*4882a593Smuzhiyun  *
1557*4882a593Smuzhiyun  * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1558*4882a593Smuzhiyun  */
dbAllocAny(struct bmap * bmp,s64 nblocks,int l2nb,s64 * results)1559*4882a593Smuzhiyun static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results)
1560*4882a593Smuzhiyun {
1561*4882a593Smuzhiyun 	int rc;
1562*4882a593Smuzhiyun 	s64 blkno = 0;
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun 	/* starting with the top level dmap control page, search
1565*4882a593Smuzhiyun 	 * down the dmap control levels for sufficient free space.
1566*4882a593Smuzhiyun 	 * if free space is found, dbFindCtl() returns the starting
1567*4882a593Smuzhiyun 	 * block number of the dmap that contains or starts off the
1568*4882a593Smuzhiyun 	 * range of free space.
1569*4882a593Smuzhiyun 	 */
1570*4882a593Smuzhiyun 	if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno)))
1571*4882a593Smuzhiyun 		return (rc);
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun 	/* allocate the blocks.
1574*4882a593Smuzhiyun 	 */
1575*4882a593Smuzhiyun 	rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1576*4882a593Smuzhiyun 	if (rc == -ENOSPC) {
1577*4882a593Smuzhiyun 		jfs_error(bmp->db_ipbmap->i_sb, "unable to allocate blocks\n");
1578*4882a593Smuzhiyun 		return -EIO;
1579*4882a593Smuzhiyun 	}
1580*4882a593Smuzhiyun 	return (rc);
1581*4882a593Smuzhiyun }
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun /*
1585*4882a593Smuzhiyun  * NAME:	dbDiscardAG()
1586*4882a593Smuzhiyun  *
1587*4882a593Smuzhiyun  * FUNCTION:	attempt to discard (TRIM) all free blocks of specific AG
1588*4882a593Smuzhiyun  *
1589*4882a593Smuzhiyun  *		algorithm:
1590*4882a593Smuzhiyun  *		1) allocate blocks, as large as possible and save them
1591*4882a593Smuzhiyun  *		   while holding IWRITE_LOCK on ipbmap
1592*4882a593Smuzhiyun  *		2) trim all these saved block/length values
1593*4882a593Smuzhiyun  *		3) mark the blocks free again
1594*4882a593Smuzhiyun  *
1595*4882a593Smuzhiyun  *		benefit:
1596*4882a593Smuzhiyun  *		- we work only on one ag at some time, minimizing how long we
1597*4882a593Smuzhiyun  *		  need to lock ipbmap
1598*4882a593Smuzhiyun  *		- reading / writing the fs is possible most time, even on
1599*4882a593Smuzhiyun  *		  trimming
1600*4882a593Smuzhiyun  *
1601*4882a593Smuzhiyun  *		downside:
1602*4882a593Smuzhiyun  *		- we write two times to the dmapctl and dmap pages
1603*4882a593Smuzhiyun  *		- but for me, this seems the best way, better ideas?
1604*4882a593Smuzhiyun  *		/TR 2012
1605*4882a593Smuzhiyun  *
1606*4882a593Smuzhiyun  * PARAMETERS:
1607*4882a593Smuzhiyun  *	ip	- pointer to in-core inode
1608*4882a593Smuzhiyun  *	agno	- ag to trim
1609*4882a593Smuzhiyun  *	minlen	- minimum value of contiguous blocks
1610*4882a593Smuzhiyun  *
1611*4882a593Smuzhiyun  * RETURN VALUES:
1612*4882a593Smuzhiyun  *	s64	- actual number of blocks trimmed
1613*4882a593Smuzhiyun  */
dbDiscardAG(struct inode * ip,int agno,s64 minlen)1614*4882a593Smuzhiyun s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen)
1615*4882a593Smuzhiyun {
1616*4882a593Smuzhiyun 	struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
1617*4882a593Smuzhiyun 	struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
1618*4882a593Smuzhiyun 	s64 nblocks, blkno;
1619*4882a593Smuzhiyun 	u64 trimmed = 0;
1620*4882a593Smuzhiyun 	int rc, l2nb;
1621*4882a593Smuzhiyun 	struct super_block *sb = ipbmap->i_sb;
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun 	struct range2trim {
1624*4882a593Smuzhiyun 		u64 blkno;
1625*4882a593Smuzhiyun 		u64 nblocks;
1626*4882a593Smuzhiyun 	} *totrim, *tt;
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun 	/* max blkno / nblocks pairs to trim */
1629*4882a593Smuzhiyun 	int count = 0, range_cnt;
1630*4882a593Smuzhiyun 	u64 max_ranges;
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun 	/* prevent others from writing new stuff here, while trimming */
1633*4882a593Smuzhiyun 	IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 	nblocks = bmp->db_agfree[agno];
1636*4882a593Smuzhiyun 	max_ranges = nblocks;
1637*4882a593Smuzhiyun 	do_div(max_ranges, minlen);
1638*4882a593Smuzhiyun 	range_cnt = min_t(u64, max_ranges + 1, 32 * 1024);
1639*4882a593Smuzhiyun 	totrim = kmalloc_array(range_cnt, sizeof(struct range2trim), GFP_NOFS);
1640*4882a593Smuzhiyun 	if (totrim == NULL) {
1641*4882a593Smuzhiyun 		jfs_error(bmp->db_ipbmap->i_sb, "no memory for trim array\n");
1642*4882a593Smuzhiyun 		IWRITE_UNLOCK(ipbmap);
1643*4882a593Smuzhiyun 		return 0;
1644*4882a593Smuzhiyun 	}
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 	tt = totrim;
1647*4882a593Smuzhiyun 	while (nblocks >= minlen) {
1648*4882a593Smuzhiyun 		l2nb = BLKSTOL2(nblocks);
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun 		/* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */
1651*4882a593Smuzhiyun 		rc = dbAllocAG(bmp, agno, nblocks, l2nb, &blkno);
1652*4882a593Smuzhiyun 		if (rc == 0) {
1653*4882a593Smuzhiyun 			tt->blkno = blkno;
1654*4882a593Smuzhiyun 			tt->nblocks = nblocks;
1655*4882a593Smuzhiyun 			tt++; count++;
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun 			/* the whole ag is free, trim now */
1658*4882a593Smuzhiyun 			if (bmp->db_agfree[agno] == 0)
1659*4882a593Smuzhiyun 				break;
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 			/* give a hint for the next while */
1662*4882a593Smuzhiyun 			nblocks = bmp->db_agfree[agno];
1663*4882a593Smuzhiyun 			continue;
1664*4882a593Smuzhiyun 		} else if (rc == -ENOSPC) {
1665*4882a593Smuzhiyun 			/* search for next smaller log2 block */
1666*4882a593Smuzhiyun 			l2nb = BLKSTOL2(nblocks) - 1;
1667*4882a593Smuzhiyun 			nblocks = 1LL << l2nb;
1668*4882a593Smuzhiyun 		} else {
1669*4882a593Smuzhiyun 			/* Trim any already allocated blocks */
1670*4882a593Smuzhiyun 			jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n");
1671*4882a593Smuzhiyun 			break;
1672*4882a593Smuzhiyun 		}
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 		/* check, if our trim array is full */
1675*4882a593Smuzhiyun 		if (unlikely(count >= range_cnt - 1))
1676*4882a593Smuzhiyun 			break;
1677*4882a593Smuzhiyun 	}
1678*4882a593Smuzhiyun 	IWRITE_UNLOCK(ipbmap);
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	tt->nblocks = 0; /* mark the current end */
1681*4882a593Smuzhiyun 	for (tt = totrim; tt->nblocks != 0; tt++) {
1682*4882a593Smuzhiyun 		/* when mounted with online discard, dbFree() will
1683*4882a593Smuzhiyun 		 * call jfs_issue_discard() itself */
1684*4882a593Smuzhiyun 		if (!(JFS_SBI(sb)->flag & JFS_DISCARD))
1685*4882a593Smuzhiyun 			jfs_issue_discard(ip, tt->blkno, tt->nblocks);
1686*4882a593Smuzhiyun 		dbFree(ip, tt->blkno, tt->nblocks);
1687*4882a593Smuzhiyun 		trimmed += tt->nblocks;
1688*4882a593Smuzhiyun 	}
1689*4882a593Smuzhiyun 	kfree(totrim);
1690*4882a593Smuzhiyun 
1691*4882a593Smuzhiyun 	return trimmed;
1692*4882a593Smuzhiyun }
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun /*
1695*4882a593Smuzhiyun  * NAME:	dbFindCtl()
1696*4882a593Smuzhiyun  *
1697*4882a593Smuzhiyun  * FUNCTION:	starting at a specified dmap control page level and block
1698*4882a593Smuzhiyun  *		number, search down the dmap control levels for a range of
1699*4882a593Smuzhiyun  *		contiguous free blocks large enough to satisfy an allocation
1700*4882a593Smuzhiyun  *		request for the specified number of free blocks.
1701*4882a593Smuzhiyun  *
1702*4882a593Smuzhiyun  *		if sufficient contiguous free blocks are found, this routine
1703*4882a593Smuzhiyun  *		returns the starting block number within a dmap page that
1704*4882a593Smuzhiyun  *		contains or starts a range of contiqious free blocks that
1705*4882a593Smuzhiyun  *		is sufficient in size.
1706*4882a593Smuzhiyun  *
1707*4882a593Smuzhiyun  * PARAMETERS:
1708*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
1709*4882a593Smuzhiyun  *	level	-  starting dmap control page level.
1710*4882a593Smuzhiyun  *	l2nb	-  log2 number of contiguous free blocks desired.
1711*4882a593Smuzhiyun  *	*blkno	-  on entry, starting block number for conducting the search.
1712*4882a593Smuzhiyun  *		   on successful return, the first block within a dmap page
1713*4882a593Smuzhiyun  *		   that contains or starts a range of contiguous free blocks.
1714*4882a593Smuzhiyun  *
1715*4882a593Smuzhiyun  * RETURN VALUES:
1716*4882a593Smuzhiyun  *	0	- success
1717*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
1718*4882a593Smuzhiyun  *	-EIO	- i/o error
1719*4882a593Smuzhiyun  *
1720*4882a593Smuzhiyun  * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1721*4882a593Smuzhiyun  */
dbFindCtl(struct bmap * bmp,int l2nb,int level,s64 * blkno)1722*4882a593Smuzhiyun static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno)
1723*4882a593Smuzhiyun {
1724*4882a593Smuzhiyun 	int rc, leafidx, lev;
1725*4882a593Smuzhiyun 	s64 b, lblkno;
1726*4882a593Smuzhiyun 	struct dmapctl *dcp;
1727*4882a593Smuzhiyun 	int budmin;
1728*4882a593Smuzhiyun 	struct metapage *mp;
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 	/* starting at the specified dmap control page level and block
1731*4882a593Smuzhiyun 	 * number, search down the dmap control levels for the starting
1732*4882a593Smuzhiyun 	 * block number of a dmap page that contains or starts off
1733*4882a593Smuzhiyun 	 * sufficient free blocks.
1734*4882a593Smuzhiyun 	 */
1735*4882a593Smuzhiyun 	for (lev = level, b = *blkno; lev >= 0; lev--) {
1736*4882a593Smuzhiyun 		/* get the buffer of the dmap control page for the block
1737*4882a593Smuzhiyun 		 * number and level (i.e. L0, L1, L2).
1738*4882a593Smuzhiyun 		 */
1739*4882a593Smuzhiyun 		lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev);
1740*4882a593Smuzhiyun 		mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1741*4882a593Smuzhiyun 		if (mp == NULL)
1742*4882a593Smuzhiyun 			return -EIO;
1743*4882a593Smuzhiyun 		dcp = (struct dmapctl *) mp->data;
1744*4882a593Smuzhiyun 		budmin = dcp->budmin;
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun 		if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1747*4882a593Smuzhiyun 			jfs_error(bmp->db_ipbmap->i_sb,
1748*4882a593Smuzhiyun 				  "Corrupt dmapctl page\n");
1749*4882a593Smuzhiyun 			release_metapage(mp);
1750*4882a593Smuzhiyun 			return -EIO;
1751*4882a593Smuzhiyun 		}
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 		/* search the tree within the dmap control page for
1754*4882a593Smuzhiyun 		 * sufficient free space.  if sufficient free space is found,
1755*4882a593Smuzhiyun 		 * dbFindLeaf() returns the index of the leaf at which
1756*4882a593Smuzhiyun 		 * free space was found.
1757*4882a593Smuzhiyun 		 */
1758*4882a593Smuzhiyun 		rc = dbFindLeaf((dmtree_t *) dcp, l2nb, &leafidx);
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 		/* release the buffer.
1761*4882a593Smuzhiyun 		 */
1762*4882a593Smuzhiyun 		release_metapage(mp);
1763*4882a593Smuzhiyun 
1764*4882a593Smuzhiyun 		/* space found ?
1765*4882a593Smuzhiyun 		 */
1766*4882a593Smuzhiyun 		if (rc) {
1767*4882a593Smuzhiyun 			if (lev != level) {
1768*4882a593Smuzhiyun 				jfs_error(bmp->db_ipbmap->i_sb,
1769*4882a593Smuzhiyun 					  "dmap inconsistent\n");
1770*4882a593Smuzhiyun 				return -EIO;
1771*4882a593Smuzhiyun 			}
1772*4882a593Smuzhiyun 			return -ENOSPC;
1773*4882a593Smuzhiyun 		}
1774*4882a593Smuzhiyun 
1775*4882a593Smuzhiyun 		/* adjust the block number to reflect the location within
1776*4882a593Smuzhiyun 		 * the dmap control page (i.e. the leaf) at which free
1777*4882a593Smuzhiyun 		 * space was found.
1778*4882a593Smuzhiyun 		 */
1779*4882a593Smuzhiyun 		b += (((s64) leafidx) << budmin);
1780*4882a593Smuzhiyun 
1781*4882a593Smuzhiyun 		/* we stop the search at this dmap control page level if
1782*4882a593Smuzhiyun 		 * the number of blocks required is greater than or equal
1783*4882a593Smuzhiyun 		 * to the maximum number of blocks described at the next
1784*4882a593Smuzhiyun 		 * (lower) level.
1785*4882a593Smuzhiyun 		 */
1786*4882a593Smuzhiyun 		if (l2nb >= budmin)
1787*4882a593Smuzhiyun 			break;
1788*4882a593Smuzhiyun 	}
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun 	*blkno = b;
1791*4882a593Smuzhiyun 	return (0);
1792*4882a593Smuzhiyun }
1793*4882a593Smuzhiyun 
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun /*
1796*4882a593Smuzhiyun  * NAME:	dbAllocCtl()
1797*4882a593Smuzhiyun  *
1798*4882a593Smuzhiyun  * FUNCTION:	attempt to allocate a specified number of contiguous
1799*4882a593Smuzhiyun  *		blocks starting within a specific dmap.
1800*4882a593Smuzhiyun  *
1801*4882a593Smuzhiyun  *		this routine is called by higher level routines that search
1802*4882a593Smuzhiyun  *		the dmap control pages above the actual dmaps for contiguous
1803*4882a593Smuzhiyun  *		free space.  the result of successful searches by these
1804*4882a593Smuzhiyun  *		routines are the starting block numbers within dmaps, with
1805*4882a593Smuzhiyun  *		the dmaps themselves containing the desired contiguous free
1806*4882a593Smuzhiyun  *		space or starting a contiguous free space of desired size
1807*4882a593Smuzhiyun  *		that is made up of the blocks of one or more dmaps. these
1808*4882a593Smuzhiyun  *		calls should not fail due to insufficent resources.
1809*4882a593Smuzhiyun  *
1810*4882a593Smuzhiyun  *		this routine is called in some cases where it is not known
1811*4882a593Smuzhiyun  *		whether it will fail due to insufficient resources.  more
1812*4882a593Smuzhiyun  *		specifically, this occurs when allocating from an allocation
1813*4882a593Smuzhiyun  *		group whose size is equal to the number of blocks per dmap.
1814*4882a593Smuzhiyun  *		in this case, the dmap control pages are not examined prior
1815*4882a593Smuzhiyun  *		to calling this routine (to save pathlength) and the call
1816*4882a593Smuzhiyun  *		might fail.
1817*4882a593Smuzhiyun  *
1818*4882a593Smuzhiyun  *		for a request size that fits within a dmap, this routine relies
1819*4882a593Smuzhiyun  *		upon the dmap's dmtree to find the requested contiguous free
1820*4882a593Smuzhiyun  *		space.  for request sizes that are larger than a dmap, the
1821*4882a593Smuzhiyun  *		requested free space will start at the first block of the
1822*4882a593Smuzhiyun  *		first dmap (i.e. blkno).
1823*4882a593Smuzhiyun  *
1824*4882a593Smuzhiyun  * PARAMETERS:
1825*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
1826*4882a593Smuzhiyun  *	nblocks	 -  actual number of contiguous free blocks to allocate.
1827*4882a593Smuzhiyun  *	l2nb	 -  log2 number of contiguous free blocks to allocate.
1828*4882a593Smuzhiyun  *	blkno	 -  starting block number of the dmap to start the allocation
1829*4882a593Smuzhiyun  *		    from.
1830*4882a593Smuzhiyun  *	results	-  on successful return, set to the starting block number
1831*4882a593Smuzhiyun  *		   of the newly allocated range.
1832*4882a593Smuzhiyun  *
1833*4882a593Smuzhiyun  * RETURN VALUES:
1834*4882a593Smuzhiyun  *	0	- success
1835*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
1836*4882a593Smuzhiyun  *	-EIO	- i/o error
1837*4882a593Smuzhiyun  *
1838*4882a593Smuzhiyun  * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1839*4882a593Smuzhiyun  */
1840*4882a593Smuzhiyun static int
dbAllocCtl(struct bmap * bmp,s64 nblocks,int l2nb,s64 blkno,s64 * results)1841*4882a593Smuzhiyun dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, s64 * results)
1842*4882a593Smuzhiyun {
1843*4882a593Smuzhiyun 	int rc, nb;
1844*4882a593Smuzhiyun 	s64 b, lblkno, n;
1845*4882a593Smuzhiyun 	struct metapage *mp;
1846*4882a593Smuzhiyun 	struct dmap *dp;
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun 	/* check if the allocation request is confined to a single dmap.
1849*4882a593Smuzhiyun 	 */
1850*4882a593Smuzhiyun 	if (l2nb <= L2BPERDMAP) {
1851*4882a593Smuzhiyun 		/* get the buffer for the dmap.
1852*4882a593Smuzhiyun 		 */
1853*4882a593Smuzhiyun 		lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
1854*4882a593Smuzhiyun 		mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1855*4882a593Smuzhiyun 		if (mp == NULL)
1856*4882a593Smuzhiyun 			return -EIO;
1857*4882a593Smuzhiyun 		dp = (struct dmap *) mp->data;
1858*4882a593Smuzhiyun 
1859*4882a593Smuzhiyun 		/* try to allocate the blocks.
1860*4882a593Smuzhiyun 		 */
1861*4882a593Smuzhiyun 		rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results);
1862*4882a593Smuzhiyun 		if (rc == 0)
1863*4882a593Smuzhiyun 			mark_metapage_dirty(mp);
1864*4882a593Smuzhiyun 
1865*4882a593Smuzhiyun 		release_metapage(mp);
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 		return (rc);
1868*4882a593Smuzhiyun 	}
1869*4882a593Smuzhiyun 
1870*4882a593Smuzhiyun 	/* allocation request involving multiple dmaps. it must start on
1871*4882a593Smuzhiyun 	 * a dmap boundary.
1872*4882a593Smuzhiyun 	 */
1873*4882a593Smuzhiyun 	assert((blkno & (BPERDMAP - 1)) == 0);
1874*4882a593Smuzhiyun 
1875*4882a593Smuzhiyun 	/* allocate the blocks dmap by dmap.
1876*4882a593Smuzhiyun 	 */
1877*4882a593Smuzhiyun 	for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) {
1878*4882a593Smuzhiyun 		/* get the buffer for the dmap.
1879*4882a593Smuzhiyun 		 */
1880*4882a593Smuzhiyun 		lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1881*4882a593Smuzhiyun 		mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1882*4882a593Smuzhiyun 		if (mp == NULL) {
1883*4882a593Smuzhiyun 			rc = -EIO;
1884*4882a593Smuzhiyun 			goto backout;
1885*4882a593Smuzhiyun 		}
1886*4882a593Smuzhiyun 		dp = (struct dmap *) mp->data;
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun 		/* the dmap better be all free.
1889*4882a593Smuzhiyun 		 */
1890*4882a593Smuzhiyun 		if (dp->tree.stree[ROOT] != L2BPERDMAP) {
1891*4882a593Smuzhiyun 			release_metapage(mp);
1892*4882a593Smuzhiyun 			jfs_error(bmp->db_ipbmap->i_sb,
1893*4882a593Smuzhiyun 				  "the dmap is not all free\n");
1894*4882a593Smuzhiyun 			rc = -EIO;
1895*4882a593Smuzhiyun 			goto backout;
1896*4882a593Smuzhiyun 		}
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun 		/* determine how many blocks to allocate from this dmap.
1899*4882a593Smuzhiyun 		 */
1900*4882a593Smuzhiyun 		nb = min_t(s64, n, BPERDMAP);
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 		/* allocate the blocks from the dmap.
1903*4882a593Smuzhiyun 		 */
1904*4882a593Smuzhiyun 		if ((rc = dbAllocDmap(bmp, dp, b, nb))) {
1905*4882a593Smuzhiyun 			release_metapage(mp);
1906*4882a593Smuzhiyun 			goto backout;
1907*4882a593Smuzhiyun 		}
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun 		/* write the buffer.
1910*4882a593Smuzhiyun 		 */
1911*4882a593Smuzhiyun 		write_metapage(mp);
1912*4882a593Smuzhiyun 	}
1913*4882a593Smuzhiyun 
1914*4882a593Smuzhiyun 	/* set the results (starting block number) and return.
1915*4882a593Smuzhiyun 	 */
1916*4882a593Smuzhiyun 	*results = blkno;
1917*4882a593Smuzhiyun 	return (0);
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun 	/* something failed in handling an allocation request involving
1920*4882a593Smuzhiyun 	 * multiple dmaps.  we'll try to clean up by backing out any
1921*4882a593Smuzhiyun 	 * allocation that has already happened for this request.  if
1922*4882a593Smuzhiyun 	 * we fail in backing out the allocation, we'll mark the file
1923*4882a593Smuzhiyun 	 * system to indicate that blocks have been leaked.
1924*4882a593Smuzhiyun 	 */
1925*4882a593Smuzhiyun       backout:
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 	/* try to backout the allocations dmap by dmap.
1928*4882a593Smuzhiyun 	 */
1929*4882a593Smuzhiyun 	for (n = nblocks - n, b = blkno; n > 0;
1930*4882a593Smuzhiyun 	     n -= BPERDMAP, b += BPERDMAP) {
1931*4882a593Smuzhiyun 		/* get the buffer for this dmap.
1932*4882a593Smuzhiyun 		 */
1933*4882a593Smuzhiyun 		lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1934*4882a593Smuzhiyun 		mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1935*4882a593Smuzhiyun 		if (mp == NULL) {
1936*4882a593Smuzhiyun 			/* could not back out.  mark the file system
1937*4882a593Smuzhiyun 			 * to indicate that we have leaked blocks.
1938*4882a593Smuzhiyun 			 */
1939*4882a593Smuzhiyun 			jfs_error(bmp->db_ipbmap->i_sb,
1940*4882a593Smuzhiyun 				  "I/O Error: Block Leakage\n");
1941*4882a593Smuzhiyun 			continue;
1942*4882a593Smuzhiyun 		}
1943*4882a593Smuzhiyun 		dp = (struct dmap *) mp->data;
1944*4882a593Smuzhiyun 
1945*4882a593Smuzhiyun 		/* free the blocks is this dmap.
1946*4882a593Smuzhiyun 		 */
1947*4882a593Smuzhiyun 		if (dbFreeDmap(bmp, dp, b, BPERDMAP)) {
1948*4882a593Smuzhiyun 			/* could not back out.  mark the file system
1949*4882a593Smuzhiyun 			 * to indicate that we have leaked blocks.
1950*4882a593Smuzhiyun 			 */
1951*4882a593Smuzhiyun 			release_metapage(mp);
1952*4882a593Smuzhiyun 			jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n");
1953*4882a593Smuzhiyun 			continue;
1954*4882a593Smuzhiyun 		}
1955*4882a593Smuzhiyun 
1956*4882a593Smuzhiyun 		/* write the buffer.
1957*4882a593Smuzhiyun 		 */
1958*4882a593Smuzhiyun 		write_metapage(mp);
1959*4882a593Smuzhiyun 	}
1960*4882a593Smuzhiyun 
1961*4882a593Smuzhiyun 	return (rc);
1962*4882a593Smuzhiyun }
1963*4882a593Smuzhiyun 
1964*4882a593Smuzhiyun 
1965*4882a593Smuzhiyun /*
1966*4882a593Smuzhiyun  * NAME:	dbAllocDmapLev()
1967*4882a593Smuzhiyun  *
1968*4882a593Smuzhiyun  * FUNCTION:	attempt to allocate a specified number of contiguous blocks
1969*4882a593Smuzhiyun  *		from a specified dmap.
1970*4882a593Smuzhiyun  *
1971*4882a593Smuzhiyun  *		this routine checks if the contiguous blocks are available.
1972*4882a593Smuzhiyun  *		if so, nblocks of blocks are allocated; otherwise, ENOSPC is
1973*4882a593Smuzhiyun  *		returned.
1974*4882a593Smuzhiyun  *
1975*4882a593Smuzhiyun  * PARAMETERS:
1976*4882a593Smuzhiyun  *	mp	-  pointer to bmap descriptor
1977*4882a593Smuzhiyun  *	dp	-  pointer to dmap to attempt to allocate blocks from.
1978*4882a593Smuzhiyun  *	l2nb	-  log2 number of contiguous block desired.
1979*4882a593Smuzhiyun  *	nblocks	-  actual number of contiguous block desired.
1980*4882a593Smuzhiyun  *	results	-  on successful return, set to the starting block number
1981*4882a593Smuzhiyun  *		   of the newly allocated range.
1982*4882a593Smuzhiyun  *
1983*4882a593Smuzhiyun  * RETURN VALUES:
1984*4882a593Smuzhiyun  *	0	- success
1985*4882a593Smuzhiyun  *	-ENOSPC	- insufficient disk resources
1986*4882a593Smuzhiyun  *	-EIO	- i/o error
1987*4882a593Smuzhiyun  *
1988*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap), e.g., from dbAlloc(), or
1989*4882a593Smuzhiyun  *	IWRITE_LOCK(ipbmap), e.g., dbAllocCtl(), held on entry/exit;
1990*4882a593Smuzhiyun  */
1991*4882a593Smuzhiyun static int
dbAllocDmapLev(struct bmap * bmp,struct dmap * dp,int nblocks,int l2nb,s64 * results)1992*4882a593Smuzhiyun dbAllocDmapLev(struct bmap * bmp,
1993*4882a593Smuzhiyun 	       struct dmap * dp, int nblocks, int l2nb, s64 * results)
1994*4882a593Smuzhiyun {
1995*4882a593Smuzhiyun 	s64 blkno;
1996*4882a593Smuzhiyun 	int leafidx, rc;
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 	/* can't be more than a dmaps worth of blocks */
1999*4882a593Smuzhiyun 	assert(l2nb <= L2BPERDMAP);
2000*4882a593Smuzhiyun 
2001*4882a593Smuzhiyun 	/* search the tree within the dmap page for sufficient
2002*4882a593Smuzhiyun 	 * free space.  if sufficient free space is found, dbFindLeaf()
2003*4882a593Smuzhiyun 	 * returns the index of the leaf at which free space was found.
2004*4882a593Smuzhiyun 	 */
2005*4882a593Smuzhiyun 	if (dbFindLeaf((dmtree_t *) & dp->tree, l2nb, &leafidx))
2006*4882a593Smuzhiyun 		return -ENOSPC;
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun 	/* determine the block number within the file system corresponding
2009*4882a593Smuzhiyun 	 * to the leaf at which free space was found.
2010*4882a593Smuzhiyun 	 */
2011*4882a593Smuzhiyun 	blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD);
2012*4882a593Smuzhiyun 
2013*4882a593Smuzhiyun 	/* if not all bits of the dmap word are free, get the starting
2014*4882a593Smuzhiyun 	 * bit number within the dmap word of the required string of free
2015*4882a593Smuzhiyun 	 * bits and adjust the block number with this value.
2016*4882a593Smuzhiyun 	 */
2017*4882a593Smuzhiyun 	if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN)
2018*4882a593Smuzhiyun 		blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb);
2019*4882a593Smuzhiyun 
2020*4882a593Smuzhiyun 	/* allocate the blocks */
2021*4882a593Smuzhiyun 	if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
2022*4882a593Smuzhiyun 		*results = blkno;
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun 	return (rc);
2025*4882a593Smuzhiyun }
2026*4882a593Smuzhiyun 
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun /*
2029*4882a593Smuzhiyun  * NAME:	dbAllocDmap()
2030*4882a593Smuzhiyun  *
2031*4882a593Smuzhiyun  * FUNCTION:	adjust the disk allocation map to reflect the allocation
2032*4882a593Smuzhiyun  *		of a specified block range within a dmap.
2033*4882a593Smuzhiyun  *
2034*4882a593Smuzhiyun  *		this routine allocates the specified blocks from the dmap
2035*4882a593Smuzhiyun  *		through a call to dbAllocBits(). if the allocation of the
2036*4882a593Smuzhiyun  *		block range causes the maximum string of free blocks within
2037*4882a593Smuzhiyun  *		the dmap to change (i.e. the value of the root of the dmap's
2038*4882a593Smuzhiyun  *		dmtree), this routine will cause this change to be reflected
2039*4882a593Smuzhiyun  *		up through the appropriate levels of the dmap control pages
2040*4882a593Smuzhiyun  *		by a call to dbAdjCtl() for the L0 dmap control page that
2041*4882a593Smuzhiyun  *		covers this dmap.
2042*4882a593Smuzhiyun  *
2043*4882a593Smuzhiyun  * PARAMETERS:
2044*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
2045*4882a593Smuzhiyun  *	dp	-  pointer to dmap to allocate the block range from.
2046*4882a593Smuzhiyun  *	blkno	-  starting block number of the block to be allocated.
2047*4882a593Smuzhiyun  *	nblocks	-  number of blocks to be allocated.
2048*4882a593Smuzhiyun  *
2049*4882a593Smuzhiyun  * RETURN VALUES:
2050*4882a593Smuzhiyun  *	0	- success
2051*4882a593Smuzhiyun  *	-EIO	- i/o error
2052*4882a593Smuzhiyun  *
2053*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2054*4882a593Smuzhiyun  */
dbAllocDmap(struct bmap * bmp,struct dmap * dp,s64 blkno,int nblocks)2055*4882a593Smuzhiyun static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2056*4882a593Smuzhiyun 		       int nblocks)
2057*4882a593Smuzhiyun {
2058*4882a593Smuzhiyun 	s8 oldroot;
2059*4882a593Smuzhiyun 	int rc;
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	/* save the current value of the root (i.e. maximum free string)
2062*4882a593Smuzhiyun 	 * of the dmap tree.
2063*4882a593Smuzhiyun 	 */
2064*4882a593Smuzhiyun 	oldroot = dp->tree.stree[ROOT];
2065*4882a593Smuzhiyun 
2066*4882a593Smuzhiyun 	/* allocate the specified (blocks) bits */
2067*4882a593Smuzhiyun 	dbAllocBits(bmp, dp, blkno, nblocks);
2068*4882a593Smuzhiyun 
2069*4882a593Smuzhiyun 	/* if the root has not changed, done. */
2070*4882a593Smuzhiyun 	if (dp->tree.stree[ROOT] == oldroot)
2071*4882a593Smuzhiyun 		return (0);
2072*4882a593Smuzhiyun 
2073*4882a593Smuzhiyun 	/* root changed. bubble the change up to the dmap control pages.
2074*4882a593Smuzhiyun 	 * if the adjustment of the upper level control pages fails,
2075*4882a593Smuzhiyun 	 * backout the bit allocation (thus making everything consistent).
2076*4882a593Smuzhiyun 	 */
2077*4882a593Smuzhiyun 	if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0)))
2078*4882a593Smuzhiyun 		dbFreeBits(bmp, dp, blkno, nblocks);
2079*4882a593Smuzhiyun 
2080*4882a593Smuzhiyun 	return (rc);
2081*4882a593Smuzhiyun }
2082*4882a593Smuzhiyun 
2083*4882a593Smuzhiyun 
2084*4882a593Smuzhiyun /*
2085*4882a593Smuzhiyun  * NAME:	dbFreeDmap()
2086*4882a593Smuzhiyun  *
2087*4882a593Smuzhiyun  * FUNCTION:	adjust the disk allocation map to reflect the allocation
2088*4882a593Smuzhiyun  *		of a specified block range within a dmap.
2089*4882a593Smuzhiyun  *
2090*4882a593Smuzhiyun  *		this routine frees the specified blocks from the dmap through
2091*4882a593Smuzhiyun  *		a call to dbFreeBits(). if the deallocation of the block range
2092*4882a593Smuzhiyun  *		causes the maximum string of free blocks within the dmap to
2093*4882a593Smuzhiyun  *		change (i.e. the value of the root of the dmap's dmtree), this
2094*4882a593Smuzhiyun  *		routine will cause this change to be reflected up through the
2095*4882a593Smuzhiyun  *		appropriate levels of the dmap control pages by a call to
2096*4882a593Smuzhiyun  *		dbAdjCtl() for the L0 dmap control page that covers this dmap.
2097*4882a593Smuzhiyun  *
2098*4882a593Smuzhiyun  * PARAMETERS:
2099*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
2100*4882a593Smuzhiyun  *	dp	-  pointer to dmap to free the block range from.
2101*4882a593Smuzhiyun  *	blkno	-  starting block number of the block to be freed.
2102*4882a593Smuzhiyun  *	nblocks	-  number of blocks to be freed.
2103*4882a593Smuzhiyun  *
2104*4882a593Smuzhiyun  * RETURN VALUES:
2105*4882a593Smuzhiyun  *	0	- success
2106*4882a593Smuzhiyun  *	-EIO	- i/o error
2107*4882a593Smuzhiyun  *
2108*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2109*4882a593Smuzhiyun  */
dbFreeDmap(struct bmap * bmp,struct dmap * dp,s64 blkno,int nblocks)2110*4882a593Smuzhiyun static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2111*4882a593Smuzhiyun 		      int nblocks)
2112*4882a593Smuzhiyun {
2113*4882a593Smuzhiyun 	s8 oldroot;
2114*4882a593Smuzhiyun 	int rc = 0, word;
2115*4882a593Smuzhiyun 
2116*4882a593Smuzhiyun 	/* save the current value of the root (i.e. maximum free string)
2117*4882a593Smuzhiyun 	 * of the dmap tree.
2118*4882a593Smuzhiyun 	 */
2119*4882a593Smuzhiyun 	oldroot = dp->tree.stree[ROOT];
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun 	/* free the specified (blocks) bits */
2122*4882a593Smuzhiyun 	rc = dbFreeBits(bmp, dp, blkno, nblocks);
2123*4882a593Smuzhiyun 
2124*4882a593Smuzhiyun 	/* if error or the root has not changed, done. */
2125*4882a593Smuzhiyun 	if (rc || (dp->tree.stree[ROOT] == oldroot))
2126*4882a593Smuzhiyun 		return (rc);
2127*4882a593Smuzhiyun 
2128*4882a593Smuzhiyun 	/* root changed. bubble the change up to the dmap control pages.
2129*4882a593Smuzhiyun 	 * if the adjustment of the upper level control pages fails,
2130*4882a593Smuzhiyun 	 * backout the deallocation.
2131*4882a593Smuzhiyun 	 */
2132*4882a593Smuzhiyun 	if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) {
2133*4882a593Smuzhiyun 		word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
2134*4882a593Smuzhiyun 
2135*4882a593Smuzhiyun 		/* as part of backing out the deallocation, we will have
2136*4882a593Smuzhiyun 		 * to back split the dmap tree if the deallocation caused
2137*4882a593Smuzhiyun 		 * the freed blocks to become part of a larger binary buddy
2138*4882a593Smuzhiyun 		 * system.
2139*4882a593Smuzhiyun 		 */
2140*4882a593Smuzhiyun 		if (dp->tree.stree[word] == NOFREE)
2141*4882a593Smuzhiyun 			dbBackSplit((dmtree_t *) & dp->tree, word);
2142*4882a593Smuzhiyun 
2143*4882a593Smuzhiyun 		dbAllocBits(bmp, dp, blkno, nblocks);
2144*4882a593Smuzhiyun 	}
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	return (rc);
2147*4882a593Smuzhiyun }
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun /*
2151*4882a593Smuzhiyun  * NAME:	dbAllocBits()
2152*4882a593Smuzhiyun  *
2153*4882a593Smuzhiyun  * FUNCTION:	allocate a specified block range from a dmap.
2154*4882a593Smuzhiyun  *
2155*4882a593Smuzhiyun  *		this routine updates the dmap to reflect the working
2156*4882a593Smuzhiyun  *		state allocation of the specified block range. it directly
2157*4882a593Smuzhiyun  *		updates the bits of the working map and causes the adjustment
2158*4882a593Smuzhiyun  *		of the binary buddy system described by the dmap's dmtree
2159*4882a593Smuzhiyun  *		leaves to reflect the bits allocated.  it also causes the
2160*4882a593Smuzhiyun  *		dmap's dmtree, as a whole, to reflect the allocated range.
2161*4882a593Smuzhiyun  *
2162*4882a593Smuzhiyun  * PARAMETERS:
2163*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
2164*4882a593Smuzhiyun  *	dp	-  pointer to dmap to allocate bits from.
2165*4882a593Smuzhiyun  *	blkno	-  starting block number of the bits to be allocated.
2166*4882a593Smuzhiyun  *	nblocks	-  number of bits to be allocated.
2167*4882a593Smuzhiyun  *
2168*4882a593Smuzhiyun  * RETURN VALUES: none
2169*4882a593Smuzhiyun  *
2170*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2171*4882a593Smuzhiyun  */
dbAllocBits(struct bmap * bmp,struct dmap * dp,s64 blkno,int nblocks)2172*4882a593Smuzhiyun static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
2173*4882a593Smuzhiyun 			int nblocks)
2174*4882a593Smuzhiyun {
2175*4882a593Smuzhiyun 	int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2176*4882a593Smuzhiyun 	dmtree_t *tp = (dmtree_t *) & dp->tree;
2177*4882a593Smuzhiyun 	int size;
2178*4882a593Smuzhiyun 	s8 *leaf;
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun 	/* pick up a pointer to the leaves of the dmap tree */
2181*4882a593Smuzhiyun 	leaf = dp->tree.stree + LEAFIND;
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 	/* determine the bit number and word within the dmap of the
2184*4882a593Smuzhiyun 	 * starting block.
2185*4882a593Smuzhiyun 	 */
2186*4882a593Smuzhiyun 	dbitno = blkno & (BPERDMAP - 1);
2187*4882a593Smuzhiyun 	word = dbitno >> L2DBWORD;
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun 	/* block range better be within the dmap */
2190*4882a593Smuzhiyun 	assert(dbitno + nblocks <= BPERDMAP);
2191*4882a593Smuzhiyun 
2192*4882a593Smuzhiyun 	/* allocate the bits of the dmap's words corresponding to the block
2193*4882a593Smuzhiyun 	 * range. not all bits of the first and last words may be contained
2194*4882a593Smuzhiyun 	 * within the block range.  if this is the case, we'll work against
2195*4882a593Smuzhiyun 	 * those words (i.e. partial first and/or last) on an individual basis
2196*4882a593Smuzhiyun 	 * (a single pass), allocating the bits of interest by hand and
2197*4882a593Smuzhiyun 	 * updating the leaf corresponding to the dmap word. a single pass
2198*4882a593Smuzhiyun 	 * will be used for all dmap words fully contained within the
2199*4882a593Smuzhiyun 	 * specified range.  within this pass, the bits of all fully contained
2200*4882a593Smuzhiyun 	 * dmap words will be marked as free in a single shot and the leaves
2201*4882a593Smuzhiyun 	 * will be updated. a single leaf may describe the free space of
2202*4882a593Smuzhiyun 	 * multiple dmap words, so we may update only a subset of the actual
2203*4882a593Smuzhiyun 	 * leaves corresponding to the dmap words of the block range.
2204*4882a593Smuzhiyun 	 */
2205*4882a593Smuzhiyun 	for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2206*4882a593Smuzhiyun 		/* determine the bit number within the word and
2207*4882a593Smuzhiyun 		 * the number of bits within the word.
2208*4882a593Smuzhiyun 		 */
2209*4882a593Smuzhiyun 		wbitno = dbitno & (DBWORD - 1);
2210*4882a593Smuzhiyun 		nb = min(rembits, DBWORD - wbitno);
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 		/* check if only part of a word is to be allocated.
2213*4882a593Smuzhiyun 		 */
2214*4882a593Smuzhiyun 		if (nb < DBWORD) {
2215*4882a593Smuzhiyun 			/* allocate (set to 1) the appropriate bits within
2216*4882a593Smuzhiyun 			 * this dmap word.
2217*4882a593Smuzhiyun 			 */
2218*4882a593Smuzhiyun 			dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
2219*4882a593Smuzhiyun 						      >> wbitno);
2220*4882a593Smuzhiyun 
2221*4882a593Smuzhiyun 			/* update the leaf for this dmap word. in addition
2222*4882a593Smuzhiyun 			 * to setting the leaf value to the binary buddy max
2223*4882a593Smuzhiyun 			 * of the updated dmap word, dbSplit() will split
2224*4882a593Smuzhiyun 			 * the binary system of the leaves if need be.
2225*4882a593Smuzhiyun 			 */
2226*4882a593Smuzhiyun 			dbSplit(tp, word, BUDMIN,
2227*4882a593Smuzhiyun 				dbMaxBud((u8 *) & dp->wmap[word]));
2228*4882a593Smuzhiyun 
2229*4882a593Smuzhiyun 			word += 1;
2230*4882a593Smuzhiyun 		} else {
2231*4882a593Smuzhiyun 			/* one or more dmap words are fully contained
2232*4882a593Smuzhiyun 			 * within the block range.  determine how many
2233*4882a593Smuzhiyun 			 * words and allocate (set to 1) the bits of these
2234*4882a593Smuzhiyun 			 * words.
2235*4882a593Smuzhiyun 			 */
2236*4882a593Smuzhiyun 			nwords = rembits >> L2DBWORD;
2237*4882a593Smuzhiyun 			memset(&dp->wmap[word], (int) ONES, nwords * 4);
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun 			/* determine how many bits.
2240*4882a593Smuzhiyun 			 */
2241*4882a593Smuzhiyun 			nb = nwords << L2DBWORD;
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun 			/* now update the appropriate leaves to reflect
2244*4882a593Smuzhiyun 			 * the allocated words.
2245*4882a593Smuzhiyun 			 */
2246*4882a593Smuzhiyun 			for (; nwords > 0; nwords -= nw) {
2247*4882a593Smuzhiyun 				if (leaf[word] < BUDMIN) {
2248*4882a593Smuzhiyun 					jfs_error(bmp->db_ipbmap->i_sb,
2249*4882a593Smuzhiyun 						  "leaf page corrupt\n");
2250*4882a593Smuzhiyun 					break;
2251*4882a593Smuzhiyun 				}
2252*4882a593Smuzhiyun 
2253*4882a593Smuzhiyun 				/* determine what the leaf value should be
2254*4882a593Smuzhiyun 				 * updated to as the minimum of the l2 number
2255*4882a593Smuzhiyun 				 * of bits being allocated and the l2 number
2256*4882a593Smuzhiyun 				 * of bits currently described by this leaf.
2257*4882a593Smuzhiyun 				 */
2258*4882a593Smuzhiyun 				size = min_t(int, leaf[word],
2259*4882a593Smuzhiyun 					     NLSTOL2BSZ(nwords));
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun 				/* update the leaf to reflect the allocation.
2262*4882a593Smuzhiyun 				 * in addition to setting the leaf value to
2263*4882a593Smuzhiyun 				 * NOFREE, dbSplit() will split the binary
2264*4882a593Smuzhiyun 				 * system of the leaves to reflect the current
2265*4882a593Smuzhiyun 				 * allocation (size).
2266*4882a593Smuzhiyun 				 */
2267*4882a593Smuzhiyun 				dbSplit(tp, word, size, NOFREE);
2268*4882a593Smuzhiyun 
2269*4882a593Smuzhiyun 				/* get the number of dmap words handled */
2270*4882a593Smuzhiyun 				nw = BUDSIZE(size, BUDMIN);
2271*4882a593Smuzhiyun 				word += nw;
2272*4882a593Smuzhiyun 			}
2273*4882a593Smuzhiyun 		}
2274*4882a593Smuzhiyun 	}
2275*4882a593Smuzhiyun 
2276*4882a593Smuzhiyun 	/* update the free count for this dmap */
2277*4882a593Smuzhiyun 	le32_add_cpu(&dp->nfree, -nblocks);
2278*4882a593Smuzhiyun 
2279*4882a593Smuzhiyun 	BMAP_LOCK(bmp);
2280*4882a593Smuzhiyun 
2281*4882a593Smuzhiyun 	/* if this allocation group is completely free,
2282*4882a593Smuzhiyun 	 * update the maximum allocation group number if this allocation
2283*4882a593Smuzhiyun 	 * group is the new max.
2284*4882a593Smuzhiyun 	 */
2285*4882a593Smuzhiyun 	agno = blkno >> bmp->db_agl2size;
2286*4882a593Smuzhiyun 	if (agno > bmp->db_maxag)
2287*4882a593Smuzhiyun 		bmp->db_maxag = agno;
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 	/* update the free count for the allocation group and map */
2290*4882a593Smuzhiyun 	bmp->db_agfree[agno] -= nblocks;
2291*4882a593Smuzhiyun 	bmp->db_nfree -= nblocks;
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun 	BMAP_UNLOCK(bmp);
2294*4882a593Smuzhiyun }
2295*4882a593Smuzhiyun 
2296*4882a593Smuzhiyun 
2297*4882a593Smuzhiyun /*
2298*4882a593Smuzhiyun  * NAME:	dbFreeBits()
2299*4882a593Smuzhiyun  *
2300*4882a593Smuzhiyun  * FUNCTION:	free a specified block range from a dmap.
2301*4882a593Smuzhiyun  *
2302*4882a593Smuzhiyun  *		this routine updates the dmap to reflect the working
2303*4882a593Smuzhiyun  *		state allocation of the specified block range. it directly
2304*4882a593Smuzhiyun  *		updates the bits of the working map and causes the adjustment
2305*4882a593Smuzhiyun  *		of the binary buddy system described by the dmap's dmtree
2306*4882a593Smuzhiyun  *		leaves to reflect the bits freed.  it also causes the dmap's
2307*4882a593Smuzhiyun  *		dmtree, as a whole, to reflect the deallocated range.
2308*4882a593Smuzhiyun  *
2309*4882a593Smuzhiyun  * PARAMETERS:
2310*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
2311*4882a593Smuzhiyun  *	dp	-  pointer to dmap to free bits from.
2312*4882a593Smuzhiyun  *	blkno	-  starting block number of the bits to be freed.
2313*4882a593Smuzhiyun  *	nblocks	-  number of bits to be freed.
2314*4882a593Smuzhiyun  *
2315*4882a593Smuzhiyun  * RETURN VALUES: 0 for success
2316*4882a593Smuzhiyun  *
2317*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2318*4882a593Smuzhiyun  */
dbFreeBits(struct bmap * bmp,struct dmap * dp,s64 blkno,int nblocks)2319*4882a593Smuzhiyun static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
2320*4882a593Smuzhiyun 		       int nblocks)
2321*4882a593Smuzhiyun {
2322*4882a593Smuzhiyun 	int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2323*4882a593Smuzhiyun 	dmtree_t *tp = (dmtree_t *) & dp->tree;
2324*4882a593Smuzhiyun 	int rc = 0;
2325*4882a593Smuzhiyun 	int size;
2326*4882a593Smuzhiyun 
2327*4882a593Smuzhiyun 	/* determine the bit number and word within the dmap of the
2328*4882a593Smuzhiyun 	 * starting block.
2329*4882a593Smuzhiyun 	 */
2330*4882a593Smuzhiyun 	dbitno = blkno & (BPERDMAP - 1);
2331*4882a593Smuzhiyun 	word = dbitno >> L2DBWORD;
2332*4882a593Smuzhiyun 
2333*4882a593Smuzhiyun 	/* block range better be within the dmap.
2334*4882a593Smuzhiyun 	 */
2335*4882a593Smuzhiyun 	assert(dbitno + nblocks <= BPERDMAP);
2336*4882a593Smuzhiyun 
2337*4882a593Smuzhiyun 	/* free the bits of the dmaps words corresponding to the block range.
2338*4882a593Smuzhiyun 	 * not all bits of the first and last words may be contained within
2339*4882a593Smuzhiyun 	 * the block range.  if this is the case, we'll work against those
2340*4882a593Smuzhiyun 	 * words (i.e. partial first and/or last) on an individual basis
2341*4882a593Smuzhiyun 	 * (a single pass), freeing the bits of interest by hand and updating
2342*4882a593Smuzhiyun 	 * the leaf corresponding to the dmap word. a single pass will be used
2343*4882a593Smuzhiyun 	 * for all dmap words fully contained within the specified range.
2344*4882a593Smuzhiyun 	 * within this pass, the bits of all fully contained dmap words will
2345*4882a593Smuzhiyun 	 * be marked as free in a single shot and the leaves will be updated. a
2346*4882a593Smuzhiyun 	 * single leaf may describe the free space of multiple dmap words,
2347*4882a593Smuzhiyun 	 * so we may update only a subset of the actual leaves corresponding
2348*4882a593Smuzhiyun 	 * to the dmap words of the block range.
2349*4882a593Smuzhiyun 	 *
2350*4882a593Smuzhiyun 	 * dbJoin() is used to update leaf values and will join the binary
2351*4882a593Smuzhiyun 	 * buddy system of the leaves if the new leaf values indicate this
2352*4882a593Smuzhiyun 	 * should be done.
2353*4882a593Smuzhiyun 	 */
2354*4882a593Smuzhiyun 	for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2355*4882a593Smuzhiyun 		/* determine the bit number within the word and
2356*4882a593Smuzhiyun 		 * the number of bits within the word.
2357*4882a593Smuzhiyun 		 */
2358*4882a593Smuzhiyun 		wbitno = dbitno & (DBWORD - 1);
2359*4882a593Smuzhiyun 		nb = min(rembits, DBWORD - wbitno);
2360*4882a593Smuzhiyun 
2361*4882a593Smuzhiyun 		/* check if only part of a word is to be freed.
2362*4882a593Smuzhiyun 		 */
2363*4882a593Smuzhiyun 		if (nb < DBWORD) {
2364*4882a593Smuzhiyun 			/* free (zero) the appropriate bits within this
2365*4882a593Smuzhiyun 			 * dmap word.
2366*4882a593Smuzhiyun 			 */
2367*4882a593Smuzhiyun 			dp->wmap[word] &=
2368*4882a593Smuzhiyun 			    cpu_to_le32(~(ONES << (DBWORD - nb)
2369*4882a593Smuzhiyun 					  >> wbitno));
2370*4882a593Smuzhiyun 
2371*4882a593Smuzhiyun 			/* update the leaf for this dmap word.
2372*4882a593Smuzhiyun 			 */
2373*4882a593Smuzhiyun 			rc = dbJoin(tp, word,
2374*4882a593Smuzhiyun 				    dbMaxBud((u8 *) & dp->wmap[word]));
2375*4882a593Smuzhiyun 			if (rc)
2376*4882a593Smuzhiyun 				return rc;
2377*4882a593Smuzhiyun 
2378*4882a593Smuzhiyun 			word += 1;
2379*4882a593Smuzhiyun 		} else {
2380*4882a593Smuzhiyun 			/* one or more dmap words are fully contained
2381*4882a593Smuzhiyun 			 * within the block range.  determine how many
2382*4882a593Smuzhiyun 			 * words and free (zero) the bits of these words.
2383*4882a593Smuzhiyun 			 */
2384*4882a593Smuzhiyun 			nwords = rembits >> L2DBWORD;
2385*4882a593Smuzhiyun 			memset(&dp->wmap[word], 0, nwords * 4);
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun 			/* determine how many bits.
2388*4882a593Smuzhiyun 			 */
2389*4882a593Smuzhiyun 			nb = nwords << L2DBWORD;
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun 			/* now update the appropriate leaves to reflect
2392*4882a593Smuzhiyun 			 * the freed words.
2393*4882a593Smuzhiyun 			 */
2394*4882a593Smuzhiyun 			for (; nwords > 0; nwords -= nw) {
2395*4882a593Smuzhiyun 				/* determine what the leaf value should be
2396*4882a593Smuzhiyun 				 * updated to as the minimum of the l2 number
2397*4882a593Smuzhiyun 				 * of bits being freed and the l2 (max) number
2398*4882a593Smuzhiyun 				 * of bits that can be described by this leaf.
2399*4882a593Smuzhiyun 				 */
2400*4882a593Smuzhiyun 				size =
2401*4882a593Smuzhiyun 				    min(LITOL2BSZ
2402*4882a593Smuzhiyun 					(word, L2LPERDMAP, BUDMIN),
2403*4882a593Smuzhiyun 					NLSTOL2BSZ(nwords));
2404*4882a593Smuzhiyun 
2405*4882a593Smuzhiyun 				/* update the leaf.
2406*4882a593Smuzhiyun 				 */
2407*4882a593Smuzhiyun 				rc = dbJoin(tp, word, size);
2408*4882a593Smuzhiyun 				if (rc)
2409*4882a593Smuzhiyun 					return rc;
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun 				/* get the number of dmap words handled.
2412*4882a593Smuzhiyun 				 */
2413*4882a593Smuzhiyun 				nw = BUDSIZE(size, BUDMIN);
2414*4882a593Smuzhiyun 				word += nw;
2415*4882a593Smuzhiyun 			}
2416*4882a593Smuzhiyun 		}
2417*4882a593Smuzhiyun 	}
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun 	/* update the free count for this dmap.
2420*4882a593Smuzhiyun 	 */
2421*4882a593Smuzhiyun 	le32_add_cpu(&dp->nfree, nblocks);
2422*4882a593Smuzhiyun 
2423*4882a593Smuzhiyun 	BMAP_LOCK(bmp);
2424*4882a593Smuzhiyun 
2425*4882a593Smuzhiyun 	/* update the free count for the allocation group and
2426*4882a593Smuzhiyun 	 * map.
2427*4882a593Smuzhiyun 	 */
2428*4882a593Smuzhiyun 	agno = blkno >> bmp->db_agl2size;
2429*4882a593Smuzhiyun 	bmp->db_nfree += nblocks;
2430*4882a593Smuzhiyun 	bmp->db_agfree[agno] += nblocks;
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun 	/* check if this allocation group is not completely free and
2433*4882a593Smuzhiyun 	 * if it is currently the maximum (rightmost) allocation group.
2434*4882a593Smuzhiyun 	 * if so, establish the new maximum allocation group number by
2435*4882a593Smuzhiyun 	 * searching left for the first allocation group with allocation.
2436*4882a593Smuzhiyun 	 */
2437*4882a593Smuzhiyun 	if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) ||
2438*4882a593Smuzhiyun 	    (agno == bmp->db_numag - 1 &&
2439*4882a593Smuzhiyun 	     bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) {
2440*4882a593Smuzhiyun 		while (bmp->db_maxag > 0) {
2441*4882a593Smuzhiyun 			bmp->db_maxag -= 1;
2442*4882a593Smuzhiyun 			if (bmp->db_agfree[bmp->db_maxag] !=
2443*4882a593Smuzhiyun 			    bmp->db_agsize)
2444*4882a593Smuzhiyun 				break;
2445*4882a593Smuzhiyun 		}
2446*4882a593Smuzhiyun 
2447*4882a593Smuzhiyun 		/* re-establish the allocation group preference if the
2448*4882a593Smuzhiyun 		 * current preference is right of the maximum allocation
2449*4882a593Smuzhiyun 		 * group.
2450*4882a593Smuzhiyun 		 */
2451*4882a593Smuzhiyun 		if (bmp->db_agpref > bmp->db_maxag)
2452*4882a593Smuzhiyun 			bmp->db_agpref = bmp->db_maxag;
2453*4882a593Smuzhiyun 	}
2454*4882a593Smuzhiyun 
2455*4882a593Smuzhiyun 	BMAP_UNLOCK(bmp);
2456*4882a593Smuzhiyun 
2457*4882a593Smuzhiyun 	return 0;
2458*4882a593Smuzhiyun }
2459*4882a593Smuzhiyun 
2460*4882a593Smuzhiyun 
2461*4882a593Smuzhiyun /*
2462*4882a593Smuzhiyun  * NAME:	dbAdjCtl()
2463*4882a593Smuzhiyun  *
2464*4882a593Smuzhiyun  * FUNCTION:	adjust a dmap control page at a specified level to reflect
2465*4882a593Smuzhiyun  *		the change in a lower level dmap or dmap control page's
2466*4882a593Smuzhiyun  *		maximum string of free blocks (i.e. a change in the root
2467*4882a593Smuzhiyun  *		of the lower level object's dmtree) due to the allocation
2468*4882a593Smuzhiyun  *		or deallocation of a range of blocks with a single dmap.
2469*4882a593Smuzhiyun  *
2470*4882a593Smuzhiyun  *		on entry, this routine is provided with the new value of
2471*4882a593Smuzhiyun  *		the lower level dmap or dmap control page root and the
2472*4882a593Smuzhiyun  *		starting block number of the block range whose allocation
2473*4882a593Smuzhiyun  *		or deallocation resulted in the root change.  this range
2474*4882a593Smuzhiyun  *		is respresented by a single leaf of the current dmapctl
2475*4882a593Smuzhiyun  *		and the leaf will be updated with this value, possibly
2476*4882a593Smuzhiyun  *		causing a binary buddy system within the leaves to be
2477*4882a593Smuzhiyun  *		split or joined.  the update may also cause the dmapctl's
2478*4882a593Smuzhiyun  *		dmtree to be updated.
2479*4882a593Smuzhiyun  *
2480*4882a593Smuzhiyun  *		if the adjustment of the dmap control page, itself, causes its
2481*4882a593Smuzhiyun  *		root to change, this change will be bubbled up to the next dmap
2482*4882a593Smuzhiyun  *		control level by a recursive call to this routine, specifying
2483*4882a593Smuzhiyun  *		the new root value and the next dmap control page level to
2484*4882a593Smuzhiyun  *		be adjusted.
2485*4882a593Smuzhiyun  * PARAMETERS:
2486*4882a593Smuzhiyun  *	bmp	-  pointer to bmap descriptor
2487*4882a593Smuzhiyun  *	blkno	-  the first block of a block range within a dmap.  it is
2488*4882a593Smuzhiyun  *		   the allocation or deallocation of this block range that
2489*4882a593Smuzhiyun  *		   requires the dmap control page to be adjusted.
2490*4882a593Smuzhiyun  *	newval	-  the new value of the lower level dmap or dmap control
2491*4882a593Smuzhiyun  *		   page root.
2492*4882a593Smuzhiyun  *	alloc	-  'true' if adjustment is due to an allocation.
2493*4882a593Smuzhiyun  *	level	-  current level of dmap control page (i.e. L0, L1, L2) to
2494*4882a593Smuzhiyun  *		   be adjusted.
2495*4882a593Smuzhiyun  *
2496*4882a593Smuzhiyun  * RETURN VALUES:
2497*4882a593Smuzhiyun  *	0	- success
2498*4882a593Smuzhiyun  *	-EIO	- i/o error
2499*4882a593Smuzhiyun  *
2500*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2501*4882a593Smuzhiyun  */
2502*4882a593Smuzhiyun static int
dbAdjCtl(struct bmap * bmp,s64 blkno,int newval,int alloc,int level)2503*4882a593Smuzhiyun dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc, int level)
2504*4882a593Smuzhiyun {
2505*4882a593Smuzhiyun 	struct metapage *mp;
2506*4882a593Smuzhiyun 	s8 oldroot;
2507*4882a593Smuzhiyun 	int oldval;
2508*4882a593Smuzhiyun 	s64 lblkno;
2509*4882a593Smuzhiyun 	struct dmapctl *dcp;
2510*4882a593Smuzhiyun 	int rc, leafno, ti;
2511*4882a593Smuzhiyun 
2512*4882a593Smuzhiyun 	/* get the buffer for the dmap control page for the specified
2513*4882a593Smuzhiyun 	 * block number and control page level.
2514*4882a593Smuzhiyun 	 */
2515*4882a593Smuzhiyun 	lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level);
2516*4882a593Smuzhiyun 	mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
2517*4882a593Smuzhiyun 	if (mp == NULL)
2518*4882a593Smuzhiyun 		return -EIO;
2519*4882a593Smuzhiyun 	dcp = (struct dmapctl *) mp->data;
2520*4882a593Smuzhiyun 
2521*4882a593Smuzhiyun 	if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
2522*4882a593Smuzhiyun 		jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n");
2523*4882a593Smuzhiyun 		release_metapage(mp);
2524*4882a593Smuzhiyun 		return -EIO;
2525*4882a593Smuzhiyun 	}
2526*4882a593Smuzhiyun 
2527*4882a593Smuzhiyun 	/* determine the leaf number corresponding to the block and
2528*4882a593Smuzhiyun 	 * the index within the dmap control tree.
2529*4882a593Smuzhiyun 	 */
2530*4882a593Smuzhiyun 	leafno = BLKTOCTLLEAF(blkno, dcp->budmin);
2531*4882a593Smuzhiyun 	ti = leafno + le32_to_cpu(dcp->leafidx);
2532*4882a593Smuzhiyun 
2533*4882a593Smuzhiyun 	/* save the current leaf value and the current root level (i.e.
2534*4882a593Smuzhiyun 	 * maximum l2 free string described by this dmapctl).
2535*4882a593Smuzhiyun 	 */
2536*4882a593Smuzhiyun 	oldval = dcp->stree[ti];
2537*4882a593Smuzhiyun 	oldroot = dcp->stree[ROOT];
2538*4882a593Smuzhiyun 
2539*4882a593Smuzhiyun 	/* check if this is a control page update for an allocation.
2540*4882a593Smuzhiyun 	 * if so, update the leaf to reflect the new leaf value using
2541*4882a593Smuzhiyun 	 * dbSplit(); otherwise (deallocation), use dbJoin() to update
2542*4882a593Smuzhiyun 	 * the leaf with the new value.  in addition to updating the
2543*4882a593Smuzhiyun 	 * leaf, dbSplit() will also split the binary buddy system of
2544*4882a593Smuzhiyun 	 * the leaves, if required, and bubble new values within the
2545*4882a593Smuzhiyun 	 * dmapctl tree, if required.  similarly, dbJoin() will join
2546*4882a593Smuzhiyun 	 * the binary buddy system of leaves and bubble new values up
2547*4882a593Smuzhiyun 	 * the dmapctl tree as required by the new leaf value.
2548*4882a593Smuzhiyun 	 */
2549*4882a593Smuzhiyun 	if (alloc) {
2550*4882a593Smuzhiyun 		/* check if we are in the middle of a binary buddy
2551*4882a593Smuzhiyun 		 * system.  this happens when we are performing the
2552*4882a593Smuzhiyun 		 * first allocation out of an allocation group that
2553*4882a593Smuzhiyun 		 * is part (not the first part) of a larger binary
2554*4882a593Smuzhiyun 		 * buddy system.  if we are in the middle, back split
2555*4882a593Smuzhiyun 		 * the system prior to calling dbSplit() which assumes
2556*4882a593Smuzhiyun 		 * that it is at the front of a binary buddy system.
2557*4882a593Smuzhiyun 		 */
2558*4882a593Smuzhiyun 		if (oldval == NOFREE) {
2559*4882a593Smuzhiyun 			rc = dbBackSplit((dmtree_t *) dcp, leafno);
2560*4882a593Smuzhiyun 			if (rc)
2561*4882a593Smuzhiyun 				return rc;
2562*4882a593Smuzhiyun 			oldval = dcp->stree[ti];
2563*4882a593Smuzhiyun 		}
2564*4882a593Smuzhiyun 		dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval);
2565*4882a593Smuzhiyun 	} else {
2566*4882a593Smuzhiyun 		rc = dbJoin((dmtree_t *) dcp, leafno, newval);
2567*4882a593Smuzhiyun 		if (rc)
2568*4882a593Smuzhiyun 			return rc;
2569*4882a593Smuzhiyun 	}
2570*4882a593Smuzhiyun 
2571*4882a593Smuzhiyun 	/* check if the root of the current dmap control page changed due
2572*4882a593Smuzhiyun 	 * to the update and if the current dmap control page is not at
2573*4882a593Smuzhiyun 	 * the current top level (i.e. L0, L1, L2) of the map.  if so (i.e.
2574*4882a593Smuzhiyun 	 * root changed and this is not the top level), call this routine
2575*4882a593Smuzhiyun 	 * again (recursion) for the next higher level of the mapping to
2576*4882a593Smuzhiyun 	 * reflect the change in root for the current dmap control page.
2577*4882a593Smuzhiyun 	 */
2578*4882a593Smuzhiyun 	if (dcp->stree[ROOT] != oldroot) {
2579*4882a593Smuzhiyun 		/* are we below the top level of the map.  if so,
2580*4882a593Smuzhiyun 		 * bubble the root up to the next higher level.
2581*4882a593Smuzhiyun 		 */
2582*4882a593Smuzhiyun 		if (level < bmp->db_maxlevel) {
2583*4882a593Smuzhiyun 			/* bubble up the new root of this dmap control page to
2584*4882a593Smuzhiyun 			 * the next level.
2585*4882a593Smuzhiyun 			 */
2586*4882a593Smuzhiyun 			if ((rc =
2587*4882a593Smuzhiyun 			     dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc,
2588*4882a593Smuzhiyun 				      level + 1))) {
2589*4882a593Smuzhiyun 				/* something went wrong in bubbling up the new
2590*4882a593Smuzhiyun 				 * root value, so backout the changes to the
2591*4882a593Smuzhiyun 				 * current dmap control page.
2592*4882a593Smuzhiyun 				 */
2593*4882a593Smuzhiyun 				if (alloc) {
2594*4882a593Smuzhiyun 					dbJoin((dmtree_t *) dcp, leafno,
2595*4882a593Smuzhiyun 					       oldval);
2596*4882a593Smuzhiyun 				} else {
2597*4882a593Smuzhiyun 					/* the dbJoin() above might have
2598*4882a593Smuzhiyun 					 * caused a larger binary buddy system
2599*4882a593Smuzhiyun 					 * to form and we may now be in the
2600*4882a593Smuzhiyun 					 * middle of it.  if this is the case,
2601*4882a593Smuzhiyun 					 * back split the buddies.
2602*4882a593Smuzhiyun 					 */
2603*4882a593Smuzhiyun 					if (dcp->stree[ti] == NOFREE)
2604*4882a593Smuzhiyun 						dbBackSplit((dmtree_t *)
2605*4882a593Smuzhiyun 							    dcp, leafno);
2606*4882a593Smuzhiyun 					dbSplit((dmtree_t *) dcp, leafno,
2607*4882a593Smuzhiyun 						dcp->budmin, oldval);
2608*4882a593Smuzhiyun 				}
2609*4882a593Smuzhiyun 
2610*4882a593Smuzhiyun 				/* release the buffer and return the error.
2611*4882a593Smuzhiyun 				 */
2612*4882a593Smuzhiyun 				release_metapage(mp);
2613*4882a593Smuzhiyun 				return (rc);
2614*4882a593Smuzhiyun 			}
2615*4882a593Smuzhiyun 		} else {
2616*4882a593Smuzhiyun 			/* we're at the top level of the map. update
2617*4882a593Smuzhiyun 			 * the bmap control page to reflect the size
2618*4882a593Smuzhiyun 			 * of the maximum free buddy system.
2619*4882a593Smuzhiyun 			 */
2620*4882a593Smuzhiyun 			assert(level == bmp->db_maxlevel);
2621*4882a593Smuzhiyun 			if (bmp->db_maxfreebud != oldroot) {
2622*4882a593Smuzhiyun 				jfs_error(bmp->db_ipbmap->i_sb,
2623*4882a593Smuzhiyun 					  "the maximum free buddy is not the old root\n");
2624*4882a593Smuzhiyun 			}
2625*4882a593Smuzhiyun 			bmp->db_maxfreebud = dcp->stree[ROOT];
2626*4882a593Smuzhiyun 		}
2627*4882a593Smuzhiyun 	}
2628*4882a593Smuzhiyun 
2629*4882a593Smuzhiyun 	/* write the buffer.
2630*4882a593Smuzhiyun 	 */
2631*4882a593Smuzhiyun 	write_metapage(mp);
2632*4882a593Smuzhiyun 
2633*4882a593Smuzhiyun 	return (0);
2634*4882a593Smuzhiyun }
2635*4882a593Smuzhiyun 
2636*4882a593Smuzhiyun 
2637*4882a593Smuzhiyun /*
2638*4882a593Smuzhiyun  * NAME:	dbSplit()
2639*4882a593Smuzhiyun  *
2640*4882a593Smuzhiyun  * FUNCTION:	update the leaf of a dmtree with a new value, splitting
2641*4882a593Smuzhiyun  *		the leaf from the binary buddy system of the dmtree's
2642*4882a593Smuzhiyun  *		leaves, as required.
2643*4882a593Smuzhiyun  *
2644*4882a593Smuzhiyun  * PARAMETERS:
2645*4882a593Smuzhiyun  *	tp	- pointer to the tree containing the leaf.
2646*4882a593Smuzhiyun  *	leafno	- the number of the leaf to be updated.
2647*4882a593Smuzhiyun  *	splitsz	- the size the binary buddy system starting at the leaf
2648*4882a593Smuzhiyun  *		  must be split to, specified as the log2 number of blocks.
2649*4882a593Smuzhiyun  *	newval	- the new value for the leaf.
2650*4882a593Smuzhiyun  *
2651*4882a593Smuzhiyun  * RETURN VALUES: none
2652*4882a593Smuzhiyun  *
2653*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2654*4882a593Smuzhiyun  */
dbSplit(dmtree_t * tp,int leafno,int splitsz,int newval)2655*4882a593Smuzhiyun static void dbSplit(dmtree_t * tp, int leafno, int splitsz, int newval)
2656*4882a593Smuzhiyun {
2657*4882a593Smuzhiyun 	int budsz;
2658*4882a593Smuzhiyun 	int cursz;
2659*4882a593Smuzhiyun 	s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2660*4882a593Smuzhiyun 
2661*4882a593Smuzhiyun 	/* check if the leaf needs to be split.
2662*4882a593Smuzhiyun 	 */
2663*4882a593Smuzhiyun 	if (leaf[leafno] > tp->dmt_budmin) {
2664*4882a593Smuzhiyun 		/* the split occurs by cutting the buddy system in half
2665*4882a593Smuzhiyun 		 * at the specified leaf until we reach the specified
2666*4882a593Smuzhiyun 		 * size.  pick up the starting split size (current size
2667*4882a593Smuzhiyun 		 * - 1 in l2) and the corresponding buddy size.
2668*4882a593Smuzhiyun 		 */
2669*4882a593Smuzhiyun 		cursz = leaf[leafno] - 1;
2670*4882a593Smuzhiyun 		budsz = BUDSIZE(cursz, tp->dmt_budmin);
2671*4882a593Smuzhiyun 
2672*4882a593Smuzhiyun 		/* split until we reach the specified size.
2673*4882a593Smuzhiyun 		 */
2674*4882a593Smuzhiyun 		while (cursz >= splitsz) {
2675*4882a593Smuzhiyun 			/* update the buddy's leaf with its new value.
2676*4882a593Smuzhiyun 			 */
2677*4882a593Smuzhiyun 			dbAdjTree(tp, leafno ^ budsz, cursz);
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 			/* on to the next size and buddy.
2680*4882a593Smuzhiyun 			 */
2681*4882a593Smuzhiyun 			cursz -= 1;
2682*4882a593Smuzhiyun 			budsz >>= 1;
2683*4882a593Smuzhiyun 		}
2684*4882a593Smuzhiyun 	}
2685*4882a593Smuzhiyun 
2686*4882a593Smuzhiyun 	/* adjust the dmap tree to reflect the specified leaf's new
2687*4882a593Smuzhiyun 	 * value.
2688*4882a593Smuzhiyun 	 */
2689*4882a593Smuzhiyun 	dbAdjTree(tp, leafno, newval);
2690*4882a593Smuzhiyun }
2691*4882a593Smuzhiyun 
2692*4882a593Smuzhiyun 
2693*4882a593Smuzhiyun /*
2694*4882a593Smuzhiyun  * NAME:	dbBackSplit()
2695*4882a593Smuzhiyun  *
2696*4882a593Smuzhiyun  * FUNCTION:	back split the binary buddy system of dmtree leaves
2697*4882a593Smuzhiyun  *		that hold a specified leaf until the specified leaf
2698*4882a593Smuzhiyun  *		starts its own binary buddy system.
2699*4882a593Smuzhiyun  *
2700*4882a593Smuzhiyun  *		the allocators typically perform allocations at the start
2701*4882a593Smuzhiyun  *		of binary buddy systems and dbSplit() is used to accomplish
2702*4882a593Smuzhiyun  *		any required splits.  in some cases, however, allocation
2703*4882a593Smuzhiyun  *		may occur in the middle of a binary system and requires a
2704*4882a593Smuzhiyun  *		back split, with the split proceeding out from the middle of
2705*4882a593Smuzhiyun  *		the system (less efficient) rather than the start of the
2706*4882a593Smuzhiyun  *		system (more efficient).  the cases in which a back split
2707*4882a593Smuzhiyun  *		is required are rare and are limited to the first allocation
2708*4882a593Smuzhiyun  *		within an allocation group which is a part (not first part)
2709*4882a593Smuzhiyun  *		of a larger binary buddy system and a few exception cases
2710*4882a593Smuzhiyun  *		in which a previous join operation must be backed out.
2711*4882a593Smuzhiyun  *
2712*4882a593Smuzhiyun  * PARAMETERS:
2713*4882a593Smuzhiyun  *	tp	- pointer to the tree containing the leaf.
2714*4882a593Smuzhiyun  *	leafno	- the number of the leaf to be updated.
2715*4882a593Smuzhiyun  *
2716*4882a593Smuzhiyun  * RETURN VALUES: none
2717*4882a593Smuzhiyun  *
2718*4882a593Smuzhiyun  * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2719*4882a593Smuzhiyun  */
dbBackSplit(dmtree_t * tp,int leafno)2720*4882a593Smuzhiyun static int dbBackSplit(dmtree_t * tp, int leafno)
2721*4882a593Smuzhiyun {
2722*4882a593Smuzhiyun 	int budsz, bud, w, bsz, size;
2723*4882a593Smuzhiyun 	int cursz;
2724*4882a593Smuzhiyun 	s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2725*4882a593Smuzhiyun 
2726*4882a593Smuzhiyun 	/* leaf should be part (not first part) of a binary
2727*4882a593Smuzhiyun 	 * buddy system.
2728*4882a593Smuzhiyun 	 */
2729*4882a593Smuzhiyun 	assert(leaf[leafno] == NOFREE);
2730*4882a593Smuzhiyun 
2731*4882a593Smuzhiyun 	/* the back split is accomplished by iteratively finding the leaf
2732*4882a593Smuzhiyun 	 * that starts the buddy system that contains the specified leaf and
2733*4882a593Smuzhiyun 	 * splitting that system in two.  this iteration continues until
2734*4882a593Smuzhiyun 	 * the specified leaf becomes the start of a buddy system.
2735*4882a593Smuzhiyun 	 *
2736*4882a593Smuzhiyun 	 * determine maximum possible l2 size for the specified leaf.
2737*4882a593Smuzhiyun 	 */
2738*4882a593Smuzhiyun 	size =
2739*4882a593Smuzhiyun 	    LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs),
2740*4882a593Smuzhiyun 		      tp->dmt_budmin);
2741*4882a593Smuzhiyun 
2742*4882a593Smuzhiyun 	/* determine the number of leaves covered by this size.  this
2743*4882a593Smuzhiyun 	 * is the buddy size that we will start with as we search for
2744*4882a593Smuzhiyun 	 * the buddy system that contains the specified leaf.
2745*4882a593Smuzhiyun 	 */
2746*4882a593Smuzhiyun 	budsz = BUDSIZE(size, tp->dmt_budmin);
2747*4882a593Smuzhiyun 
2748*4882a593Smuzhiyun 	/* back split.
2749*4882a593Smuzhiyun 	 */
2750*4882a593Smuzhiyun 	while (leaf[leafno] == NOFREE) {
2751*4882a593Smuzhiyun 		/* find the leftmost buddy leaf.
2752*4882a593Smuzhiyun 		 */
2753*4882a593Smuzhiyun 		for (w = leafno, bsz = budsz;; bsz <<= 1,
2754*4882a593Smuzhiyun 		     w = (w < bud) ? w : bud) {
2755*4882a593Smuzhiyun 			if (bsz >= le32_to_cpu(tp->dmt_nleafs)) {
2756*4882a593Smuzhiyun 				jfs_err("JFS: block map error in dbBackSplit");
2757*4882a593Smuzhiyun 				return -EIO;
2758*4882a593Smuzhiyun 			}
2759*4882a593Smuzhiyun 
2760*4882a593Smuzhiyun 			/* determine the buddy.
2761*4882a593Smuzhiyun 			 */
2762*4882a593Smuzhiyun 			bud = w ^ bsz;
2763*4882a593Smuzhiyun 
2764*4882a593Smuzhiyun 			/* check if this buddy is the start of the system.
2765*4882a593Smuzhiyun 			 */
2766*4882a593Smuzhiyun 			if (leaf[bud] != NOFREE) {
2767*4882a593Smuzhiyun 				/* split the leaf at the start of the
2768*4882a593Smuzhiyun 				 * system in two.
2769*4882a593Smuzhiyun 				 */
2770*4882a593Smuzhiyun 				cursz = leaf[bud] - 1;
2771*4882a593Smuzhiyun 				dbSplit(tp, bud, cursz, cursz);
2772*4882a593Smuzhiyun 				break;
2773*4882a593Smuzhiyun 			}
2774*4882a593Smuzhiyun 		}
2775*4882a593Smuzhiyun 	}
2776*4882a593Smuzhiyun 
2777*4882a593Smuzhiyun 	if (leaf[leafno] != size) {
2778*4882a593Smuzhiyun 		jfs_err("JFS: wrong leaf value in dbBackSplit");
2779*4882a593Smuzhiyun 		return -EIO;
2780*4882a593Smuzhiyun 	}
2781*4882a593Smuzhiyun 	return 0;
2782*4882a593Smuzhiyun }
2783*4882a593Smuzhiyun 
2784*4882a593Smuzhiyun 
2785*4882a593Smuzhiyun /*
2786*4882a593Smuzhiyun  * NAME:	dbJoin()
2787*4882a593Smuzhiyun  *
2788*4882a593Smuzhiyun  * FUNCTION:	update the leaf of a dmtree with a new value, joining
2789*4882a593Smuzhiyun  *		the leaf with other leaves of the dmtree into a multi-leaf
2790*4882a593Smuzhiyun  *		binary buddy system, as required.
2791*4882a593Smuzhiyun  *
2792*4882a593Smuzhiyun  * PARAMETERS:
2793*4882a593Smuzhiyun  *	tp	- pointer to the tree containing the leaf.
2794*4882a593Smuzhiyun  *	leafno	- the number of the leaf to be updated.
2795*4882a593Smuzhiyun  *	newval	- the new value for the leaf.
2796*4882a593Smuzhiyun  *
2797*4882a593Smuzhiyun  * RETURN VALUES: none
2798*4882a593Smuzhiyun  */
dbJoin(dmtree_t * tp,int leafno,int newval)2799*4882a593Smuzhiyun static int dbJoin(dmtree_t * tp, int leafno, int newval)
2800*4882a593Smuzhiyun {
2801*4882a593Smuzhiyun 	int budsz, buddy;
2802*4882a593Smuzhiyun 	s8 *leaf;
2803*4882a593Smuzhiyun 
2804*4882a593Smuzhiyun 	/* can the new leaf value require a join with other leaves ?
2805*4882a593Smuzhiyun 	 */
2806*4882a593Smuzhiyun 	if (newval >= tp->dmt_budmin) {
2807*4882a593Smuzhiyun 		/* pickup a pointer to the leaves of the tree.
2808*4882a593Smuzhiyun 		 */
2809*4882a593Smuzhiyun 		leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2810*4882a593Smuzhiyun 
2811*4882a593Smuzhiyun 		/* try to join the specified leaf into a large binary
2812*4882a593Smuzhiyun 		 * buddy system.  the join proceeds by attempting to join
2813*4882a593Smuzhiyun 		 * the specified leafno with its buddy (leaf) at new value.
2814*4882a593Smuzhiyun 		 * if the join occurs, we attempt to join the left leaf
2815*4882a593Smuzhiyun 		 * of the joined buddies with its buddy at new value + 1.
2816*4882a593Smuzhiyun 		 * we continue to join until we find a buddy that cannot be
2817*4882a593Smuzhiyun 		 * joined (does not have a value equal to the size of the
2818*4882a593Smuzhiyun 		 * last join) or until all leaves have been joined into a
2819*4882a593Smuzhiyun 		 * single system.
2820*4882a593Smuzhiyun 		 *
2821*4882a593Smuzhiyun 		 * get the buddy size (number of words covered) of
2822*4882a593Smuzhiyun 		 * the new value.
2823*4882a593Smuzhiyun 		 */
2824*4882a593Smuzhiyun 		budsz = BUDSIZE(newval, tp->dmt_budmin);
2825*4882a593Smuzhiyun 
2826*4882a593Smuzhiyun 		/* try to join.
2827*4882a593Smuzhiyun 		 */
2828*4882a593Smuzhiyun 		while (budsz < le32_to_cpu(tp->dmt_nleafs)) {
2829*4882a593Smuzhiyun 			/* get the buddy leaf.
2830*4882a593Smuzhiyun 			 */
2831*4882a593Smuzhiyun 			buddy = leafno ^ budsz;
2832*4882a593Smuzhiyun 
2833*4882a593Smuzhiyun 			/* if the leaf's new value is greater than its
2834*4882a593Smuzhiyun 			 * buddy's value, we join no more.
2835*4882a593Smuzhiyun 			 */
2836*4882a593Smuzhiyun 			if (newval > leaf[buddy])
2837*4882a593Smuzhiyun 				break;
2838*4882a593Smuzhiyun 
2839*4882a593Smuzhiyun 			/* It shouldn't be less */
2840*4882a593Smuzhiyun 			if (newval < leaf[buddy])
2841*4882a593Smuzhiyun 				return -EIO;
2842*4882a593Smuzhiyun 
2843*4882a593Smuzhiyun 			/* check which (leafno or buddy) is the left buddy.
2844*4882a593Smuzhiyun 			 * the left buddy gets to claim the blocks resulting
2845*4882a593Smuzhiyun 			 * from the join while the right gets to claim none.
2846*4882a593Smuzhiyun 			 * the left buddy is also eligible to participate in
2847*4882a593Smuzhiyun 			 * a join at the next higher level while the right
2848*4882a593Smuzhiyun 			 * is not.
2849*4882a593Smuzhiyun 			 *
2850*4882a593Smuzhiyun 			 */
2851*4882a593Smuzhiyun 			if (leafno < buddy) {
2852*4882a593Smuzhiyun 				/* leafno is the left buddy.
2853*4882a593Smuzhiyun 				 */
2854*4882a593Smuzhiyun 				dbAdjTree(tp, buddy, NOFREE);
2855*4882a593Smuzhiyun 			} else {
2856*4882a593Smuzhiyun 				/* buddy is the left buddy and becomes
2857*4882a593Smuzhiyun 				 * leafno.
2858*4882a593Smuzhiyun 				 */
2859*4882a593Smuzhiyun 				dbAdjTree(tp, leafno, NOFREE);
2860*4882a593Smuzhiyun 				leafno = buddy;
2861*4882a593Smuzhiyun 			}
2862*4882a593Smuzhiyun 
2863*4882a593Smuzhiyun 			/* on to try the next join.
2864*4882a593Smuzhiyun 			 */
2865*4882a593Smuzhiyun 			newval += 1;
2866*4882a593Smuzhiyun 			budsz <<= 1;
2867*4882a593Smuzhiyun 		}
2868*4882a593Smuzhiyun 	}
2869*4882a593Smuzhiyun 
2870*4882a593Smuzhiyun 	/* update the leaf value.
2871*4882a593Smuzhiyun 	 */
2872*4882a593Smuzhiyun 	dbAdjTree(tp, leafno, newval);
2873*4882a593Smuzhiyun 
2874*4882a593Smuzhiyun 	return 0;
2875*4882a593Smuzhiyun }
2876*4882a593Smuzhiyun 
2877*4882a593Smuzhiyun 
2878*4882a593Smuzhiyun /*
2879*4882a593Smuzhiyun  * NAME:	dbAdjTree()
2880*4882a593Smuzhiyun  *
2881*4882a593Smuzhiyun  * FUNCTION:	update a leaf of a dmtree with a new value, adjusting
2882*4882a593Smuzhiyun  *		the dmtree, as required, to reflect the new leaf value.
2883*4882a593Smuzhiyun  *		the combination of any buddies must already be done before
2884*4882a593Smuzhiyun  *		this is called.
2885*4882a593Smuzhiyun  *
2886*4882a593Smuzhiyun  * PARAMETERS:
2887*4882a593Smuzhiyun  *	tp	- pointer to the tree to be adjusted.
2888*4882a593Smuzhiyun  *	leafno	- the number of the leaf to be updated.
2889*4882a593Smuzhiyun  *	newval	- the new value for the leaf.
2890*4882a593Smuzhiyun  *
2891*4882a593Smuzhiyun  * RETURN VALUES: none
2892*4882a593Smuzhiyun  */
dbAdjTree(dmtree_t * tp,int leafno,int newval)2893*4882a593Smuzhiyun static void dbAdjTree(dmtree_t * tp, int leafno, int newval)
2894*4882a593Smuzhiyun {
2895*4882a593Smuzhiyun 	int lp, pp, k;
2896*4882a593Smuzhiyun 	int max;
2897*4882a593Smuzhiyun 
2898*4882a593Smuzhiyun 	/* pick up the index of the leaf for this leafno.
2899*4882a593Smuzhiyun 	 */
2900*4882a593Smuzhiyun 	lp = leafno + le32_to_cpu(tp->dmt_leafidx);
2901*4882a593Smuzhiyun 
2902*4882a593Smuzhiyun 	/* is the current value the same as the old value ?  if so,
2903*4882a593Smuzhiyun 	 * there is nothing to do.
2904*4882a593Smuzhiyun 	 */
2905*4882a593Smuzhiyun 	if (tp->dmt_stree[lp] == newval)
2906*4882a593Smuzhiyun 		return;
2907*4882a593Smuzhiyun 
2908*4882a593Smuzhiyun 	/* set the new value.
2909*4882a593Smuzhiyun 	 */
2910*4882a593Smuzhiyun 	tp->dmt_stree[lp] = newval;
2911*4882a593Smuzhiyun 
2912*4882a593Smuzhiyun 	/* bubble the new value up the tree as required.
2913*4882a593Smuzhiyun 	 */
2914*4882a593Smuzhiyun 	for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) {
2915*4882a593Smuzhiyun 		/* get the index of the first leaf of the 4 leaf
2916*4882a593Smuzhiyun 		 * group containing the specified leaf (leafno).
2917*4882a593Smuzhiyun 		 */
2918*4882a593Smuzhiyun 		lp = ((lp - 1) & ~0x03) + 1;
2919*4882a593Smuzhiyun 
2920*4882a593Smuzhiyun 		/* get the index of the parent of this 4 leaf group.
2921*4882a593Smuzhiyun 		 */
2922*4882a593Smuzhiyun 		pp = (lp - 1) >> 2;
2923*4882a593Smuzhiyun 
2924*4882a593Smuzhiyun 		/* determine the maximum of the 4 leaves.
2925*4882a593Smuzhiyun 		 */
2926*4882a593Smuzhiyun 		max = TREEMAX(&tp->dmt_stree[lp]);
2927*4882a593Smuzhiyun 
2928*4882a593Smuzhiyun 		/* if the maximum of the 4 is the same as the
2929*4882a593Smuzhiyun 		 * parent's value, we're done.
2930*4882a593Smuzhiyun 		 */
2931*4882a593Smuzhiyun 		if (tp->dmt_stree[pp] == max)
2932*4882a593Smuzhiyun 			break;
2933*4882a593Smuzhiyun 
2934*4882a593Smuzhiyun 		/* parent gets new value.
2935*4882a593Smuzhiyun 		 */
2936*4882a593Smuzhiyun 		tp->dmt_stree[pp] = max;
2937*4882a593Smuzhiyun 
2938*4882a593Smuzhiyun 		/* parent becomes leaf for next go-round.
2939*4882a593Smuzhiyun 		 */
2940*4882a593Smuzhiyun 		lp = pp;
2941*4882a593Smuzhiyun 	}
2942*4882a593Smuzhiyun }
2943*4882a593Smuzhiyun 
2944*4882a593Smuzhiyun 
2945*4882a593Smuzhiyun /*
2946*4882a593Smuzhiyun  * NAME:	dbFindLeaf()
2947*4882a593Smuzhiyun  *
2948*4882a593Smuzhiyun  * FUNCTION:	search a dmtree_t for sufficient free blocks, returning
2949*4882a593Smuzhiyun  *		the index of a leaf describing the free blocks if
2950*4882a593Smuzhiyun  *		sufficient free blocks are found.
2951*4882a593Smuzhiyun  *
2952*4882a593Smuzhiyun  *		the search starts at the top of the dmtree_t tree and
2953*4882a593Smuzhiyun  *		proceeds down the tree to the leftmost leaf with sufficient
2954*4882a593Smuzhiyun  *		free space.
2955*4882a593Smuzhiyun  *
2956*4882a593Smuzhiyun  * PARAMETERS:
2957*4882a593Smuzhiyun  *	tp	- pointer to the tree to be searched.
2958*4882a593Smuzhiyun  *	l2nb	- log2 number of free blocks to search for.
2959*4882a593Smuzhiyun  *	leafidx	- return pointer to be set to the index of the leaf
2960*4882a593Smuzhiyun  *		  describing at least l2nb free blocks if sufficient
2961*4882a593Smuzhiyun  *		  free blocks are found.
2962*4882a593Smuzhiyun  *
2963*4882a593Smuzhiyun  * RETURN VALUES:
2964*4882a593Smuzhiyun  *	0	- success
2965*4882a593Smuzhiyun  *	-ENOSPC	- insufficient free blocks.
2966*4882a593Smuzhiyun  */
dbFindLeaf(dmtree_t * tp,int l2nb,int * leafidx)2967*4882a593Smuzhiyun static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx)
2968*4882a593Smuzhiyun {
2969*4882a593Smuzhiyun 	int ti, n = 0, k, x = 0;
2970*4882a593Smuzhiyun 
2971*4882a593Smuzhiyun 	/* first check the root of the tree to see if there is
2972*4882a593Smuzhiyun 	 * sufficient free space.
2973*4882a593Smuzhiyun 	 */
2974*4882a593Smuzhiyun 	if (l2nb > tp->dmt_stree[ROOT])
2975*4882a593Smuzhiyun 		return -ENOSPC;
2976*4882a593Smuzhiyun 
2977*4882a593Smuzhiyun 	/* sufficient free space available. now search down the tree
2978*4882a593Smuzhiyun 	 * starting at the next level for the leftmost leaf that
2979*4882a593Smuzhiyun 	 * describes sufficient free space.
2980*4882a593Smuzhiyun 	 */
2981*4882a593Smuzhiyun 	for (k = le32_to_cpu(tp->dmt_height), ti = 1;
2982*4882a593Smuzhiyun 	     k > 0; k--, ti = ((ti + n) << 2) + 1) {
2983*4882a593Smuzhiyun 		/* search the four nodes at this level, starting from
2984*4882a593Smuzhiyun 		 * the left.
2985*4882a593Smuzhiyun 		 */
2986*4882a593Smuzhiyun 		for (x = ti, n = 0; n < 4; n++) {
2987*4882a593Smuzhiyun 			/* sufficient free space found.  move to the next
2988*4882a593Smuzhiyun 			 * level (or quit if this is the last level).
2989*4882a593Smuzhiyun 			 */
2990*4882a593Smuzhiyun 			if (l2nb <= tp->dmt_stree[x + n])
2991*4882a593Smuzhiyun 				break;
2992*4882a593Smuzhiyun 		}
2993*4882a593Smuzhiyun 
2994*4882a593Smuzhiyun 		/* better have found something since the higher
2995*4882a593Smuzhiyun 		 * levels of the tree said it was here.
2996*4882a593Smuzhiyun 		 */
2997*4882a593Smuzhiyun 		assert(n < 4);
2998*4882a593Smuzhiyun 	}
2999*4882a593Smuzhiyun 
3000*4882a593Smuzhiyun 	/* set the return to the leftmost leaf describing sufficient
3001*4882a593Smuzhiyun 	 * free space.
3002*4882a593Smuzhiyun 	 */
3003*4882a593Smuzhiyun 	*leafidx = x + n - le32_to_cpu(tp->dmt_leafidx);
3004*4882a593Smuzhiyun 
3005*4882a593Smuzhiyun 	return (0);
3006*4882a593Smuzhiyun }
3007*4882a593Smuzhiyun 
3008*4882a593Smuzhiyun 
3009*4882a593Smuzhiyun /*
3010*4882a593Smuzhiyun  * NAME:	dbFindBits()
3011*4882a593Smuzhiyun  *
3012*4882a593Smuzhiyun  * FUNCTION:	find a specified number of binary buddy free bits within a
3013*4882a593Smuzhiyun  *		dmap bitmap word value.
3014*4882a593Smuzhiyun  *
3015*4882a593Smuzhiyun  *		this routine searches the bitmap value for (1 << l2nb) free
3016*4882a593Smuzhiyun  *		bits at (1 << l2nb) alignments within the value.
3017*4882a593Smuzhiyun  *
3018*4882a593Smuzhiyun  * PARAMETERS:
3019*4882a593Smuzhiyun  *	word	-  dmap bitmap word value.
3020*4882a593Smuzhiyun  *	l2nb	-  number of free bits specified as a log2 number.
3021*4882a593Smuzhiyun  *
3022*4882a593Smuzhiyun  * RETURN VALUES:
3023*4882a593Smuzhiyun  *	starting bit number of free bits.
3024*4882a593Smuzhiyun  */
dbFindBits(u32 word,int l2nb)3025*4882a593Smuzhiyun static int dbFindBits(u32 word, int l2nb)
3026*4882a593Smuzhiyun {
3027*4882a593Smuzhiyun 	int bitno, nb;
3028*4882a593Smuzhiyun 	u32 mask;
3029*4882a593Smuzhiyun 
3030*4882a593Smuzhiyun 	/* get the number of bits.
3031*4882a593Smuzhiyun 	 */
3032*4882a593Smuzhiyun 	nb = 1 << l2nb;
3033*4882a593Smuzhiyun 	assert(nb <= DBWORD);
3034*4882a593Smuzhiyun 
3035*4882a593Smuzhiyun 	/* complement the word so we can use a mask (i.e. 0s represent
3036*4882a593Smuzhiyun 	 * free bits) and compute the mask.
3037*4882a593Smuzhiyun 	 */
3038*4882a593Smuzhiyun 	word = ~word;
3039*4882a593Smuzhiyun 	mask = ONES << (DBWORD - nb);
3040*4882a593Smuzhiyun 
3041*4882a593Smuzhiyun 	/* scan the word for nb free bits at nb alignments.
3042*4882a593Smuzhiyun 	 */
3043*4882a593Smuzhiyun 	for (bitno = 0; mask != 0; bitno += nb, mask >>= nb) {
3044*4882a593Smuzhiyun 		if ((mask & word) == mask)
3045*4882a593Smuzhiyun 			break;
3046*4882a593Smuzhiyun 	}
3047*4882a593Smuzhiyun 
3048*4882a593Smuzhiyun 	ASSERT(bitno < 32);
3049*4882a593Smuzhiyun 
3050*4882a593Smuzhiyun 	/* return the bit number.
3051*4882a593Smuzhiyun 	 */
3052*4882a593Smuzhiyun 	return (bitno);
3053*4882a593Smuzhiyun }
3054*4882a593Smuzhiyun 
3055*4882a593Smuzhiyun 
3056*4882a593Smuzhiyun /*
3057*4882a593Smuzhiyun  * NAME:	dbMaxBud(u8 *cp)
3058*4882a593Smuzhiyun  *
3059*4882a593Smuzhiyun  * FUNCTION:	determine the largest binary buddy string of free
3060*4882a593Smuzhiyun  *		bits within 32-bits of the map.
3061*4882a593Smuzhiyun  *
3062*4882a593Smuzhiyun  * PARAMETERS:
3063*4882a593Smuzhiyun  *	cp	-  pointer to the 32-bit value.
3064*4882a593Smuzhiyun  *
3065*4882a593Smuzhiyun  * RETURN VALUES:
3066*4882a593Smuzhiyun  *	largest binary buddy of free bits within a dmap word.
3067*4882a593Smuzhiyun  */
dbMaxBud(u8 * cp)3068*4882a593Smuzhiyun static int dbMaxBud(u8 * cp)
3069*4882a593Smuzhiyun {
3070*4882a593Smuzhiyun 	signed char tmp1, tmp2;
3071*4882a593Smuzhiyun 
3072*4882a593Smuzhiyun 	/* check if the wmap word is all free. if so, the
3073*4882a593Smuzhiyun 	 * free buddy size is BUDMIN.
3074*4882a593Smuzhiyun 	 */
3075*4882a593Smuzhiyun 	if (*((uint *) cp) == 0)
3076*4882a593Smuzhiyun 		return (BUDMIN);
3077*4882a593Smuzhiyun 
3078*4882a593Smuzhiyun 	/* check if the wmap word is half free. if so, the
3079*4882a593Smuzhiyun 	 * free buddy size is BUDMIN-1.
3080*4882a593Smuzhiyun 	 */
3081*4882a593Smuzhiyun 	if (*((u16 *) cp) == 0 || *((u16 *) cp + 1) == 0)
3082*4882a593Smuzhiyun 		return (BUDMIN - 1);
3083*4882a593Smuzhiyun 
3084*4882a593Smuzhiyun 	/* not all free or half free. determine the free buddy
3085*4882a593Smuzhiyun 	 * size thru table lookup using quarters of the wmap word.
3086*4882a593Smuzhiyun 	 */
3087*4882a593Smuzhiyun 	tmp1 = max(budtab[cp[2]], budtab[cp[3]]);
3088*4882a593Smuzhiyun 	tmp2 = max(budtab[cp[0]], budtab[cp[1]]);
3089*4882a593Smuzhiyun 	return (max(tmp1, tmp2));
3090*4882a593Smuzhiyun }
3091*4882a593Smuzhiyun 
3092*4882a593Smuzhiyun 
3093*4882a593Smuzhiyun /*
3094*4882a593Smuzhiyun  * NAME:	cnttz(uint word)
3095*4882a593Smuzhiyun  *
3096*4882a593Smuzhiyun  * FUNCTION:	determine the number of trailing zeros within a 32-bit
3097*4882a593Smuzhiyun  *		value.
3098*4882a593Smuzhiyun  *
3099*4882a593Smuzhiyun  * PARAMETERS:
3100*4882a593Smuzhiyun  *	value	-  32-bit value to be examined.
3101*4882a593Smuzhiyun  *
3102*4882a593Smuzhiyun  * RETURN VALUES:
3103*4882a593Smuzhiyun  *	count of trailing zeros
3104*4882a593Smuzhiyun  */
cnttz(u32 word)3105*4882a593Smuzhiyun static int cnttz(u32 word)
3106*4882a593Smuzhiyun {
3107*4882a593Smuzhiyun 	int n;
3108*4882a593Smuzhiyun 
3109*4882a593Smuzhiyun 	for (n = 0; n < 32; n++, word >>= 1) {
3110*4882a593Smuzhiyun 		if (word & 0x01)
3111*4882a593Smuzhiyun 			break;
3112*4882a593Smuzhiyun 	}
3113*4882a593Smuzhiyun 
3114*4882a593Smuzhiyun 	return (n);
3115*4882a593Smuzhiyun }
3116*4882a593Smuzhiyun 
3117*4882a593Smuzhiyun 
3118*4882a593Smuzhiyun /*
3119*4882a593Smuzhiyun  * NAME:	cntlz(u32 value)
3120*4882a593Smuzhiyun  *
3121*4882a593Smuzhiyun  * FUNCTION:	determine the number of leading zeros within a 32-bit
3122*4882a593Smuzhiyun  *		value.
3123*4882a593Smuzhiyun  *
3124*4882a593Smuzhiyun  * PARAMETERS:
3125*4882a593Smuzhiyun  *	value	-  32-bit value to be examined.
3126*4882a593Smuzhiyun  *
3127*4882a593Smuzhiyun  * RETURN VALUES:
3128*4882a593Smuzhiyun  *	count of leading zeros
3129*4882a593Smuzhiyun  */
cntlz(u32 value)3130*4882a593Smuzhiyun static int cntlz(u32 value)
3131*4882a593Smuzhiyun {
3132*4882a593Smuzhiyun 	int n;
3133*4882a593Smuzhiyun 
3134*4882a593Smuzhiyun 	for (n = 0; n < 32; n++, value <<= 1) {
3135*4882a593Smuzhiyun 		if (value & HIGHORDER)
3136*4882a593Smuzhiyun 			break;
3137*4882a593Smuzhiyun 	}
3138*4882a593Smuzhiyun 	return (n);
3139*4882a593Smuzhiyun }
3140*4882a593Smuzhiyun 
3141*4882a593Smuzhiyun 
3142*4882a593Smuzhiyun /*
3143*4882a593Smuzhiyun  * NAME:	blkstol2(s64 nb)
3144*4882a593Smuzhiyun  *
3145*4882a593Smuzhiyun  * FUNCTION:	convert a block count to its log2 value. if the block
3146*4882a593Smuzhiyun  *		count is not a l2 multiple, it is rounded up to the next
3147*4882a593Smuzhiyun  *		larger l2 multiple.
3148*4882a593Smuzhiyun  *
3149*4882a593Smuzhiyun  * PARAMETERS:
3150*4882a593Smuzhiyun  *	nb	-  number of blocks
3151*4882a593Smuzhiyun  *
3152*4882a593Smuzhiyun  * RETURN VALUES:
3153*4882a593Smuzhiyun  *	log2 number of blocks
3154*4882a593Smuzhiyun  */
blkstol2(s64 nb)3155*4882a593Smuzhiyun static int blkstol2(s64 nb)
3156*4882a593Smuzhiyun {
3157*4882a593Smuzhiyun 	int l2nb;
3158*4882a593Smuzhiyun 	s64 mask;		/* meant to be signed */
3159*4882a593Smuzhiyun 
3160*4882a593Smuzhiyun 	mask = (s64) 1 << (64 - 1);
3161*4882a593Smuzhiyun 
3162*4882a593Smuzhiyun 	/* count the leading bits.
3163*4882a593Smuzhiyun 	 */
3164*4882a593Smuzhiyun 	for (l2nb = 0; l2nb < 64; l2nb++, mask >>= 1) {
3165*4882a593Smuzhiyun 		/* leading bit found.
3166*4882a593Smuzhiyun 		 */
3167*4882a593Smuzhiyun 		if (nb & mask) {
3168*4882a593Smuzhiyun 			/* determine the l2 value.
3169*4882a593Smuzhiyun 			 */
3170*4882a593Smuzhiyun 			l2nb = (64 - 1) - l2nb;
3171*4882a593Smuzhiyun 
3172*4882a593Smuzhiyun 			/* check if we need to round up.
3173*4882a593Smuzhiyun 			 */
3174*4882a593Smuzhiyun 			if (~mask & nb)
3175*4882a593Smuzhiyun 				l2nb++;
3176*4882a593Smuzhiyun 
3177*4882a593Smuzhiyun 			return (l2nb);
3178*4882a593Smuzhiyun 		}
3179*4882a593Smuzhiyun 	}
3180*4882a593Smuzhiyun 	assert(0);
3181*4882a593Smuzhiyun 	return 0;		/* fix compiler warning */
3182*4882a593Smuzhiyun }
3183*4882a593Smuzhiyun 
3184*4882a593Smuzhiyun 
3185*4882a593Smuzhiyun /*
3186*4882a593Smuzhiyun  * NAME:	dbAllocBottomUp()
3187*4882a593Smuzhiyun  *
3188*4882a593Smuzhiyun  * FUNCTION:	alloc the specified block range from the working block
3189*4882a593Smuzhiyun  *		allocation map.
3190*4882a593Smuzhiyun  *
3191*4882a593Smuzhiyun  *		the blocks will be alloc from the working map one dmap
3192*4882a593Smuzhiyun  *		at a time.
3193*4882a593Smuzhiyun  *
3194*4882a593Smuzhiyun  * PARAMETERS:
3195*4882a593Smuzhiyun  *	ip	-  pointer to in-core inode;
3196*4882a593Smuzhiyun  *	blkno	-  starting block number to be freed.
3197*4882a593Smuzhiyun  *	nblocks	-  number of blocks to be freed.
3198*4882a593Smuzhiyun  *
3199*4882a593Smuzhiyun  * RETURN VALUES:
3200*4882a593Smuzhiyun  *	0	- success
3201*4882a593Smuzhiyun  *	-EIO	- i/o error
3202*4882a593Smuzhiyun  */
dbAllocBottomUp(struct inode * ip,s64 blkno,s64 nblocks)3203*4882a593Smuzhiyun int dbAllocBottomUp(struct inode *ip, s64 blkno, s64 nblocks)
3204*4882a593Smuzhiyun {
3205*4882a593Smuzhiyun 	struct metapage *mp;
3206*4882a593Smuzhiyun 	struct dmap *dp;
3207*4882a593Smuzhiyun 	int nb, rc;
3208*4882a593Smuzhiyun 	s64 lblkno, rem;
3209*4882a593Smuzhiyun 	struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
3210*4882a593Smuzhiyun 	struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
3211*4882a593Smuzhiyun 
3212*4882a593Smuzhiyun 	IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
3213*4882a593Smuzhiyun 
3214*4882a593Smuzhiyun 	/* block to be allocated better be within the mapsize. */
3215*4882a593Smuzhiyun 	ASSERT(nblocks <= bmp->db_mapsize - blkno);
3216*4882a593Smuzhiyun 
3217*4882a593Smuzhiyun 	/*
3218*4882a593Smuzhiyun 	 * allocate the blocks a dmap at a time.
3219*4882a593Smuzhiyun 	 */
3220*4882a593Smuzhiyun 	mp = NULL;
3221*4882a593Smuzhiyun 	for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
3222*4882a593Smuzhiyun 		/* release previous dmap if any */
3223*4882a593Smuzhiyun 		if (mp) {
3224*4882a593Smuzhiyun 			write_metapage(mp);
3225*4882a593Smuzhiyun 		}
3226*4882a593Smuzhiyun 
3227*4882a593Smuzhiyun 		/* get the buffer for the current dmap. */
3228*4882a593Smuzhiyun 		lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
3229*4882a593Smuzhiyun 		mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
3230*4882a593Smuzhiyun 		if (mp == NULL) {
3231*4882a593Smuzhiyun 			IREAD_UNLOCK(ipbmap);
3232*4882a593Smuzhiyun 			return -EIO;
3233*4882a593Smuzhiyun 		}
3234*4882a593Smuzhiyun 		dp = (struct dmap *) mp->data;
3235*4882a593Smuzhiyun 
3236*4882a593Smuzhiyun 		/* determine the number of blocks to be allocated from
3237*4882a593Smuzhiyun 		 * this dmap.
3238*4882a593Smuzhiyun 		 */
3239*4882a593Smuzhiyun 		nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
3240*4882a593Smuzhiyun 
3241*4882a593Smuzhiyun 		/* allocate the blocks. */
3242*4882a593Smuzhiyun 		if ((rc = dbAllocDmapBU(bmp, dp, blkno, nb))) {
3243*4882a593Smuzhiyun 			release_metapage(mp);
3244*4882a593Smuzhiyun 			IREAD_UNLOCK(ipbmap);
3245*4882a593Smuzhiyun 			return (rc);
3246*4882a593Smuzhiyun 		}
3247*4882a593Smuzhiyun 	}
3248*4882a593Smuzhiyun 
3249*4882a593Smuzhiyun 	/* write the last buffer. */
3250*4882a593Smuzhiyun 	write_metapage(mp);
3251*4882a593Smuzhiyun 
3252*4882a593Smuzhiyun 	IREAD_UNLOCK(ipbmap);
3253*4882a593Smuzhiyun 
3254*4882a593Smuzhiyun 	return (0);
3255*4882a593Smuzhiyun }
3256*4882a593Smuzhiyun 
3257*4882a593Smuzhiyun 
dbAllocDmapBU(struct bmap * bmp,struct dmap * dp,s64 blkno,int nblocks)3258*4882a593Smuzhiyun static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
3259*4882a593Smuzhiyun 			 int nblocks)
3260*4882a593Smuzhiyun {
3261*4882a593Smuzhiyun 	int rc;
3262*4882a593Smuzhiyun 	int dbitno, word, rembits, nb, nwords, wbitno, agno;
3263*4882a593Smuzhiyun 	s8 oldroot;
3264*4882a593Smuzhiyun 	struct dmaptree *tp = (struct dmaptree *) & dp->tree;
3265*4882a593Smuzhiyun 
3266*4882a593Smuzhiyun 	/* save the current value of the root (i.e. maximum free string)
3267*4882a593Smuzhiyun 	 * of the dmap tree.
3268*4882a593Smuzhiyun 	 */
3269*4882a593Smuzhiyun 	oldroot = tp->stree[ROOT];
3270*4882a593Smuzhiyun 
3271*4882a593Smuzhiyun 	/* determine the bit number and word within the dmap of the
3272*4882a593Smuzhiyun 	 * starting block.
3273*4882a593Smuzhiyun 	 */
3274*4882a593Smuzhiyun 	dbitno = blkno & (BPERDMAP - 1);
3275*4882a593Smuzhiyun 	word = dbitno >> L2DBWORD;
3276*4882a593Smuzhiyun 
3277*4882a593Smuzhiyun 	/* block range better be within the dmap */
3278*4882a593Smuzhiyun 	assert(dbitno + nblocks <= BPERDMAP);
3279*4882a593Smuzhiyun 
3280*4882a593Smuzhiyun 	/* allocate the bits of the dmap's words corresponding to the block
3281*4882a593Smuzhiyun 	 * range. not all bits of the first and last words may be contained
3282*4882a593Smuzhiyun 	 * within the block range.  if this is the case, we'll work against
3283*4882a593Smuzhiyun 	 * those words (i.e. partial first and/or last) on an individual basis
3284*4882a593Smuzhiyun 	 * (a single pass), allocating the bits of interest by hand and
3285*4882a593Smuzhiyun 	 * updating the leaf corresponding to the dmap word. a single pass
3286*4882a593Smuzhiyun 	 * will be used for all dmap words fully contained within the
3287*4882a593Smuzhiyun 	 * specified range.  within this pass, the bits of all fully contained
3288*4882a593Smuzhiyun 	 * dmap words will be marked as free in a single shot and the leaves
3289*4882a593Smuzhiyun 	 * will be updated. a single leaf may describe the free space of
3290*4882a593Smuzhiyun 	 * multiple dmap words, so we may update only a subset of the actual
3291*4882a593Smuzhiyun 	 * leaves corresponding to the dmap words of the block range.
3292*4882a593Smuzhiyun 	 */
3293*4882a593Smuzhiyun 	for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
3294*4882a593Smuzhiyun 		/* determine the bit number within the word and
3295*4882a593Smuzhiyun 		 * the number of bits within the word.
3296*4882a593Smuzhiyun 		 */
3297*4882a593Smuzhiyun 		wbitno = dbitno & (DBWORD - 1);
3298*4882a593Smuzhiyun 		nb = min(rembits, DBWORD - wbitno);
3299*4882a593Smuzhiyun 
3300*4882a593Smuzhiyun 		/* check if only part of a word is to be allocated.
3301*4882a593Smuzhiyun 		 */
3302*4882a593Smuzhiyun 		if (nb < DBWORD) {
3303*4882a593Smuzhiyun 			/* allocate (set to 1) the appropriate bits within
3304*4882a593Smuzhiyun 			 * this dmap word.
3305*4882a593Smuzhiyun 			 */
3306*4882a593Smuzhiyun 			dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
3307*4882a593Smuzhiyun 						      >> wbitno);
3308*4882a593Smuzhiyun 
3309*4882a593Smuzhiyun 			word++;
3310*4882a593Smuzhiyun 		} else {
3311*4882a593Smuzhiyun 			/* one or more dmap words are fully contained
3312*4882a593Smuzhiyun 			 * within the block range.  determine how many
3313*4882a593Smuzhiyun 			 * words and allocate (set to 1) the bits of these
3314*4882a593Smuzhiyun 			 * words.
3315*4882a593Smuzhiyun 			 */
3316*4882a593Smuzhiyun 			nwords = rembits >> L2DBWORD;
3317*4882a593Smuzhiyun 			memset(&dp->wmap[word], (int) ONES, nwords * 4);
3318*4882a593Smuzhiyun 
3319*4882a593Smuzhiyun 			/* determine how many bits */
3320*4882a593Smuzhiyun 			nb = nwords << L2DBWORD;
3321*4882a593Smuzhiyun 			word += nwords;
3322*4882a593Smuzhiyun 		}
3323*4882a593Smuzhiyun 	}
3324*4882a593Smuzhiyun 
3325*4882a593Smuzhiyun 	/* update the free count for this dmap */
3326*4882a593Smuzhiyun 	le32_add_cpu(&dp->nfree, -nblocks);
3327*4882a593Smuzhiyun 
3328*4882a593Smuzhiyun 	/* reconstruct summary tree */
3329*4882a593Smuzhiyun 	dbInitDmapTree(dp);
3330*4882a593Smuzhiyun 
3331*4882a593Smuzhiyun 	BMAP_LOCK(bmp);
3332*4882a593Smuzhiyun 
3333*4882a593Smuzhiyun 	/* if this allocation group is completely free,
3334*4882a593Smuzhiyun 	 * update the highest active allocation group number
3335*4882a593Smuzhiyun 	 * if this allocation group is the new max.
3336*4882a593Smuzhiyun 	 */
3337*4882a593Smuzhiyun 	agno = blkno >> bmp->db_agl2size;
3338*4882a593Smuzhiyun 	if (agno > bmp->db_maxag)
3339*4882a593Smuzhiyun 		bmp->db_maxag = agno;
3340*4882a593Smuzhiyun 
3341*4882a593Smuzhiyun 	/* update the free count for the allocation group and map */
3342*4882a593Smuzhiyun 	bmp->db_agfree[agno] -= nblocks;
3343*4882a593Smuzhiyun 	bmp->db_nfree -= nblocks;
3344*4882a593Smuzhiyun 
3345*4882a593Smuzhiyun 	BMAP_UNLOCK(bmp);
3346*4882a593Smuzhiyun 
3347*4882a593Smuzhiyun 	/* if the root has not changed, done. */
3348*4882a593Smuzhiyun 	if (tp->stree[ROOT] == oldroot)
3349*4882a593Smuzhiyun 		return (0);
3350*4882a593Smuzhiyun 
3351*4882a593Smuzhiyun 	/* root changed. bubble the change up to the dmap control pages.
3352*4882a593Smuzhiyun 	 * if the adjustment of the upper level control pages fails,
3353*4882a593Smuzhiyun 	 * backout the bit allocation (thus making everything consistent).
3354*4882a593Smuzhiyun 	 */
3355*4882a593Smuzhiyun 	if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0)))
3356*4882a593Smuzhiyun 		dbFreeBits(bmp, dp, blkno, nblocks);
3357*4882a593Smuzhiyun 
3358*4882a593Smuzhiyun 	return (rc);
3359*4882a593Smuzhiyun }
3360*4882a593Smuzhiyun 
3361*4882a593Smuzhiyun 
3362*4882a593Smuzhiyun /*
3363*4882a593Smuzhiyun  * NAME:	dbExtendFS()
3364*4882a593Smuzhiyun  *
3365*4882a593Smuzhiyun  * FUNCTION:	extend bmap from blkno for nblocks;
3366*4882a593Smuzhiyun  *		dbExtendFS() updates bmap ready for dbAllocBottomUp();
3367*4882a593Smuzhiyun  *
3368*4882a593Smuzhiyun  * L2
3369*4882a593Smuzhiyun  *  |
3370*4882a593Smuzhiyun  *   L1---------------------------------L1
3371*4882a593Smuzhiyun  *    |					 |
3372*4882a593Smuzhiyun  *     L0---------L0---------L0		  L0---------L0---------L0
3373*4882a593Smuzhiyun  *      |	   |	      |		   |	      |		 |
3374*4882a593Smuzhiyun  *	 d0,...,dn  d0,...,dn  d0,...,dn    d0,...,dn  d0,...,dn  d0,.,dm;
3375*4882a593Smuzhiyun  * L2L1L0d0,...,dnL0d0,...,dnL0d0,...,dnL1L0d0,...,dnL0d0,...,dnL0d0,..dm
3376*4882a593Smuzhiyun  *
3377*4882a593Smuzhiyun  * <---old---><----------------------------extend----------------------->
3378*4882a593Smuzhiyun  */
dbExtendFS(struct inode * ipbmap,s64 blkno,s64 nblocks)3379*4882a593Smuzhiyun int dbExtendFS(struct inode *ipbmap, s64 blkno,	s64 nblocks)
3380*4882a593Smuzhiyun {
3381*4882a593Smuzhiyun 	struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb);
3382*4882a593Smuzhiyun 	int nbperpage = sbi->nbperpage;
3383*4882a593Smuzhiyun 	int i, i0 = true, j, j0 = true, k, n;
3384*4882a593Smuzhiyun 	s64 newsize;
3385*4882a593Smuzhiyun 	s64 p;
3386*4882a593Smuzhiyun 	struct metapage *mp, *l2mp, *l1mp = NULL, *l0mp = NULL;
3387*4882a593Smuzhiyun 	struct dmapctl *l2dcp, *l1dcp, *l0dcp;
3388*4882a593Smuzhiyun 	struct dmap *dp;
3389*4882a593Smuzhiyun 	s8 *l0leaf, *l1leaf, *l2leaf;
3390*4882a593Smuzhiyun 	struct bmap *bmp = sbi->bmap;
3391*4882a593Smuzhiyun 	int agno, l2agsize, oldl2agsize;
3392*4882a593Smuzhiyun 	s64 ag_rem;
3393*4882a593Smuzhiyun 
3394*4882a593Smuzhiyun 	newsize = blkno + nblocks;
3395*4882a593Smuzhiyun 
3396*4882a593Smuzhiyun 	jfs_info("dbExtendFS: blkno:%Ld nblocks:%Ld newsize:%Ld",
3397*4882a593Smuzhiyun 		 (long long) blkno, (long long) nblocks, (long long) newsize);
3398*4882a593Smuzhiyun 
3399*4882a593Smuzhiyun 	/*
3400*4882a593Smuzhiyun 	 *	initialize bmap control page.
3401*4882a593Smuzhiyun 	 *
3402*4882a593Smuzhiyun 	 * all the data in bmap control page should exclude
3403*4882a593Smuzhiyun 	 * the mkfs hidden dmap page.
3404*4882a593Smuzhiyun 	 */
3405*4882a593Smuzhiyun 
3406*4882a593Smuzhiyun 	/* update mapsize */
3407*4882a593Smuzhiyun 	bmp->db_mapsize = newsize;
3408*4882a593Smuzhiyun 	bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize);
3409*4882a593Smuzhiyun 
3410*4882a593Smuzhiyun 	/* compute new AG size */
3411*4882a593Smuzhiyun 	l2agsize = dbGetL2AGSize(newsize);
3412*4882a593Smuzhiyun 	oldl2agsize = bmp->db_agl2size;
3413*4882a593Smuzhiyun 
3414*4882a593Smuzhiyun 	bmp->db_agl2size = l2agsize;
3415*4882a593Smuzhiyun 	bmp->db_agsize = 1 << l2agsize;
3416*4882a593Smuzhiyun 
3417*4882a593Smuzhiyun 	/* compute new number of AG */
3418*4882a593Smuzhiyun 	agno = bmp->db_numag;
3419*4882a593Smuzhiyun 	bmp->db_numag = newsize >> l2agsize;
3420*4882a593Smuzhiyun 	bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0;
3421*4882a593Smuzhiyun 
3422*4882a593Smuzhiyun 	/*
3423*4882a593Smuzhiyun 	 *	reconfigure db_agfree[]
3424*4882a593Smuzhiyun 	 * from old AG configuration to new AG configuration;
3425*4882a593Smuzhiyun 	 *
3426*4882a593Smuzhiyun 	 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
3427*4882a593Smuzhiyun 	 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
3428*4882a593Smuzhiyun 	 * note: new AG size = old AG size * (2**x).
3429*4882a593Smuzhiyun 	 */
3430*4882a593Smuzhiyun 	if (l2agsize == oldl2agsize)
3431*4882a593Smuzhiyun 		goto extend;
3432*4882a593Smuzhiyun 	k = 1 << (l2agsize - oldl2agsize);
3433*4882a593Smuzhiyun 	ag_rem = bmp->db_agfree[0];	/* save agfree[0] */
3434*4882a593Smuzhiyun 	for (i = 0, n = 0; i < agno; n++) {
3435*4882a593Smuzhiyun 		bmp->db_agfree[n] = 0;	/* init collection point */
3436*4882a593Smuzhiyun 
3437*4882a593Smuzhiyun 		/* coalesce contiguous k AGs; */
3438*4882a593Smuzhiyun 		for (j = 0; j < k && i < agno; j++, i++) {
3439*4882a593Smuzhiyun 			/* merge AGi to AGn */
3440*4882a593Smuzhiyun 			bmp->db_agfree[n] += bmp->db_agfree[i];
3441*4882a593Smuzhiyun 		}
3442*4882a593Smuzhiyun 	}
3443*4882a593Smuzhiyun 	bmp->db_agfree[0] += ag_rem;	/* restore agfree[0] */
3444*4882a593Smuzhiyun 
3445*4882a593Smuzhiyun 	for (; n < MAXAG; n++)
3446*4882a593Smuzhiyun 		bmp->db_agfree[n] = 0;
3447*4882a593Smuzhiyun 
3448*4882a593Smuzhiyun 	/*
3449*4882a593Smuzhiyun 	 * update highest active ag number
3450*4882a593Smuzhiyun 	 */
3451*4882a593Smuzhiyun 
3452*4882a593Smuzhiyun 	bmp->db_maxag = bmp->db_maxag / k;
3453*4882a593Smuzhiyun 
3454*4882a593Smuzhiyun 	/*
3455*4882a593Smuzhiyun 	 *	extend bmap
3456*4882a593Smuzhiyun 	 *
3457*4882a593Smuzhiyun 	 * update bit maps and corresponding level control pages;
3458*4882a593Smuzhiyun 	 * global control page db_nfree, db_agfree[agno], db_maxfreebud;
3459*4882a593Smuzhiyun 	 */
3460*4882a593Smuzhiyun       extend:
3461*4882a593Smuzhiyun 	/* get L2 page */
3462*4882a593Smuzhiyun 	p = BMAPBLKNO + nbperpage;	/* L2 page */
3463*4882a593Smuzhiyun 	l2mp = read_metapage(ipbmap, p, PSIZE, 0);
3464*4882a593Smuzhiyun 	if (!l2mp) {
3465*4882a593Smuzhiyun 		jfs_error(ipbmap->i_sb, "L2 page could not be read\n");
3466*4882a593Smuzhiyun 		return -EIO;
3467*4882a593Smuzhiyun 	}
3468*4882a593Smuzhiyun 	l2dcp = (struct dmapctl *) l2mp->data;
3469*4882a593Smuzhiyun 
3470*4882a593Smuzhiyun 	/* compute start L1 */
3471*4882a593Smuzhiyun 	k = blkno >> L2MAXL1SIZE;
3472*4882a593Smuzhiyun 	l2leaf = l2dcp->stree + CTLLEAFIND + k;
3473*4882a593Smuzhiyun 	p = BLKTOL1(blkno, sbi->l2nbperpage);	/* L1 page */
3474*4882a593Smuzhiyun 
3475*4882a593Smuzhiyun 	/*
3476*4882a593Smuzhiyun 	 * extend each L1 in L2
3477*4882a593Smuzhiyun 	 */
3478*4882a593Smuzhiyun 	for (; k < LPERCTL; k++, p += nbperpage) {
3479*4882a593Smuzhiyun 		/* get L1 page */
3480*4882a593Smuzhiyun 		if (j0) {
3481*4882a593Smuzhiyun 			/* read in L1 page: (blkno & (MAXL1SIZE - 1)) */
3482*4882a593Smuzhiyun 			l1mp = read_metapage(ipbmap, p, PSIZE, 0);
3483*4882a593Smuzhiyun 			if (l1mp == NULL)
3484*4882a593Smuzhiyun 				goto errout;
3485*4882a593Smuzhiyun 			l1dcp = (struct dmapctl *) l1mp->data;
3486*4882a593Smuzhiyun 
3487*4882a593Smuzhiyun 			/* compute start L0 */
3488*4882a593Smuzhiyun 			j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE;
3489*4882a593Smuzhiyun 			l1leaf = l1dcp->stree + CTLLEAFIND + j;
3490*4882a593Smuzhiyun 			p = BLKTOL0(blkno, sbi->l2nbperpage);
3491*4882a593Smuzhiyun 			j0 = false;
3492*4882a593Smuzhiyun 		} else {
3493*4882a593Smuzhiyun 			/* assign/init L1 page */
3494*4882a593Smuzhiyun 			l1mp = get_metapage(ipbmap, p, PSIZE, 0);
3495*4882a593Smuzhiyun 			if (l1mp == NULL)
3496*4882a593Smuzhiyun 				goto errout;
3497*4882a593Smuzhiyun 
3498*4882a593Smuzhiyun 			l1dcp = (struct dmapctl *) l1mp->data;
3499*4882a593Smuzhiyun 
3500*4882a593Smuzhiyun 			/* compute start L0 */
3501*4882a593Smuzhiyun 			j = 0;
3502*4882a593Smuzhiyun 			l1leaf = l1dcp->stree + CTLLEAFIND;
3503*4882a593Smuzhiyun 			p += nbperpage;	/* 1st L0 of L1.k */
3504*4882a593Smuzhiyun 		}
3505*4882a593Smuzhiyun 
3506*4882a593Smuzhiyun 		/*
3507*4882a593Smuzhiyun 		 * extend each L0 in L1
3508*4882a593Smuzhiyun 		 */
3509*4882a593Smuzhiyun 		for (; j < LPERCTL; j++) {
3510*4882a593Smuzhiyun 			/* get L0 page */
3511*4882a593Smuzhiyun 			if (i0) {
3512*4882a593Smuzhiyun 				/* read in L0 page: (blkno & (MAXL0SIZE - 1)) */
3513*4882a593Smuzhiyun 
3514*4882a593Smuzhiyun 				l0mp = read_metapage(ipbmap, p, PSIZE, 0);
3515*4882a593Smuzhiyun 				if (l0mp == NULL)
3516*4882a593Smuzhiyun 					goto errout;
3517*4882a593Smuzhiyun 				l0dcp = (struct dmapctl *) l0mp->data;
3518*4882a593Smuzhiyun 
3519*4882a593Smuzhiyun 				/* compute start dmap */
3520*4882a593Smuzhiyun 				i = (blkno & (MAXL0SIZE - 1)) >>
3521*4882a593Smuzhiyun 				    L2BPERDMAP;
3522*4882a593Smuzhiyun 				l0leaf = l0dcp->stree + CTLLEAFIND + i;
3523*4882a593Smuzhiyun 				p = BLKTODMAP(blkno,
3524*4882a593Smuzhiyun 					      sbi->l2nbperpage);
3525*4882a593Smuzhiyun 				i0 = false;
3526*4882a593Smuzhiyun 			} else {
3527*4882a593Smuzhiyun 				/* assign/init L0 page */
3528*4882a593Smuzhiyun 				l0mp = get_metapage(ipbmap, p, PSIZE, 0);
3529*4882a593Smuzhiyun 				if (l0mp == NULL)
3530*4882a593Smuzhiyun 					goto errout;
3531*4882a593Smuzhiyun 
3532*4882a593Smuzhiyun 				l0dcp = (struct dmapctl *) l0mp->data;
3533*4882a593Smuzhiyun 
3534*4882a593Smuzhiyun 				/* compute start dmap */
3535*4882a593Smuzhiyun 				i = 0;
3536*4882a593Smuzhiyun 				l0leaf = l0dcp->stree + CTLLEAFIND;
3537*4882a593Smuzhiyun 				p += nbperpage;	/* 1st dmap of L0.j */
3538*4882a593Smuzhiyun 			}
3539*4882a593Smuzhiyun 
3540*4882a593Smuzhiyun 			/*
3541*4882a593Smuzhiyun 			 * extend each dmap in L0
3542*4882a593Smuzhiyun 			 */
3543*4882a593Smuzhiyun 			for (; i < LPERCTL; i++) {
3544*4882a593Smuzhiyun 				/*
3545*4882a593Smuzhiyun 				 * reconstruct the dmap page, and
3546*4882a593Smuzhiyun 				 * initialize corresponding parent L0 leaf
3547*4882a593Smuzhiyun 				 */
3548*4882a593Smuzhiyun 				if ((n = blkno & (BPERDMAP - 1))) {
3549*4882a593Smuzhiyun 					/* read in dmap page: */
3550*4882a593Smuzhiyun 					mp = read_metapage(ipbmap, p,
3551*4882a593Smuzhiyun 							   PSIZE, 0);
3552*4882a593Smuzhiyun 					if (mp == NULL)
3553*4882a593Smuzhiyun 						goto errout;
3554*4882a593Smuzhiyun 					n = min(nblocks, (s64)BPERDMAP - n);
3555*4882a593Smuzhiyun 				} else {
3556*4882a593Smuzhiyun 					/* assign/init dmap page */
3557*4882a593Smuzhiyun 					mp = read_metapage(ipbmap, p,
3558*4882a593Smuzhiyun 							   PSIZE, 0);
3559*4882a593Smuzhiyun 					if (mp == NULL)
3560*4882a593Smuzhiyun 						goto errout;
3561*4882a593Smuzhiyun 
3562*4882a593Smuzhiyun 					n = min_t(s64, nblocks, BPERDMAP);
3563*4882a593Smuzhiyun 				}
3564*4882a593Smuzhiyun 
3565*4882a593Smuzhiyun 				dp = (struct dmap *) mp->data;
3566*4882a593Smuzhiyun 				*l0leaf = dbInitDmap(dp, blkno, n);
3567*4882a593Smuzhiyun 
3568*4882a593Smuzhiyun 				bmp->db_nfree += n;
3569*4882a593Smuzhiyun 				agno = le64_to_cpu(dp->start) >> l2agsize;
3570*4882a593Smuzhiyun 				bmp->db_agfree[agno] += n;
3571*4882a593Smuzhiyun 
3572*4882a593Smuzhiyun 				write_metapage(mp);
3573*4882a593Smuzhiyun 
3574*4882a593Smuzhiyun 				l0leaf++;
3575*4882a593Smuzhiyun 				p += nbperpage;
3576*4882a593Smuzhiyun 
3577*4882a593Smuzhiyun 				blkno += n;
3578*4882a593Smuzhiyun 				nblocks -= n;
3579*4882a593Smuzhiyun 				if (nblocks == 0)
3580*4882a593Smuzhiyun 					break;
3581*4882a593Smuzhiyun 			}	/* for each dmap in a L0 */
3582*4882a593Smuzhiyun 
3583*4882a593Smuzhiyun 			/*
3584*4882a593Smuzhiyun 			 * build current L0 page from its leaves, and
3585*4882a593Smuzhiyun 			 * initialize corresponding parent L1 leaf
3586*4882a593Smuzhiyun 			 */
3587*4882a593Smuzhiyun 			*l1leaf = dbInitDmapCtl(l0dcp, 0, ++i);
3588*4882a593Smuzhiyun 			write_metapage(l0mp);
3589*4882a593Smuzhiyun 			l0mp = NULL;
3590*4882a593Smuzhiyun 
3591*4882a593Smuzhiyun 			if (nblocks)
3592*4882a593Smuzhiyun 				l1leaf++;	/* continue for next L0 */
3593*4882a593Smuzhiyun 			else {
3594*4882a593Smuzhiyun 				/* more than 1 L0 ? */
3595*4882a593Smuzhiyun 				if (j > 0)
3596*4882a593Smuzhiyun 					break;	/* build L1 page */
3597*4882a593Smuzhiyun 				else {
3598*4882a593Smuzhiyun 					/* summarize in global bmap page */
3599*4882a593Smuzhiyun 					bmp->db_maxfreebud = *l1leaf;
3600*4882a593Smuzhiyun 					release_metapage(l1mp);
3601*4882a593Smuzhiyun 					release_metapage(l2mp);
3602*4882a593Smuzhiyun 					goto finalize;
3603*4882a593Smuzhiyun 				}
3604*4882a593Smuzhiyun 			}
3605*4882a593Smuzhiyun 		}		/* for each L0 in a L1 */
3606*4882a593Smuzhiyun 
3607*4882a593Smuzhiyun 		/*
3608*4882a593Smuzhiyun 		 * build current L1 page from its leaves, and
3609*4882a593Smuzhiyun 		 * initialize corresponding parent L2 leaf
3610*4882a593Smuzhiyun 		 */
3611*4882a593Smuzhiyun 		*l2leaf = dbInitDmapCtl(l1dcp, 1, ++j);
3612*4882a593Smuzhiyun 		write_metapage(l1mp);
3613*4882a593Smuzhiyun 		l1mp = NULL;
3614*4882a593Smuzhiyun 
3615*4882a593Smuzhiyun 		if (nblocks)
3616*4882a593Smuzhiyun 			l2leaf++;	/* continue for next L1 */
3617*4882a593Smuzhiyun 		else {
3618*4882a593Smuzhiyun 			/* more than 1 L1 ? */
3619*4882a593Smuzhiyun 			if (k > 0)
3620*4882a593Smuzhiyun 				break;	/* build L2 page */
3621*4882a593Smuzhiyun 			else {
3622*4882a593Smuzhiyun 				/* summarize in global bmap page */
3623*4882a593Smuzhiyun 				bmp->db_maxfreebud = *l2leaf;
3624*4882a593Smuzhiyun 				release_metapage(l2mp);
3625*4882a593Smuzhiyun 				goto finalize;
3626*4882a593Smuzhiyun 			}
3627*4882a593Smuzhiyun 		}
3628*4882a593Smuzhiyun 	}			/* for each L1 in a L2 */
3629*4882a593Smuzhiyun 
3630*4882a593Smuzhiyun 	jfs_error(ipbmap->i_sb, "function has not returned as expected\n");
3631*4882a593Smuzhiyun errout:
3632*4882a593Smuzhiyun 	if (l0mp)
3633*4882a593Smuzhiyun 		release_metapage(l0mp);
3634*4882a593Smuzhiyun 	if (l1mp)
3635*4882a593Smuzhiyun 		release_metapage(l1mp);
3636*4882a593Smuzhiyun 	release_metapage(l2mp);
3637*4882a593Smuzhiyun 	return -EIO;
3638*4882a593Smuzhiyun 
3639*4882a593Smuzhiyun 	/*
3640*4882a593Smuzhiyun 	 *	finalize bmap control page
3641*4882a593Smuzhiyun 	 */
3642*4882a593Smuzhiyun finalize:
3643*4882a593Smuzhiyun 
3644*4882a593Smuzhiyun 	return 0;
3645*4882a593Smuzhiyun }
3646*4882a593Smuzhiyun 
3647*4882a593Smuzhiyun 
3648*4882a593Smuzhiyun /*
3649*4882a593Smuzhiyun  *	dbFinalizeBmap()
3650*4882a593Smuzhiyun  */
dbFinalizeBmap(struct inode * ipbmap)3651*4882a593Smuzhiyun void dbFinalizeBmap(struct inode *ipbmap)
3652*4882a593Smuzhiyun {
3653*4882a593Smuzhiyun 	struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
3654*4882a593Smuzhiyun 	int actags, inactags, l2nl;
3655*4882a593Smuzhiyun 	s64 ag_rem, actfree, inactfree, avgfree;
3656*4882a593Smuzhiyun 	int i, n;
3657*4882a593Smuzhiyun 
3658*4882a593Smuzhiyun 	/*
3659*4882a593Smuzhiyun 	 *	finalize bmap control page
3660*4882a593Smuzhiyun 	 */
3661*4882a593Smuzhiyun //finalize:
3662*4882a593Smuzhiyun 	/*
3663*4882a593Smuzhiyun 	 * compute db_agpref: preferred ag to allocate from
3664*4882a593Smuzhiyun 	 * (the leftmost ag with average free space in it);
3665*4882a593Smuzhiyun 	 */
3666*4882a593Smuzhiyun //agpref:
3667*4882a593Smuzhiyun 	/* get the number of active ags and inacitve ags */
3668*4882a593Smuzhiyun 	actags = bmp->db_maxag + 1;
3669*4882a593Smuzhiyun 	inactags = bmp->db_numag - actags;
3670*4882a593Smuzhiyun 	ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1);	/* ??? */
3671*4882a593Smuzhiyun 
3672*4882a593Smuzhiyun 	/* determine how many blocks are in the inactive allocation
3673*4882a593Smuzhiyun 	 * groups. in doing this, we must account for the fact that
3674*4882a593Smuzhiyun 	 * the rightmost group might be a partial group (i.e. file
3675*4882a593Smuzhiyun 	 * system size is not a multiple of the group size).
3676*4882a593Smuzhiyun 	 */
3677*4882a593Smuzhiyun 	inactfree = (inactags && ag_rem) ?
3678*4882a593Smuzhiyun 	    ((inactags - 1) << bmp->db_agl2size) + ag_rem
3679*4882a593Smuzhiyun 	    : inactags << bmp->db_agl2size;
3680*4882a593Smuzhiyun 
3681*4882a593Smuzhiyun 	/* determine how many free blocks are in the active
3682*4882a593Smuzhiyun 	 * allocation groups plus the average number of free blocks
3683*4882a593Smuzhiyun 	 * within the active ags.
3684*4882a593Smuzhiyun 	 */
3685*4882a593Smuzhiyun 	actfree = bmp->db_nfree - inactfree;
3686*4882a593Smuzhiyun 	avgfree = (u32) actfree / (u32) actags;
3687*4882a593Smuzhiyun 
3688*4882a593Smuzhiyun 	/* if the preferred allocation group has not average free space.
3689*4882a593Smuzhiyun 	 * re-establish the preferred group as the leftmost
3690*4882a593Smuzhiyun 	 * group with average free space.
3691*4882a593Smuzhiyun 	 */
3692*4882a593Smuzhiyun 	if (bmp->db_agfree[bmp->db_agpref] < avgfree) {
3693*4882a593Smuzhiyun 		for (bmp->db_agpref = 0; bmp->db_agpref < actags;
3694*4882a593Smuzhiyun 		     bmp->db_agpref++) {
3695*4882a593Smuzhiyun 			if (bmp->db_agfree[bmp->db_agpref] >= avgfree)
3696*4882a593Smuzhiyun 				break;
3697*4882a593Smuzhiyun 		}
3698*4882a593Smuzhiyun 		if (bmp->db_agpref >= bmp->db_numag) {
3699*4882a593Smuzhiyun 			jfs_error(ipbmap->i_sb,
3700*4882a593Smuzhiyun 				  "cannot find ag with average freespace\n");
3701*4882a593Smuzhiyun 		}
3702*4882a593Smuzhiyun 	}
3703*4882a593Smuzhiyun 
3704*4882a593Smuzhiyun 	/*
3705*4882a593Smuzhiyun 	 * compute db_aglevel, db_agheight, db_width, db_agstart:
3706*4882a593Smuzhiyun 	 * an ag is covered in aglevel dmapctl summary tree,
3707*4882a593Smuzhiyun 	 * at agheight level height (from leaf) with agwidth number of nodes
3708*4882a593Smuzhiyun 	 * each, which starts at agstart index node of the smmary tree node
3709*4882a593Smuzhiyun 	 * array;
3710*4882a593Smuzhiyun 	 */
3711*4882a593Smuzhiyun 	bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize);
3712*4882a593Smuzhiyun 	l2nl =
3713*4882a593Smuzhiyun 	    bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL);
3714*4882a593Smuzhiyun 	bmp->db_agheight = l2nl >> 1;
3715*4882a593Smuzhiyun 	bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1));
3716*4882a593Smuzhiyun 	for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0;
3717*4882a593Smuzhiyun 	     i--) {
3718*4882a593Smuzhiyun 		bmp->db_agstart += n;
3719*4882a593Smuzhiyun 		n <<= 2;
3720*4882a593Smuzhiyun 	}
3721*4882a593Smuzhiyun 
3722*4882a593Smuzhiyun }
3723*4882a593Smuzhiyun 
3724*4882a593Smuzhiyun 
3725*4882a593Smuzhiyun /*
3726*4882a593Smuzhiyun  * NAME:	dbInitDmap()/ujfs_idmap_page()
3727*4882a593Smuzhiyun  *
3728*4882a593Smuzhiyun  * FUNCTION:	initialize working/persistent bitmap of the dmap page
3729*4882a593Smuzhiyun  *		for the specified number of blocks:
3730*4882a593Smuzhiyun  *
3731*4882a593Smuzhiyun  *		at entry, the bitmaps had been initialized as free (ZEROS);
3732*4882a593Smuzhiyun  *		The number of blocks will only account for the actually
3733*4882a593Smuzhiyun  *		existing blocks. Blocks which don't actually exist in
3734*4882a593Smuzhiyun  *		the aggregate will be marked as allocated (ONES);
3735*4882a593Smuzhiyun  *
3736*4882a593Smuzhiyun  * PARAMETERS:
3737*4882a593Smuzhiyun  *	dp	- pointer to page of map
3738*4882a593Smuzhiyun  *	nblocks	- number of blocks this page
3739*4882a593Smuzhiyun  *
3740*4882a593Smuzhiyun  * RETURNS: NONE
3741*4882a593Smuzhiyun  */
dbInitDmap(struct dmap * dp,s64 Blkno,int nblocks)3742*4882a593Smuzhiyun static int dbInitDmap(struct dmap * dp, s64 Blkno, int nblocks)
3743*4882a593Smuzhiyun {
3744*4882a593Smuzhiyun 	int blkno, w, b, r, nw, nb, i;
3745*4882a593Smuzhiyun 
3746*4882a593Smuzhiyun 	/* starting block number within the dmap */
3747*4882a593Smuzhiyun 	blkno = Blkno & (BPERDMAP - 1);
3748*4882a593Smuzhiyun 
3749*4882a593Smuzhiyun 	if (blkno == 0) {
3750*4882a593Smuzhiyun 		dp->nblocks = dp->nfree = cpu_to_le32(nblocks);
3751*4882a593Smuzhiyun 		dp->start = cpu_to_le64(Blkno);
3752*4882a593Smuzhiyun 
3753*4882a593Smuzhiyun 		if (nblocks == BPERDMAP) {
3754*4882a593Smuzhiyun 			memset(&dp->wmap[0], 0, LPERDMAP * 4);
3755*4882a593Smuzhiyun 			memset(&dp->pmap[0], 0, LPERDMAP * 4);
3756*4882a593Smuzhiyun 			goto initTree;
3757*4882a593Smuzhiyun 		}
3758*4882a593Smuzhiyun 	} else {
3759*4882a593Smuzhiyun 		le32_add_cpu(&dp->nblocks, nblocks);
3760*4882a593Smuzhiyun 		le32_add_cpu(&dp->nfree, nblocks);
3761*4882a593Smuzhiyun 	}
3762*4882a593Smuzhiyun 
3763*4882a593Smuzhiyun 	/* word number containing start block number */
3764*4882a593Smuzhiyun 	w = blkno >> L2DBWORD;
3765*4882a593Smuzhiyun 
3766*4882a593Smuzhiyun 	/*
3767*4882a593Smuzhiyun 	 * free the bits corresponding to the block range (ZEROS):
3768*4882a593Smuzhiyun 	 * note: not all bits of the first and last words may be contained
3769*4882a593Smuzhiyun 	 * within the block range.
3770*4882a593Smuzhiyun 	 */
3771*4882a593Smuzhiyun 	for (r = nblocks; r > 0; r -= nb, blkno += nb) {
3772*4882a593Smuzhiyun 		/* number of bits preceding range to be freed in the word */
3773*4882a593Smuzhiyun 		b = blkno & (DBWORD - 1);
3774*4882a593Smuzhiyun 		/* number of bits to free in the word */
3775*4882a593Smuzhiyun 		nb = min(r, DBWORD - b);
3776*4882a593Smuzhiyun 
3777*4882a593Smuzhiyun 		/* is partial word to be freed ? */
3778*4882a593Smuzhiyun 		if (nb < DBWORD) {
3779*4882a593Smuzhiyun 			/* free (set to 0) from the bitmap word */
3780*4882a593Smuzhiyun 			dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3781*4882a593Smuzhiyun 						     >> b));
3782*4882a593Smuzhiyun 			dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3783*4882a593Smuzhiyun 						     >> b));
3784*4882a593Smuzhiyun 
3785*4882a593Smuzhiyun 			/* skip the word freed */
3786*4882a593Smuzhiyun 			w++;
3787*4882a593Smuzhiyun 		} else {
3788*4882a593Smuzhiyun 			/* free (set to 0) contiguous bitmap words */
3789*4882a593Smuzhiyun 			nw = r >> L2DBWORD;
3790*4882a593Smuzhiyun 			memset(&dp->wmap[w], 0, nw * 4);
3791*4882a593Smuzhiyun 			memset(&dp->pmap[w], 0, nw * 4);
3792*4882a593Smuzhiyun 
3793*4882a593Smuzhiyun 			/* skip the words freed */
3794*4882a593Smuzhiyun 			nb = nw << L2DBWORD;
3795*4882a593Smuzhiyun 			w += nw;
3796*4882a593Smuzhiyun 		}
3797*4882a593Smuzhiyun 	}
3798*4882a593Smuzhiyun 
3799*4882a593Smuzhiyun 	/*
3800*4882a593Smuzhiyun 	 * mark bits following the range to be freed (non-existing
3801*4882a593Smuzhiyun 	 * blocks) as allocated (ONES)
3802*4882a593Smuzhiyun 	 */
3803*4882a593Smuzhiyun 
3804*4882a593Smuzhiyun 	if (blkno == BPERDMAP)
3805*4882a593Smuzhiyun 		goto initTree;
3806*4882a593Smuzhiyun 
3807*4882a593Smuzhiyun 	/* the first word beyond the end of existing blocks */
3808*4882a593Smuzhiyun 	w = blkno >> L2DBWORD;
3809*4882a593Smuzhiyun 
3810*4882a593Smuzhiyun 	/* does nblocks fall on a 32-bit boundary ? */
3811*4882a593Smuzhiyun 	b = blkno & (DBWORD - 1);
3812*4882a593Smuzhiyun 	if (b) {
3813*4882a593Smuzhiyun 		/* mark a partial word allocated */
3814*4882a593Smuzhiyun 		dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b);
3815*4882a593Smuzhiyun 		w++;
3816*4882a593Smuzhiyun 	}
3817*4882a593Smuzhiyun 
3818*4882a593Smuzhiyun 	/* set the rest of the words in the page to allocated (ONES) */
3819*4882a593Smuzhiyun 	for (i = w; i < LPERDMAP; i++)
3820*4882a593Smuzhiyun 		dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES);
3821*4882a593Smuzhiyun 
3822*4882a593Smuzhiyun 	/*
3823*4882a593Smuzhiyun 	 * init tree
3824*4882a593Smuzhiyun 	 */
3825*4882a593Smuzhiyun       initTree:
3826*4882a593Smuzhiyun 	return (dbInitDmapTree(dp));
3827*4882a593Smuzhiyun }
3828*4882a593Smuzhiyun 
3829*4882a593Smuzhiyun 
3830*4882a593Smuzhiyun /*
3831*4882a593Smuzhiyun  * NAME:	dbInitDmapTree()/ujfs_complete_dmap()
3832*4882a593Smuzhiyun  *
3833*4882a593Smuzhiyun  * FUNCTION:	initialize summary tree of the specified dmap:
3834*4882a593Smuzhiyun  *
3835*4882a593Smuzhiyun  *		at entry, bitmap of the dmap has been initialized;
3836*4882a593Smuzhiyun  *
3837*4882a593Smuzhiyun  * PARAMETERS:
3838*4882a593Smuzhiyun  *	dp	- dmap to complete
3839*4882a593Smuzhiyun  *	blkno	- starting block number for this dmap
3840*4882a593Smuzhiyun  *	treemax	- will be filled in with max free for this dmap
3841*4882a593Smuzhiyun  *
3842*4882a593Smuzhiyun  * RETURNS:	max free string at the root of the tree
3843*4882a593Smuzhiyun  */
dbInitDmapTree(struct dmap * dp)3844*4882a593Smuzhiyun static int dbInitDmapTree(struct dmap * dp)
3845*4882a593Smuzhiyun {
3846*4882a593Smuzhiyun 	struct dmaptree *tp;
3847*4882a593Smuzhiyun 	s8 *cp;
3848*4882a593Smuzhiyun 	int i;
3849*4882a593Smuzhiyun 
3850*4882a593Smuzhiyun 	/* init fixed info of tree */
3851*4882a593Smuzhiyun 	tp = &dp->tree;
3852*4882a593Smuzhiyun 	tp->nleafs = cpu_to_le32(LPERDMAP);
3853*4882a593Smuzhiyun 	tp->l2nleafs = cpu_to_le32(L2LPERDMAP);
3854*4882a593Smuzhiyun 	tp->leafidx = cpu_to_le32(LEAFIND);
3855*4882a593Smuzhiyun 	tp->height = cpu_to_le32(4);
3856*4882a593Smuzhiyun 	tp->budmin = BUDMIN;
3857*4882a593Smuzhiyun 
3858*4882a593Smuzhiyun 	/* init each leaf from corresponding wmap word:
3859*4882a593Smuzhiyun 	 * note: leaf is set to NOFREE(-1) if all blocks of corresponding
3860*4882a593Smuzhiyun 	 * bitmap word are allocated.
3861*4882a593Smuzhiyun 	 */
3862*4882a593Smuzhiyun 	cp = tp->stree + le32_to_cpu(tp->leafidx);
3863*4882a593Smuzhiyun 	for (i = 0; i < LPERDMAP; i++)
3864*4882a593Smuzhiyun 		*cp++ = dbMaxBud((u8 *) & dp->wmap[i]);
3865*4882a593Smuzhiyun 
3866*4882a593Smuzhiyun 	/* build the dmap's binary buddy summary tree */
3867*4882a593Smuzhiyun 	return (dbInitTree(tp));
3868*4882a593Smuzhiyun }
3869*4882a593Smuzhiyun 
3870*4882a593Smuzhiyun 
3871*4882a593Smuzhiyun /*
3872*4882a593Smuzhiyun  * NAME:	dbInitTree()/ujfs_adjtree()
3873*4882a593Smuzhiyun  *
3874*4882a593Smuzhiyun  * FUNCTION:	initialize binary buddy summary tree of a dmap or dmapctl.
3875*4882a593Smuzhiyun  *
3876*4882a593Smuzhiyun  *		at entry, the leaves of the tree has been initialized
3877*4882a593Smuzhiyun  *		from corresponding bitmap word or root of summary tree
3878*4882a593Smuzhiyun  *		of the child control page;
3879*4882a593Smuzhiyun  *		configure binary buddy system at the leaf level, then
3880*4882a593Smuzhiyun  *		bubble up the values of the leaf nodes up the tree.
3881*4882a593Smuzhiyun  *
3882*4882a593Smuzhiyun  * PARAMETERS:
3883*4882a593Smuzhiyun  *	cp	- Pointer to the root of the tree
3884*4882a593Smuzhiyun  *	l2leaves- Number of leaf nodes as a power of 2
3885*4882a593Smuzhiyun  *	l2min	- Number of blocks that can be covered by a leaf
3886*4882a593Smuzhiyun  *		  as a power of 2
3887*4882a593Smuzhiyun  *
3888*4882a593Smuzhiyun  * RETURNS: max free string at the root of the tree
3889*4882a593Smuzhiyun  */
dbInitTree(struct dmaptree * dtp)3890*4882a593Smuzhiyun static int dbInitTree(struct dmaptree * dtp)
3891*4882a593Smuzhiyun {
3892*4882a593Smuzhiyun 	int l2max, l2free, bsize, nextb, i;
3893*4882a593Smuzhiyun 	int child, parent, nparent;
3894*4882a593Smuzhiyun 	s8 *tp, *cp, *cp1;
3895*4882a593Smuzhiyun 
3896*4882a593Smuzhiyun 	tp = dtp->stree;
3897*4882a593Smuzhiyun 
3898*4882a593Smuzhiyun 	/* Determine the maximum free string possible for the leaves */
3899*4882a593Smuzhiyun 	l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin;
3900*4882a593Smuzhiyun 
3901*4882a593Smuzhiyun 	/*
3902*4882a593Smuzhiyun 	 * configure the leaf levevl into binary buddy system
3903*4882a593Smuzhiyun 	 *
3904*4882a593Smuzhiyun 	 * Try to combine buddies starting with a buddy size of 1
3905*4882a593Smuzhiyun 	 * (i.e. two leaves). At a buddy size of 1 two buddy leaves
3906*4882a593Smuzhiyun 	 * can be combined if both buddies have a maximum free of l2min;
3907*4882a593Smuzhiyun 	 * the combination will result in the left-most buddy leaf having
3908*4882a593Smuzhiyun 	 * a maximum free of l2min+1.
3909*4882a593Smuzhiyun 	 * After processing all buddies for a given size, process buddies
3910*4882a593Smuzhiyun 	 * at the next higher buddy size (i.e. current size * 2) and
3911*4882a593Smuzhiyun 	 * the next maximum free (current free + 1).
3912*4882a593Smuzhiyun 	 * This continues until the maximum possible buddy combination
3913*4882a593Smuzhiyun 	 * yields maximum free.
3914*4882a593Smuzhiyun 	 */
3915*4882a593Smuzhiyun 	for (l2free = dtp->budmin, bsize = 1; l2free < l2max;
3916*4882a593Smuzhiyun 	     l2free++, bsize = nextb) {
3917*4882a593Smuzhiyun 		/* get next buddy size == current buddy pair size */
3918*4882a593Smuzhiyun 		nextb = bsize << 1;
3919*4882a593Smuzhiyun 
3920*4882a593Smuzhiyun 		/* scan each adjacent buddy pair at current buddy size */
3921*4882a593Smuzhiyun 		for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx);
3922*4882a593Smuzhiyun 		     i < le32_to_cpu(dtp->nleafs);
3923*4882a593Smuzhiyun 		     i += nextb, cp += nextb) {
3924*4882a593Smuzhiyun 			/* coalesce if both adjacent buddies are max free */
3925*4882a593Smuzhiyun 			if (*cp == l2free && *(cp + bsize) == l2free) {
3926*4882a593Smuzhiyun 				*cp = l2free + 1;	/* left take right */
3927*4882a593Smuzhiyun 				*(cp + bsize) = -1;	/* right give left */
3928*4882a593Smuzhiyun 			}
3929*4882a593Smuzhiyun 		}
3930*4882a593Smuzhiyun 	}
3931*4882a593Smuzhiyun 
3932*4882a593Smuzhiyun 	/*
3933*4882a593Smuzhiyun 	 * bubble summary information of leaves up the tree.
3934*4882a593Smuzhiyun 	 *
3935*4882a593Smuzhiyun 	 * Starting at the leaf node level, the four nodes described by
3936*4882a593Smuzhiyun 	 * the higher level parent node are compared for a maximum free and
3937*4882a593Smuzhiyun 	 * this maximum becomes the value of the parent node.
3938*4882a593Smuzhiyun 	 * when all lower level nodes are processed in this fashion then
3939*4882a593Smuzhiyun 	 * move up to the next level (parent becomes a lower level node) and
3940*4882a593Smuzhiyun 	 * continue the process for that level.
3941*4882a593Smuzhiyun 	 */
3942*4882a593Smuzhiyun 	for (child = le32_to_cpu(dtp->leafidx),
3943*4882a593Smuzhiyun 	     nparent = le32_to_cpu(dtp->nleafs) >> 2;
3944*4882a593Smuzhiyun 	     nparent > 0; nparent >>= 2, child = parent) {
3945*4882a593Smuzhiyun 		/* get index of 1st node of parent level */
3946*4882a593Smuzhiyun 		parent = (child - 1) >> 2;
3947*4882a593Smuzhiyun 
3948*4882a593Smuzhiyun 		/* set the value of the parent node as the maximum
3949*4882a593Smuzhiyun 		 * of the four nodes of the current level.
3950*4882a593Smuzhiyun 		 */
3951*4882a593Smuzhiyun 		for (i = 0, cp = tp + child, cp1 = tp + parent;
3952*4882a593Smuzhiyun 		     i < nparent; i++, cp += 4, cp1++)
3953*4882a593Smuzhiyun 			*cp1 = TREEMAX(cp);
3954*4882a593Smuzhiyun 	}
3955*4882a593Smuzhiyun 
3956*4882a593Smuzhiyun 	return (*tp);
3957*4882a593Smuzhiyun }
3958*4882a593Smuzhiyun 
3959*4882a593Smuzhiyun 
3960*4882a593Smuzhiyun /*
3961*4882a593Smuzhiyun  *	dbInitDmapCtl()
3962*4882a593Smuzhiyun  *
3963*4882a593Smuzhiyun  * function: initialize dmapctl page
3964*4882a593Smuzhiyun  */
dbInitDmapCtl(struct dmapctl * dcp,int level,int i)3965*4882a593Smuzhiyun static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i)
3966*4882a593Smuzhiyun {				/* start leaf index not covered by range */
3967*4882a593Smuzhiyun 	s8 *cp;
3968*4882a593Smuzhiyun 
3969*4882a593Smuzhiyun 	dcp->nleafs = cpu_to_le32(LPERCTL);
3970*4882a593Smuzhiyun 	dcp->l2nleafs = cpu_to_le32(L2LPERCTL);
3971*4882a593Smuzhiyun 	dcp->leafidx = cpu_to_le32(CTLLEAFIND);
3972*4882a593Smuzhiyun 	dcp->height = cpu_to_le32(5);
3973*4882a593Smuzhiyun 	dcp->budmin = L2BPERDMAP + L2LPERCTL * level;
3974*4882a593Smuzhiyun 
3975*4882a593Smuzhiyun 	/*
3976*4882a593Smuzhiyun 	 * initialize the leaves of current level that were not covered
3977*4882a593Smuzhiyun 	 * by the specified input block range (i.e. the leaves have no
3978*4882a593Smuzhiyun 	 * low level dmapctl or dmap).
3979*4882a593Smuzhiyun 	 */
3980*4882a593Smuzhiyun 	cp = &dcp->stree[CTLLEAFIND + i];
3981*4882a593Smuzhiyun 	for (; i < LPERCTL; i++)
3982*4882a593Smuzhiyun 		*cp++ = NOFREE;
3983*4882a593Smuzhiyun 
3984*4882a593Smuzhiyun 	/* build the dmap's binary buddy summary tree */
3985*4882a593Smuzhiyun 	return (dbInitTree((struct dmaptree *) dcp));
3986*4882a593Smuzhiyun }
3987*4882a593Smuzhiyun 
3988*4882a593Smuzhiyun 
3989*4882a593Smuzhiyun /*
3990*4882a593Smuzhiyun  * NAME:	dbGetL2AGSize()/ujfs_getagl2size()
3991*4882a593Smuzhiyun  *
3992*4882a593Smuzhiyun  * FUNCTION:	Determine log2(allocation group size) from aggregate size
3993*4882a593Smuzhiyun  *
3994*4882a593Smuzhiyun  * PARAMETERS:
3995*4882a593Smuzhiyun  *	nblocks	- Number of blocks in aggregate
3996*4882a593Smuzhiyun  *
3997*4882a593Smuzhiyun  * RETURNS: log2(allocation group size) in aggregate blocks
3998*4882a593Smuzhiyun  */
dbGetL2AGSize(s64 nblocks)3999*4882a593Smuzhiyun static int dbGetL2AGSize(s64 nblocks)
4000*4882a593Smuzhiyun {
4001*4882a593Smuzhiyun 	s64 sz;
4002*4882a593Smuzhiyun 	s64 m;
4003*4882a593Smuzhiyun 	int l2sz;
4004*4882a593Smuzhiyun 
4005*4882a593Smuzhiyun 	if (nblocks < BPERDMAP * MAXAG)
4006*4882a593Smuzhiyun 		return (L2BPERDMAP);
4007*4882a593Smuzhiyun 
4008*4882a593Smuzhiyun 	/* round up aggregate size to power of 2 */
4009*4882a593Smuzhiyun 	m = ((u64) 1 << (64 - 1));
4010*4882a593Smuzhiyun 	for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) {
4011*4882a593Smuzhiyun 		if (m & nblocks)
4012*4882a593Smuzhiyun 			break;
4013*4882a593Smuzhiyun 	}
4014*4882a593Smuzhiyun 
4015*4882a593Smuzhiyun 	sz = (s64) 1 << l2sz;
4016*4882a593Smuzhiyun 	if (sz < nblocks)
4017*4882a593Smuzhiyun 		l2sz += 1;
4018*4882a593Smuzhiyun 
4019*4882a593Smuzhiyun 	/* agsize = roundupSize/max_number_of_ag */
4020*4882a593Smuzhiyun 	return (l2sz - L2MAXAG);
4021*4882a593Smuzhiyun }
4022*4882a593Smuzhiyun 
4023*4882a593Smuzhiyun 
4024*4882a593Smuzhiyun /*
4025*4882a593Smuzhiyun  * NAME:	dbMapFileSizeToMapSize()
4026*4882a593Smuzhiyun  *
4027*4882a593Smuzhiyun  * FUNCTION:	compute number of blocks the block allocation map file
4028*4882a593Smuzhiyun  *		can cover from the map file size;
4029*4882a593Smuzhiyun  *
4030*4882a593Smuzhiyun  * RETURNS:	Number of blocks which can be covered by this block map file;
4031*4882a593Smuzhiyun  */
4032*4882a593Smuzhiyun 
4033*4882a593Smuzhiyun /*
4034*4882a593Smuzhiyun  * maximum number of map pages at each level including control pages
4035*4882a593Smuzhiyun  */
4036*4882a593Smuzhiyun #define MAXL0PAGES	(1 + LPERCTL)
4037*4882a593Smuzhiyun #define MAXL1PAGES	(1 + LPERCTL * MAXL0PAGES)
4038*4882a593Smuzhiyun 
4039*4882a593Smuzhiyun /*
4040*4882a593Smuzhiyun  * convert number of map pages to the zero origin top dmapctl level
4041*4882a593Smuzhiyun  */
4042*4882a593Smuzhiyun #define BMAPPGTOLEV(npages)	\
4043*4882a593Smuzhiyun 	(((npages) <= 3 + MAXL0PAGES) ? 0 : \
4044*4882a593Smuzhiyun 	 ((npages) <= 2 + MAXL1PAGES) ? 1 : 2)
4045*4882a593Smuzhiyun 
dbMapFileSizeToMapSize(struct inode * ipbmap)4046*4882a593Smuzhiyun s64 dbMapFileSizeToMapSize(struct inode * ipbmap)
4047*4882a593Smuzhiyun {
4048*4882a593Smuzhiyun 	struct super_block *sb = ipbmap->i_sb;
4049*4882a593Smuzhiyun 	s64 nblocks;
4050*4882a593Smuzhiyun 	s64 npages, ndmaps;
4051*4882a593Smuzhiyun 	int level, i;
4052*4882a593Smuzhiyun 	int complete, factor;
4053*4882a593Smuzhiyun 
4054*4882a593Smuzhiyun 	nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize;
4055*4882a593Smuzhiyun 	npages = nblocks >> JFS_SBI(sb)->l2nbperpage;
4056*4882a593Smuzhiyun 	level = BMAPPGTOLEV(npages);
4057*4882a593Smuzhiyun 
4058*4882a593Smuzhiyun 	/* At each level, accumulate the number of dmap pages covered by
4059*4882a593Smuzhiyun 	 * the number of full child levels below it;
4060*4882a593Smuzhiyun 	 * repeat for the last incomplete child level.
4061*4882a593Smuzhiyun 	 */
4062*4882a593Smuzhiyun 	ndmaps = 0;
4063*4882a593Smuzhiyun 	npages--;		/* skip the first global control page */
4064*4882a593Smuzhiyun 	/* skip higher level control pages above top level covered by map */
4065*4882a593Smuzhiyun 	npages -= (2 - level);
4066*4882a593Smuzhiyun 	npages--;		/* skip top level's control page */
4067*4882a593Smuzhiyun 	for (i = level; i >= 0; i--) {
4068*4882a593Smuzhiyun 		factor =
4069*4882a593Smuzhiyun 		    (i == 2) ? MAXL1PAGES : ((i == 1) ? MAXL0PAGES : 1);
4070*4882a593Smuzhiyun 		complete = (u32) npages / factor;
4071*4882a593Smuzhiyun 		ndmaps += complete * ((i == 2) ? LPERCTL * LPERCTL :
4072*4882a593Smuzhiyun 				      ((i == 1) ? LPERCTL : 1));
4073*4882a593Smuzhiyun 
4074*4882a593Smuzhiyun 		/* pages in last/incomplete child */
4075*4882a593Smuzhiyun 		npages = (u32) npages % factor;
4076*4882a593Smuzhiyun 		/* skip incomplete child's level control page */
4077*4882a593Smuzhiyun 		npages--;
4078*4882a593Smuzhiyun 	}
4079*4882a593Smuzhiyun 
4080*4882a593Smuzhiyun 	/* convert the number of dmaps into the number of blocks
4081*4882a593Smuzhiyun 	 * which can be covered by the dmaps;
4082*4882a593Smuzhiyun 	 */
4083*4882a593Smuzhiyun 	nblocks = ndmaps << L2BPERDMAP;
4084*4882a593Smuzhiyun 
4085*4882a593Smuzhiyun 	return (nblocks);
4086*4882a593Smuzhiyun }
4087