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);
67 static int dbBackSplit(dmtree_t * tp, int leafno);
68 static int dbJoin(dmtree_t * tp, int leafno, int newval);
69 static void dbAdjTree(dmtree_t * tp, int leafno, int newval);
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);
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);
181 bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
182 bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
183 if (!bmp->db_numag) {
184 release_metapage(mp);
189 bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel);
190 bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag);
191 bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref);
192 bmp->db_aglevel = le32_to_cpu(dbmp_le->dn_aglevel);
193 bmp->db_agheight = le32_to_cpu(dbmp_le->dn_agheight);
194 bmp->db_agwidth = le32_to_cpu(dbmp_le->dn_agwidth);
195 bmp->db_agstart = le32_to_cpu(dbmp_le->dn_agstart);
196 bmp->db_agl2size = le32_to_cpu(dbmp_le->dn_agl2size);
197 for (i = 0; i < MAXAG; i++)
198 bmp->db_agfree[i] = le64_to_cpu(dbmp_le->dn_agfree[i]);
199 bmp->db_agsize = le64_to_cpu(dbmp_le->dn_agsize);
200 bmp->db_maxfreebud = dbmp_le->dn_maxfreebud;
202 /* release the buffer. */
203 release_metapage(mp);
205 /* bind the bmap inode and the bmap descriptor to each other. */
206 bmp->db_ipbmap = ipbmap;
207 JFS_SBI(ipbmap->i_sb)->bmap = bmp;
209 memset(bmp->db_active, 0, sizeof(bmp->db_active));
212 * allocate/initialize the bmap lock
223 * FUNCTION: terminate the block allocation map in preparation for
224 * file system unmount.
226 * the in-core bmap descriptor is written to disk and
227 * the memory for this descriptor is freed.
230 * ipbmap - pointer to in-core inode for the block map.
236 int dbUnmount(struct inode *ipbmap, int mounterror)
238 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
240 if (!(mounterror || isReadOnly(ipbmap)))
244 * Invalidate the page cache buffers
246 truncate_inode_pages(ipbmap->i_mapping, 0);
248 /* free the memory for the in-memory bmap. */
257 int dbSync(struct inode *ipbmap)
259 struct dbmap_disk *dbmp_le;
260 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
265 * write bmap global control page
267 /* get the buffer for the on-disk bmap descriptor. */
268 mp = read_metapage(ipbmap,
269 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
272 jfs_err("dbSync: read_metapage failed!");
275 /* copy the in-memory version of the bmap to the on-disk version */
276 dbmp_le = (struct dbmap_disk *) mp->data;
277 dbmp_le->dn_mapsize = cpu_to_le64(bmp->db_mapsize);
278 dbmp_le->dn_nfree = cpu_to_le64(bmp->db_nfree);
279 dbmp_le->dn_l2nbperpage = cpu_to_le32(bmp->db_l2nbperpage);
280 dbmp_le->dn_numag = cpu_to_le32(bmp->db_numag);
281 dbmp_le->dn_maxlevel = cpu_to_le32(bmp->db_maxlevel);
282 dbmp_le->dn_maxag = cpu_to_le32(bmp->db_maxag);
283 dbmp_le->dn_agpref = cpu_to_le32(bmp->db_agpref);
284 dbmp_le->dn_aglevel = cpu_to_le32(bmp->db_aglevel);
285 dbmp_le->dn_agheight = cpu_to_le32(bmp->db_agheight);
286 dbmp_le->dn_agwidth = cpu_to_le32(bmp->db_agwidth);
287 dbmp_le->dn_agstart = cpu_to_le32(bmp->db_agstart);
288 dbmp_le->dn_agl2size = cpu_to_le32(bmp->db_agl2size);
289 for (i = 0; i < MAXAG; i++)
290 dbmp_le->dn_agfree[i] = cpu_to_le64(bmp->db_agfree[i]);
291 dbmp_le->dn_agsize = cpu_to_le64(bmp->db_agsize);
292 dbmp_le->dn_maxfreebud = bmp->db_maxfreebud;
294 /* write the buffer */
298 * write out dirty pages of bmap
300 filemap_write_and_wait(ipbmap->i_mapping);
302 diWriteSpecial(ipbmap, 0);
310 * FUNCTION: free the specified block range from the working block
313 * the blocks will be free from the working map one dmap
317 * ip - pointer to in-core inode;
318 * blkno - starting block number to be freed.
319 * nblocks - number of blocks to be freed.
325 int dbFree(struct inode *ip, s64 blkno, s64 nblocks)
331 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
332 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
333 struct super_block *sb = ipbmap->i_sb;
335 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
337 /* block to be freed better be within the mapsize. */
338 if (unlikely((blkno == 0) || (blkno + nblocks > bmp->db_mapsize))) {
339 IREAD_UNLOCK(ipbmap);
340 printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
341 (unsigned long long) blkno,
342 (unsigned long long) nblocks);
343 jfs_error(ip->i_sb, "block to be freed is outside the map\n");
348 * TRIM the blocks, when mounted with discard option
350 if (JFS_SBI(sb)->flag & JFS_DISCARD)
351 if (JFS_SBI(sb)->minblks_trim <= nblocks)
352 jfs_issue_discard(ipbmap, blkno, nblocks);
355 * free the blocks a dmap at a time.
358 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
359 /* release previous dmap if any */
364 /* get the buffer for the current dmap. */
365 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
366 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
368 IREAD_UNLOCK(ipbmap);
371 dp = (struct dmap *) mp->data;
373 /* determine the number of blocks to be freed from
376 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
378 /* free the blocks. */
379 if ((rc = dbFreeDmap(bmp, dp, blkno, nb))) {
380 jfs_error(ip->i_sb, "error in block map\n");
381 release_metapage(mp);
382 IREAD_UNLOCK(ipbmap);
387 /* write the last buffer. */
391 IREAD_UNLOCK(ipbmap);
398 * NAME: dbUpdatePMap()
400 * FUNCTION: update the allocation state (free or allocate) of the
401 * specified block range in the persistent block allocation map.
403 * the blocks will be updated in the persistent map one
407 * ipbmap - pointer to in-core inode for the block map.
408 * free - 'true' if block range is to be freed from the persistent
409 * map; 'false' if it is to be allocated.
410 * blkno - starting block number of the range.
411 * nblocks - number of contiguous blocks in the range.
412 * tblk - transaction block;
419 dbUpdatePMap(struct inode *ipbmap,
420 int free, s64 blkno, s64 nblocks, struct tblock * tblk)
422 int nblks, dbitno, wbitno, rbits;
423 int word, nbits, nwords;
424 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
425 s64 lblkno, rem, lastlblkno;
430 int lsn, difft, diffp;
433 /* the blocks better be within the mapsize. */
434 if (blkno + nblocks > bmp->db_mapsize) {
435 printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
436 (unsigned long long) blkno,
437 (unsigned long long) nblocks);
438 jfs_error(ipbmap->i_sb, "blocks are outside the map\n");
442 /* compute delta of transaction lsn from log syncpt */
444 log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
445 logdiff(difft, lsn, log);
448 * update the block state a dmap at a time.
452 for (rem = nblocks; rem > 0; rem -= nblks, blkno += nblks) {
453 /* get the buffer for the current dmap. */
454 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
455 if (lblkno != lastlblkno) {
460 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE,
464 metapage_wait_for_io(mp);
466 dp = (struct dmap *) mp->data;
468 /* determine the bit number and word within the dmap of
469 * the starting block. also determine how many blocks
470 * are to be updated within this dmap.
472 dbitno = blkno & (BPERDMAP - 1);
473 word = dbitno >> L2DBWORD;
474 nblks = min(rem, (s64)BPERDMAP - dbitno);
476 /* update the bits of the dmap words. the first and last
477 * words may only have a subset of their bits updated. if
478 * this is the case, we'll work against that word (i.e.
479 * partial first and/or last) only in a single pass. a
480 * single pass will also be used to update all words that
481 * are to have all their bits updated.
483 for (rbits = nblks; rbits > 0;
484 rbits -= nbits, dbitno += nbits) {
485 /* determine the bit number within the word and
486 * the number of bits within the word.
488 wbitno = dbitno & (DBWORD - 1);
489 nbits = min(rbits, DBWORD - wbitno);
491 /* check if only part of the word is to be updated. */
492 if (nbits < DBWORD) {
493 /* update (free or allocate) the bits
497 (ONES << (DBWORD - nbits) >> wbitno);
507 /* one or more words are to have all
508 * their bits updated. determine how
509 * many words and how many bits.
511 nwords = rbits >> L2DBWORD;
512 nbits = nwords << L2DBWORD;
514 /* update (free or allocate) the bits
518 memset(&dp->pmap[word], 0,
521 memset(&dp->pmap[word], (int) ONES,
531 if (lblkno == lastlblkno)
536 LOGSYNC_LOCK(log, flags);
538 /* inherit older/smaller lsn */
539 logdiff(diffp, mp->lsn, log);
543 /* move bp after tblock in logsync list */
544 list_move(&mp->synclist, &tblk->synclist);
547 /* inherit younger/larger clsn */
548 logdiff(difft, tblk->clsn, log);
549 logdiff(diffp, mp->clsn, log);
551 mp->clsn = tblk->clsn;
556 /* insert bp after tblock in logsync list */
558 list_add(&mp->synclist, &tblk->synclist);
560 mp->clsn = tblk->clsn;
562 LOGSYNC_UNLOCK(log, flags);
565 /* write the last buffer. */
577 * FUNCTION: find the preferred allocation group for new allocations.
579 * Within the allocation groups, we maintain a preferred
580 * allocation group which consists of a group with at least
581 * average free space. It is the preferred group that we target
582 * new inode allocation towards. The tie-in between inode
583 * allocation and block allocation occurs as we allocate the
584 * first (data) block of an inode and specify the inode (block)
585 * as the allocation hint for this block.
587 * We try to avoid having more than one open file growing in
588 * an allocation group, as this will lead to fragmentation.
589 * This differs from the old OS/2 method of trying to keep
590 * empty ags around for large allocations.
593 * ipbmap - pointer to in-core inode for the block map.
596 * the preferred allocation group number.
598 int dbNextAG(struct inode *ipbmap)
605 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
609 /* determine the average number of free blocks within the ags. */
610 avgfree = (u32)bmp->db_nfree / bmp->db_numag;
613 * if the current preferred ag does not have an active allocator
614 * and has at least average freespace, return it
616 agpref = bmp->db_agpref;
617 if ((atomic_read(&bmp->db_active[agpref]) == 0) &&
618 (bmp->db_agfree[agpref] >= avgfree))
621 /* From the last preferred ag, find the next one with at least
622 * average free space.
624 for (i = 0 ; i < bmp->db_numag; i++, agpref++) {
625 if (agpref == bmp->db_numag)
628 if (atomic_read(&bmp->db_active[agpref]))
629 /* open file is currently growing in this ag */
631 if (bmp->db_agfree[agpref] >= avgfree) {
632 /* Return this one */
633 bmp->db_agpref = agpref;
635 } else if (bmp->db_agfree[agpref] > hwm) {
636 /* Less than avg. freespace, but best so far */
637 hwm = bmp->db_agfree[agpref];
643 * If no inactive ag was found with average freespace, use the
647 bmp->db_agpref = next_best;
648 /* else leave db_agpref unchanged */
652 /* return the preferred group.
654 return (bmp->db_agpref);
660 * FUNCTION: attempt to allocate a specified number of contiguous free
661 * blocks from the working allocation block map.
663 * the block allocation policy uses hints and a multi-step
666 * for allocation requests smaller than the number of blocks
667 * per dmap, we first try to allocate the new blocks
668 * immediately following the hint. if these blocks are not
669 * available, we try to allocate blocks near the hint. if
670 * no blocks near the hint are available, we next try to
671 * allocate within the same dmap as contains the hint.
673 * if no blocks are available in the dmap or the allocation
674 * request is larger than the dmap size, we try to allocate
675 * within the same allocation group as contains the hint. if
676 * this does not succeed, we finally try to allocate anywhere
677 * within the aggregate.
679 * we also try to allocate anywhere within the aggregate for
680 * for allocation requests larger than the allocation group
681 * size or requests that specify no hint value.
684 * ip - pointer to in-core inode;
685 * hint - allocation hint.
686 * nblocks - number of contiguous blocks in the range.
687 * results - on successful return, set to the starting block number
688 * of the newly allocated contiguous range.
692 * -ENOSPC - insufficient disk resources
695 int dbAlloc(struct inode *ip, s64 hint, s64 nblocks, s64 * results)
698 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
707 /* assert that nblocks is valid */
710 /* get the log2 number of blocks to be allocated.
711 * if the number of blocks is not a log2 multiple,
712 * it will be rounded up to the next log2 multiple.
714 l2nb = BLKSTOL2(nblocks);
716 bmp = JFS_SBI(ip->i_sb)->bmap;
718 mapSize = bmp->db_mapsize;
720 /* the hint should be within the map */
721 if (hint >= mapSize) {
722 jfs_error(ip->i_sb, "the hint is outside the map\n");
726 /* if the number of blocks to be allocated is greater than the
727 * allocation group size, try to allocate anywhere.
729 if (l2nb > bmp->db_agl2size) {
730 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
732 rc = dbAllocAny(bmp, nblocks, l2nb, results);
738 * If no hint, let dbNextAG recommend an allocation group
743 /* we would like to allocate close to the hint. adjust the
744 * hint to the block following the hint since the allocators
745 * will start looking for free space starting at this point.
749 if (blkno >= bmp->db_mapsize)
752 agno = blkno >> bmp->db_agl2size;
754 /* check if blkno crosses over into a new allocation group.
755 * if so, check if we should allow allocations within this
758 if ((blkno & (bmp->db_agsize - 1)) == 0)
759 /* check if the AG is currently being written to.
760 * if so, call dbNextAG() to find a non-busy
761 * AG with sufficient free space.
763 if (atomic_read(&bmp->db_active[agno]))
766 /* check if the allocation request size can be satisfied from a
767 * single dmap. if so, try to allocate from the dmap containing
768 * the hint using a tiered strategy.
770 if (nblocks <= BPERDMAP) {
771 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
773 /* get the buffer for the dmap containing the hint.
776 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
777 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
781 dp = (struct dmap *) mp->data;
783 /* first, try to satisfy the allocation request with the
784 * blocks beginning at the hint.
786 if ((rc = dbAllocNext(bmp, dp, blkno, (int) nblocks))
790 mark_metapage_dirty(mp);
793 release_metapage(mp);
797 writers = atomic_read(&bmp->db_active[agno]);
799 ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) {
801 * Someone else is writing in this allocation
802 * group. To avoid fragmenting, try another ag
804 release_metapage(mp);
805 IREAD_UNLOCK(ipbmap);
809 /* next, try to satisfy the allocation request with blocks
813 dbAllocNear(bmp, dp, blkno, (int) nblocks, l2nb, results))
816 mark_metapage_dirty(mp);
818 release_metapage(mp);
822 /* try to satisfy the allocation request with blocks within
823 * the same dmap as the hint.
825 if ((rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results))
828 mark_metapage_dirty(mp);
830 release_metapage(mp);
834 release_metapage(mp);
835 IREAD_UNLOCK(ipbmap);
838 /* try to satisfy the allocation request with blocks within
839 * the same allocation group as the hint.
841 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
842 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC)
845 IWRITE_UNLOCK(ipbmap);
850 * Let dbNextAG recommend a preferred allocation group
852 agno = dbNextAG(ipbmap);
853 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
855 /* Try to allocate within this allocation group. if that fails, try to
856 * allocate anywhere in the map.
858 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC)
859 rc = dbAllocAny(bmp, nblocks, l2nb, results);
862 IWRITE_UNLOCK(ipbmap);
867 IREAD_UNLOCK(ipbmap);
874 * NAME: dbAllocExact()
876 * FUNCTION: try to allocate the requested extent;
879 * ip - pointer to in-core inode;
880 * blkno - extent address;
881 * nblocks - extent length;
885 * -ENOSPC - insufficient disk resources
888 int dbAllocExact(struct inode *ip, s64 blkno, int nblocks)
891 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
892 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
897 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
900 * validate extent request:
902 * note: defragfs policy:
903 * max 64 blocks will be moved.
904 * allocation request size must be satisfied from a single dmap.
906 if (nblocks <= 0 || nblocks > BPERDMAP || blkno >= bmp->db_mapsize) {
907 IREAD_UNLOCK(ipbmap);
911 if (nblocks > ((s64) 1 << bmp->db_maxfreebud)) {
912 /* the free space is no longer available */
913 IREAD_UNLOCK(ipbmap);
917 /* read in the dmap covering the extent */
918 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
919 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
921 IREAD_UNLOCK(ipbmap);
924 dp = (struct dmap *) mp->data;
926 /* try to allocate the requested extent */
927 rc = dbAllocNext(bmp, dp, blkno, nblocks);
929 IREAD_UNLOCK(ipbmap);
932 mark_metapage_dirty(mp);
934 release_metapage(mp);
943 * FUNCTION: attempt to extend a current allocation by a specified
946 * this routine attempts to satisfy the allocation request
947 * by first trying to extend the existing allocation in
948 * place by allocating the additional blocks as the blocks
949 * immediately following the current allocation. if these
950 * blocks are not available, this routine will attempt to
951 * allocate a new set of contiguous blocks large enough
952 * to cover the existing allocation plus the additional
953 * number of blocks required.
956 * ip - pointer to in-core inode requiring allocation.
957 * blkno - starting block of the current allocation.
958 * nblocks - number of contiguous blocks within the current
960 * addnblocks - number of blocks to add to the allocation.
961 * results - on successful return, set to the starting block number
962 * of the existing allocation if the existing allocation
963 * was extended in place or to a newly allocated contiguous
964 * range if the existing allocation could not be extended
969 * -ENOSPC - insufficient disk resources
973 dbReAlloc(struct inode *ip,
974 s64 blkno, s64 nblocks, s64 addnblocks, s64 * results)
978 /* try to extend the allocation in place.
980 if ((rc = dbExtend(ip, blkno, nblocks, addnblocks)) == 0) {
988 /* could not extend the allocation in place, so allocate a
989 * new set of blocks for the entire request (i.e. try to get
990 * a range of contiguous blocks large enough to cover the
991 * existing allocation plus the additional blocks.)
994 (ip, blkno + nblocks - 1, addnblocks + nblocks, results));
1001 * FUNCTION: attempt to extend a current allocation by a specified
1004 * this routine attempts to satisfy the allocation request
1005 * by first trying to extend the existing allocation in
1006 * place by allocating the additional blocks as the blocks
1007 * immediately following the current allocation.
1010 * ip - pointer to in-core inode requiring allocation.
1011 * blkno - starting block of the current allocation.
1012 * nblocks - number of contiguous blocks within the current
1014 * addnblocks - number of blocks to add to the allocation.
1018 * -ENOSPC - insufficient disk resources
1021 static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks)
1023 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1024 s64 lblkno, lastblkno, extblkno;
1026 struct metapage *mp;
1029 struct inode *ipbmap = sbi->ipbmap;
1033 * We don't want a non-aligned extent to cross a page boundary
1035 if (((rel_block = blkno & (sbi->nbperpage - 1))) &&
1036 (rel_block + nblocks + addnblocks > sbi->nbperpage))
1039 /* get the last block of the current allocation */
1040 lastblkno = blkno + nblocks - 1;
1042 /* determine the block number of the block following
1043 * the existing allocation.
1045 extblkno = lastblkno + 1;
1047 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
1049 /* better be within the file system */
1051 if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) {
1052 IREAD_UNLOCK(ipbmap);
1053 jfs_error(ip->i_sb, "the block is outside the filesystem\n");
1057 /* we'll attempt to extend the current allocation in place by
1058 * allocating the additional blocks as the blocks immediately
1059 * following the current allocation. we only try to extend the
1060 * current allocation in place if the number of additional blocks
1061 * can fit into a dmap, the last block of the current allocation
1062 * is not the last block of the file system, and the start of the
1063 * inplace extension is not on an allocation group boundary.
1065 if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize ||
1066 (extblkno & (bmp->db_agsize - 1)) == 0) {
1067 IREAD_UNLOCK(ipbmap);
1071 /* get the buffer for the dmap containing the first block
1074 lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage);
1075 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
1077 IREAD_UNLOCK(ipbmap);
1081 dp = (struct dmap *) mp->data;
1083 /* try to allocate the blocks immediately following the
1084 * current allocation.
1086 rc = dbAllocNext(bmp, dp, extblkno, (int) addnblocks);
1088 IREAD_UNLOCK(ipbmap);
1090 /* were we successful ? */
1094 /* we were not successful */
1095 release_metapage(mp);
1102 * NAME: dbAllocNext()
1104 * FUNCTION: attempt to allocate the blocks of the specified block
1105 * range within a dmap.
1108 * bmp - pointer to bmap descriptor
1109 * dp - pointer to dmap.
1110 * blkno - starting block number of the range.
1111 * nblocks - number of contiguous free blocks of the range.
1115 * -ENOSPC - insufficient disk resources
1118 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1120 static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
1123 int dbitno, word, rembits, nb, nwords, wbitno, nw;
1128 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1129 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n");
1133 /* pick up a pointer to the leaves of the dmap tree.
1135 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
1137 /* determine the bit number and word within the dmap of the
1140 dbitno = blkno & (BPERDMAP - 1);
1141 word = dbitno >> L2DBWORD;
1143 /* check if the specified block range is contained within
1146 if (dbitno + nblocks > BPERDMAP)
1149 /* check if the starting leaf indicates that anything
1152 if (leaf[word] == NOFREE)
1155 /* check the dmaps words corresponding to block range to see
1156 * if the block range is free. not all bits of the first and
1157 * last words may be contained within the block range. if this
1158 * is the case, we'll work against those words (i.e. partial first
1159 * and/or last) on an individual basis (a single pass) and examine
1160 * the actual bits to determine if they are free. a single pass
1161 * will be used for all dmap words fully contained within the
1162 * specified range. within this pass, the leaves of the dmap
1163 * tree will be examined to determine if the blocks are free. a
1164 * single leaf may describe the free space of multiple dmap
1165 * words, so we may visit only a subset of the actual leaves
1166 * corresponding to the dmap words of the block range.
1168 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
1169 /* determine the bit number within the word and
1170 * the number of bits within the word.
1172 wbitno = dbitno & (DBWORD - 1);
1173 nb = min(rembits, DBWORD - wbitno);
1175 /* check if only part of the word is to be examined.
1178 /* check if the bits are free.
1180 mask = (ONES << (DBWORD - nb) >> wbitno);
1181 if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask)
1186 /* one or more dmap words are fully contained
1187 * within the block range. determine how many
1188 * words and how many bits.
1190 nwords = rembits >> L2DBWORD;
1191 nb = nwords << L2DBWORD;
1193 /* now examine the appropriate leaves to determine
1194 * if the blocks are free.
1196 while (nwords > 0) {
1197 /* does the leaf describe any free space ?
1199 if (leaf[word] < BUDMIN)
1202 /* determine the l2 number of bits provided
1206 min_t(int, leaf[word], NLSTOL2BSZ(nwords));
1208 /* determine how many words were handled.
1210 nw = BUDSIZE(l2size, BUDMIN);
1218 /* allocate the blocks.
1220 return (dbAllocDmap(bmp, dp, blkno, nblocks));
1225 * NAME: dbAllocNear()
1227 * FUNCTION: attempt to allocate a number of contiguous free blocks near
1228 * a specified block (hint) within a dmap.
1230 * starting with the dmap leaf that covers the hint, we'll
1231 * check the next four contiguous leaves for sufficient free
1232 * space. if sufficient free space is found, we'll allocate
1233 * the desired free space.
1236 * bmp - pointer to bmap descriptor
1237 * dp - pointer to dmap.
1238 * blkno - block number to allocate near.
1239 * nblocks - actual number of contiguous free blocks desired.
1240 * l2nb - log2 number of contiguous free blocks desired.
1241 * results - on successful return, set to the starting block number
1242 * of the newly allocated range.
1246 * -ENOSPC - insufficient disk resources
1249 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1252 dbAllocNear(struct bmap * bmp,
1253 struct dmap * dp, s64 blkno, int nblocks, int l2nb, s64 * results)
1255 int word, lword, rc;
1258 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1259 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n");
1263 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
1265 /* determine the word within the dmap that holds the hint
1266 * (i.e. blkno). also, determine the last word in the dmap
1267 * that we'll include in our examination.
1269 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
1270 lword = min(word + 4, LPERDMAP);
1272 /* examine the leaves for sufficient free space.
1274 for (; word < lword; word++) {
1275 /* does the leaf describe sufficient free space ?
1277 if (leaf[word] < l2nb)
1280 /* determine the block number within the file system
1281 * of the first block described by this dmap word.
1283 blkno = le64_to_cpu(dp->start) + (word << L2DBWORD);
1285 /* if not all bits of the dmap word are free, get the
1286 * starting bit number within the dmap word of the required
1287 * string of free bits and adjust the block number with the
1290 if (leaf[word] < BUDMIN)
1292 dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb);
1294 /* allocate the blocks.
1296 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
1309 * FUNCTION: attempt to allocate the specified number of contiguous
1310 * free blocks within the specified allocation group.
1312 * unless the allocation group size is equal to the number
1313 * of blocks per dmap, the dmap control pages will be used to
1314 * find the required free space, if available. we start the
1315 * search at the highest dmap control page level which
1316 * distinctly describes the allocation group's free space
1317 * (i.e. the highest level at which the allocation group's
1318 * free space is not mixed in with that of any other group).
1319 * in addition, we start the search within this level at a
1320 * height of the dmapctl dmtree at which the nodes distinctly
1321 * describe the allocation group's free space. at this height,
1322 * the allocation group's free space may be represented by 1
1323 * or two sub-trees, depending on the allocation group size.
1324 * we search the top nodes of these subtrees left to right for
1325 * sufficient free space. if sufficient free space is found,
1326 * the subtree is searched to find the leftmost leaf that
1327 * has free space. once we have made it to the leaf, we
1328 * move the search to the next lower level dmap control page
1329 * corresponding to this leaf. we continue down the dmap control
1330 * pages until we find the dmap that contains or starts the
1331 * sufficient free space and we allocate at this dmap.
1333 * if the allocation group size is equal to the dmap size,
1334 * we'll start at the dmap corresponding to the allocation
1335 * group and attempt the allocation at this level.
1337 * the dmap control page search is also not performed if the
1338 * allocation group is completely free and we go to the first
1339 * dmap of the allocation group to do the allocation. this is
1340 * done because the allocation group may be part (not the first
1341 * part) of a larger binary buddy system, causing the dmap
1342 * control pages to indicate no free space (NOFREE) within
1343 * the allocation group.
1346 * bmp - pointer to bmap descriptor
1347 * agno - allocation group number.
1348 * nblocks - actual number of contiguous free blocks desired.
1349 * l2nb - log2 number of contiguous free blocks desired.
1350 * results - on successful return, set to the starting block number
1351 * of the newly allocated range.
1355 * -ENOSPC - insufficient disk resources
1358 * note: IWRITE_LOCK(ipmap) held on entry/exit;
1361 dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb, s64 * results)
1363 struct metapage *mp;
1364 struct dmapctl *dcp;
1365 int rc, ti, i, k, m, n, agperlev;
1369 /* allocation request should not be for more than the
1370 * allocation group size.
1372 if (l2nb > bmp->db_agl2size) {
1373 jfs_error(bmp->db_ipbmap->i_sb,
1374 "allocation request is larger than the allocation group size\n");
1378 /* determine the starting block number of the allocation
1381 blkno = (s64) agno << bmp->db_agl2size;
1383 /* check if the allocation group size is the minimum allocation
1384 * group size or if the allocation group is completely free. if
1385 * the allocation group size is the minimum size of BPERDMAP (i.e.
1386 * 1 dmap), there is no need to search the dmap control page (below)
1387 * that fully describes the allocation group since the allocation
1388 * group is already fully described by a dmap. in this case, we
1389 * just call dbAllocCtl() to search the dmap tree and allocate the
1390 * required space if available.
1392 * if the allocation group is completely free, dbAllocCtl() is
1393 * also called to allocate the required space. this is done for
1394 * two reasons. first, it makes no sense searching the dmap control
1395 * pages for free space when we know that free space exists. second,
1396 * the dmap control pages may indicate that the allocation group
1397 * has no free space if the allocation group is part (not the first
1398 * part) of a larger binary buddy system.
1400 if (bmp->db_agsize == BPERDMAP
1401 || bmp->db_agfree[agno] == bmp->db_agsize) {
1402 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1403 if ((rc == -ENOSPC) &&
1404 (bmp->db_agfree[agno] == bmp->db_agsize)) {
1405 printk(KERN_ERR "blkno = %Lx, blocks = %Lx\n",
1406 (unsigned long long) blkno,
1407 (unsigned long long) nblocks);
1408 jfs_error(bmp->db_ipbmap->i_sb,
1409 "dbAllocCtl failed in free AG\n");
1414 /* the buffer for the dmap control page that fully describes the
1417 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel);
1418 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1421 dcp = (struct dmapctl *) mp->data;
1422 budmin = dcp->budmin;
1424 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1425 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n");
1426 release_metapage(mp);
1430 /* search the subtree(s) of the dmap control page that describes
1431 * the allocation group, looking for sufficient free space. to begin,
1432 * determine how many allocation groups are represented in a dmap
1433 * control page at the control page level (i.e. L0, L1, L2) that
1434 * fully describes an allocation group. next, determine the starting
1435 * tree index of this allocation group within the control page.
1438 (1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth;
1439 ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1));
1441 /* dmap control page trees fan-out by 4 and a single allocation
1442 * group may be described by 1 or 2 subtrees within the ag level
1443 * dmap control page, depending upon the ag size. examine the ag's
1444 * subtrees for sufficient free space, starting with the leftmost
1447 for (i = 0; i < bmp->db_agwidth; i++, ti++) {
1448 /* is there sufficient free space ?
1450 if (l2nb > dcp->stree[ti])
1453 /* sufficient free space found in a subtree. now search down
1454 * the subtree to find the leftmost leaf that describes this
1457 for (k = bmp->db_agheight; k > 0; k--) {
1458 for (n = 0, m = (ti << 2) + 1; n < 4; n++) {
1459 if (l2nb <= dcp->stree[m + n]) {
1465 jfs_error(bmp->db_ipbmap->i_sb,
1466 "failed descending stree\n");
1467 release_metapage(mp);
1472 /* determine the block number within the file system
1473 * that corresponds to this leaf.
1475 if (bmp->db_aglevel == 2)
1477 else if (bmp->db_aglevel == 1)
1478 blkno &= ~(MAXL1SIZE - 1);
1479 else /* bmp->db_aglevel == 0 */
1480 blkno &= ~(MAXL0SIZE - 1);
1483 ((s64) (ti - le32_to_cpu(dcp->leafidx))) << budmin;
1485 /* release the buffer in preparation for going down
1486 * the next level of dmap control pages.
1488 release_metapage(mp);
1490 /* check if we need to continue to search down the lower
1491 * level dmap control pages. we need to if the number of
1492 * blocks required is less than maximum number of blocks
1493 * described at the next lower level.
1495 if (l2nb < budmin) {
1497 /* search the lower level dmap control pages to get
1498 * the starting block number of the dmap that
1499 * contains or starts off the free space.
1502 dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1,
1504 if (rc == -ENOSPC) {
1505 jfs_error(bmp->db_ipbmap->i_sb,
1506 "control page inconsistent\n");
1513 /* allocate the blocks.
1515 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1516 if (rc == -ENOSPC) {
1517 jfs_error(bmp->db_ipbmap->i_sb,
1518 "unable to allocate blocks\n");
1524 /* no space in the allocation group. release the buffer and
1527 release_metapage(mp);
1534 * NAME: dbAllocAny()
1536 * FUNCTION: attempt to allocate the specified number of contiguous
1537 * free blocks anywhere in the file system.
1539 * dbAllocAny() attempts to find the sufficient free space by
1540 * searching down the dmap control pages, starting with the
1541 * highest level (i.e. L0, L1, L2) control page. if free space
1542 * large enough to satisfy the desired free space is found, the
1543 * desired free space is allocated.
1546 * bmp - pointer to bmap descriptor
1547 * nblocks - actual number of contiguous free blocks desired.
1548 * l2nb - log2 number of contiguous free blocks desired.
1549 * results - on successful return, set to the starting block number
1550 * of the newly allocated range.
1554 * -ENOSPC - insufficient disk resources
1557 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1559 static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results)
1564 /* starting with the top level dmap control page, search
1565 * down the dmap control levels for sufficient free space.
1566 * if free space is found, dbFindCtl() returns the starting
1567 * block number of the dmap that contains or starts off the
1568 * range of free space.
1570 if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno)))
1573 /* allocate the blocks.
1575 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1576 if (rc == -ENOSPC) {
1577 jfs_error(bmp->db_ipbmap->i_sb, "unable to allocate blocks\n");
1585 * NAME: dbDiscardAG()
1587 * FUNCTION: attempt to discard (TRIM) all free blocks of specific AG
1590 * 1) allocate blocks, as large as possible and save them
1591 * while holding IWRITE_LOCK on ipbmap
1592 * 2) trim all these saved block/length values
1593 * 3) mark the blocks free again
1596 * - we work only on one ag at some time, minimizing how long we
1597 * need to lock ipbmap
1598 * - reading / writing the fs is possible most time, even on
1602 * - we write two times to the dmapctl and dmap pages
1603 * - but for me, this seems the best way, better ideas?
1607 * ip - pointer to in-core inode
1609 * minlen - minimum value of contiguous blocks
1612 * s64 - actual number of blocks trimmed
1614 s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen)
1616 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
1617 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
1621 struct super_block *sb = ipbmap->i_sb;
1628 /* max blkno / nblocks pairs to trim */
1629 int count = 0, range_cnt;
1632 /* prevent others from writing new stuff here, while trimming */
1633 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
1635 nblocks = bmp->db_agfree[agno];
1636 max_ranges = nblocks;
1637 do_div(max_ranges, minlen);
1638 range_cnt = min_t(u64, max_ranges + 1, 32 * 1024);
1639 totrim = kmalloc_array(range_cnt, sizeof(struct range2trim), GFP_NOFS);
1640 if (totrim == NULL) {
1641 jfs_error(bmp->db_ipbmap->i_sb, "no memory for trim array\n");
1642 IWRITE_UNLOCK(ipbmap);
1647 while (nblocks >= minlen) {
1648 l2nb = BLKSTOL2(nblocks);
1650 /* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */
1651 rc = dbAllocAG(bmp, agno, nblocks, l2nb, &blkno);
1654 tt->nblocks = nblocks;
1657 /* the whole ag is free, trim now */
1658 if (bmp->db_agfree[agno] == 0)
1661 /* give a hint for the next while */
1662 nblocks = bmp->db_agfree[agno];
1664 } else if (rc == -ENOSPC) {
1665 /* search for next smaller log2 block */
1666 l2nb = BLKSTOL2(nblocks) - 1;
1667 nblocks = 1LL << l2nb;
1669 /* Trim any already allocated blocks */
1670 jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n");
1674 /* check, if our trim array is full */
1675 if (unlikely(count >= range_cnt - 1))
1678 IWRITE_UNLOCK(ipbmap);
1680 tt->nblocks = 0; /* mark the current end */
1681 for (tt = totrim; tt->nblocks != 0; tt++) {
1682 /* when mounted with online discard, dbFree() will
1683 * call jfs_issue_discard() itself */
1684 if (!(JFS_SBI(sb)->flag & JFS_DISCARD))
1685 jfs_issue_discard(ip, tt->blkno, tt->nblocks);
1686 dbFree(ip, tt->blkno, tt->nblocks);
1687 trimmed += tt->nblocks;
1697 * FUNCTION: starting at a specified dmap control page level and block
1698 * number, search down the dmap control levels for a range of
1699 * contiguous free blocks large enough to satisfy an allocation
1700 * request for the specified number of free blocks.
1702 * if sufficient contiguous free blocks are found, this routine
1703 * returns the starting block number within a dmap page that
1704 * contains or starts a range of contiqious free blocks that
1705 * is sufficient in size.
1708 * bmp - pointer to bmap descriptor
1709 * level - starting dmap control page level.
1710 * l2nb - log2 number of contiguous free blocks desired.
1711 * *blkno - on entry, starting block number for conducting the search.
1712 * on successful return, the first block within a dmap page
1713 * that contains or starts a range of contiguous free blocks.
1717 * -ENOSPC - insufficient disk resources
1720 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1722 static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno)
1724 int rc, leafidx, lev;
1726 struct dmapctl *dcp;
1728 struct metapage *mp;
1730 /* starting at the specified dmap control page level and block
1731 * number, search down the dmap control levels for the starting
1732 * block number of a dmap page that contains or starts off
1733 * sufficient free blocks.
1735 for (lev = level, b = *blkno; lev >= 0; lev--) {
1736 /* get the buffer of the dmap control page for the block
1737 * number and level (i.e. L0, L1, L2).
1739 lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev);
1740 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1743 dcp = (struct dmapctl *) mp->data;
1744 budmin = dcp->budmin;
1746 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1747 jfs_error(bmp->db_ipbmap->i_sb,
1748 "Corrupt dmapctl page\n");
1749 release_metapage(mp);
1753 /* search the tree within the dmap control page for
1754 * sufficient free space. if sufficient free space is found,
1755 * dbFindLeaf() returns the index of the leaf at which
1756 * free space was found.
1758 rc = dbFindLeaf((dmtree_t *) dcp, l2nb, &leafidx);
1760 /* release the buffer.
1762 release_metapage(mp);
1768 jfs_error(bmp->db_ipbmap->i_sb,
1769 "dmap inconsistent\n");
1775 /* adjust the block number to reflect the location within
1776 * the dmap control page (i.e. the leaf) at which free
1779 b += (((s64) leafidx) << budmin);
1781 /* we stop the search at this dmap control page level if
1782 * the number of blocks required is greater than or equal
1783 * to the maximum number of blocks described at the next
1796 * NAME: dbAllocCtl()
1798 * FUNCTION: attempt to allocate a specified number of contiguous
1799 * blocks starting within a specific dmap.
1801 * this routine is called by higher level routines that search
1802 * the dmap control pages above the actual dmaps for contiguous
1803 * free space. the result of successful searches by these
1804 * routines are the starting block numbers within dmaps, with
1805 * the dmaps themselves containing the desired contiguous free
1806 * space or starting a contiguous free space of desired size
1807 * that is made up of the blocks of one or more dmaps. these
1808 * calls should not fail due to insufficent resources.
1810 * this routine is called in some cases where it is not known
1811 * whether it will fail due to insufficient resources. more
1812 * specifically, this occurs when allocating from an allocation
1813 * group whose size is equal to the number of blocks per dmap.
1814 * in this case, the dmap control pages are not examined prior
1815 * to calling this routine (to save pathlength) and the call
1818 * for a request size that fits within a dmap, this routine relies
1819 * upon the dmap's dmtree to find the requested contiguous free
1820 * space. for request sizes that are larger than a dmap, the
1821 * requested free space will start at the first block of the
1822 * first dmap (i.e. blkno).
1825 * bmp - pointer to bmap descriptor
1826 * nblocks - actual number of contiguous free blocks to allocate.
1827 * l2nb - log2 number of contiguous free blocks to allocate.
1828 * blkno - starting block number of the dmap to start the allocation
1830 * results - on successful return, set to the starting block number
1831 * of the newly allocated range.
1835 * -ENOSPC - insufficient disk resources
1838 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1841 dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, s64 * results)
1845 struct metapage *mp;
1848 /* check if the allocation request is confined to a single dmap.
1850 if (l2nb <= L2BPERDMAP) {
1851 /* get the buffer for the dmap.
1853 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
1854 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1857 dp = (struct dmap *) mp->data;
1859 /* try to allocate the blocks.
1861 rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results);
1863 mark_metapage_dirty(mp);
1865 release_metapage(mp);
1870 /* allocation request involving multiple dmaps. it must start on
1873 assert((blkno & (BPERDMAP - 1)) == 0);
1875 /* allocate the blocks dmap by dmap.
1877 for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) {
1878 /* get the buffer for the dmap.
1880 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1881 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1886 dp = (struct dmap *) mp->data;
1888 /* the dmap better be all free.
1890 if (dp->tree.stree[ROOT] != L2BPERDMAP) {
1891 release_metapage(mp);
1892 jfs_error(bmp->db_ipbmap->i_sb,
1893 "the dmap is not all free\n");
1898 /* determine how many blocks to allocate from this dmap.
1900 nb = min_t(s64, n, BPERDMAP);
1902 /* allocate the blocks from the dmap.
1904 if ((rc = dbAllocDmap(bmp, dp, b, nb))) {
1905 release_metapage(mp);
1909 /* write the buffer.
1914 /* set the results (starting block number) and return.
1919 /* something failed in handling an allocation request involving
1920 * multiple dmaps. we'll try to clean up by backing out any
1921 * allocation that has already happened for this request. if
1922 * we fail in backing out the allocation, we'll mark the file
1923 * system to indicate that blocks have been leaked.
1927 /* try to backout the allocations dmap by dmap.
1929 for (n = nblocks - n, b = blkno; n > 0;
1930 n -= BPERDMAP, b += BPERDMAP) {
1931 /* get the buffer for this dmap.
1933 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1934 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1936 /* could not back out. mark the file system
1937 * to indicate that we have leaked blocks.
1939 jfs_error(bmp->db_ipbmap->i_sb,
1940 "I/O Error: Block Leakage\n");
1943 dp = (struct dmap *) mp->data;
1945 /* free the blocks is this dmap.
1947 if (dbFreeDmap(bmp, dp, b, BPERDMAP)) {
1948 /* could not back out. mark the file system
1949 * to indicate that we have leaked blocks.
1951 release_metapage(mp);
1952 jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n");
1956 /* write the buffer.
1966 * NAME: dbAllocDmapLev()
1968 * FUNCTION: attempt to allocate a specified number of contiguous blocks
1969 * from a specified dmap.
1971 * this routine checks if the contiguous blocks are available.
1972 * if so, nblocks of blocks are allocated; otherwise, ENOSPC is
1976 * mp - pointer to bmap descriptor
1977 * dp - pointer to dmap to attempt to allocate blocks from.
1978 * l2nb - log2 number of contiguous block desired.
1979 * nblocks - actual number of contiguous block desired.
1980 * results - on successful return, set to the starting block number
1981 * of the newly allocated range.
1985 * -ENOSPC - insufficient disk resources
1988 * serialization: IREAD_LOCK(ipbmap), e.g., from dbAlloc(), or
1989 * IWRITE_LOCK(ipbmap), e.g., dbAllocCtl(), held on entry/exit;
1992 dbAllocDmapLev(struct bmap * bmp,
1993 struct dmap * dp, int nblocks, int l2nb, s64 * results)
1998 /* can't be more than a dmaps worth of blocks */
1999 assert(l2nb <= L2BPERDMAP);
2001 /* search the tree within the dmap page for sufficient
2002 * free space. if sufficient free space is found, dbFindLeaf()
2003 * returns the index of the leaf at which free space was found.
2005 if (dbFindLeaf((dmtree_t *) & dp->tree, l2nb, &leafidx))
2008 /* determine the block number within the file system corresponding
2009 * to the leaf at which free space was found.
2011 blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD);
2013 /* if not all bits of the dmap word are free, get the starting
2014 * bit number within the dmap word of the required string of free
2015 * bits and adjust the block number with this value.
2017 if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN)
2018 blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb);
2020 /* allocate the blocks */
2021 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
2029 * NAME: dbAllocDmap()
2031 * FUNCTION: adjust the disk allocation map to reflect the allocation
2032 * of a specified block range within a dmap.
2034 * this routine allocates the specified blocks from the dmap
2035 * through a call to dbAllocBits(). if the allocation of the
2036 * block range causes the maximum string of free blocks within
2037 * the dmap to change (i.e. the value of the root of the dmap's
2038 * dmtree), this routine will cause this change to be reflected
2039 * up through the appropriate levels of the dmap control pages
2040 * by a call to dbAdjCtl() for the L0 dmap control page that
2044 * bmp - pointer to bmap descriptor
2045 * dp - pointer to dmap to allocate the block range from.
2046 * blkno - starting block number of the block to be allocated.
2047 * nblocks - number of blocks to be allocated.
2053 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2055 static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2061 /* save the current value of the root (i.e. maximum free string)
2064 oldroot = dp->tree.stree[ROOT];
2066 /* allocate the specified (blocks) bits */
2067 dbAllocBits(bmp, dp, blkno, nblocks);
2069 /* if the root has not changed, done. */
2070 if (dp->tree.stree[ROOT] == oldroot)
2073 /* root changed. bubble the change up to the dmap control pages.
2074 * if the adjustment of the upper level control pages fails,
2075 * backout the bit allocation (thus making everything consistent).
2077 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0)))
2078 dbFreeBits(bmp, dp, blkno, nblocks);
2085 * NAME: dbFreeDmap()
2087 * FUNCTION: adjust the disk allocation map to reflect the allocation
2088 * of a specified block range within a dmap.
2090 * this routine frees the specified blocks from the dmap through
2091 * a call to dbFreeBits(). if the deallocation of the block range
2092 * causes the maximum string of free blocks within the dmap to
2093 * change (i.e. the value of the root of the dmap's dmtree), this
2094 * routine will cause this change to be reflected up through the
2095 * appropriate levels of the dmap control pages by a call to
2096 * dbAdjCtl() for the L0 dmap control page that covers this dmap.
2099 * bmp - pointer to bmap descriptor
2100 * dp - pointer to dmap to free the block range from.
2101 * blkno - starting block number of the block to be freed.
2102 * nblocks - number of blocks to be freed.
2108 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2110 static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2116 /* save the current value of the root (i.e. maximum free string)
2119 oldroot = dp->tree.stree[ROOT];
2121 /* free the specified (blocks) bits */
2122 rc = dbFreeBits(bmp, dp, blkno, nblocks);
2124 /* if error or the root has not changed, done. */
2125 if (rc || (dp->tree.stree[ROOT] == oldroot))
2128 /* root changed. bubble the change up to the dmap control pages.
2129 * if the adjustment of the upper level control pages fails,
2130 * backout the deallocation.
2132 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) {
2133 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
2135 /* as part of backing out the deallocation, we will have
2136 * to back split the dmap tree if the deallocation caused
2137 * the freed blocks to become part of a larger binary buddy
2140 if (dp->tree.stree[word] == NOFREE)
2141 dbBackSplit((dmtree_t *) & dp->tree, word);
2143 dbAllocBits(bmp, dp, blkno, nblocks);
2151 * NAME: dbAllocBits()
2153 * FUNCTION: allocate a specified block range from a dmap.
2155 * this routine updates the dmap to reflect the working
2156 * state allocation of the specified block range. it directly
2157 * updates the bits of the working map and causes the adjustment
2158 * of the binary buddy system described by the dmap's dmtree
2159 * leaves to reflect the bits allocated. it also causes the
2160 * dmap's dmtree, as a whole, to reflect the allocated range.
2163 * bmp - pointer to bmap descriptor
2164 * dp - pointer to dmap to allocate bits from.
2165 * blkno - starting block number of the bits to be allocated.
2166 * nblocks - number of bits to be allocated.
2168 * RETURN VALUES: none
2170 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2172 static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
2175 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2176 dmtree_t *tp = (dmtree_t *) & dp->tree;
2180 /* pick up a pointer to the leaves of the dmap tree */
2181 leaf = dp->tree.stree + LEAFIND;
2183 /* determine the bit number and word within the dmap of the
2186 dbitno = blkno & (BPERDMAP - 1);
2187 word = dbitno >> L2DBWORD;
2189 /* block range better be within the dmap */
2190 assert(dbitno + nblocks <= BPERDMAP);
2192 /* allocate the bits of the dmap's words corresponding to the block
2193 * range. not all bits of the first and last words may be contained
2194 * within the block range. if this is the case, we'll work against
2195 * those words (i.e. partial first and/or last) on an individual basis
2196 * (a single pass), allocating the bits of interest by hand and
2197 * updating the leaf corresponding to the dmap word. a single pass
2198 * will be used for all dmap words fully contained within the
2199 * specified range. within this pass, the bits of all fully contained
2200 * dmap words will be marked as free in a single shot and the leaves
2201 * will be updated. a single leaf may describe the free space of
2202 * multiple dmap words, so we may update only a subset of the actual
2203 * leaves corresponding to the dmap words of the block range.
2205 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2206 /* determine the bit number within the word and
2207 * the number of bits within the word.
2209 wbitno = dbitno & (DBWORD - 1);
2210 nb = min(rembits, DBWORD - wbitno);
2212 /* check if only part of a word is to be allocated.
2215 /* allocate (set to 1) the appropriate bits within
2218 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
2221 /* update the leaf for this dmap word. in addition
2222 * to setting the leaf value to the binary buddy max
2223 * of the updated dmap word, dbSplit() will split
2224 * the binary system of the leaves if need be.
2226 dbSplit(tp, word, BUDMIN,
2227 dbMaxBud((u8 *) & dp->wmap[word]));
2231 /* one or more dmap words are fully contained
2232 * within the block range. determine how many
2233 * words and allocate (set to 1) the bits of these
2236 nwords = rembits >> L2DBWORD;
2237 memset(&dp->wmap[word], (int) ONES, nwords * 4);
2239 /* determine how many bits.
2241 nb = nwords << L2DBWORD;
2243 /* now update the appropriate leaves to reflect
2244 * the allocated words.
2246 for (; nwords > 0; nwords -= nw) {
2247 if (leaf[word] < BUDMIN) {
2248 jfs_error(bmp->db_ipbmap->i_sb,
2249 "leaf page corrupt\n");
2253 /* determine what the leaf value should be
2254 * updated to as the minimum of the l2 number
2255 * of bits being allocated and the l2 number
2256 * of bits currently described by this leaf.
2258 size = min_t(int, leaf[word],
2259 NLSTOL2BSZ(nwords));
2261 /* update the leaf to reflect the allocation.
2262 * in addition to setting the leaf value to
2263 * NOFREE, dbSplit() will split the binary
2264 * system of the leaves to reflect the current
2265 * allocation (size).
2267 dbSplit(tp, word, size, NOFREE);
2269 /* get the number of dmap words handled */
2270 nw = BUDSIZE(size, BUDMIN);
2276 /* update the free count for this dmap */
2277 le32_add_cpu(&dp->nfree, -nblocks);
2281 /* if this allocation group is completely free,
2282 * update the maximum allocation group number if this allocation
2283 * group is the new max.
2285 agno = blkno >> bmp->db_agl2size;
2286 if (agno > bmp->db_maxag)
2287 bmp->db_maxag = agno;
2289 /* update the free count for the allocation group and map */
2290 bmp->db_agfree[agno] -= nblocks;
2291 bmp->db_nfree -= nblocks;
2298 * NAME: dbFreeBits()
2300 * FUNCTION: free a specified block range from a dmap.
2302 * this routine updates the dmap to reflect the working
2303 * state allocation of the specified block range. it directly
2304 * updates the bits of the working map and causes the adjustment
2305 * of the binary buddy system described by the dmap's dmtree
2306 * leaves to reflect the bits freed. it also causes the dmap's
2307 * dmtree, as a whole, to reflect the deallocated range.
2310 * bmp - pointer to bmap descriptor
2311 * dp - pointer to dmap to free bits from.
2312 * blkno - starting block number of the bits to be freed.
2313 * nblocks - number of bits to be freed.
2315 * RETURN VALUES: 0 for success
2317 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2319 static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
2322 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2323 dmtree_t *tp = (dmtree_t *) & dp->tree;
2327 /* determine the bit number and word within the dmap of the
2330 dbitno = blkno & (BPERDMAP - 1);
2331 word = dbitno >> L2DBWORD;
2333 /* block range better be within the dmap.
2335 assert(dbitno + nblocks <= BPERDMAP);
2337 /* free the bits of the dmaps words corresponding to the block range.
2338 * not all bits of the first and last words may be contained within
2339 * the block range. if this is the case, we'll work against those
2340 * words (i.e. partial first and/or last) on an individual basis
2341 * (a single pass), freeing the bits of interest by hand and updating
2342 * the leaf corresponding to the dmap word. a single pass will be used
2343 * for all dmap words fully contained within the specified range.
2344 * within this pass, the bits of all fully contained dmap words will
2345 * be marked as free in a single shot and the leaves will be updated. a
2346 * single leaf may describe the free space of multiple dmap words,
2347 * so we may update only a subset of the actual leaves corresponding
2348 * to the dmap words of the block range.
2350 * dbJoin() is used to update leaf values and will join the binary
2351 * buddy system of the leaves if the new leaf values indicate this
2354 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2355 /* determine the bit number within the word and
2356 * the number of bits within the word.
2358 wbitno = dbitno & (DBWORD - 1);
2359 nb = min(rembits, DBWORD - wbitno);
2361 /* check if only part of a word is to be freed.
2364 /* free (zero) the appropriate bits within this
2368 cpu_to_le32(~(ONES << (DBWORD - nb)
2371 /* update the leaf for this dmap word.
2373 rc = dbJoin(tp, word,
2374 dbMaxBud((u8 *) & dp->wmap[word]));
2380 /* one or more dmap words are fully contained
2381 * within the block range. determine how many
2382 * words and free (zero) the bits of these words.
2384 nwords = rembits >> L2DBWORD;
2385 memset(&dp->wmap[word], 0, nwords * 4);
2387 /* determine how many bits.
2389 nb = nwords << L2DBWORD;
2391 /* now update the appropriate leaves to reflect
2394 for (; nwords > 0; nwords -= nw) {
2395 /* determine what the leaf value should be
2396 * updated to as the minimum of the l2 number
2397 * of bits being freed and the l2 (max) number
2398 * of bits that can be described by this leaf.
2402 (word, L2LPERDMAP, BUDMIN),
2403 NLSTOL2BSZ(nwords));
2407 rc = dbJoin(tp, word, size);
2411 /* get the number of dmap words handled.
2413 nw = BUDSIZE(size, BUDMIN);
2419 /* update the free count for this dmap.
2421 le32_add_cpu(&dp->nfree, nblocks);
2425 /* update the free count for the allocation group and
2428 agno = blkno >> bmp->db_agl2size;
2429 bmp->db_nfree += nblocks;
2430 bmp->db_agfree[agno] += nblocks;
2432 /* check if this allocation group is not completely free and
2433 * if it is currently the maximum (rightmost) allocation group.
2434 * if so, establish the new maximum allocation group number by
2435 * searching left for the first allocation group with allocation.
2437 if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) ||
2438 (agno == bmp->db_numag - 1 &&
2439 bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) {
2440 while (bmp->db_maxag > 0) {
2442 if (bmp->db_agfree[bmp->db_maxag] !=
2447 /* re-establish the allocation group preference if the
2448 * current preference is right of the maximum allocation
2451 if (bmp->db_agpref > bmp->db_maxag)
2452 bmp->db_agpref = bmp->db_maxag;
2464 * FUNCTION: adjust a dmap control page at a specified level to reflect
2465 * the change in a lower level dmap or dmap control page's
2466 * maximum string of free blocks (i.e. a change in the root
2467 * of the lower level object's dmtree) due to the allocation
2468 * or deallocation of a range of blocks with a single dmap.
2470 * on entry, this routine is provided with the new value of
2471 * the lower level dmap or dmap control page root and the
2472 * starting block number of the block range whose allocation
2473 * or deallocation resulted in the root change. this range
2474 * is respresented by a single leaf of the current dmapctl
2475 * and the leaf will be updated with this value, possibly
2476 * causing a binary buddy system within the leaves to be
2477 * split or joined. the update may also cause the dmapctl's
2478 * dmtree to be updated.
2480 * if the adjustment of the dmap control page, itself, causes its
2481 * root to change, this change will be bubbled up to the next dmap
2482 * control level by a recursive call to this routine, specifying
2483 * the new root value and the next dmap control page level to
2486 * bmp - pointer to bmap descriptor
2487 * blkno - the first block of a block range within a dmap. it is
2488 * the allocation or deallocation of this block range that
2489 * requires the dmap control page to be adjusted.
2490 * newval - the new value of the lower level dmap or dmap control
2492 * alloc - 'true' if adjustment is due to an allocation.
2493 * level - current level of dmap control page (i.e. L0, L1, L2) to
2500 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2503 dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc, int level)
2505 struct metapage *mp;
2509 struct dmapctl *dcp;
2512 /* get the buffer for the dmap control page for the specified
2513 * block number and control page level.
2515 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level);
2516 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
2519 dcp = (struct dmapctl *) mp->data;
2521 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
2522 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n");
2523 release_metapage(mp);
2527 /* determine the leaf number corresponding to the block and
2528 * the index within the dmap control tree.
2530 leafno = BLKTOCTLLEAF(blkno, dcp->budmin);
2531 ti = leafno + le32_to_cpu(dcp->leafidx);
2533 /* save the current leaf value and the current root level (i.e.
2534 * maximum l2 free string described by this dmapctl).
2536 oldval = dcp->stree[ti];
2537 oldroot = dcp->stree[ROOT];
2539 /* check if this is a control page update for an allocation.
2540 * if so, update the leaf to reflect the new leaf value using
2541 * dbSplit(); otherwise (deallocation), use dbJoin() to update
2542 * the leaf with the new value. in addition to updating the
2543 * leaf, dbSplit() will also split the binary buddy system of
2544 * the leaves, if required, and bubble new values within the
2545 * dmapctl tree, if required. similarly, dbJoin() will join
2546 * the binary buddy system of leaves and bubble new values up
2547 * the dmapctl tree as required by the new leaf value.
2550 /* check if we are in the middle of a binary buddy
2551 * system. this happens when we are performing the
2552 * first allocation out of an allocation group that
2553 * is part (not the first part) of a larger binary
2554 * buddy system. if we are in the middle, back split
2555 * the system prior to calling dbSplit() which assumes
2556 * that it is at the front of a binary buddy system.
2558 if (oldval == NOFREE) {
2559 rc = dbBackSplit((dmtree_t *) dcp, leafno);
2562 oldval = dcp->stree[ti];
2564 dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval);
2566 rc = dbJoin((dmtree_t *) dcp, leafno, newval);
2571 /* check if the root of the current dmap control page changed due
2572 * to the update and if the current dmap control page is not at
2573 * the current top level (i.e. L0, L1, L2) of the map. if so (i.e.
2574 * root changed and this is not the top level), call this routine
2575 * again (recursion) for the next higher level of the mapping to
2576 * reflect the change in root for the current dmap control page.
2578 if (dcp->stree[ROOT] != oldroot) {
2579 /* are we below the top level of the map. if so,
2580 * bubble the root up to the next higher level.
2582 if (level < bmp->db_maxlevel) {
2583 /* bubble up the new root of this dmap control page to
2587 dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc,
2589 /* something went wrong in bubbling up the new
2590 * root value, so backout the changes to the
2591 * current dmap control page.
2594 dbJoin((dmtree_t *) dcp, leafno,
2597 /* the dbJoin() above might have
2598 * caused a larger binary buddy system
2599 * to form and we may now be in the
2600 * middle of it. if this is the case,
2601 * back split the buddies.
2603 if (dcp->stree[ti] == NOFREE)
2604 dbBackSplit((dmtree_t *)
2606 dbSplit((dmtree_t *) dcp, leafno,
2607 dcp->budmin, oldval);
2610 /* release the buffer and return the error.
2612 release_metapage(mp);
2616 /* we're at the top level of the map. update
2617 * the bmap control page to reflect the size
2618 * of the maximum free buddy system.
2620 assert(level == bmp->db_maxlevel);
2621 if (bmp->db_maxfreebud != oldroot) {
2622 jfs_error(bmp->db_ipbmap->i_sb,
2623 "the maximum free buddy is not the old root\n");
2625 bmp->db_maxfreebud = dcp->stree[ROOT];
2629 /* write the buffer.
2640 * FUNCTION: update the leaf of a dmtree with a new value, splitting
2641 * the leaf from the binary buddy system of the dmtree's
2642 * leaves, as required.
2645 * tp - pointer to the tree containing the leaf.
2646 * leafno - the number of the leaf to be updated.
2647 * splitsz - the size the binary buddy system starting at the leaf
2648 * must be split to, specified as the log2 number of blocks.
2649 * newval - the new value for the leaf.
2651 * RETURN VALUES: none
2653 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2655 static void dbSplit(dmtree_t * tp, int leafno, int splitsz, int newval)
2659 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2661 /* check if the leaf needs to be split.
2663 if (leaf[leafno] > tp->dmt_budmin) {
2664 /* the split occurs by cutting the buddy system in half
2665 * at the specified leaf until we reach the specified
2666 * size. pick up the starting split size (current size
2667 * - 1 in l2) and the corresponding buddy size.
2669 cursz = leaf[leafno] - 1;
2670 budsz = BUDSIZE(cursz, tp->dmt_budmin);
2672 /* split until we reach the specified size.
2674 while (cursz >= splitsz) {
2675 /* update the buddy's leaf with its new value.
2677 dbAdjTree(tp, leafno ^ budsz, cursz);
2679 /* on to the next size and buddy.
2686 /* adjust the dmap tree to reflect the specified leaf's new
2689 dbAdjTree(tp, leafno, newval);
2694 * NAME: dbBackSplit()
2696 * FUNCTION: back split the binary buddy system of dmtree leaves
2697 * that hold a specified leaf until the specified leaf
2698 * starts its own binary buddy system.
2700 * the allocators typically perform allocations at the start
2701 * of binary buddy systems and dbSplit() is used to accomplish
2702 * any required splits. in some cases, however, allocation
2703 * may occur in the middle of a binary system and requires a
2704 * back split, with the split proceeding out from the middle of
2705 * the system (less efficient) rather than the start of the
2706 * system (more efficient). the cases in which a back split
2707 * is required are rare and are limited to the first allocation
2708 * within an allocation group which is a part (not first part)
2709 * of a larger binary buddy system and a few exception cases
2710 * in which a previous join operation must be backed out.
2713 * tp - pointer to the tree containing the leaf.
2714 * leafno - the number of the leaf to be updated.
2716 * RETURN VALUES: none
2718 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2720 static int dbBackSplit(dmtree_t * tp, int leafno)
2722 int budsz, bud, w, bsz, size;
2724 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2726 /* leaf should be part (not first part) of a binary
2729 assert(leaf[leafno] == NOFREE);
2731 /* the back split is accomplished by iteratively finding the leaf
2732 * that starts the buddy system that contains the specified leaf and
2733 * splitting that system in two. this iteration continues until
2734 * the specified leaf becomes the start of a buddy system.
2736 * determine maximum possible l2 size for the specified leaf.
2739 LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs),
2742 /* determine the number of leaves covered by this size. this
2743 * is the buddy size that we will start with as we search for
2744 * the buddy system that contains the specified leaf.
2746 budsz = BUDSIZE(size, tp->dmt_budmin);
2750 while (leaf[leafno] == NOFREE) {
2751 /* find the leftmost buddy leaf.
2753 for (w = leafno, bsz = budsz;; bsz <<= 1,
2754 w = (w < bud) ? w : bud) {
2755 if (bsz >= le32_to_cpu(tp->dmt_nleafs)) {
2756 jfs_err("JFS: block map error in dbBackSplit");
2760 /* determine the buddy.
2764 /* check if this buddy is the start of the system.
2766 if (leaf[bud] != NOFREE) {
2767 /* split the leaf at the start of the
2770 cursz = leaf[bud] - 1;
2771 dbSplit(tp, bud, cursz, cursz);
2777 if (leaf[leafno] != size) {
2778 jfs_err("JFS: wrong leaf value in dbBackSplit");
2788 * FUNCTION: update the leaf of a dmtree with a new value, joining
2789 * the leaf with other leaves of the dmtree into a multi-leaf
2790 * binary buddy system, as required.
2793 * tp - pointer to the tree containing the leaf.
2794 * leafno - the number of the leaf to be updated.
2795 * newval - the new value for the leaf.
2797 * RETURN VALUES: none
2799 static int dbJoin(dmtree_t * tp, int leafno, int newval)
2804 /* can the new leaf value require a join with other leaves ?
2806 if (newval >= tp->dmt_budmin) {
2807 /* pickup a pointer to the leaves of the tree.
2809 leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2811 /* try to join the specified leaf into a large binary
2812 * buddy system. the join proceeds by attempting to join
2813 * the specified leafno with its buddy (leaf) at new value.
2814 * if the join occurs, we attempt to join the left leaf
2815 * of the joined buddies with its buddy at new value + 1.
2816 * we continue to join until we find a buddy that cannot be
2817 * joined (does not have a value equal to the size of the
2818 * last join) or until all leaves have been joined into a
2821 * get the buddy size (number of words covered) of
2824 budsz = BUDSIZE(newval, tp->dmt_budmin);
2828 while (budsz < le32_to_cpu(tp->dmt_nleafs)) {
2829 /* get the buddy leaf.
2831 buddy = leafno ^ budsz;
2833 /* if the leaf's new value is greater than its
2834 * buddy's value, we join no more.
2836 if (newval > leaf[buddy])
2839 /* It shouldn't be less */
2840 if (newval < leaf[buddy])
2843 /* check which (leafno or buddy) is the left buddy.
2844 * the left buddy gets to claim the blocks resulting
2845 * from the join while the right gets to claim none.
2846 * the left buddy is also eligible to participate in
2847 * a join at the next higher level while the right
2851 if (leafno < buddy) {
2852 /* leafno is the left buddy.
2854 dbAdjTree(tp, buddy, NOFREE);
2856 /* buddy is the left buddy and becomes
2859 dbAdjTree(tp, leafno, NOFREE);
2863 /* on to try the next join.
2870 /* update the leaf value.
2872 dbAdjTree(tp, leafno, newval);
2881 * FUNCTION: update a leaf of a dmtree with a new value, adjusting
2882 * the dmtree, as required, to reflect the new leaf value.
2883 * the combination of any buddies must already be done before
2887 * tp - pointer to the tree to be adjusted.
2888 * leafno - the number of the leaf to be updated.
2889 * newval - the new value for the leaf.
2891 * RETURN VALUES: none
2893 static void dbAdjTree(dmtree_t * tp, int leafno, int newval)
2898 /* pick up the index of the leaf for this leafno.
2900 lp = leafno + le32_to_cpu(tp->dmt_leafidx);
2902 /* is the current value the same as the old value ? if so,
2903 * there is nothing to do.
2905 if (tp->dmt_stree[lp] == newval)
2908 /* set the new value.
2910 tp->dmt_stree[lp] = newval;
2912 /* bubble the new value up the tree as required.
2914 for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) {
2915 /* get the index of the first leaf of the 4 leaf
2916 * group containing the specified leaf (leafno).
2918 lp = ((lp - 1) & ~0x03) + 1;
2920 /* get the index of the parent of this 4 leaf group.
2924 /* determine the maximum of the 4 leaves.
2926 max = TREEMAX(&tp->dmt_stree[lp]);
2928 /* if the maximum of the 4 is the same as the
2929 * parent's value, we're done.
2931 if (tp->dmt_stree[pp] == max)
2934 /* parent gets new value.
2936 tp->dmt_stree[pp] = max;
2938 /* parent becomes leaf for next go-round.
2946 * NAME: dbFindLeaf()
2948 * FUNCTION: search a dmtree_t for sufficient free blocks, returning
2949 * the index of a leaf describing the free blocks if
2950 * sufficient free blocks are found.
2952 * the search starts at the top of the dmtree_t tree and
2953 * proceeds down the tree to the leftmost leaf with sufficient
2957 * tp - pointer to the tree to be searched.
2958 * l2nb - log2 number of free blocks to search for.
2959 * leafidx - return pointer to be set to the index of the leaf
2960 * describing at least l2nb free blocks if sufficient
2961 * free blocks are found.
2965 * -ENOSPC - insufficient free blocks.
2967 static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx)
2969 int ti, n = 0, k, x = 0;
2971 /* first check the root of the tree to see if there is
2972 * sufficient free space.
2974 if (l2nb > tp->dmt_stree[ROOT])
2977 /* sufficient free space available. now search down the tree
2978 * starting at the next level for the leftmost leaf that
2979 * describes sufficient free space.
2981 for (k = le32_to_cpu(tp->dmt_height), ti = 1;
2982 k > 0; k--, ti = ((ti + n) << 2) + 1) {
2983 /* search the four nodes at this level, starting from
2986 for (x = ti, n = 0; n < 4; n++) {
2987 /* sufficient free space found. move to the next
2988 * level (or quit if this is the last level).
2990 if (l2nb <= tp->dmt_stree[x + n])
2994 /* better have found something since the higher
2995 * levels of the tree said it was here.
3000 /* set the return to the leftmost leaf describing sufficient
3003 *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx);
3010 * NAME: dbFindBits()
3012 * FUNCTION: find a specified number of binary buddy free bits within a
3013 * dmap bitmap word value.
3015 * this routine searches the bitmap value for (1 << l2nb) free
3016 * bits at (1 << l2nb) alignments within the value.
3019 * word - dmap bitmap word value.
3020 * l2nb - number of free bits specified as a log2 number.
3023 * starting bit number of free bits.
3025 static int dbFindBits(u32 word, int l2nb)
3030 /* get the number of bits.
3033 assert(nb <= DBWORD);
3035 /* complement the word so we can use a mask (i.e. 0s represent
3036 * free bits) and compute the mask.
3039 mask = ONES << (DBWORD - nb);
3041 /* scan the word for nb free bits at nb alignments.
3043 for (bitno = 0; mask != 0; bitno += nb, mask >>= nb) {
3044 if ((mask & word) == mask)
3050 /* return the bit number.
3057 * NAME: dbMaxBud(u8 *cp)
3059 * FUNCTION: determine the largest binary buddy string of free
3060 * bits within 32-bits of the map.
3063 * cp - pointer to the 32-bit value.
3066 * largest binary buddy of free bits within a dmap word.
3068 static int dbMaxBud(u8 * cp)
3070 signed char tmp1, tmp2;
3072 /* check if the wmap word is all free. if so, the
3073 * free buddy size is BUDMIN.
3075 if (*((uint *) cp) == 0)
3078 /* check if the wmap word is half free. if so, the
3079 * free buddy size is BUDMIN-1.
3081 if (*((u16 *) cp) == 0 || *((u16 *) cp + 1) == 0)
3082 return (BUDMIN - 1);
3084 /* not all free or half free. determine the free buddy
3085 * size thru table lookup using quarters of the wmap word.
3087 tmp1 = max(budtab[cp[2]], budtab[cp[3]]);
3088 tmp2 = max(budtab[cp[0]], budtab[cp[1]]);
3089 return (max(tmp1, tmp2));
3094 * NAME: cnttz(uint word)
3096 * FUNCTION: determine the number of trailing zeros within a 32-bit
3100 * value - 32-bit value to be examined.
3103 * count of trailing zeros
3105 static int cnttz(u32 word)
3109 for (n = 0; n < 32; n++, word >>= 1) {
3119 * NAME: cntlz(u32 value)
3121 * FUNCTION: determine the number of leading zeros within a 32-bit
3125 * value - 32-bit value to be examined.
3128 * count of leading zeros
3130 static int cntlz(u32 value)
3134 for (n = 0; n < 32; n++, value <<= 1) {
3135 if (value & HIGHORDER)
3143 * NAME: blkstol2(s64 nb)
3145 * FUNCTION: convert a block count to its log2 value. if the block
3146 * count is not a l2 multiple, it is rounded up to the next
3147 * larger l2 multiple.
3150 * nb - number of blocks
3153 * log2 number of blocks
3155 static int blkstol2(s64 nb)
3158 s64 mask; /* meant to be signed */
3160 mask = (s64) 1 << (64 - 1);
3162 /* count the leading bits.
3164 for (l2nb = 0; l2nb < 64; l2nb++, mask >>= 1) {
3165 /* leading bit found.
3168 /* determine the l2 value.
3170 l2nb = (64 - 1) - l2nb;
3172 /* check if we need to round up.
3181 return 0; /* fix compiler warning */
3186 * NAME: dbAllocBottomUp()
3188 * FUNCTION: alloc the specified block range from the working block
3191 * the blocks will be alloc from the working map one dmap
3195 * ip - pointer to in-core inode;
3196 * blkno - starting block number to be freed.
3197 * nblocks - number of blocks to be freed.
3203 int dbAllocBottomUp(struct inode *ip, s64 blkno, s64 nblocks)
3205 struct metapage *mp;
3209 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
3210 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
3212 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
3214 /* block to be allocated better be within the mapsize. */
3215 ASSERT(nblocks <= bmp->db_mapsize - blkno);
3218 * allocate the blocks a dmap at a time.
3221 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
3222 /* release previous dmap if any */
3227 /* get the buffer for the current dmap. */
3228 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
3229 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
3231 IREAD_UNLOCK(ipbmap);
3234 dp = (struct dmap *) mp->data;
3236 /* determine the number of blocks to be allocated from
3239 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
3241 /* allocate the blocks. */
3242 if ((rc = dbAllocDmapBU(bmp, dp, blkno, nb))) {
3243 release_metapage(mp);
3244 IREAD_UNLOCK(ipbmap);
3249 /* write the last buffer. */
3252 IREAD_UNLOCK(ipbmap);
3258 static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
3262 int dbitno, word, rembits, nb, nwords, wbitno, agno;
3264 struct dmaptree *tp = (struct dmaptree *) & dp->tree;
3266 /* save the current value of the root (i.e. maximum free string)
3269 oldroot = tp->stree[ROOT];
3271 /* determine the bit number and word within the dmap of the
3274 dbitno = blkno & (BPERDMAP - 1);
3275 word = dbitno >> L2DBWORD;
3277 /* block range better be within the dmap */
3278 assert(dbitno + nblocks <= BPERDMAP);
3280 /* allocate the bits of the dmap's words corresponding to the block
3281 * range. not all bits of the first and last words may be contained
3282 * within the block range. if this is the case, we'll work against
3283 * those words (i.e. partial first and/or last) on an individual basis
3284 * (a single pass), allocating the bits of interest by hand and
3285 * updating the leaf corresponding to the dmap word. a single pass
3286 * will be used for all dmap words fully contained within the
3287 * specified range. within this pass, the bits of all fully contained
3288 * dmap words will be marked as free in a single shot and the leaves
3289 * will be updated. a single leaf may describe the free space of
3290 * multiple dmap words, so we may update only a subset of the actual
3291 * leaves corresponding to the dmap words of the block range.
3293 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
3294 /* determine the bit number within the word and
3295 * the number of bits within the word.
3297 wbitno = dbitno & (DBWORD - 1);
3298 nb = min(rembits, DBWORD - wbitno);
3300 /* check if only part of a word is to be allocated.
3303 /* allocate (set to 1) the appropriate bits within
3306 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
3311 /* one or more dmap words are fully contained
3312 * within the block range. determine how many
3313 * words and allocate (set to 1) the bits of these
3316 nwords = rembits >> L2DBWORD;
3317 memset(&dp->wmap[word], (int) ONES, nwords * 4);
3319 /* determine how many bits */
3320 nb = nwords << L2DBWORD;
3325 /* update the free count for this dmap */
3326 le32_add_cpu(&dp->nfree, -nblocks);
3328 /* reconstruct summary tree */
3333 /* if this allocation group is completely free,
3334 * update the highest active allocation group number
3335 * if this allocation group is the new max.
3337 agno = blkno >> bmp->db_agl2size;
3338 if (agno > bmp->db_maxag)
3339 bmp->db_maxag = agno;
3341 /* update the free count for the allocation group and map */
3342 bmp->db_agfree[agno] -= nblocks;
3343 bmp->db_nfree -= nblocks;
3347 /* if the root has not changed, done. */
3348 if (tp->stree[ROOT] == oldroot)
3351 /* root changed. bubble the change up to the dmap control pages.
3352 * if the adjustment of the upper level control pages fails,
3353 * backout the bit allocation (thus making everything consistent).
3355 if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0)))
3356 dbFreeBits(bmp, dp, blkno, nblocks);
3363 * NAME: dbExtendFS()
3365 * FUNCTION: extend bmap from blkno for nblocks;
3366 * dbExtendFS() updates bmap ready for dbAllocBottomUp();
3370 * L1---------------------------------L1
3372 * L0---------L0---------L0 L0---------L0---------L0
3374 * d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,.,dm;
3375 * L2L1L0d0,...,dnL0d0,...,dnL0d0,...,dnL1L0d0,...,dnL0d0,...,dnL0d0,..dm
3377 * <---old---><----------------------------extend----------------------->
3379 int dbExtendFS(struct inode *ipbmap, s64 blkno, s64 nblocks)
3381 struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb);
3382 int nbperpage = sbi->nbperpage;
3383 int i, i0 = true, j, j0 = true, k, n;
3386 struct metapage *mp, *l2mp, *l1mp = NULL, *l0mp = NULL;
3387 struct dmapctl *l2dcp, *l1dcp, *l0dcp;
3389 s8 *l0leaf, *l1leaf, *l2leaf;
3390 struct bmap *bmp = sbi->bmap;
3391 int agno, l2agsize, oldl2agsize;
3394 newsize = blkno + nblocks;
3396 jfs_info("dbExtendFS: blkno:%Ld nblocks:%Ld newsize:%Ld",
3397 (long long) blkno, (long long) nblocks, (long long) newsize);
3400 * initialize bmap control page.
3402 * all the data in bmap control page should exclude
3403 * the mkfs hidden dmap page.
3406 /* update mapsize */
3407 bmp->db_mapsize = newsize;
3408 bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize);
3410 /* compute new AG size */
3411 l2agsize = dbGetL2AGSize(newsize);
3412 oldl2agsize = bmp->db_agl2size;
3414 bmp->db_agl2size = l2agsize;
3415 bmp->db_agsize = 1 << l2agsize;
3417 /* compute new number of AG */
3418 agno = bmp->db_numag;
3419 bmp->db_numag = newsize >> l2agsize;
3420 bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0;
3423 * reconfigure db_agfree[]
3424 * from old AG configuration to new AG configuration;
3426 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
3427 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
3428 * note: new AG size = old AG size * (2**x).
3430 if (l2agsize == oldl2agsize)
3432 k = 1 << (l2agsize - oldl2agsize);
3433 ag_rem = bmp->db_agfree[0]; /* save agfree[0] */
3434 for (i = 0, n = 0; i < agno; n++) {
3435 bmp->db_agfree[n] = 0; /* init collection point */
3437 /* coalesce contiguous k AGs; */
3438 for (j = 0; j < k && i < agno; j++, i++) {
3439 /* merge AGi to AGn */
3440 bmp->db_agfree[n] += bmp->db_agfree[i];
3443 bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */
3445 for (; n < MAXAG; n++)
3446 bmp->db_agfree[n] = 0;
3449 * update highest active ag number
3452 bmp->db_maxag = bmp->db_maxag / k;
3457 * update bit maps and corresponding level control pages;
3458 * global control page db_nfree, db_agfree[agno], db_maxfreebud;
3462 p = BMAPBLKNO + nbperpage; /* L2 page */
3463 l2mp = read_metapage(ipbmap, p, PSIZE, 0);
3465 jfs_error(ipbmap->i_sb, "L2 page could not be read\n");
3468 l2dcp = (struct dmapctl *) l2mp->data;
3470 /* compute start L1 */
3471 k = blkno >> L2MAXL1SIZE;
3472 l2leaf = l2dcp->stree + CTLLEAFIND + k;
3473 p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */
3476 * extend each L1 in L2
3478 for (; k < LPERCTL; k++, p += nbperpage) {
3481 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */
3482 l1mp = read_metapage(ipbmap, p, PSIZE, 0);
3485 l1dcp = (struct dmapctl *) l1mp->data;
3487 /* compute start L0 */
3488 j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE;
3489 l1leaf = l1dcp->stree + CTLLEAFIND + j;
3490 p = BLKTOL0(blkno, sbi->l2nbperpage);
3493 /* assign/init L1 page */
3494 l1mp = get_metapage(ipbmap, p, PSIZE, 0);
3498 l1dcp = (struct dmapctl *) l1mp->data;
3500 /* compute start L0 */
3502 l1leaf = l1dcp->stree + CTLLEAFIND;
3503 p += nbperpage; /* 1st L0 of L1.k */
3507 * extend each L0 in L1
3509 for (; j < LPERCTL; j++) {
3512 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */
3514 l0mp = read_metapage(ipbmap, p, PSIZE, 0);
3517 l0dcp = (struct dmapctl *) l0mp->data;
3519 /* compute start dmap */
3520 i = (blkno & (MAXL0SIZE - 1)) >>
3522 l0leaf = l0dcp->stree + CTLLEAFIND + i;
3523 p = BLKTODMAP(blkno,
3527 /* assign/init L0 page */
3528 l0mp = get_metapage(ipbmap, p, PSIZE, 0);
3532 l0dcp = (struct dmapctl *) l0mp->data;
3534 /* compute start dmap */
3536 l0leaf = l0dcp->stree + CTLLEAFIND;
3537 p += nbperpage; /* 1st dmap of L0.j */
3541 * extend each dmap in L0
3543 for (; i < LPERCTL; i++) {
3545 * reconstruct the dmap page, and
3546 * initialize corresponding parent L0 leaf
3548 if ((n = blkno & (BPERDMAP - 1))) {
3549 /* read in dmap page: */
3550 mp = read_metapage(ipbmap, p,
3554 n = min(nblocks, (s64)BPERDMAP - n);
3556 /* assign/init dmap page */
3557 mp = read_metapage(ipbmap, p,
3562 n = min_t(s64, nblocks, BPERDMAP);
3565 dp = (struct dmap *) mp->data;
3566 *l0leaf = dbInitDmap(dp, blkno, n);
3569 agno = le64_to_cpu(dp->start) >> l2agsize;
3570 bmp->db_agfree[agno] += n;
3581 } /* for each dmap in a L0 */
3584 * build current L0 page from its leaves, and
3585 * initialize corresponding parent L1 leaf
3587 *l1leaf = dbInitDmapCtl(l0dcp, 0, ++i);
3588 write_metapage(l0mp);
3592 l1leaf++; /* continue for next L0 */
3594 /* more than 1 L0 ? */
3596 break; /* build L1 page */
3598 /* summarize in global bmap page */
3599 bmp->db_maxfreebud = *l1leaf;
3600 release_metapage(l1mp);
3601 release_metapage(l2mp);
3605 } /* for each L0 in a L1 */
3608 * build current L1 page from its leaves, and
3609 * initialize corresponding parent L2 leaf
3611 *l2leaf = dbInitDmapCtl(l1dcp, 1, ++j);
3612 write_metapage(l1mp);
3616 l2leaf++; /* continue for next L1 */
3618 /* more than 1 L1 ? */
3620 break; /* build L2 page */
3622 /* summarize in global bmap page */
3623 bmp->db_maxfreebud = *l2leaf;
3624 release_metapage(l2mp);
3628 } /* for each L1 in a L2 */
3630 jfs_error(ipbmap->i_sb, "function has not returned as expected\n");
3633 release_metapage(l0mp);
3635 release_metapage(l1mp);
3636 release_metapage(l2mp);
3640 * finalize bmap control page
3651 void dbFinalizeBmap(struct inode *ipbmap)
3653 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
3654 int actags, inactags, l2nl;
3655 s64 ag_rem, actfree, inactfree, avgfree;
3659 * finalize bmap control page
3663 * compute db_agpref: preferred ag to allocate from
3664 * (the leftmost ag with average free space in it);
3667 /* get the number of active ags and inacitve ags */
3668 actags = bmp->db_maxag + 1;
3669 inactags = bmp->db_numag - actags;
3670 ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */
3672 /* determine how many blocks are in the inactive allocation
3673 * groups. in doing this, we must account for the fact that
3674 * the rightmost group might be a partial group (i.e. file
3675 * system size is not a multiple of the group size).
3677 inactfree = (inactags && ag_rem) ?
3678 ((inactags - 1) << bmp->db_agl2size) + ag_rem
3679 : inactags << bmp->db_agl2size;
3681 /* determine how many free blocks are in the active
3682 * allocation groups plus the average number of free blocks
3683 * within the active ags.
3685 actfree = bmp->db_nfree - inactfree;
3686 avgfree = (u32) actfree / (u32) actags;
3688 /* if the preferred allocation group has not average free space.
3689 * re-establish the preferred group as the leftmost
3690 * group with average free space.
3692 if (bmp->db_agfree[bmp->db_agpref] < avgfree) {
3693 for (bmp->db_agpref = 0; bmp->db_agpref < actags;
3695 if (bmp->db_agfree[bmp->db_agpref] >= avgfree)
3698 if (bmp->db_agpref >= bmp->db_numag) {
3699 jfs_error(ipbmap->i_sb,
3700 "cannot find ag with average freespace\n");
3705 * compute db_aglevel, db_agheight, db_width, db_agstart:
3706 * an ag is covered in aglevel dmapctl summary tree,
3707 * at agheight level height (from leaf) with agwidth number of nodes
3708 * each, which starts at agstart index node of the smmary tree node
3711 bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize);
3713 bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL);
3714 bmp->db_agheight = l2nl >> 1;
3715 bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1));
3716 for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0;
3718 bmp->db_agstart += n;
3726 * NAME: dbInitDmap()/ujfs_idmap_page()
3728 * FUNCTION: initialize working/persistent bitmap of the dmap page
3729 * for the specified number of blocks:
3731 * at entry, the bitmaps had been initialized as free (ZEROS);
3732 * The number of blocks will only account for the actually
3733 * existing blocks. Blocks which don't actually exist in
3734 * the aggregate will be marked as allocated (ONES);
3737 * dp - pointer to page of map
3738 * nblocks - number of blocks this page
3742 static int dbInitDmap(struct dmap * dp, s64 Blkno, int nblocks)
3744 int blkno, w, b, r, nw, nb, i;
3746 /* starting block number within the dmap */
3747 blkno = Blkno & (BPERDMAP - 1);
3750 dp->nblocks = dp->nfree = cpu_to_le32(nblocks);
3751 dp->start = cpu_to_le64(Blkno);
3753 if (nblocks == BPERDMAP) {
3754 memset(&dp->wmap[0], 0, LPERDMAP * 4);
3755 memset(&dp->pmap[0], 0, LPERDMAP * 4);
3759 le32_add_cpu(&dp->nblocks, nblocks);
3760 le32_add_cpu(&dp->nfree, nblocks);
3763 /* word number containing start block number */
3764 w = blkno >> L2DBWORD;
3767 * free the bits corresponding to the block range (ZEROS):
3768 * note: not all bits of the first and last words may be contained
3769 * within the block range.
3771 for (r = nblocks; r > 0; r -= nb, blkno += nb) {
3772 /* number of bits preceding range to be freed in the word */
3773 b = blkno & (DBWORD - 1);
3774 /* number of bits to free in the word */
3775 nb = min(r, DBWORD - b);
3777 /* is partial word to be freed ? */
3779 /* free (set to 0) from the bitmap word */
3780 dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3782 dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3785 /* skip the word freed */
3788 /* free (set to 0) contiguous bitmap words */
3790 memset(&dp->wmap[w], 0, nw * 4);
3791 memset(&dp->pmap[w], 0, nw * 4);
3793 /* skip the words freed */
3794 nb = nw << L2DBWORD;
3800 * mark bits following the range to be freed (non-existing
3801 * blocks) as allocated (ONES)
3804 if (blkno == BPERDMAP)
3807 /* the first word beyond the end of existing blocks */
3808 w = blkno >> L2DBWORD;
3810 /* does nblocks fall on a 32-bit boundary ? */
3811 b = blkno & (DBWORD - 1);
3813 /* mark a partial word allocated */
3814 dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b);
3818 /* set the rest of the words in the page to allocated (ONES) */
3819 for (i = w; i < LPERDMAP; i++)
3820 dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES);
3826 return (dbInitDmapTree(dp));
3831 * NAME: dbInitDmapTree()/ujfs_complete_dmap()
3833 * FUNCTION: initialize summary tree of the specified dmap:
3835 * at entry, bitmap of the dmap has been initialized;
3838 * dp - dmap to complete
3839 * blkno - starting block number for this dmap
3840 * treemax - will be filled in with max free for this dmap
3842 * RETURNS: max free string at the root of the tree
3844 static int dbInitDmapTree(struct dmap * dp)
3846 struct dmaptree *tp;
3850 /* init fixed info of tree */
3852 tp->nleafs = cpu_to_le32(LPERDMAP);
3853 tp->l2nleafs = cpu_to_le32(L2LPERDMAP);
3854 tp->leafidx = cpu_to_le32(LEAFIND);
3855 tp->height = cpu_to_le32(4);
3856 tp->budmin = BUDMIN;
3858 /* init each leaf from corresponding wmap word:
3859 * note: leaf is set to NOFREE(-1) if all blocks of corresponding
3860 * bitmap word are allocated.
3862 cp = tp->stree + le32_to_cpu(tp->leafidx);
3863 for (i = 0; i < LPERDMAP; i++)
3864 *cp++ = dbMaxBud((u8 *) & dp->wmap[i]);
3866 /* build the dmap's binary buddy summary tree */
3867 return (dbInitTree(tp));
3872 * NAME: dbInitTree()/ujfs_adjtree()
3874 * FUNCTION: initialize binary buddy summary tree of a dmap or dmapctl.
3876 * at entry, the leaves of the tree has been initialized
3877 * from corresponding bitmap word or root of summary tree
3878 * of the child control page;
3879 * configure binary buddy system at the leaf level, then
3880 * bubble up the values of the leaf nodes up the tree.
3883 * cp - Pointer to the root of the tree
3884 * l2leaves- Number of leaf nodes as a power of 2
3885 * l2min - Number of blocks that can be covered by a leaf
3888 * RETURNS: max free string at the root of the tree
3890 static int dbInitTree(struct dmaptree * dtp)
3892 int l2max, l2free, bsize, nextb, i;
3893 int child, parent, nparent;
3898 /* Determine the maximum free string possible for the leaves */
3899 l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin;
3902 * configure the leaf levevl into binary buddy system
3904 * Try to combine buddies starting with a buddy size of 1
3905 * (i.e. two leaves). At a buddy size of 1 two buddy leaves
3906 * can be combined if both buddies have a maximum free of l2min;
3907 * the combination will result in the left-most buddy leaf having
3908 * a maximum free of l2min+1.
3909 * After processing all buddies for a given size, process buddies
3910 * at the next higher buddy size (i.e. current size * 2) and
3911 * the next maximum free (current free + 1).
3912 * This continues until the maximum possible buddy combination
3913 * yields maximum free.
3915 for (l2free = dtp->budmin, bsize = 1; l2free < l2max;
3916 l2free++, bsize = nextb) {
3917 /* get next buddy size == current buddy pair size */
3920 /* scan each adjacent buddy pair at current buddy size */
3921 for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx);
3922 i < le32_to_cpu(dtp->nleafs);
3923 i += nextb, cp += nextb) {
3924 /* coalesce if both adjacent buddies are max free */
3925 if (*cp == l2free && *(cp + bsize) == l2free) {
3926 *cp = l2free + 1; /* left take right */
3927 *(cp + bsize) = -1; /* right give left */
3933 * bubble summary information of leaves up the tree.
3935 * Starting at the leaf node level, the four nodes described by
3936 * the higher level parent node are compared for a maximum free and
3937 * this maximum becomes the value of the parent node.
3938 * when all lower level nodes are processed in this fashion then
3939 * move up to the next level (parent becomes a lower level node) and
3940 * continue the process for that level.
3942 for (child = le32_to_cpu(dtp->leafidx),
3943 nparent = le32_to_cpu(dtp->nleafs) >> 2;
3944 nparent > 0; nparent >>= 2, child = parent) {
3945 /* get index of 1st node of parent level */
3946 parent = (child - 1) >> 2;
3948 /* set the value of the parent node as the maximum
3949 * of the four nodes of the current level.
3951 for (i = 0, cp = tp + child, cp1 = tp + parent;
3952 i < nparent; i++, cp += 4, cp1++)
3963 * function: initialize dmapctl page
3965 static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i)
3966 { /* start leaf index not covered by range */
3969 dcp->nleafs = cpu_to_le32(LPERCTL);
3970 dcp->l2nleafs = cpu_to_le32(L2LPERCTL);
3971 dcp->leafidx = cpu_to_le32(CTLLEAFIND);
3972 dcp->height = cpu_to_le32(5);
3973 dcp->budmin = L2BPERDMAP + L2LPERCTL * level;
3976 * initialize the leaves of current level that were not covered
3977 * by the specified input block range (i.e. the leaves have no
3978 * low level dmapctl or dmap).
3980 cp = &dcp->stree[CTLLEAFIND + i];
3981 for (; i < LPERCTL; i++)
3984 /* build the dmap's binary buddy summary tree */
3985 return (dbInitTree((struct dmaptree *) dcp));
3990 * NAME: dbGetL2AGSize()/ujfs_getagl2size()
3992 * FUNCTION: Determine log2(allocation group size) from aggregate size
3995 * nblocks - Number of blocks in aggregate
3997 * RETURNS: log2(allocation group size) in aggregate blocks
3999 static int dbGetL2AGSize(s64 nblocks)
4005 if (nblocks < BPERDMAP * MAXAG)
4006 return (L2BPERDMAP);
4008 /* round up aggregate size to power of 2 */
4009 m = ((u64) 1 << (64 - 1));
4010 for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) {
4015 sz = (s64) 1 << l2sz;
4019 /* agsize = roundupSize/max_number_of_ag */
4020 return (l2sz - L2MAXAG);
4025 * NAME: dbMapFileSizeToMapSize()
4027 * FUNCTION: compute number of blocks the block allocation map file
4028 * can cover from the map file size;
4030 * RETURNS: Number of blocks which can be covered by this block map file;
4034 * maximum number of map pages at each level including control pages
4036 #define MAXL0PAGES (1 + LPERCTL)
4037 #define MAXL1PAGES (1 + LPERCTL * MAXL0PAGES)
4040 * convert number of map pages to the zero origin top dmapctl level
4042 #define BMAPPGTOLEV(npages) \
4043 (((npages) <= 3 + MAXL0PAGES) ? 0 : \
4044 ((npages) <= 2 + MAXL1PAGES) ? 1 : 2)
4046 s64 dbMapFileSizeToMapSize(struct inode * ipbmap)
4048 struct super_block *sb = ipbmap->i_sb;
4052 int complete, factor;
4054 nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize;
4055 npages = nblocks >> JFS_SBI(sb)->l2nbperpage;
4056 level = BMAPPGTOLEV(npages);
4058 /* At each level, accumulate the number of dmap pages covered by
4059 * the number of full child levels below it;
4060 * repeat for the last incomplete child level.
4063 npages--; /* skip the first global control page */
4064 /* skip higher level control pages above top level covered by map */
4065 npages -= (2 - level);
4066 npages--; /* skip top level's control page */
4067 for (i = level; i >= 0; i--) {
4069 (i == 2) ? MAXL1PAGES : ((i == 1) ? MAXL0PAGES : 1);
4070 complete = (u32) npages / factor;
4071 ndmaps += complete * ((i == 2) ? LPERCTL * LPERCTL :
4072 ((i == 1) ? LPERCTL : 1));
4074 /* pages in last/incomplete child */
4075 npages = (u32) npages % factor;
4076 /* skip incomplete child's level control page */
4080 /* convert the number of dmaps into the number of blocks
4081 * which can be covered by the dmaps;
4083 nblocks = ndmaps << L2BPERDMAP;