2 * Copyright (C) International Business Machines Corp., 2000-2004
3 * Portions Copyright (C) Tino Reichardt, 2012
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/slab.h>
22 #include "jfs_incore.h"
23 #include "jfs_superblock.h"
27 #include "jfs_metapage.h"
28 #include "jfs_debug.h"
29 #include "jfs_discard.h"
32 * SERIALIZATION of the Block Allocation Map.
34 * the working state of the block allocation map is accessed in
37 * 1) allocation and free requests that start at the dmap
38 * level and move up through the dmap control pages (i.e.
39 * the vast majority of requests).
41 * 2) allocation requests that start at dmap control page
42 * level and work down towards the dmaps.
44 * the serialization scheme used here is as follows.
46 * requests which start at the bottom are serialized against each
47 * other through buffers and each requests holds onto its buffers
48 * as it works it way up from a single dmap to the required level
49 * of dmap control page.
50 * requests that start at the top are serialized against each other
51 * and request that start from the bottom by the multiple read/single
52 * write inode lock of the bmap inode. requests starting at the top
53 * take this lock in write mode while request starting at the bottom
54 * take the lock in read mode. a single top-down request may proceed
55 * exclusively while multiple bottoms-up requests may proceed
56 * simultaneously (under the protection of busy buffers).
58 * in addition to information found in dmaps and dmap control pages,
59 * the working state of the block allocation map also includes read/
60 * write information maintained in the bmap descriptor (i.e. total
61 * free block count, allocation group level free block counts).
62 * a single exclusive lock (BMAP_LOCK) is used to guard this information
63 * in the face of multiple-bottoms up requests.
64 * (lock ordering: IREAD_LOCK, BMAP_LOCK);
66 * accesses to the persistent state of the block allocation map (limited
67 * to the persistent bitmaps in dmaps) is guarded by (busy) buffers.
70 #define BMAP_LOCK_INIT(bmp) mutex_init(&bmp->db_bmaplock)
71 #define BMAP_LOCK(bmp) mutex_lock(&bmp->db_bmaplock)
72 #define BMAP_UNLOCK(bmp) mutex_unlock(&bmp->db_bmaplock)
77 static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
79 static void dbSplit(dmtree_t *tp, int leafno, int splitsz, int newval, bool is_ctl);
80 static int dbBackSplit(dmtree_t *tp, int leafno, bool is_ctl);
81 static int dbJoin(dmtree_t *tp, int leafno, int newval, bool is_ctl);
82 static void dbAdjTree(dmtree_t *tp, int leafno, int newval, bool is_ctl);
83 static int dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc,
85 static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results);
86 static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
88 static int dbAllocNear(struct bmap * bmp, struct dmap * dp, s64 blkno,
90 int l2nb, s64 * results);
91 static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
93 static int dbAllocDmapLev(struct bmap * bmp, struct dmap * dp, int nblocks,
96 static int dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb,
98 static int dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno,
100 static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks);
101 static int dbFindBits(u32 word, int l2nb);
102 static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno);
103 static int dbFindLeaf(dmtree_t *tp, int l2nb, int *leafidx, bool is_ctl);
104 static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
106 static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
108 static int dbMaxBud(u8 * cp);
109 static int blkstol2(s64 nb);
111 static int cntlz(u32 value);
112 static int cnttz(u32 word);
114 static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
116 static int dbInitDmap(struct dmap * dp, s64 blkno, int nblocks);
117 static int dbInitDmapTree(struct dmap * dp);
118 static int dbInitTree(struct dmaptree * dtp);
119 static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i);
120 static int dbGetL2AGSize(s64 nblocks);
125 * table used for determining buddy sizes within characters of
126 * dmap bitmap words. the characters themselves serve as indexes
127 * into the table, with the table elements yielding the maximum
128 * binary buddy of free bits within the character.
130 static const s8 budtab[256] = {
131 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
132 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
133 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
134 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
135 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
136 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
137 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
138 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
139 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
140 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
141 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
142 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
143 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
144 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
145 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
146 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1
152 * FUNCTION: initializate the block allocation map.
154 * memory is allocated for the in-core bmap descriptor and
155 * the in-core descriptor is initialized from disk.
158 * ipbmap - pointer to in-core inode for the block map.
162 * -ENOMEM - insufficient memory
164 * -EINVAL - wrong bmap data
166 int dbMount(struct inode *ipbmap)
169 struct dbmap_disk *dbmp_le;
174 * allocate/initialize the in-memory bmap descriptor
176 /* allocate memory for the in-memory bmap descriptor */
177 bmp = kmalloc(sizeof(struct bmap), GFP_KERNEL);
181 /* read the on-disk bmap descriptor. */
182 mp = read_metapage(ipbmap,
183 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
190 /* copy the on-disk bmap descriptor to its in-memory version. */
191 dbmp_le = (struct dbmap_disk *) mp->data;
192 bmp->db_mapsize = le64_to_cpu(dbmp_le->dn_mapsize);
193 bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree);
195 bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
196 if (bmp->db_l2nbperpage > L2PSIZE - L2MINBLOCKSIZE ||
197 bmp->db_l2nbperpage < 0) {
199 goto err_release_metapage;
202 bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
203 if (!bmp->db_numag) {
205 goto err_release_metapage;
208 bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel);
209 bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag);
210 bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref);
211 if (bmp->db_maxag >= MAXAG || bmp->db_maxag < 0 ||
212 bmp->db_agpref >= MAXAG || bmp->db_agpref < 0) {
214 goto err_release_metapage;
217 bmp->db_aglevel = le32_to_cpu(dbmp_le->dn_aglevel);
218 bmp->db_agheight = le32_to_cpu(dbmp_le->dn_agheight);
219 bmp->db_agwidth = le32_to_cpu(dbmp_le->dn_agwidth);
220 bmp->db_agstart = le32_to_cpu(dbmp_le->dn_agstart);
221 bmp->db_agl2size = le32_to_cpu(dbmp_le->dn_agl2size);
222 if (bmp->db_agl2size > L2MAXL2SIZE - L2MAXAG ||
223 bmp->db_agl2size < 0) {
225 goto err_release_metapage;
228 if (((bmp->db_mapsize - 1) >> bmp->db_agl2size) > MAXAG) {
230 goto err_release_metapage;
233 for (i = 0; i < MAXAG; i++)
234 bmp->db_agfree[i] = le64_to_cpu(dbmp_le->dn_agfree[i]);
235 bmp->db_agsize = le64_to_cpu(dbmp_le->dn_agsize);
236 bmp->db_maxfreebud = dbmp_le->dn_maxfreebud;
238 /* release the buffer. */
239 release_metapage(mp);
241 /* bind the bmap inode and the bmap descriptor to each other. */
242 bmp->db_ipbmap = ipbmap;
243 JFS_SBI(ipbmap->i_sb)->bmap = bmp;
245 memset(bmp->db_active, 0, sizeof(bmp->db_active));
248 * allocate/initialize the bmap lock
254 err_release_metapage:
255 release_metapage(mp);
265 * FUNCTION: terminate the block allocation map in preparation for
266 * file system unmount.
268 * the in-core bmap descriptor is written to disk and
269 * the memory for this descriptor is freed.
272 * ipbmap - pointer to in-core inode for the block map.
278 int dbUnmount(struct inode *ipbmap, int mounterror)
280 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
282 if (!(mounterror || isReadOnly(ipbmap)))
286 * Invalidate the page cache buffers
288 truncate_inode_pages(ipbmap->i_mapping, 0);
290 /* free the memory for the in-memory bmap. */
292 JFS_SBI(ipbmap->i_sb)->bmap = NULL;
300 int dbSync(struct inode *ipbmap)
302 struct dbmap_disk *dbmp_le;
303 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
308 * write bmap global control page
310 /* get the buffer for the on-disk bmap descriptor. */
311 mp = read_metapage(ipbmap,
312 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
315 jfs_err("dbSync: read_metapage failed!");
318 /* copy the in-memory version of the bmap to the on-disk version */
319 dbmp_le = (struct dbmap_disk *) mp->data;
320 dbmp_le->dn_mapsize = cpu_to_le64(bmp->db_mapsize);
321 dbmp_le->dn_nfree = cpu_to_le64(bmp->db_nfree);
322 dbmp_le->dn_l2nbperpage = cpu_to_le32(bmp->db_l2nbperpage);
323 dbmp_le->dn_numag = cpu_to_le32(bmp->db_numag);
324 dbmp_le->dn_maxlevel = cpu_to_le32(bmp->db_maxlevel);
325 dbmp_le->dn_maxag = cpu_to_le32(bmp->db_maxag);
326 dbmp_le->dn_agpref = cpu_to_le32(bmp->db_agpref);
327 dbmp_le->dn_aglevel = cpu_to_le32(bmp->db_aglevel);
328 dbmp_le->dn_agheight = cpu_to_le32(bmp->db_agheight);
329 dbmp_le->dn_agwidth = cpu_to_le32(bmp->db_agwidth);
330 dbmp_le->dn_agstart = cpu_to_le32(bmp->db_agstart);
331 dbmp_le->dn_agl2size = cpu_to_le32(bmp->db_agl2size);
332 for (i = 0; i < MAXAG; i++)
333 dbmp_le->dn_agfree[i] = cpu_to_le64(bmp->db_agfree[i]);
334 dbmp_le->dn_agsize = cpu_to_le64(bmp->db_agsize);
335 dbmp_le->dn_maxfreebud = bmp->db_maxfreebud;
337 /* write the buffer */
341 * write out dirty pages of bmap
343 filemap_write_and_wait(ipbmap->i_mapping);
345 diWriteSpecial(ipbmap, 0);
353 * FUNCTION: free the specified block range from the working block
356 * the blocks will be free from the working map one dmap
360 * ip - pointer to in-core inode;
361 * blkno - starting block number to be freed.
362 * nblocks - number of blocks to be freed.
368 int dbFree(struct inode *ip, s64 blkno, s64 nblocks)
374 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
375 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
376 struct super_block *sb = ipbmap->i_sb;
378 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
380 /* block to be freed better be within the mapsize. */
381 if (unlikely((blkno == 0) || (blkno + nblocks > bmp->db_mapsize))) {
382 IREAD_UNLOCK(ipbmap);
383 printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
384 (unsigned long long) blkno,
385 (unsigned long long) nblocks);
386 jfs_error(ip->i_sb, "block to be freed is outside the map\n");
391 * TRIM the blocks, when mounted with discard option
393 if (JFS_SBI(sb)->flag & JFS_DISCARD)
394 if (JFS_SBI(sb)->minblks_trim <= nblocks)
395 jfs_issue_discard(ipbmap, blkno, nblocks);
398 * free the blocks a dmap at a time.
401 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
402 /* release previous dmap if any */
407 /* get the buffer for the current dmap. */
408 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
409 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
411 IREAD_UNLOCK(ipbmap);
414 dp = (struct dmap *) mp->data;
416 /* determine the number of blocks to be freed from
419 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
421 /* free the blocks. */
422 if ((rc = dbFreeDmap(bmp, dp, blkno, nb))) {
423 jfs_error(ip->i_sb, "error in block map\n");
424 release_metapage(mp);
425 IREAD_UNLOCK(ipbmap);
430 /* write the last buffer. */
434 IREAD_UNLOCK(ipbmap);
441 * NAME: dbUpdatePMap()
443 * FUNCTION: update the allocation state (free or allocate) of the
444 * specified block range in the persistent block allocation map.
446 * the blocks will be updated in the persistent map one
450 * ipbmap - pointer to in-core inode for the block map.
451 * free - 'true' if block range is to be freed from the persistent
452 * map; 'false' if it is to be allocated.
453 * blkno - starting block number of the range.
454 * nblocks - number of contiguous blocks in the range.
455 * tblk - transaction block;
462 dbUpdatePMap(struct inode *ipbmap,
463 int free, s64 blkno, s64 nblocks, struct tblock * tblk)
465 int nblks, dbitno, wbitno, rbits;
466 int word, nbits, nwords;
467 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
468 s64 lblkno, rem, lastlblkno;
473 int lsn, difft, diffp;
476 /* the blocks better be within the mapsize. */
477 if (blkno + nblocks > bmp->db_mapsize) {
478 printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
479 (unsigned long long) blkno,
480 (unsigned long long) nblocks);
481 jfs_error(ipbmap->i_sb, "blocks are outside the map\n");
485 /* compute delta of transaction lsn from log syncpt */
487 log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
488 logdiff(difft, lsn, log);
491 * update the block state a dmap at a time.
495 for (rem = nblocks; rem > 0; rem -= nblks, blkno += nblks) {
496 /* get the buffer for the current dmap. */
497 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
498 if (lblkno != lastlblkno) {
503 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE,
507 metapage_wait_for_io(mp);
509 dp = (struct dmap *) mp->data;
511 /* determine the bit number and word within the dmap of
512 * the starting block. also determine how many blocks
513 * are to be updated within this dmap.
515 dbitno = blkno & (BPERDMAP - 1);
516 word = dbitno >> L2DBWORD;
517 nblks = min(rem, (s64)BPERDMAP - dbitno);
519 /* update the bits of the dmap words. the first and last
520 * words may only have a subset of their bits updated. if
521 * this is the case, we'll work against that word (i.e.
522 * partial first and/or last) only in a single pass. a
523 * single pass will also be used to update all words that
524 * are to have all their bits updated.
526 for (rbits = nblks; rbits > 0;
527 rbits -= nbits, dbitno += nbits) {
528 /* determine the bit number within the word and
529 * the number of bits within the word.
531 wbitno = dbitno & (DBWORD - 1);
532 nbits = min(rbits, DBWORD - wbitno);
534 /* check if only part of the word is to be updated. */
535 if (nbits < DBWORD) {
536 /* update (free or allocate) the bits
540 (ONES << (DBWORD - nbits) >> wbitno);
550 /* one or more words are to have all
551 * their bits updated. determine how
552 * many words and how many bits.
554 nwords = rbits >> L2DBWORD;
555 nbits = nwords << L2DBWORD;
557 /* update (free or allocate) the bits
561 memset(&dp->pmap[word], 0,
564 memset(&dp->pmap[word], (int) ONES,
574 if (lblkno == lastlblkno)
579 LOGSYNC_LOCK(log, flags);
581 /* inherit older/smaller lsn */
582 logdiff(diffp, mp->lsn, log);
586 /* move bp after tblock in logsync list */
587 list_move(&mp->synclist, &tblk->synclist);
590 /* inherit younger/larger clsn */
591 logdiff(difft, tblk->clsn, log);
592 logdiff(diffp, mp->clsn, log);
594 mp->clsn = tblk->clsn;
599 /* insert bp after tblock in logsync list */
601 list_add(&mp->synclist, &tblk->synclist);
603 mp->clsn = tblk->clsn;
605 LOGSYNC_UNLOCK(log, flags);
608 /* write the last buffer. */
620 * FUNCTION: find the preferred allocation group for new allocations.
622 * Within the allocation groups, we maintain a preferred
623 * allocation group which consists of a group with at least
624 * average free space. It is the preferred group that we target
625 * new inode allocation towards. The tie-in between inode
626 * allocation and block allocation occurs as we allocate the
627 * first (data) block of an inode and specify the inode (block)
628 * as the allocation hint for this block.
630 * We try to avoid having more than one open file growing in
631 * an allocation group, as this will lead to fragmentation.
632 * This differs from the old OS/2 method of trying to keep
633 * empty ags around for large allocations.
636 * ipbmap - pointer to in-core inode for the block map.
639 * the preferred allocation group number.
641 int dbNextAG(struct inode *ipbmap)
648 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
652 /* determine the average number of free blocks within the ags. */
653 avgfree = (u32)bmp->db_nfree / bmp->db_numag;
656 * if the current preferred ag does not have an active allocator
657 * and has at least average freespace, return it
659 agpref = bmp->db_agpref;
660 if ((atomic_read(&bmp->db_active[agpref]) == 0) &&
661 (bmp->db_agfree[agpref] >= avgfree))
664 /* From the last preferred ag, find the next one with at least
665 * average free space.
667 for (i = 0 ; i < bmp->db_numag; i++, agpref++) {
668 if (agpref == bmp->db_numag)
671 if (atomic_read(&bmp->db_active[agpref]))
672 /* open file is currently growing in this ag */
674 if (bmp->db_agfree[agpref] >= avgfree) {
675 /* Return this one */
676 bmp->db_agpref = agpref;
678 } else if (bmp->db_agfree[agpref] > hwm) {
679 /* Less than avg. freespace, but best so far */
680 hwm = bmp->db_agfree[agpref];
686 * If no inactive ag was found with average freespace, use the
690 bmp->db_agpref = next_best;
691 /* else leave db_agpref unchanged */
695 /* return the preferred group.
697 return (bmp->db_agpref);
703 * FUNCTION: attempt to allocate a specified number of contiguous free
704 * blocks from the working allocation block map.
706 * the block allocation policy uses hints and a multi-step
709 * for allocation requests smaller than the number of blocks
710 * per dmap, we first try to allocate the new blocks
711 * immediately following the hint. if these blocks are not
712 * available, we try to allocate blocks near the hint. if
713 * no blocks near the hint are available, we next try to
714 * allocate within the same dmap as contains the hint.
716 * if no blocks are available in the dmap or the allocation
717 * request is larger than the dmap size, we try to allocate
718 * within the same allocation group as contains the hint. if
719 * this does not succeed, we finally try to allocate anywhere
720 * within the aggregate.
722 * we also try to allocate anywhere within the aggregate for
723 * for allocation requests larger than the allocation group
724 * size or requests that specify no hint value.
727 * ip - pointer to in-core inode;
728 * hint - allocation hint.
729 * nblocks - number of contiguous blocks in the range.
730 * results - on successful return, set to the starting block number
731 * of the newly allocated contiguous range.
735 * -ENOSPC - insufficient disk resources
738 int dbAlloc(struct inode *ip, s64 hint, s64 nblocks, s64 * results)
741 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
750 /* assert that nblocks is valid */
753 /* get the log2 number of blocks to be allocated.
754 * if the number of blocks is not a log2 multiple,
755 * it will be rounded up to the next log2 multiple.
757 l2nb = BLKSTOL2(nblocks);
759 bmp = JFS_SBI(ip->i_sb)->bmap;
761 mapSize = bmp->db_mapsize;
763 /* the hint should be within the map */
764 if (hint >= mapSize) {
765 jfs_error(ip->i_sb, "the hint is outside the map\n");
769 /* if the number of blocks to be allocated is greater than the
770 * allocation group size, try to allocate anywhere.
772 if (l2nb > bmp->db_agl2size) {
773 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
775 rc = dbAllocAny(bmp, nblocks, l2nb, results);
781 * If no hint, let dbNextAG recommend an allocation group
786 /* we would like to allocate close to the hint. adjust the
787 * hint to the block following the hint since the allocators
788 * will start looking for free space starting at this point.
792 if (blkno >= bmp->db_mapsize)
795 agno = blkno >> bmp->db_agl2size;
797 /* check if blkno crosses over into a new allocation group.
798 * if so, check if we should allow allocations within this
801 if ((blkno & (bmp->db_agsize - 1)) == 0)
802 /* check if the AG is currently being written to.
803 * if so, call dbNextAG() to find a non-busy
804 * AG with sufficient free space.
806 if (atomic_read(&bmp->db_active[agno]))
809 /* check if the allocation request size can be satisfied from a
810 * single dmap. if so, try to allocate from the dmap containing
811 * the hint using a tiered strategy.
813 if (nblocks <= BPERDMAP) {
814 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
816 /* get the buffer for the dmap containing the hint.
819 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
820 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
824 dp = (struct dmap *) mp->data;
826 /* first, try to satisfy the allocation request with the
827 * blocks beginning at the hint.
829 if ((rc = dbAllocNext(bmp, dp, blkno, (int) nblocks))
833 mark_metapage_dirty(mp);
836 release_metapage(mp);
840 writers = atomic_read(&bmp->db_active[agno]);
842 ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) {
844 * Someone else is writing in this allocation
845 * group. To avoid fragmenting, try another ag
847 release_metapage(mp);
848 IREAD_UNLOCK(ipbmap);
852 /* next, try to satisfy the allocation request with blocks
856 dbAllocNear(bmp, dp, blkno, (int) nblocks, l2nb, results))
859 mark_metapage_dirty(mp);
861 release_metapage(mp);
865 /* try to satisfy the allocation request with blocks within
866 * the same dmap as the hint.
868 if ((rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results))
871 mark_metapage_dirty(mp);
873 release_metapage(mp);
877 release_metapage(mp);
878 IREAD_UNLOCK(ipbmap);
881 /* try to satisfy the allocation request with blocks within
882 * the same allocation group as the hint.
884 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
885 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC)
888 IWRITE_UNLOCK(ipbmap);
893 * Let dbNextAG recommend a preferred allocation group
895 agno = dbNextAG(ipbmap);
896 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
898 /* Try to allocate within this allocation group. if that fails, try to
899 * allocate anywhere in the map.
901 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC)
902 rc = dbAllocAny(bmp, nblocks, l2nb, results);
905 IWRITE_UNLOCK(ipbmap);
910 IREAD_UNLOCK(ipbmap);
917 * NAME: dbAllocExact()
919 * FUNCTION: try to allocate the requested extent;
922 * ip - pointer to in-core inode;
923 * blkno - extent address;
924 * nblocks - extent length;
928 * -ENOSPC - insufficient disk resources
931 int dbAllocExact(struct inode *ip, s64 blkno, int nblocks)
934 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
935 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
940 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
943 * validate extent request:
945 * note: defragfs policy:
946 * max 64 blocks will be moved.
947 * allocation request size must be satisfied from a single dmap.
949 if (nblocks <= 0 || nblocks > BPERDMAP || blkno >= bmp->db_mapsize) {
950 IREAD_UNLOCK(ipbmap);
954 if (nblocks > ((s64) 1 << bmp->db_maxfreebud)) {
955 /* the free space is no longer available */
956 IREAD_UNLOCK(ipbmap);
960 /* read in the dmap covering the extent */
961 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
962 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
964 IREAD_UNLOCK(ipbmap);
967 dp = (struct dmap *) mp->data;
969 /* try to allocate the requested extent */
970 rc = dbAllocNext(bmp, dp, blkno, nblocks);
972 IREAD_UNLOCK(ipbmap);
975 mark_metapage_dirty(mp);
977 release_metapage(mp);
986 * FUNCTION: attempt to extend a current allocation by a specified
989 * this routine attempts to satisfy the allocation request
990 * by first trying to extend the existing allocation in
991 * place by allocating the additional blocks as the blocks
992 * immediately following the current allocation. if these
993 * blocks are not available, this routine will attempt to
994 * allocate a new set of contiguous blocks large enough
995 * to cover the existing allocation plus the additional
996 * number of blocks required.
999 * ip - pointer to in-core inode requiring allocation.
1000 * blkno - starting block of the current allocation.
1001 * nblocks - number of contiguous blocks within the current
1003 * addnblocks - number of blocks to add to the allocation.
1004 * results - on successful return, set to the starting block number
1005 * of the existing allocation if the existing allocation
1006 * was extended in place or to a newly allocated contiguous
1007 * range if the existing allocation could not be extended
1012 * -ENOSPC - insufficient disk resources
1016 dbReAlloc(struct inode *ip,
1017 s64 blkno, s64 nblocks, s64 addnblocks, s64 * results)
1021 /* try to extend the allocation in place.
1023 if ((rc = dbExtend(ip, blkno, nblocks, addnblocks)) == 0) {
1031 /* could not extend the allocation in place, so allocate a
1032 * new set of blocks for the entire request (i.e. try to get
1033 * a range of contiguous blocks large enough to cover the
1034 * existing allocation plus the additional blocks.)
1037 (ip, blkno + nblocks - 1, addnblocks + nblocks, results));
1044 * FUNCTION: attempt to extend a current allocation by a specified
1047 * this routine attempts to satisfy the allocation request
1048 * by first trying to extend the existing allocation in
1049 * place by allocating the additional blocks as the blocks
1050 * immediately following the current allocation.
1053 * ip - pointer to in-core inode requiring allocation.
1054 * blkno - starting block of the current allocation.
1055 * nblocks - number of contiguous blocks within the current
1057 * addnblocks - number of blocks to add to the allocation.
1061 * -ENOSPC - insufficient disk resources
1064 static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks)
1066 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1067 s64 lblkno, lastblkno, extblkno;
1069 struct metapage *mp;
1072 struct inode *ipbmap = sbi->ipbmap;
1076 * We don't want a non-aligned extent to cross a page boundary
1078 if (((rel_block = blkno & (sbi->nbperpage - 1))) &&
1079 (rel_block + nblocks + addnblocks > sbi->nbperpage))
1082 /* get the last block of the current allocation */
1083 lastblkno = blkno + nblocks - 1;
1085 /* determine the block number of the block following
1086 * the existing allocation.
1088 extblkno = lastblkno + 1;
1090 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
1092 /* better be within the file system */
1094 if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) {
1095 IREAD_UNLOCK(ipbmap);
1096 jfs_error(ip->i_sb, "the block is outside the filesystem\n");
1100 /* we'll attempt to extend the current allocation in place by
1101 * allocating the additional blocks as the blocks immediately
1102 * following the current allocation. we only try to extend the
1103 * current allocation in place if the number of additional blocks
1104 * can fit into a dmap, the last block of the current allocation
1105 * is not the last block of the file system, and the start of the
1106 * inplace extension is not on an allocation group boundary.
1108 if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize ||
1109 (extblkno & (bmp->db_agsize - 1)) == 0) {
1110 IREAD_UNLOCK(ipbmap);
1114 /* get the buffer for the dmap containing the first block
1117 lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage);
1118 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
1120 IREAD_UNLOCK(ipbmap);
1124 dp = (struct dmap *) mp->data;
1126 /* try to allocate the blocks immediately following the
1127 * current allocation.
1129 rc = dbAllocNext(bmp, dp, extblkno, (int) addnblocks);
1131 IREAD_UNLOCK(ipbmap);
1133 /* were we successful ? */
1137 /* we were not successful */
1138 release_metapage(mp);
1145 * NAME: dbAllocNext()
1147 * FUNCTION: attempt to allocate the blocks of the specified block
1148 * range within a dmap.
1151 * bmp - pointer to bmap descriptor
1152 * dp - pointer to dmap.
1153 * blkno - starting block number of the range.
1154 * nblocks - number of contiguous free blocks of the range.
1158 * -ENOSPC - insufficient disk resources
1161 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1163 static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
1166 int dbitno, word, rembits, nb, nwords, wbitno, nw;
1171 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1172 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n");
1176 /* pick up a pointer to the leaves of the dmap tree.
1178 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
1180 /* determine the bit number and word within the dmap of the
1183 dbitno = blkno & (BPERDMAP - 1);
1184 word = dbitno >> L2DBWORD;
1186 /* check if the specified block range is contained within
1189 if (dbitno + nblocks > BPERDMAP)
1192 /* check if the starting leaf indicates that anything
1195 if (leaf[word] == NOFREE)
1198 /* check the dmaps words corresponding to block range to see
1199 * if the block range is free. not all bits of the first and
1200 * last words may be contained within the block range. if this
1201 * is the case, we'll work against those words (i.e. partial first
1202 * and/or last) on an individual basis (a single pass) and examine
1203 * the actual bits to determine if they are free. a single pass
1204 * will be used for all dmap words fully contained within the
1205 * specified range. within this pass, the leaves of the dmap
1206 * tree will be examined to determine if the blocks are free. a
1207 * single leaf may describe the free space of multiple dmap
1208 * words, so we may visit only a subset of the actual leaves
1209 * corresponding to the dmap words of the block range.
1211 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
1212 /* determine the bit number within the word and
1213 * the number of bits within the word.
1215 wbitno = dbitno & (DBWORD - 1);
1216 nb = min(rembits, DBWORD - wbitno);
1218 /* check if only part of the word is to be examined.
1221 /* check if the bits are free.
1223 mask = (ONES << (DBWORD - nb) >> wbitno);
1224 if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask)
1229 /* one or more dmap words are fully contained
1230 * within the block range. determine how many
1231 * words and how many bits.
1233 nwords = rembits >> L2DBWORD;
1234 nb = nwords << L2DBWORD;
1236 /* now examine the appropriate leaves to determine
1237 * if the blocks are free.
1239 while (nwords > 0) {
1240 /* does the leaf describe any free space ?
1242 if (leaf[word] < BUDMIN)
1245 /* determine the l2 number of bits provided
1249 min_t(int, leaf[word], NLSTOL2BSZ(nwords));
1251 /* determine how many words were handled.
1253 nw = BUDSIZE(l2size, BUDMIN);
1261 /* allocate the blocks.
1263 return (dbAllocDmap(bmp, dp, blkno, nblocks));
1268 * NAME: dbAllocNear()
1270 * FUNCTION: attempt to allocate a number of contiguous free blocks near
1271 * a specified block (hint) within a dmap.
1273 * starting with the dmap leaf that covers the hint, we'll
1274 * check the next four contiguous leaves for sufficient free
1275 * space. if sufficient free space is found, we'll allocate
1276 * the desired free space.
1279 * bmp - pointer to bmap descriptor
1280 * dp - pointer to dmap.
1281 * blkno - block number to allocate near.
1282 * nblocks - actual number of contiguous free blocks desired.
1283 * l2nb - log2 number of contiguous free blocks desired.
1284 * results - on successful return, set to the starting block number
1285 * of the newly allocated range.
1289 * -ENOSPC - insufficient disk resources
1292 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1295 dbAllocNear(struct bmap * bmp,
1296 struct dmap * dp, s64 blkno, int nblocks, int l2nb, s64 * results)
1298 int word, lword, rc;
1301 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1302 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n");
1306 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
1308 /* determine the word within the dmap that holds the hint
1309 * (i.e. blkno). also, determine the last word in the dmap
1310 * that we'll include in our examination.
1312 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
1313 lword = min(word + 4, LPERDMAP);
1315 /* examine the leaves for sufficient free space.
1317 for (; word < lword; word++) {
1318 /* does the leaf describe sufficient free space ?
1320 if (leaf[word] < l2nb)
1323 /* determine the block number within the file system
1324 * of the first block described by this dmap word.
1326 blkno = le64_to_cpu(dp->start) + (word << L2DBWORD);
1328 /* if not all bits of the dmap word are free, get the
1329 * starting bit number within the dmap word of the required
1330 * string of free bits and adjust the block number with the
1333 if (leaf[word] < BUDMIN)
1335 dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb);
1337 /* allocate the blocks.
1339 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
1352 * FUNCTION: attempt to allocate the specified number of contiguous
1353 * free blocks within the specified allocation group.
1355 * unless the allocation group size is equal to the number
1356 * of blocks per dmap, the dmap control pages will be used to
1357 * find the required free space, if available. we start the
1358 * search at the highest dmap control page level which
1359 * distinctly describes the allocation group's free space
1360 * (i.e. the highest level at which the allocation group's
1361 * free space is not mixed in with that of any other group).
1362 * in addition, we start the search within this level at a
1363 * height of the dmapctl dmtree at which the nodes distinctly
1364 * describe the allocation group's free space. at this height,
1365 * the allocation group's free space may be represented by 1
1366 * or two sub-trees, depending on the allocation group size.
1367 * we search the top nodes of these subtrees left to right for
1368 * sufficient free space. if sufficient free space is found,
1369 * the subtree is searched to find the leftmost leaf that
1370 * has free space. once we have made it to the leaf, we
1371 * move the search to the next lower level dmap control page
1372 * corresponding to this leaf. we continue down the dmap control
1373 * pages until we find the dmap that contains or starts the
1374 * sufficient free space and we allocate at this dmap.
1376 * if the allocation group size is equal to the dmap size,
1377 * we'll start at the dmap corresponding to the allocation
1378 * group and attempt the allocation at this level.
1380 * the dmap control page search is also not performed if the
1381 * allocation group is completely free and we go to the first
1382 * dmap of the allocation group to do the allocation. this is
1383 * done because the allocation group may be part (not the first
1384 * part) of a larger binary buddy system, causing the dmap
1385 * control pages to indicate no free space (NOFREE) within
1386 * the allocation group.
1389 * bmp - pointer to bmap descriptor
1390 * agno - allocation group number.
1391 * nblocks - actual number of contiguous free blocks desired.
1392 * l2nb - log2 number of contiguous free blocks desired.
1393 * results - on successful return, set to the starting block number
1394 * of the newly allocated range.
1398 * -ENOSPC - insufficient disk resources
1401 * note: IWRITE_LOCK(ipmap) held on entry/exit;
1404 dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb, s64 * results)
1406 struct metapage *mp;
1407 struct dmapctl *dcp;
1408 int rc, ti, i, k, m, n, agperlev;
1412 /* allocation request should not be for more than the
1413 * allocation group size.
1415 if (l2nb > bmp->db_agl2size) {
1416 jfs_error(bmp->db_ipbmap->i_sb,
1417 "allocation request is larger than the allocation group size\n");
1421 /* determine the starting block number of the allocation
1424 blkno = (s64) agno << bmp->db_agl2size;
1426 /* check if the allocation group size is the minimum allocation
1427 * group size or if the allocation group is completely free. if
1428 * the allocation group size is the minimum size of BPERDMAP (i.e.
1429 * 1 dmap), there is no need to search the dmap control page (below)
1430 * that fully describes the allocation group since the allocation
1431 * group is already fully described by a dmap. in this case, we
1432 * just call dbAllocCtl() to search the dmap tree and allocate the
1433 * required space if available.
1435 * if the allocation group is completely free, dbAllocCtl() is
1436 * also called to allocate the required space. this is done for
1437 * two reasons. first, it makes no sense searching the dmap control
1438 * pages for free space when we know that free space exists. second,
1439 * the dmap control pages may indicate that the allocation group
1440 * has no free space if the allocation group is part (not the first
1441 * part) of a larger binary buddy system.
1443 if (bmp->db_agsize == BPERDMAP
1444 || bmp->db_agfree[agno] == bmp->db_agsize) {
1445 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1446 if ((rc == -ENOSPC) &&
1447 (bmp->db_agfree[agno] == bmp->db_agsize)) {
1448 printk(KERN_ERR "blkno = %Lx, blocks = %Lx\n",
1449 (unsigned long long) blkno,
1450 (unsigned long long) nblocks);
1451 jfs_error(bmp->db_ipbmap->i_sb,
1452 "dbAllocCtl failed in free AG\n");
1457 /* the buffer for the dmap control page that fully describes the
1460 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel);
1461 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1464 dcp = (struct dmapctl *) mp->data;
1465 budmin = dcp->budmin;
1467 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1468 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n");
1469 release_metapage(mp);
1473 /* search the subtree(s) of the dmap control page that describes
1474 * the allocation group, looking for sufficient free space. to begin,
1475 * determine how many allocation groups are represented in a dmap
1476 * control page at the control page level (i.e. L0, L1, L2) that
1477 * fully describes an allocation group. next, determine the starting
1478 * tree index of this allocation group within the control page.
1481 (1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth;
1482 ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1));
1484 /* dmap control page trees fan-out by 4 and a single allocation
1485 * group may be described by 1 or 2 subtrees within the ag level
1486 * dmap control page, depending upon the ag size. examine the ag's
1487 * subtrees for sufficient free space, starting with the leftmost
1490 for (i = 0; i < bmp->db_agwidth; i++, ti++) {
1491 /* is there sufficient free space ?
1493 if (l2nb > dcp->stree[ti])
1496 /* sufficient free space found in a subtree. now search down
1497 * the subtree to find the leftmost leaf that describes this
1500 for (k = bmp->db_agheight; k > 0; k--) {
1501 for (n = 0, m = (ti << 2) + 1; n < 4; n++) {
1502 if (l2nb <= dcp->stree[m + n]) {
1508 jfs_error(bmp->db_ipbmap->i_sb,
1509 "failed descending stree\n");
1510 release_metapage(mp);
1515 /* determine the block number within the file system
1516 * that corresponds to this leaf.
1518 if (bmp->db_aglevel == 2)
1520 else if (bmp->db_aglevel == 1)
1521 blkno &= ~(MAXL1SIZE - 1);
1522 else /* bmp->db_aglevel == 0 */
1523 blkno &= ~(MAXL0SIZE - 1);
1526 ((s64) (ti - le32_to_cpu(dcp->leafidx))) << budmin;
1528 /* release the buffer in preparation for going down
1529 * the next level of dmap control pages.
1531 release_metapage(mp);
1533 /* check if we need to continue to search down the lower
1534 * level dmap control pages. we need to if the number of
1535 * blocks required is less than maximum number of blocks
1536 * described at the next lower level.
1538 if (l2nb < budmin) {
1540 /* search the lower level dmap control pages to get
1541 * the starting block number of the dmap that
1542 * contains or starts off the free space.
1545 dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1,
1547 if (rc == -ENOSPC) {
1548 jfs_error(bmp->db_ipbmap->i_sb,
1549 "control page inconsistent\n");
1556 /* allocate the blocks.
1558 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1559 if (rc == -ENOSPC) {
1560 jfs_error(bmp->db_ipbmap->i_sb,
1561 "unable to allocate blocks\n");
1567 /* no space in the allocation group. release the buffer and
1570 release_metapage(mp);
1577 * NAME: dbAllocAny()
1579 * FUNCTION: attempt to allocate the specified number of contiguous
1580 * free blocks anywhere in the file system.
1582 * dbAllocAny() attempts to find the sufficient free space by
1583 * searching down the dmap control pages, starting with the
1584 * highest level (i.e. L0, L1, L2) control page. if free space
1585 * large enough to satisfy the desired free space is found, the
1586 * desired free space is allocated.
1589 * bmp - pointer to bmap descriptor
1590 * nblocks - actual number of contiguous free blocks desired.
1591 * l2nb - log2 number of contiguous free blocks desired.
1592 * results - on successful return, set to the starting block number
1593 * of the newly allocated range.
1597 * -ENOSPC - insufficient disk resources
1600 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1602 static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results)
1607 /* starting with the top level dmap control page, search
1608 * down the dmap control levels for sufficient free space.
1609 * if free space is found, dbFindCtl() returns the starting
1610 * block number of the dmap that contains or starts off the
1611 * range of free space.
1613 if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno)))
1616 /* allocate the blocks.
1618 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1619 if (rc == -ENOSPC) {
1620 jfs_error(bmp->db_ipbmap->i_sb, "unable to allocate blocks\n");
1628 * NAME: dbDiscardAG()
1630 * FUNCTION: attempt to discard (TRIM) all free blocks of specific AG
1633 * 1) allocate blocks, as large as possible and save them
1634 * while holding IWRITE_LOCK on ipbmap
1635 * 2) trim all these saved block/length values
1636 * 3) mark the blocks free again
1639 * - we work only on one ag at some time, minimizing how long we
1640 * need to lock ipbmap
1641 * - reading / writing the fs is possible most time, even on
1645 * - we write two times to the dmapctl and dmap pages
1646 * - but for me, this seems the best way, better ideas?
1650 * ip - pointer to in-core inode
1652 * minlen - minimum value of contiguous blocks
1655 * s64 - actual number of blocks trimmed
1657 s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen)
1659 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
1660 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
1664 struct super_block *sb = ipbmap->i_sb;
1671 /* max blkno / nblocks pairs to trim */
1672 int count = 0, range_cnt;
1675 /* prevent others from writing new stuff here, while trimming */
1676 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
1678 nblocks = bmp->db_agfree[agno];
1679 max_ranges = nblocks;
1680 do_div(max_ranges, minlen);
1681 range_cnt = min_t(u64, max_ranges + 1, 32 * 1024);
1682 totrim = kmalloc_array(range_cnt, sizeof(struct range2trim), GFP_NOFS);
1683 if (totrim == NULL) {
1684 jfs_error(bmp->db_ipbmap->i_sb, "no memory for trim array\n");
1685 IWRITE_UNLOCK(ipbmap);
1690 while (nblocks >= minlen) {
1691 l2nb = BLKSTOL2(nblocks);
1693 /* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */
1694 rc = dbAllocAG(bmp, agno, nblocks, l2nb, &blkno);
1697 tt->nblocks = nblocks;
1700 /* the whole ag is free, trim now */
1701 if (bmp->db_agfree[agno] == 0)
1704 /* give a hint for the next while */
1705 nblocks = bmp->db_agfree[agno];
1707 } else if (rc == -ENOSPC) {
1708 /* search for next smaller log2 block */
1709 l2nb = BLKSTOL2(nblocks) - 1;
1710 nblocks = 1LL << l2nb;
1712 /* Trim any already allocated blocks */
1713 jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n");
1717 /* check, if our trim array is full */
1718 if (unlikely(count >= range_cnt - 1))
1721 IWRITE_UNLOCK(ipbmap);
1723 tt->nblocks = 0; /* mark the current end */
1724 for (tt = totrim; tt->nblocks != 0; tt++) {
1725 /* when mounted with online discard, dbFree() will
1726 * call jfs_issue_discard() itself */
1727 if (!(JFS_SBI(sb)->flag & JFS_DISCARD))
1728 jfs_issue_discard(ip, tt->blkno, tt->nblocks);
1729 dbFree(ip, tt->blkno, tt->nblocks);
1730 trimmed += tt->nblocks;
1740 * FUNCTION: starting at a specified dmap control page level and block
1741 * number, search down the dmap control levels for a range of
1742 * contiguous free blocks large enough to satisfy an allocation
1743 * request for the specified number of free blocks.
1745 * if sufficient contiguous free blocks are found, this routine
1746 * returns the starting block number within a dmap page that
1747 * contains or starts a range of contiqious free blocks that
1748 * is sufficient in size.
1751 * bmp - pointer to bmap descriptor
1752 * level - starting dmap control page level.
1753 * l2nb - log2 number of contiguous free blocks desired.
1754 * *blkno - on entry, starting block number for conducting the search.
1755 * on successful return, the first block within a dmap page
1756 * that contains or starts a range of contiguous free blocks.
1760 * -ENOSPC - insufficient disk resources
1763 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1765 static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno)
1767 int rc, leafidx, lev;
1769 struct dmapctl *dcp;
1771 struct metapage *mp;
1773 /* starting at the specified dmap control page level and block
1774 * number, search down the dmap control levels for the starting
1775 * block number of a dmap page that contains or starts off
1776 * sufficient free blocks.
1778 for (lev = level, b = *blkno; lev >= 0; lev--) {
1779 /* get the buffer of the dmap control page for the block
1780 * number and level (i.e. L0, L1, L2).
1782 lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev);
1783 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1786 dcp = (struct dmapctl *) mp->data;
1787 budmin = dcp->budmin;
1789 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1790 jfs_error(bmp->db_ipbmap->i_sb,
1791 "Corrupt dmapctl page\n");
1792 release_metapage(mp);
1796 /* search the tree within the dmap control page for
1797 * sufficient free space. if sufficient free space is found,
1798 * dbFindLeaf() returns the index of the leaf at which
1799 * free space was found.
1801 rc = dbFindLeaf((dmtree_t *) dcp, l2nb, &leafidx, true);
1803 /* release the buffer.
1805 release_metapage(mp);
1811 jfs_error(bmp->db_ipbmap->i_sb,
1812 "dmap inconsistent\n");
1818 /* adjust the block number to reflect the location within
1819 * the dmap control page (i.e. the leaf) at which free
1822 b += (((s64) leafidx) << budmin);
1824 /* we stop the search at this dmap control page level if
1825 * the number of blocks required is greater than or equal
1826 * to the maximum number of blocks described at the next
1839 * NAME: dbAllocCtl()
1841 * FUNCTION: attempt to allocate a specified number of contiguous
1842 * blocks starting within a specific dmap.
1844 * this routine is called by higher level routines that search
1845 * the dmap control pages above the actual dmaps for contiguous
1846 * free space. the result of successful searches by these
1847 * routines are the starting block numbers within dmaps, with
1848 * the dmaps themselves containing the desired contiguous free
1849 * space or starting a contiguous free space of desired size
1850 * that is made up of the blocks of one or more dmaps. these
1851 * calls should not fail due to insufficent resources.
1853 * this routine is called in some cases where it is not known
1854 * whether it will fail due to insufficient resources. more
1855 * specifically, this occurs when allocating from an allocation
1856 * group whose size is equal to the number of blocks per dmap.
1857 * in this case, the dmap control pages are not examined prior
1858 * to calling this routine (to save pathlength) and the call
1861 * for a request size that fits within a dmap, this routine relies
1862 * upon the dmap's dmtree to find the requested contiguous free
1863 * space. for request sizes that are larger than a dmap, the
1864 * requested free space will start at the first block of the
1865 * first dmap (i.e. blkno).
1868 * bmp - pointer to bmap descriptor
1869 * nblocks - actual number of contiguous free blocks to allocate.
1870 * l2nb - log2 number of contiguous free blocks to allocate.
1871 * blkno - starting block number of the dmap to start the allocation
1873 * results - on successful return, set to the starting block number
1874 * of the newly allocated range.
1878 * -ENOSPC - insufficient disk resources
1881 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1884 dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, s64 * results)
1888 struct metapage *mp;
1891 /* check if the allocation request is confined to a single dmap.
1893 if (l2nb <= L2BPERDMAP) {
1894 /* get the buffer for the dmap.
1896 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
1897 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1900 dp = (struct dmap *) mp->data;
1902 /* try to allocate the blocks.
1904 rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results);
1906 mark_metapage_dirty(mp);
1908 release_metapage(mp);
1913 /* allocation request involving multiple dmaps. it must start on
1916 assert((blkno & (BPERDMAP - 1)) == 0);
1918 /* allocate the blocks dmap by dmap.
1920 for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) {
1921 /* get the buffer for the dmap.
1923 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1924 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1929 dp = (struct dmap *) mp->data;
1931 /* the dmap better be all free.
1933 if (dp->tree.stree[ROOT] != L2BPERDMAP) {
1934 release_metapage(mp);
1935 jfs_error(bmp->db_ipbmap->i_sb,
1936 "the dmap is not all free\n");
1941 /* determine how many blocks to allocate from this dmap.
1943 nb = min_t(s64, n, BPERDMAP);
1945 /* allocate the blocks from the dmap.
1947 if ((rc = dbAllocDmap(bmp, dp, b, nb))) {
1948 release_metapage(mp);
1952 /* write the buffer.
1957 /* set the results (starting block number) and return.
1962 /* something failed in handling an allocation request involving
1963 * multiple dmaps. we'll try to clean up by backing out any
1964 * allocation that has already happened for this request. if
1965 * we fail in backing out the allocation, we'll mark the file
1966 * system to indicate that blocks have been leaked.
1970 /* try to backout the allocations dmap by dmap.
1972 for (n = nblocks - n, b = blkno; n > 0;
1973 n -= BPERDMAP, b += BPERDMAP) {
1974 /* get the buffer for this dmap.
1976 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1977 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1979 /* could not back out. mark the file system
1980 * to indicate that we have leaked blocks.
1982 jfs_error(bmp->db_ipbmap->i_sb,
1983 "I/O Error: Block Leakage\n");
1986 dp = (struct dmap *) mp->data;
1988 /* free the blocks is this dmap.
1990 if (dbFreeDmap(bmp, dp, b, BPERDMAP)) {
1991 /* could not back out. mark the file system
1992 * to indicate that we have leaked blocks.
1994 release_metapage(mp);
1995 jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n");
1999 /* write the buffer.
2009 * NAME: dbAllocDmapLev()
2011 * FUNCTION: attempt to allocate a specified number of contiguous blocks
2012 * from a specified dmap.
2014 * this routine checks if the contiguous blocks are available.
2015 * if so, nblocks of blocks are allocated; otherwise, ENOSPC is
2019 * mp - pointer to bmap descriptor
2020 * dp - pointer to dmap to attempt to allocate blocks from.
2021 * l2nb - log2 number of contiguous block desired.
2022 * nblocks - actual number of contiguous block desired.
2023 * results - on successful return, set to the starting block number
2024 * of the newly allocated range.
2028 * -ENOSPC - insufficient disk resources
2031 * serialization: IREAD_LOCK(ipbmap), e.g., from dbAlloc(), or
2032 * IWRITE_LOCK(ipbmap), e.g., dbAllocCtl(), held on entry/exit;
2035 dbAllocDmapLev(struct bmap * bmp,
2036 struct dmap * dp, int nblocks, int l2nb, s64 * results)
2041 /* can't be more than a dmaps worth of blocks */
2042 assert(l2nb <= L2BPERDMAP);
2044 /* search the tree within the dmap page for sufficient
2045 * free space. if sufficient free space is found, dbFindLeaf()
2046 * returns the index of the leaf at which free space was found.
2048 if (dbFindLeaf((dmtree_t *) &dp->tree, l2nb, &leafidx, false))
2054 /* determine the block number within the file system corresponding
2055 * to the leaf at which free space was found.
2057 blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD);
2059 /* if not all bits of the dmap word are free, get the starting
2060 * bit number within the dmap word of the required string of free
2061 * bits and adjust the block number with this value.
2063 if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN)
2064 blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb);
2066 /* allocate the blocks */
2067 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
2075 * NAME: dbAllocDmap()
2077 * FUNCTION: adjust the disk allocation map to reflect the allocation
2078 * of a specified block range within a dmap.
2080 * this routine allocates the specified blocks from the dmap
2081 * through a call to dbAllocBits(). if the allocation of the
2082 * block range causes the maximum string of free blocks within
2083 * the dmap to change (i.e. the value of the root of the dmap's
2084 * dmtree), this routine will cause this change to be reflected
2085 * up through the appropriate levels of the dmap control pages
2086 * by a call to dbAdjCtl() for the L0 dmap control page that
2090 * bmp - pointer to bmap descriptor
2091 * dp - pointer to dmap to allocate the block range from.
2092 * blkno - starting block number of the block to be allocated.
2093 * nblocks - number of blocks to be allocated.
2099 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2101 static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2107 /* save the current value of the root (i.e. maximum free string)
2110 oldroot = dp->tree.stree[ROOT];
2112 /* allocate the specified (blocks) bits */
2113 dbAllocBits(bmp, dp, blkno, nblocks);
2115 /* if the root has not changed, done. */
2116 if (dp->tree.stree[ROOT] == oldroot)
2119 /* root changed. bubble the change up to the dmap control pages.
2120 * if the adjustment of the upper level control pages fails,
2121 * backout the bit allocation (thus making everything consistent).
2123 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0)))
2124 dbFreeBits(bmp, dp, blkno, nblocks);
2131 * NAME: dbFreeDmap()
2133 * FUNCTION: adjust the disk allocation map to reflect the allocation
2134 * of a specified block range within a dmap.
2136 * this routine frees the specified blocks from the dmap through
2137 * a call to dbFreeBits(). if the deallocation of the block range
2138 * causes the maximum string of free blocks within the dmap to
2139 * change (i.e. the value of the root of the dmap's dmtree), this
2140 * routine will cause this change to be reflected up through the
2141 * appropriate levels of the dmap control pages by a call to
2142 * dbAdjCtl() for the L0 dmap control page that covers this dmap.
2145 * bmp - pointer to bmap descriptor
2146 * dp - pointer to dmap to free the block range from.
2147 * blkno - starting block number of the block to be freed.
2148 * nblocks - number of blocks to be freed.
2154 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2156 static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2162 /* save the current value of the root (i.e. maximum free string)
2165 oldroot = dp->tree.stree[ROOT];
2167 /* free the specified (blocks) bits */
2168 rc = dbFreeBits(bmp, dp, blkno, nblocks);
2170 /* if error or the root has not changed, done. */
2171 if (rc || (dp->tree.stree[ROOT] == oldroot))
2174 /* root changed. bubble the change up to the dmap control pages.
2175 * if the adjustment of the upper level control pages fails,
2176 * backout the deallocation.
2178 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) {
2179 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
2181 /* as part of backing out the deallocation, we will have
2182 * to back split the dmap tree if the deallocation caused
2183 * the freed blocks to become part of a larger binary buddy
2186 if (dp->tree.stree[word] == NOFREE)
2187 dbBackSplit((dmtree_t *)&dp->tree, word, false);
2189 dbAllocBits(bmp, dp, blkno, nblocks);
2197 * NAME: dbAllocBits()
2199 * FUNCTION: allocate a specified block range from a dmap.
2201 * this routine updates the dmap to reflect the working
2202 * state allocation of the specified block range. it directly
2203 * updates the bits of the working map and causes the adjustment
2204 * of the binary buddy system described by the dmap's dmtree
2205 * leaves to reflect the bits allocated. it also causes the
2206 * dmap's dmtree, as a whole, to reflect the allocated range.
2209 * bmp - pointer to bmap descriptor
2210 * dp - pointer to dmap to allocate bits from.
2211 * blkno - starting block number of the bits to be allocated.
2212 * nblocks - number of bits to be allocated.
2214 * RETURN VALUES: none
2216 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2218 static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
2221 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2222 dmtree_t *tp = (dmtree_t *) & dp->tree;
2226 /* pick up a pointer to the leaves of the dmap tree */
2227 leaf = dp->tree.stree + LEAFIND;
2229 /* determine the bit number and word within the dmap of the
2232 dbitno = blkno & (BPERDMAP - 1);
2233 word = dbitno >> L2DBWORD;
2235 /* block range better be within the dmap */
2236 assert(dbitno + nblocks <= BPERDMAP);
2238 /* allocate the bits of the dmap's words corresponding to the block
2239 * range. not all bits of the first and last words may be contained
2240 * within the block range. if this is the case, we'll work against
2241 * those words (i.e. partial first and/or last) on an individual basis
2242 * (a single pass), allocating the bits of interest by hand and
2243 * updating the leaf corresponding to the dmap word. a single pass
2244 * will be used for all dmap words fully contained within the
2245 * specified range. within this pass, the bits of all fully contained
2246 * dmap words will be marked as free in a single shot and the leaves
2247 * will be updated. a single leaf may describe the free space of
2248 * multiple dmap words, so we may update only a subset of the actual
2249 * leaves corresponding to the dmap words of the block range.
2251 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2252 /* determine the bit number within the word and
2253 * the number of bits within the word.
2255 wbitno = dbitno & (DBWORD - 1);
2256 nb = min(rembits, DBWORD - wbitno);
2258 /* check if only part of a word is to be allocated.
2261 /* allocate (set to 1) the appropriate bits within
2264 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
2267 /* update the leaf for this dmap word. in addition
2268 * to setting the leaf value to the binary buddy max
2269 * of the updated dmap word, dbSplit() will split
2270 * the binary system of the leaves if need be.
2272 dbSplit(tp, word, BUDMIN,
2273 dbMaxBud((u8 *)&dp->wmap[word]), false);
2277 /* one or more dmap words are fully contained
2278 * within the block range. determine how many
2279 * words and allocate (set to 1) the bits of these
2282 nwords = rembits >> L2DBWORD;
2283 memset(&dp->wmap[word], (int) ONES, nwords * 4);
2285 /* determine how many bits.
2287 nb = nwords << L2DBWORD;
2289 /* now update the appropriate leaves to reflect
2290 * the allocated words.
2292 for (; nwords > 0; nwords -= nw) {
2293 if (leaf[word] < BUDMIN) {
2294 jfs_error(bmp->db_ipbmap->i_sb,
2295 "leaf page corrupt\n");
2299 /* determine what the leaf value should be
2300 * updated to as the minimum of the l2 number
2301 * of bits being allocated and the l2 number
2302 * of bits currently described by this leaf.
2304 size = min_t(int, leaf[word],
2305 NLSTOL2BSZ(nwords));
2307 /* update the leaf to reflect the allocation.
2308 * in addition to setting the leaf value to
2309 * NOFREE, dbSplit() will split the binary
2310 * system of the leaves to reflect the current
2311 * allocation (size).
2313 dbSplit(tp, word, size, NOFREE, false);
2315 /* get the number of dmap words handled */
2316 nw = BUDSIZE(size, BUDMIN);
2322 /* update the free count for this dmap */
2323 le32_add_cpu(&dp->nfree, -nblocks);
2327 /* if this allocation group is completely free,
2328 * update the maximum allocation group number if this allocation
2329 * group is the new max.
2331 agno = blkno >> bmp->db_agl2size;
2332 if (agno > bmp->db_maxag)
2333 bmp->db_maxag = agno;
2335 /* update the free count for the allocation group and map */
2336 bmp->db_agfree[agno] -= nblocks;
2337 bmp->db_nfree -= nblocks;
2344 * NAME: dbFreeBits()
2346 * FUNCTION: free a specified block range from a dmap.
2348 * this routine updates the dmap to reflect the working
2349 * state allocation of the specified block range. it directly
2350 * updates the bits of the working map and causes the adjustment
2351 * of the binary buddy system described by the dmap's dmtree
2352 * leaves to reflect the bits freed. it also causes the dmap's
2353 * dmtree, as a whole, to reflect the deallocated range.
2356 * bmp - pointer to bmap descriptor
2357 * dp - pointer to dmap to free bits from.
2358 * blkno - starting block number of the bits to be freed.
2359 * nblocks - number of bits to be freed.
2361 * RETURN VALUES: 0 for success
2363 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2365 static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
2368 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2369 dmtree_t *tp = (dmtree_t *) & dp->tree;
2373 /* determine the bit number and word within the dmap of the
2376 dbitno = blkno & (BPERDMAP - 1);
2377 word = dbitno >> L2DBWORD;
2379 /* block range better be within the dmap.
2381 assert(dbitno + nblocks <= BPERDMAP);
2383 /* free the bits of the dmaps words corresponding to the block range.
2384 * not all bits of the first and last words may be contained within
2385 * the block range. if this is the case, we'll work against those
2386 * words (i.e. partial first and/or last) on an individual basis
2387 * (a single pass), freeing the bits of interest by hand and updating
2388 * the leaf corresponding to the dmap word. a single pass will be used
2389 * for all dmap words fully contained within the specified range.
2390 * within this pass, the bits of all fully contained dmap words will
2391 * be marked as free in a single shot and the leaves will be updated. a
2392 * single leaf may describe the free space of multiple dmap words,
2393 * so we may update only a subset of the actual leaves corresponding
2394 * to the dmap words of the block range.
2396 * dbJoin() is used to update leaf values and will join the binary
2397 * buddy system of the leaves if the new leaf values indicate this
2400 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2401 /* determine the bit number within the word and
2402 * the number of bits within the word.
2404 wbitno = dbitno & (DBWORD - 1);
2405 nb = min(rembits, DBWORD - wbitno);
2407 /* check if only part of a word is to be freed.
2410 /* free (zero) the appropriate bits within this
2414 cpu_to_le32(~(ONES << (DBWORD - nb)
2417 /* update the leaf for this dmap word.
2419 rc = dbJoin(tp, word,
2420 dbMaxBud((u8 *)&dp->wmap[word]), false);
2426 /* one or more dmap words are fully contained
2427 * within the block range. determine how many
2428 * words and free (zero) the bits of these words.
2430 nwords = rembits >> L2DBWORD;
2431 memset(&dp->wmap[word], 0, nwords * 4);
2433 /* determine how many bits.
2435 nb = nwords << L2DBWORD;
2437 /* now update the appropriate leaves to reflect
2440 for (; nwords > 0; nwords -= nw) {
2441 /* determine what the leaf value should be
2442 * updated to as the minimum of the l2 number
2443 * of bits being freed and the l2 (max) number
2444 * of bits that can be described by this leaf.
2448 (word, L2LPERDMAP, BUDMIN),
2449 NLSTOL2BSZ(nwords));
2453 rc = dbJoin(tp, word, size, false);
2457 /* get the number of dmap words handled.
2459 nw = BUDSIZE(size, BUDMIN);
2465 /* update the free count for this dmap.
2467 le32_add_cpu(&dp->nfree, nblocks);
2471 /* update the free count for the allocation group and
2474 agno = blkno >> bmp->db_agl2size;
2475 bmp->db_nfree += nblocks;
2476 bmp->db_agfree[agno] += nblocks;
2478 /* check if this allocation group is not completely free and
2479 * if it is currently the maximum (rightmost) allocation group.
2480 * if so, establish the new maximum allocation group number by
2481 * searching left for the first allocation group with allocation.
2483 if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) ||
2484 (agno == bmp->db_numag - 1 &&
2485 bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) {
2486 while (bmp->db_maxag > 0) {
2488 if (bmp->db_agfree[bmp->db_maxag] !=
2493 /* re-establish the allocation group preference if the
2494 * current preference is right of the maximum allocation
2497 if (bmp->db_agpref > bmp->db_maxag)
2498 bmp->db_agpref = bmp->db_maxag;
2510 * FUNCTION: adjust a dmap control page at a specified level to reflect
2511 * the change in a lower level dmap or dmap control page's
2512 * maximum string of free blocks (i.e. a change in the root
2513 * of the lower level object's dmtree) due to the allocation
2514 * or deallocation of a range of blocks with a single dmap.
2516 * on entry, this routine is provided with the new value of
2517 * the lower level dmap or dmap control page root and the
2518 * starting block number of the block range whose allocation
2519 * or deallocation resulted in the root change. this range
2520 * is respresented by a single leaf of the current dmapctl
2521 * and the leaf will be updated with this value, possibly
2522 * causing a binary buddy system within the leaves to be
2523 * split or joined. the update may also cause the dmapctl's
2524 * dmtree to be updated.
2526 * if the adjustment of the dmap control page, itself, causes its
2527 * root to change, this change will be bubbled up to the next dmap
2528 * control level by a recursive call to this routine, specifying
2529 * the new root value and the next dmap control page level to
2532 * bmp - pointer to bmap descriptor
2533 * blkno - the first block of a block range within a dmap. it is
2534 * the allocation or deallocation of this block range that
2535 * requires the dmap control page to be adjusted.
2536 * newval - the new value of the lower level dmap or dmap control
2538 * alloc - 'true' if adjustment is due to an allocation.
2539 * level - current level of dmap control page (i.e. L0, L1, L2) to
2546 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2549 dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc, int level)
2551 struct metapage *mp;
2555 struct dmapctl *dcp;
2558 /* get the buffer for the dmap control page for the specified
2559 * block number and control page level.
2561 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level);
2562 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
2565 dcp = (struct dmapctl *) mp->data;
2567 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
2568 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n");
2569 release_metapage(mp);
2573 /* determine the leaf number corresponding to the block and
2574 * the index within the dmap control tree.
2576 leafno = BLKTOCTLLEAF(blkno, dcp->budmin);
2577 ti = leafno + le32_to_cpu(dcp->leafidx);
2579 /* save the current leaf value and the current root level (i.e.
2580 * maximum l2 free string described by this dmapctl).
2582 oldval = dcp->stree[ti];
2583 oldroot = dcp->stree[ROOT];
2585 /* check if this is a control page update for an allocation.
2586 * if so, update the leaf to reflect the new leaf value using
2587 * dbSplit(); otherwise (deallocation), use dbJoin() to update
2588 * the leaf with the new value. in addition to updating the
2589 * leaf, dbSplit() will also split the binary buddy system of
2590 * the leaves, if required, and bubble new values within the
2591 * dmapctl tree, if required. similarly, dbJoin() will join
2592 * the binary buddy system of leaves and bubble new values up
2593 * the dmapctl tree as required by the new leaf value.
2596 /* check if we are in the middle of a binary buddy
2597 * system. this happens when we are performing the
2598 * first allocation out of an allocation group that
2599 * is part (not the first part) of a larger binary
2600 * buddy system. if we are in the middle, back split
2601 * the system prior to calling dbSplit() which assumes
2602 * that it is at the front of a binary buddy system.
2604 if (oldval == NOFREE) {
2605 rc = dbBackSplit((dmtree_t *)dcp, leafno, true);
2608 oldval = dcp->stree[ti];
2610 dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval, true);
2612 rc = dbJoin((dmtree_t *) dcp, leafno, newval, true);
2617 /* check if the root of the current dmap control page changed due
2618 * to the update and if the current dmap control page is not at
2619 * the current top level (i.e. L0, L1, L2) of the map. if so (i.e.
2620 * root changed and this is not the top level), call this routine
2621 * again (recursion) for the next higher level of the mapping to
2622 * reflect the change in root for the current dmap control page.
2624 if (dcp->stree[ROOT] != oldroot) {
2625 /* are we below the top level of the map. if so,
2626 * bubble the root up to the next higher level.
2628 if (level < bmp->db_maxlevel) {
2629 /* bubble up the new root of this dmap control page to
2633 dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc,
2635 /* something went wrong in bubbling up the new
2636 * root value, so backout the changes to the
2637 * current dmap control page.
2640 dbJoin((dmtree_t *) dcp, leafno,
2643 /* the dbJoin() above might have
2644 * caused a larger binary buddy system
2645 * to form and we may now be in the
2646 * middle of it. if this is the case,
2647 * back split the buddies.
2649 if (dcp->stree[ti] == NOFREE)
2650 dbBackSplit((dmtree_t *)
2652 dbSplit((dmtree_t *) dcp, leafno,
2653 dcp->budmin, oldval, true);
2656 /* release the buffer and return the error.
2658 release_metapage(mp);
2662 /* we're at the top level of the map. update
2663 * the bmap control page to reflect the size
2664 * of the maximum free buddy system.
2666 assert(level == bmp->db_maxlevel);
2667 if (bmp->db_maxfreebud != oldroot) {
2668 jfs_error(bmp->db_ipbmap->i_sb,
2669 "the maximum free buddy is not the old root\n");
2671 bmp->db_maxfreebud = dcp->stree[ROOT];
2675 /* write the buffer.
2686 * FUNCTION: update the leaf of a dmtree with a new value, splitting
2687 * the leaf from the binary buddy system of the dmtree's
2688 * leaves, as required.
2691 * tp - pointer to the tree containing the leaf.
2692 * leafno - the number of the leaf to be updated.
2693 * splitsz - the size the binary buddy system starting at the leaf
2694 * must be split to, specified as the log2 number of blocks.
2695 * newval - the new value for the leaf.
2697 * RETURN VALUES: none
2699 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2701 static void dbSplit(dmtree_t *tp, int leafno, int splitsz, int newval, bool is_ctl)
2705 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2707 /* check if the leaf needs to be split.
2709 if (leaf[leafno] > tp->dmt_budmin) {
2710 /* the split occurs by cutting the buddy system in half
2711 * at the specified leaf until we reach the specified
2712 * size. pick up the starting split size (current size
2713 * - 1 in l2) and the corresponding buddy size.
2715 cursz = leaf[leafno] - 1;
2716 budsz = BUDSIZE(cursz, tp->dmt_budmin);
2718 /* split until we reach the specified size.
2720 while (cursz >= splitsz) {
2721 /* update the buddy's leaf with its new value.
2723 dbAdjTree(tp, leafno ^ budsz, cursz, is_ctl);
2725 /* on to the next size and buddy.
2732 /* adjust the dmap tree to reflect the specified leaf's new
2735 dbAdjTree(tp, leafno, newval, is_ctl);
2740 * NAME: dbBackSplit()
2742 * FUNCTION: back split the binary buddy system of dmtree leaves
2743 * that hold a specified leaf until the specified leaf
2744 * starts its own binary buddy system.
2746 * the allocators typically perform allocations at the start
2747 * of binary buddy systems and dbSplit() is used to accomplish
2748 * any required splits. in some cases, however, allocation
2749 * may occur in the middle of a binary system and requires a
2750 * back split, with the split proceeding out from the middle of
2751 * the system (less efficient) rather than the start of the
2752 * system (more efficient). the cases in which a back split
2753 * is required are rare and are limited to the first allocation
2754 * within an allocation group which is a part (not first part)
2755 * of a larger binary buddy system and a few exception cases
2756 * in which a previous join operation must be backed out.
2759 * tp - pointer to the tree containing the leaf.
2760 * leafno - the number of the leaf to be updated.
2762 * RETURN VALUES: none
2764 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2766 static int dbBackSplit(dmtree_t *tp, int leafno, bool is_ctl)
2768 int budsz, bud, w, bsz, size;
2770 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2772 /* leaf should be part (not first part) of a binary
2775 assert(leaf[leafno] == NOFREE);
2777 /* the back split is accomplished by iteratively finding the leaf
2778 * that starts the buddy system that contains the specified leaf and
2779 * splitting that system in two. this iteration continues until
2780 * the specified leaf becomes the start of a buddy system.
2782 * determine maximum possible l2 size for the specified leaf.
2785 LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs),
2788 /* determine the number of leaves covered by this size. this
2789 * is the buddy size that we will start with as we search for
2790 * the buddy system that contains the specified leaf.
2792 budsz = BUDSIZE(size, tp->dmt_budmin);
2796 while (leaf[leafno] == NOFREE) {
2797 /* find the leftmost buddy leaf.
2799 for (w = leafno, bsz = budsz;; bsz <<= 1,
2800 w = (w < bud) ? w : bud) {
2801 if (bsz >= le32_to_cpu(tp->dmt_nleafs)) {
2802 jfs_err("JFS: block map error in dbBackSplit");
2806 /* determine the buddy.
2810 /* check if this buddy is the start of the system.
2812 if (leaf[bud] != NOFREE) {
2813 /* split the leaf at the start of the
2816 cursz = leaf[bud] - 1;
2817 dbSplit(tp, bud, cursz, cursz, is_ctl);
2823 if (leaf[leafno] != size) {
2824 jfs_err("JFS: wrong leaf value in dbBackSplit");
2834 * FUNCTION: update the leaf of a dmtree with a new value, joining
2835 * the leaf with other leaves of the dmtree into a multi-leaf
2836 * binary buddy system, as required.
2839 * tp - pointer to the tree containing the leaf.
2840 * leafno - the number of the leaf to be updated.
2841 * newval - the new value for the leaf.
2843 * RETURN VALUES: none
2845 static int dbJoin(dmtree_t *tp, int leafno, int newval, bool is_ctl)
2850 /* can the new leaf value require a join with other leaves ?
2852 if (newval >= tp->dmt_budmin) {
2853 /* pickup a pointer to the leaves of the tree.
2855 leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2857 /* try to join the specified leaf into a large binary
2858 * buddy system. the join proceeds by attempting to join
2859 * the specified leafno with its buddy (leaf) at new value.
2860 * if the join occurs, we attempt to join the left leaf
2861 * of the joined buddies with its buddy at new value + 1.
2862 * we continue to join until we find a buddy that cannot be
2863 * joined (does not have a value equal to the size of the
2864 * last join) or until all leaves have been joined into a
2867 * get the buddy size (number of words covered) of
2870 budsz = BUDSIZE(newval, tp->dmt_budmin);
2874 while (budsz < le32_to_cpu(tp->dmt_nleafs)) {
2875 /* get the buddy leaf.
2877 buddy = leafno ^ budsz;
2879 /* if the leaf's new value is greater than its
2880 * buddy's value, we join no more.
2882 if (newval > leaf[buddy])
2885 /* It shouldn't be less */
2886 if (newval < leaf[buddy])
2889 /* check which (leafno or buddy) is the left buddy.
2890 * the left buddy gets to claim the blocks resulting
2891 * from the join while the right gets to claim none.
2892 * the left buddy is also eligible to participate in
2893 * a join at the next higher level while the right
2897 if (leafno < buddy) {
2898 /* leafno is the left buddy.
2900 dbAdjTree(tp, buddy, NOFREE, is_ctl);
2902 /* buddy is the left buddy and becomes
2905 dbAdjTree(tp, leafno, NOFREE, is_ctl);
2909 /* on to try the next join.
2916 /* update the leaf value.
2918 dbAdjTree(tp, leafno, newval, is_ctl);
2927 * FUNCTION: update a leaf of a dmtree with a new value, adjusting
2928 * the dmtree, as required, to reflect the new leaf value.
2929 * the combination of any buddies must already be done before
2933 * tp - pointer to the tree to be adjusted.
2934 * leafno - the number of the leaf to be updated.
2935 * newval - the new value for the leaf.
2937 * RETURN VALUES: none
2939 static void dbAdjTree(dmtree_t *tp, int leafno, int newval, bool is_ctl)
2944 size = is_ctl ? CTLTREESIZE : TREESIZE;
2946 /* pick up the index of the leaf for this leafno.
2948 lp = leafno + le32_to_cpu(tp->dmt_leafidx);
2950 if (WARN_ON_ONCE(lp >= size || lp < 0))
2953 /* is the current value the same as the old value ? if so,
2954 * there is nothing to do.
2956 if (tp->dmt_stree[lp] == newval)
2959 /* set the new value.
2961 tp->dmt_stree[lp] = newval;
2963 /* bubble the new value up the tree as required.
2965 for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) {
2966 /* get the index of the first leaf of the 4 leaf
2967 * group containing the specified leaf (leafno).
2969 lp = ((lp - 1) & ~0x03) + 1;
2971 /* get the index of the parent of this 4 leaf group.
2975 /* determine the maximum of the 4 leaves.
2977 max = TREEMAX(&tp->dmt_stree[lp]);
2979 /* if the maximum of the 4 is the same as the
2980 * parent's value, we're done.
2982 if (tp->dmt_stree[pp] == max)
2985 /* parent gets new value.
2987 tp->dmt_stree[pp] = max;
2989 /* parent becomes leaf for next go-round.
2997 * NAME: dbFindLeaf()
2999 * FUNCTION: search a dmtree_t for sufficient free blocks, returning
3000 * the index of a leaf describing the free blocks if
3001 * sufficient free blocks are found.
3003 * the search starts at the top of the dmtree_t tree and
3004 * proceeds down the tree to the leftmost leaf with sufficient
3008 * tp - pointer to the tree to be searched.
3009 * l2nb - log2 number of free blocks to search for.
3010 * leafidx - return pointer to be set to the index of the leaf
3011 * describing at least l2nb free blocks if sufficient
3012 * free blocks are found.
3013 * is_ctl - determines if the tree is of type ctl
3017 * -ENOSPC - insufficient free blocks.
3019 static int dbFindLeaf(dmtree_t *tp, int l2nb, int *leafidx, bool is_ctl)
3021 int ti, n = 0, k, x = 0;
3024 max_size = is_ctl ? CTLTREESIZE : TREESIZE;
3026 /* first check the root of the tree to see if there is
3027 * sufficient free space.
3029 if (l2nb > tp->dmt_stree[ROOT])
3032 /* sufficient free space available. now search down the tree
3033 * starting at the next level for the leftmost leaf that
3034 * describes sufficient free space.
3036 for (k = le32_to_cpu(tp->dmt_height), ti = 1;
3037 k > 0; k--, ti = ((ti + n) << 2) + 1) {
3038 /* search the four nodes at this level, starting from
3041 for (x = ti, n = 0; n < 4; n++) {
3042 /* sufficient free space found. move to the next
3043 * level (or quit if this is the last level).
3045 if (x + n > max_size)
3047 if (l2nb <= tp->dmt_stree[x + n])
3051 /* better have found something since the higher
3052 * levels of the tree said it was here.
3057 /* set the return to the leftmost leaf describing sufficient
3060 *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx);
3067 * NAME: dbFindBits()
3069 * FUNCTION: find a specified number of binary buddy free bits within a
3070 * dmap bitmap word value.
3072 * this routine searches the bitmap value for (1 << l2nb) free
3073 * bits at (1 << l2nb) alignments within the value.
3076 * word - dmap bitmap word value.
3077 * l2nb - number of free bits specified as a log2 number.
3080 * starting bit number of free bits.
3082 static int dbFindBits(u32 word, int l2nb)
3087 /* get the number of bits.
3090 assert(nb <= DBWORD);
3092 /* complement the word so we can use a mask (i.e. 0s represent
3093 * free bits) and compute the mask.
3096 mask = ONES << (DBWORD - nb);
3098 /* scan the word for nb free bits at nb alignments.
3100 for (bitno = 0; mask != 0; bitno += nb, mask >>= nb) {
3101 if ((mask & word) == mask)
3107 /* return the bit number.
3114 * NAME: dbMaxBud(u8 *cp)
3116 * FUNCTION: determine the largest binary buddy string of free
3117 * bits within 32-bits of the map.
3120 * cp - pointer to the 32-bit value.
3123 * largest binary buddy of free bits within a dmap word.
3125 static int dbMaxBud(u8 * cp)
3127 signed char tmp1, tmp2;
3129 /* check if the wmap word is all free. if so, the
3130 * free buddy size is BUDMIN.
3132 if (*((uint *) cp) == 0)
3135 /* check if the wmap word is half free. if so, the
3136 * free buddy size is BUDMIN-1.
3138 if (*((u16 *) cp) == 0 || *((u16 *) cp + 1) == 0)
3139 return (BUDMIN - 1);
3141 /* not all free or half free. determine the free buddy
3142 * size thru table lookup using quarters of the wmap word.
3144 tmp1 = max(budtab[cp[2]], budtab[cp[3]]);
3145 tmp2 = max(budtab[cp[0]], budtab[cp[1]]);
3146 return (max(tmp1, tmp2));
3151 * NAME: cnttz(uint word)
3153 * FUNCTION: determine the number of trailing zeros within a 32-bit
3157 * value - 32-bit value to be examined.
3160 * count of trailing zeros
3162 static int cnttz(u32 word)
3166 for (n = 0; n < 32; n++, word >>= 1) {
3176 * NAME: cntlz(u32 value)
3178 * FUNCTION: determine the number of leading zeros within a 32-bit
3182 * value - 32-bit value to be examined.
3185 * count of leading zeros
3187 static int cntlz(u32 value)
3191 for (n = 0; n < 32; n++, value <<= 1) {
3192 if (value & HIGHORDER)
3200 * NAME: blkstol2(s64 nb)
3202 * FUNCTION: convert a block count to its log2 value. if the block
3203 * count is not a l2 multiple, it is rounded up to the next
3204 * larger l2 multiple.
3207 * nb - number of blocks
3210 * log2 number of blocks
3212 static int blkstol2(s64 nb)
3215 s64 mask; /* meant to be signed */
3217 mask = (s64) 1 << (64 - 1);
3219 /* count the leading bits.
3221 for (l2nb = 0; l2nb < 64; l2nb++, mask >>= 1) {
3222 /* leading bit found.
3225 /* determine the l2 value.
3227 l2nb = (64 - 1) - l2nb;
3229 /* check if we need to round up.
3238 return 0; /* fix compiler warning */
3243 * NAME: dbAllocBottomUp()
3245 * FUNCTION: alloc the specified block range from the working block
3248 * the blocks will be alloc from the working map one dmap
3252 * ip - pointer to in-core inode;
3253 * blkno - starting block number to be freed.
3254 * nblocks - number of blocks to be freed.
3260 int dbAllocBottomUp(struct inode *ip, s64 blkno, s64 nblocks)
3262 struct metapage *mp;
3266 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
3267 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
3269 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
3271 /* block to be allocated better be within the mapsize. */
3272 ASSERT(nblocks <= bmp->db_mapsize - blkno);
3275 * allocate the blocks a dmap at a time.
3278 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
3279 /* release previous dmap if any */
3284 /* get the buffer for the current dmap. */
3285 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
3286 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
3288 IREAD_UNLOCK(ipbmap);
3291 dp = (struct dmap *) mp->data;
3293 /* determine the number of blocks to be allocated from
3296 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
3298 /* allocate the blocks. */
3299 if ((rc = dbAllocDmapBU(bmp, dp, blkno, nb))) {
3300 release_metapage(mp);
3301 IREAD_UNLOCK(ipbmap);
3306 /* write the last buffer. */
3309 IREAD_UNLOCK(ipbmap);
3315 static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
3319 int dbitno, word, rembits, nb, nwords, wbitno, agno;
3321 struct dmaptree *tp = (struct dmaptree *) & dp->tree;
3323 /* save the current value of the root (i.e. maximum free string)
3326 oldroot = tp->stree[ROOT];
3328 /* determine the bit number and word within the dmap of the
3331 dbitno = blkno & (BPERDMAP - 1);
3332 word = dbitno >> L2DBWORD;
3334 /* block range better be within the dmap */
3335 assert(dbitno + nblocks <= BPERDMAP);
3337 /* allocate the bits of the dmap's words corresponding to the block
3338 * range. not all bits of the first and last words may be contained
3339 * within the block range. if this is the case, we'll work against
3340 * those words (i.e. partial first and/or last) on an individual basis
3341 * (a single pass), allocating the bits of interest by hand and
3342 * updating the leaf corresponding to the dmap word. a single pass
3343 * will be used for all dmap words fully contained within the
3344 * specified range. within this pass, the bits of all fully contained
3345 * dmap words will be marked as free in a single shot and the leaves
3346 * will be updated. a single leaf may describe the free space of
3347 * multiple dmap words, so we may update only a subset of the actual
3348 * leaves corresponding to the dmap words of the block range.
3350 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
3351 /* determine the bit number within the word and
3352 * the number of bits within the word.
3354 wbitno = dbitno & (DBWORD - 1);
3355 nb = min(rembits, DBWORD - wbitno);
3357 /* check if only part of a word is to be allocated.
3360 /* allocate (set to 1) the appropriate bits within
3363 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
3368 /* one or more dmap words are fully contained
3369 * within the block range. determine how many
3370 * words and allocate (set to 1) the bits of these
3373 nwords = rembits >> L2DBWORD;
3374 memset(&dp->wmap[word], (int) ONES, nwords * 4);
3376 /* determine how many bits */
3377 nb = nwords << L2DBWORD;
3382 /* update the free count for this dmap */
3383 le32_add_cpu(&dp->nfree, -nblocks);
3385 /* reconstruct summary tree */
3390 /* if this allocation group is completely free,
3391 * update the highest active allocation group number
3392 * if this allocation group is the new max.
3394 agno = blkno >> bmp->db_agl2size;
3395 if (agno > bmp->db_maxag)
3396 bmp->db_maxag = agno;
3398 /* update the free count for the allocation group and map */
3399 bmp->db_agfree[agno] -= nblocks;
3400 bmp->db_nfree -= nblocks;
3404 /* if the root has not changed, done. */
3405 if (tp->stree[ROOT] == oldroot)
3408 /* root changed. bubble the change up to the dmap control pages.
3409 * if the adjustment of the upper level control pages fails,
3410 * backout the bit allocation (thus making everything consistent).
3412 if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0)))
3413 dbFreeBits(bmp, dp, blkno, nblocks);
3420 * NAME: dbExtendFS()
3422 * FUNCTION: extend bmap from blkno for nblocks;
3423 * dbExtendFS() updates bmap ready for dbAllocBottomUp();
3427 * L1---------------------------------L1
3429 * L0---------L0---------L0 L0---------L0---------L0
3431 * d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,.,dm;
3432 * L2L1L0d0,...,dnL0d0,...,dnL0d0,...,dnL1L0d0,...,dnL0d0,...,dnL0d0,..dm
3434 * <---old---><----------------------------extend----------------------->
3436 int dbExtendFS(struct inode *ipbmap, s64 blkno, s64 nblocks)
3438 struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb);
3439 int nbperpage = sbi->nbperpage;
3440 int i, i0 = true, j, j0 = true, k, n;
3443 struct metapage *mp, *l2mp, *l1mp = NULL, *l0mp = NULL;
3444 struct dmapctl *l2dcp, *l1dcp, *l0dcp;
3446 s8 *l0leaf, *l1leaf, *l2leaf;
3447 struct bmap *bmp = sbi->bmap;
3448 int agno, l2agsize, oldl2agsize;
3451 newsize = blkno + nblocks;
3453 jfs_info("dbExtendFS: blkno:%Ld nblocks:%Ld newsize:%Ld",
3454 (long long) blkno, (long long) nblocks, (long long) newsize);
3457 * initialize bmap control page.
3459 * all the data in bmap control page should exclude
3460 * the mkfs hidden dmap page.
3463 /* update mapsize */
3464 bmp->db_mapsize = newsize;
3465 bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize);
3467 /* compute new AG size */
3468 l2agsize = dbGetL2AGSize(newsize);
3469 oldl2agsize = bmp->db_agl2size;
3471 bmp->db_agl2size = l2agsize;
3472 bmp->db_agsize = 1 << l2agsize;
3474 /* compute new number of AG */
3475 agno = bmp->db_numag;
3476 bmp->db_numag = newsize >> l2agsize;
3477 bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0;
3480 * reconfigure db_agfree[]
3481 * from old AG configuration to new AG configuration;
3483 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
3484 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
3485 * note: new AG size = old AG size * (2**x).
3487 if (l2agsize == oldl2agsize)
3489 k = 1 << (l2agsize - oldl2agsize);
3490 ag_rem = bmp->db_agfree[0]; /* save agfree[0] */
3491 for (i = 0, n = 0; i < agno; n++) {
3492 bmp->db_agfree[n] = 0; /* init collection point */
3494 /* coalesce contiguous k AGs; */
3495 for (j = 0; j < k && i < agno; j++, i++) {
3496 /* merge AGi to AGn */
3497 bmp->db_agfree[n] += bmp->db_agfree[i];
3500 bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */
3502 for (; n < MAXAG; n++)
3503 bmp->db_agfree[n] = 0;
3506 * update highest active ag number
3509 bmp->db_maxag = bmp->db_maxag / k;
3514 * update bit maps and corresponding level control pages;
3515 * global control page db_nfree, db_agfree[agno], db_maxfreebud;
3519 p = BMAPBLKNO + nbperpage; /* L2 page */
3520 l2mp = read_metapage(ipbmap, p, PSIZE, 0);
3522 jfs_error(ipbmap->i_sb, "L2 page could not be read\n");
3525 l2dcp = (struct dmapctl *) l2mp->data;
3527 /* compute start L1 */
3528 k = blkno >> L2MAXL1SIZE;
3529 l2leaf = l2dcp->stree + CTLLEAFIND + k;
3530 p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */
3533 * extend each L1 in L2
3535 for (; k < LPERCTL; k++, p += nbperpage) {
3538 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */
3539 l1mp = read_metapage(ipbmap, p, PSIZE, 0);
3542 l1dcp = (struct dmapctl *) l1mp->data;
3544 /* compute start L0 */
3545 j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE;
3546 l1leaf = l1dcp->stree + CTLLEAFIND + j;
3547 p = BLKTOL0(blkno, sbi->l2nbperpage);
3550 /* assign/init L1 page */
3551 l1mp = get_metapage(ipbmap, p, PSIZE, 0);
3555 l1dcp = (struct dmapctl *) l1mp->data;
3557 /* compute start L0 */
3559 l1leaf = l1dcp->stree + CTLLEAFIND;
3560 p += nbperpage; /* 1st L0 of L1.k */
3564 * extend each L0 in L1
3566 for (; j < LPERCTL; j++) {
3569 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */
3571 l0mp = read_metapage(ipbmap, p, PSIZE, 0);
3574 l0dcp = (struct dmapctl *) l0mp->data;
3576 /* compute start dmap */
3577 i = (blkno & (MAXL0SIZE - 1)) >>
3579 l0leaf = l0dcp->stree + CTLLEAFIND + i;
3580 p = BLKTODMAP(blkno,
3584 /* assign/init L0 page */
3585 l0mp = get_metapage(ipbmap, p, PSIZE, 0);
3589 l0dcp = (struct dmapctl *) l0mp->data;
3591 /* compute start dmap */
3593 l0leaf = l0dcp->stree + CTLLEAFIND;
3594 p += nbperpage; /* 1st dmap of L0.j */
3598 * extend each dmap in L0
3600 for (; i < LPERCTL; i++) {
3602 * reconstruct the dmap page, and
3603 * initialize corresponding parent L0 leaf
3605 if ((n = blkno & (BPERDMAP - 1))) {
3606 /* read in dmap page: */
3607 mp = read_metapage(ipbmap, p,
3611 n = min(nblocks, (s64)BPERDMAP - n);
3613 /* assign/init dmap page */
3614 mp = read_metapage(ipbmap, p,
3619 n = min_t(s64, nblocks, BPERDMAP);
3622 dp = (struct dmap *) mp->data;
3623 *l0leaf = dbInitDmap(dp, blkno, n);
3626 agno = le64_to_cpu(dp->start) >> l2agsize;
3627 bmp->db_agfree[agno] += n;
3638 } /* for each dmap in a L0 */
3641 * build current L0 page from its leaves, and
3642 * initialize corresponding parent L1 leaf
3644 *l1leaf = dbInitDmapCtl(l0dcp, 0, ++i);
3645 write_metapage(l0mp);
3649 l1leaf++; /* continue for next L0 */
3651 /* more than 1 L0 ? */
3653 break; /* build L1 page */
3655 /* summarize in global bmap page */
3656 bmp->db_maxfreebud = *l1leaf;
3657 release_metapage(l1mp);
3658 release_metapage(l2mp);
3662 } /* for each L0 in a L1 */
3665 * build current L1 page from its leaves, and
3666 * initialize corresponding parent L2 leaf
3668 *l2leaf = dbInitDmapCtl(l1dcp, 1, ++j);
3669 write_metapage(l1mp);
3673 l2leaf++; /* continue for next L1 */
3675 /* more than 1 L1 ? */
3677 break; /* build L2 page */
3679 /* summarize in global bmap page */
3680 bmp->db_maxfreebud = *l2leaf;
3681 release_metapage(l2mp);
3685 } /* for each L1 in a L2 */
3687 jfs_error(ipbmap->i_sb, "function has not returned as expected\n");
3690 release_metapage(l0mp);
3692 release_metapage(l1mp);
3693 release_metapage(l2mp);
3697 * finalize bmap control page
3708 void dbFinalizeBmap(struct inode *ipbmap)
3710 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
3711 int actags, inactags, l2nl;
3712 s64 ag_rem, actfree, inactfree, avgfree;
3716 * finalize bmap control page
3720 * compute db_agpref: preferred ag to allocate from
3721 * (the leftmost ag with average free space in it);
3724 /* get the number of active ags and inacitve ags */
3725 actags = bmp->db_maxag + 1;
3726 inactags = bmp->db_numag - actags;
3727 ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */
3729 /* determine how many blocks are in the inactive allocation
3730 * groups. in doing this, we must account for the fact that
3731 * the rightmost group might be a partial group (i.e. file
3732 * system size is not a multiple of the group size).
3734 inactfree = (inactags && ag_rem) ?
3735 ((inactags - 1) << bmp->db_agl2size) + ag_rem
3736 : inactags << bmp->db_agl2size;
3738 /* determine how many free blocks are in the active
3739 * allocation groups plus the average number of free blocks
3740 * within the active ags.
3742 actfree = bmp->db_nfree - inactfree;
3743 avgfree = (u32) actfree / (u32) actags;
3745 /* if the preferred allocation group has not average free space.
3746 * re-establish the preferred group as the leftmost
3747 * group with average free space.
3749 if (bmp->db_agfree[bmp->db_agpref] < avgfree) {
3750 for (bmp->db_agpref = 0; bmp->db_agpref < actags;
3752 if (bmp->db_agfree[bmp->db_agpref] >= avgfree)
3755 if (bmp->db_agpref >= bmp->db_numag) {
3756 jfs_error(ipbmap->i_sb,
3757 "cannot find ag with average freespace\n");
3762 * compute db_aglevel, db_agheight, db_width, db_agstart:
3763 * an ag is covered in aglevel dmapctl summary tree,
3764 * at agheight level height (from leaf) with agwidth number of nodes
3765 * each, which starts at agstart index node of the smmary tree node
3768 bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize);
3770 bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL);
3771 bmp->db_agheight = l2nl >> 1;
3772 bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1));
3773 for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0;
3775 bmp->db_agstart += n;
3783 * NAME: dbInitDmap()/ujfs_idmap_page()
3785 * FUNCTION: initialize working/persistent bitmap of the dmap page
3786 * for the specified number of blocks:
3788 * at entry, the bitmaps had been initialized as free (ZEROS);
3789 * The number of blocks will only account for the actually
3790 * existing blocks. Blocks which don't actually exist in
3791 * the aggregate will be marked as allocated (ONES);
3794 * dp - pointer to page of map
3795 * nblocks - number of blocks this page
3799 static int dbInitDmap(struct dmap * dp, s64 Blkno, int nblocks)
3801 int blkno, w, b, r, nw, nb, i;
3803 /* starting block number within the dmap */
3804 blkno = Blkno & (BPERDMAP - 1);
3807 dp->nblocks = dp->nfree = cpu_to_le32(nblocks);
3808 dp->start = cpu_to_le64(Blkno);
3810 if (nblocks == BPERDMAP) {
3811 memset(&dp->wmap[0], 0, LPERDMAP * 4);
3812 memset(&dp->pmap[0], 0, LPERDMAP * 4);
3816 le32_add_cpu(&dp->nblocks, nblocks);
3817 le32_add_cpu(&dp->nfree, nblocks);
3820 /* word number containing start block number */
3821 w = blkno >> L2DBWORD;
3824 * free the bits corresponding to the block range (ZEROS):
3825 * note: not all bits of the first and last words may be contained
3826 * within the block range.
3828 for (r = nblocks; r > 0; r -= nb, blkno += nb) {
3829 /* number of bits preceding range to be freed in the word */
3830 b = blkno & (DBWORD - 1);
3831 /* number of bits to free in the word */
3832 nb = min(r, DBWORD - b);
3834 /* is partial word to be freed ? */
3836 /* free (set to 0) from the bitmap word */
3837 dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3839 dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3842 /* skip the word freed */
3845 /* free (set to 0) contiguous bitmap words */
3847 memset(&dp->wmap[w], 0, nw * 4);
3848 memset(&dp->pmap[w], 0, nw * 4);
3850 /* skip the words freed */
3851 nb = nw << L2DBWORD;
3857 * mark bits following the range to be freed (non-existing
3858 * blocks) as allocated (ONES)
3861 if (blkno == BPERDMAP)
3864 /* the first word beyond the end of existing blocks */
3865 w = blkno >> L2DBWORD;
3867 /* does nblocks fall on a 32-bit boundary ? */
3868 b = blkno & (DBWORD - 1);
3870 /* mark a partial word allocated */
3871 dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b);
3875 /* set the rest of the words in the page to allocated (ONES) */
3876 for (i = w; i < LPERDMAP; i++)
3877 dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES);
3883 return (dbInitDmapTree(dp));
3888 * NAME: dbInitDmapTree()/ujfs_complete_dmap()
3890 * FUNCTION: initialize summary tree of the specified dmap:
3892 * at entry, bitmap of the dmap has been initialized;
3895 * dp - dmap to complete
3896 * blkno - starting block number for this dmap
3897 * treemax - will be filled in with max free for this dmap
3899 * RETURNS: max free string at the root of the tree
3901 static int dbInitDmapTree(struct dmap * dp)
3903 struct dmaptree *tp;
3907 /* init fixed info of tree */
3909 tp->nleafs = cpu_to_le32(LPERDMAP);
3910 tp->l2nleafs = cpu_to_le32(L2LPERDMAP);
3911 tp->leafidx = cpu_to_le32(LEAFIND);
3912 tp->height = cpu_to_le32(4);
3913 tp->budmin = BUDMIN;
3915 /* init each leaf from corresponding wmap word:
3916 * note: leaf is set to NOFREE(-1) if all blocks of corresponding
3917 * bitmap word are allocated.
3919 cp = tp->stree + le32_to_cpu(tp->leafidx);
3920 for (i = 0; i < LPERDMAP; i++)
3921 *cp++ = dbMaxBud((u8 *) & dp->wmap[i]);
3923 /* build the dmap's binary buddy summary tree */
3924 return (dbInitTree(tp));
3929 * NAME: dbInitTree()/ujfs_adjtree()
3931 * FUNCTION: initialize binary buddy summary tree of a dmap or dmapctl.
3933 * at entry, the leaves of the tree has been initialized
3934 * from corresponding bitmap word or root of summary tree
3935 * of the child control page;
3936 * configure binary buddy system at the leaf level, then
3937 * bubble up the values of the leaf nodes up the tree.
3940 * cp - Pointer to the root of the tree
3941 * l2leaves- Number of leaf nodes as a power of 2
3942 * l2min - Number of blocks that can be covered by a leaf
3945 * RETURNS: max free string at the root of the tree
3947 static int dbInitTree(struct dmaptree * dtp)
3949 int l2max, l2free, bsize, nextb, i;
3950 int child, parent, nparent;
3955 /* Determine the maximum free string possible for the leaves */
3956 l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin;
3959 * configure the leaf levevl into binary buddy system
3961 * Try to combine buddies starting with a buddy size of 1
3962 * (i.e. two leaves). At a buddy size of 1 two buddy leaves
3963 * can be combined if both buddies have a maximum free of l2min;
3964 * the combination will result in the left-most buddy leaf having
3965 * a maximum free of l2min+1.
3966 * After processing all buddies for a given size, process buddies
3967 * at the next higher buddy size (i.e. current size * 2) and
3968 * the next maximum free (current free + 1).
3969 * This continues until the maximum possible buddy combination
3970 * yields maximum free.
3972 for (l2free = dtp->budmin, bsize = 1; l2free < l2max;
3973 l2free++, bsize = nextb) {
3974 /* get next buddy size == current buddy pair size */
3977 /* scan each adjacent buddy pair at current buddy size */
3978 for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx);
3979 i < le32_to_cpu(dtp->nleafs);
3980 i += nextb, cp += nextb) {
3981 /* coalesce if both adjacent buddies are max free */
3982 if (*cp == l2free && *(cp + bsize) == l2free) {
3983 *cp = l2free + 1; /* left take right */
3984 *(cp + bsize) = -1; /* right give left */
3990 * bubble summary information of leaves up the tree.
3992 * Starting at the leaf node level, the four nodes described by
3993 * the higher level parent node are compared for a maximum free and
3994 * this maximum becomes the value of the parent node.
3995 * when all lower level nodes are processed in this fashion then
3996 * move up to the next level (parent becomes a lower level node) and
3997 * continue the process for that level.
3999 for (child = le32_to_cpu(dtp->leafidx),
4000 nparent = le32_to_cpu(dtp->nleafs) >> 2;
4001 nparent > 0; nparent >>= 2, child = parent) {
4002 /* get index of 1st node of parent level */
4003 parent = (child - 1) >> 2;
4005 /* set the value of the parent node as the maximum
4006 * of the four nodes of the current level.
4008 for (i = 0, cp = tp + child, cp1 = tp + parent;
4009 i < nparent; i++, cp += 4, cp1++)
4020 * function: initialize dmapctl page
4022 static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i)
4023 { /* start leaf index not covered by range */
4026 dcp->nleafs = cpu_to_le32(LPERCTL);
4027 dcp->l2nleafs = cpu_to_le32(L2LPERCTL);
4028 dcp->leafidx = cpu_to_le32(CTLLEAFIND);
4029 dcp->height = cpu_to_le32(5);
4030 dcp->budmin = L2BPERDMAP + L2LPERCTL * level;
4033 * initialize the leaves of current level that were not covered
4034 * by the specified input block range (i.e. the leaves have no
4035 * low level dmapctl or dmap).
4037 cp = &dcp->stree[CTLLEAFIND + i];
4038 for (; i < LPERCTL; i++)
4041 /* build the dmap's binary buddy summary tree */
4042 return (dbInitTree((struct dmaptree *) dcp));
4047 * NAME: dbGetL2AGSize()/ujfs_getagl2size()
4049 * FUNCTION: Determine log2(allocation group size) from aggregate size
4052 * nblocks - Number of blocks in aggregate
4054 * RETURNS: log2(allocation group size) in aggregate blocks
4056 static int dbGetL2AGSize(s64 nblocks)
4062 if (nblocks < BPERDMAP * MAXAG)
4063 return (L2BPERDMAP);
4065 /* round up aggregate size to power of 2 */
4066 m = ((u64) 1 << (64 - 1));
4067 for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) {
4072 sz = (s64) 1 << l2sz;
4076 /* agsize = roundupSize/max_number_of_ag */
4077 return (l2sz - L2MAXAG);
4082 * NAME: dbMapFileSizeToMapSize()
4084 * FUNCTION: compute number of blocks the block allocation map file
4085 * can cover from the map file size;
4087 * RETURNS: Number of blocks which can be covered by this block map file;
4091 * maximum number of map pages at each level including control pages
4093 #define MAXL0PAGES (1 + LPERCTL)
4094 #define MAXL1PAGES (1 + LPERCTL * MAXL0PAGES)
4095 #define MAXL2PAGES (1 + LPERCTL * MAXL1PAGES)
4098 * convert number of map pages to the zero origin top dmapctl level
4100 #define BMAPPGTOLEV(npages) \
4101 (((npages) <= 3 + MAXL0PAGES) ? 0 : \
4102 ((npages) <= 2 + MAXL1PAGES) ? 1 : 2)
4104 s64 dbMapFileSizeToMapSize(struct inode * ipbmap)
4106 struct super_block *sb = ipbmap->i_sb;
4110 int complete, factor;
4112 nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize;
4113 npages = nblocks >> JFS_SBI(sb)->l2nbperpage;
4114 level = BMAPPGTOLEV(npages);
4116 /* At each level, accumulate the number of dmap pages covered by
4117 * the number of full child levels below it;
4118 * repeat for the last incomplete child level.
4121 npages--; /* skip the first global control page */
4122 /* skip higher level control pages above top level covered by map */
4123 npages -= (2 - level);
4124 npages--; /* skip top level's control page */
4125 for (i = level; i >= 0; i--) {
4127 (i == 2) ? MAXL1PAGES : ((i == 1) ? MAXL0PAGES : 1);
4128 complete = (u32) npages / factor;
4129 ndmaps += complete * ((i == 2) ? LPERCTL * LPERCTL :
4130 ((i == 1) ? LPERCTL : 1));
4132 /* pages in last/incomplete child */
4133 npages = (u32) npages % factor;
4134 /* skip incomplete child's level control page */
4138 /* convert the number of dmaps into the number of blocks
4139 * which can be covered by the dmaps;
4141 nblocks = ndmaps << L2BPERDMAP;