1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2004
4 * Portions Copyright (C) Tino Reichardt, 2012
8 #include <linux/slab.h>
9 #include "jfs_incore.h"
10 #include "jfs_superblock.h"
14 #include "jfs_metapage.h"
15 #include "jfs_debug.h"
16 #include "jfs_discard.h"
19 * SERIALIZATION of the Block Allocation Map.
21 * the working state of the block allocation map is accessed in
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).
28 * 2) allocation requests that start at dmap control page
29 * level and work down towards the dmaps.
31 * the serialization scheme used here is as follows.
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
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
42 * exclusively while multiple bottoms-up requests may proceed
43 * simultaneously (under the protection of busy buffers).
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);
53 * accesses to the persistent state of the block allocation map (limited
54 * to the persistent bitmaps in dmaps) is guarded by (busy) buffers.
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)
64 static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
66 static void dbSplit(dmtree_t *tp, int leafno, int splitsz, int newval, bool is_ctl);
67 static int dbBackSplit(dmtree_t *tp, int leafno, bool is_ctl);
68 static int dbJoin(dmtree_t *tp, int leafno, int newval, bool is_ctl);
69 static void dbAdjTree(dmtree_t *tp, int leafno, int newval, bool is_ctl);
70 static int dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc,
72 static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results);
73 static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
75 static int dbAllocNear(struct bmap * bmp, struct dmap * dp, s64 blkno,
77 int l2nb, s64 * results);
78 static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
80 static int dbAllocDmapLev(struct bmap * bmp, struct dmap * dp, int nblocks,
83 static int dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb,
85 static int dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno,
87 static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks);
88 static int dbFindBits(u32 word, int l2nb);
89 static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno);
90 static int dbFindLeaf(dmtree_t *tp, int l2nb, int *leafidx, bool is_ctl);
91 static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
93 static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
95 static int dbMaxBud(u8 * cp);
96 static int blkstol2(s64 nb);
98 static int cntlz(u32 value);
99 static int cnttz(u32 word);
101 static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
103 static int dbInitDmap(struct dmap * dp, s64 blkno, int nblocks);
104 static int dbInitDmapTree(struct dmap * dp);
105 static int dbInitTree(struct dmaptree * dtp);
106 static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i);
107 static int dbGetL2AGSize(s64 nblocks);
112 * table used for determining buddy sizes within characters of
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.
117 static const s8 budtab[256] = {
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
139 * FUNCTION: initializate the block allocation map.
141 * memory is allocated for the in-core bmap descriptor and
142 * the in-core descriptor is initialized from disk.
145 * ipbmap - pointer to in-core inode for the block map.
149 * -ENOMEM - insufficient memory
151 * -EINVAL - wrong bmap data
153 int dbMount(struct inode *ipbmap)
156 struct dbmap_disk *dbmp_le;
161 * allocate/initialize the in-memory bmap descriptor
163 /* allocate memory for the in-memory bmap descriptor */
164 bmp = kmalloc(sizeof(struct bmap), GFP_KERNEL);
168 /* read the on-disk bmap descriptor. */
169 mp = read_metapage(ipbmap,
170 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
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);
182 bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
183 if (bmp->db_l2nbperpage > L2PSIZE - L2MINBLOCKSIZE ||
184 bmp->db_l2nbperpage < 0) {
186 goto err_release_metapage;
189 bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
190 if (!bmp->db_numag) {
192 goto err_release_metapage;
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);
198 if (bmp->db_maxag >= MAXAG || bmp->db_maxag < 0 ||
199 bmp->db_agpref >= MAXAG || bmp->db_agpref < 0) {
201 goto err_release_metapage;
204 bmp->db_aglevel = le32_to_cpu(dbmp_le->dn_aglevel);
205 bmp->db_agheight = le32_to_cpu(dbmp_le->dn_agheight);
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);
209 if (bmp->db_agl2size > L2MAXL2SIZE - L2MAXAG ||
210 bmp->db_agl2size < 0) {
212 goto err_release_metapage;
215 if (((bmp->db_mapsize - 1) >> bmp->db_agl2size) > MAXAG) {
217 goto err_release_metapage;
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;
225 /* release the buffer. */
226 release_metapage(mp);
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;
232 memset(bmp->db_active, 0, sizeof(bmp->db_active));
235 * allocate/initialize the bmap lock
241 err_release_metapage:
242 release_metapage(mp);
252 * FUNCTION: terminate the block allocation map in preparation for
253 * file system unmount.
255 * the in-core bmap descriptor is written to disk and
256 * the memory for this descriptor is freed.
259 * ipbmap - pointer to in-core inode for the block map.
265 int dbUnmount(struct inode *ipbmap, int mounterror)
267 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
269 if (!(mounterror || isReadOnly(ipbmap)))
273 * Invalidate the page cache buffers
275 truncate_inode_pages(ipbmap->i_mapping, 0);
277 /* free the memory for the in-memory bmap. */
279 JFS_SBI(ipbmap->i_sb)->bmap = NULL;
287 int dbSync(struct inode *ipbmap)
289 struct dbmap_disk *dbmp_le;
290 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
295 * write bmap global control page
297 /* get the buffer for the on-disk bmap descriptor. */
298 mp = read_metapage(ipbmap,
299 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
302 jfs_err("dbSync: read_metapage failed!");
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);
315 dbmp_le->dn_agheight = cpu_to_le32(bmp->db_agheight);
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;
324 /* write the buffer */
328 * write out dirty pages of bmap
330 filemap_write_and_wait(ipbmap->i_mapping);
332 diWriteSpecial(ipbmap, 0);
340 * FUNCTION: free the specified block range from the working block
343 * the blocks will be free from the working map one dmap
347 * ip - pointer to in-core inode;
348 * blkno - starting block number to be freed.
349 * nblocks - number of blocks to be freed.
355 int dbFree(struct inode *ip, s64 blkno, s64 nblocks)
361 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
362 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
363 struct super_block *sb = ipbmap->i_sb;
365 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
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);
373 jfs_error(ip->i_sb, "block to be freed is outside the map\n");
378 * TRIM the blocks, when mounted with discard option
380 if (JFS_SBI(sb)->flag & JFS_DISCARD)
381 if (JFS_SBI(sb)->minblks_trim <= nblocks)
382 jfs_issue_discard(ipbmap, blkno, nblocks);
385 * free the blocks a dmap at a time.
388 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
389 /* release previous dmap if any */
394 /* get the buffer for the current dmap. */
395 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
396 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
398 IREAD_UNLOCK(ipbmap);
401 dp = (struct dmap *) mp->data;
403 /* determine the number of blocks to be freed from
406 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
408 /* free the blocks. */
409 if ((rc = dbFreeDmap(bmp, dp, blkno, nb))) {
410 jfs_error(ip->i_sb, "error in block map\n");
411 release_metapage(mp);
412 IREAD_UNLOCK(ipbmap);
417 /* write the last buffer. */
421 IREAD_UNLOCK(ipbmap);
428 * NAME: dbUpdatePMap()
430 * FUNCTION: update the allocation state (free or allocate) of the
431 * specified block range in the persistent block allocation map.
433 * the blocks will be updated in the persistent map one
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;
449 dbUpdatePMap(struct inode *ipbmap,
450 int free, s64 blkno, s64 nblocks, struct tblock * tblk)
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;
460 int lsn, difft, diffp;
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);
468 jfs_error(ipbmap->i_sb, "blocks are outside the map\n");
472 /* compute delta of transaction lsn from log syncpt */
474 log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
475 logdiff(difft, lsn, log);
478 * update the block state a dmap at a time.
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) {
490 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE,
494 metapage_wait_for_io(mp);
496 dp = (struct dmap *) mp->data;
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.
502 dbitno = blkno & (BPERDMAP - 1);
503 word = dbitno >> L2DBWORD;
504 nblks = min(rem, (s64)BPERDMAP - dbitno);
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.
509 * partial first and/or last) only in a single pass. a
510 * single pass will also be used to update all words that
511 * are to have all their bits updated.
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.
518 wbitno = dbitno & (DBWORD - 1);
519 nbits = min(rbits, DBWORD - wbitno);
521 /* check if only part of the word is to be updated. */
522 if (nbits < DBWORD) {
523 /* update (free or allocate) the bits
527 (ONES << (DBWORD - nbits) >> wbitno);
537 /* one or more words are to have all
538 * their bits updated. determine how
539 * many words and how many bits.
541 nwords = rbits >> L2DBWORD;
542 nbits = nwords << L2DBWORD;
544 /* update (free or allocate) the bits
548 memset(&dp->pmap[word], 0,
551 memset(&dp->pmap[word], (int) ONES,
561 if (lblkno == lastlblkno)
566 LOGSYNC_LOCK(log, flags);
568 /* inherit older/smaller lsn */
569 logdiff(diffp, mp->lsn, log);
573 /* move bp after tblock in logsync list */
574 list_move(&mp->synclist, &tblk->synclist);
577 /* inherit younger/larger clsn */
578 logdiff(difft, tblk->clsn, log);
579 logdiff(diffp, mp->clsn, log);
581 mp->clsn = tblk->clsn;
586 /* insert bp after tblock in logsync list */
588 list_add(&mp->synclist, &tblk->synclist);
590 mp->clsn = tblk->clsn;
592 LOGSYNC_UNLOCK(log, flags);
595 /* write the last buffer. */
607 * FUNCTION: find the preferred allocation group for new allocations.
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.
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.
623 * ipbmap - pointer to in-core inode for the block map.
626 * the preferred allocation group number.
628 int dbNextAG(struct inode *ipbmap)
635 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
639 /* determine the average number of free blocks within the ags. */
640 avgfree = (u32)bmp->db_nfree / bmp->db_numag;
643 * if the current preferred ag does not have an active allocator
644 * and has at least average freespace, return it
646 agpref = bmp->db_agpref;
647 if ((atomic_read(&bmp->db_active[agpref]) == 0) &&
648 (bmp->db_agfree[agpref] >= avgfree))
651 /* From the last preferred ag, find the next one with at least
652 * average free space.
654 for (i = 0 ; i < bmp->db_numag; i++, agpref++) {
655 if (agpref == bmp->db_numag)
658 if (atomic_read(&bmp->db_active[agpref]))
659 /* open file is currently growing in this ag */
661 if (bmp->db_agfree[agpref] >= avgfree) {
662 /* Return this one */
663 bmp->db_agpref = agpref;
665 } else if (bmp->db_agfree[agpref] > hwm) {
666 /* Less than avg. freespace, but best so far */
667 hwm = bmp->db_agfree[agpref];
673 * If no inactive ag was found with average freespace, use the
677 bmp->db_agpref = next_best;
678 /* else leave db_agpref unchanged */
682 /* return the preferred group.
684 return (bmp->db_agpref);
690 * FUNCTION: attempt to allocate a specified number of contiguous free
691 * blocks from the working allocation block map.
693 * the block allocation policy uses hints and a multi-step
696 * for allocation requests smaller than the number of blocks
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
700 * no blocks near the hint are available, we next try to
701 * allocate within the same dmap as contains the hint.
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.
709 * we also try to allocate anywhere within the aggregate
710 * for allocation requests larger than the allocation group
711 * size or requests that specify no hint value.
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
718 * of the newly allocated contiguous range.
722 * -ENOSPC - insufficient disk resources
725 int dbAlloc(struct inode *ip, s64 hint, s64 nblocks, s64 * results)
728 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
737 /* assert that nblocks is valid */
740 /* get the log2 number of blocks to be allocated.
741 * if the number of blocks is not a log2 multiple,
742 * it will be rounded up to the next log2 multiple.
744 l2nb = BLKSTOL2(nblocks);
746 bmp = JFS_SBI(ip->i_sb)->bmap;
748 mapSize = bmp->db_mapsize;
750 /* the hint should be within the map */
751 if (hint >= mapSize) {
752 jfs_error(ip->i_sb, "the hint is outside the map\n");
756 /* if the number of blocks to be allocated is greater than the
757 * allocation group size, try to allocate anywhere.
759 if (l2nb > bmp->db_agl2size) {
760 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
762 rc = dbAllocAny(bmp, nblocks, l2nb, results);
768 * If no hint, let dbNextAG recommend an allocation group
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.
779 if (blkno >= bmp->db_mapsize)
782 agno = blkno >> bmp->db_agl2size;
784 /* check if blkno crosses over into a new allocation group.
785 * if so, check if we should allow allocations within this
788 if ((blkno & (bmp->db_agsize - 1)) == 0)
789 /* check if the AG is currently being written to.
790 * if so, call dbNextAG() to find a non-busy
791 * AG with sufficient free space.
793 if (atomic_read(&bmp->db_active[agno]))
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.
800 if (nblocks <= BPERDMAP) {
801 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
803 /* get the buffer for the dmap containing the hint.
806 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
807 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
811 dp = (struct dmap *) mp->data;
813 /* first, try to satisfy the allocation request with the
814 * blocks beginning at the hint.
816 if ((rc = dbAllocNext(bmp, dp, blkno, (int) nblocks))
820 mark_metapage_dirty(mp);
823 release_metapage(mp);
827 writers = atomic_read(&bmp->db_active[agno]);
829 ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) {
831 * Someone else is writing in this allocation
832 * group. To avoid fragmenting, try another ag
834 release_metapage(mp);
835 IREAD_UNLOCK(ipbmap);
839 /* next, try to satisfy the allocation request with blocks
843 dbAllocNear(bmp, dp, blkno, (int) nblocks, l2nb, results))
846 mark_metapage_dirty(mp);
848 release_metapage(mp);
852 /* try to satisfy the allocation request with blocks within
853 * the same dmap as the hint.
855 if ((rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results))
858 mark_metapage_dirty(mp);
860 release_metapage(mp);
864 release_metapage(mp);
865 IREAD_UNLOCK(ipbmap);
868 /* try to satisfy the allocation request with blocks within
869 * the same allocation group as the hint.
871 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
872 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC)
875 IWRITE_UNLOCK(ipbmap);
880 * Let dbNextAG recommend a preferred allocation group
882 agno = dbNextAG(ipbmap);
883 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
885 /* Try to allocate within this allocation group. if that fails, try to
886 * allocate anywhere in the map.
888 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC)
889 rc = dbAllocAny(bmp, nblocks, l2nb, results);
892 IWRITE_UNLOCK(ipbmap);
897 IREAD_UNLOCK(ipbmap);
905 * FUNCTION: attempt to extend a current allocation by a specified
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.
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
922 * addnblocks - number of blocks to add to the allocation.
923 * results - on successful return, set to the starting block number
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
931 * -ENOSPC - insufficient disk resources
935 dbReAlloc(struct inode *ip,
936 s64 blkno, s64 nblocks, s64 addnblocks, s64 * results)
940 /* try to extend the allocation in place.
942 if ((rc = dbExtend(ip, blkno, nblocks, addnblocks)) == 0) {
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.)
956 (ip, blkno + nblocks - 1, addnblocks + nblocks, results));
963 * FUNCTION: attempt to extend a current allocation by a specified
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.
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
976 * addnblocks - number of blocks to add to the allocation.
980 * -ENOSPC - insufficient disk resources
983 static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks)
985 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
986 s64 lblkno, lastblkno, extblkno;
991 struct inode *ipbmap = sbi->ipbmap;
995 * We don't want a non-aligned extent to cross a page boundary
997 if (((rel_block = blkno & (sbi->nbperpage - 1))) &&
998 (rel_block + nblocks + addnblocks > sbi->nbperpage))
1001 /* get the last block of the current allocation */
1002 lastblkno = blkno + nblocks - 1;
1004 /* determine the block number of the block following
1005 * the existing allocation.
1007 extblkno = lastblkno + 1;
1009 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
1011 /* better be within the file system */
1013 if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) {
1014 IREAD_UNLOCK(ipbmap);
1015 jfs_error(ip->i_sb, "the block is outside the filesystem\n");
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.
1027 if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize ||
1028 (extblkno & (bmp->db_agsize - 1)) == 0) {
1029 IREAD_UNLOCK(ipbmap);
1033 /* get the buffer for the dmap containing the first block
1036 lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage);
1037 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
1039 IREAD_UNLOCK(ipbmap);
1043 dp = (struct dmap *) mp->data;
1045 /* try to allocate the blocks immediately following the
1046 * current allocation.
1048 rc = dbAllocNext(bmp, dp, extblkno, (int) addnblocks);
1050 IREAD_UNLOCK(ipbmap);
1052 /* were we successful ? */
1056 /* we were not successful */
1057 release_metapage(mp);
1064 * NAME: dbAllocNext()
1066 * FUNCTION: attempt to allocate the blocks of the specified block
1067 * range within a dmap.
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.
1077 * -ENOSPC - insufficient disk resources
1080 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1082 static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
1085 int dbitno, word, rembits, nb, nwords, wbitno, nw;
1090 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1091 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n");
1095 /* pick up a pointer to the leaves of the dmap tree.
1097 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
1099 /* determine the bit number and word within the dmap of the
1102 dbitno = blkno & (BPERDMAP - 1);
1103 word = dbitno >> L2DBWORD;
1105 /* check if the specified block range is contained within
1108 if (dbitno + nblocks > BPERDMAP)
1111 /* check if the starting leaf indicates that anything
1114 if (leaf[word] == NOFREE)
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.
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.
1134 wbitno = dbitno & (DBWORD - 1);
1135 nb = min(rembits, DBWORD - wbitno);
1137 /* check if only part of the word is to be examined.
1140 /* check if the bits are free.
1142 mask = (ONES << (DBWORD - nb) >> wbitno);
1143 if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask)
1148 /* one or more dmap words are fully contained
1149 * within the block range. determine how many
1150 * words and how many bits.
1152 nwords = rembits >> L2DBWORD;
1153 nb = nwords << L2DBWORD;
1155 /* now examine the appropriate leaves to determine
1156 * if the blocks are free.
1158 while (nwords > 0) {
1159 /* does the leaf describe any free space ?
1161 if (leaf[word] < BUDMIN)
1164 /* determine the l2 number of bits provided
1168 min_t(int, leaf[word], NLSTOL2BSZ(nwords));
1170 /* determine how many words were handled.
1172 nw = BUDSIZE(l2size, BUDMIN);
1180 /* allocate the blocks.
1182 return (dbAllocDmap(bmp, dp, blkno, nblocks));
1187 * NAME: dbAllocNear()
1189 * FUNCTION: attempt to allocate a number of contiguous free blocks near
1190 * a specified block (hint) within a dmap.
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.
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
1204 * of the newly allocated range.
1208 * -ENOSPC - insufficient disk resources
1211 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1214 dbAllocNear(struct bmap * bmp,
1215 struct dmap * dp, s64 blkno, int nblocks, int l2nb, s64 * results)
1217 int word, lword, rc;
1220 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1221 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n");
1225 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
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.
1231 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
1232 lword = min(word + 4, LPERDMAP);
1234 /* examine the leaves for sufficient free space.
1236 for (; word < lword; word++) {
1237 /* does the leaf describe sufficient free space ?
1239 if (leaf[word] < l2nb)
1242 /* determine the block number within the file system
1243 * of the first block described by this dmap word.
1245 blkno = le64_to_cpu(dp->start) + (word << L2DBWORD);
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
1252 if (leaf[word] < BUDMIN)
1254 dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb);
1256 /* allocate the blocks.
1258 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
1271 * FUNCTION: attempt to allocate the specified number of contiguous
1272 * free blocks within the specified allocation group.
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,
1288 * the subtree is searched to find the leftmost leaf that
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.
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.
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.
1308 * bmp - pointer to bmap descriptor
1309 * agno - allocation group number.
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
1313 * of the newly allocated range.
1317 * -ENOSPC - insufficient disk resources
1320 * note: IWRITE_LOCK(ipmap) held on entry/exit;
1323 dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb, s64 * results)
1325 struct metapage *mp;
1326 struct dmapctl *dcp;
1327 int rc, ti, i, k, m, n, agperlev;
1331 /* allocation request should not be for more than the
1332 * allocation group size.
1334 if (l2nb > bmp->db_agl2size) {
1335 jfs_error(bmp->db_ipbmap->i_sb,
1336 "allocation request is larger than the allocation group size\n");
1340 /* determine the starting block number of the allocation
1343 blkno = (s64) agno << bmp->db_agl2size;
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
1352 * required space if available.
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.
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,
1371 "dbAllocCtl failed in free AG\n");
1376 /* the buffer for the dmap control page that fully describes the
1379 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel);
1380 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1383 dcp = (struct dmapctl *) mp->data;
1384 budmin = dcp->budmin;
1386 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1387 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n");
1388 release_metapage(mp);
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.
1400 (1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth;
1401 ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1));
1403 /* dmap control page trees fan-out by 4 and a single allocation
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
1409 for (i = 0; i < bmp->db_agwidth; i++, ti++) {
1410 /* is there sufficient free space ?
1412 if (l2nb > dcp->stree[ti])
1415 /* sufficient free space found in a subtree. now search down
1416 * the subtree to find the leftmost leaf that describes this
1419 for (k = bmp->db_agheight; k > 0; k--) {
1420 for (n = 0, m = (ti << 2) + 1; n < 4; n++) {
1421 if (l2nb <= dcp->stree[m + n]) {
1427 jfs_error(bmp->db_ipbmap->i_sb,
1428 "failed descending stree\n");
1429 release_metapage(mp);
1434 /* determine the block number within the file system
1435 * that corresponds to this leaf.
1437 if (bmp->db_aglevel == 2)
1439 else if (bmp->db_aglevel == 1)
1440 blkno &= ~(MAXL1SIZE - 1);
1441 else /* bmp->db_aglevel == 0 */
1442 blkno &= ~(MAXL0SIZE - 1);
1445 ((s64) (ti - le32_to_cpu(dcp->leafidx))) << budmin;
1447 /* release the buffer in preparation for going down
1448 * the next level of dmap control pages.
1450 release_metapage(mp);
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.
1457 if (l2nb < budmin) {
1459 /* search the lower level dmap control pages to get
1460 * the starting block number of the dmap that
1461 * contains or starts off the free space.
1464 dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1,
1466 if (rc == -ENOSPC) {
1467 jfs_error(bmp->db_ipbmap->i_sb,
1468 "control page inconsistent\n");
1475 /* allocate the blocks.
1477 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1478 if (rc == -ENOSPC) {
1479 jfs_error(bmp->db_ipbmap->i_sb,
1480 "unable to allocate blocks\n");
1486 /* no space in the allocation group. release the buffer and
1489 release_metapage(mp);
1496 * NAME: dbAllocAny()
1498 * FUNCTION: attempt to allocate the specified number of contiguous
1499 * free blocks anywhere in the file system.
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.
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
1512 * of the newly allocated range.
1516 * -ENOSPC - insufficient disk resources
1519 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1521 static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results)
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.
1532 if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno)))
1535 /* allocate the blocks.
1537 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1538 if (rc == -ENOSPC) {
1539 jfs_error(bmp->db_ipbmap->i_sb, "unable to allocate blocks\n");
1547 * NAME: dbDiscardAG()
1549 * FUNCTION: attempt to discard (TRIM) all free blocks of specific AG
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
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
1564 * - we write two times to the dmapctl and dmap pages
1565 * - but for me, this seems the best way, better ideas?
1569 * ip - pointer to in-core inode
1571 * minlen - minimum value of contiguous blocks
1574 * s64 - actual number of blocks trimmed
1576 s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen)
1578 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
1579 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
1583 struct super_block *sb = ipbmap->i_sb;
1590 /* max blkno / nblocks pairs to trim */
1591 int count = 0, range_cnt;
1594 /* prevent others from writing new stuff here, while trimming */
1595 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
1597 nblocks = bmp->db_agfree[agno];
1598 max_ranges = nblocks;
1599 do_div(max_ranges, minlen);
1600 range_cnt = min_t(u64, max_ranges + 1, 32 * 1024);
1601 totrim = kmalloc_array(range_cnt, sizeof(struct range2trim), GFP_NOFS);
1602 if (totrim == NULL) {
1603 jfs_error(bmp->db_ipbmap->i_sb, "no memory for trim array\n");
1604 IWRITE_UNLOCK(ipbmap);
1609 while (nblocks >= minlen) {
1610 l2nb = BLKSTOL2(nblocks);
1612 /* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */
1613 rc = dbAllocAG(bmp, agno, nblocks, l2nb, &blkno);
1616 tt->nblocks = nblocks;
1619 /* the whole ag is free, trim now */
1620 if (bmp->db_agfree[agno] == 0)
1623 /* give a hint for the next while */
1624 nblocks = bmp->db_agfree[agno];
1626 } else if (rc == -ENOSPC) {
1627 /* search for next smaller log2 block */
1628 l2nb = BLKSTOL2(nblocks) - 1;
1629 if (unlikely(l2nb < 0))
1631 nblocks = 1LL << l2nb;
1633 /* Trim any already allocated blocks */
1634 jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n");
1638 /* check, if our trim array is full */
1639 if (unlikely(count >= range_cnt - 1))
1642 IWRITE_UNLOCK(ipbmap);
1644 tt->nblocks = 0; /* mark the current end */
1645 for (tt = totrim; tt->nblocks != 0; tt++) {
1646 /* when mounted with online discard, dbFree() will
1647 * call jfs_issue_discard() itself */
1648 if (!(JFS_SBI(sb)->flag & JFS_DISCARD))
1649 jfs_issue_discard(ip, tt->blkno, tt->nblocks);
1650 dbFree(ip, tt->blkno, tt->nblocks);
1651 trimmed += tt->nblocks;
1661 * FUNCTION: starting at a specified dmap control page level and block
1662 * number, search down the dmap control levels for a range of
1663 * contiguous free blocks large enough to satisfy an allocation
1664 * request for the specified number of free blocks.
1666 * if sufficient contiguous free blocks are found, this routine
1667 * returns the starting block number within a dmap page that
1668 * contains or starts a range of contiqious free blocks that
1669 * is sufficient in size.
1672 * bmp - pointer to bmap descriptor
1673 * level - starting dmap control page level.
1674 * l2nb - log2 number of contiguous free blocks desired.
1675 * *blkno - on entry, starting block number for conducting the search.
1676 * on successful return, the first block within a dmap page
1677 * that contains or starts a range of contiguous free blocks.
1681 * -ENOSPC - insufficient disk resources
1684 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1686 static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno)
1688 int rc, leafidx, lev;
1690 struct dmapctl *dcp;
1692 struct metapage *mp;
1694 /* starting at the specified dmap control page level and block
1695 * number, search down the dmap control levels for the starting
1696 * block number of a dmap page that contains or starts off
1697 * sufficient free blocks.
1699 for (lev = level, b = *blkno; lev >= 0; lev--) {
1700 /* get the buffer of the dmap control page for the block
1701 * number and level (i.e. L0, L1, L2).
1703 lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev);
1704 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1707 dcp = (struct dmapctl *) mp->data;
1708 budmin = dcp->budmin;
1710 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1711 jfs_error(bmp->db_ipbmap->i_sb,
1712 "Corrupt dmapctl page\n");
1713 release_metapage(mp);
1717 /* search the tree within the dmap control page for
1718 * sufficient free space. if sufficient free space is found,
1719 * dbFindLeaf() returns the index of the leaf at which
1720 * free space was found.
1722 rc = dbFindLeaf((dmtree_t *) dcp, l2nb, &leafidx, true);
1724 /* release the buffer.
1726 release_metapage(mp);
1732 jfs_error(bmp->db_ipbmap->i_sb,
1733 "dmap inconsistent\n");
1739 /* adjust the block number to reflect the location within
1740 * the dmap control page (i.e. the leaf) at which free
1743 b += (((s64) leafidx) << budmin);
1745 /* we stop the search at this dmap control page level if
1746 * the number of blocks required is greater than or equal
1747 * to the maximum number of blocks described at the next
1760 * NAME: dbAllocCtl()
1762 * FUNCTION: attempt to allocate a specified number of contiguous
1763 * blocks starting within a specific dmap.
1765 * this routine is called by higher level routines that search
1766 * the dmap control pages above the actual dmaps for contiguous
1767 * free space. the result of successful searches by these
1768 * routines are the starting block numbers within dmaps, with
1769 * the dmaps themselves containing the desired contiguous free
1770 * space or starting a contiguous free space of desired size
1771 * that is made up of the blocks of one or more dmaps. these
1772 * calls should not fail due to insufficent resources.
1774 * this routine is called in some cases where it is not known
1775 * whether it will fail due to insufficient resources. more
1776 * specifically, this occurs when allocating from an allocation
1777 * group whose size is equal to the number of blocks per dmap.
1778 * in this case, the dmap control pages are not examined prior
1779 * to calling this routine (to save pathlength) and the call
1782 * for a request size that fits within a dmap, this routine relies
1783 * upon the dmap's dmtree to find the requested contiguous free
1784 * space. for request sizes that are larger than a dmap, the
1785 * requested free space will start at the first block of the
1786 * first dmap (i.e. blkno).
1789 * bmp - pointer to bmap descriptor
1790 * nblocks - actual number of contiguous free blocks to allocate.
1791 * l2nb - log2 number of contiguous free blocks to allocate.
1792 * blkno - starting block number of the dmap to start the allocation
1794 * results - on successful return, set to the starting block number
1795 * of the newly allocated range.
1799 * -ENOSPC - insufficient disk resources
1802 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1805 dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, s64 * results)
1809 struct metapage *mp;
1812 /* check if the allocation request is confined to a single dmap.
1814 if (l2nb <= L2BPERDMAP) {
1815 /* get the buffer for the dmap.
1817 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
1818 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1821 dp = (struct dmap *) mp->data;
1823 /* try to allocate the blocks.
1825 rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results);
1827 mark_metapage_dirty(mp);
1829 release_metapage(mp);
1834 /* allocation request involving multiple dmaps. it must start on
1837 assert((blkno & (BPERDMAP - 1)) == 0);
1839 /* allocate the blocks dmap by dmap.
1841 for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) {
1842 /* get the buffer for the dmap.
1844 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1845 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1850 dp = (struct dmap *) mp->data;
1852 /* the dmap better be all free.
1854 if (dp->tree.stree[ROOT] != L2BPERDMAP) {
1855 release_metapage(mp);
1856 jfs_error(bmp->db_ipbmap->i_sb,
1857 "the dmap is not all free\n");
1862 /* determine how many blocks to allocate from this dmap.
1864 nb = min_t(s64, n, BPERDMAP);
1866 /* allocate the blocks from the dmap.
1868 if ((rc = dbAllocDmap(bmp, dp, b, nb))) {
1869 release_metapage(mp);
1873 /* write the buffer.
1878 /* set the results (starting block number) and return.
1883 /* something failed in handling an allocation request involving
1884 * multiple dmaps. we'll try to clean up by backing out any
1885 * allocation that has already happened for this request. if
1886 * we fail in backing out the allocation, we'll mark the file
1887 * system to indicate that blocks have been leaked.
1891 /* try to backout the allocations dmap by dmap.
1893 for (n = nblocks - n, b = blkno; n > 0;
1894 n -= BPERDMAP, b += BPERDMAP) {
1895 /* get the buffer for this dmap.
1897 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1898 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1900 /* could not back out. mark the file system
1901 * to indicate that we have leaked blocks.
1903 jfs_error(bmp->db_ipbmap->i_sb,
1904 "I/O Error: Block Leakage\n");
1907 dp = (struct dmap *) mp->data;
1909 /* free the blocks is this dmap.
1911 if (dbFreeDmap(bmp, dp, b, BPERDMAP)) {
1912 /* could not back out. mark the file system
1913 * to indicate that we have leaked blocks.
1915 release_metapage(mp);
1916 jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n");
1920 /* write the buffer.
1930 * NAME: dbAllocDmapLev()
1932 * FUNCTION: attempt to allocate a specified number of contiguous blocks
1933 * from a specified dmap.
1935 * this routine checks if the contiguous blocks are available.
1936 * if so, nblocks of blocks are allocated; otherwise, ENOSPC is
1940 * mp - pointer to bmap descriptor
1941 * dp - pointer to dmap to attempt to allocate blocks from.
1942 * l2nb - log2 number of contiguous block desired.
1943 * nblocks - actual number of contiguous block desired.
1944 * results - on successful return, set to the starting block number
1945 * of the newly allocated range.
1949 * -ENOSPC - insufficient disk resources
1952 * serialization: IREAD_LOCK(ipbmap), e.g., from dbAlloc(), or
1953 * IWRITE_LOCK(ipbmap), e.g., dbAllocCtl(), held on entry/exit;
1956 dbAllocDmapLev(struct bmap * bmp,
1957 struct dmap * dp, int nblocks, int l2nb, s64 * results)
1962 /* can't be more than a dmaps worth of blocks */
1963 assert(l2nb <= L2BPERDMAP);
1965 /* search the tree within the dmap page for sufficient
1966 * free space. if sufficient free space is found, dbFindLeaf()
1967 * returns the index of the leaf at which free space was found.
1969 if (dbFindLeaf((dmtree_t *) &dp->tree, l2nb, &leafidx, false))
1975 /* determine the block number within the file system corresponding
1976 * to the leaf at which free space was found.
1978 blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD);
1980 /* if not all bits of the dmap word are free, get the starting
1981 * bit number within the dmap word of the required string of free
1982 * bits and adjust the block number with this value.
1984 if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN)
1985 blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb);
1987 /* allocate the blocks */
1988 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
1996 * NAME: dbAllocDmap()
1998 * FUNCTION: adjust the disk allocation map to reflect the allocation
1999 * of a specified block range within a dmap.
2001 * this routine allocates the specified blocks from the dmap
2002 * through a call to dbAllocBits(). if the allocation of the
2003 * block range causes the maximum string of free blocks within
2004 * the dmap to change (i.e. the value of the root of the dmap's
2005 * dmtree), this routine will cause this change to be reflected
2006 * up through the appropriate levels of the dmap control pages
2007 * by a call to dbAdjCtl() for the L0 dmap control page that
2011 * bmp - pointer to bmap descriptor
2012 * dp - pointer to dmap to allocate the block range from.
2013 * blkno - starting block number of the block to be allocated.
2014 * nblocks - number of blocks to be allocated.
2020 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2022 static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2028 /* save the current value of the root (i.e. maximum free string)
2031 oldroot = dp->tree.stree[ROOT];
2033 /* allocate the specified (blocks) bits */
2034 dbAllocBits(bmp, dp, blkno, nblocks);
2036 /* if the root has not changed, done. */
2037 if (dp->tree.stree[ROOT] == oldroot)
2040 /* root changed. bubble the change up to the dmap control pages.
2041 * if the adjustment of the upper level control pages fails,
2042 * backout the bit allocation (thus making everything consistent).
2044 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0)))
2045 dbFreeBits(bmp, dp, blkno, nblocks);
2052 * NAME: dbFreeDmap()
2054 * FUNCTION: adjust the disk allocation map to reflect the allocation
2055 * of a specified block range within a dmap.
2057 * this routine frees the specified blocks from the dmap through
2058 * a call to dbFreeBits(). if the deallocation of the block range
2059 * causes the maximum string of free blocks within the dmap to
2060 * change (i.e. the value of the root of the dmap's dmtree), this
2061 * routine will cause this change to be reflected up through the
2062 * appropriate levels of the dmap control pages by a call to
2063 * dbAdjCtl() for the L0 dmap control page that covers this dmap.
2066 * bmp - pointer to bmap descriptor
2067 * dp - pointer to dmap to free the block range from.
2068 * blkno - starting block number of the block to be freed.
2069 * nblocks - number of blocks to be freed.
2075 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2077 static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2083 /* save the current value of the root (i.e. maximum free string)
2086 oldroot = dp->tree.stree[ROOT];
2088 /* free the specified (blocks) bits */
2089 rc = dbFreeBits(bmp, dp, blkno, nblocks);
2091 /* if error or the root has not changed, done. */
2092 if (rc || (dp->tree.stree[ROOT] == oldroot))
2095 /* root changed. bubble the change up to the dmap control pages.
2096 * if the adjustment of the upper level control pages fails,
2097 * backout the deallocation.
2099 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) {
2100 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
2102 /* as part of backing out the deallocation, we will have
2103 * to back split the dmap tree if the deallocation caused
2104 * the freed blocks to become part of a larger binary buddy
2107 if (dp->tree.stree[word] == NOFREE)
2108 dbBackSplit((dmtree_t *)&dp->tree, word, false);
2110 dbAllocBits(bmp, dp, blkno, nblocks);
2118 * NAME: dbAllocBits()
2120 * FUNCTION: allocate a specified block range from a dmap.
2122 * this routine updates the dmap to reflect the working
2123 * state allocation of the specified block range. it directly
2124 * updates the bits of the working map and causes the adjustment
2125 * of the binary buddy system described by the dmap's dmtree
2126 * leaves to reflect the bits allocated. it also causes the
2127 * dmap's dmtree, as a whole, to reflect the allocated range.
2130 * bmp - pointer to bmap descriptor
2131 * dp - pointer to dmap to allocate bits from.
2132 * blkno - starting block number of the bits to be allocated.
2133 * nblocks - number of bits to be allocated.
2135 * RETURN VALUES: none
2137 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2139 static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
2142 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2143 dmtree_t *tp = (dmtree_t *) & dp->tree;
2147 /* pick up a pointer to the leaves of the dmap tree */
2148 leaf = dp->tree.stree + LEAFIND;
2150 /* determine the bit number and word within the dmap of the
2153 dbitno = blkno & (BPERDMAP - 1);
2154 word = dbitno >> L2DBWORD;
2156 /* block range better be within the dmap */
2157 assert(dbitno + nblocks <= BPERDMAP);
2159 /* allocate the bits of the dmap's words corresponding to the block
2160 * range. not all bits of the first and last words may be contained
2161 * within the block range. if this is the case, we'll work against
2162 * those words (i.e. partial first and/or last) on an individual basis
2163 * (a single pass), allocating the bits of interest by hand and
2164 * updating the leaf corresponding to the dmap word. a single pass
2165 * will be used for all dmap words fully contained within the
2166 * specified range. within this pass, the bits of all fully contained
2167 * dmap words will be marked as free in a single shot and the leaves
2168 * will be updated. a single leaf may describe the free space of
2169 * multiple dmap words, so we may update only a subset of the actual
2170 * leaves corresponding to the dmap words of the block range.
2172 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2173 /* determine the bit number within the word and
2174 * the number of bits within the word.
2176 wbitno = dbitno & (DBWORD - 1);
2177 nb = min(rembits, DBWORD - wbitno);
2179 /* check if only part of a word is to be allocated.
2182 /* allocate (set to 1) the appropriate bits within
2185 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
2188 /* update the leaf for this dmap word. in addition
2189 * to setting the leaf value to the binary buddy max
2190 * of the updated dmap word, dbSplit() will split
2191 * the binary system of the leaves if need be.
2193 dbSplit(tp, word, BUDMIN,
2194 dbMaxBud((u8 *)&dp->wmap[word]), false);
2198 /* one or more dmap words are fully contained
2199 * within the block range. determine how many
2200 * words and allocate (set to 1) the bits of these
2203 nwords = rembits >> L2DBWORD;
2204 memset(&dp->wmap[word], (int) ONES, nwords * 4);
2206 /* determine how many bits.
2208 nb = nwords << L2DBWORD;
2210 /* now update the appropriate leaves to reflect
2211 * the allocated words.
2213 for (; nwords > 0; nwords -= nw) {
2214 if (leaf[word] < BUDMIN) {
2215 jfs_error(bmp->db_ipbmap->i_sb,
2216 "leaf page corrupt\n");
2220 /* determine what the leaf value should be
2221 * updated to as the minimum of the l2 number
2222 * of bits being allocated and the l2 number
2223 * of bits currently described by this leaf.
2225 size = min_t(int, leaf[word],
2226 NLSTOL2BSZ(nwords));
2228 /* update the leaf to reflect the allocation.
2229 * in addition to setting the leaf value to
2230 * NOFREE, dbSplit() will split the binary
2231 * system of the leaves to reflect the current
2232 * allocation (size).
2234 dbSplit(tp, word, size, NOFREE, false);
2236 /* get the number of dmap words handled */
2237 nw = BUDSIZE(size, BUDMIN);
2243 /* update the free count for this dmap */
2244 le32_add_cpu(&dp->nfree, -nblocks);
2248 /* if this allocation group is completely free,
2249 * update the maximum allocation group number if this allocation
2250 * group is the new max.
2252 agno = blkno >> bmp->db_agl2size;
2253 if (agno > bmp->db_maxag)
2254 bmp->db_maxag = agno;
2256 /* update the free count for the allocation group and map */
2257 bmp->db_agfree[agno] -= nblocks;
2258 bmp->db_nfree -= nblocks;
2265 * NAME: dbFreeBits()
2267 * FUNCTION: free a specified block range from a dmap.
2269 * this routine updates the dmap to reflect the working
2270 * state allocation of the specified block range. it directly
2271 * updates the bits of the working map and causes the adjustment
2272 * of the binary buddy system described by the dmap's dmtree
2273 * leaves to reflect the bits freed. it also causes the dmap's
2274 * dmtree, as a whole, to reflect the deallocated range.
2277 * bmp - pointer to bmap descriptor
2278 * dp - pointer to dmap to free bits from.
2279 * blkno - starting block number of the bits to be freed.
2280 * nblocks - number of bits to be freed.
2282 * RETURN VALUES: 0 for success
2284 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2286 static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
2289 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2290 dmtree_t *tp = (dmtree_t *) & dp->tree;
2294 /* determine the bit number and word within the dmap of the
2297 dbitno = blkno & (BPERDMAP - 1);
2298 word = dbitno >> L2DBWORD;
2300 /* block range better be within the dmap.
2302 assert(dbitno + nblocks <= BPERDMAP);
2304 /* free the bits of the dmaps words corresponding to the block range.
2305 * not all bits of the first and last words may be contained within
2306 * the block range. if this is the case, we'll work against those
2307 * words (i.e. partial first and/or last) on an individual basis
2308 * (a single pass), freeing the bits of interest by hand and updating
2309 * the leaf corresponding to the dmap word. a single pass will be used
2310 * for all dmap words fully contained within the specified range.
2311 * within this pass, the bits of all fully contained dmap words will
2312 * be marked as free in a single shot and the leaves will be updated. a
2313 * single leaf may describe the free space of multiple dmap words,
2314 * so we may update only a subset of the actual leaves corresponding
2315 * to the dmap words of the block range.
2317 * dbJoin() is used to update leaf values and will join the binary
2318 * buddy system of the leaves if the new leaf values indicate this
2321 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2322 /* determine the bit number within the word and
2323 * the number of bits within the word.
2325 wbitno = dbitno & (DBWORD - 1);
2326 nb = min(rembits, DBWORD - wbitno);
2328 /* check if only part of a word is to be freed.
2331 /* free (zero) the appropriate bits within this
2335 cpu_to_le32(~(ONES << (DBWORD - nb)
2338 /* update the leaf for this dmap word.
2340 rc = dbJoin(tp, word,
2341 dbMaxBud((u8 *)&dp->wmap[word]), false);
2347 /* one or more dmap words are fully contained
2348 * within the block range. determine how many
2349 * words and free (zero) the bits of these words.
2351 nwords = rembits >> L2DBWORD;
2352 memset(&dp->wmap[word], 0, nwords * 4);
2354 /* determine how many bits.
2356 nb = nwords << L2DBWORD;
2358 /* now update the appropriate leaves to reflect
2361 for (; nwords > 0; nwords -= nw) {
2362 /* determine what the leaf value should be
2363 * updated to as the minimum of the l2 number
2364 * of bits being freed and the l2 (max) number
2365 * of bits that can be described by this leaf.
2369 (word, L2LPERDMAP, BUDMIN),
2370 NLSTOL2BSZ(nwords));
2374 rc = dbJoin(tp, word, size, false);
2378 /* get the number of dmap words handled.
2380 nw = BUDSIZE(size, BUDMIN);
2386 /* update the free count for this dmap.
2388 le32_add_cpu(&dp->nfree, nblocks);
2392 /* update the free count for the allocation group and
2395 agno = blkno >> bmp->db_agl2size;
2396 bmp->db_nfree += nblocks;
2397 bmp->db_agfree[agno] += nblocks;
2399 /* check if this allocation group is not completely free and
2400 * if it is currently the maximum (rightmost) allocation group.
2401 * if so, establish the new maximum allocation group number by
2402 * searching left for the first allocation group with allocation.
2404 if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) ||
2405 (agno == bmp->db_numag - 1 &&
2406 bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) {
2407 while (bmp->db_maxag > 0) {
2409 if (bmp->db_agfree[bmp->db_maxag] !=
2414 /* re-establish the allocation group preference if the
2415 * current preference is right of the maximum allocation
2418 if (bmp->db_agpref > bmp->db_maxag)
2419 bmp->db_agpref = bmp->db_maxag;
2431 * FUNCTION: adjust a dmap control page at a specified level to reflect
2432 * the change in a lower level dmap or dmap control page's
2433 * maximum string of free blocks (i.e. a change in the root
2434 * of the lower level object's dmtree) due to the allocation
2435 * or deallocation of a range of blocks with a single dmap.
2437 * on entry, this routine is provided with the new value of
2438 * the lower level dmap or dmap control page root and the
2439 * starting block number of the block range whose allocation
2440 * or deallocation resulted in the root change. this range
2441 * is respresented by a single leaf of the current dmapctl
2442 * and the leaf will be updated with this value, possibly
2443 * causing a binary buddy system within the leaves to be
2444 * split or joined. the update may also cause the dmapctl's
2445 * dmtree to be updated.
2447 * if the adjustment of the dmap control page, itself, causes its
2448 * root to change, this change will be bubbled up to the next dmap
2449 * control level by a recursive call to this routine, specifying
2450 * the new root value and the next dmap control page level to
2453 * bmp - pointer to bmap descriptor
2454 * blkno - the first block of a block range within a dmap. it is
2455 * the allocation or deallocation of this block range that
2456 * requires the dmap control page to be adjusted.
2457 * newval - the new value of the lower level dmap or dmap control
2459 * alloc - 'true' if adjustment is due to an allocation.
2460 * level - current level of dmap control page (i.e. L0, L1, L2) to
2467 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2470 dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc, int level)
2472 struct metapage *mp;
2476 struct dmapctl *dcp;
2479 /* get the buffer for the dmap control page for the specified
2480 * block number and control page level.
2482 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level);
2483 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
2486 dcp = (struct dmapctl *) mp->data;
2488 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
2489 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n");
2490 release_metapage(mp);
2494 /* determine the leaf number corresponding to the block and
2495 * the index within the dmap control tree.
2497 leafno = BLKTOCTLLEAF(blkno, dcp->budmin);
2498 ti = leafno + le32_to_cpu(dcp->leafidx);
2500 /* save the current leaf value and the current root level (i.e.
2501 * maximum l2 free string described by this dmapctl).
2503 oldval = dcp->stree[ti];
2504 oldroot = dcp->stree[ROOT];
2506 /* check if this is a control page update for an allocation.
2507 * if so, update the leaf to reflect the new leaf value using
2508 * dbSplit(); otherwise (deallocation), use dbJoin() to update
2509 * the leaf with the new value. in addition to updating the
2510 * leaf, dbSplit() will also split the binary buddy system of
2511 * the leaves, if required, and bubble new values within the
2512 * dmapctl tree, if required. similarly, dbJoin() will join
2513 * the binary buddy system of leaves and bubble new values up
2514 * the dmapctl tree as required by the new leaf value.
2517 /* check if we are in the middle of a binary buddy
2518 * system. this happens when we are performing the
2519 * first allocation out of an allocation group that
2520 * is part (not the first part) of a larger binary
2521 * buddy system. if we are in the middle, back split
2522 * the system prior to calling dbSplit() which assumes
2523 * that it is at the front of a binary buddy system.
2525 if (oldval == NOFREE) {
2526 rc = dbBackSplit((dmtree_t *)dcp, leafno, true);
2528 release_metapage(mp);
2531 oldval = dcp->stree[ti];
2533 dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval, true);
2535 rc = dbJoin((dmtree_t *) dcp, leafno, newval, true);
2537 release_metapage(mp);
2542 /* check if the root of the current dmap control page changed due
2543 * to the update and if the current dmap control page is not at
2544 * the current top level (i.e. L0, L1, L2) of the map. if so (i.e.
2545 * root changed and this is not the top level), call this routine
2546 * again (recursion) for the next higher level of the mapping to
2547 * reflect the change in root for the current dmap control page.
2549 if (dcp->stree[ROOT] != oldroot) {
2550 /* are we below the top level of the map. if so,
2551 * bubble the root up to the next higher level.
2553 if (level < bmp->db_maxlevel) {
2554 /* bubble up the new root of this dmap control page to
2558 dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc,
2560 /* something went wrong in bubbling up the new
2561 * root value, so backout the changes to the
2562 * current dmap control page.
2565 dbJoin((dmtree_t *) dcp, leafno,
2568 /* the dbJoin() above might have
2569 * caused a larger binary buddy system
2570 * to form and we may now be in the
2571 * middle of it. if this is the case,
2572 * back split the buddies.
2574 if (dcp->stree[ti] == NOFREE)
2575 dbBackSplit((dmtree_t *)
2577 dbSplit((dmtree_t *) dcp, leafno,
2578 dcp->budmin, oldval, true);
2581 /* release the buffer and return the error.
2583 release_metapage(mp);
2587 /* we're at the top level of the map. update
2588 * the bmap control page to reflect the size
2589 * of the maximum free buddy system.
2591 assert(level == bmp->db_maxlevel);
2592 if (bmp->db_maxfreebud != oldroot) {
2593 jfs_error(bmp->db_ipbmap->i_sb,
2594 "the maximum free buddy is not the old root\n");
2596 bmp->db_maxfreebud = dcp->stree[ROOT];
2600 /* write the buffer.
2611 * FUNCTION: update the leaf of a dmtree with a new value, splitting
2612 * the leaf from the binary buddy system of the dmtree's
2613 * leaves, as required.
2616 * tp - pointer to the tree containing the leaf.
2617 * leafno - the number of the leaf to be updated.
2618 * splitsz - the size the binary buddy system starting at the leaf
2619 * must be split to, specified as the log2 number of blocks.
2620 * newval - the new value for the leaf.
2622 * RETURN VALUES: none
2624 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2626 static void dbSplit(dmtree_t *tp, int leafno, int splitsz, int newval, bool is_ctl)
2630 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2632 /* check if the leaf needs to be split.
2634 if (leaf[leafno] > tp->dmt_budmin) {
2635 /* the split occurs by cutting the buddy system in half
2636 * at the specified leaf until we reach the specified
2637 * size. pick up the starting split size (current size
2638 * - 1 in l2) and the corresponding buddy size.
2640 cursz = leaf[leafno] - 1;
2641 budsz = BUDSIZE(cursz, tp->dmt_budmin);
2643 /* split until we reach the specified size.
2645 while (cursz >= splitsz) {
2646 /* update the buddy's leaf with its new value.
2648 dbAdjTree(tp, leafno ^ budsz, cursz, is_ctl);
2650 /* on to the next size and buddy.
2657 /* adjust the dmap tree to reflect the specified leaf's new
2660 dbAdjTree(tp, leafno, newval, is_ctl);
2665 * NAME: dbBackSplit()
2667 * FUNCTION: back split the binary buddy system of dmtree leaves
2668 * that hold a specified leaf until the specified leaf
2669 * starts its own binary buddy system.
2671 * the allocators typically perform allocations at the start
2672 * of binary buddy systems and dbSplit() is used to accomplish
2673 * any required splits. in some cases, however, allocation
2674 * may occur in the middle of a binary system and requires a
2675 * back split, with the split proceeding out from the middle of
2676 * the system (less efficient) rather than the start of the
2677 * system (more efficient). the cases in which a back split
2678 * is required are rare and are limited to the first allocation
2679 * within an allocation group which is a part (not first part)
2680 * of a larger binary buddy system and a few exception cases
2681 * in which a previous join operation must be backed out.
2684 * tp - pointer to the tree containing the leaf.
2685 * leafno - the number of the leaf to be updated.
2687 * RETURN VALUES: none
2689 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2691 static int dbBackSplit(dmtree_t *tp, int leafno, bool is_ctl)
2693 int budsz, bud, w, bsz, size;
2695 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2697 /* leaf should be part (not first part) of a binary
2700 assert(leaf[leafno] == NOFREE);
2702 /* the back split is accomplished by iteratively finding the leaf
2703 * that starts the buddy system that contains the specified leaf and
2704 * splitting that system in two. this iteration continues until
2705 * the specified leaf becomes the start of a buddy system.
2707 * determine maximum possible l2 size for the specified leaf.
2710 LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs),
2713 /* determine the number of leaves covered by this size. this
2714 * is the buddy size that we will start with as we search for
2715 * the buddy system that contains the specified leaf.
2717 budsz = BUDSIZE(size, tp->dmt_budmin);
2721 while (leaf[leafno] == NOFREE) {
2722 /* find the leftmost buddy leaf.
2724 for (w = leafno, bsz = budsz;; bsz <<= 1,
2725 w = (w < bud) ? w : bud) {
2726 if (bsz >= le32_to_cpu(tp->dmt_nleafs)) {
2727 jfs_err("JFS: block map error in dbBackSplit");
2731 /* determine the buddy.
2735 /* check if this buddy is the start of the system.
2737 if (leaf[bud] != NOFREE) {
2738 /* split the leaf at the start of the
2741 cursz = leaf[bud] - 1;
2742 dbSplit(tp, bud, cursz, cursz, is_ctl);
2748 if (leaf[leafno] != size) {
2749 jfs_err("JFS: wrong leaf value in dbBackSplit");
2759 * FUNCTION: update the leaf of a dmtree with a new value, joining
2760 * the leaf with other leaves of the dmtree into a multi-leaf
2761 * binary buddy system, as required.
2764 * tp - pointer to the tree containing the leaf.
2765 * leafno - the number of the leaf to be updated.
2766 * newval - the new value for the leaf.
2768 * RETURN VALUES: none
2770 static int dbJoin(dmtree_t *tp, int leafno, int newval, bool is_ctl)
2775 /* can the new leaf value require a join with other leaves ?
2777 if (newval >= tp->dmt_budmin) {
2778 /* pickup a pointer to the leaves of the tree.
2780 leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2782 /* try to join the specified leaf into a large binary
2783 * buddy system. the join proceeds by attempting to join
2784 * the specified leafno with its buddy (leaf) at new value.
2785 * if the join occurs, we attempt to join the left leaf
2786 * of the joined buddies with its buddy at new value + 1.
2787 * we continue to join until we find a buddy that cannot be
2788 * joined (does not have a value equal to the size of the
2789 * last join) or until all leaves have been joined into a
2792 * get the buddy size (number of words covered) of
2795 budsz = BUDSIZE(newval, tp->dmt_budmin);
2799 while (budsz < le32_to_cpu(tp->dmt_nleafs)) {
2800 /* get the buddy leaf.
2802 buddy = leafno ^ budsz;
2804 /* if the leaf's new value is greater than its
2805 * buddy's value, we join no more.
2807 if (newval > leaf[buddy])
2810 /* It shouldn't be less */
2811 if (newval < leaf[buddy])
2814 /* check which (leafno or buddy) is the left buddy.
2815 * the left buddy gets to claim the blocks resulting
2816 * from the join while the right gets to claim none.
2817 * the left buddy is also eligible to participate in
2818 * a join at the next higher level while the right
2822 if (leafno < buddy) {
2823 /* leafno is the left buddy.
2825 dbAdjTree(tp, buddy, NOFREE, is_ctl);
2827 /* buddy is the left buddy and becomes
2830 dbAdjTree(tp, leafno, NOFREE, is_ctl);
2834 /* on to try the next join.
2841 /* update the leaf value.
2843 dbAdjTree(tp, leafno, newval, is_ctl);
2852 * FUNCTION: update a leaf of a dmtree with a new value, adjusting
2853 * the dmtree, as required, to reflect the new leaf value.
2854 * the combination of any buddies must already be done before
2858 * tp - pointer to the tree to be adjusted.
2859 * leafno - the number of the leaf to be updated.
2860 * newval - the new value for the leaf.
2862 * RETURN VALUES: none
2864 static void dbAdjTree(dmtree_t *tp, int leafno, int newval, bool is_ctl)
2869 size = is_ctl ? CTLTREESIZE : TREESIZE;
2871 /* pick up the index of the leaf for this leafno.
2873 lp = leafno + le32_to_cpu(tp->dmt_leafidx);
2875 if (WARN_ON_ONCE(lp >= size || lp < 0))
2878 /* is the current value the same as the old value ? if so,
2879 * there is nothing to do.
2881 if (tp->dmt_stree[lp] == newval)
2884 /* set the new value.
2886 tp->dmt_stree[lp] = newval;
2888 /* bubble the new value up the tree as required.
2890 for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) {
2891 /* get the index of the first leaf of the 4 leaf
2892 * group containing the specified leaf (leafno).
2894 lp = ((lp - 1) & ~0x03) + 1;
2896 /* get the index of the parent of this 4 leaf group.
2900 /* determine the maximum of the 4 leaves.
2902 max = TREEMAX(&tp->dmt_stree[lp]);
2904 /* if the maximum of the 4 is the same as the
2905 * parent's value, we're done.
2907 if (tp->dmt_stree[pp] == max)
2910 /* parent gets new value.
2912 tp->dmt_stree[pp] = max;
2914 /* parent becomes leaf for next go-round.
2922 * NAME: dbFindLeaf()
2924 * FUNCTION: search a dmtree_t for sufficient free blocks, returning
2925 * the index of a leaf describing the free blocks if
2926 * sufficient free blocks are found.
2928 * the search starts at the top of the dmtree_t tree and
2929 * proceeds down the tree to the leftmost leaf with sufficient
2933 * tp - pointer to the tree to be searched.
2934 * l2nb - log2 number of free blocks to search for.
2935 * leafidx - return pointer to be set to the index of the leaf
2936 * describing at least l2nb free blocks if sufficient
2937 * free blocks are found.
2938 * is_ctl - determines if the tree is of type ctl
2942 * -ENOSPC - insufficient free blocks.
2944 static int dbFindLeaf(dmtree_t *tp, int l2nb, int *leafidx, bool is_ctl)
2946 int ti, n = 0, k, x = 0;
2949 max_size = is_ctl ? CTLTREESIZE : TREESIZE;
2951 /* first check the root of the tree to see if there is
2952 * sufficient free space.
2954 if (l2nb > tp->dmt_stree[ROOT])
2957 /* sufficient free space available. now search down the tree
2958 * starting at the next level for the leftmost leaf that
2959 * describes sufficient free space.
2961 for (k = le32_to_cpu(tp->dmt_height), ti = 1;
2962 k > 0; k--, ti = ((ti + n) << 2) + 1) {
2963 /* search the four nodes at this level, starting from
2966 for (x = ti, n = 0; n < 4; n++) {
2967 /* sufficient free space found. move to the next
2968 * level (or quit if this is the last level).
2970 if (x + n > max_size)
2972 if (l2nb <= tp->dmt_stree[x + n])
2976 /* better have found something since the higher
2977 * levels of the tree said it was here.
2982 /* set the return to the leftmost leaf describing sufficient
2985 *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx);
2992 * NAME: dbFindBits()
2994 * FUNCTION: find a specified number of binary buddy free bits within a
2995 * dmap bitmap word value.
2997 * this routine searches the bitmap value for (1 << l2nb) free
2998 * bits at (1 << l2nb) alignments within the value.
3001 * word - dmap bitmap word value.
3002 * l2nb - number of free bits specified as a log2 number.
3005 * starting bit number of free bits.
3007 static int dbFindBits(u32 word, int l2nb)
3012 /* get the number of bits.
3015 assert(nb <= DBWORD);
3017 /* complement the word so we can use a mask (i.e. 0s represent
3018 * free bits) and compute the mask.
3021 mask = ONES << (DBWORD - nb);
3023 /* scan the word for nb free bits at nb alignments.
3025 for (bitno = 0; mask != 0; bitno += nb, mask >>= nb) {
3026 if ((mask & word) == mask)
3032 /* return the bit number.
3039 * NAME: dbMaxBud(u8 *cp)
3041 * FUNCTION: determine the largest binary buddy string of free
3042 * bits within 32-bits of the map.
3045 * cp - pointer to the 32-bit value.
3048 * largest binary buddy of free bits within a dmap word.
3050 static int dbMaxBud(u8 * cp)
3052 signed char tmp1, tmp2;
3054 /* check if the wmap word is all free. if so, the
3055 * free buddy size is BUDMIN.
3057 if (*((uint *) cp) == 0)
3060 /* check if the wmap word is half free. if so, the
3061 * free buddy size is BUDMIN-1.
3063 if (*((u16 *) cp) == 0 || *((u16 *) cp + 1) == 0)
3064 return (BUDMIN - 1);
3066 /* not all free or half free. determine the free buddy
3067 * size thru table lookup using quarters of the wmap word.
3069 tmp1 = max(budtab[cp[2]], budtab[cp[3]]);
3070 tmp2 = max(budtab[cp[0]], budtab[cp[1]]);
3071 return (max(tmp1, tmp2));
3076 * NAME: cnttz(uint word)
3078 * FUNCTION: determine the number of trailing zeros within a 32-bit
3082 * value - 32-bit value to be examined.
3085 * count of trailing zeros
3087 static int cnttz(u32 word)
3091 for (n = 0; n < 32; n++, word >>= 1) {
3101 * NAME: cntlz(u32 value)
3103 * FUNCTION: determine the number of leading zeros within a 32-bit
3107 * value - 32-bit value to be examined.
3110 * count of leading zeros
3112 static int cntlz(u32 value)
3116 for (n = 0; n < 32; n++, value <<= 1) {
3117 if (value & HIGHORDER)
3125 * NAME: blkstol2(s64 nb)
3127 * FUNCTION: convert a block count to its log2 value. if the block
3128 * count is not a l2 multiple, it is rounded up to the next
3129 * larger l2 multiple.
3132 * nb - number of blocks
3135 * log2 number of blocks
3137 static int blkstol2(s64 nb)
3140 s64 mask; /* meant to be signed */
3142 mask = (s64) 1 << (64 - 1);
3144 /* count the leading bits.
3146 for (l2nb = 0; l2nb < 64; l2nb++, mask >>= 1) {
3147 /* leading bit found.
3150 /* determine the l2 value.
3152 l2nb = (64 - 1) - l2nb;
3154 /* check if we need to round up.
3163 return 0; /* fix compiler warning */
3168 * NAME: dbAllocBottomUp()
3170 * FUNCTION: alloc the specified block range from the working block
3173 * the blocks will be alloc from the working map one dmap
3177 * ip - pointer to in-core inode;
3178 * blkno - starting block number to be freed.
3179 * nblocks - number of blocks to be freed.
3185 int dbAllocBottomUp(struct inode *ip, s64 blkno, s64 nblocks)
3187 struct metapage *mp;
3191 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
3192 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
3194 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
3196 /* block to be allocated better be within the mapsize. */
3197 ASSERT(nblocks <= bmp->db_mapsize - blkno);
3200 * allocate the blocks a dmap at a time.
3203 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
3204 /* release previous dmap if any */
3209 /* get the buffer for the current dmap. */
3210 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
3211 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
3213 IREAD_UNLOCK(ipbmap);
3216 dp = (struct dmap *) mp->data;
3218 /* determine the number of blocks to be allocated from
3221 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
3223 /* allocate the blocks. */
3224 if ((rc = dbAllocDmapBU(bmp, dp, blkno, nb))) {
3225 release_metapage(mp);
3226 IREAD_UNLOCK(ipbmap);
3231 /* write the last buffer. */
3234 IREAD_UNLOCK(ipbmap);
3240 static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
3244 int dbitno, word, rembits, nb, nwords, wbitno, agno;
3246 struct dmaptree *tp = (struct dmaptree *) & dp->tree;
3248 /* save the current value of the root (i.e. maximum free string)
3251 oldroot = tp->stree[ROOT];
3253 /* determine the bit number and word within the dmap of the
3256 dbitno = blkno & (BPERDMAP - 1);
3257 word = dbitno >> L2DBWORD;
3259 /* block range better be within the dmap */
3260 assert(dbitno + nblocks <= BPERDMAP);
3262 /* allocate the bits of the dmap's words corresponding to the block
3263 * range. not all bits of the first and last words may be contained
3264 * within the block range. if this is the case, we'll work against
3265 * those words (i.e. partial first and/or last) on an individual basis
3266 * (a single pass), allocating the bits of interest by hand and
3267 * updating the leaf corresponding to the dmap word. a single pass
3268 * will be used for all dmap words fully contained within the
3269 * specified range. within this pass, the bits of all fully contained
3270 * dmap words will be marked as free in a single shot and the leaves
3271 * will be updated. a single leaf may describe the free space of
3272 * multiple dmap words, so we may update only a subset of the actual
3273 * leaves corresponding to the dmap words of the block range.
3275 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
3276 /* determine the bit number within the word and
3277 * the number of bits within the word.
3279 wbitno = dbitno & (DBWORD - 1);
3280 nb = min(rembits, DBWORD - wbitno);
3282 /* check if only part of a word is to be allocated.
3285 /* allocate (set to 1) the appropriate bits within
3288 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
3293 /* one or more dmap words are fully contained
3294 * within the block range. determine how many
3295 * words and allocate (set to 1) the bits of these
3298 nwords = rembits >> L2DBWORD;
3299 memset(&dp->wmap[word], (int) ONES, nwords * 4);
3301 /* determine how many bits */
3302 nb = nwords << L2DBWORD;
3307 /* update the free count for this dmap */
3308 le32_add_cpu(&dp->nfree, -nblocks);
3310 /* reconstruct summary tree */
3315 /* if this allocation group is completely free,
3316 * update the highest active allocation group number
3317 * if this allocation group is the new max.
3319 agno = blkno >> bmp->db_agl2size;
3320 if (agno > bmp->db_maxag)
3321 bmp->db_maxag = agno;
3323 /* update the free count for the allocation group and map */
3324 bmp->db_agfree[agno] -= nblocks;
3325 bmp->db_nfree -= nblocks;
3329 /* if the root has not changed, done. */
3330 if (tp->stree[ROOT] == oldroot)
3333 /* root changed. bubble the change up to the dmap control pages.
3334 * if the adjustment of the upper level control pages fails,
3335 * backout the bit allocation (thus making everything consistent).
3337 if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0)))
3338 dbFreeBits(bmp, dp, blkno, nblocks);
3345 * NAME: dbExtendFS()
3347 * FUNCTION: extend bmap from blkno for nblocks;
3348 * dbExtendFS() updates bmap ready for dbAllocBottomUp();
3352 * L1---------------------------------L1
3354 * L0---------L0---------L0 L0---------L0---------L0
3356 * d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,.,dm;
3357 * L2L1L0d0,...,dnL0d0,...,dnL0d0,...,dnL1L0d0,...,dnL0d0,...,dnL0d0,..dm
3359 * <---old---><----------------------------extend----------------------->
3361 int dbExtendFS(struct inode *ipbmap, s64 blkno, s64 nblocks)
3363 struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb);
3364 int nbperpage = sbi->nbperpage;
3365 int i, i0 = true, j, j0 = true, k, n;
3368 struct metapage *mp, *l2mp, *l1mp = NULL, *l0mp = NULL;
3369 struct dmapctl *l2dcp, *l1dcp, *l0dcp;
3371 s8 *l0leaf, *l1leaf, *l2leaf;
3372 struct bmap *bmp = sbi->bmap;
3373 int agno, l2agsize, oldl2agsize;
3376 newsize = blkno + nblocks;
3378 jfs_info("dbExtendFS: blkno:%Ld nblocks:%Ld newsize:%Ld",
3379 (long long) blkno, (long long) nblocks, (long long) newsize);
3382 * initialize bmap control page.
3384 * all the data in bmap control page should exclude
3385 * the mkfs hidden dmap page.
3388 /* update mapsize */
3389 bmp->db_mapsize = newsize;
3390 bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize);
3392 /* compute new AG size */
3393 l2agsize = dbGetL2AGSize(newsize);
3394 oldl2agsize = bmp->db_agl2size;
3396 bmp->db_agl2size = l2agsize;
3397 bmp->db_agsize = 1 << l2agsize;
3399 /* compute new number of AG */
3400 agno = bmp->db_numag;
3401 bmp->db_numag = newsize >> l2agsize;
3402 bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0;
3405 * reconfigure db_agfree[]
3406 * from old AG configuration to new AG configuration;
3408 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
3409 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
3410 * note: new AG size = old AG size * (2**x).
3412 if (l2agsize == oldl2agsize)
3414 k = 1 << (l2agsize - oldl2agsize);
3415 ag_rem = bmp->db_agfree[0]; /* save agfree[0] */
3416 for (i = 0, n = 0; i < agno; n++) {
3417 bmp->db_agfree[n] = 0; /* init collection point */
3419 /* coalesce contiguous k AGs; */
3420 for (j = 0; j < k && i < agno; j++, i++) {
3421 /* merge AGi to AGn */
3422 bmp->db_agfree[n] += bmp->db_agfree[i];
3425 bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */
3427 for (; n < MAXAG; n++)
3428 bmp->db_agfree[n] = 0;
3431 * update highest active ag number
3434 bmp->db_maxag = bmp->db_maxag / k;
3439 * update bit maps and corresponding level control pages;
3440 * global control page db_nfree, db_agfree[agno], db_maxfreebud;
3444 p = BMAPBLKNO + nbperpage; /* L2 page */
3445 l2mp = read_metapage(ipbmap, p, PSIZE, 0);
3447 jfs_error(ipbmap->i_sb, "L2 page could not be read\n");
3450 l2dcp = (struct dmapctl *) l2mp->data;
3452 /* compute start L1 */
3453 k = blkno >> L2MAXL1SIZE;
3454 l2leaf = l2dcp->stree + CTLLEAFIND + k;
3455 p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */
3458 * extend each L1 in L2
3460 for (; k < LPERCTL; k++, p += nbperpage) {
3463 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */
3464 l1mp = read_metapage(ipbmap, p, PSIZE, 0);
3467 l1dcp = (struct dmapctl *) l1mp->data;
3469 /* compute start L0 */
3470 j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE;
3471 l1leaf = l1dcp->stree + CTLLEAFIND + j;
3472 p = BLKTOL0(blkno, sbi->l2nbperpage);
3475 /* assign/init L1 page */
3476 l1mp = get_metapage(ipbmap, p, PSIZE, 0);
3480 l1dcp = (struct dmapctl *) l1mp->data;
3482 /* compute start L0 */
3484 l1leaf = l1dcp->stree + CTLLEAFIND;
3485 p += nbperpage; /* 1st L0 of L1.k */
3489 * extend each L0 in L1
3491 for (; j < LPERCTL; j++) {
3494 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */
3496 l0mp = read_metapage(ipbmap, p, PSIZE, 0);
3499 l0dcp = (struct dmapctl *) l0mp->data;
3501 /* compute start dmap */
3502 i = (blkno & (MAXL0SIZE - 1)) >>
3504 l0leaf = l0dcp->stree + CTLLEAFIND + i;
3505 p = BLKTODMAP(blkno,
3509 /* assign/init L0 page */
3510 l0mp = get_metapage(ipbmap, p, PSIZE, 0);
3514 l0dcp = (struct dmapctl *) l0mp->data;
3516 /* compute start dmap */
3518 l0leaf = l0dcp->stree + CTLLEAFIND;
3519 p += nbperpage; /* 1st dmap of L0.j */
3523 * extend each dmap in L0
3525 for (; i < LPERCTL; i++) {
3527 * reconstruct the dmap page, and
3528 * initialize corresponding parent L0 leaf
3530 if ((n = blkno & (BPERDMAP - 1))) {
3531 /* read in dmap page: */
3532 mp = read_metapage(ipbmap, p,
3536 n = min(nblocks, (s64)BPERDMAP - n);
3538 /* assign/init dmap page */
3539 mp = read_metapage(ipbmap, p,
3544 n = min_t(s64, nblocks, BPERDMAP);
3547 dp = (struct dmap *) mp->data;
3548 *l0leaf = dbInitDmap(dp, blkno, n);
3551 agno = le64_to_cpu(dp->start) >> l2agsize;
3552 bmp->db_agfree[agno] += n;
3563 } /* for each dmap in a L0 */
3566 * build current L0 page from its leaves, and
3567 * initialize corresponding parent L1 leaf
3569 *l1leaf = dbInitDmapCtl(l0dcp, 0, ++i);
3570 write_metapage(l0mp);
3574 l1leaf++; /* continue for next L0 */
3576 /* more than 1 L0 ? */
3578 break; /* build L1 page */
3580 /* summarize in global bmap page */
3581 bmp->db_maxfreebud = *l1leaf;
3582 release_metapage(l1mp);
3583 release_metapage(l2mp);
3587 } /* for each L0 in a L1 */
3590 * build current L1 page from its leaves, and
3591 * initialize corresponding parent L2 leaf
3593 *l2leaf = dbInitDmapCtl(l1dcp, 1, ++j);
3594 write_metapage(l1mp);
3598 l2leaf++; /* continue for next L1 */
3600 /* more than 1 L1 ? */
3602 break; /* build L2 page */
3604 /* summarize in global bmap page */
3605 bmp->db_maxfreebud = *l2leaf;
3606 release_metapage(l2mp);
3610 } /* for each L1 in a L2 */
3612 jfs_error(ipbmap->i_sb, "function has not returned as expected\n");
3615 release_metapage(l0mp);
3617 release_metapage(l1mp);
3618 release_metapage(l2mp);
3622 * finalize bmap control page
3633 void dbFinalizeBmap(struct inode *ipbmap)
3635 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
3636 int actags, inactags, l2nl;
3637 s64 ag_rem, actfree, inactfree, avgfree;
3641 * finalize bmap control page
3645 * compute db_agpref: preferred ag to allocate from
3646 * (the leftmost ag with average free space in it);
3649 /* get the number of active ags and inactive ags */
3650 actags = bmp->db_maxag + 1;
3651 inactags = bmp->db_numag - actags;
3652 ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */
3654 /* determine how many blocks are in the inactive allocation
3655 * groups. in doing this, we must account for the fact that
3656 * the rightmost group might be a partial group (i.e. file
3657 * system size is not a multiple of the group size).
3659 inactfree = (inactags && ag_rem) ?
3660 ((inactags - 1) << bmp->db_agl2size) + ag_rem
3661 : inactags << bmp->db_agl2size;
3663 /* determine how many free blocks are in the active
3664 * allocation groups plus the average number of free blocks
3665 * within the active ags.
3667 actfree = bmp->db_nfree - inactfree;
3668 avgfree = (u32) actfree / (u32) actags;
3670 /* if the preferred allocation group has not average free space.
3671 * re-establish the preferred group as the leftmost
3672 * group with average free space.
3674 if (bmp->db_agfree[bmp->db_agpref] < avgfree) {
3675 for (bmp->db_agpref = 0; bmp->db_agpref < actags;
3677 if (bmp->db_agfree[bmp->db_agpref] >= avgfree)
3680 if (bmp->db_agpref >= bmp->db_numag) {
3681 jfs_error(ipbmap->i_sb,
3682 "cannot find ag with average freespace\n");
3687 * compute db_aglevel, db_agheight, db_width, db_agstart:
3688 * an ag is covered in aglevel dmapctl summary tree,
3689 * at agheight level height (from leaf) with agwidth number of nodes
3690 * each, which starts at agstart index node of the smmary tree node
3693 bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize);
3695 bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL);
3696 bmp->db_agheight = l2nl >> 1;
3697 bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1));
3698 for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0;
3700 bmp->db_agstart += n;
3708 * NAME: dbInitDmap()/ujfs_idmap_page()
3710 * FUNCTION: initialize working/persistent bitmap of the dmap page
3711 * for the specified number of blocks:
3713 * at entry, the bitmaps had been initialized as free (ZEROS);
3714 * The number of blocks will only account for the actually
3715 * existing blocks. Blocks which don't actually exist in
3716 * the aggregate will be marked as allocated (ONES);
3719 * dp - pointer to page of map
3720 * nblocks - number of blocks this page
3724 static int dbInitDmap(struct dmap * dp, s64 Blkno, int nblocks)
3726 int blkno, w, b, r, nw, nb, i;
3728 /* starting block number within the dmap */
3729 blkno = Blkno & (BPERDMAP - 1);
3732 dp->nblocks = dp->nfree = cpu_to_le32(nblocks);
3733 dp->start = cpu_to_le64(Blkno);
3735 if (nblocks == BPERDMAP) {
3736 memset(&dp->wmap[0], 0, LPERDMAP * 4);
3737 memset(&dp->pmap[0], 0, LPERDMAP * 4);
3741 le32_add_cpu(&dp->nblocks, nblocks);
3742 le32_add_cpu(&dp->nfree, nblocks);
3745 /* word number containing start block number */
3746 w = blkno >> L2DBWORD;
3749 * free the bits corresponding to the block range (ZEROS):
3750 * note: not all bits of the first and last words may be contained
3751 * within the block range.
3753 for (r = nblocks; r > 0; r -= nb, blkno += nb) {
3754 /* number of bits preceding range to be freed in the word */
3755 b = blkno & (DBWORD - 1);
3756 /* number of bits to free in the word */
3757 nb = min(r, DBWORD - b);
3759 /* is partial word to be freed ? */
3761 /* free (set to 0) from the bitmap word */
3762 dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3764 dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3767 /* skip the word freed */
3770 /* free (set to 0) contiguous bitmap words */
3772 memset(&dp->wmap[w], 0, nw * 4);
3773 memset(&dp->pmap[w], 0, nw * 4);
3775 /* skip the words freed */
3776 nb = nw << L2DBWORD;
3782 * mark bits following the range to be freed (non-existing
3783 * blocks) as allocated (ONES)
3786 if (blkno == BPERDMAP)
3789 /* the first word beyond the end of existing blocks */
3790 w = blkno >> L2DBWORD;
3792 /* does nblocks fall on a 32-bit boundary ? */
3793 b = blkno & (DBWORD - 1);
3795 /* mark a partial word allocated */
3796 dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b);
3800 /* set the rest of the words in the page to allocated (ONES) */
3801 for (i = w; i < LPERDMAP; i++)
3802 dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES);
3808 return (dbInitDmapTree(dp));
3813 * NAME: dbInitDmapTree()/ujfs_complete_dmap()
3815 * FUNCTION: initialize summary tree of the specified dmap:
3817 * at entry, bitmap of the dmap has been initialized;
3820 * dp - dmap to complete
3821 * blkno - starting block number for this dmap
3822 * treemax - will be filled in with max free for this dmap
3824 * RETURNS: max free string at the root of the tree
3826 static int dbInitDmapTree(struct dmap * dp)
3828 struct dmaptree *tp;
3832 /* init fixed info of tree */
3834 tp->nleafs = cpu_to_le32(LPERDMAP);
3835 tp->l2nleafs = cpu_to_le32(L2LPERDMAP);
3836 tp->leafidx = cpu_to_le32(LEAFIND);
3837 tp->height = cpu_to_le32(4);
3838 tp->budmin = BUDMIN;
3840 /* init each leaf from corresponding wmap word:
3841 * note: leaf is set to NOFREE(-1) if all blocks of corresponding
3842 * bitmap word are allocated.
3844 cp = tp->stree + le32_to_cpu(tp->leafidx);
3845 for (i = 0; i < LPERDMAP; i++)
3846 *cp++ = dbMaxBud((u8 *) & dp->wmap[i]);
3848 /* build the dmap's binary buddy summary tree */
3849 return (dbInitTree(tp));
3854 * NAME: dbInitTree()/ujfs_adjtree()
3856 * FUNCTION: initialize binary buddy summary tree of a dmap or dmapctl.
3858 * at entry, the leaves of the tree has been initialized
3859 * from corresponding bitmap word or root of summary tree
3860 * of the child control page;
3861 * configure binary buddy system at the leaf level, then
3862 * bubble up the values of the leaf nodes up the tree.
3865 * cp - Pointer to the root of the tree
3866 * l2leaves- Number of leaf nodes as a power of 2
3867 * l2min - Number of blocks that can be covered by a leaf
3870 * RETURNS: max free string at the root of the tree
3872 static int dbInitTree(struct dmaptree * dtp)
3874 int l2max, l2free, bsize, nextb, i;
3875 int child, parent, nparent;
3880 /* Determine the maximum free string possible for the leaves */
3881 l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin;
3884 * configure the leaf level into binary buddy system
3886 * Try to combine buddies starting with a buddy size of 1
3887 * (i.e. two leaves). At a buddy size of 1 two buddy leaves
3888 * can be combined if both buddies have a maximum free of l2min;
3889 * the combination will result in the left-most buddy leaf having
3890 * a maximum free of l2min+1.
3891 * After processing all buddies for a given size, process buddies
3892 * at the next higher buddy size (i.e. current size * 2) and
3893 * the next maximum free (current free + 1).
3894 * This continues until the maximum possible buddy combination
3895 * yields maximum free.
3897 for (l2free = dtp->budmin, bsize = 1; l2free < l2max;
3898 l2free++, bsize = nextb) {
3899 /* get next buddy size == current buddy pair size */
3902 /* scan each adjacent buddy pair at current buddy size */
3903 for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx);
3904 i < le32_to_cpu(dtp->nleafs);
3905 i += nextb, cp += nextb) {
3906 /* coalesce if both adjacent buddies are max free */
3907 if (*cp == l2free && *(cp + bsize) == l2free) {
3908 *cp = l2free + 1; /* left take right */
3909 *(cp + bsize) = -1; /* right give left */
3915 * bubble summary information of leaves up the tree.
3917 * Starting at the leaf node level, the four nodes described by
3918 * the higher level parent node are compared for a maximum free and
3919 * this maximum becomes the value of the parent node.
3920 * when all lower level nodes are processed in this fashion then
3921 * move up to the next level (parent becomes a lower level node) and
3922 * continue the process for that level.
3924 for (child = le32_to_cpu(dtp->leafidx),
3925 nparent = le32_to_cpu(dtp->nleafs) >> 2;
3926 nparent > 0; nparent >>= 2, child = parent) {
3927 /* get index of 1st node of parent level */
3928 parent = (child - 1) >> 2;
3930 /* set the value of the parent node as the maximum
3931 * of the four nodes of the current level.
3933 for (i = 0, cp = tp + child, cp1 = tp + parent;
3934 i < nparent; i++, cp += 4, cp1++)
3945 * function: initialize dmapctl page
3947 static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i)
3948 { /* start leaf index not covered by range */
3951 dcp->nleafs = cpu_to_le32(LPERCTL);
3952 dcp->l2nleafs = cpu_to_le32(L2LPERCTL);
3953 dcp->leafidx = cpu_to_le32(CTLLEAFIND);
3954 dcp->height = cpu_to_le32(5);
3955 dcp->budmin = L2BPERDMAP + L2LPERCTL * level;
3958 * initialize the leaves of current level that were not covered
3959 * by the specified input block range (i.e. the leaves have no
3960 * low level dmapctl or dmap).
3962 cp = &dcp->stree[CTLLEAFIND + i];
3963 for (; i < LPERCTL; i++)
3966 /* build the dmap's binary buddy summary tree */
3967 return (dbInitTree((struct dmaptree *) dcp));
3972 * NAME: dbGetL2AGSize()/ujfs_getagl2size()
3974 * FUNCTION: Determine log2(allocation group size) from aggregate size
3977 * nblocks - Number of blocks in aggregate
3979 * RETURNS: log2(allocation group size) in aggregate blocks
3981 static int dbGetL2AGSize(s64 nblocks)
3987 if (nblocks < BPERDMAP * MAXAG)
3988 return (L2BPERDMAP);
3990 /* round up aggregate size to power of 2 */
3991 m = ((u64) 1 << (64 - 1));
3992 for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) {
3997 sz = (s64) 1 << l2sz;
4001 /* agsize = roundupSize/max_number_of_ag */
4002 return (l2sz - L2MAXAG);
4007 * NAME: dbMapFileSizeToMapSize()
4009 * FUNCTION: compute number of blocks the block allocation map file
4010 * can cover from the map file size;
4012 * RETURNS: Number of blocks which can be covered by this block map file;
4016 * maximum number of map pages at each level including control pages
4018 #define MAXL0PAGES (1 + LPERCTL)
4019 #define MAXL1PAGES (1 + LPERCTL * MAXL0PAGES)
4022 * convert number of map pages to the zero origin top dmapctl level
4024 #define BMAPPGTOLEV(npages) \
4025 (((npages) <= 3 + MAXL0PAGES) ? 0 : \
4026 ((npages) <= 2 + MAXL1PAGES) ? 1 : 2)
4028 s64 dbMapFileSizeToMapSize(struct inode * ipbmap)
4030 struct super_block *sb = ipbmap->i_sb;
4034 int complete, factor;
4036 nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize;
4037 npages = nblocks >> JFS_SBI(sb)->l2nbperpage;
4038 level = BMAPPGTOLEV(npages);
4040 /* At each level, accumulate the number of dmap pages covered by
4041 * the number of full child levels below it;
4042 * repeat for the last incomplete child level.
4045 npages--; /* skip the first global control page */
4046 /* skip higher level control pages above top level covered by map */
4047 npages -= (2 - level);
4048 npages--; /* skip top level's control page */
4049 for (i = level; i >= 0; i--) {
4051 (i == 2) ? MAXL1PAGES : ((i == 1) ? MAXL0PAGES : 1);
4052 complete = (u32) npages / factor;
4053 ndmaps += complete * ((i == 2) ? LPERCTL * LPERCTL :
4054 ((i == 1) ? LPERCTL : 1));
4056 /* pages in last/incomplete child */
4057 npages = (u32) npages % factor;
4058 /* skip incomplete child's level control page */
4062 /* convert the number of dmaps into the number of blocks
4063 * which can be covered by the dmaps;
4065 nblocks = ndmaps << L2BPERDMAP;