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
);
80 static int dbBackSplit(dmtree_t
* tp
, int leafno
);
81 static int dbJoin(dmtree_t
* tp
, int leafno
, int newval
);
82 static void dbAdjTree(dmtree_t
* tp
, int leafno
, int newval
);
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
);
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
165 int dbMount(struct inode
*ipbmap
)
168 struct dbmap_disk
*dbmp_le
;
173 * allocate/initialize the in-memory bmap descriptor
175 /* allocate memory for the in-memory bmap descriptor */
176 bmp
= kmalloc(sizeof(struct bmap
), GFP_KERNEL
);
180 /* read the on-disk bmap descriptor. */
181 mp
= read_metapage(ipbmap
,
182 BMAPBLKNO
<< JFS_SBI(ipbmap
->i_sb
)->l2nbperpage
,
189 /* copy the on-disk bmap descriptor to its in-memory version. */
190 dbmp_le
= (struct dbmap_disk
*) mp
->data
;
191 bmp
->db_mapsize
= le64_to_cpu(dbmp_le
->dn_mapsize
);
192 bmp
->db_nfree
= le64_to_cpu(dbmp_le
->dn_nfree
);
193 bmp
->db_l2nbperpage
= le32_to_cpu(dbmp_le
->dn_l2nbperpage
);
194 bmp
->db_numag
= le32_to_cpu(dbmp_le
->dn_numag
);
195 bmp
->db_maxlevel
= le32_to_cpu(dbmp_le
->dn_maxlevel
);
196 bmp
->db_maxag
= le32_to_cpu(dbmp_le
->dn_maxag
);
197 bmp
->db_agpref
= le32_to_cpu(dbmp_le
->dn_agpref
);
198 bmp
->db_aglevel
= le32_to_cpu(dbmp_le
->dn_aglevel
);
199 bmp
->db_agheight
= le32_to_cpu(dbmp_le
->dn_agheight
);
200 bmp
->db_agwidth
= le32_to_cpu(dbmp_le
->dn_agwidth
);
201 bmp
->db_agstart
= le32_to_cpu(dbmp_le
->dn_agstart
);
202 bmp
->db_agl2size
= le32_to_cpu(dbmp_le
->dn_agl2size
);
203 for (i
= 0; i
< MAXAG
; i
++)
204 bmp
->db_agfree
[i
] = le64_to_cpu(dbmp_le
->dn_agfree
[i
]);
205 bmp
->db_agsize
= le64_to_cpu(dbmp_le
->dn_agsize
);
206 bmp
->db_maxfreebud
= dbmp_le
->dn_maxfreebud
;
208 /* release the buffer. */
209 release_metapage(mp
);
211 /* bind the bmap inode and the bmap descriptor to each other. */
212 bmp
->db_ipbmap
= ipbmap
;
213 JFS_SBI(ipbmap
->i_sb
)->bmap
= bmp
;
215 memset(bmp
->db_active
, 0, sizeof(bmp
->db_active
));
218 * allocate/initialize the bmap lock
229 * FUNCTION: terminate the block allocation map in preparation for
230 * file system unmount.
232 * the in-core bmap descriptor is written to disk and
233 * the memory for this descriptor is freed.
236 * ipbmap - pointer to in-core inode for the block map.
242 int dbUnmount(struct inode
*ipbmap
, int mounterror
)
244 struct bmap
*bmp
= JFS_SBI(ipbmap
->i_sb
)->bmap
;
246 if (!(mounterror
|| isReadOnly(ipbmap
)))
250 * Invalidate the page cache buffers
252 truncate_inode_pages(ipbmap
->i_mapping
, 0);
254 /* free the memory for the in-memory bmap. */
263 int dbSync(struct inode
*ipbmap
)
265 struct dbmap_disk
*dbmp_le
;
266 struct bmap
*bmp
= JFS_SBI(ipbmap
->i_sb
)->bmap
;
271 * write bmap global control page
273 /* get the buffer for the on-disk bmap descriptor. */
274 mp
= read_metapage(ipbmap
,
275 BMAPBLKNO
<< JFS_SBI(ipbmap
->i_sb
)->l2nbperpage
,
278 jfs_err("dbSync: read_metapage failed!");
281 /* copy the in-memory version of the bmap to the on-disk version */
282 dbmp_le
= (struct dbmap_disk
*) mp
->data
;
283 dbmp_le
->dn_mapsize
= cpu_to_le64(bmp
->db_mapsize
);
284 dbmp_le
->dn_nfree
= cpu_to_le64(bmp
->db_nfree
);
285 dbmp_le
->dn_l2nbperpage
= cpu_to_le32(bmp
->db_l2nbperpage
);
286 dbmp_le
->dn_numag
= cpu_to_le32(bmp
->db_numag
);
287 dbmp_le
->dn_maxlevel
= cpu_to_le32(bmp
->db_maxlevel
);
288 dbmp_le
->dn_maxag
= cpu_to_le32(bmp
->db_maxag
);
289 dbmp_le
->dn_agpref
= cpu_to_le32(bmp
->db_agpref
);
290 dbmp_le
->dn_aglevel
= cpu_to_le32(bmp
->db_aglevel
);
291 dbmp_le
->dn_agheight
= cpu_to_le32(bmp
->db_agheight
);
292 dbmp_le
->dn_agwidth
= cpu_to_le32(bmp
->db_agwidth
);
293 dbmp_le
->dn_agstart
= cpu_to_le32(bmp
->db_agstart
);
294 dbmp_le
->dn_agl2size
= cpu_to_le32(bmp
->db_agl2size
);
295 for (i
= 0; i
< MAXAG
; i
++)
296 dbmp_le
->dn_agfree
[i
] = cpu_to_le64(bmp
->db_agfree
[i
]);
297 dbmp_le
->dn_agsize
= cpu_to_le64(bmp
->db_agsize
);
298 dbmp_le
->dn_maxfreebud
= bmp
->db_maxfreebud
;
300 /* write the buffer */
304 * write out dirty pages of bmap
306 filemap_write_and_wait(ipbmap
->i_mapping
);
308 diWriteSpecial(ipbmap
, 0);
316 * FUNCTION: free the specified block range from the working block
319 * the blocks will be free from the working map one dmap
323 * ip - pointer to in-core inode;
324 * blkno - starting block number to be freed.
325 * nblocks - number of blocks to be freed.
331 int dbFree(struct inode
*ip
, s64 blkno
, s64 nblocks
)
337 struct inode
*ipbmap
= JFS_SBI(ip
->i_sb
)->ipbmap
;
338 struct bmap
*bmp
= JFS_SBI(ip
->i_sb
)->bmap
;
339 struct super_block
*sb
= ipbmap
->i_sb
;
341 IREAD_LOCK(ipbmap
, RDWRLOCK_DMAP
);
343 /* block to be freed better be within the mapsize. */
344 if (unlikely((blkno
== 0) || (blkno
+ nblocks
> bmp
->db_mapsize
))) {
345 IREAD_UNLOCK(ipbmap
);
346 printk(KERN_ERR
"blkno = %Lx, nblocks = %Lx\n",
347 (unsigned long long) blkno
,
348 (unsigned long long) nblocks
);
350 "dbFree: block to be freed is outside the map");
355 * TRIM the blocks, when mounted with discard option
357 if (JFS_SBI(sb
)->flag
& JFS_DISCARD
)
358 if (JFS_SBI(sb
)->minblks_trim
<= nblocks
)
359 jfs_issue_discard(ipbmap
, blkno
, nblocks
);
362 * free the blocks a dmap at a time.
365 for (rem
= nblocks
; rem
> 0; rem
-= nb
, blkno
+= nb
) {
366 /* release previous dmap if any */
371 /* get the buffer for the current dmap. */
372 lblkno
= BLKTODMAP(blkno
, bmp
->db_l2nbperpage
);
373 mp
= read_metapage(ipbmap
, lblkno
, PSIZE
, 0);
375 IREAD_UNLOCK(ipbmap
);
378 dp
= (struct dmap
*) mp
->data
;
380 /* determine the number of blocks to be freed from
383 nb
= min(rem
, BPERDMAP
- (blkno
& (BPERDMAP
- 1)));
385 /* free the blocks. */
386 if ((rc
= dbFreeDmap(bmp
, dp
, blkno
, nb
))) {
387 jfs_error(ip
->i_sb
, "dbFree: error in block map\n");
388 release_metapage(mp
);
389 IREAD_UNLOCK(ipbmap
);
394 /* write the last buffer. */
397 IREAD_UNLOCK(ipbmap
);
404 * NAME: dbUpdatePMap()
406 * FUNCTION: update the allocation state (free or allocate) of the
407 * specified block range in the persistent block allocation map.
409 * the blocks will be updated in the persistent map one
413 * ipbmap - pointer to in-core inode for the block map.
414 * free - 'true' if block range is to be freed from the persistent
415 * map; 'false' if it is to be allocated.
416 * blkno - starting block number of the range.
417 * nblocks - number of contiguous blocks in the range.
418 * tblk - transaction block;
425 dbUpdatePMap(struct inode
*ipbmap
,
426 int free
, s64 blkno
, s64 nblocks
, struct tblock
* tblk
)
428 int nblks
, dbitno
, wbitno
, rbits
;
429 int word
, nbits
, nwords
;
430 struct bmap
*bmp
= JFS_SBI(ipbmap
->i_sb
)->bmap
;
431 s64 lblkno
, rem
, lastlblkno
;
436 int lsn
, difft
, diffp
;
439 /* the blocks better be within the mapsize. */
440 if (blkno
+ nblocks
> bmp
->db_mapsize
) {
441 printk(KERN_ERR
"blkno = %Lx, nblocks = %Lx\n",
442 (unsigned long long) blkno
,
443 (unsigned long long) nblocks
);
444 jfs_error(ipbmap
->i_sb
,
445 "dbUpdatePMap: blocks are outside the map");
449 /* compute delta of transaction lsn from log syncpt */
451 log
= (struct jfs_log
*) JFS_SBI(tblk
->sb
)->log
;
452 logdiff(difft
, lsn
, log
);
455 * update the block state a dmap at a time.
459 for (rem
= nblocks
; rem
> 0; rem
-= nblks
, blkno
+= nblks
) {
460 /* get the buffer for the current dmap. */
461 lblkno
= BLKTODMAP(blkno
, bmp
->db_l2nbperpage
);
462 if (lblkno
!= lastlblkno
) {
467 mp
= read_metapage(bmp
->db_ipbmap
, lblkno
, PSIZE
,
471 metapage_wait_for_io(mp
);
473 dp
= (struct dmap
*) mp
->data
;
475 /* determine the bit number and word within the dmap of
476 * the starting block. also determine how many blocks
477 * are to be updated within this dmap.
479 dbitno
= blkno
& (BPERDMAP
- 1);
480 word
= dbitno
>> L2DBWORD
;
481 nblks
= min(rem
, (s64
)BPERDMAP
- dbitno
);
483 /* update the bits of the dmap words. the first and last
484 * words may only have a subset of their bits updated. if
485 * this is the case, we'll work against that word (i.e.
486 * partial first and/or last) only in a single pass. a
487 * single pass will also be used to update all words that
488 * are to have all their bits updated.
490 for (rbits
= nblks
; rbits
> 0;
491 rbits
-= nbits
, dbitno
+= nbits
) {
492 /* determine the bit number within the word and
493 * the number of bits within the word.
495 wbitno
= dbitno
& (DBWORD
- 1);
496 nbits
= min(rbits
, DBWORD
- wbitno
);
498 /* check if only part of the word is to be updated. */
499 if (nbits
< DBWORD
) {
500 /* update (free or allocate) the bits
504 (ONES
<< (DBWORD
- nbits
) >> wbitno
);
514 /* one or more words are to have all
515 * their bits updated. determine how
516 * many words and how many bits.
518 nwords
= rbits
>> L2DBWORD
;
519 nbits
= nwords
<< L2DBWORD
;
521 /* update (free or allocate) the bits
525 memset(&dp
->pmap
[word
], 0,
528 memset(&dp
->pmap
[word
], (int) ONES
,
538 if (lblkno
== lastlblkno
)
543 LOGSYNC_LOCK(log
, flags
);
545 /* inherit older/smaller lsn */
546 logdiff(diffp
, mp
->lsn
, log
);
550 /* move bp after tblock in logsync list */
551 list_move(&mp
->synclist
, &tblk
->synclist
);
554 /* inherit younger/larger clsn */
555 logdiff(difft
, tblk
->clsn
, log
);
556 logdiff(diffp
, mp
->clsn
, log
);
558 mp
->clsn
= tblk
->clsn
;
563 /* insert bp after tblock in logsync list */
565 list_add(&mp
->synclist
, &tblk
->synclist
);
567 mp
->clsn
= tblk
->clsn
;
569 LOGSYNC_UNLOCK(log
, flags
);
572 /* write the last buffer. */
584 * FUNCTION: find the preferred allocation group for new allocations.
586 * Within the allocation groups, we maintain a preferred
587 * allocation group which consists of a group with at least
588 * average free space. It is the preferred group that we target
589 * new inode allocation towards. The tie-in between inode
590 * allocation and block allocation occurs as we allocate the
591 * first (data) block of an inode and specify the inode (block)
592 * as the allocation hint for this block.
594 * We try to avoid having more than one open file growing in
595 * an allocation group, as this will lead to fragmentation.
596 * This differs from the old OS/2 method of trying to keep
597 * empty ags around for large allocations.
600 * ipbmap - pointer to in-core inode for the block map.
603 * the preferred allocation group number.
605 int dbNextAG(struct inode
*ipbmap
)
612 struct bmap
*bmp
= JFS_SBI(ipbmap
->i_sb
)->bmap
;
616 /* determine the average number of free blocks within the ags. */
617 avgfree
= (u32
)bmp
->db_nfree
/ bmp
->db_numag
;
620 * if the current preferred ag does not have an active allocator
621 * and has at least average freespace, return it
623 agpref
= bmp
->db_agpref
;
624 if ((atomic_read(&bmp
->db_active
[agpref
]) == 0) &&
625 (bmp
->db_agfree
[agpref
] >= avgfree
))
628 /* From the last preferred ag, find the next one with at least
629 * average free space.
631 for (i
= 0 ; i
< bmp
->db_numag
; i
++, agpref
++) {
632 if (agpref
== bmp
->db_numag
)
635 if (atomic_read(&bmp
->db_active
[agpref
]))
636 /* open file is currently growing in this ag */
638 if (bmp
->db_agfree
[agpref
] >= avgfree
) {
639 /* Return this one */
640 bmp
->db_agpref
= agpref
;
642 } else if (bmp
->db_agfree
[agpref
] > hwm
) {
643 /* Less than avg. freespace, but best so far */
644 hwm
= bmp
->db_agfree
[agpref
];
650 * If no inactive ag was found with average freespace, use the
654 bmp
->db_agpref
= next_best
;
655 /* else leave db_agpref unchanged */
659 /* return the preferred group.
661 return (bmp
->db_agpref
);
667 * FUNCTION: attempt to allocate a specified number of contiguous free
668 * blocks from the working allocation block map.
670 * the block allocation policy uses hints and a multi-step
673 * for allocation requests smaller than the number of blocks
674 * per dmap, we first try to allocate the new blocks
675 * immediately following the hint. if these blocks are not
676 * available, we try to allocate blocks near the hint. if
677 * no blocks near the hint are available, we next try to
678 * allocate within the same dmap as contains the hint.
680 * if no blocks are available in the dmap or the allocation
681 * request is larger than the dmap size, we try to allocate
682 * within the same allocation group as contains the hint. if
683 * this does not succeed, we finally try to allocate anywhere
684 * within the aggregate.
686 * we also try to allocate anywhere within the aggregate for
687 * for allocation requests larger than the allocation group
688 * size or requests that specify no hint value.
691 * ip - pointer to in-core inode;
692 * hint - allocation hint.
693 * nblocks - number of contiguous blocks in the range.
694 * results - on successful return, set to the starting block number
695 * of the newly allocated contiguous range.
699 * -ENOSPC - insufficient disk resources
702 int dbAlloc(struct inode
*ip
, s64 hint
, s64 nblocks
, s64
* results
)
705 struct inode
*ipbmap
= JFS_SBI(ip
->i_sb
)->ipbmap
;
714 /* assert that nblocks is valid */
717 /* get the log2 number of blocks to be allocated.
718 * if the number of blocks is not a log2 multiple,
719 * it will be rounded up to the next log2 multiple.
721 l2nb
= BLKSTOL2(nblocks
);
723 bmp
= JFS_SBI(ip
->i_sb
)->bmap
;
725 mapSize
= bmp
->db_mapsize
;
727 /* the hint should be within the map */
728 if (hint
>= mapSize
) {
729 jfs_error(ip
->i_sb
, "dbAlloc: the hint is outside the map");
733 /* if the number of blocks to be allocated is greater than the
734 * allocation group size, try to allocate anywhere.
736 if (l2nb
> bmp
->db_agl2size
) {
737 IWRITE_LOCK(ipbmap
, RDWRLOCK_DMAP
);
739 rc
= dbAllocAny(bmp
, nblocks
, l2nb
, results
);
745 * If no hint, let dbNextAG recommend an allocation group
750 /* we would like to allocate close to the hint. adjust the
751 * hint to the block following the hint since the allocators
752 * will start looking for free space starting at this point.
756 if (blkno
>= bmp
->db_mapsize
)
759 agno
= blkno
>> bmp
->db_agl2size
;
761 /* check if blkno crosses over into a new allocation group.
762 * if so, check if we should allow allocations within this
765 if ((blkno
& (bmp
->db_agsize
- 1)) == 0)
766 /* check if the AG is currently being written to.
767 * if so, call dbNextAG() to find a non-busy
768 * AG with sufficient free space.
770 if (atomic_read(&bmp
->db_active
[agno
]))
773 /* check if the allocation request size can be satisfied from a
774 * single dmap. if so, try to allocate from the dmap containing
775 * the hint using a tiered strategy.
777 if (nblocks
<= BPERDMAP
) {
778 IREAD_LOCK(ipbmap
, RDWRLOCK_DMAP
);
780 /* get the buffer for the dmap containing the hint.
783 lblkno
= BLKTODMAP(blkno
, bmp
->db_l2nbperpage
);
784 mp
= read_metapage(ipbmap
, lblkno
, PSIZE
, 0);
788 dp
= (struct dmap
*) mp
->data
;
790 /* first, try to satisfy the allocation request with the
791 * blocks beginning at the hint.
793 if ((rc
= dbAllocNext(bmp
, dp
, blkno
, (int) nblocks
))
797 mark_metapage_dirty(mp
);
800 release_metapage(mp
);
804 writers
= atomic_read(&bmp
->db_active
[agno
]);
806 ((writers
== 1) && (JFS_IP(ip
)->active_ag
!= agno
))) {
808 * Someone else is writing in this allocation
809 * group. To avoid fragmenting, try another ag
811 release_metapage(mp
);
812 IREAD_UNLOCK(ipbmap
);
816 /* next, try to satisfy the allocation request with blocks
820 dbAllocNear(bmp
, dp
, blkno
, (int) nblocks
, l2nb
, results
))
823 mark_metapage_dirty(mp
);
825 release_metapage(mp
);
829 /* try to satisfy the allocation request with blocks within
830 * the same dmap as the hint.
832 if ((rc
= dbAllocDmapLev(bmp
, dp
, (int) nblocks
, l2nb
, results
))
835 mark_metapage_dirty(mp
);
837 release_metapage(mp
);
841 release_metapage(mp
);
842 IREAD_UNLOCK(ipbmap
);
845 /* try to satisfy the allocation request with blocks within
846 * the same allocation group as the hint.
848 IWRITE_LOCK(ipbmap
, RDWRLOCK_DMAP
);
849 if ((rc
= dbAllocAG(bmp
, agno
, nblocks
, l2nb
, results
)) != -ENOSPC
)
852 IWRITE_UNLOCK(ipbmap
);
857 * Let dbNextAG recommend a preferred allocation group
859 agno
= dbNextAG(ipbmap
);
860 IWRITE_LOCK(ipbmap
, RDWRLOCK_DMAP
);
862 /* Try to allocate within this allocation group. if that fails, try to
863 * allocate anywhere in the map.
865 if ((rc
= dbAllocAG(bmp
, agno
, nblocks
, l2nb
, results
)) == -ENOSPC
)
866 rc
= dbAllocAny(bmp
, nblocks
, l2nb
, results
);
869 IWRITE_UNLOCK(ipbmap
);
874 IREAD_UNLOCK(ipbmap
);
881 * NAME: dbAllocExact()
883 * FUNCTION: try to allocate the requested extent;
886 * ip - pointer to in-core inode;
887 * blkno - extent address;
888 * nblocks - extent length;
892 * -ENOSPC - insufficient disk resources
895 int dbAllocExact(struct inode
*ip
, s64 blkno
, int nblocks
)
898 struct inode
*ipbmap
= JFS_SBI(ip
->i_sb
)->ipbmap
;
899 struct bmap
*bmp
= JFS_SBI(ip
->i_sb
)->bmap
;
904 IREAD_LOCK(ipbmap
, RDWRLOCK_DMAP
);
907 * validate extent request:
909 * note: defragfs policy:
910 * max 64 blocks will be moved.
911 * allocation request size must be satisfied from a single dmap.
913 if (nblocks
<= 0 || nblocks
> BPERDMAP
|| blkno
>= bmp
->db_mapsize
) {
914 IREAD_UNLOCK(ipbmap
);
918 if (nblocks
> ((s64
) 1 << bmp
->db_maxfreebud
)) {
919 /* the free space is no longer available */
920 IREAD_UNLOCK(ipbmap
);
924 /* read in the dmap covering the extent */
925 lblkno
= BLKTODMAP(blkno
, bmp
->db_l2nbperpage
);
926 mp
= read_metapage(ipbmap
, lblkno
, PSIZE
, 0);
928 IREAD_UNLOCK(ipbmap
);
931 dp
= (struct dmap
*) mp
->data
;
933 /* try to allocate the requested extent */
934 rc
= dbAllocNext(bmp
, dp
, blkno
, nblocks
);
936 IREAD_UNLOCK(ipbmap
);
939 mark_metapage_dirty(mp
);
941 release_metapage(mp
);
950 * FUNCTION: attempt to extend a current allocation by a specified
953 * this routine attempts to satisfy the allocation request
954 * by first trying to extend the existing allocation in
955 * place by allocating the additional blocks as the blocks
956 * immediately following the current allocation. if these
957 * blocks are not available, this routine will attempt to
958 * allocate a new set of contiguous blocks large enough
959 * to cover the existing allocation plus the additional
960 * number of blocks required.
963 * ip - pointer to in-core inode requiring allocation.
964 * blkno - starting block of the current allocation.
965 * nblocks - number of contiguous blocks within the current
967 * addnblocks - number of blocks to add to the allocation.
968 * results - on successful return, set to the starting block number
969 * of the existing allocation if the existing allocation
970 * was extended in place or to a newly allocated contiguous
971 * range if the existing allocation could not be extended
976 * -ENOSPC - insufficient disk resources
980 dbReAlloc(struct inode
*ip
,
981 s64 blkno
, s64 nblocks
, s64 addnblocks
, s64
* results
)
985 /* try to extend the allocation in place.
987 if ((rc
= dbExtend(ip
, blkno
, nblocks
, addnblocks
)) == 0) {
995 /* could not extend the allocation in place, so allocate a
996 * new set of blocks for the entire request (i.e. try to get
997 * a range of contiguous blocks large enough to cover the
998 * existing allocation plus the additional blocks.)
1001 (ip
, blkno
+ nblocks
- 1, addnblocks
+ nblocks
, results
));
1008 * FUNCTION: attempt to extend a current allocation by a specified
1011 * this routine attempts to satisfy the allocation request
1012 * by first trying to extend the existing allocation in
1013 * place by allocating the additional blocks as the blocks
1014 * immediately following the current allocation.
1017 * ip - pointer to in-core inode requiring allocation.
1018 * blkno - starting block of the current allocation.
1019 * nblocks - number of contiguous blocks within the current
1021 * addnblocks - number of blocks to add to the allocation.
1025 * -ENOSPC - insufficient disk resources
1028 static int dbExtend(struct inode
*ip
, s64 blkno
, s64 nblocks
, s64 addnblocks
)
1030 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
1031 s64 lblkno
, lastblkno
, extblkno
;
1033 struct metapage
*mp
;
1036 struct inode
*ipbmap
= sbi
->ipbmap
;
1040 * We don't want a non-aligned extent to cross a page boundary
1042 if (((rel_block
= blkno
& (sbi
->nbperpage
- 1))) &&
1043 (rel_block
+ nblocks
+ addnblocks
> sbi
->nbperpage
))
1046 /* get the last block of the current allocation */
1047 lastblkno
= blkno
+ nblocks
- 1;
1049 /* determine the block number of the block following
1050 * the existing allocation.
1052 extblkno
= lastblkno
+ 1;
1054 IREAD_LOCK(ipbmap
, RDWRLOCK_DMAP
);
1056 /* better be within the file system */
1058 if (lastblkno
< 0 || lastblkno
>= bmp
->db_mapsize
) {
1059 IREAD_UNLOCK(ipbmap
);
1061 "dbExtend: the block is outside the filesystem");
1065 /* we'll attempt to extend the current allocation in place by
1066 * allocating the additional blocks as the blocks immediately
1067 * following the current allocation. we only try to extend the
1068 * current allocation in place if the number of additional blocks
1069 * can fit into a dmap, the last block of the current allocation
1070 * is not the last block of the file system, and the start of the
1071 * inplace extension is not on an allocation group boundary.
1073 if (addnblocks
> BPERDMAP
|| extblkno
>= bmp
->db_mapsize
||
1074 (extblkno
& (bmp
->db_agsize
- 1)) == 0) {
1075 IREAD_UNLOCK(ipbmap
);
1079 /* get the buffer for the dmap containing the first block
1082 lblkno
= BLKTODMAP(extblkno
, bmp
->db_l2nbperpage
);
1083 mp
= read_metapage(ipbmap
, lblkno
, PSIZE
, 0);
1085 IREAD_UNLOCK(ipbmap
);
1089 dp
= (struct dmap
*) mp
->data
;
1091 /* try to allocate the blocks immediately following the
1092 * current allocation.
1094 rc
= dbAllocNext(bmp
, dp
, extblkno
, (int) addnblocks
);
1096 IREAD_UNLOCK(ipbmap
);
1098 /* were we successful ? */
1102 /* we were not successful */
1103 release_metapage(mp
);
1110 * NAME: dbAllocNext()
1112 * FUNCTION: attempt to allocate the blocks of the specified block
1113 * range within a dmap.
1116 * bmp - pointer to bmap descriptor
1117 * dp - pointer to dmap.
1118 * blkno - starting block number of the range.
1119 * nblocks - number of contiguous free blocks of the range.
1123 * -ENOSPC - insufficient disk resources
1126 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1128 static int dbAllocNext(struct bmap
* bmp
, struct dmap
* dp
, s64 blkno
,
1131 int dbitno
, word
, rembits
, nb
, nwords
, wbitno
, nw
;
1136 if (dp
->tree
.leafidx
!= cpu_to_le32(LEAFIND
)) {
1137 jfs_error(bmp
->db_ipbmap
->i_sb
,
1138 "dbAllocNext: Corrupt dmap page");
1142 /* pick up a pointer to the leaves of the dmap tree.
1144 leaf
= dp
->tree
.stree
+ le32_to_cpu(dp
->tree
.leafidx
);
1146 /* determine the bit number and word within the dmap of the
1149 dbitno
= blkno
& (BPERDMAP
- 1);
1150 word
= dbitno
>> L2DBWORD
;
1152 /* check if the specified block range is contained within
1155 if (dbitno
+ nblocks
> BPERDMAP
)
1158 /* check if the starting leaf indicates that anything
1161 if (leaf
[word
] == NOFREE
)
1164 /* check the dmaps words corresponding to block range to see
1165 * if the block range is free. not all bits of the first and
1166 * last words may be contained within the block range. if this
1167 * is the case, we'll work against those words (i.e. partial first
1168 * and/or last) on an individual basis (a single pass) and examine
1169 * the actual bits to determine if they are free. a single pass
1170 * will be used for all dmap words fully contained within the
1171 * specified range. within this pass, the leaves of the dmap
1172 * tree will be examined to determine if the blocks are free. a
1173 * single leaf may describe the free space of multiple dmap
1174 * words, so we may visit only a subset of the actual leaves
1175 * corresponding to the dmap words of the block range.
1177 for (rembits
= nblocks
; rembits
> 0; rembits
-= nb
, dbitno
+= nb
) {
1178 /* determine the bit number within the word and
1179 * the number of bits within the word.
1181 wbitno
= dbitno
& (DBWORD
- 1);
1182 nb
= min(rembits
, DBWORD
- wbitno
);
1184 /* check if only part of the word is to be examined.
1187 /* check if the bits are free.
1189 mask
= (ONES
<< (DBWORD
- nb
) >> wbitno
);
1190 if ((mask
& ~le32_to_cpu(dp
->wmap
[word
])) != mask
)
1195 /* one or more dmap words are fully contained
1196 * within the block range. determine how many
1197 * words and how many bits.
1199 nwords
= rembits
>> L2DBWORD
;
1200 nb
= nwords
<< L2DBWORD
;
1202 /* now examine the appropriate leaves to determine
1203 * if the blocks are free.
1205 while (nwords
> 0) {
1206 /* does the leaf describe any free space ?
1208 if (leaf
[word
] < BUDMIN
)
1211 /* determine the l2 number of bits provided
1215 min((int)leaf
[word
], NLSTOL2BSZ(nwords
));
1217 /* determine how many words were handled.
1219 nw
= BUDSIZE(l2size
, BUDMIN
);
1227 /* allocate the blocks.
1229 return (dbAllocDmap(bmp
, dp
, blkno
, nblocks
));
1234 * NAME: dbAllocNear()
1236 * FUNCTION: attempt to allocate a number of contiguous free blocks near
1237 * a specified block (hint) within a dmap.
1239 * starting with the dmap leaf that covers the hint, we'll
1240 * check the next four contiguous leaves for sufficient free
1241 * space. if sufficient free space is found, we'll allocate
1242 * the desired free space.
1245 * bmp - pointer to bmap descriptor
1246 * dp - pointer to dmap.
1247 * blkno - block number to allocate near.
1248 * nblocks - actual number of contiguous free blocks desired.
1249 * l2nb - log2 number of contiguous free blocks desired.
1250 * results - on successful return, set to the starting block number
1251 * of the newly allocated range.
1255 * -ENOSPC - insufficient disk resources
1258 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1261 dbAllocNear(struct bmap
* bmp
,
1262 struct dmap
* dp
, s64 blkno
, int nblocks
, int l2nb
, s64
* results
)
1264 int word
, lword
, rc
;
1267 if (dp
->tree
.leafidx
!= cpu_to_le32(LEAFIND
)) {
1268 jfs_error(bmp
->db_ipbmap
->i_sb
,
1269 "dbAllocNear: Corrupt dmap page");
1273 leaf
= dp
->tree
.stree
+ le32_to_cpu(dp
->tree
.leafidx
);
1275 /* determine the word within the dmap that holds the hint
1276 * (i.e. blkno). also, determine the last word in the dmap
1277 * that we'll include in our examination.
1279 word
= (blkno
& (BPERDMAP
- 1)) >> L2DBWORD
;
1280 lword
= min(word
+ 4, LPERDMAP
);
1282 /* examine the leaves for sufficient free space.
1284 for (; word
< lword
; word
++) {
1285 /* does the leaf describe sufficient free space ?
1287 if (leaf
[word
] < l2nb
)
1290 /* determine the block number within the file system
1291 * of the first block described by this dmap word.
1293 blkno
= le64_to_cpu(dp
->start
) + (word
<< L2DBWORD
);
1295 /* if not all bits of the dmap word are free, get the
1296 * starting bit number within the dmap word of the required
1297 * string of free bits and adjust the block number with the
1300 if (leaf
[word
] < BUDMIN
)
1302 dbFindBits(le32_to_cpu(dp
->wmap
[word
]), l2nb
);
1304 /* allocate the blocks.
1306 if ((rc
= dbAllocDmap(bmp
, dp
, blkno
, nblocks
)) == 0)
1319 * FUNCTION: attempt to allocate the specified number of contiguous
1320 * free blocks within the specified allocation group.
1322 * unless the allocation group size is equal to the number
1323 * of blocks per dmap, the dmap control pages will be used to
1324 * find the required free space, if available. we start the
1325 * search at the highest dmap control page level which
1326 * distinctly describes the allocation group's free space
1327 * (i.e. the highest level at which the allocation group's
1328 * free space is not mixed in with that of any other group).
1329 * in addition, we start the search within this level at a
1330 * height of the dmapctl dmtree at which the nodes distinctly
1331 * describe the allocation group's free space. at this height,
1332 * the allocation group's free space may be represented by 1
1333 * or two sub-trees, depending on the allocation group size.
1334 * we search the top nodes of these subtrees left to right for
1335 * sufficient free space. if sufficient free space is found,
1336 * the subtree is searched to find the leftmost leaf that
1337 * has free space. once we have made it to the leaf, we
1338 * move the search to the next lower level dmap control page
1339 * corresponding to this leaf. we continue down the dmap control
1340 * pages until we find the dmap that contains or starts the
1341 * sufficient free space and we allocate at this dmap.
1343 * if the allocation group size is equal to the dmap size,
1344 * we'll start at the dmap corresponding to the allocation
1345 * group and attempt the allocation at this level.
1347 * the dmap control page search is also not performed if the
1348 * allocation group is completely free and we go to the first
1349 * dmap of the allocation group to do the allocation. this is
1350 * done because the allocation group may be part (not the first
1351 * part) of a larger binary buddy system, causing the dmap
1352 * control pages to indicate no free space (NOFREE) within
1353 * the allocation group.
1356 * bmp - pointer to bmap descriptor
1357 * agno - allocation group number.
1358 * nblocks - actual number of contiguous free blocks desired.
1359 * l2nb - log2 number of contiguous free blocks desired.
1360 * results - on successful return, set to the starting block number
1361 * of the newly allocated range.
1365 * -ENOSPC - insufficient disk resources
1368 * note: IWRITE_LOCK(ipmap) held on entry/exit;
1371 dbAllocAG(struct bmap
* bmp
, int agno
, s64 nblocks
, int l2nb
, s64
* results
)
1373 struct metapage
*mp
;
1374 struct dmapctl
*dcp
;
1375 int rc
, ti
, i
, k
, m
, n
, agperlev
;
1379 /* allocation request should not be for more than the
1380 * allocation group size.
1382 if (l2nb
> bmp
->db_agl2size
) {
1383 jfs_error(bmp
->db_ipbmap
->i_sb
,
1384 "dbAllocAG: allocation request is larger than the "
1385 "allocation group size");
1389 /* determine the starting block number of the allocation
1392 blkno
= (s64
) agno
<< bmp
->db_agl2size
;
1394 /* check if the allocation group size is the minimum allocation
1395 * group size or if the allocation group is completely free. if
1396 * the allocation group size is the minimum size of BPERDMAP (i.e.
1397 * 1 dmap), there is no need to search the dmap control page (below)
1398 * that fully describes the allocation group since the allocation
1399 * group is already fully described by a dmap. in this case, we
1400 * just call dbAllocCtl() to search the dmap tree and allocate the
1401 * required space if available.
1403 * if the allocation group is completely free, dbAllocCtl() is
1404 * also called to allocate the required space. this is done for
1405 * two reasons. first, it makes no sense searching the dmap control
1406 * pages for free space when we know that free space exists. second,
1407 * the dmap control pages may indicate that the allocation group
1408 * has no free space if the allocation group is part (not the first
1409 * part) of a larger binary buddy system.
1411 if (bmp
->db_agsize
== BPERDMAP
1412 || bmp
->db_agfree
[agno
] == bmp
->db_agsize
) {
1413 rc
= dbAllocCtl(bmp
, nblocks
, l2nb
, blkno
, results
);
1414 if ((rc
== -ENOSPC
) &&
1415 (bmp
->db_agfree
[agno
] == bmp
->db_agsize
)) {
1416 printk(KERN_ERR
"blkno = %Lx, blocks = %Lx\n",
1417 (unsigned long long) blkno
,
1418 (unsigned long long) nblocks
);
1419 jfs_error(bmp
->db_ipbmap
->i_sb
,
1420 "dbAllocAG: dbAllocCtl failed in free AG");
1425 /* the buffer for the dmap control page that fully describes the
1428 lblkno
= BLKTOCTL(blkno
, bmp
->db_l2nbperpage
, bmp
->db_aglevel
);
1429 mp
= read_metapage(bmp
->db_ipbmap
, lblkno
, PSIZE
, 0);
1432 dcp
= (struct dmapctl
*) mp
->data
;
1433 budmin
= dcp
->budmin
;
1435 if (dcp
->leafidx
!= cpu_to_le32(CTLLEAFIND
)) {
1436 jfs_error(bmp
->db_ipbmap
->i_sb
,
1437 "dbAllocAG: Corrupt dmapctl page");
1438 release_metapage(mp
);
1442 /* search the subtree(s) of the dmap control page that describes
1443 * the allocation group, looking for sufficient free space. to begin,
1444 * determine how many allocation groups are represented in a dmap
1445 * control page at the control page level (i.e. L0, L1, L2) that
1446 * fully describes an allocation group. next, determine the starting
1447 * tree index of this allocation group within the control page.
1450 (1 << (L2LPERCTL
- (bmp
->db_agheight
<< 1))) / bmp
->db_agwidth
;
1451 ti
= bmp
->db_agstart
+ bmp
->db_agwidth
* (agno
& (agperlev
- 1));
1453 /* dmap control page trees fan-out by 4 and a single allocation
1454 * group may be described by 1 or 2 subtrees within the ag level
1455 * dmap control page, depending upon the ag size. examine the ag's
1456 * subtrees for sufficient free space, starting with the leftmost
1459 for (i
= 0; i
< bmp
->db_agwidth
; i
++, ti
++) {
1460 /* is there sufficient free space ?
1462 if (l2nb
> dcp
->stree
[ti
])
1465 /* sufficient free space found in a subtree. now search down
1466 * the subtree to find the leftmost leaf that describes this
1469 for (k
= bmp
->db_agheight
; k
> 0; k
--) {
1470 for (n
= 0, m
= (ti
<< 2) + 1; n
< 4; n
++) {
1471 if (l2nb
<= dcp
->stree
[m
+ n
]) {
1477 jfs_error(bmp
->db_ipbmap
->i_sb
,
1478 "dbAllocAG: failed descending stree");
1479 release_metapage(mp
);
1484 /* determine the block number within the file system
1485 * that corresponds to this leaf.
1487 if (bmp
->db_aglevel
== 2)
1489 else if (bmp
->db_aglevel
== 1)
1490 blkno
&= ~(MAXL1SIZE
- 1);
1491 else /* bmp->db_aglevel == 0 */
1492 blkno
&= ~(MAXL0SIZE
- 1);
1495 ((s64
) (ti
- le32_to_cpu(dcp
->leafidx
))) << budmin
;
1497 /* release the buffer in preparation for going down
1498 * the next level of dmap control pages.
1500 release_metapage(mp
);
1502 /* check if we need to continue to search down the lower
1503 * level dmap control pages. we need to if the number of
1504 * blocks required is less than maximum number of blocks
1505 * described at the next lower level.
1507 if (l2nb
< budmin
) {
1509 /* search the lower level dmap control pages to get
1510 * the starting block number of the dmap that
1511 * contains or starts off the free space.
1514 dbFindCtl(bmp
, l2nb
, bmp
->db_aglevel
- 1,
1516 if (rc
== -ENOSPC
) {
1517 jfs_error(bmp
->db_ipbmap
->i_sb
,
1518 "dbAllocAG: control page "
1526 /* allocate the blocks.
1528 rc
= dbAllocCtl(bmp
, nblocks
, l2nb
, blkno
, results
);
1529 if (rc
== -ENOSPC
) {
1530 jfs_error(bmp
->db_ipbmap
->i_sb
,
1531 "dbAllocAG: unable to allocate blocks");
1537 /* no space in the allocation group. release the buffer and
1540 release_metapage(mp
);
1547 * NAME: dbAllocAny()
1549 * FUNCTION: attempt to allocate the specified number of contiguous
1550 * free blocks anywhere in the file system.
1552 * dbAllocAny() attempts to find the sufficient free space by
1553 * searching down the dmap control pages, starting with the
1554 * highest level (i.e. L0, L1, L2) control page. if free space
1555 * large enough to satisfy the desired free space is found, the
1556 * desired free space is allocated.
1559 * bmp - pointer to bmap descriptor
1560 * nblocks - actual number of contiguous free blocks desired.
1561 * l2nb - log2 number of contiguous free blocks desired.
1562 * results - on successful return, set to the starting block number
1563 * of the newly allocated range.
1567 * -ENOSPC - insufficient disk resources
1570 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1572 static int dbAllocAny(struct bmap
* bmp
, s64 nblocks
, int l2nb
, s64
* results
)
1577 /* starting with the top level dmap control page, search
1578 * down the dmap control levels for sufficient free space.
1579 * if free space is found, dbFindCtl() returns the starting
1580 * block number of the dmap that contains or starts off the
1581 * range of free space.
1583 if ((rc
= dbFindCtl(bmp
, l2nb
, bmp
->db_maxlevel
, &blkno
)))
1586 /* allocate the blocks.
1588 rc
= dbAllocCtl(bmp
, nblocks
, l2nb
, blkno
, results
);
1589 if (rc
== -ENOSPC
) {
1590 jfs_error(bmp
->db_ipbmap
->i_sb
,
1591 "dbAllocAny: unable to allocate blocks");
1599 * NAME: dbDiscardAG()
1601 * FUNCTION: attempt to discard (TRIM) all free blocks of specific AG
1604 * 1) allocate blocks, as large as possible and save them
1605 * while holding IWRITE_LOCK on ipbmap
1606 * 2) trim all these saved block/length values
1607 * 3) mark the blocks free again
1610 * - we work only on one ag at some time, minimizing how long we
1611 * need to lock ipbmap
1612 * - reading / writing the fs is possible most time, even on
1616 * - we write two times to the dmapctl and dmap pages
1617 * - but for me, this seems the best way, better ideas?
1621 * ip - pointer to in-core inode
1623 * minlen - minimum value of contiguous blocks
1626 * s64 - actual number of blocks trimmed
1628 s64
dbDiscardAG(struct inode
*ip
, int agno
, s64 minlen
)
1630 struct inode
*ipbmap
= JFS_SBI(ip
->i_sb
)->ipbmap
;
1631 struct bmap
*bmp
= JFS_SBI(ip
->i_sb
)->bmap
;
1635 struct super_block
*sb
= ipbmap
->i_sb
;
1642 /* max blkno / nblocks pairs to trim */
1643 int count
= 0, range_cnt
;
1646 /* prevent others from writing new stuff here, while trimming */
1647 IWRITE_LOCK(ipbmap
, RDWRLOCK_DMAP
);
1649 nblocks
= bmp
->db_agfree
[agno
];
1650 max_ranges
= nblocks
;
1651 do_div(max_ranges
, minlen
);
1652 range_cnt
= min_t(u64
, max_ranges
+ 1, 32 * 1024);
1653 totrim
= kmalloc(sizeof(struct range2trim
) * range_cnt
, GFP_NOFS
);
1654 if (totrim
== NULL
) {
1655 jfs_error(bmp
->db_ipbmap
->i_sb
,
1656 "dbDiscardAG: no memory for trim array");
1657 IWRITE_UNLOCK(ipbmap
);
1662 while (nblocks
>= minlen
) {
1663 l2nb
= BLKSTOL2(nblocks
);
1665 /* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */
1666 rc
= dbAllocAG(bmp
, agno
, nblocks
, l2nb
, &blkno
);
1669 tt
->nblocks
= nblocks
;
1672 /* the whole ag is free, trim now */
1673 if (bmp
->db_agfree
[agno
] == 0)
1676 /* give a hint for the next while */
1677 nblocks
= bmp
->db_agfree
[agno
];
1679 } else if (rc
== -ENOSPC
) {
1680 /* search for next smaller log2 block */
1681 l2nb
= BLKSTOL2(nblocks
) - 1;
1682 nblocks
= 1 << l2nb
;
1684 /* Trim any already allocated blocks */
1685 jfs_error(bmp
->db_ipbmap
->i_sb
,
1686 "dbDiscardAG: -EIO");
1690 /* check, if our trim array is full */
1691 if (unlikely(count
>= range_cnt
- 1))
1694 IWRITE_UNLOCK(ipbmap
);
1696 tt
->nblocks
= 0; /* mark the current end */
1697 for (tt
= totrim
; tt
->nblocks
!= 0; tt
++) {
1698 /* when mounted with online discard, dbFree() will
1699 * call jfs_issue_discard() itself */
1700 if (!(JFS_SBI(sb
)->flag
& JFS_DISCARD
))
1701 jfs_issue_discard(ip
, tt
->blkno
, tt
->nblocks
);
1702 dbFree(ip
, tt
->blkno
, tt
->nblocks
);
1703 trimmed
+= tt
->nblocks
;
1713 * FUNCTION: starting at a specified dmap control page level and block
1714 * number, search down the dmap control levels for a range of
1715 * contiguous free blocks large enough to satisfy an allocation
1716 * request for the specified number of free blocks.
1718 * if sufficient contiguous free blocks are found, this routine
1719 * returns the starting block number within a dmap page that
1720 * contains or starts a range of contiqious free blocks that
1721 * is sufficient in size.
1724 * bmp - pointer to bmap descriptor
1725 * level - starting dmap control page level.
1726 * l2nb - log2 number of contiguous free blocks desired.
1727 * *blkno - on entry, starting block number for conducting the search.
1728 * on successful return, the first block within a dmap page
1729 * that contains or starts a range of contiguous free blocks.
1733 * -ENOSPC - insufficient disk resources
1736 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1738 static int dbFindCtl(struct bmap
* bmp
, int l2nb
, int level
, s64
* blkno
)
1740 int rc
, leafidx
, lev
;
1742 struct dmapctl
*dcp
;
1744 struct metapage
*mp
;
1746 /* starting at the specified dmap control page level and block
1747 * number, search down the dmap control levels for the starting
1748 * block number of a dmap page that contains or starts off
1749 * sufficient free blocks.
1751 for (lev
= level
, b
= *blkno
; lev
>= 0; lev
--) {
1752 /* get the buffer of the dmap control page for the block
1753 * number and level (i.e. L0, L1, L2).
1755 lblkno
= BLKTOCTL(b
, bmp
->db_l2nbperpage
, lev
);
1756 mp
= read_metapage(bmp
->db_ipbmap
, lblkno
, PSIZE
, 0);
1759 dcp
= (struct dmapctl
*) mp
->data
;
1760 budmin
= dcp
->budmin
;
1762 if (dcp
->leafidx
!= cpu_to_le32(CTLLEAFIND
)) {
1763 jfs_error(bmp
->db_ipbmap
->i_sb
,
1764 "dbFindCtl: Corrupt dmapctl page");
1765 release_metapage(mp
);
1769 /* search the tree within the dmap control page for
1770 * sufficient free space. if sufficient free space is found,
1771 * dbFindLeaf() returns the index of the leaf at which
1772 * free space was found.
1774 rc
= dbFindLeaf((dmtree_t
*) dcp
, l2nb
, &leafidx
);
1776 /* release the buffer.
1778 release_metapage(mp
);
1784 jfs_error(bmp
->db_ipbmap
->i_sb
,
1785 "dbFindCtl: dmap inconsistent");
1791 /* adjust the block number to reflect the location within
1792 * the dmap control page (i.e. the leaf) at which free
1795 b
+= (((s64
) leafidx
) << budmin
);
1797 /* we stop the search at this dmap control page level if
1798 * the number of blocks required is greater than or equal
1799 * to the maximum number of blocks described at the next
1812 * NAME: dbAllocCtl()
1814 * FUNCTION: attempt to allocate a specified number of contiguous
1815 * blocks starting within a specific dmap.
1817 * this routine is called by higher level routines that search
1818 * the dmap control pages above the actual dmaps for contiguous
1819 * free space. the result of successful searches by these
1820 * routines are the starting block numbers within dmaps, with
1821 * the dmaps themselves containing the desired contiguous free
1822 * space or starting a contiguous free space of desired size
1823 * that is made up of the blocks of one or more dmaps. these
1824 * calls should not fail due to insufficent resources.
1826 * this routine is called in some cases where it is not known
1827 * whether it will fail due to insufficient resources. more
1828 * specifically, this occurs when allocating from an allocation
1829 * group whose size is equal to the number of blocks per dmap.
1830 * in this case, the dmap control pages are not examined prior
1831 * to calling this routine (to save pathlength) and the call
1834 * for a request size that fits within a dmap, this routine relies
1835 * upon the dmap's dmtree to find the requested contiguous free
1836 * space. for request sizes that are larger than a dmap, the
1837 * requested free space will start at the first block of the
1838 * first dmap (i.e. blkno).
1841 * bmp - pointer to bmap descriptor
1842 * nblocks - actual number of contiguous free blocks to allocate.
1843 * l2nb - log2 number of contiguous free blocks to allocate.
1844 * blkno - starting block number of the dmap to start the allocation
1846 * results - on successful return, set to the starting block number
1847 * of the newly allocated range.
1851 * -ENOSPC - insufficient disk resources
1854 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1857 dbAllocCtl(struct bmap
* bmp
, s64 nblocks
, int l2nb
, s64 blkno
, s64
* results
)
1861 struct metapage
*mp
;
1864 /* check if the allocation request is confined to a single dmap.
1866 if (l2nb
<= L2BPERDMAP
) {
1867 /* get the buffer for the dmap.
1869 lblkno
= BLKTODMAP(blkno
, bmp
->db_l2nbperpage
);
1870 mp
= read_metapage(bmp
->db_ipbmap
, lblkno
, PSIZE
, 0);
1873 dp
= (struct dmap
*) mp
->data
;
1875 /* try to allocate the blocks.
1877 rc
= dbAllocDmapLev(bmp
, dp
, (int) nblocks
, l2nb
, results
);
1879 mark_metapage_dirty(mp
);
1881 release_metapage(mp
);
1886 /* allocation request involving multiple dmaps. it must start on
1889 assert((blkno
& (BPERDMAP
- 1)) == 0);
1891 /* allocate the blocks dmap by dmap.
1893 for (n
= nblocks
, b
= blkno
; n
> 0; n
-= nb
, b
+= nb
) {
1894 /* get the buffer for the dmap.
1896 lblkno
= BLKTODMAP(b
, bmp
->db_l2nbperpage
);
1897 mp
= read_metapage(bmp
->db_ipbmap
, lblkno
, PSIZE
, 0);
1902 dp
= (struct dmap
*) mp
->data
;
1904 /* the dmap better be all free.
1906 if (dp
->tree
.stree
[ROOT
] != L2BPERDMAP
) {
1907 release_metapage(mp
);
1908 jfs_error(bmp
->db_ipbmap
->i_sb
,
1909 "dbAllocCtl: the dmap is not all free");
1914 /* determine how many blocks to allocate from this dmap.
1916 nb
= min(n
, (s64
)BPERDMAP
);
1918 /* allocate the blocks from the dmap.
1920 if ((rc
= dbAllocDmap(bmp
, dp
, b
, nb
))) {
1921 release_metapage(mp
);
1925 /* write the buffer.
1930 /* set the results (starting block number) and return.
1935 /* something failed in handling an allocation request involving
1936 * multiple dmaps. we'll try to clean up by backing out any
1937 * allocation that has already happened for this request. if
1938 * we fail in backing out the allocation, we'll mark the file
1939 * system to indicate that blocks have been leaked.
1943 /* try to backout the allocations dmap by dmap.
1945 for (n
= nblocks
- n
, b
= blkno
; n
> 0;
1946 n
-= BPERDMAP
, b
+= BPERDMAP
) {
1947 /* get the buffer for this dmap.
1949 lblkno
= BLKTODMAP(b
, bmp
->db_l2nbperpage
);
1950 mp
= read_metapage(bmp
->db_ipbmap
, lblkno
, PSIZE
, 0);
1952 /* could not back out. mark the file system
1953 * to indicate that we have leaked blocks.
1955 jfs_error(bmp
->db_ipbmap
->i_sb
,
1956 "dbAllocCtl: I/O Error: Block Leakage.");
1959 dp
= (struct dmap
*) mp
->data
;
1961 /* free the blocks is this dmap.
1963 if (dbFreeDmap(bmp
, dp
, b
, BPERDMAP
)) {
1964 /* could not back out. mark the file system
1965 * to indicate that we have leaked blocks.
1967 release_metapage(mp
);
1968 jfs_error(bmp
->db_ipbmap
->i_sb
,
1969 "dbAllocCtl: Block Leakage.");
1973 /* write the buffer.
1983 * NAME: dbAllocDmapLev()
1985 * FUNCTION: attempt to allocate a specified number of contiguous blocks
1986 * from a specified dmap.
1988 * this routine checks if the contiguous blocks are available.
1989 * if so, nblocks of blocks are allocated; otherwise, ENOSPC is
1993 * mp - pointer to bmap descriptor
1994 * dp - pointer to dmap to attempt to allocate blocks from.
1995 * l2nb - log2 number of contiguous block desired.
1996 * nblocks - actual number of contiguous block desired.
1997 * results - on successful return, set to the starting block number
1998 * of the newly allocated range.
2002 * -ENOSPC - insufficient disk resources
2005 * serialization: IREAD_LOCK(ipbmap), e.g., from dbAlloc(), or
2006 * IWRITE_LOCK(ipbmap), e.g., dbAllocCtl(), held on entry/exit;
2009 dbAllocDmapLev(struct bmap
* bmp
,
2010 struct dmap
* dp
, int nblocks
, int l2nb
, s64
* results
)
2015 /* can't be more than a dmaps worth of blocks */
2016 assert(l2nb
<= L2BPERDMAP
);
2018 /* search the tree within the dmap page for sufficient
2019 * free space. if sufficient free space is found, dbFindLeaf()
2020 * returns the index of the leaf at which free space was found.
2022 if (dbFindLeaf((dmtree_t
*) & dp
->tree
, l2nb
, &leafidx
))
2025 /* determine the block number within the file system corresponding
2026 * to the leaf at which free space was found.
2028 blkno
= le64_to_cpu(dp
->start
) + (leafidx
<< L2DBWORD
);
2030 /* if not all bits of the dmap word are free, get the starting
2031 * bit number within the dmap word of the required string of free
2032 * bits and adjust the block number with this value.
2034 if (dp
->tree
.stree
[leafidx
+ LEAFIND
] < BUDMIN
)
2035 blkno
+= dbFindBits(le32_to_cpu(dp
->wmap
[leafidx
]), l2nb
);
2037 /* allocate the blocks */
2038 if ((rc
= dbAllocDmap(bmp
, dp
, blkno
, nblocks
)) == 0)
2046 * NAME: dbAllocDmap()
2048 * FUNCTION: adjust the disk allocation map to reflect the allocation
2049 * of a specified block range within a dmap.
2051 * this routine allocates the specified blocks from the dmap
2052 * through a call to dbAllocBits(). if the allocation of the
2053 * block range causes the maximum string of free blocks within
2054 * the dmap to change (i.e. the value of the root of the dmap's
2055 * dmtree), this routine will cause this change to be reflected
2056 * up through the appropriate levels of the dmap control pages
2057 * by a call to dbAdjCtl() for the L0 dmap control page that
2061 * bmp - pointer to bmap descriptor
2062 * dp - pointer to dmap to allocate the block range from.
2063 * blkno - starting block number of the block to be allocated.
2064 * nblocks - number of blocks to be allocated.
2070 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2072 static int dbAllocDmap(struct bmap
* bmp
, struct dmap
* dp
, s64 blkno
,
2078 /* save the current value of the root (i.e. maximum free string)
2081 oldroot
= dp
->tree
.stree
[ROOT
];
2083 /* allocate the specified (blocks) bits */
2084 dbAllocBits(bmp
, dp
, blkno
, nblocks
);
2086 /* if the root has not changed, done. */
2087 if (dp
->tree
.stree
[ROOT
] == oldroot
)
2090 /* root changed. bubble the change up to the dmap control pages.
2091 * if the adjustment of the upper level control pages fails,
2092 * backout the bit allocation (thus making everything consistent).
2094 if ((rc
= dbAdjCtl(bmp
, blkno
, dp
->tree
.stree
[ROOT
], 1, 0)))
2095 dbFreeBits(bmp
, dp
, blkno
, nblocks
);
2102 * NAME: dbFreeDmap()
2104 * FUNCTION: adjust the disk allocation map to reflect the allocation
2105 * of a specified block range within a dmap.
2107 * this routine frees the specified blocks from the dmap through
2108 * a call to dbFreeBits(). if the deallocation of the block range
2109 * causes the maximum string of free blocks within the dmap to
2110 * change (i.e. the value of the root of the dmap's dmtree), this
2111 * routine will cause this change to be reflected up through the
2112 * appropriate levels of the dmap control pages by a call to
2113 * dbAdjCtl() for the L0 dmap control page that covers this dmap.
2116 * bmp - pointer to bmap descriptor
2117 * dp - pointer to dmap to free the block range from.
2118 * blkno - starting block number of the block to be freed.
2119 * nblocks - number of blocks to be freed.
2125 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2127 static int dbFreeDmap(struct bmap
* bmp
, struct dmap
* dp
, s64 blkno
,
2133 /* save the current value of the root (i.e. maximum free string)
2136 oldroot
= dp
->tree
.stree
[ROOT
];
2138 /* free the specified (blocks) bits */
2139 rc
= dbFreeBits(bmp
, dp
, blkno
, nblocks
);
2141 /* if error or the root has not changed, done. */
2142 if (rc
|| (dp
->tree
.stree
[ROOT
] == oldroot
))
2145 /* root changed. bubble the change up to the dmap control pages.
2146 * if the adjustment of the upper level control pages fails,
2147 * backout the deallocation.
2149 if ((rc
= dbAdjCtl(bmp
, blkno
, dp
->tree
.stree
[ROOT
], 0, 0))) {
2150 word
= (blkno
& (BPERDMAP
- 1)) >> L2DBWORD
;
2152 /* as part of backing out the deallocation, we will have
2153 * to back split the dmap tree if the deallocation caused
2154 * the freed blocks to become part of a larger binary buddy
2157 if (dp
->tree
.stree
[word
] == NOFREE
)
2158 dbBackSplit((dmtree_t
*) & dp
->tree
, word
);
2160 dbAllocBits(bmp
, dp
, blkno
, nblocks
);
2168 * NAME: dbAllocBits()
2170 * FUNCTION: allocate a specified block range from a dmap.
2172 * this routine updates the dmap to reflect the working
2173 * state allocation of the specified block range. it directly
2174 * updates the bits of the working map and causes the adjustment
2175 * of the binary buddy system described by the dmap's dmtree
2176 * leaves to reflect the bits allocated. it also causes the
2177 * dmap's dmtree, as a whole, to reflect the allocated range.
2180 * bmp - pointer to bmap descriptor
2181 * dp - pointer to dmap to allocate bits from.
2182 * blkno - starting block number of the bits to be allocated.
2183 * nblocks - number of bits to be allocated.
2185 * RETURN VALUES: none
2187 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2189 static void dbAllocBits(struct bmap
* bmp
, struct dmap
* dp
, s64 blkno
,
2192 int dbitno
, word
, rembits
, nb
, nwords
, wbitno
, nw
, agno
;
2193 dmtree_t
*tp
= (dmtree_t
*) & dp
->tree
;
2197 /* pick up a pointer to the leaves of the dmap tree */
2198 leaf
= dp
->tree
.stree
+ LEAFIND
;
2200 /* determine the bit number and word within the dmap of the
2203 dbitno
= blkno
& (BPERDMAP
- 1);
2204 word
= dbitno
>> L2DBWORD
;
2206 /* block range better be within the dmap */
2207 assert(dbitno
+ nblocks
<= BPERDMAP
);
2209 /* allocate the bits of the dmap's words corresponding to the block
2210 * range. not all bits of the first and last words may be contained
2211 * within the block range. if this is the case, we'll work against
2212 * those words (i.e. partial first and/or last) on an individual basis
2213 * (a single pass), allocating the bits of interest by hand and
2214 * updating the leaf corresponding to the dmap word. a single pass
2215 * will be used for all dmap words fully contained within the
2216 * specified range. within this pass, the bits of all fully contained
2217 * dmap words will be marked as free in a single shot and the leaves
2218 * will be updated. a single leaf may describe the free space of
2219 * multiple dmap words, so we may update only a subset of the actual
2220 * leaves corresponding to the dmap words of the block range.
2222 for (rembits
= nblocks
; rembits
> 0; rembits
-= nb
, dbitno
+= nb
) {
2223 /* determine the bit number within the word and
2224 * the number of bits within the word.
2226 wbitno
= dbitno
& (DBWORD
- 1);
2227 nb
= min(rembits
, DBWORD
- wbitno
);
2229 /* check if only part of a word is to be allocated.
2232 /* allocate (set to 1) the appropriate bits within
2235 dp
->wmap
[word
] |= cpu_to_le32(ONES
<< (DBWORD
- nb
)
2238 /* update the leaf for this dmap word. in addition
2239 * to setting the leaf value to the binary buddy max
2240 * of the updated dmap word, dbSplit() will split
2241 * the binary system of the leaves if need be.
2243 dbSplit(tp
, word
, BUDMIN
,
2244 dbMaxBud((u8
*) & dp
->wmap
[word
]));
2248 /* one or more dmap words are fully contained
2249 * within the block range. determine how many
2250 * words and allocate (set to 1) the bits of these
2253 nwords
= rembits
>> L2DBWORD
;
2254 memset(&dp
->wmap
[word
], (int) ONES
, nwords
* 4);
2256 /* determine how many bits.
2258 nb
= nwords
<< L2DBWORD
;
2260 /* now update the appropriate leaves to reflect
2261 * the allocated words.
2263 for (; nwords
> 0; nwords
-= nw
) {
2264 if (leaf
[word
] < BUDMIN
) {
2265 jfs_error(bmp
->db_ipbmap
->i_sb
,
2266 "dbAllocBits: leaf page "
2271 /* determine what the leaf value should be
2272 * updated to as the minimum of the l2 number
2273 * of bits being allocated and the l2 number
2274 * of bits currently described by this leaf.
2276 size
= min((int)leaf
[word
], NLSTOL2BSZ(nwords
));
2278 /* update the leaf to reflect the allocation.
2279 * in addition to setting the leaf value to
2280 * NOFREE, dbSplit() will split the binary
2281 * system of the leaves to reflect the current
2282 * allocation (size).
2284 dbSplit(tp
, word
, size
, NOFREE
);
2286 /* get the number of dmap words handled */
2287 nw
= BUDSIZE(size
, BUDMIN
);
2293 /* update the free count for this dmap */
2294 le32_add_cpu(&dp
->nfree
, -nblocks
);
2298 /* if this allocation group is completely free,
2299 * update the maximum allocation group number if this allocation
2300 * group is the new max.
2302 agno
= blkno
>> bmp
->db_agl2size
;
2303 if (agno
> bmp
->db_maxag
)
2304 bmp
->db_maxag
= agno
;
2306 /* update the free count for the allocation group and map */
2307 bmp
->db_agfree
[agno
] -= nblocks
;
2308 bmp
->db_nfree
-= nblocks
;
2315 * NAME: dbFreeBits()
2317 * FUNCTION: free a specified block range from a dmap.
2319 * this routine updates the dmap to reflect the working
2320 * state allocation of the specified block range. it directly
2321 * updates the bits of the working map and causes the adjustment
2322 * of the binary buddy system described by the dmap's dmtree
2323 * leaves to reflect the bits freed. it also causes the dmap's
2324 * dmtree, as a whole, to reflect the deallocated range.
2327 * bmp - pointer to bmap descriptor
2328 * dp - pointer to dmap to free bits from.
2329 * blkno - starting block number of the bits to be freed.
2330 * nblocks - number of bits to be freed.
2332 * RETURN VALUES: 0 for success
2334 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2336 static int dbFreeBits(struct bmap
* bmp
, struct dmap
* dp
, s64 blkno
,
2339 int dbitno
, word
, rembits
, nb
, nwords
, wbitno
, nw
, agno
;
2340 dmtree_t
*tp
= (dmtree_t
*) & dp
->tree
;
2344 /* determine the bit number and word within the dmap of the
2347 dbitno
= blkno
& (BPERDMAP
- 1);
2348 word
= dbitno
>> L2DBWORD
;
2350 /* block range better be within the dmap.
2352 assert(dbitno
+ nblocks
<= BPERDMAP
);
2354 /* free the bits of the dmaps words corresponding to the block range.
2355 * not all bits of the first and last words may be contained within
2356 * the block range. if this is the case, we'll work against those
2357 * words (i.e. partial first and/or last) on an individual basis
2358 * (a single pass), freeing the bits of interest by hand and updating
2359 * the leaf corresponding to the dmap word. a single pass will be used
2360 * for all dmap words fully contained within the specified range.
2361 * within this pass, the bits of all fully contained dmap words will
2362 * be marked as free in a single shot and the leaves will be updated. a
2363 * single leaf may describe the free space of multiple dmap words,
2364 * so we may update only a subset of the actual leaves corresponding
2365 * to the dmap words of the block range.
2367 * dbJoin() is used to update leaf values and will join the binary
2368 * buddy system of the leaves if the new leaf values indicate this
2371 for (rembits
= nblocks
; rembits
> 0; rembits
-= nb
, dbitno
+= nb
) {
2372 /* determine the bit number within the word and
2373 * the number of bits within the word.
2375 wbitno
= dbitno
& (DBWORD
- 1);
2376 nb
= min(rembits
, DBWORD
- wbitno
);
2378 /* check if only part of a word is to be freed.
2381 /* free (zero) the appropriate bits within this
2385 cpu_to_le32(~(ONES
<< (DBWORD
- nb
)
2388 /* update the leaf for this dmap word.
2390 rc
= dbJoin(tp
, word
,
2391 dbMaxBud((u8
*) & dp
->wmap
[word
]));
2397 /* one or more dmap words are fully contained
2398 * within the block range. determine how many
2399 * words and free (zero) the bits of these words.
2401 nwords
= rembits
>> L2DBWORD
;
2402 memset(&dp
->wmap
[word
], 0, nwords
* 4);
2404 /* determine how many bits.
2406 nb
= nwords
<< L2DBWORD
;
2408 /* now update the appropriate leaves to reflect
2411 for (; nwords
> 0; nwords
-= nw
) {
2412 /* determine what the leaf value should be
2413 * updated to as the minimum of the l2 number
2414 * of bits being freed and the l2 (max) number
2415 * of bits that can be described by this leaf.
2419 (word
, L2LPERDMAP
, BUDMIN
),
2420 NLSTOL2BSZ(nwords
));
2424 rc
= dbJoin(tp
, word
, size
);
2428 /* get the number of dmap words handled.
2430 nw
= BUDSIZE(size
, BUDMIN
);
2436 /* update the free count for this dmap.
2438 le32_add_cpu(&dp
->nfree
, nblocks
);
2442 /* update the free count for the allocation group and
2445 agno
= blkno
>> bmp
->db_agl2size
;
2446 bmp
->db_nfree
+= nblocks
;
2447 bmp
->db_agfree
[agno
] += nblocks
;
2449 /* check if this allocation group is not completely free and
2450 * if it is currently the maximum (rightmost) allocation group.
2451 * if so, establish the new maximum allocation group number by
2452 * searching left for the first allocation group with allocation.
2454 if ((bmp
->db_agfree
[agno
] == bmp
->db_agsize
&& agno
== bmp
->db_maxag
) ||
2455 (agno
== bmp
->db_numag
- 1 &&
2456 bmp
->db_agfree
[agno
] == (bmp
-> db_mapsize
& (BPERDMAP
- 1)))) {
2457 while (bmp
->db_maxag
> 0) {
2459 if (bmp
->db_agfree
[bmp
->db_maxag
] !=
2464 /* re-establish the allocation group preference if the
2465 * current preference is right of the maximum allocation
2468 if (bmp
->db_agpref
> bmp
->db_maxag
)
2469 bmp
->db_agpref
= bmp
->db_maxag
;
2481 * FUNCTION: adjust a dmap control page at a specified level to reflect
2482 * the change in a lower level dmap or dmap control page's
2483 * maximum string of free blocks (i.e. a change in the root
2484 * of the lower level object's dmtree) due to the allocation
2485 * or deallocation of a range of blocks with a single dmap.
2487 * on entry, this routine is provided with the new value of
2488 * the lower level dmap or dmap control page root and the
2489 * starting block number of the block range whose allocation
2490 * or deallocation resulted in the root change. this range
2491 * is respresented by a single leaf of the current dmapctl
2492 * and the leaf will be updated with this value, possibly
2493 * causing a binary buddy system within the leaves to be
2494 * split or joined. the update may also cause the dmapctl's
2495 * dmtree to be updated.
2497 * if the adjustment of the dmap control page, itself, causes its
2498 * root to change, this change will be bubbled up to the next dmap
2499 * control level by a recursive call to this routine, specifying
2500 * the new root value and the next dmap control page level to
2503 * bmp - pointer to bmap descriptor
2504 * blkno - the first block of a block range within a dmap. it is
2505 * the allocation or deallocation of this block range that
2506 * requires the dmap control page to be adjusted.
2507 * newval - the new value of the lower level dmap or dmap control
2509 * alloc - 'true' if adjustment is due to an allocation.
2510 * level - current level of dmap control page (i.e. L0, L1, L2) to
2517 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2520 dbAdjCtl(struct bmap
* bmp
, s64 blkno
, int newval
, int alloc
, int level
)
2522 struct metapage
*mp
;
2526 struct dmapctl
*dcp
;
2529 /* get the buffer for the dmap control page for the specified
2530 * block number and control page level.
2532 lblkno
= BLKTOCTL(blkno
, bmp
->db_l2nbperpage
, level
);
2533 mp
= read_metapage(bmp
->db_ipbmap
, lblkno
, PSIZE
, 0);
2536 dcp
= (struct dmapctl
*) mp
->data
;
2538 if (dcp
->leafidx
!= cpu_to_le32(CTLLEAFIND
)) {
2539 jfs_error(bmp
->db_ipbmap
->i_sb
,
2540 "dbAdjCtl: Corrupt dmapctl page");
2541 release_metapage(mp
);
2545 /* determine the leaf number corresponding to the block and
2546 * the index within the dmap control tree.
2548 leafno
= BLKTOCTLLEAF(blkno
, dcp
->budmin
);
2549 ti
= leafno
+ le32_to_cpu(dcp
->leafidx
);
2551 /* save the current leaf value and the current root level (i.e.
2552 * maximum l2 free string described by this dmapctl).
2554 oldval
= dcp
->stree
[ti
];
2555 oldroot
= dcp
->stree
[ROOT
];
2557 /* check if this is a control page update for an allocation.
2558 * if so, update the leaf to reflect the new leaf value using
2559 * dbSplit(); otherwise (deallocation), use dbJoin() to update
2560 * the leaf with the new value. in addition to updating the
2561 * leaf, dbSplit() will also split the binary buddy system of
2562 * the leaves, if required, and bubble new values within the
2563 * dmapctl tree, if required. similarly, dbJoin() will join
2564 * the binary buddy system of leaves and bubble new values up
2565 * the dmapctl tree as required by the new leaf value.
2568 /* check if we are in the middle of a binary buddy
2569 * system. this happens when we are performing the
2570 * first allocation out of an allocation group that
2571 * is part (not the first part) of a larger binary
2572 * buddy system. if we are in the middle, back split
2573 * the system prior to calling dbSplit() which assumes
2574 * that it is at the front of a binary buddy system.
2576 if (oldval
== NOFREE
) {
2577 rc
= dbBackSplit((dmtree_t
*) dcp
, leafno
);
2580 oldval
= dcp
->stree
[ti
];
2582 dbSplit((dmtree_t
*) dcp
, leafno
, dcp
->budmin
, newval
);
2584 rc
= dbJoin((dmtree_t
*) dcp
, leafno
, newval
);
2589 /* check if the root of the current dmap control page changed due
2590 * to the update and if the current dmap control page is not at
2591 * the current top level (i.e. L0, L1, L2) of the map. if so (i.e.
2592 * root changed and this is not the top level), call this routine
2593 * again (recursion) for the next higher level of the mapping to
2594 * reflect the change in root for the current dmap control page.
2596 if (dcp
->stree
[ROOT
] != oldroot
) {
2597 /* are we below the top level of the map. if so,
2598 * bubble the root up to the next higher level.
2600 if (level
< bmp
->db_maxlevel
) {
2601 /* bubble up the new root of this dmap control page to
2605 dbAdjCtl(bmp
, blkno
, dcp
->stree
[ROOT
], alloc
,
2607 /* something went wrong in bubbling up the new
2608 * root value, so backout the changes to the
2609 * current dmap control page.
2612 dbJoin((dmtree_t
*) dcp
, leafno
,
2615 /* the dbJoin() above might have
2616 * caused a larger binary buddy system
2617 * to form and we may now be in the
2618 * middle of it. if this is the case,
2619 * back split the buddies.
2621 if (dcp
->stree
[ti
] == NOFREE
)
2622 dbBackSplit((dmtree_t
*)
2624 dbSplit((dmtree_t
*) dcp
, leafno
,
2625 dcp
->budmin
, oldval
);
2628 /* release the buffer and return the error.
2630 release_metapage(mp
);
2634 /* we're at the top level of the map. update
2635 * the bmap control page to reflect the size
2636 * of the maximum free buddy system.
2638 assert(level
== bmp
->db_maxlevel
);
2639 if (bmp
->db_maxfreebud
!= oldroot
) {
2640 jfs_error(bmp
->db_ipbmap
->i_sb
,
2641 "dbAdjCtl: the maximum free buddy is "
2642 "not the old root");
2644 bmp
->db_maxfreebud
= dcp
->stree
[ROOT
];
2648 /* write the buffer.
2659 * FUNCTION: update the leaf of a dmtree with a new value, splitting
2660 * the leaf from the binary buddy system of the dmtree's
2661 * leaves, as required.
2664 * tp - pointer to the tree containing the leaf.
2665 * leafno - the number of the leaf to be updated.
2666 * splitsz - the size the binary buddy system starting at the leaf
2667 * must be split to, specified as the log2 number of blocks.
2668 * newval - the new value for the leaf.
2670 * RETURN VALUES: none
2672 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2674 static void dbSplit(dmtree_t
* tp
, int leafno
, int splitsz
, int newval
)
2678 s8
*leaf
= tp
->dmt_stree
+ le32_to_cpu(tp
->dmt_leafidx
);
2680 /* check if the leaf needs to be split.
2682 if (leaf
[leafno
] > tp
->dmt_budmin
) {
2683 /* the split occurs by cutting the buddy system in half
2684 * at the specified leaf until we reach the specified
2685 * size. pick up the starting split size (current size
2686 * - 1 in l2) and the corresponding buddy size.
2688 cursz
= leaf
[leafno
] - 1;
2689 budsz
= BUDSIZE(cursz
, tp
->dmt_budmin
);
2691 /* split until we reach the specified size.
2693 while (cursz
>= splitsz
) {
2694 /* update the buddy's leaf with its new value.
2696 dbAdjTree(tp
, leafno
^ budsz
, cursz
);
2698 /* on to the next size and buddy.
2705 /* adjust the dmap tree to reflect the specified leaf's new
2708 dbAdjTree(tp
, leafno
, newval
);
2713 * NAME: dbBackSplit()
2715 * FUNCTION: back split the binary buddy system of dmtree leaves
2716 * that hold a specified leaf until the specified leaf
2717 * starts its own binary buddy system.
2719 * the allocators typically perform allocations at the start
2720 * of binary buddy systems and dbSplit() is used to accomplish
2721 * any required splits. in some cases, however, allocation
2722 * may occur in the middle of a binary system and requires a
2723 * back split, with the split proceeding out from the middle of
2724 * the system (less efficient) rather than the start of the
2725 * system (more efficient). the cases in which a back split
2726 * is required are rare and are limited to the first allocation
2727 * within an allocation group which is a part (not first part)
2728 * of a larger binary buddy system and a few exception cases
2729 * in which a previous join operation must be backed out.
2732 * tp - pointer to the tree containing the leaf.
2733 * leafno - the number of the leaf to be updated.
2735 * RETURN VALUES: none
2737 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2739 static int dbBackSplit(dmtree_t
* tp
, int leafno
)
2741 int budsz
, bud
, w
, bsz
, size
;
2743 s8
*leaf
= tp
->dmt_stree
+ le32_to_cpu(tp
->dmt_leafidx
);
2745 /* leaf should be part (not first part) of a binary
2748 assert(leaf
[leafno
] == NOFREE
);
2750 /* the back split is accomplished by iteratively finding the leaf
2751 * that starts the buddy system that contains the specified leaf and
2752 * splitting that system in two. this iteration continues until
2753 * the specified leaf becomes the start of a buddy system.
2755 * determine maximum possible l2 size for the specified leaf.
2758 LITOL2BSZ(leafno
, le32_to_cpu(tp
->dmt_l2nleafs
),
2761 /* determine the number of leaves covered by this size. this
2762 * is the buddy size that we will start with as we search for
2763 * the buddy system that contains the specified leaf.
2765 budsz
= BUDSIZE(size
, tp
->dmt_budmin
);
2769 while (leaf
[leafno
] == NOFREE
) {
2770 /* find the leftmost buddy leaf.
2772 for (w
= leafno
, bsz
= budsz
;; bsz
<<= 1,
2773 w
= (w
< bud
) ? w
: bud
) {
2774 if (bsz
>= le32_to_cpu(tp
->dmt_nleafs
)) {
2775 jfs_err("JFS: block map error in dbBackSplit");
2779 /* determine the buddy.
2783 /* check if this buddy is the start of the system.
2785 if (leaf
[bud
] != NOFREE
) {
2786 /* split the leaf at the start of the
2789 cursz
= leaf
[bud
] - 1;
2790 dbSplit(tp
, bud
, cursz
, cursz
);
2796 if (leaf
[leafno
] != size
) {
2797 jfs_err("JFS: wrong leaf value in dbBackSplit");
2807 * FUNCTION: update the leaf of a dmtree with a new value, joining
2808 * the leaf with other leaves of the dmtree into a multi-leaf
2809 * binary buddy system, as required.
2812 * tp - pointer to the tree containing the leaf.
2813 * leafno - the number of the leaf to be updated.
2814 * newval - the new value for the leaf.
2816 * RETURN VALUES: none
2818 static int dbJoin(dmtree_t
* tp
, int leafno
, int newval
)
2823 /* can the new leaf value require a join with other leaves ?
2825 if (newval
>= tp
->dmt_budmin
) {
2826 /* pickup a pointer to the leaves of the tree.
2828 leaf
= tp
->dmt_stree
+ le32_to_cpu(tp
->dmt_leafidx
);
2830 /* try to join the specified leaf into a large binary
2831 * buddy system. the join proceeds by attempting to join
2832 * the specified leafno with its buddy (leaf) at new value.
2833 * if the join occurs, we attempt to join the left leaf
2834 * of the joined buddies with its buddy at new value + 1.
2835 * we continue to join until we find a buddy that cannot be
2836 * joined (does not have a value equal to the size of the
2837 * last join) or until all leaves have been joined into a
2840 * get the buddy size (number of words covered) of
2843 budsz
= BUDSIZE(newval
, tp
->dmt_budmin
);
2847 while (budsz
< le32_to_cpu(tp
->dmt_nleafs
)) {
2848 /* get the buddy leaf.
2850 buddy
= leafno
^ budsz
;
2852 /* if the leaf's new value is greater than its
2853 * buddy's value, we join no more.
2855 if (newval
> leaf
[buddy
])
2858 /* It shouldn't be less */
2859 if (newval
< leaf
[buddy
])
2862 /* check which (leafno or buddy) is the left buddy.
2863 * the left buddy gets to claim the blocks resulting
2864 * from the join while the right gets to claim none.
2865 * the left buddy is also eligible to participate in
2866 * a join at the next higher level while the right
2870 if (leafno
< buddy
) {
2871 /* leafno is the left buddy.
2873 dbAdjTree(tp
, buddy
, NOFREE
);
2875 /* buddy is the left buddy and becomes
2878 dbAdjTree(tp
, leafno
, NOFREE
);
2882 /* on to try the next join.
2889 /* update the leaf value.
2891 dbAdjTree(tp
, leafno
, newval
);
2900 * FUNCTION: update a leaf of a dmtree with a new value, adjusting
2901 * the dmtree, as required, to reflect the new leaf value.
2902 * the combination of any buddies must already be done before
2906 * tp - pointer to the tree to be adjusted.
2907 * leafno - the number of the leaf to be updated.
2908 * newval - the new value for the leaf.
2910 * RETURN VALUES: none
2912 static void dbAdjTree(dmtree_t
* tp
, int leafno
, int newval
)
2917 /* pick up the index of the leaf for this leafno.
2919 lp
= leafno
+ le32_to_cpu(tp
->dmt_leafidx
);
2921 /* is the current value the same as the old value ? if so,
2922 * there is nothing to do.
2924 if (tp
->dmt_stree
[lp
] == newval
)
2927 /* set the new value.
2929 tp
->dmt_stree
[lp
] = newval
;
2931 /* bubble the new value up the tree as required.
2933 for (k
= 0; k
< le32_to_cpu(tp
->dmt_height
); k
++) {
2934 /* get the index of the first leaf of the 4 leaf
2935 * group containing the specified leaf (leafno).
2937 lp
= ((lp
- 1) & ~0x03) + 1;
2939 /* get the index of the parent of this 4 leaf group.
2943 /* determine the maximum of the 4 leaves.
2945 max
= TREEMAX(&tp
->dmt_stree
[lp
]);
2947 /* if the maximum of the 4 is the same as the
2948 * parent's value, we're done.
2950 if (tp
->dmt_stree
[pp
] == max
)
2953 /* parent gets new value.
2955 tp
->dmt_stree
[pp
] = max
;
2957 /* parent becomes leaf for next go-round.
2965 * NAME: dbFindLeaf()
2967 * FUNCTION: search a dmtree_t for sufficient free blocks, returning
2968 * the index of a leaf describing the free blocks if
2969 * sufficient free blocks are found.
2971 * the search starts at the top of the dmtree_t tree and
2972 * proceeds down the tree to the leftmost leaf with sufficient
2976 * tp - pointer to the tree to be searched.
2977 * l2nb - log2 number of free blocks to search for.
2978 * leafidx - return pointer to be set to the index of the leaf
2979 * describing at least l2nb free blocks if sufficient
2980 * free blocks are found.
2984 * -ENOSPC - insufficient free blocks.
2986 static int dbFindLeaf(dmtree_t
* tp
, int l2nb
, int *leafidx
)
2988 int ti
, n
= 0, k
, x
= 0;
2990 /* first check the root of the tree to see if there is
2991 * sufficient free space.
2993 if (l2nb
> tp
->dmt_stree
[ROOT
])
2996 /* sufficient free space available. now search down the tree
2997 * starting at the next level for the leftmost leaf that
2998 * describes sufficient free space.
3000 for (k
= le32_to_cpu(tp
->dmt_height
), ti
= 1;
3001 k
> 0; k
--, ti
= ((ti
+ n
) << 2) + 1) {
3002 /* search the four nodes at this level, starting from
3005 for (x
= ti
, n
= 0; n
< 4; n
++) {
3006 /* sufficient free space found. move to the next
3007 * level (or quit if this is the last level).
3009 if (l2nb
<= tp
->dmt_stree
[x
+ n
])
3013 /* better have found something since the higher
3014 * levels of the tree said it was here.
3019 /* set the return to the leftmost leaf describing sufficient
3022 *leafidx
= x
+ n
- le32_to_cpu(tp
->dmt_leafidx
);
3029 * NAME: dbFindBits()
3031 * FUNCTION: find a specified number of binary buddy free bits within a
3032 * dmap bitmap word value.
3034 * this routine searches the bitmap value for (1 << l2nb) free
3035 * bits at (1 << l2nb) alignments within the value.
3038 * word - dmap bitmap word value.
3039 * l2nb - number of free bits specified as a log2 number.
3042 * starting bit number of free bits.
3044 static int dbFindBits(u32 word
, int l2nb
)
3049 /* get the number of bits.
3052 assert(nb
<= DBWORD
);
3054 /* complement the word so we can use a mask (i.e. 0s represent
3055 * free bits) and compute the mask.
3058 mask
= ONES
<< (DBWORD
- nb
);
3060 /* scan the word for nb free bits at nb alignments.
3062 for (bitno
= 0; mask
!= 0; bitno
+= nb
, mask
>>= nb
) {
3063 if ((mask
& word
) == mask
)
3069 /* return the bit number.
3076 * NAME: dbMaxBud(u8 *cp)
3078 * FUNCTION: determine the largest binary buddy string of free
3079 * bits within 32-bits of the map.
3082 * cp - pointer to the 32-bit value.
3085 * largest binary buddy of free bits within a dmap word.
3087 static int dbMaxBud(u8
* cp
)
3089 signed char tmp1
, tmp2
;
3091 /* check if the wmap word is all free. if so, the
3092 * free buddy size is BUDMIN.
3094 if (*((uint
*) cp
) == 0)
3097 /* check if the wmap word is half free. if so, the
3098 * free buddy size is BUDMIN-1.
3100 if (*((u16
*) cp
) == 0 || *((u16
*) cp
+ 1) == 0)
3101 return (BUDMIN
- 1);
3103 /* not all free or half free. determine the free buddy
3104 * size thru table lookup using quarters of the wmap word.
3106 tmp1
= max(budtab
[cp
[2]], budtab
[cp
[3]]);
3107 tmp2
= max(budtab
[cp
[0]], budtab
[cp
[1]]);
3108 return (max(tmp1
, tmp2
));
3113 * NAME: cnttz(uint word)
3115 * FUNCTION: determine the number of trailing zeros within a 32-bit
3119 * value - 32-bit value to be examined.
3122 * count of trailing zeros
3124 static int cnttz(u32 word
)
3128 for (n
= 0; n
< 32; n
++, word
>>= 1) {
3138 * NAME: cntlz(u32 value)
3140 * FUNCTION: determine the number of leading zeros within a 32-bit
3144 * value - 32-bit value to be examined.
3147 * count of leading zeros
3149 static int cntlz(u32 value
)
3153 for (n
= 0; n
< 32; n
++, value
<<= 1) {
3154 if (value
& HIGHORDER
)
3162 * NAME: blkstol2(s64 nb)
3164 * FUNCTION: convert a block count to its log2 value. if the block
3165 * count is not a l2 multiple, it is rounded up to the next
3166 * larger l2 multiple.
3169 * nb - number of blocks
3172 * log2 number of blocks
3174 static int blkstol2(s64 nb
)
3177 s64 mask
; /* meant to be signed */
3179 mask
= (s64
) 1 << (64 - 1);
3181 /* count the leading bits.
3183 for (l2nb
= 0; l2nb
< 64; l2nb
++, mask
>>= 1) {
3184 /* leading bit found.
3187 /* determine the l2 value.
3189 l2nb
= (64 - 1) - l2nb
;
3191 /* check if we need to round up.
3200 return 0; /* fix compiler warning */
3205 * NAME: dbAllocBottomUp()
3207 * FUNCTION: alloc the specified block range from the working block
3210 * the blocks will be alloc from the working map one dmap
3214 * ip - pointer to in-core inode;
3215 * blkno - starting block number to be freed.
3216 * nblocks - number of blocks to be freed.
3222 int dbAllocBottomUp(struct inode
*ip
, s64 blkno
, s64 nblocks
)
3224 struct metapage
*mp
;
3228 struct inode
*ipbmap
= JFS_SBI(ip
->i_sb
)->ipbmap
;
3229 struct bmap
*bmp
= JFS_SBI(ip
->i_sb
)->bmap
;
3231 IREAD_LOCK(ipbmap
, RDWRLOCK_DMAP
);
3233 /* block to be allocated better be within the mapsize. */
3234 ASSERT(nblocks
<= bmp
->db_mapsize
- blkno
);
3237 * allocate the blocks a dmap at a time.
3240 for (rem
= nblocks
; rem
> 0; rem
-= nb
, blkno
+= nb
) {
3241 /* release previous dmap if any */
3246 /* get the buffer for the current dmap. */
3247 lblkno
= BLKTODMAP(blkno
, bmp
->db_l2nbperpage
);
3248 mp
= read_metapage(ipbmap
, lblkno
, PSIZE
, 0);
3250 IREAD_UNLOCK(ipbmap
);
3253 dp
= (struct dmap
*) mp
->data
;
3255 /* determine the number of blocks to be allocated from
3258 nb
= min(rem
, BPERDMAP
- (blkno
& (BPERDMAP
- 1)));
3260 /* allocate the blocks. */
3261 if ((rc
= dbAllocDmapBU(bmp
, dp
, blkno
, nb
))) {
3262 release_metapage(mp
);
3263 IREAD_UNLOCK(ipbmap
);
3268 /* write the last buffer. */
3271 IREAD_UNLOCK(ipbmap
);
3277 static int dbAllocDmapBU(struct bmap
* bmp
, struct dmap
* dp
, s64 blkno
,
3281 int dbitno
, word
, rembits
, nb
, nwords
, wbitno
, agno
;
3283 struct dmaptree
*tp
= (struct dmaptree
*) & dp
->tree
;
3285 /* save the current value of the root (i.e. maximum free string)
3288 oldroot
= tp
->stree
[ROOT
];
3290 /* determine the bit number and word within the dmap of the
3293 dbitno
= blkno
& (BPERDMAP
- 1);
3294 word
= dbitno
>> L2DBWORD
;
3296 /* block range better be within the dmap */
3297 assert(dbitno
+ nblocks
<= BPERDMAP
);
3299 /* allocate the bits of the dmap's words corresponding to the block
3300 * range. not all bits of the first and last words may be contained
3301 * within the block range. if this is the case, we'll work against
3302 * those words (i.e. partial first and/or last) on an individual basis
3303 * (a single pass), allocating the bits of interest by hand and
3304 * updating the leaf corresponding to the dmap word. a single pass
3305 * will be used for all dmap words fully contained within the
3306 * specified range. within this pass, the bits of all fully contained
3307 * dmap words will be marked as free in a single shot and the leaves
3308 * will be updated. a single leaf may describe the free space of
3309 * multiple dmap words, so we may update only a subset of the actual
3310 * leaves corresponding to the dmap words of the block range.
3312 for (rembits
= nblocks
; rembits
> 0; rembits
-= nb
, dbitno
+= nb
) {
3313 /* determine the bit number within the word and
3314 * the number of bits within the word.
3316 wbitno
= dbitno
& (DBWORD
- 1);
3317 nb
= min(rembits
, DBWORD
- wbitno
);
3319 /* check if only part of a word is to be allocated.
3322 /* allocate (set to 1) the appropriate bits within
3325 dp
->wmap
[word
] |= cpu_to_le32(ONES
<< (DBWORD
- nb
)
3330 /* one or more dmap words are fully contained
3331 * within the block range. determine how many
3332 * words and allocate (set to 1) the bits of these
3335 nwords
= rembits
>> L2DBWORD
;
3336 memset(&dp
->wmap
[word
], (int) ONES
, nwords
* 4);
3338 /* determine how many bits */
3339 nb
= nwords
<< L2DBWORD
;
3344 /* update the free count for this dmap */
3345 le32_add_cpu(&dp
->nfree
, -nblocks
);
3347 /* reconstruct summary tree */
3352 /* if this allocation group is completely free,
3353 * update the highest active allocation group number
3354 * if this allocation group is the new max.
3356 agno
= blkno
>> bmp
->db_agl2size
;
3357 if (agno
> bmp
->db_maxag
)
3358 bmp
->db_maxag
= agno
;
3360 /* update the free count for the allocation group and map */
3361 bmp
->db_agfree
[agno
] -= nblocks
;
3362 bmp
->db_nfree
-= nblocks
;
3366 /* if the root has not changed, done. */
3367 if (tp
->stree
[ROOT
] == oldroot
)
3370 /* root changed. bubble the change up to the dmap control pages.
3371 * if the adjustment of the upper level control pages fails,
3372 * backout the bit allocation (thus making everything consistent).
3374 if ((rc
= dbAdjCtl(bmp
, blkno
, tp
->stree
[ROOT
], 1, 0)))
3375 dbFreeBits(bmp
, dp
, blkno
, nblocks
);
3382 * NAME: dbExtendFS()
3384 * FUNCTION: extend bmap from blkno for nblocks;
3385 * dbExtendFS() updates bmap ready for dbAllocBottomUp();
3389 * L1---------------------------------L1
3391 * L0---------L0---------L0 L0---------L0---------L0
3393 * d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,.,dm;
3394 * L2L1L0d0,...,dnL0d0,...,dnL0d0,...,dnL1L0d0,...,dnL0d0,...,dnL0d0,..dm
3396 * <---old---><----------------------------extend----------------------->
3398 int dbExtendFS(struct inode
*ipbmap
, s64 blkno
, s64 nblocks
)
3400 struct jfs_sb_info
*sbi
= JFS_SBI(ipbmap
->i_sb
);
3401 int nbperpage
= sbi
->nbperpage
;
3402 int i
, i0
= true, j
, j0
= true, k
, n
;
3405 struct metapage
*mp
, *l2mp
, *l1mp
= NULL
, *l0mp
= NULL
;
3406 struct dmapctl
*l2dcp
, *l1dcp
, *l0dcp
;
3408 s8
*l0leaf
, *l1leaf
, *l2leaf
;
3409 struct bmap
*bmp
= sbi
->bmap
;
3410 int agno
, l2agsize
, oldl2agsize
;
3413 newsize
= blkno
+ nblocks
;
3415 jfs_info("dbExtendFS: blkno:%Ld nblocks:%Ld newsize:%Ld",
3416 (long long) blkno
, (long long) nblocks
, (long long) newsize
);
3419 * initialize bmap control page.
3421 * all the data in bmap control page should exclude
3422 * the mkfs hidden dmap page.
3425 /* update mapsize */
3426 bmp
->db_mapsize
= newsize
;
3427 bmp
->db_maxlevel
= BMAPSZTOLEV(bmp
->db_mapsize
);
3429 /* compute new AG size */
3430 l2agsize
= dbGetL2AGSize(newsize
);
3431 oldl2agsize
= bmp
->db_agl2size
;
3433 bmp
->db_agl2size
= l2agsize
;
3434 bmp
->db_agsize
= 1 << l2agsize
;
3436 /* compute new number of AG */
3437 agno
= bmp
->db_numag
;
3438 bmp
->db_numag
= newsize
>> l2agsize
;
3439 bmp
->db_numag
+= ((u32
) newsize
% (u32
) bmp
->db_agsize
) ? 1 : 0;
3442 * reconfigure db_agfree[]
3443 * from old AG configuration to new AG configuration;
3445 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
3446 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
3447 * note: new AG size = old AG size * (2**x).
3449 if (l2agsize
== oldl2agsize
)
3451 k
= 1 << (l2agsize
- oldl2agsize
);
3452 ag_rem
= bmp
->db_agfree
[0]; /* save agfree[0] */
3453 for (i
= 0, n
= 0; i
< agno
; n
++) {
3454 bmp
->db_agfree
[n
] = 0; /* init collection point */
3456 /* coalesce contiguous k AGs; */
3457 for (j
= 0; j
< k
&& i
< agno
; j
++, i
++) {
3458 /* merge AGi to AGn */
3459 bmp
->db_agfree
[n
] += bmp
->db_agfree
[i
];
3462 bmp
->db_agfree
[0] += ag_rem
; /* restore agfree[0] */
3464 for (; n
< MAXAG
; n
++)
3465 bmp
->db_agfree
[n
] = 0;
3468 * update highest active ag number
3471 bmp
->db_maxag
= bmp
->db_maxag
/ k
;
3476 * update bit maps and corresponding level control pages;
3477 * global control page db_nfree, db_agfree[agno], db_maxfreebud;
3481 p
= BMAPBLKNO
+ nbperpage
; /* L2 page */
3482 l2mp
= read_metapage(ipbmap
, p
, PSIZE
, 0);
3484 jfs_error(ipbmap
->i_sb
, "dbExtendFS: L2 page could not be read");
3487 l2dcp
= (struct dmapctl
*) l2mp
->data
;
3489 /* compute start L1 */
3490 k
= blkno
>> L2MAXL1SIZE
;
3491 l2leaf
= l2dcp
->stree
+ CTLLEAFIND
+ k
;
3492 p
= BLKTOL1(blkno
, sbi
->l2nbperpage
); /* L1 page */
3495 * extend each L1 in L2
3497 for (; k
< LPERCTL
; k
++, p
+= nbperpage
) {
3500 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */
3501 l1mp
= read_metapage(ipbmap
, p
, PSIZE
, 0);
3504 l1dcp
= (struct dmapctl
*) l1mp
->data
;
3506 /* compute start L0 */
3507 j
= (blkno
& (MAXL1SIZE
- 1)) >> L2MAXL0SIZE
;
3508 l1leaf
= l1dcp
->stree
+ CTLLEAFIND
+ j
;
3509 p
= BLKTOL0(blkno
, sbi
->l2nbperpage
);
3512 /* assign/init L1 page */
3513 l1mp
= get_metapage(ipbmap
, p
, PSIZE
, 0);
3517 l1dcp
= (struct dmapctl
*) l1mp
->data
;
3519 /* compute start L0 */
3521 l1leaf
= l1dcp
->stree
+ CTLLEAFIND
;
3522 p
+= nbperpage
; /* 1st L0 of L1.k */
3526 * extend each L0 in L1
3528 for (; j
< LPERCTL
; j
++) {
3531 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */
3533 l0mp
= read_metapage(ipbmap
, p
, PSIZE
, 0);
3536 l0dcp
= (struct dmapctl
*) l0mp
->data
;
3538 /* compute start dmap */
3539 i
= (blkno
& (MAXL0SIZE
- 1)) >>
3541 l0leaf
= l0dcp
->stree
+ CTLLEAFIND
+ i
;
3542 p
= BLKTODMAP(blkno
,
3546 /* assign/init L0 page */
3547 l0mp
= get_metapage(ipbmap
, p
, PSIZE
, 0);
3551 l0dcp
= (struct dmapctl
*) l0mp
->data
;
3553 /* compute start dmap */
3555 l0leaf
= l0dcp
->stree
+ CTLLEAFIND
;
3556 p
+= nbperpage
; /* 1st dmap of L0.j */
3560 * extend each dmap in L0
3562 for (; i
< LPERCTL
; i
++) {
3564 * reconstruct the dmap page, and
3565 * initialize corresponding parent L0 leaf
3567 if ((n
= blkno
& (BPERDMAP
- 1))) {
3568 /* read in dmap page: */
3569 mp
= read_metapage(ipbmap
, p
,
3573 n
= min(nblocks
, (s64
)BPERDMAP
- n
);
3575 /* assign/init dmap page */
3576 mp
= read_metapage(ipbmap
, p
,
3581 n
= min(nblocks
, (s64
)BPERDMAP
);
3584 dp
= (struct dmap
*) mp
->data
;
3585 *l0leaf
= dbInitDmap(dp
, blkno
, n
);
3588 agno
= le64_to_cpu(dp
->start
) >> l2agsize
;
3589 bmp
->db_agfree
[agno
] += n
;
3600 } /* for each dmap in a L0 */
3603 * build current L0 page from its leaves, and
3604 * initialize corresponding parent L1 leaf
3606 *l1leaf
= dbInitDmapCtl(l0dcp
, 0, ++i
);
3607 write_metapage(l0mp
);
3611 l1leaf
++; /* continue for next L0 */
3613 /* more than 1 L0 ? */
3615 break; /* build L1 page */
3617 /* summarize in global bmap page */
3618 bmp
->db_maxfreebud
= *l1leaf
;
3619 release_metapage(l1mp
);
3620 release_metapage(l2mp
);
3624 } /* for each L0 in a L1 */
3627 * build current L1 page from its leaves, and
3628 * initialize corresponding parent L2 leaf
3630 *l2leaf
= dbInitDmapCtl(l1dcp
, 1, ++j
);
3631 write_metapage(l1mp
);
3635 l2leaf
++; /* continue for next L1 */
3637 /* more than 1 L1 ? */
3639 break; /* build L2 page */
3641 /* summarize in global bmap page */
3642 bmp
->db_maxfreebud
= *l2leaf
;
3643 release_metapage(l2mp
);
3647 } /* for each L1 in a L2 */
3649 jfs_error(ipbmap
->i_sb
,
3650 "dbExtendFS: function has not returned as expected");
3653 release_metapage(l0mp
);
3655 release_metapage(l1mp
);
3656 release_metapage(l2mp
);
3660 * finalize bmap control page
3671 void dbFinalizeBmap(struct inode
*ipbmap
)
3673 struct bmap
*bmp
= JFS_SBI(ipbmap
->i_sb
)->bmap
;
3674 int actags
, inactags
, l2nl
;
3675 s64 ag_rem
, actfree
, inactfree
, avgfree
;
3679 * finalize bmap control page
3683 * compute db_agpref: preferred ag to allocate from
3684 * (the leftmost ag with average free space in it);
3687 /* get the number of active ags and inacitve ags */
3688 actags
= bmp
->db_maxag
+ 1;
3689 inactags
= bmp
->db_numag
- actags
;
3690 ag_rem
= bmp
->db_mapsize
& (bmp
->db_agsize
- 1); /* ??? */
3692 /* determine how many blocks are in the inactive allocation
3693 * groups. in doing this, we must account for the fact that
3694 * the rightmost group might be a partial group (i.e. file
3695 * system size is not a multiple of the group size).
3697 inactfree
= (inactags
&& ag_rem
) ?
3698 ((inactags
- 1) << bmp
->db_agl2size
) + ag_rem
3699 : inactags
<< bmp
->db_agl2size
;
3701 /* determine how many free blocks are in the active
3702 * allocation groups plus the average number of free blocks
3703 * within the active ags.
3705 actfree
= bmp
->db_nfree
- inactfree
;
3706 avgfree
= (u32
) actfree
/ (u32
) actags
;
3708 /* if the preferred allocation group has not average free space.
3709 * re-establish the preferred group as the leftmost
3710 * group with average free space.
3712 if (bmp
->db_agfree
[bmp
->db_agpref
] < avgfree
) {
3713 for (bmp
->db_agpref
= 0; bmp
->db_agpref
< actags
;
3715 if (bmp
->db_agfree
[bmp
->db_agpref
] >= avgfree
)
3718 if (bmp
->db_agpref
>= bmp
->db_numag
) {
3719 jfs_error(ipbmap
->i_sb
,
3720 "cannot find ag with average freespace");
3725 * compute db_aglevel, db_agheight, db_width, db_agstart:
3726 * an ag is covered in aglevel dmapctl summary tree,
3727 * at agheight level height (from leaf) with agwidth number of nodes
3728 * each, which starts at agstart index node of the smmary tree node
3731 bmp
->db_aglevel
= BMAPSZTOLEV(bmp
->db_agsize
);
3733 bmp
->db_agl2size
- (L2BPERDMAP
+ bmp
->db_aglevel
* L2LPERCTL
);
3734 bmp
->db_agheight
= l2nl
>> 1;
3735 bmp
->db_agwidth
= 1 << (l2nl
- (bmp
->db_agheight
<< 1));
3736 for (i
= 5 - bmp
->db_agheight
, bmp
->db_agstart
= 0, n
= 1; i
> 0;
3738 bmp
->db_agstart
+= n
;
3746 * NAME: dbInitDmap()/ujfs_idmap_page()
3748 * FUNCTION: initialize working/persistent bitmap of the dmap page
3749 * for the specified number of blocks:
3751 * at entry, the bitmaps had been initialized as free (ZEROS);
3752 * The number of blocks will only account for the actually
3753 * existing blocks. Blocks which don't actually exist in
3754 * the aggregate will be marked as allocated (ONES);
3757 * dp - pointer to page of map
3758 * nblocks - number of blocks this page
3762 static int dbInitDmap(struct dmap
* dp
, s64 Blkno
, int nblocks
)
3764 int blkno
, w
, b
, r
, nw
, nb
, i
;
3766 /* starting block number within the dmap */
3767 blkno
= Blkno
& (BPERDMAP
- 1);
3770 dp
->nblocks
= dp
->nfree
= cpu_to_le32(nblocks
);
3771 dp
->start
= cpu_to_le64(Blkno
);
3773 if (nblocks
== BPERDMAP
) {
3774 memset(&dp
->wmap
[0], 0, LPERDMAP
* 4);
3775 memset(&dp
->pmap
[0], 0, LPERDMAP
* 4);
3779 le32_add_cpu(&dp
->nblocks
, nblocks
);
3780 le32_add_cpu(&dp
->nfree
, nblocks
);
3783 /* word number containing start block number */
3784 w
= blkno
>> L2DBWORD
;
3787 * free the bits corresponding to the block range (ZEROS):
3788 * note: not all bits of the first and last words may be contained
3789 * within the block range.
3791 for (r
= nblocks
; r
> 0; r
-= nb
, blkno
+= nb
) {
3792 /* number of bits preceding range to be freed in the word */
3793 b
= blkno
& (DBWORD
- 1);
3794 /* number of bits to free in the word */
3795 nb
= min(r
, DBWORD
- b
);
3797 /* is partial word to be freed ? */
3799 /* free (set to 0) from the bitmap word */
3800 dp
->wmap
[w
] &= cpu_to_le32(~(ONES
<< (DBWORD
- nb
)
3802 dp
->pmap
[w
] &= cpu_to_le32(~(ONES
<< (DBWORD
- nb
)
3805 /* skip the word freed */
3808 /* free (set to 0) contiguous bitmap words */
3810 memset(&dp
->wmap
[w
], 0, nw
* 4);
3811 memset(&dp
->pmap
[w
], 0, nw
* 4);
3813 /* skip the words freed */
3814 nb
= nw
<< L2DBWORD
;
3820 * mark bits following the range to be freed (non-existing
3821 * blocks) as allocated (ONES)
3824 if (blkno
== BPERDMAP
)
3827 /* the first word beyond the end of existing blocks */
3828 w
= blkno
>> L2DBWORD
;
3830 /* does nblocks fall on a 32-bit boundary ? */
3831 b
= blkno
& (DBWORD
- 1);
3833 /* mark a partial word allocated */
3834 dp
->wmap
[w
] = dp
->pmap
[w
] = cpu_to_le32(ONES
>> b
);
3838 /* set the rest of the words in the page to allocated (ONES) */
3839 for (i
= w
; i
< LPERDMAP
; i
++)
3840 dp
->pmap
[i
] = dp
->wmap
[i
] = cpu_to_le32(ONES
);
3846 return (dbInitDmapTree(dp
));
3851 * NAME: dbInitDmapTree()/ujfs_complete_dmap()
3853 * FUNCTION: initialize summary tree of the specified dmap:
3855 * at entry, bitmap of the dmap has been initialized;
3858 * dp - dmap to complete
3859 * blkno - starting block number for this dmap
3860 * treemax - will be filled in with max free for this dmap
3862 * RETURNS: max free string at the root of the tree
3864 static int dbInitDmapTree(struct dmap
* dp
)
3866 struct dmaptree
*tp
;
3870 /* init fixed info of tree */
3872 tp
->nleafs
= cpu_to_le32(LPERDMAP
);
3873 tp
->l2nleafs
= cpu_to_le32(L2LPERDMAP
);
3874 tp
->leafidx
= cpu_to_le32(LEAFIND
);
3875 tp
->height
= cpu_to_le32(4);
3876 tp
->budmin
= BUDMIN
;
3878 /* init each leaf from corresponding wmap word:
3879 * note: leaf is set to NOFREE(-1) if all blocks of corresponding
3880 * bitmap word are allocated.
3882 cp
= tp
->stree
+ le32_to_cpu(tp
->leafidx
);
3883 for (i
= 0; i
< LPERDMAP
; i
++)
3884 *cp
++ = dbMaxBud((u8
*) & dp
->wmap
[i
]);
3886 /* build the dmap's binary buddy summary tree */
3887 return (dbInitTree(tp
));
3892 * NAME: dbInitTree()/ujfs_adjtree()
3894 * FUNCTION: initialize binary buddy summary tree of a dmap or dmapctl.
3896 * at entry, the leaves of the tree has been initialized
3897 * from corresponding bitmap word or root of summary tree
3898 * of the child control page;
3899 * configure binary buddy system at the leaf level, then
3900 * bubble up the values of the leaf nodes up the tree.
3903 * cp - Pointer to the root of the tree
3904 * l2leaves- Number of leaf nodes as a power of 2
3905 * l2min - Number of blocks that can be covered by a leaf
3908 * RETURNS: max free string at the root of the tree
3910 static int dbInitTree(struct dmaptree
* dtp
)
3912 int l2max
, l2free
, bsize
, nextb
, i
;
3913 int child
, parent
, nparent
;
3918 /* Determine the maximum free string possible for the leaves */
3919 l2max
= le32_to_cpu(dtp
->l2nleafs
) + dtp
->budmin
;
3922 * configure the leaf levevl into binary buddy system
3924 * Try to combine buddies starting with a buddy size of 1
3925 * (i.e. two leaves). At a buddy size of 1 two buddy leaves
3926 * can be combined if both buddies have a maximum free of l2min;
3927 * the combination will result in the left-most buddy leaf having
3928 * a maximum free of l2min+1.
3929 * After processing all buddies for a given size, process buddies
3930 * at the next higher buddy size (i.e. current size * 2) and
3931 * the next maximum free (current free + 1).
3932 * This continues until the maximum possible buddy combination
3933 * yields maximum free.
3935 for (l2free
= dtp
->budmin
, bsize
= 1; l2free
< l2max
;
3936 l2free
++, bsize
= nextb
) {
3937 /* get next buddy size == current buddy pair size */
3940 /* scan each adjacent buddy pair at current buddy size */
3941 for (i
= 0, cp
= tp
+ le32_to_cpu(dtp
->leafidx
);
3942 i
< le32_to_cpu(dtp
->nleafs
);
3943 i
+= nextb
, cp
+= nextb
) {
3944 /* coalesce if both adjacent buddies are max free */
3945 if (*cp
== l2free
&& *(cp
+ bsize
) == l2free
) {
3946 *cp
= l2free
+ 1; /* left take right */
3947 *(cp
+ bsize
) = -1; /* right give left */
3953 * bubble summary information of leaves up the tree.
3955 * Starting at the leaf node level, the four nodes described by
3956 * the higher level parent node are compared for a maximum free and
3957 * this maximum becomes the value of the parent node.
3958 * when all lower level nodes are processed in this fashion then
3959 * move up to the next level (parent becomes a lower level node) and
3960 * continue the process for that level.
3962 for (child
= le32_to_cpu(dtp
->leafidx
),
3963 nparent
= le32_to_cpu(dtp
->nleafs
) >> 2;
3964 nparent
> 0; nparent
>>= 2, child
= parent
) {
3965 /* get index of 1st node of parent level */
3966 parent
= (child
- 1) >> 2;
3968 /* set the value of the parent node as the maximum
3969 * of the four nodes of the current level.
3971 for (i
= 0, cp
= tp
+ child
, cp1
= tp
+ parent
;
3972 i
< nparent
; i
++, cp
+= 4, cp1
++)
3983 * function: initialize dmapctl page
3985 static int dbInitDmapCtl(struct dmapctl
* dcp
, int level
, int i
)
3986 { /* start leaf index not covered by range */
3989 dcp
->nleafs
= cpu_to_le32(LPERCTL
);
3990 dcp
->l2nleafs
= cpu_to_le32(L2LPERCTL
);
3991 dcp
->leafidx
= cpu_to_le32(CTLLEAFIND
);
3992 dcp
->height
= cpu_to_le32(5);
3993 dcp
->budmin
= L2BPERDMAP
+ L2LPERCTL
* level
;
3996 * initialize the leaves of current level that were not covered
3997 * by the specified input block range (i.e. the leaves have no
3998 * low level dmapctl or dmap).
4000 cp
= &dcp
->stree
[CTLLEAFIND
+ i
];
4001 for (; i
< LPERCTL
; i
++)
4004 /* build the dmap's binary buddy summary tree */
4005 return (dbInitTree((struct dmaptree
*) dcp
));
4010 * NAME: dbGetL2AGSize()/ujfs_getagl2size()
4012 * FUNCTION: Determine log2(allocation group size) from aggregate size
4015 * nblocks - Number of blocks in aggregate
4017 * RETURNS: log2(allocation group size) in aggregate blocks
4019 static int dbGetL2AGSize(s64 nblocks
)
4025 if (nblocks
< BPERDMAP
* MAXAG
)
4026 return (L2BPERDMAP
);
4028 /* round up aggregate size to power of 2 */
4029 m
= ((u64
) 1 << (64 - 1));
4030 for (l2sz
= 64; l2sz
>= 0; l2sz
--, m
>>= 1) {
4035 sz
= (s64
) 1 << l2sz
;
4039 /* agsize = roundupSize/max_number_of_ag */
4040 return (l2sz
- L2MAXAG
);
4045 * NAME: dbMapFileSizeToMapSize()
4047 * FUNCTION: compute number of blocks the block allocation map file
4048 * can cover from the map file size;
4050 * RETURNS: Number of blocks which can be covered by this block map file;
4054 * maximum number of map pages at each level including control pages
4056 #define MAXL0PAGES (1 + LPERCTL)
4057 #define MAXL1PAGES (1 + LPERCTL * MAXL0PAGES)
4058 #define MAXL2PAGES (1 + LPERCTL * MAXL1PAGES)
4061 * convert number of map pages to the zero origin top dmapctl level
4063 #define BMAPPGTOLEV(npages) \
4064 (((npages) <= 3 + MAXL0PAGES) ? 0 : \
4065 ((npages) <= 2 + MAXL1PAGES) ? 1 : 2)
4067 s64
dbMapFileSizeToMapSize(struct inode
* ipbmap
)
4069 struct super_block
*sb
= ipbmap
->i_sb
;
4073 int complete
, factor
;
4075 nblocks
= ipbmap
->i_size
>> JFS_SBI(sb
)->l2bsize
;
4076 npages
= nblocks
>> JFS_SBI(sb
)->l2nbperpage
;
4077 level
= BMAPPGTOLEV(npages
);
4079 /* At each level, accumulate the number of dmap pages covered by
4080 * the number of full child levels below it;
4081 * repeat for the last incomplete child level.
4084 npages
--; /* skip the first global control page */
4085 /* skip higher level control pages above top level covered by map */
4086 npages
-= (2 - level
);
4087 npages
--; /* skip top level's control page */
4088 for (i
= level
; i
>= 0; i
--) {
4090 (i
== 2) ? MAXL1PAGES
: ((i
== 1) ? MAXL0PAGES
: 1);
4091 complete
= (u32
) npages
/ factor
;
4092 ndmaps
+= complete
* ((i
== 2) ? LPERCTL
* LPERCTL
:
4093 ((i
== 1) ? LPERCTL
: 1));
4095 /* pages in last/incomplete child */
4096 npages
= (u32
) npages
% factor
;
4097 /* skip incomplete child's level control page */
4101 /* convert the number of dmaps into the number of blocks
4102 * which can be covered by the dmaps;
4104 nblocks
= ndmaps
<< L2BPERDMAP
;