Lines Matching +full:max +full:- +full:bits +full:- +full:per +full:- +full:word

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2004
41 * take the lock in read mode. a single top-down request may proceed
42 * exclusively while multiple bottoms-up requests may proceed
50 * in the face of multiple-bottoms up requests.
57 #define BMAP_LOCK_INIT(bmp) mutex_init(&bmp->db_bmaplock)
58 #define BMAP_LOCK(bmp) mutex_lock(&bmp->db_bmaplock)
59 #define BMAP_UNLOCK(bmp) mutex_unlock(&bmp->db_bmaplock)
88 static int dbFindBits(u32 word, int l2nb);
99 static int cnttz(u32 word);
115 * binary buddy of free bits within the character.
133 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1
141 * memory is allocated for the in-core bmap descriptor and
142 * the in-core descriptor is initialized from disk.
145 * ipbmap - pointer to in-core inode for the block map.
148 * 0 - success
149 * -ENOMEM - insufficient memory
150 * -EIO - i/o error
151 * -EINVAL - wrong bmap data
161 * allocate/initialize the in-memory bmap descriptor in dbMount()
163 /* allocate memory for the in-memory bmap descriptor */ in dbMount()
166 return -ENOMEM; in dbMount()
168 /* read the on-disk bmap descriptor. */ in dbMount()
170 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage, in dbMount()
174 return -EIO; in dbMount()
177 /* copy the on-disk bmap descriptor to its in-memory version. */ in dbMount()
178 dbmp_le = (struct dbmap_disk *) mp->data; in dbMount()
179 bmp->db_mapsize = le64_to_cpu(dbmp_le->dn_mapsize); in dbMount()
180 bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree); in dbMount()
181 bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage); in dbMount()
182 bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag); in dbMount()
183 if (!bmp->db_numag) { in dbMount()
186 return -EINVAL; in dbMount()
189 bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel); in dbMount()
190 bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag); in dbMount()
191 bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref); in dbMount()
192 bmp->db_aglevel = le32_to_cpu(dbmp_le->dn_aglevel); in dbMount()
193 bmp->db_agheight = le32_to_cpu(dbmp_le->dn_agheight); in dbMount()
194 bmp->db_agwidth = le32_to_cpu(dbmp_le->dn_agwidth); in dbMount()
195 bmp->db_agstart = le32_to_cpu(dbmp_le->dn_agstart); in dbMount()
196 bmp->db_agl2size = le32_to_cpu(dbmp_le->dn_agl2size); in dbMount()
198 bmp->db_agfree[i] = le64_to_cpu(dbmp_le->dn_agfree[i]); in dbMount()
199 bmp->db_agsize = le64_to_cpu(dbmp_le->dn_agsize); in dbMount()
200 bmp->db_maxfreebud = dbmp_le->dn_maxfreebud; in dbMount()
206 bmp->db_ipbmap = ipbmap; in dbMount()
207 JFS_SBI(ipbmap->i_sb)->bmap = bmp; in dbMount()
209 memset(bmp->db_active, 0, sizeof(bmp->db_active)); in dbMount()
226 * the in-core bmap descriptor is written to disk and
230 * ipbmap - pointer to in-core inode for the block map.
233 * 0 - success
234 * -EIO - i/o error
238 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbUnmount()
246 truncate_inode_pages(ipbmap->i_mapping, 0); in dbUnmount()
248 /* free the memory for the in-memory bmap. */ in dbUnmount()
260 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbSync()
267 /* get the buffer for the on-disk bmap descriptor. */ in dbSync()
269 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage, in dbSync()
273 return -EIO; in dbSync()
275 /* copy the in-memory version of the bmap to the on-disk version */ in dbSync()
276 dbmp_le = (struct dbmap_disk *) mp->data; in dbSync()
277 dbmp_le->dn_mapsize = cpu_to_le64(bmp->db_mapsize); in dbSync()
278 dbmp_le->dn_nfree = cpu_to_le64(bmp->db_nfree); in dbSync()
279 dbmp_le->dn_l2nbperpage = cpu_to_le32(bmp->db_l2nbperpage); in dbSync()
280 dbmp_le->dn_numag = cpu_to_le32(bmp->db_numag); in dbSync()
281 dbmp_le->dn_maxlevel = cpu_to_le32(bmp->db_maxlevel); in dbSync()
282 dbmp_le->dn_maxag = cpu_to_le32(bmp->db_maxag); in dbSync()
283 dbmp_le->dn_agpref = cpu_to_le32(bmp->db_agpref); in dbSync()
284 dbmp_le->dn_aglevel = cpu_to_le32(bmp->db_aglevel); in dbSync()
285 dbmp_le->dn_agheight = cpu_to_le32(bmp->db_agheight); in dbSync()
286 dbmp_le->dn_agwidth = cpu_to_le32(bmp->db_agwidth); in dbSync()
287 dbmp_le->dn_agstart = cpu_to_le32(bmp->db_agstart); in dbSync()
288 dbmp_le->dn_agl2size = cpu_to_le32(bmp->db_agl2size); in dbSync()
290 dbmp_le->dn_agfree[i] = cpu_to_le64(bmp->db_agfree[i]); in dbSync()
291 dbmp_le->dn_agsize = cpu_to_le64(bmp->db_agsize); in dbSync()
292 dbmp_le->dn_maxfreebud = bmp->db_maxfreebud; in dbSync()
300 filemap_write_and_wait(ipbmap->i_mapping); in dbSync()
317 * ip - pointer to in-core inode;
318 * blkno - starting block number to be freed.
319 * nblocks - number of blocks to be freed.
322 * 0 - success
323 * -EIO - i/o error
331 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbFree()
332 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; in dbFree()
333 struct super_block *sb = ipbmap->i_sb; in dbFree()
338 if (unlikely((blkno == 0) || (blkno + nblocks > bmp->db_mapsize))) { in dbFree()
343 jfs_error(ip->i_sb, "block to be freed is outside the map\n"); in dbFree()
344 return -EIO; in dbFree()
350 if (JFS_SBI(sb)->flag & JFS_DISCARD) in dbFree()
351 if (JFS_SBI(sb)->minblks_trim <= nblocks) in dbFree()
358 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { in dbFree()
365 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbFree()
369 return -EIO; in dbFree()
371 dp = (struct dmap *) mp->data; in dbFree()
376 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1))); in dbFree()
380 jfs_error(ip->i_sb, "error in block map\n"); in dbFree()
407 * ipbmap - pointer to in-core inode for the block map.
408 * free - 'true' if block range is to be freed from the persistent
410 * blkno - starting block number of the range.
411 * nblocks - number of contiguous blocks in the range.
412 * tblk - transaction block;
415 * 0 - success
416 * -EIO - i/o error
423 int word, nbits, nwords; in dbUpdatePMap() local
424 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbUpdatePMap()
434 if (blkno + nblocks > bmp->db_mapsize) { in dbUpdatePMap()
438 jfs_error(ipbmap->i_sb, "blocks are outside the map\n"); in dbUpdatePMap()
439 return -EIO; in dbUpdatePMap()
443 lsn = tblk->lsn; in dbUpdatePMap()
444 log = (struct jfs_log *) JFS_SBI(tblk->sb)->log; in dbUpdatePMap()
452 for (rem = nblocks; rem > 0; rem -= nblks, blkno += nblks) { in dbUpdatePMap()
454 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbUpdatePMap()
460 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, in dbUpdatePMap()
463 return -EIO; in dbUpdatePMap()
466 dp = (struct dmap *) mp->data; in dbUpdatePMap()
468 /* determine the bit number and word within the dmap of in dbUpdatePMap()
472 dbitno = blkno & (BPERDMAP - 1); in dbUpdatePMap()
473 word = dbitno >> L2DBWORD; in dbUpdatePMap()
474 nblks = min(rem, (s64)BPERDMAP - dbitno); in dbUpdatePMap()
476 /* update the bits of the dmap words. the first and last in dbUpdatePMap()
477 * words may only have a subset of their bits updated. if in dbUpdatePMap()
478 * this is the case, we'll work against that word (i.e. in dbUpdatePMap()
481 * are to have all their bits updated. in dbUpdatePMap()
484 rbits -= nbits, dbitno += nbits) { in dbUpdatePMap()
485 /* determine the bit number within the word and in dbUpdatePMap()
486 * the number of bits within the word. in dbUpdatePMap()
488 wbitno = dbitno & (DBWORD - 1); in dbUpdatePMap()
489 nbits = min(rbits, DBWORD - wbitno); in dbUpdatePMap()
491 /* check if only part of the word is to be updated. */ in dbUpdatePMap()
493 /* update (free or allocate) the bits in dbUpdatePMap()
494 * in this word. in dbUpdatePMap()
497 (ONES << (DBWORD - nbits) >> wbitno); in dbUpdatePMap()
499 dp->pmap[word] &= in dbUpdatePMap()
502 dp->pmap[word] |= in dbUpdatePMap()
505 word += 1; in dbUpdatePMap()
508 * their bits updated. determine how in dbUpdatePMap()
509 * many words and how many bits. in dbUpdatePMap()
514 /* update (free or allocate) the bits in dbUpdatePMap()
518 memset(&dp->pmap[word], 0, in dbUpdatePMap()
521 memset(&dp->pmap[word], (int) ONES, in dbUpdatePMap()
524 word += nwords; in dbUpdatePMap()
537 if (mp->lsn != 0) { in dbUpdatePMap()
539 logdiff(diffp, mp->lsn, log); in dbUpdatePMap()
541 mp->lsn = lsn; in dbUpdatePMap()
544 list_move(&mp->synclist, &tblk->synclist); in dbUpdatePMap()
548 logdiff(difft, tblk->clsn, log); in dbUpdatePMap()
549 logdiff(diffp, mp->clsn, log); in dbUpdatePMap()
551 mp->clsn = tblk->clsn; in dbUpdatePMap()
553 mp->log = log; in dbUpdatePMap()
554 mp->lsn = lsn; in dbUpdatePMap()
557 log->count++; in dbUpdatePMap()
558 list_add(&mp->synclist, &tblk->synclist); in dbUpdatePMap()
560 mp->clsn = tblk->clsn; in dbUpdatePMap()
582 * new inode allocation towards. The tie-in between inode
593 * ipbmap - pointer to in-core inode for the block map.
604 int next_best = -1; in dbNextAG()
605 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbNextAG()
610 avgfree = (u32)bmp->db_nfree / bmp->db_numag; in dbNextAG()
616 agpref = bmp->db_agpref; in dbNextAG()
617 if ((atomic_read(&bmp->db_active[agpref]) == 0) && in dbNextAG()
618 (bmp->db_agfree[agpref] >= avgfree)) in dbNextAG()
624 for (i = 0 ; i < bmp->db_numag; i++, agpref++) { in dbNextAG()
625 if (agpref == bmp->db_numag) in dbNextAG()
628 if (atomic_read(&bmp->db_active[agpref])) in dbNextAG()
631 if (bmp->db_agfree[agpref] >= avgfree) { in dbNextAG()
633 bmp->db_agpref = agpref; in dbNextAG()
635 } else if (bmp->db_agfree[agpref] > hwm) { in dbNextAG()
637 hwm = bmp->db_agfree[agpref]; in dbNextAG()
646 if (next_best != -1) in dbNextAG()
647 bmp->db_agpref = next_best; in dbNextAG()
654 return (bmp->db_agpref); in dbNextAG()
663 * the block allocation policy uses hints and a multi-step
667 * per dmap, we first try to allocate the new blocks
684 * ip - pointer to in-core inode;
685 * hint - allocation hint.
686 * nblocks - number of contiguous blocks in the range.
687 * results - on successful return, set to the starting block number
691 * 0 - success
692 * -ENOSPC - insufficient disk resources
693 * -EIO - i/o error
698 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbAlloc()
716 bmp = JFS_SBI(ip->i_sb)->bmap; in dbAlloc()
718 mapSize = bmp->db_mapsize; in dbAlloc()
722 jfs_error(ip->i_sb, "the hint is outside the map\n"); in dbAlloc()
723 return -EIO; in dbAlloc()
729 if (l2nb > bmp->db_agl2size) { in dbAlloc()
749 if (blkno >= bmp->db_mapsize) in dbAlloc()
752 agno = blkno >> bmp->db_agl2size; in dbAlloc()
758 if ((blkno & (bmp->db_agsize - 1)) == 0) in dbAlloc()
760 * if so, call dbNextAG() to find a non-busy in dbAlloc()
763 if (atomic_read(&bmp->db_active[agno])) in dbAlloc()
775 rc = -EIO; in dbAlloc()
776 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbAlloc()
781 dp = (struct dmap *) mp->data; in dbAlloc()
787 != -ENOSPC) { in dbAlloc()
797 writers = atomic_read(&bmp->db_active[agno]); in dbAlloc()
799 ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) { in dbAlloc()
814 != -ENOSPC) { in dbAlloc()
826 != -ENOSPC) { in dbAlloc()
842 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC) in dbAlloc()
858 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC) in dbAlloc()
888 * ip - pointer to in-core inode requiring allocation.
889 * blkno - starting block of the current allocation.
890 * nblocks - number of contiguous blocks within the current
892 * addnblocks - number of blocks to add to the allocation.
893 * results - on successful return, set to the starting block number
900 * 0 - success
901 * -ENOSPC - insufficient disk resources
902 * -EIO - i/o error
916 if (rc != -ENOSPC) in dbReAlloc()
926 (ip, blkno + nblocks - 1, addnblocks + nblocks, results)); in dbReAlloc()
942 * ip - pointer to in-core inode requiring allocation.
943 * blkno - starting block of the current allocation.
944 * nblocks - number of contiguous blocks within the current
946 * addnblocks - number of blocks to add to the allocation.
949 * 0 - success
950 * -ENOSPC - insufficient disk resources
951 * -EIO - i/o error
955 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb); in dbExtend()
961 struct inode *ipbmap = sbi->ipbmap; in dbExtend()
965 * We don't want a non-aligned extent to cross a page boundary in dbExtend()
967 if (((rel_block = blkno & (sbi->nbperpage - 1))) && in dbExtend()
968 (rel_block + nblocks + addnblocks > sbi->nbperpage)) in dbExtend()
969 return -ENOSPC; in dbExtend()
972 lastblkno = blkno + nblocks - 1; in dbExtend()
982 bmp = sbi->bmap; in dbExtend()
983 if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) { in dbExtend()
985 jfs_error(ip->i_sb, "the block is outside the filesystem\n"); in dbExtend()
986 return -EIO; in dbExtend()
997 if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize || in dbExtend()
998 (extblkno & (bmp->db_agsize - 1)) == 0) { in dbExtend()
1000 return -ENOSPC; in dbExtend()
1006 lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage); in dbExtend()
1010 return -EIO; in dbExtend()
1013 dp = (struct dmap *) mp->data; in dbExtend()
1040 * bmp - pointer to bmap descriptor
1041 * dp - pointer to dmap.
1042 * blkno - starting block number of the range.
1043 * nblocks - number of contiguous free blocks of the range.
1046 * 0 - success
1047 * -ENOSPC - insufficient disk resources
1048 * -EIO - i/o error
1055 int dbitno, word, rembits, nb, nwords, wbitno, nw; in dbAllocNext() local
1060 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) { in dbAllocNext()
1061 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n"); in dbAllocNext()
1062 return -EIO; in dbAllocNext()
1067 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx); in dbAllocNext()
1069 /* determine the bit number and word within the dmap of the in dbAllocNext()
1072 dbitno = blkno & (BPERDMAP - 1); in dbAllocNext()
1073 word = dbitno >> L2DBWORD; in dbAllocNext()
1079 return -ENOSPC; in dbAllocNext()
1084 if (leaf[word] == NOFREE) in dbAllocNext()
1085 return -ENOSPC; in dbAllocNext()
1088 * if the block range is free. not all bits of the first and in dbAllocNext()
1092 * the actual bits to determine if they are free. a single pass in dbAllocNext()
1100 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocNext()
1101 /* determine the bit number within the word and in dbAllocNext()
1102 * the number of bits within the word. in dbAllocNext()
1104 wbitno = dbitno & (DBWORD - 1); in dbAllocNext()
1105 nb = min(rembits, DBWORD - wbitno); in dbAllocNext()
1107 /* check if only part of the word is to be examined. in dbAllocNext()
1110 /* check if the bits are free. in dbAllocNext()
1112 mask = (ONES << (DBWORD - nb) >> wbitno); in dbAllocNext()
1113 if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask) in dbAllocNext()
1114 return -ENOSPC; in dbAllocNext()
1116 word += 1; in dbAllocNext()
1120 * words and how many bits. in dbAllocNext()
1131 if (leaf[word] < BUDMIN) in dbAllocNext()
1132 return -ENOSPC; in dbAllocNext()
1134 /* determine the l2 number of bits provided in dbAllocNext()
1138 min_t(int, leaf[word], NLSTOL2BSZ(nwords)); in dbAllocNext()
1144 nwords -= nw; in dbAllocNext()
1145 word += nw; in dbAllocNext()
1168 * bmp - pointer to bmap descriptor
1169 * dp - pointer to dmap.
1170 * blkno - block number to allocate near.
1171 * nblocks - actual number of contiguous free blocks desired.
1172 * l2nb - log2 number of contiguous free blocks desired.
1173 * results - on successful return, set to the starting block number
1177 * 0 - success
1178 * -ENOSPC - insufficient disk resources
1179 * -EIO - i/o error
1187 int word, lword, rc; in dbAllocNear() local
1190 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) { in dbAllocNear()
1191 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n"); in dbAllocNear()
1192 return -EIO; in dbAllocNear()
1195 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx); in dbAllocNear()
1197 /* determine the word within the dmap that holds the hint in dbAllocNear()
1198 * (i.e. blkno). also, determine the last word in the dmap in dbAllocNear()
1201 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD; in dbAllocNear()
1202 lword = min(word + 4, LPERDMAP); in dbAllocNear()
1206 for (; word < lword; word++) { in dbAllocNear()
1209 if (leaf[word] < l2nb) in dbAllocNear()
1213 * of the first block described by this dmap word. in dbAllocNear()
1215 blkno = le64_to_cpu(dp->start) + (word << L2DBWORD); in dbAllocNear()
1217 /* if not all bits of the dmap word are free, get the in dbAllocNear()
1218 * starting bit number within the dmap word of the required in dbAllocNear()
1219 * string of free bits and adjust the block number with the in dbAllocNear()
1222 if (leaf[word] < BUDMIN) in dbAllocNear()
1224 dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb); in dbAllocNear()
1234 return -ENOSPC; in dbAllocNear()
1245 * of blocks per dmap, the dmap control pages will be used to
1255 * or two sub-trees, depending on the allocation group size.
1278 * bmp - pointer to bmap descriptor
1279 * agno - allocation group number.
1280 * nblocks - actual number of contiguous free blocks desired.
1281 * l2nb - log2 number of contiguous free blocks desired.
1282 * results - on successful return, set to the starting block number
1286 * 0 - success
1287 * -ENOSPC - insufficient disk resources
1288 * -EIO - i/o error
1304 if (l2nb > bmp->db_agl2size) { in dbAllocAG()
1305 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1307 return -EIO; in dbAllocAG()
1313 blkno = (s64) agno << bmp->db_agl2size; in dbAllocAG()
1332 if (bmp->db_agsize == BPERDMAP in dbAllocAG()
1333 || bmp->db_agfree[agno] == bmp->db_agsize) { in dbAllocAG()
1335 if ((rc == -ENOSPC) && in dbAllocAG()
1336 (bmp->db_agfree[agno] == bmp->db_agsize)) { in dbAllocAG()
1340 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1349 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel); in dbAllocAG()
1350 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocAG()
1352 return -EIO; in dbAllocAG()
1353 dcp = (struct dmapctl *) mp->data; in dbAllocAG()
1354 budmin = dcp->budmin; in dbAllocAG()
1356 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) { in dbAllocAG()
1357 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n"); in dbAllocAG()
1359 return -EIO; in dbAllocAG()
1370 (1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth; in dbAllocAG()
1371 ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1)); in dbAllocAG()
1373 /* dmap control page trees fan-out by 4 and a single allocation in dbAllocAG()
1379 for (i = 0; i < bmp->db_agwidth; i++, ti++) { in dbAllocAG()
1382 if (l2nb > dcp->stree[ti]) in dbAllocAG()
1389 for (k = bmp->db_agheight; k > 0; k--) { in dbAllocAG()
1391 if (l2nb <= dcp->stree[m + n]) { in dbAllocAG()
1397 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1400 return -EIO; in dbAllocAG()
1407 if (bmp->db_aglevel == 2) in dbAllocAG()
1409 else if (bmp->db_aglevel == 1) in dbAllocAG()
1410 blkno &= ~(MAXL1SIZE - 1); in dbAllocAG()
1411 else /* bmp->db_aglevel == 0 */ in dbAllocAG()
1412 blkno &= ~(MAXL0SIZE - 1); in dbAllocAG()
1415 ((s64) (ti - le32_to_cpu(dcp->leafidx))) << budmin; in dbAllocAG()
1434 dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1, in dbAllocAG()
1436 if (rc == -ENOSPC) { in dbAllocAG()
1437 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1439 return -EIO; in dbAllocAG()
1448 if (rc == -ENOSPC) { in dbAllocAG()
1449 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1451 rc = -EIO; in dbAllocAG()
1457 * return -ENOSPC. in dbAllocAG()
1461 return -ENOSPC; in dbAllocAG()
1478 * bmp - pointer to bmap descriptor
1479 * nblocks - actual number of contiguous free blocks desired.
1480 * l2nb - log2 number of contiguous free blocks desired.
1481 * results - on successful return, set to the starting block number
1485 * 0 - success
1486 * -ENOSPC - insufficient disk resources
1487 * -EIO - i/o error
1502 if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno))) in dbAllocAny()
1508 if (rc == -ENOSPC) { in dbAllocAny()
1509 jfs_error(bmp->db_ipbmap->i_sb, "unable to allocate blocks\n"); in dbAllocAny()
1510 return -EIO; in dbAllocAny()
1528 * - we work only on one ag at some time, minimizing how long we
1530 * - reading / writing the fs is possible most time, even on
1534 * - we write two times to the dmapctl and dmap pages
1535 * - but for me, this seems the best way, better ideas?
1539 * ip - pointer to in-core inode
1540 * agno - ag to trim
1541 * minlen - minimum value of contiguous blocks
1544 * s64 - actual number of blocks trimmed
1548 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbDiscardAG()
1549 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; in dbDiscardAG()
1553 struct super_block *sb = ipbmap->i_sb; in dbDiscardAG()
1560 /* max blkno / nblocks pairs to trim */ in dbDiscardAG()
1567 nblocks = bmp->db_agfree[agno]; in dbDiscardAG()
1573 jfs_error(bmp->db_ipbmap->i_sb, "no memory for trim array\n"); in dbDiscardAG()
1582 /* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */ in dbDiscardAG()
1585 tt->blkno = blkno; in dbDiscardAG()
1586 tt->nblocks = nblocks; in dbDiscardAG()
1590 if (bmp->db_agfree[agno] == 0) in dbDiscardAG()
1594 nblocks = bmp->db_agfree[agno]; in dbDiscardAG()
1596 } else if (rc == -ENOSPC) { in dbDiscardAG()
1598 l2nb = BLKSTOL2(nblocks) - 1; in dbDiscardAG()
1602 jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n"); in dbDiscardAG()
1607 if (unlikely(count >= range_cnt - 1)) in dbDiscardAG()
1612 tt->nblocks = 0; /* mark the current end */ in dbDiscardAG()
1613 for (tt = totrim; tt->nblocks != 0; tt++) { in dbDiscardAG()
1616 if (!(JFS_SBI(sb)->flag & JFS_DISCARD)) in dbDiscardAG()
1617 jfs_issue_discard(ip, tt->blkno, tt->nblocks); in dbDiscardAG()
1618 dbFree(ip, tt->blkno, tt->nblocks); in dbDiscardAG()
1619 trimmed += tt->nblocks; in dbDiscardAG()
1640 * bmp - pointer to bmap descriptor
1641 * level - starting dmap control page level.
1642 * l2nb - log2 number of contiguous free blocks desired.
1643 * *blkno - on entry, starting block number for conducting the search.
1648 * 0 - success
1649 * -ENOSPC - insufficient disk resources
1650 * -EIO - i/o error
1667 for (lev = level, b = *blkno; lev >= 0; lev--) { in dbFindCtl()
1671 lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev); in dbFindCtl()
1672 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbFindCtl()
1674 return -EIO; in dbFindCtl()
1675 dcp = (struct dmapctl *) mp->data; in dbFindCtl()
1676 budmin = dcp->budmin; in dbFindCtl()
1678 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) { in dbFindCtl()
1679 jfs_error(bmp->db_ipbmap->i_sb, in dbFindCtl()
1682 return -EIO; in dbFindCtl()
1700 jfs_error(bmp->db_ipbmap->i_sb, in dbFindCtl()
1702 return -EIO; in dbFindCtl()
1704 return -ENOSPC; in dbFindCtl()
1745 * group whose size is equal to the number of blocks per dmap.
1757 * bmp - pointer to bmap descriptor
1758 * nblocks - actual number of contiguous free blocks to allocate.
1759 * l2nb - log2 number of contiguous free blocks to allocate.
1760 * blkno - starting block number of the dmap to start the allocation
1762 * results - on successful return, set to the starting block number
1766 * 0 - success
1767 * -ENOSPC - insufficient disk resources
1768 * -EIO - i/o error
1785 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbAllocCtl()
1786 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1788 return -EIO; in dbAllocCtl()
1789 dp = (struct dmap *) mp->data; in dbAllocCtl()
1805 assert((blkno & (BPERDMAP - 1)) == 0); in dbAllocCtl()
1809 for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) { in dbAllocCtl()
1812 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); in dbAllocCtl()
1813 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1815 rc = -EIO; in dbAllocCtl()
1818 dp = (struct dmap *) mp->data; in dbAllocCtl()
1822 if (dp->tree.stree[ROOT] != L2BPERDMAP) { in dbAllocCtl()
1824 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocCtl()
1826 rc = -EIO; in dbAllocCtl()
1861 for (n = nblocks - n, b = blkno; n > 0; in dbAllocCtl()
1862 n -= BPERDMAP, b += BPERDMAP) { in dbAllocCtl()
1865 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); in dbAllocCtl()
1866 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1871 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocCtl()
1875 dp = (struct dmap *) mp->data; in dbAllocCtl()
1884 jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n"); in dbAllocCtl()
1908 * mp - pointer to bmap descriptor
1909 * dp - pointer to dmap to attempt to allocate blocks from.
1910 * l2nb - log2 number of contiguous block desired.
1911 * nblocks - actual number of contiguous block desired.
1912 * results - on successful return, set to the starting block number
1916 * 0 - success
1917 * -ENOSPC - insufficient disk resources
1918 * -EIO - i/o error
1937 if (dbFindLeaf((dmtree_t *) & dp->tree, l2nb, &leafidx)) in dbAllocDmapLev()
1938 return -ENOSPC; in dbAllocDmapLev()
1943 blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD); in dbAllocDmapLev()
1945 /* if not all bits of the dmap word are free, get the starting in dbAllocDmapLev()
1946 * bit number within the dmap word of the required string of free in dbAllocDmapLev()
1947 * bits and adjust the block number with this value. in dbAllocDmapLev()
1949 if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN) in dbAllocDmapLev()
1950 blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb); in dbAllocDmapLev()
1976 * bmp - pointer to bmap descriptor
1977 * dp - pointer to dmap to allocate the block range from.
1978 * blkno - starting block number of the block to be allocated.
1979 * nblocks - number of blocks to be allocated.
1982 * 0 - success
1983 * -EIO - i/o error
1996 oldroot = dp->tree.stree[ROOT]; in dbAllocDmap()
1998 /* allocate the specified (blocks) bits */ in dbAllocDmap()
2002 if (dp->tree.stree[ROOT] == oldroot) in dbAllocDmap()
2009 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0))) in dbAllocDmap()
2031 * bmp - pointer to bmap descriptor
2032 * dp - pointer to dmap to free the block range from.
2033 * blkno - starting block number of the block to be freed.
2034 * nblocks - number of blocks to be freed.
2037 * 0 - success
2038 * -EIO - i/o error
2046 int rc = 0, word; in dbFreeDmap() local
2051 oldroot = dp->tree.stree[ROOT]; in dbFreeDmap()
2053 /* free the specified (blocks) bits */ in dbFreeDmap()
2057 if (rc || (dp->tree.stree[ROOT] == oldroot)) in dbFreeDmap()
2064 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) { in dbFreeDmap()
2065 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD; in dbFreeDmap()
2072 if (dp->tree.stree[word] == NOFREE) in dbFreeDmap()
2073 dbBackSplit((dmtree_t *) & dp->tree, word); in dbFreeDmap()
2089 * updates the bits of the working map and causes the adjustment
2091 * leaves to reflect the bits allocated. it also causes the
2095 * bmp - pointer to bmap descriptor
2096 * dp - pointer to dmap to allocate bits from.
2097 * blkno - starting block number of the bits to be allocated.
2098 * nblocks - number of bits to be allocated.
2107 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno; in dbAllocBits() local
2108 dmtree_t *tp = (dmtree_t *) & dp->tree; in dbAllocBits()
2113 leaf = dp->tree.stree + LEAFIND; in dbAllocBits()
2115 /* determine the bit number and word within the dmap of the in dbAllocBits()
2118 dbitno = blkno & (BPERDMAP - 1); in dbAllocBits()
2119 word = dbitno >> L2DBWORD; in dbAllocBits()
2124 /* allocate the bits of the dmap's words corresponding to the block in dbAllocBits()
2125 * range. not all bits of the first and last words may be contained in dbAllocBits()
2128 * (a single pass), allocating the bits of interest by hand and in dbAllocBits()
2129 * updating the leaf corresponding to the dmap word. a single pass in dbAllocBits()
2131 * specified range. within this pass, the bits of all fully contained in dbAllocBits()
2137 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocBits()
2138 /* determine the bit number within the word and in dbAllocBits()
2139 * the number of bits within the word. in dbAllocBits()
2141 wbitno = dbitno & (DBWORD - 1); in dbAllocBits()
2142 nb = min(rembits, DBWORD - wbitno); in dbAllocBits()
2144 /* check if only part of a word is to be allocated. in dbAllocBits()
2147 /* allocate (set to 1) the appropriate bits within in dbAllocBits()
2148 * this dmap word. in dbAllocBits()
2150 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) in dbAllocBits()
2153 /* update the leaf for this dmap word. in addition in dbAllocBits()
2154 * to setting the leaf value to the binary buddy max in dbAllocBits()
2155 * of the updated dmap word, dbSplit() will split in dbAllocBits()
2158 dbSplit(tp, word, BUDMIN, in dbAllocBits()
2159 dbMaxBud((u8 *) & dp->wmap[word])); in dbAllocBits()
2161 word += 1; in dbAllocBits()
2165 * words and allocate (set to 1) the bits of these in dbAllocBits()
2169 memset(&dp->wmap[word], (int) ONES, nwords * 4); in dbAllocBits()
2171 /* determine how many bits. in dbAllocBits()
2178 for (; nwords > 0; nwords -= nw) { in dbAllocBits()
2179 if (leaf[word] < BUDMIN) { in dbAllocBits()
2180 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocBits()
2187 * of bits being allocated and the l2 number in dbAllocBits()
2188 * of bits currently described by this leaf. in dbAllocBits()
2190 size = min_t(int, leaf[word], in dbAllocBits()
2199 dbSplit(tp, word, size, NOFREE); in dbAllocBits()
2203 word += nw; in dbAllocBits()
2209 le32_add_cpu(&dp->nfree, -nblocks); in dbAllocBits()
2215 * group is the new max. in dbAllocBits()
2217 agno = blkno >> bmp->db_agl2size; in dbAllocBits()
2218 if (agno > bmp->db_maxag) in dbAllocBits()
2219 bmp->db_maxag = agno; in dbAllocBits()
2222 bmp->db_agfree[agno] -= nblocks; in dbAllocBits()
2223 bmp->db_nfree -= nblocks; in dbAllocBits()
2236 * updates the bits of the working map and causes the adjustment
2238 * leaves to reflect the bits freed. it also causes the dmap's
2242 * bmp - pointer to bmap descriptor
2243 * dp - pointer to dmap to free bits from.
2244 * blkno - starting block number of the bits to be freed.
2245 * nblocks - number of bits to be freed.
2254 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno; in dbFreeBits() local
2255 dmtree_t *tp = (dmtree_t *) & dp->tree; in dbFreeBits()
2259 /* determine the bit number and word within the dmap of the in dbFreeBits()
2262 dbitno = blkno & (BPERDMAP - 1); in dbFreeBits()
2263 word = dbitno >> L2DBWORD; in dbFreeBits()
2269 /* free the bits of the dmaps words corresponding to the block range. in dbFreeBits()
2270 * not all bits of the first and last words may be contained within in dbFreeBits()
2273 * (a single pass), freeing the bits of interest by hand and updating in dbFreeBits()
2274 * the leaf corresponding to the dmap word. a single pass will be used in dbFreeBits()
2276 * within this pass, the bits of all fully contained dmap words will in dbFreeBits()
2286 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbFreeBits()
2287 /* determine the bit number within the word and in dbFreeBits()
2288 * the number of bits within the word. in dbFreeBits()
2290 wbitno = dbitno & (DBWORD - 1); in dbFreeBits()
2291 nb = min(rembits, DBWORD - wbitno); in dbFreeBits()
2293 /* check if only part of a word is to be freed. in dbFreeBits()
2296 /* free (zero) the appropriate bits within this in dbFreeBits()
2297 * dmap word. in dbFreeBits()
2299 dp->wmap[word] &= in dbFreeBits()
2300 cpu_to_le32(~(ONES << (DBWORD - nb) in dbFreeBits()
2303 /* update the leaf for this dmap word. in dbFreeBits()
2305 rc = dbJoin(tp, word, in dbFreeBits()
2306 dbMaxBud((u8 *) & dp->wmap[word])); in dbFreeBits()
2310 word += 1; in dbFreeBits()
2314 * words and free (zero) the bits of these words. in dbFreeBits()
2317 memset(&dp->wmap[word], 0, nwords * 4); in dbFreeBits()
2319 /* determine how many bits. in dbFreeBits()
2326 for (; nwords > 0; nwords -= nw) { in dbFreeBits()
2329 * of bits being freed and the l2 (max) number in dbFreeBits()
2330 * of bits that can be described by this leaf. in dbFreeBits()
2334 (word, L2LPERDMAP, BUDMIN), in dbFreeBits()
2339 rc = dbJoin(tp, word, size); in dbFreeBits()
2346 word += nw; in dbFreeBits()
2353 le32_add_cpu(&dp->nfree, nblocks); in dbFreeBits()
2360 agno = blkno >> bmp->db_agl2size; in dbFreeBits()
2361 bmp->db_nfree += nblocks; in dbFreeBits()
2362 bmp->db_agfree[agno] += nblocks; in dbFreeBits()
2369 if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) || in dbFreeBits()
2370 (agno == bmp->db_numag - 1 && in dbFreeBits()
2371 bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) { in dbFreeBits()
2372 while (bmp->db_maxag > 0) { in dbFreeBits()
2373 bmp->db_maxag -= 1; in dbFreeBits()
2374 if (bmp->db_agfree[bmp->db_maxag] != in dbFreeBits()
2375 bmp->db_agsize) in dbFreeBits()
2379 /* re-establish the allocation group preference if the in dbFreeBits()
2383 if (bmp->db_agpref > bmp->db_maxag) in dbFreeBits()
2384 bmp->db_agpref = bmp->db_maxag; in dbFreeBits()
2418 * bmp - pointer to bmap descriptor
2419 * blkno - the first block of a block range within a dmap. it is
2422 * newval - the new value of the lower level dmap or dmap control
2424 * alloc - 'true' if adjustment is due to an allocation.
2425 * level - current level of dmap control page (i.e. L0, L1, L2) to
2429 * 0 - success
2430 * -EIO - i/o error
2447 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level); in dbAdjCtl()
2448 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAdjCtl()
2450 return -EIO; in dbAdjCtl()
2451 dcp = (struct dmapctl *) mp->data; in dbAdjCtl()
2453 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) { in dbAdjCtl()
2454 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n"); in dbAdjCtl()
2456 return -EIO; in dbAdjCtl()
2462 leafno = BLKTOCTLLEAF(blkno, dcp->budmin); in dbAdjCtl()
2463 ti = leafno + le32_to_cpu(dcp->leafidx); in dbAdjCtl()
2468 oldval = dcp->stree[ti]; in dbAdjCtl()
2469 oldroot = dcp->stree[ROOT]; in dbAdjCtl()
2496 oldval = dcp->stree[ti]; in dbAdjCtl()
2498 dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval); in dbAdjCtl()
2514 if (dcp->stree[ROOT] != oldroot) { in dbAdjCtl()
2518 if (level < bmp->db_maxlevel) { in dbAdjCtl()
2523 dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc, in dbAdjCtl()
2539 if (dcp->stree[ti] == NOFREE) in dbAdjCtl()
2543 dcp->budmin, oldval); in dbAdjCtl()
2556 assert(level == bmp->db_maxlevel); in dbAdjCtl()
2557 if (bmp->db_maxfreebud != oldroot) { in dbAdjCtl()
2558 jfs_error(bmp->db_ipbmap->i_sb, in dbAdjCtl()
2561 bmp->db_maxfreebud = dcp->stree[ROOT]; in dbAdjCtl()
2581 * tp - pointer to the tree containing the leaf.
2582 * leafno - the number of the leaf to be updated.
2583 * splitsz - the size the binary buddy system starting at the leaf
2585 * newval - the new value for the leaf.
2595 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbSplit()
2599 if (leaf[leafno] > tp->dmt_budmin) { in dbSplit()
2603 * - 1 in l2) and the corresponding buddy size. in dbSplit()
2605 cursz = leaf[leafno] - 1; in dbSplit()
2606 budsz = BUDSIZE(cursz, tp->dmt_budmin); in dbSplit()
2617 cursz -= 1; in dbSplit()
2649 * tp - pointer to the tree containing the leaf.
2650 * leafno - the number of the leaf to be updated.
2660 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbBackSplit()
2675 LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs), in dbBackSplit()
2676 tp->dmt_budmin); in dbBackSplit()
2682 budsz = BUDSIZE(size, tp->dmt_budmin); in dbBackSplit()
2691 if (bsz >= le32_to_cpu(tp->dmt_nleafs)) { in dbBackSplit()
2693 return -EIO; in dbBackSplit()
2706 cursz = leaf[bud] - 1; in dbBackSplit()
2715 return -EIO; in dbBackSplit()
2725 * the leaf with other leaves of the dmtree into a multi-leaf
2729 * tp - pointer to the tree containing the leaf.
2730 * leafno - the number of the leaf to be updated.
2731 * newval - the new value for the leaf.
2742 if (newval >= tp->dmt_budmin) { in dbJoin()
2745 leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbJoin()
2760 budsz = BUDSIZE(newval, tp->dmt_budmin); in dbJoin()
2764 while (budsz < le32_to_cpu(tp->dmt_nleafs)) { in dbJoin()
2777 return -EIO; in dbJoin()
2823 * tp - pointer to the tree to be adjusted.
2824 * leafno - the number of the leaf to be updated.
2825 * newval - the new value for the leaf.
2832 int max; in dbAdjTree() local
2836 lp = leafno + le32_to_cpu(tp->dmt_leafidx); in dbAdjTree()
2841 if (tp->dmt_stree[lp] == newval) in dbAdjTree()
2846 tp->dmt_stree[lp] = newval; in dbAdjTree()
2850 for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) { in dbAdjTree()
2854 lp = ((lp - 1) & ~0x03) + 1; in dbAdjTree()
2858 pp = (lp - 1) >> 2; in dbAdjTree()
2862 max = TREEMAX(&tp->dmt_stree[lp]); in dbAdjTree()
2867 if (tp->dmt_stree[pp] == max) in dbAdjTree()
2872 tp->dmt_stree[pp] = max; in dbAdjTree()
2874 /* parent becomes leaf for next go-round. in dbAdjTree()
2893 * tp - pointer to the tree to be searched.
2894 * l2nb - log2 number of free blocks to search for.
2895 * leafidx - return pointer to be set to the index of the leaf
2900 * 0 - success
2901 * -ENOSPC - insufficient free blocks.
2910 if (l2nb > tp->dmt_stree[ROOT]) in dbFindLeaf()
2911 return -ENOSPC; in dbFindLeaf()
2917 for (k = le32_to_cpu(tp->dmt_height), ti = 1; in dbFindLeaf()
2918 k > 0; k--, ti = ((ti + n) << 2) + 1) { in dbFindLeaf()
2926 if (l2nb <= tp->dmt_stree[x + n]) in dbFindLeaf()
2939 *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx); in dbFindLeaf()
2948 * FUNCTION: find a specified number of binary buddy free bits within a
2949 * dmap bitmap word value.
2952 * bits at (1 << l2nb) alignments within the value.
2955 * word - dmap bitmap word value.
2956 * l2nb - number of free bits specified as a log2 number.
2959 * starting bit number of free bits.
2961 static int dbFindBits(u32 word, int l2nb) in dbFindBits() argument
2966 /* get the number of bits. in dbFindBits()
2971 /* complement the word so we can use a mask (i.e. 0s represent in dbFindBits()
2972 * free bits) and compute the mask. in dbFindBits()
2974 word = ~word; in dbFindBits()
2975 mask = ONES << (DBWORD - nb); in dbFindBits()
2977 /* scan the word for nb free bits at nb alignments. in dbFindBits()
2980 if ((mask & word) == mask) in dbFindBits()
2996 * bits within 32-bits of the map.
2999 * cp - pointer to the 32-bit value.
3002 * largest binary buddy of free bits within a dmap word.
3008 /* check if the wmap word is all free. if so, the in dbMaxBud()
3014 /* check if the wmap word is half free. if so, the in dbMaxBud()
3015 * free buddy size is BUDMIN-1. in dbMaxBud()
3018 return (BUDMIN - 1); in dbMaxBud()
3021 * size thru table lookup using quarters of the wmap word. in dbMaxBud()
3023 tmp1 = max(budtab[cp[2]], budtab[cp[3]]); in dbMaxBud()
3024 tmp2 = max(budtab[cp[0]], budtab[cp[1]]); in dbMaxBud()
3025 return (max(tmp1, tmp2)); in dbMaxBud()
3030 * NAME: cnttz(uint word)
3032 * FUNCTION: determine the number of trailing zeros within a 32-bit
3036 * value - 32-bit value to be examined.
3041 static int cnttz(u32 word) in cnttz() argument
3045 for (n = 0; n < 32; n++, word >>= 1) { in cnttz()
3046 if (word & 0x01) in cnttz()
3057 * FUNCTION: determine the number of leading zeros within a 32-bit
3061 * value - 32-bit value to be examined.
3086 * nb - number of blocks
3096 mask = (s64) 1 << (64 - 1); in blkstol2()
3098 /* count the leading bits. in blkstol2()
3106 l2nb = (64 - 1) - l2nb; in blkstol2()
3131 * ip - pointer to in-core inode;
3132 * blkno - starting block number to be freed.
3133 * nblocks - number of blocks to be freed.
3136 * 0 - success
3137 * -EIO - i/o error
3145 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbAllocBottomUp()
3146 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; in dbAllocBottomUp()
3151 ASSERT(nblocks <= bmp->db_mapsize - blkno); in dbAllocBottomUp()
3157 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { in dbAllocBottomUp()
3164 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbAllocBottomUp()
3168 return -EIO; in dbAllocBottomUp()
3170 dp = (struct dmap *) mp->data; in dbAllocBottomUp()
3175 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1))); in dbAllocBottomUp()
3198 int dbitno, word, rembits, nb, nwords, wbitno, agno; in dbAllocDmapBU() local
3200 struct dmaptree *tp = (struct dmaptree *) & dp->tree; in dbAllocDmapBU()
3205 oldroot = tp->stree[ROOT]; in dbAllocDmapBU()
3207 /* determine the bit number and word within the dmap of the in dbAllocDmapBU()
3210 dbitno = blkno & (BPERDMAP - 1); in dbAllocDmapBU()
3211 word = dbitno >> L2DBWORD; in dbAllocDmapBU()
3216 /* allocate the bits of the dmap's words corresponding to the block in dbAllocDmapBU()
3217 * range. not all bits of the first and last words may be contained in dbAllocDmapBU()
3220 * (a single pass), allocating the bits of interest by hand and in dbAllocDmapBU()
3221 * updating the leaf corresponding to the dmap word. a single pass in dbAllocDmapBU()
3223 * specified range. within this pass, the bits of all fully contained in dbAllocDmapBU()
3229 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocDmapBU()
3230 /* determine the bit number within the word and in dbAllocDmapBU()
3231 * the number of bits within the word. in dbAllocDmapBU()
3233 wbitno = dbitno & (DBWORD - 1); in dbAllocDmapBU()
3234 nb = min(rembits, DBWORD - wbitno); in dbAllocDmapBU()
3236 /* check if only part of a word is to be allocated. in dbAllocDmapBU()
3239 /* allocate (set to 1) the appropriate bits within in dbAllocDmapBU()
3240 * this dmap word. in dbAllocDmapBU()
3242 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) in dbAllocDmapBU()
3245 word++; in dbAllocDmapBU()
3249 * words and allocate (set to 1) the bits of these in dbAllocDmapBU()
3253 memset(&dp->wmap[word], (int) ONES, nwords * 4); in dbAllocDmapBU()
3255 /* determine how many bits */ in dbAllocDmapBU()
3257 word += nwords; in dbAllocDmapBU()
3262 le32_add_cpu(&dp->nfree, -nblocks); in dbAllocDmapBU()
3271 * if this allocation group is the new max. in dbAllocDmapBU()
3273 agno = blkno >> bmp->db_agl2size; in dbAllocDmapBU()
3274 if (agno > bmp->db_maxag) in dbAllocDmapBU()
3275 bmp->db_maxag = agno; in dbAllocDmapBU()
3278 bmp->db_agfree[agno] -= nblocks; in dbAllocDmapBU()
3279 bmp->db_nfree -= nblocks; in dbAllocDmapBU()
3284 if (tp->stree[ROOT] == oldroot) in dbAllocDmapBU()
3291 if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0))) in dbAllocDmapBU()
3306 * L1---------------------------------L1
3308 * L0---------L0---------L0 L0---------L0---------L0
3313 * <---old---><----------------------------extend----------------------->
3317 struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb); in dbExtendFS()
3318 int nbperpage = sbi->nbperpage; in dbExtendFS()
3326 struct bmap *bmp = sbi->bmap; in dbExtendFS()
3343 bmp->db_mapsize = newsize; in dbExtendFS()
3344 bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize); in dbExtendFS()
3348 oldl2agsize = bmp->db_agl2size; in dbExtendFS()
3350 bmp->db_agl2size = l2agsize; in dbExtendFS()
3351 bmp->db_agsize = 1 << l2agsize; in dbExtendFS()
3354 agno = bmp->db_numag; in dbExtendFS()
3355 bmp->db_numag = newsize >> l2agsize; in dbExtendFS()
3356 bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0; in dbExtendFS()
3363 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn; in dbExtendFS()
3368 k = 1 << (l2agsize - oldl2agsize); in dbExtendFS()
3369 ag_rem = bmp->db_agfree[0]; /* save agfree[0] */ in dbExtendFS()
3371 bmp->db_agfree[n] = 0; /* init collection point */ in dbExtendFS()
3376 bmp->db_agfree[n] += bmp->db_agfree[i]; in dbExtendFS()
3379 bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */ in dbExtendFS()
3382 bmp->db_agfree[n] = 0; in dbExtendFS()
3388 bmp->db_maxag = bmp->db_maxag / k; in dbExtendFS()
3401 jfs_error(ipbmap->i_sb, "L2 page could not be read\n"); in dbExtendFS()
3402 return -EIO; in dbExtendFS()
3404 l2dcp = (struct dmapctl *) l2mp->data; in dbExtendFS()
3408 l2leaf = l2dcp->stree + CTLLEAFIND + k; in dbExtendFS()
3409 p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */ in dbExtendFS()
3417 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */ in dbExtendFS()
3421 l1dcp = (struct dmapctl *) l1mp->data; in dbExtendFS()
3424 j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE; in dbExtendFS()
3425 l1leaf = l1dcp->stree + CTLLEAFIND + j; in dbExtendFS()
3426 p = BLKTOL0(blkno, sbi->l2nbperpage); in dbExtendFS()
3434 l1dcp = (struct dmapctl *) l1mp->data; in dbExtendFS()
3438 l1leaf = l1dcp->stree + CTLLEAFIND; in dbExtendFS()
3448 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */ in dbExtendFS()
3453 l0dcp = (struct dmapctl *) l0mp->data; in dbExtendFS()
3456 i = (blkno & (MAXL0SIZE - 1)) >> in dbExtendFS()
3458 l0leaf = l0dcp->stree + CTLLEAFIND + i; in dbExtendFS()
3460 sbi->l2nbperpage); in dbExtendFS()
3468 l0dcp = (struct dmapctl *) l0mp->data; in dbExtendFS()
3472 l0leaf = l0dcp->stree + CTLLEAFIND; in dbExtendFS()
3484 if ((n = blkno & (BPERDMAP - 1))) { in dbExtendFS()
3490 n = min(nblocks, (s64)BPERDMAP - n); in dbExtendFS()
3501 dp = (struct dmap *) mp->data; in dbExtendFS()
3504 bmp->db_nfree += n; in dbExtendFS()
3505 agno = le64_to_cpu(dp->start) >> l2agsize; in dbExtendFS()
3506 bmp->db_agfree[agno] += n; in dbExtendFS()
3514 nblocks -= n; in dbExtendFS()
3535 bmp->db_maxfreebud = *l1leaf; in dbExtendFS()
3559 bmp->db_maxfreebud = *l2leaf; in dbExtendFS()
3566 jfs_error(ipbmap->i_sb, "function has not returned as expected\n"); in dbExtendFS()
3573 return -EIO; in dbExtendFS()
3589 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbFinalizeBmap()
3604 actags = bmp->db_maxag + 1; in dbFinalizeBmap()
3605 inactags = bmp->db_numag - actags; in dbFinalizeBmap()
3606 ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */ in dbFinalizeBmap()
3614 ((inactags - 1) << bmp->db_agl2size) + ag_rem in dbFinalizeBmap()
3615 : inactags << bmp->db_agl2size; in dbFinalizeBmap()
3621 actfree = bmp->db_nfree - inactfree; in dbFinalizeBmap()
3625 * re-establish the preferred group as the leftmost in dbFinalizeBmap()
3628 if (bmp->db_agfree[bmp->db_agpref] < avgfree) { in dbFinalizeBmap()
3629 for (bmp->db_agpref = 0; bmp->db_agpref < actags; in dbFinalizeBmap()
3630 bmp->db_agpref++) { in dbFinalizeBmap()
3631 if (bmp->db_agfree[bmp->db_agpref] >= avgfree) in dbFinalizeBmap()
3634 if (bmp->db_agpref >= bmp->db_numag) { in dbFinalizeBmap()
3635 jfs_error(ipbmap->i_sb, in dbFinalizeBmap()
3647 bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize); in dbFinalizeBmap()
3649 bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL); in dbFinalizeBmap()
3650 bmp->db_agheight = l2nl >> 1; in dbFinalizeBmap()
3651 bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1)); in dbFinalizeBmap()
3652 for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0; in dbFinalizeBmap()
3653 i--) { in dbFinalizeBmap()
3654 bmp->db_agstart += n; in dbFinalizeBmap()
3673 * dp - pointer to page of map
3674 * nblocks - number of blocks this page
3683 blkno = Blkno & (BPERDMAP - 1); in dbInitDmap()
3686 dp->nblocks = dp->nfree = cpu_to_le32(nblocks); in dbInitDmap()
3687 dp->start = cpu_to_le64(Blkno); in dbInitDmap()
3690 memset(&dp->wmap[0], 0, LPERDMAP * 4); in dbInitDmap()
3691 memset(&dp->pmap[0], 0, LPERDMAP * 4); in dbInitDmap()
3695 le32_add_cpu(&dp->nblocks, nblocks); in dbInitDmap()
3696 le32_add_cpu(&dp->nfree, nblocks); in dbInitDmap()
3699 /* word number containing start block number */ in dbInitDmap()
3703 * free the bits corresponding to the block range (ZEROS): in dbInitDmap()
3704 * note: not all bits of the first and last words may be contained in dbInitDmap()
3707 for (r = nblocks; r > 0; r -= nb, blkno += nb) { in dbInitDmap()
3708 /* number of bits preceding range to be freed in the word */ in dbInitDmap()
3709 b = blkno & (DBWORD - 1); in dbInitDmap()
3710 /* number of bits to free in the word */ in dbInitDmap()
3711 nb = min(r, DBWORD - b); in dbInitDmap()
3713 /* is partial word to be freed ? */ in dbInitDmap()
3715 /* free (set to 0) from the bitmap word */ in dbInitDmap()
3716 dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) in dbInitDmap()
3718 dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) in dbInitDmap()
3721 /* skip the word freed */ in dbInitDmap()
3726 memset(&dp->wmap[w], 0, nw * 4); in dbInitDmap()
3727 memset(&dp->pmap[w], 0, nw * 4); in dbInitDmap()
3736 * mark bits following the range to be freed (non-existing in dbInitDmap()
3743 /* the first word beyond the end of existing blocks */ in dbInitDmap()
3746 /* does nblocks fall on a 32-bit boundary ? */ in dbInitDmap()
3747 b = blkno & (DBWORD - 1); in dbInitDmap()
3749 /* mark a partial word allocated */ in dbInitDmap()
3750 dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b); in dbInitDmap()
3756 dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES); in dbInitDmap()
3774 * dp - dmap to complete
3775 * blkno - starting block number for this dmap
3776 * treemax - will be filled in with max free for this dmap
3778 * RETURNS: max free string at the root of the tree
3787 tp = &dp->tree; in dbInitDmapTree()
3788 tp->nleafs = cpu_to_le32(LPERDMAP); in dbInitDmapTree()
3789 tp->l2nleafs = cpu_to_le32(L2LPERDMAP); in dbInitDmapTree()
3790 tp->leafidx = cpu_to_le32(LEAFIND); in dbInitDmapTree()
3791 tp->height = cpu_to_le32(4); in dbInitDmapTree()
3792 tp->budmin = BUDMIN; in dbInitDmapTree()
3794 /* init each leaf from corresponding wmap word: in dbInitDmapTree()
3795 * note: leaf is set to NOFREE(-1) if all blocks of corresponding in dbInitDmapTree()
3796 * bitmap word are allocated. in dbInitDmapTree()
3798 cp = tp->stree + le32_to_cpu(tp->leafidx); in dbInitDmapTree()
3800 *cp++ = dbMaxBud((u8 *) & dp->wmap[i]); in dbInitDmapTree()
3813 * from corresponding bitmap word or root of summary tree
3819 * cp - Pointer to the root of the tree
3820 * l2leaves- Number of leaf nodes as a power of 2
3821 * l2min - Number of blocks that can be covered by a leaf
3824 * RETURNS: max free string at the root of the tree
3832 tp = dtp->stree; in dbInitTree()
3835 l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin; in dbInitTree()
3843 * the combination will result in the left-most buddy leaf having in dbInitTree()
3851 for (l2free = dtp->budmin, bsize = 1; l2free < l2max; in dbInitTree()
3857 for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx); in dbInitTree()
3858 i < le32_to_cpu(dtp->nleafs); in dbInitTree()
3860 /* coalesce if both adjacent buddies are max free */ in dbInitTree()
3863 *(cp + bsize) = -1; /* right give left */ in dbInitTree()
3878 for (child = le32_to_cpu(dtp->leafidx), in dbInitTree()
3879 nparent = le32_to_cpu(dtp->nleafs) >> 2; in dbInitTree()
3882 parent = (child - 1) >> 2; in dbInitTree()
3905 dcp->nleafs = cpu_to_le32(LPERCTL); in dbInitDmapCtl()
3906 dcp->l2nleafs = cpu_to_le32(L2LPERCTL); in dbInitDmapCtl()
3907 dcp->leafidx = cpu_to_le32(CTLLEAFIND); in dbInitDmapCtl()
3908 dcp->height = cpu_to_le32(5); in dbInitDmapCtl()
3909 dcp->budmin = L2BPERDMAP + L2LPERCTL * level; in dbInitDmapCtl()
3916 cp = &dcp->stree[CTLLEAFIND + i]; in dbInitDmapCtl()
3931 * nblocks - Number of blocks in aggregate
3945 m = ((u64) 1 << (64 - 1)); in dbGetL2AGSize()
3946 for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) { in dbGetL2AGSize()
3956 return (l2sz - L2MAXAG); in dbGetL2AGSize()
3984 struct super_block *sb = ipbmap->i_sb; in dbMapFileSizeToMapSize()
3990 nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize; in dbMapFileSizeToMapSize()
3991 npages = nblocks >> JFS_SBI(sb)->l2nbperpage; in dbMapFileSizeToMapSize()
3999 npages--; /* skip the first global control page */ in dbMapFileSizeToMapSize()
4001 npages -= (2 - level); in dbMapFileSizeToMapSize()
4002 npages--; /* skip top level's control page */ in dbMapFileSizeToMapSize()
4003 for (i = level; i >= 0; i--) { in dbMapFileSizeToMapSize()
4013 npages--; in dbMapFileSizeToMapSize()