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()
173 err = -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()
182 bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage); in dbMount()
183 if (bmp->db_l2nbperpage > L2PSIZE - L2MINBLOCKSIZE || in dbMount()
184 bmp->db_l2nbperpage < 0) { in dbMount()
185 err = -EINVAL; in dbMount()
189 bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag); in dbMount()
190 if (!bmp->db_numag) { in dbMount()
191 err = -EINVAL; in dbMount()
195 bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel); in dbMount()
196 bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag); in dbMount()
197 bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref); in dbMount()
198 if (bmp->db_maxag >= MAXAG || bmp->db_maxag < 0 || in dbMount()
199 bmp->db_agpref >= MAXAG || bmp->db_agpref < 0) { in dbMount()
200 err = -EINVAL; in dbMount()
204 bmp->db_aglevel = le32_to_cpu(dbmp_le->dn_aglevel); in dbMount()
205 bmp->db_agheight = le32_to_cpu(dbmp_le->dn_agheight); in dbMount()
206 bmp->db_agwidth = le32_to_cpu(dbmp_le->dn_agwidth); in dbMount()
207 bmp->db_agstart = le32_to_cpu(dbmp_le->dn_agstart); in dbMount()
208 bmp->db_agl2size = le32_to_cpu(dbmp_le->dn_agl2size); in dbMount()
209 if (bmp->db_agl2size > L2MAXL2SIZE - L2MAXAG || in dbMount()
210 bmp->db_agl2size < 0) { in dbMount()
211 err = -EINVAL; in dbMount()
215 if (((bmp->db_mapsize - 1) >> bmp->db_agl2size) > MAXAG) { in dbMount()
216 err = -EINVAL; in dbMount()
221 bmp->db_agfree[i] = le64_to_cpu(dbmp_le->dn_agfree[i]); in dbMount()
222 bmp->db_agsize = le64_to_cpu(dbmp_le->dn_agsize); in dbMount()
223 bmp->db_maxfreebud = dbmp_le->dn_maxfreebud; in dbMount()
229 bmp->db_ipbmap = ipbmap; in dbMount()
230 JFS_SBI(ipbmap->i_sb)->bmap = bmp; in dbMount()
232 memset(bmp->db_active, 0, sizeof(bmp->db_active)); in dbMount()
255 * the in-core bmap descriptor is written to disk and
259 * ipbmap - pointer to in-core inode for the block map.
262 * 0 - success
263 * -EIO - i/o error
267 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbUnmount()
275 truncate_inode_pages(ipbmap->i_mapping, 0); in dbUnmount()
277 /* free the memory for the in-memory bmap. */ in dbUnmount()
279 JFS_SBI(ipbmap->i_sb)->bmap = NULL; in dbUnmount()
290 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbSync()
297 /* get the buffer for the on-disk bmap descriptor. */ in dbSync()
299 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage, in dbSync()
303 return -EIO; in dbSync()
305 /* copy the in-memory version of the bmap to the on-disk version */ in dbSync()
306 dbmp_le = (struct dbmap_disk *) mp->data; in dbSync()
307 dbmp_le->dn_mapsize = cpu_to_le64(bmp->db_mapsize); in dbSync()
308 dbmp_le->dn_nfree = cpu_to_le64(bmp->db_nfree); in dbSync()
309 dbmp_le->dn_l2nbperpage = cpu_to_le32(bmp->db_l2nbperpage); in dbSync()
310 dbmp_le->dn_numag = cpu_to_le32(bmp->db_numag); in dbSync()
311 dbmp_le->dn_maxlevel = cpu_to_le32(bmp->db_maxlevel); in dbSync()
312 dbmp_le->dn_maxag = cpu_to_le32(bmp->db_maxag); in dbSync()
313 dbmp_le->dn_agpref = cpu_to_le32(bmp->db_agpref); in dbSync()
314 dbmp_le->dn_aglevel = cpu_to_le32(bmp->db_aglevel); in dbSync()
315 dbmp_le->dn_agheight = cpu_to_le32(bmp->db_agheight); in dbSync()
316 dbmp_le->dn_agwidth = cpu_to_le32(bmp->db_agwidth); in dbSync()
317 dbmp_le->dn_agstart = cpu_to_le32(bmp->db_agstart); in dbSync()
318 dbmp_le->dn_agl2size = cpu_to_le32(bmp->db_agl2size); in dbSync()
320 dbmp_le->dn_agfree[i] = cpu_to_le64(bmp->db_agfree[i]); in dbSync()
321 dbmp_le->dn_agsize = cpu_to_le64(bmp->db_agsize); in dbSync()
322 dbmp_le->dn_maxfreebud = bmp->db_maxfreebud; in dbSync()
330 filemap_write_and_wait(ipbmap->i_mapping); in dbSync()
347 * ip - pointer to in-core inode;
348 * blkno - starting block number to be freed.
349 * nblocks - number of blocks to be freed.
352 * 0 - success
353 * -EIO - i/o error
361 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbFree()
362 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; in dbFree()
363 struct super_block *sb = ipbmap->i_sb; in dbFree()
368 if (unlikely((blkno == 0) || (blkno + nblocks > bmp->db_mapsize))) { in dbFree()
373 jfs_error(ip->i_sb, "block to be freed is outside the map\n"); in dbFree()
374 return -EIO; in dbFree()
380 if (JFS_SBI(sb)->flag & JFS_DISCARD) in dbFree()
381 if (JFS_SBI(sb)->minblks_trim <= nblocks) in dbFree()
388 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { in dbFree()
395 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbFree()
399 return -EIO; in dbFree()
401 dp = (struct dmap *) mp->data; in dbFree()
406 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1))); in dbFree()
410 jfs_error(ip->i_sb, "error in block map\n"); in dbFree()
437 * ipbmap - pointer to in-core inode for the block map.
438 * free - 'true' if block range is to be freed from the persistent
440 * blkno - starting block number of the range.
441 * nblocks - number of contiguous blocks in the range.
442 * tblk - transaction block;
445 * 0 - success
446 * -EIO - i/o error
453 int word, nbits, nwords; in dbUpdatePMap() local
454 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbUpdatePMap()
464 if (blkno + nblocks > bmp->db_mapsize) { in dbUpdatePMap()
468 jfs_error(ipbmap->i_sb, "blocks are outside the map\n"); in dbUpdatePMap()
469 return -EIO; in dbUpdatePMap()
473 lsn = tblk->lsn; in dbUpdatePMap()
474 log = (struct jfs_log *) JFS_SBI(tblk->sb)->log; in dbUpdatePMap()
482 for (rem = nblocks; rem > 0; rem -= nblks, blkno += nblks) { in dbUpdatePMap()
484 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbUpdatePMap()
490 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, in dbUpdatePMap()
493 return -EIO; in dbUpdatePMap()
496 dp = (struct dmap *) mp->data; in dbUpdatePMap()
498 /* determine the bit number and word within the dmap of in dbUpdatePMap()
502 dbitno = blkno & (BPERDMAP - 1); in dbUpdatePMap()
503 word = dbitno >> L2DBWORD; in dbUpdatePMap()
504 nblks = min(rem, (s64)BPERDMAP - dbitno); in dbUpdatePMap()
506 /* update the bits of the dmap words. the first and last in dbUpdatePMap()
507 * words may only have a subset of their bits updated. if in dbUpdatePMap()
508 * this is the case, we'll work against that word (i.e. in dbUpdatePMap()
511 * are to have all their bits updated. in dbUpdatePMap()
514 rbits -= nbits, dbitno += nbits) { in dbUpdatePMap()
515 /* determine the bit number within the word and in dbUpdatePMap()
516 * the number of bits within the word. in dbUpdatePMap()
518 wbitno = dbitno & (DBWORD - 1); in dbUpdatePMap()
519 nbits = min(rbits, DBWORD - wbitno); in dbUpdatePMap()
521 /* check if only part of the word is to be updated. */ in dbUpdatePMap()
523 /* update (free or allocate) the bits in dbUpdatePMap()
524 * in this word. in dbUpdatePMap()
527 (ONES << (DBWORD - nbits) >> wbitno); in dbUpdatePMap()
529 dp->pmap[word] &= in dbUpdatePMap()
532 dp->pmap[word] |= in dbUpdatePMap()
535 word += 1; in dbUpdatePMap()
538 * their bits updated. determine how in dbUpdatePMap()
539 * many words and how many bits. in dbUpdatePMap()
544 /* update (free or allocate) the bits in dbUpdatePMap()
548 memset(&dp->pmap[word], 0, in dbUpdatePMap()
551 memset(&dp->pmap[word], (int) ONES, in dbUpdatePMap()
554 word += nwords; in dbUpdatePMap()
567 if (mp->lsn != 0) { in dbUpdatePMap()
569 logdiff(diffp, mp->lsn, log); in dbUpdatePMap()
571 mp->lsn = lsn; in dbUpdatePMap()
574 list_move(&mp->synclist, &tblk->synclist); in dbUpdatePMap()
578 logdiff(difft, tblk->clsn, log); in dbUpdatePMap()
579 logdiff(diffp, mp->clsn, log); in dbUpdatePMap()
581 mp->clsn = tblk->clsn; in dbUpdatePMap()
583 mp->log = log; in dbUpdatePMap()
584 mp->lsn = lsn; in dbUpdatePMap()
587 log->count++; in dbUpdatePMap()
588 list_add(&mp->synclist, &tblk->synclist); in dbUpdatePMap()
590 mp->clsn = tblk->clsn; in dbUpdatePMap()
612 * new inode allocation towards. The tie-in between inode
623 * ipbmap - pointer to in-core inode for the block map.
634 int next_best = -1; in dbNextAG()
635 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbNextAG()
640 avgfree = (u32)bmp->db_nfree / bmp->db_numag; in dbNextAG()
646 agpref = bmp->db_agpref; in dbNextAG()
647 if ((atomic_read(&bmp->db_active[agpref]) == 0) && in dbNextAG()
648 (bmp->db_agfree[agpref] >= avgfree)) in dbNextAG()
654 for (i = 0 ; i < bmp->db_numag; i++, agpref++) { in dbNextAG()
655 if (agpref == bmp->db_numag) in dbNextAG()
658 if (atomic_read(&bmp->db_active[agpref])) in dbNextAG()
661 if (bmp->db_agfree[agpref] >= avgfree) { in dbNextAG()
663 bmp->db_agpref = agpref; in dbNextAG()
665 } else if (bmp->db_agfree[agpref] > hwm) { in dbNextAG()
667 hwm = bmp->db_agfree[agpref]; in dbNextAG()
676 if (next_best != -1) in dbNextAG()
677 bmp->db_agpref = next_best; in dbNextAG()
684 return (bmp->db_agpref); in dbNextAG()
693 * the block allocation policy uses hints and a multi-step
697 * per dmap, we first try to allocate the new blocks
714 * ip - pointer to in-core inode;
715 * hint - allocation hint.
716 * nblocks - number of contiguous blocks in the range.
717 * results - on successful return, set to the starting block number
721 * 0 - success
722 * -ENOSPC - insufficient disk resources
723 * -EIO - i/o error
728 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbAlloc()
746 bmp = JFS_SBI(ip->i_sb)->bmap; in dbAlloc()
748 mapSize = bmp->db_mapsize; in dbAlloc()
752 jfs_error(ip->i_sb, "the hint is outside the map\n"); in dbAlloc()
753 return -EIO; in dbAlloc()
759 if (l2nb > bmp->db_agl2size) { in dbAlloc()
779 if (blkno >= bmp->db_mapsize) in dbAlloc()
782 agno = blkno >> bmp->db_agl2size; in dbAlloc()
788 if ((blkno & (bmp->db_agsize - 1)) == 0) in dbAlloc()
790 * if so, call dbNextAG() to find a non-busy in dbAlloc()
793 if (atomic_read(&bmp->db_active[agno])) in dbAlloc()
805 rc = -EIO; in dbAlloc()
806 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbAlloc()
811 dp = (struct dmap *) mp->data; in dbAlloc()
817 != -ENOSPC) { in dbAlloc()
827 writers = atomic_read(&bmp->db_active[agno]); in dbAlloc()
829 ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) { in dbAlloc()
844 != -ENOSPC) { in dbAlloc()
856 != -ENOSPC) { in dbAlloc()
872 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC) in dbAlloc()
888 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC) in dbAlloc()
918 * ip - pointer to in-core inode requiring allocation.
919 * blkno - starting block of the current allocation.
920 * nblocks - number of contiguous blocks within the current
922 * addnblocks - number of blocks to add to the allocation.
923 * results - on successful return, set to the starting block number
930 * 0 - success
931 * -ENOSPC - insufficient disk resources
932 * -EIO - i/o error
946 if (rc != -ENOSPC) in dbReAlloc()
956 (ip, blkno + nblocks - 1, addnblocks + nblocks, results)); in dbReAlloc()
972 * ip - pointer to in-core inode requiring allocation.
973 * blkno - starting block of the current allocation.
974 * nblocks - number of contiguous blocks within the current
976 * addnblocks - number of blocks to add to the allocation.
979 * 0 - success
980 * -ENOSPC - insufficient disk resources
981 * -EIO - i/o error
985 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb); in dbExtend()
991 struct inode *ipbmap = sbi->ipbmap; in dbExtend()
995 * We don't want a non-aligned extent to cross a page boundary in dbExtend()
997 if (((rel_block = blkno & (sbi->nbperpage - 1))) && in dbExtend()
998 (rel_block + nblocks + addnblocks > sbi->nbperpage)) in dbExtend()
999 return -ENOSPC; in dbExtend()
1002 lastblkno = blkno + nblocks - 1; in dbExtend()
1012 bmp = sbi->bmap; in dbExtend()
1013 if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) { in dbExtend()
1015 jfs_error(ip->i_sb, "the block is outside the filesystem\n"); in dbExtend()
1016 return -EIO; in dbExtend()
1027 if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize || in dbExtend()
1028 (extblkno & (bmp->db_agsize - 1)) == 0) { in dbExtend()
1030 return -ENOSPC; in dbExtend()
1036 lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage); in dbExtend()
1040 return -EIO; in dbExtend()
1043 dp = (struct dmap *) mp->data; in dbExtend()
1070 * bmp - pointer to bmap descriptor
1071 * dp - pointer to dmap.
1072 * blkno - starting block number of the range.
1073 * nblocks - number of contiguous free blocks of the range.
1076 * 0 - success
1077 * -ENOSPC - insufficient disk resources
1078 * -EIO - i/o error
1085 int dbitno, word, rembits, nb, nwords, wbitno, nw; in dbAllocNext() local
1090 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) { in dbAllocNext()
1091 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n"); in dbAllocNext()
1092 return -EIO; in dbAllocNext()
1097 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx); in dbAllocNext()
1099 /* determine the bit number and word within the dmap of the in dbAllocNext()
1102 dbitno = blkno & (BPERDMAP - 1); in dbAllocNext()
1103 word = dbitno >> L2DBWORD; in dbAllocNext()
1109 return -ENOSPC; in dbAllocNext()
1114 if (leaf[word] == NOFREE) in dbAllocNext()
1115 return -ENOSPC; in dbAllocNext()
1118 * if the block range is free. not all bits of the first and in dbAllocNext()
1122 * the actual bits to determine if they are free. a single pass in dbAllocNext()
1130 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocNext()
1131 /* determine the bit number within the word and in dbAllocNext()
1132 * the number of bits within the word. in dbAllocNext()
1134 wbitno = dbitno & (DBWORD - 1); in dbAllocNext()
1135 nb = min(rembits, DBWORD - wbitno); in dbAllocNext()
1137 /* check if only part of the word is to be examined. in dbAllocNext()
1140 /* check if the bits are free. in dbAllocNext()
1142 mask = (ONES << (DBWORD - nb) >> wbitno); in dbAllocNext()
1143 if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask) in dbAllocNext()
1144 return -ENOSPC; in dbAllocNext()
1146 word += 1; in dbAllocNext()
1150 * words and how many bits. in dbAllocNext()
1161 if (leaf[word] < BUDMIN) in dbAllocNext()
1162 return -ENOSPC; in dbAllocNext()
1164 /* determine the l2 number of bits provided in dbAllocNext()
1168 min_t(int, leaf[word], NLSTOL2BSZ(nwords)); in dbAllocNext()
1174 nwords -= nw; in dbAllocNext()
1175 word += nw; in dbAllocNext()
1198 * bmp - pointer to bmap descriptor
1199 * dp - pointer to dmap.
1200 * blkno - block number to allocate near.
1201 * nblocks - actual number of contiguous free blocks desired.
1202 * l2nb - log2 number of contiguous free blocks desired.
1203 * results - on successful return, set to the starting block number
1207 * 0 - success
1208 * -ENOSPC - insufficient disk resources
1209 * -EIO - i/o error
1217 int word, lword, rc; in dbAllocNear() local
1220 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) { in dbAllocNear()
1221 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n"); in dbAllocNear()
1222 return -EIO; in dbAllocNear()
1225 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx); in dbAllocNear()
1227 /* determine the word within the dmap that holds the hint in dbAllocNear()
1228 * (i.e. blkno). also, determine the last word in the dmap in dbAllocNear()
1231 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD; in dbAllocNear()
1232 lword = min(word + 4, LPERDMAP); in dbAllocNear()
1236 for (; word < lword; word++) { in dbAllocNear()
1239 if (leaf[word] < l2nb) in dbAllocNear()
1243 * of the first block described by this dmap word. in dbAllocNear()
1245 blkno = le64_to_cpu(dp->start) + (word << L2DBWORD); in dbAllocNear()
1247 /* if not all bits of the dmap word are free, get the in dbAllocNear()
1248 * starting bit number within the dmap word of the required in dbAllocNear()
1249 * string of free bits and adjust the block number with the in dbAllocNear()
1252 if (leaf[word] < BUDMIN) in dbAllocNear()
1254 dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb); in dbAllocNear()
1264 return -ENOSPC; in dbAllocNear()
1275 * of blocks per dmap, the dmap control pages will be used to
1285 * or two sub-trees, depending on the allocation group size.
1308 * bmp - pointer to bmap descriptor
1309 * agno - allocation group number.
1310 * nblocks - actual number of contiguous free blocks desired.
1311 * l2nb - log2 number of contiguous free blocks desired.
1312 * results - on successful return, set to the starting block number
1316 * 0 - success
1317 * -ENOSPC - insufficient disk resources
1318 * -EIO - i/o error
1334 if (l2nb > bmp->db_agl2size) { in dbAllocAG()
1335 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1337 return -EIO; in dbAllocAG()
1343 blkno = (s64) agno << bmp->db_agl2size; in dbAllocAG()
1362 if (bmp->db_agsize == BPERDMAP in dbAllocAG()
1363 || bmp->db_agfree[agno] == bmp->db_agsize) { in dbAllocAG()
1365 if ((rc == -ENOSPC) && in dbAllocAG()
1366 (bmp->db_agfree[agno] == bmp->db_agsize)) { in dbAllocAG()
1370 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1379 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel); in dbAllocAG()
1380 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocAG()
1382 return -EIO; in dbAllocAG()
1383 dcp = (struct dmapctl *) mp->data; in dbAllocAG()
1384 budmin = dcp->budmin; in dbAllocAG()
1386 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) { in dbAllocAG()
1387 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n"); in dbAllocAG()
1389 return -EIO; in dbAllocAG()
1400 (1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth; in dbAllocAG()
1401 ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1)); in dbAllocAG()
1403 /* dmap control page trees fan-out by 4 and a single allocation in dbAllocAG()
1409 for (i = 0; i < bmp->db_agwidth; i++, ti++) { in dbAllocAG()
1412 if (l2nb > dcp->stree[ti]) in dbAllocAG()
1419 for (k = bmp->db_agheight; k > 0; k--) { in dbAllocAG()
1421 if (l2nb <= dcp->stree[m + n]) { in dbAllocAG()
1427 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1430 return -EIO; in dbAllocAG()
1437 if (bmp->db_aglevel == 2) in dbAllocAG()
1439 else if (bmp->db_aglevel == 1) in dbAllocAG()
1440 blkno &= ~(MAXL1SIZE - 1); in dbAllocAG()
1441 else /* bmp->db_aglevel == 0 */ in dbAllocAG()
1442 blkno &= ~(MAXL0SIZE - 1); in dbAllocAG()
1445 ((s64) (ti - le32_to_cpu(dcp->leafidx))) << budmin; in dbAllocAG()
1464 dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1, in dbAllocAG()
1466 if (rc == -ENOSPC) { in dbAllocAG()
1467 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1469 return -EIO; in dbAllocAG()
1478 if (rc == -ENOSPC) { in dbAllocAG()
1479 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1481 rc = -EIO; in dbAllocAG()
1487 * return -ENOSPC. in dbAllocAG()
1491 return -ENOSPC; in dbAllocAG()
1508 * bmp - pointer to bmap descriptor
1509 * nblocks - actual number of contiguous free blocks desired.
1510 * l2nb - log2 number of contiguous free blocks desired.
1511 * results - on successful return, set to the starting block number
1515 * 0 - success
1516 * -ENOSPC - insufficient disk resources
1517 * -EIO - i/o error
1532 if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno))) in dbAllocAny()
1538 if (rc == -ENOSPC) { in dbAllocAny()
1539 jfs_error(bmp->db_ipbmap->i_sb, "unable to allocate blocks\n"); in dbAllocAny()
1540 return -EIO; in dbAllocAny()
1558 * - we work only on one ag at some time, minimizing how long we
1560 * - reading / writing the fs is possible most time, even on
1564 * - we write two times to the dmapctl and dmap pages
1565 * - but for me, this seems the best way, better ideas?
1569 * ip - pointer to in-core inode
1570 * agno - ag to trim
1571 * minlen - minimum value of contiguous blocks
1574 * s64 - actual number of blocks trimmed
1578 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbDiscardAG()
1579 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; in dbDiscardAG()
1583 struct super_block *sb = ipbmap->i_sb; in dbDiscardAG()
1590 /* max blkno / nblocks pairs to trim */ in dbDiscardAG()
1597 nblocks = bmp->db_agfree[agno]; in dbDiscardAG()
1603 jfs_error(bmp->db_ipbmap->i_sb, "no memory for trim array\n"); in dbDiscardAG()
1612 /* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */ in dbDiscardAG()
1615 tt->blkno = blkno; in dbDiscardAG()
1616 tt->nblocks = nblocks; in dbDiscardAG()
1620 if (bmp->db_agfree[agno] == 0) in dbDiscardAG()
1624 nblocks = bmp->db_agfree[agno]; in dbDiscardAG()
1626 } else if (rc == -ENOSPC) { in dbDiscardAG()
1628 l2nb = BLKSTOL2(nblocks) - 1; in dbDiscardAG()
1632 jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n"); in dbDiscardAG()
1637 if (unlikely(count >= range_cnt - 1)) in dbDiscardAG()
1642 tt->nblocks = 0; /* mark the current end */ in dbDiscardAG()
1643 for (tt = totrim; tt->nblocks != 0; tt++) { in dbDiscardAG()
1646 if (!(JFS_SBI(sb)->flag & JFS_DISCARD)) in dbDiscardAG()
1647 jfs_issue_discard(ip, tt->blkno, tt->nblocks); in dbDiscardAG()
1648 dbFree(ip, tt->blkno, tt->nblocks); in dbDiscardAG()
1649 trimmed += tt->nblocks; in dbDiscardAG()
1670 * bmp - pointer to bmap descriptor
1671 * level - starting dmap control page level.
1672 * l2nb - log2 number of contiguous free blocks desired.
1673 * *blkno - on entry, starting block number for conducting the search.
1678 * 0 - success
1679 * -ENOSPC - insufficient disk resources
1680 * -EIO - i/o error
1697 for (lev = level, b = *blkno; lev >= 0; lev--) { in dbFindCtl()
1701 lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev); in dbFindCtl()
1702 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbFindCtl()
1704 return -EIO; in dbFindCtl()
1705 dcp = (struct dmapctl *) mp->data; in dbFindCtl()
1706 budmin = dcp->budmin; in dbFindCtl()
1708 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) { in dbFindCtl()
1709 jfs_error(bmp->db_ipbmap->i_sb, in dbFindCtl()
1712 return -EIO; in dbFindCtl()
1730 jfs_error(bmp->db_ipbmap->i_sb, in dbFindCtl()
1732 return -EIO; in dbFindCtl()
1734 return -ENOSPC; in dbFindCtl()
1775 * group whose size is equal to the number of blocks per dmap.
1787 * bmp - pointer to bmap descriptor
1788 * nblocks - actual number of contiguous free blocks to allocate.
1789 * l2nb - log2 number of contiguous free blocks to allocate.
1790 * blkno - starting block number of the dmap to start the allocation
1792 * results - on successful return, set to the starting block number
1796 * 0 - success
1797 * -ENOSPC - insufficient disk resources
1798 * -EIO - i/o error
1815 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbAllocCtl()
1816 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1818 return -EIO; in dbAllocCtl()
1819 dp = (struct dmap *) mp->data; in dbAllocCtl()
1835 assert((blkno & (BPERDMAP - 1)) == 0); in dbAllocCtl()
1839 for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) { in dbAllocCtl()
1842 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); in dbAllocCtl()
1843 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1845 rc = -EIO; in dbAllocCtl()
1848 dp = (struct dmap *) mp->data; in dbAllocCtl()
1852 if (dp->tree.stree[ROOT] != L2BPERDMAP) { in dbAllocCtl()
1854 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocCtl()
1856 rc = -EIO; in dbAllocCtl()
1891 for (n = nblocks - n, b = blkno; n > 0; in dbAllocCtl()
1892 n -= BPERDMAP, b += BPERDMAP) { in dbAllocCtl()
1895 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); in dbAllocCtl()
1896 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1901 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocCtl()
1905 dp = (struct dmap *) mp->data; in dbAllocCtl()
1914 jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n"); in dbAllocCtl()
1938 * mp - pointer to bmap descriptor
1939 * dp - pointer to dmap to attempt to allocate blocks from.
1940 * l2nb - log2 number of contiguous block desired.
1941 * nblocks - actual number of contiguous block desired.
1942 * results - on successful return, set to the starting block number
1946 * 0 - success
1947 * -ENOSPC - insufficient disk resources
1948 * -EIO - i/o error
1967 if (dbFindLeaf((dmtree_t *) &dp->tree, l2nb, &leafidx, false)) in dbAllocDmapLev()
1968 return -ENOSPC; in dbAllocDmapLev()
1971 return -EIO; in dbAllocDmapLev()
1976 blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD); in dbAllocDmapLev()
1978 /* if not all bits of the dmap word are free, get the starting in dbAllocDmapLev()
1979 * bit number within the dmap word of the required string of free in dbAllocDmapLev()
1980 * bits and adjust the block number with this value. in dbAllocDmapLev()
1982 if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN) in dbAllocDmapLev()
1983 blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb); in dbAllocDmapLev()
2009 * bmp - pointer to bmap descriptor
2010 * dp - pointer to dmap to allocate the block range from.
2011 * blkno - starting block number of the block to be allocated.
2012 * nblocks - number of blocks to be allocated.
2015 * 0 - success
2016 * -EIO - i/o error
2029 oldroot = dp->tree.stree[ROOT]; in dbAllocDmap()
2031 /* allocate the specified (blocks) bits */ in dbAllocDmap()
2035 if (dp->tree.stree[ROOT] == oldroot) in dbAllocDmap()
2042 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0))) in dbAllocDmap()
2064 * bmp - pointer to bmap descriptor
2065 * dp - pointer to dmap to free the block range from.
2066 * blkno - starting block number of the block to be freed.
2067 * nblocks - number of blocks to be freed.
2070 * 0 - success
2071 * -EIO - i/o error
2079 int rc = 0, word; in dbFreeDmap() local
2084 oldroot = dp->tree.stree[ROOT]; in dbFreeDmap()
2086 /* free the specified (blocks) bits */ in dbFreeDmap()
2090 if (rc || (dp->tree.stree[ROOT] == oldroot)) in dbFreeDmap()
2097 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) { in dbFreeDmap()
2098 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD; in dbFreeDmap()
2105 if (dp->tree.stree[word] == NOFREE) in dbFreeDmap()
2106 dbBackSplit((dmtree_t *)&dp->tree, word, false); in dbFreeDmap()
2122 * updates the bits of the working map and causes the adjustment
2124 * leaves to reflect the bits allocated. it also causes the
2128 * bmp - pointer to bmap descriptor
2129 * dp - pointer to dmap to allocate bits from.
2130 * blkno - starting block number of the bits to be allocated.
2131 * nblocks - number of bits to be allocated.
2140 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno; in dbAllocBits() local
2141 dmtree_t *tp = (dmtree_t *) & dp->tree; in dbAllocBits()
2146 leaf = dp->tree.stree + LEAFIND; in dbAllocBits()
2148 /* determine the bit number and word within the dmap of the in dbAllocBits()
2151 dbitno = blkno & (BPERDMAP - 1); in dbAllocBits()
2152 word = dbitno >> L2DBWORD; in dbAllocBits()
2157 /* allocate the bits of the dmap's words corresponding to the block in dbAllocBits()
2158 * range. not all bits of the first and last words may be contained in dbAllocBits()
2161 * (a single pass), allocating the bits of interest by hand and in dbAllocBits()
2162 * updating the leaf corresponding to the dmap word. a single pass in dbAllocBits()
2164 * specified range. within this pass, the bits of all fully contained in dbAllocBits()
2170 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocBits()
2171 /* determine the bit number within the word and in dbAllocBits()
2172 * the number of bits within the word. in dbAllocBits()
2174 wbitno = dbitno & (DBWORD - 1); in dbAllocBits()
2175 nb = min(rembits, DBWORD - wbitno); in dbAllocBits()
2177 /* check if only part of a word is to be allocated. in dbAllocBits()
2180 /* allocate (set to 1) the appropriate bits within in dbAllocBits()
2181 * this dmap word. in dbAllocBits()
2183 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) in dbAllocBits()
2186 /* update the leaf for this dmap word. in addition in dbAllocBits()
2187 * to setting the leaf value to the binary buddy max in dbAllocBits()
2188 * of the updated dmap word, dbSplit() will split in dbAllocBits()
2191 dbSplit(tp, word, BUDMIN, in dbAllocBits()
2192 dbMaxBud((u8 *)&dp->wmap[word]), false); in dbAllocBits()
2194 word += 1; in dbAllocBits()
2198 * words and allocate (set to 1) the bits of these in dbAllocBits()
2202 memset(&dp->wmap[word], (int) ONES, nwords * 4); in dbAllocBits()
2204 /* determine how many bits. in dbAllocBits()
2211 for (; nwords > 0; nwords -= nw) { in dbAllocBits()
2212 if (leaf[word] < BUDMIN) { in dbAllocBits()
2213 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocBits()
2220 * of bits being allocated and the l2 number in dbAllocBits()
2221 * of bits currently described by this leaf. in dbAllocBits()
2223 size = min_t(int, leaf[word], in dbAllocBits()
2232 dbSplit(tp, word, size, NOFREE, false); in dbAllocBits()
2236 word += nw; in dbAllocBits()
2242 le32_add_cpu(&dp->nfree, -nblocks); in dbAllocBits()
2248 * group is the new max. in dbAllocBits()
2250 agno = blkno >> bmp->db_agl2size; in dbAllocBits()
2251 if (agno > bmp->db_maxag) in dbAllocBits()
2252 bmp->db_maxag = agno; in dbAllocBits()
2255 bmp->db_agfree[agno] -= nblocks; in dbAllocBits()
2256 bmp->db_nfree -= nblocks; in dbAllocBits()
2269 * updates the bits of the working map and causes the adjustment
2271 * leaves to reflect the bits freed. it also causes the dmap's
2275 * bmp - pointer to bmap descriptor
2276 * dp - pointer to dmap to free bits from.
2277 * blkno - starting block number of the bits to be freed.
2278 * nblocks - number of bits to be freed.
2287 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno; in dbFreeBits() local
2288 dmtree_t *tp = (dmtree_t *) & dp->tree; in dbFreeBits()
2292 /* determine the bit number and word within the dmap of the in dbFreeBits()
2295 dbitno = blkno & (BPERDMAP - 1); in dbFreeBits()
2296 word = dbitno >> L2DBWORD; in dbFreeBits()
2302 /* free the bits of the dmaps words corresponding to the block range. in dbFreeBits()
2303 * not all bits of the first and last words may be contained within in dbFreeBits()
2306 * (a single pass), freeing the bits of interest by hand and updating in dbFreeBits()
2307 * the leaf corresponding to the dmap word. a single pass will be used in dbFreeBits()
2309 * within this pass, the bits of all fully contained dmap words will in dbFreeBits()
2319 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbFreeBits()
2320 /* determine the bit number within the word and in dbFreeBits()
2321 * the number of bits within the word. in dbFreeBits()
2323 wbitno = dbitno & (DBWORD - 1); in dbFreeBits()
2324 nb = min(rembits, DBWORD - wbitno); in dbFreeBits()
2326 /* check if only part of a word is to be freed. in dbFreeBits()
2329 /* free (zero) the appropriate bits within this in dbFreeBits()
2330 * dmap word. in dbFreeBits()
2332 dp->wmap[word] &= in dbFreeBits()
2333 cpu_to_le32(~(ONES << (DBWORD - nb) in dbFreeBits()
2336 /* update the leaf for this dmap word. in dbFreeBits()
2338 rc = dbJoin(tp, word, in dbFreeBits()
2339 dbMaxBud((u8 *)&dp->wmap[word]), false); in dbFreeBits()
2343 word += 1; in dbFreeBits()
2347 * words and free (zero) the bits of these words. in dbFreeBits()
2350 memset(&dp->wmap[word], 0, nwords * 4); in dbFreeBits()
2352 /* determine how many bits. in dbFreeBits()
2359 for (; nwords > 0; nwords -= nw) { in dbFreeBits()
2362 * of bits being freed and the l2 (max) number in dbFreeBits()
2363 * of bits that can be described by this leaf. in dbFreeBits()
2367 (word, L2LPERDMAP, BUDMIN), in dbFreeBits()
2372 rc = dbJoin(tp, word, size, false); in dbFreeBits()
2379 word += nw; in dbFreeBits()
2386 le32_add_cpu(&dp->nfree, nblocks); in dbFreeBits()
2393 agno = blkno >> bmp->db_agl2size; in dbFreeBits()
2394 bmp->db_nfree += nblocks; in dbFreeBits()
2395 bmp->db_agfree[agno] += nblocks; in dbFreeBits()
2402 if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) || in dbFreeBits()
2403 (agno == bmp->db_numag - 1 && in dbFreeBits()
2404 bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) { in dbFreeBits()
2405 while (bmp->db_maxag > 0) { in dbFreeBits()
2406 bmp->db_maxag -= 1; in dbFreeBits()
2407 if (bmp->db_agfree[bmp->db_maxag] != in dbFreeBits()
2408 bmp->db_agsize) in dbFreeBits()
2412 /* re-establish the allocation group preference if the in dbFreeBits()
2416 if (bmp->db_agpref > bmp->db_maxag) in dbFreeBits()
2417 bmp->db_agpref = bmp->db_maxag; in dbFreeBits()
2451 * bmp - pointer to bmap descriptor
2452 * blkno - the first block of a block range within a dmap. it is
2455 * newval - the new value of the lower level dmap or dmap control
2457 * alloc - 'true' if adjustment is due to an allocation.
2458 * level - current level of dmap control page (i.e. L0, L1, L2) to
2462 * 0 - success
2463 * -EIO - i/o error
2480 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level); in dbAdjCtl()
2481 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAdjCtl()
2483 return -EIO; in dbAdjCtl()
2484 dcp = (struct dmapctl *) mp->data; in dbAdjCtl()
2486 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) { in dbAdjCtl()
2487 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n"); in dbAdjCtl()
2489 return -EIO; in dbAdjCtl()
2495 leafno = BLKTOCTLLEAF(blkno, dcp->budmin); in dbAdjCtl()
2496 ti = leafno + le32_to_cpu(dcp->leafidx); in dbAdjCtl()
2501 oldval = dcp->stree[ti]; in dbAdjCtl()
2502 oldroot = dcp->stree[ROOT]; in dbAdjCtl()
2529 oldval = dcp->stree[ti]; in dbAdjCtl()
2531 dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval, true); in dbAdjCtl()
2547 if (dcp->stree[ROOT] != oldroot) { in dbAdjCtl()
2551 if (level < bmp->db_maxlevel) { in dbAdjCtl()
2556 dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc, in dbAdjCtl()
2572 if (dcp->stree[ti] == NOFREE) in dbAdjCtl()
2576 dcp->budmin, oldval, true); in dbAdjCtl()
2589 assert(level == bmp->db_maxlevel); in dbAdjCtl()
2590 if (bmp->db_maxfreebud != oldroot) { in dbAdjCtl()
2591 jfs_error(bmp->db_ipbmap->i_sb, in dbAdjCtl()
2594 bmp->db_maxfreebud = dcp->stree[ROOT]; in dbAdjCtl()
2614 * tp - pointer to the tree containing the leaf.
2615 * leafno - the number of the leaf to be updated.
2616 * splitsz - the size the binary buddy system starting at the leaf
2618 * newval - the new value for the leaf.
2628 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbSplit()
2632 if (leaf[leafno] > tp->dmt_budmin) { in dbSplit()
2636 * - 1 in l2) and the corresponding buddy size. in dbSplit()
2638 cursz = leaf[leafno] - 1; in dbSplit()
2639 budsz = BUDSIZE(cursz, tp->dmt_budmin); in dbSplit()
2650 cursz -= 1; in dbSplit()
2682 * tp - pointer to the tree containing the leaf.
2683 * leafno - the number of the leaf to be updated.
2693 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbBackSplit()
2708 LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs), in dbBackSplit()
2709 tp->dmt_budmin); in dbBackSplit()
2715 budsz = BUDSIZE(size, tp->dmt_budmin); in dbBackSplit()
2724 if (bsz >= le32_to_cpu(tp->dmt_nleafs)) { in dbBackSplit()
2726 return -EIO; in dbBackSplit()
2739 cursz = leaf[bud] - 1; in dbBackSplit()
2748 return -EIO; in dbBackSplit()
2758 * the leaf with other leaves of the dmtree into a multi-leaf
2762 * tp - pointer to the tree containing the leaf.
2763 * leafno - the number of the leaf to be updated.
2764 * newval - the new value for the leaf.
2775 if (newval >= tp->dmt_budmin) { in dbJoin()
2778 leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbJoin()
2793 budsz = BUDSIZE(newval, tp->dmt_budmin); in dbJoin()
2797 while (budsz < le32_to_cpu(tp->dmt_nleafs)) { in dbJoin()
2810 return -EIO; in dbJoin()
2856 * tp - pointer to the tree to be adjusted.
2857 * leafno - the number of the leaf to be updated.
2858 * newval - the new value for the leaf.
2865 int max, size; in dbAdjTree() local
2871 lp = leafno + le32_to_cpu(tp->dmt_leafidx); in dbAdjTree()
2879 if (tp->dmt_stree[lp] == newval) in dbAdjTree()
2884 tp->dmt_stree[lp] = newval; in dbAdjTree()
2888 for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) { in dbAdjTree()
2892 lp = ((lp - 1) & ~0x03) + 1; in dbAdjTree()
2896 pp = (lp - 1) >> 2; in dbAdjTree()
2900 max = TREEMAX(&tp->dmt_stree[lp]); in dbAdjTree()
2905 if (tp->dmt_stree[pp] == max) in dbAdjTree()
2910 tp->dmt_stree[pp] = max; in dbAdjTree()
2912 /* parent becomes leaf for next go-round. in dbAdjTree()
2931 * tp - pointer to the tree to be searched.
2932 * l2nb - log2 number of free blocks to search for.
2933 * leafidx - return pointer to be set to the index of the leaf
2936 * is_ctl - determines if the tree is of type ctl
2939 * 0 - success
2940 * -ENOSPC - insufficient free blocks.
2952 if (l2nb > tp->dmt_stree[ROOT]) in dbFindLeaf()
2953 return -ENOSPC; in dbFindLeaf()
2959 for (k = le32_to_cpu(tp->dmt_height), ti = 1; in dbFindLeaf()
2960 k > 0; k--, ti = ((ti + n) << 2) + 1) { in dbFindLeaf()
2969 return -ENOSPC; in dbFindLeaf()
2970 if (l2nb <= tp->dmt_stree[x + n]) in dbFindLeaf()
2983 *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx); in dbFindLeaf()
2992 * FUNCTION: find a specified number of binary buddy free bits within a
2993 * dmap bitmap word value.
2996 * bits at (1 << l2nb) alignments within the value.
2999 * word - dmap bitmap word value.
3000 * l2nb - number of free bits specified as a log2 number.
3003 * starting bit number of free bits.
3005 static int dbFindBits(u32 word, int l2nb) in dbFindBits() argument
3010 /* get the number of bits. in dbFindBits()
3015 /* complement the word so we can use a mask (i.e. 0s represent in dbFindBits()
3016 * free bits) and compute the mask. in dbFindBits()
3018 word = ~word; in dbFindBits()
3019 mask = ONES << (DBWORD - nb); in dbFindBits()
3021 /* scan the word for nb free bits at nb alignments. in dbFindBits()
3024 if ((mask & word) == mask) in dbFindBits()
3040 * bits within 32-bits of the map.
3043 * cp - pointer to the 32-bit value.
3046 * largest binary buddy of free bits within a dmap word.
3052 /* check if the wmap word is all free. if so, the in dbMaxBud()
3058 /* check if the wmap word is half free. if so, the in dbMaxBud()
3059 * free buddy size is BUDMIN-1. in dbMaxBud()
3062 return (BUDMIN - 1); in dbMaxBud()
3065 * size thru table lookup using quarters of the wmap word. in dbMaxBud()
3067 tmp1 = max(budtab[cp[2]], budtab[cp[3]]); in dbMaxBud()
3068 tmp2 = max(budtab[cp[0]], budtab[cp[1]]); in dbMaxBud()
3069 return (max(tmp1, tmp2)); in dbMaxBud()
3074 * NAME: cnttz(uint word)
3076 * FUNCTION: determine the number of trailing zeros within a 32-bit
3080 * value - 32-bit value to be examined.
3085 static int cnttz(u32 word) in cnttz() argument
3089 for (n = 0; n < 32; n++, word >>= 1) { in cnttz()
3090 if (word & 0x01) in cnttz()
3101 * FUNCTION: determine the number of leading zeros within a 32-bit
3105 * value - 32-bit value to be examined.
3130 * nb - number of blocks
3140 mask = (s64) 1 << (64 - 1); in blkstol2()
3142 /* count the leading bits. in blkstol2()
3150 l2nb = (64 - 1) - l2nb; in blkstol2()
3175 * ip - pointer to in-core inode;
3176 * blkno - starting block number to be freed.
3177 * nblocks - number of blocks to be freed.
3180 * 0 - success
3181 * -EIO - i/o error
3189 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbAllocBottomUp()
3190 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; in dbAllocBottomUp()
3195 ASSERT(nblocks <= bmp->db_mapsize - blkno); in dbAllocBottomUp()
3201 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { in dbAllocBottomUp()
3208 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbAllocBottomUp()
3212 return -EIO; in dbAllocBottomUp()
3214 dp = (struct dmap *) mp->data; in dbAllocBottomUp()
3219 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1))); in dbAllocBottomUp()
3242 int dbitno, word, rembits, nb, nwords, wbitno, agno; in dbAllocDmapBU() local
3244 struct dmaptree *tp = (struct dmaptree *) & dp->tree; in dbAllocDmapBU()
3249 oldroot = tp->stree[ROOT]; in dbAllocDmapBU()
3251 /* determine the bit number and word within the dmap of the in dbAllocDmapBU()
3254 dbitno = blkno & (BPERDMAP - 1); in dbAllocDmapBU()
3255 word = dbitno >> L2DBWORD; in dbAllocDmapBU()
3260 /* allocate the bits of the dmap's words corresponding to the block in dbAllocDmapBU()
3261 * range. not all bits of the first and last words may be contained in dbAllocDmapBU()
3264 * (a single pass), allocating the bits of interest by hand and in dbAllocDmapBU()
3265 * updating the leaf corresponding to the dmap word. a single pass in dbAllocDmapBU()
3267 * specified range. within this pass, the bits of all fully contained in dbAllocDmapBU()
3273 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocDmapBU()
3274 /* determine the bit number within the word and in dbAllocDmapBU()
3275 * the number of bits within the word. in dbAllocDmapBU()
3277 wbitno = dbitno & (DBWORD - 1); in dbAllocDmapBU()
3278 nb = min(rembits, DBWORD - wbitno); in dbAllocDmapBU()
3280 /* check if only part of a word is to be allocated. in dbAllocDmapBU()
3283 /* allocate (set to 1) the appropriate bits within in dbAllocDmapBU()
3284 * this dmap word. in dbAllocDmapBU()
3286 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) in dbAllocDmapBU()
3289 word++; in dbAllocDmapBU()
3293 * words and allocate (set to 1) the bits of these in dbAllocDmapBU()
3297 memset(&dp->wmap[word], (int) ONES, nwords * 4); in dbAllocDmapBU()
3299 /* determine how many bits */ in dbAllocDmapBU()
3301 word += nwords; in dbAllocDmapBU()
3306 le32_add_cpu(&dp->nfree, -nblocks); in dbAllocDmapBU()
3315 * if this allocation group is the new max. in dbAllocDmapBU()
3317 agno = blkno >> bmp->db_agl2size; in dbAllocDmapBU()
3318 if (agno > bmp->db_maxag) in dbAllocDmapBU()
3319 bmp->db_maxag = agno; in dbAllocDmapBU()
3322 bmp->db_agfree[agno] -= nblocks; in dbAllocDmapBU()
3323 bmp->db_nfree -= nblocks; in dbAllocDmapBU()
3328 if (tp->stree[ROOT] == oldroot) in dbAllocDmapBU()
3335 if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0))) in dbAllocDmapBU()
3350 * L1---------------------------------L1
3352 * L0---------L0---------L0 L0---------L0---------L0
3357 * <---old---><----------------------------extend----------------------->
3361 struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb); in dbExtendFS()
3362 int nbperpage = sbi->nbperpage; in dbExtendFS()
3370 struct bmap *bmp = sbi->bmap; in dbExtendFS()
3387 bmp->db_mapsize = newsize; in dbExtendFS()
3388 bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize); in dbExtendFS()
3392 oldl2agsize = bmp->db_agl2size; in dbExtendFS()
3394 bmp->db_agl2size = l2agsize; in dbExtendFS()
3395 bmp->db_agsize = 1 << l2agsize; in dbExtendFS()
3398 agno = bmp->db_numag; in dbExtendFS()
3399 bmp->db_numag = newsize >> l2agsize; in dbExtendFS()
3400 bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0; in dbExtendFS()
3407 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn; in dbExtendFS()
3412 k = 1 << (l2agsize - oldl2agsize); in dbExtendFS()
3413 ag_rem = bmp->db_agfree[0]; /* save agfree[0] */ in dbExtendFS()
3415 bmp->db_agfree[n] = 0; /* init collection point */ in dbExtendFS()
3420 bmp->db_agfree[n] += bmp->db_agfree[i]; in dbExtendFS()
3423 bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */ in dbExtendFS()
3426 bmp->db_agfree[n] = 0; in dbExtendFS()
3432 bmp->db_maxag = bmp->db_maxag / k; in dbExtendFS()
3445 jfs_error(ipbmap->i_sb, "L2 page could not be read\n"); in dbExtendFS()
3446 return -EIO; in dbExtendFS()
3448 l2dcp = (struct dmapctl *) l2mp->data; in dbExtendFS()
3452 l2leaf = l2dcp->stree + CTLLEAFIND + k; in dbExtendFS()
3453 p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */ in dbExtendFS()
3461 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */ in dbExtendFS()
3465 l1dcp = (struct dmapctl *) l1mp->data; in dbExtendFS()
3468 j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE; in dbExtendFS()
3469 l1leaf = l1dcp->stree + CTLLEAFIND + j; in dbExtendFS()
3470 p = BLKTOL0(blkno, sbi->l2nbperpage); in dbExtendFS()
3478 l1dcp = (struct dmapctl *) l1mp->data; in dbExtendFS()
3482 l1leaf = l1dcp->stree + CTLLEAFIND; in dbExtendFS()
3492 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */ in dbExtendFS()
3497 l0dcp = (struct dmapctl *) l0mp->data; in dbExtendFS()
3500 i = (blkno & (MAXL0SIZE - 1)) >> in dbExtendFS()
3502 l0leaf = l0dcp->stree + CTLLEAFIND + i; in dbExtendFS()
3504 sbi->l2nbperpage); in dbExtendFS()
3512 l0dcp = (struct dmapctl *) l0mp->data; in dbExtendFS()
3516 l0leaf = l0dcp->stree + CTLLEAFIND; in dbExtendFS()
3528 if ((n = blkno & (BPERDMAP - 1))) { in dbExtendFS()
3534 n = min(nblocks, (s64)BPERDMAP - n); in dbExtendFS()
3545 dp = (struct dmap *) mp->data; in dbExtendFS()
3548 bmp->db_nfree += n; in dbExtendFS()
3549 agno = le64_to_cpu(dp->start) >> l2agsize; in dbExtendFS()
3550 bmp->db_agfree[agno] += n; in dbExtendFS()
3558 nblocks -= n; in dbExtendFS()
3579 bmp->db_maxfreebud = *l1leaf; in dbExtendFS()
3603 bmp->db_maxfreebud = *l2leaf; in dbExtendFS()
3610 jfs_error(ipbmap->i_sb, "function has not returned as expected\n"); in dbExtendFS()
3617 return -EIO; in dbExtendFS()
3633 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbFinalizeBmap()
3648 actags = bmp->db_maxag + 1; in dbFinalizeBmap()
3649 inactags = bmp->db_numag - actags; in dbFinalizeBmap()
3650 ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */ in dbFinalizeBmap()
3658 ((inactags - 1) << bmp->db_agl2size) + ag_rem in dbFinalizeBmap()
3659 : inactags << bmp->db_agl2size; in dbFinalizeBmap()
3665 actfree = bmp->db_nfree - inactfree; in dbFinalizeBmap()
3669 * re-establish the preferred group as the leftmost in dbFinalizeBmap()
3672 if (bmp->db_agfree[bmp->db_agpref] < avgfree) { in dbFinalizeBmap()
3673 for (bmp->db_agpref = 0; bmp->db_agpref < actags; in dbFinalizeBmap()
3674 bmp->db_agpref++) { in dbFinalizeBmap()
3675 if (bmp->db_agfree[bmp->db_agpref] >= avgfree) in dbFinalizeBmap()
3678 if (bmp->db_agpref >= bmp->db_numag) { in dbFinalizeBmap()
3679 jfs_error(ipbmap->i_sb, in dbFinalizeBmap()
3691 bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize); in dbFinalizeBmap()
3693 bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL); in dbFinalizeBmap()
3694 bmp->db_agheight = l2nl >> 1; in dbFinalizeBmap()
3695 bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1)); in dbFinalizeBmap()
3696 for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0; in dbFinalizeBmap()
3697 i--) { in dbFinalizeBmap()
3698 bmp->db_agstart += n; in dbFinalizeBmap()
3717 * dp - pointer to page of map
3718 * nblocks - number of blocks this page
3727 blkno = Blkno & (BPERDMAP - 1); in dbInitDmap()
3730 dp->nblocks = dp->nfree = cpu_to_le32(nblocks); in dbInitDmap()
3731 dp->start = cpu_to_le64(Blkno); in dbInitDmap()
3734 memset(&dp->wmap[0], 0, LPERDMAP * 4); in dbInitDmap()
3735 memset(&dp->pmap[0], 0, LPERDMAP * 4); in dbInitDmap()
3739 le32_add_cpu(&dp->nblocks, nblocks); in dbInitDmap()
3740 le32_add_cpu(&dp->nfree, nblocks); in dbInitDmap()
3743 /* word number containing start block number */ in dbInitDmap()
3747 * free the bits corresponding to the block range (ZEROS): in dbInitDmap()
3748 * note: not all bits of the first and last words may be contained in dbInitDmap()
3751 for (r = nblocks; r > 0; r -= nb, blkno += nb) { in dbInitDmap()
3752 /* number of bits preceding range to be freed in the word */ in dbInitDmap()
3753 b = blkno & (DBWORD - 1); in dbInitDmap()
3754 /* number of bits to free in the word */ in dbInitDmap()
3755 nb = min(r, DBWORD - b); in dbInitDmap()
3757 /* is partial word to be freed ? */ in dbInitDmap()
3759 /* free (set to 0) from the bitmap word */ in dbInitDmap()
3760 dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) in dbInitDmap()
3762 dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) in dbInitDmap()
3765 /* skip the word freed */ in dbInitDmap()
3770 memset(&dp->wmap[w], 0, nw * 4); in dbInitDmap()
3771 memset(&dp->pmap[w], 0, nw * 4); in dbInitDmap()
3780 * mark bits following the range to be freed (non-existing in dbInitDmap()
3787 /* the first word beyond the end of existing blocks */ in dbInitDmap()
3790 /* does nblocks fall on a 32-bit boundary ? */ in dbInitDmap()
3791 b = blkno & (DBWORD - 1); in dbInitDmap()
3793 /* mark a partial word allocated */ in dbInitDmap()
3794 dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b); in dbInitDmap()
3800 dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES); in dbInitDmap()
3818 * dp - dmap to complete
3819 * blkno - starting block number for this dmap
3820 * treemax - will be filled in with max free for this dmap
3822 * RETURNS: max free string at the root of the tree
3831 tp = &dp->tree; in dbInitDmapTree()
3832 tp->nleafs = cpu_to_le32(LPERDMAP); in dbInitDmapTree()
3833 tp->l2nleafs = cpu_to_le32(L2LPERDMAP); in dbInitDmapTree()
3834 tp->leafidx = cpu_to_le32(LEAFIND); in dbInitDmapTree()
3835 tp->height = cpu_to_le32(4); in dbInitDmapTree()
3836 tp->budmin = BUDMIN; in dbInitDmapTree()
3838 /* init each leaf from corresponding wmap word: in dbInitDmapTree()
3839 * note: leaf is set to NOFREE(-1) if all blocks of corresponding in dbInitDmapTree()
3840 * bitmap word are allocated. in dbInitDmapTree()
3842 cp = tp->stree + le32_to_cpu(tp->leafidx); in dbInitDmapTree()
3844 *cp++ = dbMaxBud((u8 *) & dp->wmap[i]); in dbInitDmapTree()
3857 * from corresponding bitmap word or root of summary tree
3863 * cp - Pointer to the root of the tree
3864 * l2leaves- Number of leaf nodes as a power of 2
3865 * l2min - Number of blocks that can be covered by a leaf
3868 * RETURNS: max free string at the root of the tree
3876 tp = dtp->stree; in dbInitTree()
3879 l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin; in dbInitTree()
3887 * the combination will result in the left-most buddy leaf having in dbInitTree()
3895 for (l2free = dtp->budmin, bsize = 1; l2free < l2max; in dbInitTree()
3901 for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx); in dbInitTree()
3902 i < le32_to_cpu(dtp->nleafs); in dbInitTree()
3904 /* coalesce if both adjacent buddies are max free */ in dbInitTree()
3907 *(cp + bsize) = -1; /* right give left */ in dbInitTree()
3922 for (child = le32_to_cpu(dtp->leafidx), in dbInitTree()
3923 nparent = le32_to_cpu(dtp->nleafs) >> 2; in dbInitTree()
3926 parent = (child - 1) >> 2; in dbInitTree()
3949 dcp->nleafs = cpu_to_le32(LPERCTL); in dbInitDmapCtl()
3950 dcp->l2nleafs = cpu_to_le32(L2LPERCTL); in dbInitDmapCtl()
3951 dcp->leafidx = cpu_to_le32(CTLLEAFIND); in dbInitDmapCtl()
3952 dcp->height = cpu_to_le32(5); in dbInitDmapCtl()
3953 dcp->budmin = L2BPERDMAP + L2LPERCTL * level; in dbInitDmapCtl()
3960 cp = &dcp->stree[CTLLEAFIND + i]; in dbInitDmapCtl()
3975 * nblocks - Number of blocks in aggregate
3989 m = ((u64) 1 << (64 - 1)); in dbGetL2AGSize()
3990 for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) { in dbGetL2AGSize()
4000 return (l2sz - L2MAXAG); in dbGetL2AGSize()
4028 struct super_block *sb = ipbmap->i_sb; in dbMapFileSizeToMapSize()
4034 nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize; in dbMapFileSizeToMapSize()
4035 npages = nblocks >> JFS_SBI(sb)->l2nbperpage; in dbMapFileSizeToMapSize()
4043 npages--; /* skip the first global control page */ in dbMapFileSizeToMapSize()
4045 npages -= (2 - level); in dbMapFileSizeToMapSize()
4046 npages--; /* skip top level's control page */ in dbMapFileSizeToMapSize()
4047 for (i = level; i >= 0; i--) { in dbMapFileSizeToMapSize()
4057 npages--; in dbMapFileSizeToMapSize()