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