Lines Matching +full:rc +full:- +full:map +full:- +full:name
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2004
19 * SERIALIZATION of the Block Allocation Map.
21 * the working state of the block allocation map is accessed in
41 * take the lock in read mode. a single top-down request may proceed
42 * exclusively while multiple bottoms-up requests may proceed
46 * the working state of the block allocation map also includes read/
50 * in the face of multiple-bottoms up requests.
53 * accesses to the persistent state of the block allocation map (limited
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)
133 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1
137 * NAME: dbMount()
139 * FUNCTION: initializate the block allocation map.
141 * memory is allocated for the in-core bmap descriptor and
142 * the in-core descriptor is initialized from disk.
145 * ipbmap - pointer to in-core inode for the block map.
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 || bmp->db_numag > MAXAG) { 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()
250 * NAME: dbUnmount()
252 * FUNCTION: terminate the block allocation map in preparation for
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()
338 * NAME: dbFree()
341 * allocation map.
343 * the blocks will be free from the working map one dmap
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
359 int nb, rc; in dbFree() local
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()
409 if ((rc = dbFreeDmap(bmp, dp, blkno, nb))) { in dbFree()
410 jfs_error(ip->i_sb, "error in block map\n"); in dbFree()
413 return (rc); in dbFree()
428 * NAME: dbUpdatePMap()
431 * specified block range in the persistent block allocation map.
433 * the blocks will be updated in the persistent map one
437 * ipbmap - pointer to in-core inode for the block map.
438 * free - 'true' if block range is to be freed from the persistent
439 * map; 'false' if it is to be allocated.
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
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()
502 dbitno = blkno & (BPERDMAP - 1); in dbUpdatePMap()
504 nblks = min(rem, (s64)BPERDMAP - dbitno); in dbUpdatePMap()
514 rbits -= nbits, dbitno += nbits) { in dbUpdatePMap()
518 wbitno = dbitno & (DBWORD - 1); in dbUpdatePMap()
519 nbits = min(rbits, DBWORD - wbitno); in dbUpdatePMap()
527 (ONES << (DBWORD - nbits) >> wbitno); in dbUpdatePMap()
529 dp->pmap[word] &= in dbUpdatePMap()
532 dp->pmap[word] |= in dbUpdatePMap()
548 memset(&dp->pmap[word], 0, in dbUpdatePMap()
551 memset(&dp->pmap[word], (int) ONES, 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()
605 * NAME: dbNextAG()
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()
688 * NAME: dbAlloc()
691 * blocks from the working allocation block map.
693 * the block allocation policy uses hints and a multi-step
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
727 int rc, agno; in dbAlloc() local
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()
750 /* the hint should be within the map */ 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()
762 rc = dbAllocAny(bmp, nblocks, l2nb, results); 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()
816 if ((rc = dbAllocNext(bmp, dp, blkno, (int) nblocks)) in dbAlloc()
817 != -ENOSPC) { in dbAlloc()
818 if (rc == 0) { in dbAlloc()
827 writers = atomic_read(&bmp->db_active[agno]); in dbAlloc()
829 ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) { in dbAlloc()
842 if ((rc = in dbAlloc()
844 != -ENOSPC) { in dbAlloc()
845 if (rc == 0) in dbAlloc()
855 if ((rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results)) in dbAlloc()
856 != -ENOSPC) { in dbAlloc()
857 if (rc == 0) in dbAlloc()
872 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC) in dbAlloc()
886 * allocate anywhere in the map. in dbAlloc()
888 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC) in dbAlloc()
889 rc = dbAllocAny(bmp, nblocks, l2nb, results); in dbAlloc()
894 return (rc); in dbAlloc()
899 return (rc); in dbAlloc()
903 * NAME: dbReAlloc()
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
938 int rc; in dbReAlloc() local
942 if ((rc = dbExtend(ip, blkno, nblocks, addnblocks)) == 0) { in dbReAlloc()
946 if (rc != -ENOSPC) in dbReAlloc()
947 return (rc); in dbReAlloc()
956 (ip, blkno + nblocks - 1, addnblocks + nblocks, results)); in dbReAlloc()
961 * NAME: dbExtend()
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()
990 int rc; in dbExtend() local
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()
1048 rc = dbAllocNext(bmp, dp, extblkno, (int) addnblocks); in dbExtend()
1053 if (rc == 0) in dbExtend()
1059 return (rc); in dbExtend()
1064 * NAME: dbAllocNext()
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
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()
1102 dbitno = blkno & (BPERDMAP - 1); in dbAllocNext()
1109 return -ENOSPC; in dbAllocNext()
1115 return -ENOSPC; in dbAllocNext()
1130 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocNext()
1134 wbitno = dbitno & (DBWORD - 1); in dbAllocNext()
1135 nb = min(rembits, DBWORD - wbitno); 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()
1162 return -ENOSPC; in dbAllocNext()
1174 nwords -= nw; in dbAllocNext()
1187 * NAME: dbAllocNear()
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()
1231 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD; in dbAllocNear()
1245 blkno = le64_to_cpu(dp->start) + (word << L2DBWORD); in dbAllocNear()
1254 dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb); in dbAllocNear()
1258 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0) in dbAllocNear()
1261 return (rc); in dbAllocNear()
1264 return -ENOSPC; in dbAllocNear()
1269 * NAME: dbAllocAG()
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
1327 int rc, ti, i, k, m, n, agperlev; in dbAllocAG() local
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()
1364 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results); 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()
1373 return (rc); 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()
1463 if ((rc = 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()
1471 return (rc); in dbAllocAG()
1477 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results); in dbAllocAG()
1478 if (rc == -ENOSPC) { in dbAllocAG()
1479 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocAG()
1481 rc = -EIO; in dbAllocAG()
1483 return (rc); in dbAllocAG()
1487 * return -ENOSPC. in dbAllocAG()
1491 return -ENOSPC; in dbAllocAG()
1496 * NAME: dbAllocAny()
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
1523 int rc; in dbAllocAny() local
1532 if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno))) in dbAllocAny()
1533 return (rc); in dbAllocAny()
1537 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results); 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()
1542 return (rc); in dbAllocAny()
1547 * NAME: dbDiscardAG()
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()
1582 int rc, l2nb; in dbDiscardAG() local
1583 struct super_block *sb = ipbmap->i_sb; 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()
1613 rc = dbAllocAG(bmp, agno, nblocks, l2nb, &blkno); in dbDiscardAG()
1614 if (rc == 0) { 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()
1634 jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n"); in dbDiscardAG()
1639 if (unlikely(count >= range_cnt - 1)) in dbDiscardAG()
1644 tt->nblocks = 0; /* mark the current end */ in dbDiscardAG()
1645 for (tt = totrim; tt->nblocks != 0; tt++) { in dbDiscardAG()
1648 if (!(JFS_SBI(sb)->flag & JFS_DISCARD)) in dbDiscardAG()
1649 jfs_issue_discard(ip, tt->blkno, tt->nblocks); in dbDiscardAG()
1650 dbFree(ip, tt->blkno, tt->nblocks); in dbDiscardAG()
1651 trimmed += tt->nblocks; in dbDiscardAG()
1659 * NAME: dbFindCtl()
1672 * bmp - pointer to bmap descriptor
1673 * level - starting dmap control page level.
1674 * l2nb - log2 number of contiguous free blocks desired.
1675 * *blkno - on entry, starting block number for conducting the search.
1680 * 0 - success
1681 * -ENOSPC - insufficient disk resources
1682 * -EIO - i/o error
1688 int rc, leafidx, lev; in dbFindCtl() local
1699 for (lev = level, b = *blkno; lev >= 0; lev--) { in dbFindCtl()
1703 lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev); in dbFindCtl()
1704 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbFindCtl()
1706 return -EIO; in dbFindCtl()
1707 dcp = (struct dmapctl *) mp->data; in dbFindCtl()
1708 budmin = dcp->budmin; in dbFindCtl()
1710 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) { in dbFindCtl()
1711 jfs_error(bmp->db_ipbmap->i_sb, in dbFindCtl()
1714 return -EIO; in dbFindCtl()
1722 rc = dbFindLeaf((dmtree_t *) dcp, l2nb, &leafidx, true); in dbFindCtl()
1730 if (rc) { in dbFindCtl()
1732 jfs_error(bmp->db_ipbmap->i_sb, in dbFindCtl()
1734 return -EIO; in dbFindCtl()
1736 return -ENOSPC; in dbFindCtl()
1760 * NAME: dbAllocCtl()
1789 * bmp - pointer to bmap descriptor
1790 * nblocks - actual number of contiguous free blocks to allocate.
1791 * l2nb - log2 number of contiguous free blocks to allocate.
1792 * blkno - starting block number of the dmap to start the allocation
1794 * results - on successful return, set to the starting block number
1798 * 0 - success
1799 * -ENOSPC - insufficient disk resources
1800 * -EIO - i/o error
1807 int rc, nb; in dbAllocCtl() local
1817 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbAllocCtl()
1818 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1820 return -EIO; in dbAllocCtl()
1821 dp = (struct dmap *) mp->data; in dbAllocCtl()
1823 if (dp->tree.budmin < 0) in dbAllocCtl()
1824 return -EIO; in dbAllocCtl()
1828 rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results); in dbAllocCtl()
1829 if (rc == 0) in dbAllocCtl()
1834 return (rc); in dbAllocCtl()
1840 assert((blkno & (BPERDMAP - 1)) == 0); in dbAllocCtl()
1844 for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) { in dbAllocCtl()
1847 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); in dbAllocCtl()
1848 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1850 rc = -EIO; in dbAllocCtl()
1853 dp = (struct dmap *) mp->data; in dbAllocCtl()
1857 if (dp->tree.stree[ROOT] != L2BPERDMAP) { in dbAllocCtl()
1859 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocCtl()
1861 rc = -EIO; in dbAllocCtl()
1871 if ((rc = dbAllocDmap(bmp, dp, b, nb))) { in dbAllocCtl()
1896 for (n = nblocks - n, b = blkno; n > 0; in dbAllocCtl()
1897 n -= BPERDMAP, b += BPERDMAP) { in dbAllocCtl()
1900 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); in dbAllocCtl()
1901 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1906 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocCtl()
1910 dp = (struct dmap *) mp->data; in dbAllocCtl()
1919 jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n"); in dbAllocCtl()
1928 return (rc); in dbAllocCtl()
1933 * NAME: dbAllocDmapLev()
1943 * mp - pointer to bmap descriptor
1944 * dp - pointer to dmap to attempt to allocate blocks from.
1945 * l2nb - log2 number of contiguous block desired.
1946 * nblocks - actual number of contiguous block desired.
1947 * results - on successful return, set to the starting block number
1951 * 0 - success
1952 * -ENOSPC - insufficient disk resources
1953 * -EIO - i/o error
1963 int leafidx, rc; in dbAllocDmapLev() local
1972 if (dbFindLeaf((dmtree_t *) &dp->tree, l2nb, &leafidx, false)) in dbAllocDmapLev()
1973 return -ENOSPC; in dbAllocDmapLev()
1976 return -EIO; in dbAllocDmapLev()
1981 blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD); in dbAllocDmapLev()
1987 if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN) in dbAllocDmapLev()
1988 blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb); in dbAllocDmapLev()
1991 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0) in dbAllocDmapLev()
1994 return (rc); in dbAllocDmapLev()
1999 * NAME: dbAllocDmap()
2001 * FUNCTION: adjust the disk allocation map to reflect the allocation
2014 * bmp - pointer to bmap descriptor
2015 * dp - pointer to dmap to allocate the block range from.
2016 * blkno - starting block number of the block to be allocated.
2017 * nblocks - number of blocks to be allocated.
2020 * 0 - success
2021 * -EIO - i/o error
2029 int rc; in dbAllocDmap() local
2034 oldroot = dp->tree.stree[ROOT]; in dbAllocDmap()
2040 if (dp->tree.stree[ROOT] == oldroot) in dbAllocDmap()
2047 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0))) in dbAllocDmap()
2050 return (rc); in dbAllocDmap()
2055 * NAME: dbFreeDmap()
2057 * FUNCTION: adjust the disk allocation map to reflect the allocation
2069 * bmp - pointer to bmap descriptor
2070 * dp - pointer to dmap to free the block range from.
2071 * blkno - starting block number of the block to be freed.
2072 * nblocks - number of blocks to be freed.
2075 * 0 - success
2076 * -EIO - i/o error
2084 int rc = 0, word; in dbFreeDmap() local
2089 oldroot = dp->tree.stree[ROOT]; in dbFreeDmap()
2092 rc = dbFreeBits(bmp, dp, blkno, nblocks); in dbFreeDmap()
2095 if (rc || (dp->tree.stree[ROOT] == oldroot)) in dbFreeDmap()
2096 return (rc); in dbFreeDmap()
2102 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) { in dbFreeDmap()
2103 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD; in dbFreeDmap()
2110 if (dp->tree.stree[word] == NOFREE) in dbFreeDmap()
2111 dbBackSplit((dmtree_t *)&dp->tree, word, false); in dbFreeDmap()
2116 return (rc); in dbFreeDmap()
2121 * NAME: dbAllocBits()
2127 * updates the bits of the working map and causes the adjustment
2133 * bmp - pointer to bmap descriptor
2134 * dp - pointer to dmap to allocate bits from.
2135 * blkno - starting block number of the bits to be allocated.
2136 * nblocks - number of bits to be allocated.
2146 dmtree_t *tp = (dmtree_t *) & dp->tree; in dbAllocBits()
2151 leaf = dp->tree.stree + LEAFIND; in dbAllocBits()
2156 dbitno = blkno & (BPERDMAP - 1); in dbAllocBits()
2175 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocBits()
2179 wbitno = dbitno & (DBWORD - 1); in dbAllocBits()
2180 nb = min(rembits, DBWORD - wbitno); in dbAllocBits()
2188 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) in dbAllocBits()
2197 dbMaxBud((u8 *)&dp->wmap[word]), false); in dbAllocBits()
2207 memset(&dp->wmap[word], (int) ONES, nwords * 4); in dbAllocBits()
2216 for (; nwords > 0; nwords -= nw) { in dbAllocBits()
2218 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocBits()
2247 le32_add_cpu(&dp->nfree, -nblocks); in dbAllocBits()
2255 agno = blkno >> bmp->db_agl2size; in dbAllocBits()
2256 if (agno > bmp->db_maxag) in dbAllocBits()
2257 bmp->db_maxag = agno; in dbAllocBits()
2259 /* update the free count for the allocation group and map */ in dbAllocBits()
2260 bmp->db_agfree[agno] -= nblocks; in dbAllocBits()
2261 bmp->db_nfree -= nblocks; in dbAllocBits()
2268 * NAME: dbFreeBits()
2274 * updates the bits of the working map and causes the adjustment
2280 * bmp - pointer to bmap descriptor
2281 * dp - pointer to dmap to free bits from.
2282 * blkno - starting block number of the bits to be freed.
2283 * nblocks - number of bits to be freed.
2293 dmtree_t *tp = (dmtree_t *) & dp->tree; in dbFreeBits()
2294 int rc = 0; in dbFreeBits() local
2300 dbitno = blkno & (BPERDMAP - 1); in dbFreeBits()
2324 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbFreeBits()
2328 wbitno = dbitno & (DBWORD - 1); in dbFreeBits()
2329 nb = min(rembits, DBWORD - wbitno); in dbFreeBits()
2337 dp->wmap[word] &= in dbFreeBits()
2338 cpu_to_le32(~(ONES << (DBWORD - nb) in dbFreeBits()
2343 rc = dbJoin(tp, word, in dbFreeBits()
2344 dbMaxBud((u8 *)&dp->wmap[word]), false); in dbFreeBits()
2345 if (rc) in dbFreeBits()
2346 return rc; in dbFreeBits()
2355 memset(&dp->wmap[word], 0, nwords * 4); in dbFreeBits()
2364 for (; nwords > 0; nwords -= nw) { in dbFreeBits()
2377 rc = dbJoin(tp, word, size, false); in dbFreeBits()
2378 if (rc) in dbFreeBits()
2379 return rc; in dbFreeBits()
2391 le32_add_cpu(&dp->nfree, nblocks); in dbFreeBits()
2396 * map. in dbFreeBits()
2398 agno = blkno >> bmp->db_agl2size; in dbFreeBits()
2399 bmp->db_nfree += nblocks; in dbFreeBits()
2400 bmp->db_agfree[agno] += nblocks; in dbFreeBits()
2407 if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) || in dbFreeBits()
2408 (agno == bmp->db_numag - 1 && in dbFreeBits()
2409 bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) { in dbFreeBits()
2410 while (bmp->db_maxag > 0) { in dbFreeBits()
2411 bmp->db_maxag -= 1; in dbFreeBits()
2412 if (bmp->db_agfree[bmp->db_maxag] != in dbFreeBits()
2413 bmp->db_agsize) in dbFreeBits()
2417 /* re-establish the allocation group preference if the in dbFreeBits()
2421 if (bmp->db_agpref > bmp->db_maxag) in dbFreeBits()
2422 bmp->db_agpref = bmp->db_maxag; in dbFreeBits()
2432 * NAME: dbAdjCtl()
2456 * bmp - pointer to bmap descriptor
2457 * blkno - the first block of a block range within a dmap. it is
2460 * newval - the new value of the lower level dmap or dmap control
2462 * alloc - 'true' if adjustment is due to an allocation.
2463 * level - current level of dmap control page (i.e. L0, L1, L2) to
2467 * 0 - success
2468 * -EIO - i/o error
2480 int rc, leafno, ti; in dbAdjCtl() local
2485 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level); in dbAdjCtl()
2486 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAdjCtl()
2488 return -EIO; in dbAdjCtl()
2489 dcp = (struct dmapctl *) mp->data; in dbAdjCtl()
2491 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) { in dbAdjCtl()
2492 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n"); in dbAdjCtl()
2494 return -EIO; in dbAdjCtl()
2500 leafno = BLKTOCTLLEAF(blkno, dcp->budmin); in dbAdjCtl()
2501 ti = leafno + le32_to_cpu(dcp->leafidx); in dbAdjCtl()
2506 oldval = dcp->stree[ti]; in dbAdjCtl()
2507 oldroot = dcp->stree[ROOT]; in dbAdjCtl()
2529 rc = dbBackSplit((dmtree_t *)dcp, leafno, true); in dbAdjCtl()
2530 if (rc) { in dbAdjCtl()
2532 return rc; in dbAdjCtl()
2534 oldval = dcp->stree[ti]; in dbAdjCtl()
2536 dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval, true); in dbAdjCtl()
2538 rc = dbJoin((dmtree_t *) dcp, leafno, newval, true); in dbAdjCtl()
2539 if (rc) { in dbAdjCtl()
2541 return rc; in dbAdjCtl()
2547 * the current top level (i.e. L0, L1, L2) of the map. if so (i.e. in dbAdjCtl()
2552 if (dcp->stree[ROOT] != oldroot) { in dbAdjCtl()
2553 /* are we below the top level of the map. if so, in dbAdjCtl()
2556 if (level < bmp->db_maxlevel) { in dbAdjCtl()
2560 if ((rc = in dbAdjCtl()
2561 dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc, in dbAdjCtl()
2577 if (dcp->stree[ti] == NOFREE) in dbAdjCtl()
2581 dcp->budmin, oldval, true); in dbAdjCtl()
2587 return (rc); in dbAdjCtl()
2590 /* we're at the top level of the map. update in dbAdjCtl()
2594 assert(level == bmp->db_maxlevel); in dbAdjCtl()
2595 if (bmp->db_maxfreebud != oldroot) { in dbAdjCtl()
2596 jfs_error(bmp->db_ipbmap->i_sb, in dbAdjCtl()
2599 bmp->db_maxfreebud = dcp->stree[ROOT]; in dbAdjCtl()
2612 * NAME: dbSplit()
2619 * tp - pointer to the tree containing the leaf.
2620 * leafno - the number of the leaf to be updated.
2621 * splitsz - the size the binary buddy system starting at the leaf
2623 * newval - the new value for the leaf.
2633 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbSplit()
2637 if (leaf[leafno] > tp->dmt_budmin) { in dbSplit()
2641 * - 1 in l2) and the corresponding buddy size. in dbSplit()
2643 cursz = leaf[leafno] - 1; in dbSplit()
2644 budsz = BUDSIZE(cursz, tp->dmt_budmin); in dbSplit()
2655 cursz -= 1; in dbSplit()
2668 * NAME: dbBackSplit()
2687 * tp - pointer to the tree containing the leaf.
2688 * leafno - the number of the leaf to be updated.
2698 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbBackSplit()
2713 LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs), in dbBackSplit()
2714 tp->dmt_budmin); in dbBackSplit()
2720 budsz = BUDSIZE(size, tp->dmt_budmin); in dbBackSplit()
2729 if (bsz >= le32_to_cpu(tp->dmt_nleafs)) { in dbBackSplit()
2730 jfs_err("JFS: block map error in dbBackSplit"); in dbBackSplit()
2731 return -EIO; in dbBackSplit()
2744 cursz = leaf[bud] - 1; in dbBackSplit()
2753 return -EIO; in dbBackSplit()
2760 * NAME: dbJoin()
2763 * the leaf with other leaves of the dmtree into a multi-leaf
2767 * tp - pointer to the tree containing the leaf.
2768 * leafno - the number of the leaf to be updated.
2769 * newval - the new value for the leaf.
2780 if (newval >= tp->dmt_budmin) { in dbJoin()
2783 leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbJoin()
2798 budsz = BUDSIZE(newval, tp->dmt_budmin); in dbJoin()
2802 while (budsz < le32_to_cpu(tp->dmt_nleafs)) { in dbJoin()
2815 return -EIO; in dbJoin()
2853 * NAME: dbAdjTree()
2861 * tp - pointer to the tree to be adjusted.
2862 * leafno - the number of the leaf to be updated.
2863 * newval - the new value for the leaf.
2876 lp = leafno + le32_to_cpu(tp->dmt_leafidx); in dbAdjTree()
2884 if (tp->dmt_stree[lp] == newval) in dbAdjTree()
2889 tp->dmt_stree[lp] = newval; in dbAdjTree()
2893 for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) { in dbAdjTree()
2900 lp = ((lp - 1) & ~0x03) + 1; in dbAdjTree()
2904 pp = (lp - 1) >> 2; in dbAdjTree()
2908 max = TREEMAX(&tp->dmt_stree[lp]); in dbAdjTree()
2913 if (tp->dmt_stree[pp] == max) in dbAdjTree()
2918 tp->dmt_stree[pp] = max; in dbAdjTree()
2920 /* parent becomes leaf for next go-round. in dbAdjTree()
2928 * NAME: dbFindLeaf()
2939 * tp - pointer to the tree to be searched.
2940 * l2nb - log2 number of free blocks to search for.
2941 * leafidx - return pointer to be set to the index of the leaf
2944 * is_ctl - determines if the tree is of type ctl
2947 * 0 - success
2948 * -ENOSPC - insufficient free blocks.
2961 if (l2nb > tp->dmt_stree[ROOT]) in dbFindLeaf()
2962 return -ENOSPC; in dbFindLeaf()
2968 for (k = le32_to_cpu(tp->dmt_height), ti = 1; in dbFindLeaf()
2969 k > 0; k--, ti = ((ti + n) << 2) + 1) { in dbFindLeaf()
2978 return -ENOSPC; in dbFindLeaf()
2979 if (l2nb <= tp->dmt_stree[x + n]) in dbFindLeaf()
2988 if (le32_to_cpu(tp->dmt_leafidx) >= max_idx) in dbFindLeaf()
2989 return -ENOSPC; in dbFindLeaf()
2994 *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx); in dbFindLeaf()
3001 * NAME: dbFindBits()
3010 * word - dmap bitmap word value.
3011 * l2nb - number of free bits specified as a log2 number.
3030 mask = ONES << (DBWORD - nb); in dbFindBits()
3048 * NAME: dbMaxBud(u8 *cp)
3051 * bits within 32-bits of the map.
3054 * cp - pointer to the 32-bit value.
3070 * free buddy size is BUDMIN-1. in dbMaxBud()
3073 return (BUDMIN - 1); in dbMaxBud()
3085 * NAME: cnttz(uint word)
3087 * FUNCTION: determine the number of trailing zeros within a 32-bit
3091 * value - 32-bit value to be examined.
3110 * NAME: cntlz(u32 value)
3112 * FUNCTION: determine the number of leading zeros within a 32-bit
3116 * value - 32-bit value to be examined.
3134 * NAME: blkstol2(s64 nb)
3141 * nb - number of blocks
3151 mask = (s64) 1 << (64 - 1); in blkstol2()
3161 l2nb = (64 - 1) - l2nb; in blkstol2()
3177 * NAME: dbAllocBottomUp()
3180 * allocation map.
3182 * the blocks will be alloc from the working map one dmap
3186 * ip - pointer to in-core inode;
3187 * blkno - starting block number to be freed.
3188 * nblocks - number of blocks to be freed.
3191 * 0 - success
3192 * -EIO - i/o error
3198 int nb, rc; in dbAllocBottomUp() local
3200 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbAllocBottomUp()
3201 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; in dbAllocBottomUp()
3206 ASSERT(nblocks <= bmp->db_mapsize - blkno); in dbAllocBottomUp()
3212 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { in dbAllocBottomUp()
3219 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbAllocBottomUp()
3223 return -EIO; in dbAllocBottomUp()
3225 dp = (struct dmap *) mp->data; in dbAllocBottomUp()
3230 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1))); in dbAllocBottomUp()
3233 if ((rc = dbAllocDmapBU(bmp, dp, blkno, nb))) { in dbAllocBottomUp()
3236 return (rc); in dbAllocBottomUp()
3252 int rc; in dbAllocDmapBU() local
3255 struct dmaptree *tp = (struct dmaptree *) & dp->tree; in dbAllocDmapBU()
3260 oldroot = tp->stree[ROOT]; in dbAllocDmapBU()
3265 dbitno = blkno & (BPERDMAP - 1); in dbAllocDmapBU()
3284 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocDmapBU()
3288 wbitno = dbitno & (DBWORD - 1); in dbAllocDmapBU()
3289 nb = min(rembits, DBWORD - wbitno); in dbAllocDmapBU()
3297 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) in dbAllocDmapBU()
3308 memset(&dp->wmap[word], (int) ONES, nwords * 4); in dbAllocDmapBU()
3317 le32_add_cpu(&dp->nfree, -nblocks); in dbAllocDmapBU()
3328 agno = blkno >> bmp->db_agl2size; in dbAllocDmapBU()
3329 if (agno > bmp->db_maxag) in dbAllocDmapBU()
3330 bmp->db_maxag = agno; in dbAllocDmapBU()
3332 /* update the free count for the allocation group and map */ in dbAllocDmapBU()
3333 bmp->db_agfree[agno] -= nblocks; in dbAllocDmapBU()
3334 bmp->db_nfree -= nblocks; in dbAllocDmapBU()
3339 if (tp->stree[ROOT] == oldroot) in dbAllocDmapBU()
3346 if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0))) in dbAllocDmapBU()
3349 return (rc); in dbAllocDmapBU()
3354 * NAME: dbExtendFS()
3361 * L1---------------------------------L1
3363 * L0---------L0---------L0 L0---------L0---------L0
3368 * <---old---><----------------------------extend----------------------->
3372 struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb); in dbExtendFS()
3373 int nbperpage = sbi->nbperpage; in dbExtendFS()
3381 struct bmap *bmp = sbi->bmap; in dbExtendFS()
3398 bmp->db_mapsize = newsize; in dbExtendFS()
3399 bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize); in dbExtendFS()
3403 oldl2agsize = bmp->db_agl2size; in dbExtendFS()
3405 bmp->db_agl2size = l2agsize; in dbExtendFS()
3406 bmp->db_agsize = 1 << l2agsize; in dbExtendFS()
3409 agno = bmp->db_numag; in dbExtendFS()
3410 bmp->db_numag = newsize >> l2agsize; in dbExtendFS()
3411 bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0; in dbExtendFS()
3418 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn; in dbExtendFS()
3423 k = 1 << (l2agsize - oldl2agsize); in dbExtendFS()
3424 ag_rem = bmp->db_agfree[0]; /* save agfree[0] */ in dbExtendFS()
3426 bmp->db_agfree[n] = 0; /* init collection point */ in dbExtendFS()
3431 bmp->db_agfree[n] += bmp->db_agfree[i]; in dbExtendFS()
3434 bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */ in dbExtendFS()
3437 bmp->db_agfree[n] = 0; in dbExtendFS()
3443 bmp->db_maxag = bmp->db_maxag / k; in dbExtendFS()
3456 jfs_error(ipbmap->i_sb, "L2 page could not be read\n"); in dbExtendFS()
3457 return -EIO; in dbExtendFS()
3459 l2dcp = (struct dmapctl *) l2mp->data; in dbExtendFS()
3463 l2leaf = l2dcp->stree + CTLLEAFIND + k; in dbExtendFS()
3464 p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */ in dbExtendFS()
3472 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */ in dbExtendFS()
3476 l1dcp = (struct dmapctl *) l1mp->data; in dbExtendFS()
3479 j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE; in dbExtendFS()
3480 l1leaf = l1dcp->stree + CTLLEAFIND + j; in dbExtendFS()
3481 p = BLKTOL0(blkno, sbi->l2nbperpage); in dbExtendFS()
3489 l1dcp = (struct dmapctl *) l1mp->data; in dbExtendFS()
3493 l1leaf = l1dcp->stree + CTLLEAFIND; in dbExtendFS()
3503 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */ in dbExtendFS()
3508 l0dcp = (struct dmapctl *) l0mp->data; in dbExtendFS()
3511 i = (blkno & (MAXL0SIZE - 1)) >> in dbExtendFS()
3513 l0leaf = l0dcp->stree + CTLLEAFIND + i; in dbExtendFS()
3515 sbi->l2nbperpage); in dbExtendFS()
3523 l0dcp = (struct dmapctl *) l0mp->data; in dbExtendFS()
3527 l0leaf = l0dcp->stree + CTLLEAFIND; in dbExtendFS()
3539 if ((n = blkno & (BPERDMAP - 1))) { in dbExtendFS()
3545 n = min(nblocks, (s64)BPERDMAP - n); in dbExtendFS()
3556 dp = (struct dmap *) mp->data; in dbExtendFS()
3559 bmp->db_nfree += n; in dbExtendFS()
3560 agno = le64_to_cpu(dp->start) >> l2agsize; in dbExtendFS()
3561 bmp->db_agfree[agno] += n; in dbExtendFS()
3569 nblocks -= n; in dbExtendFS()
3590 bmp->db_maxfreebud = *l1leaf; in dbExtendFS()
3614 bmp->db_maxfreebud = *l2leaf; in dbExtendFS()
3621 jfs_error(ipbmap->i_sb, "function has not returned as expected\n"); in dbExtendFS()
3628 return -EIO; in dbExtendFS()
3644 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbFinalizeBmap()
3659 actags = bmp->db_maxag + 1; in dbFinalizeBmap()
3660 inactags = bmp->db_numag - actags; in dbFinalizeBmap()
3661 ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */ in dbFinalizeBmap()
3669 ((inactags - 1) << bmp->db_agl2size) + ag_rem in dbFinalizeBmap()
3670 : inactags << bmp->db_agl2size; in dbFinalizeBmap()
3676 actfree = bmp->db_nfree - inactfree; in dbFinalizeBmap()
3680 * re-establish the preferred group as the leftmost in dbFinalizeBmap()
3683 if (bmp->db_agfree[bmp->db_agpref] < avgfree) { in dbFinalizeBmap()
3684 for (bmp->db_agpref = 0; bmp->db_agpref < actags; in dbFinalizeBmap()
3685 bmp->db_agpref++) { in dbFinalizeBmap()
3686 if (bmp->db_agfree[bmp->db_agpref] >= avgfree) in dbFinalizeBmap()
3689 if (bmp->db_agpref >= bmp->db_numag) { in dbFinalizeBmap()
3690 jfs_error(ipbmap->i_sb, in dbFinalizeBmap()
3702 bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize); in dbFinalizeBmap()
3704 bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL); in dbFinalizeBmap()
3705 bmp->db_agheight = l2nl >> 1; in dbFinalizeBmap()
3706 bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1)); in dbFinalizeBmap()
3707 for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0; in dbFinalizeBmap()
3708 i--) { in dbFinalizeBmap()
3709 bmp->db_agstart += n; in dbFinalizeBmap()
3717 * NAME: dbInitDmap()/ujfs_idmap_page()
3728 * dp - pointer to page of map
3729 * nblocks - number of blocks this page
3738 blkno = Blkno & (BPERDMAP - 1); in dbInitDmap()
3741 dp->nblocks = dp->nfree = cpu_to_le32(nblocks); in dbInitDmap()
3742 dp->start = cpu_to_le64(Blkno); in dbInitDmap()
3745 memset(&dp->wmap[0], 0, LPERDMAP * 4); in dbInitDmap()
3746 memset(&dp->pmap[0], 0, LPERDMAP * 4); in dbInitDmap()
3750 le32_add_cpu(&dp->nblocks, nblocks); in dbInitDmap()
3751 le32_add_cpu(&dp->nfree, nblocks); in dbInitDmap()
3762 for (r = nblocks; r > 0; r -= nb, blkno += nb) { in dbInitDmap()
3764 b = blkno & (DBWORD - 1); in dbInitDmap()
3766 nb = min(r, DBWORD - b); in dbInitDmap()
3771 dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) in dbInitDmap()
3773 dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) in dbInitDmap()
3781 memset(&dp->wmap[w], 0, nw * 4); in dbInitDmap()
3782 memset(&dp->pmap[w], 0, nw * 4); in dbInitDmap()
3791 * mark bits following the range to be freed (non-existing in dbInitDmap()
3801 /* does nblocks fall on a 32-bit boundary ? */ in dbInitDmap()
3802 b = blkno & (DBWORD - 1); in dbInitDmap()
3805 dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b); in dbInitDmap()
3811 dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES); in dbInitDmap()
3822 * NAME: dbInitDmapTree()/ujfs_complete_dmap()
3829 * dp - dmap to complete
3830 * blkno - starting block number for this dmap
3831 * treemax - will be filled in with max free for this dmap
3842 tp = &dp->tree; in dbInitDmapTree()
3843 tp->nleafs = cpu_to_le32(LPERDMAP); in dbInitDmapTree()
3844 tp->l2nleafs = cpu_to_le32(L2LPERDMAP); in dbInitDmapTree()
3845 tp->leafidx = cpu_to_le32(LEAFIND); in dbInitDmapTree()
3846 tp->height = cpu_to_le32(4); in dbInitDmapTree()
3847 tp->budmin = BUDMIN; in dbInitDmapTree()
3850 * note: leaf is set to NOFREE(-1) if all blocks of corresponding in dbInitDmapTree()
3853 cp = tp->stree + le32_to_cpu(tp->leafidx); in dbInitDmapTree()
3855 *cp++ = dbMaxBud((u8 *) & dp->wmap[i]); in dbInitDmapTree()
3863 * NAME: dbInitTree()/ujfs_adjtree()
3874 * cp - Pointer to the root of the tree
3875 * l2leaves- Number of leaf nodes as a power of 2
3876 * l2min - Number of blocks that can be covered by a leaf
3887 tp = dtp->stree; in dbInitTree()
3890 l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin; in dbInitTree()
3898 * the combination will result in the left-most buddy leaf having in dbInitTree()
3906 for (l2free = dtp->budmin, bsize = 1; l2free < l2max; in dbInitTree()
3912 for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx); in dbInitTree()
3913 i < le32_to_cpu(dtp->nleafs); in dbInitTree()
3918 *(cp + bsize) = -1; /* right give left */ in dbInitTree()
3933 for (child = le32_to_cpu(dtp->leafidx), in dbInitTree()
3934 nparent = le32_to_cpu(dtp->nleafs) >> 2; in dbInitTree()
3937 parent = (child - 1) >> 2; in dbInitTree()
3960 dcp->nleafs = cpu_to_le32(LPERCTL); in dbInitDmapCtl()
3961 dcp->l2nleafs = cpu_to_le32(L2LPERCTL); in dbInitDmapCtl()
3962 dcp->leafidx = cpu_to_le32(CTLLEAFIND); in dbInitDmapCtl()
3963 dcp->height = cpu_to_le32(5); in dbInitDmapCtl()
3964 dcp->budmin = L2BPERDMAP + L2LPERCTL * level; in dbInitDmapCtl()
3971 cp = &dcp->stree[CTLLEAFIND + i]; in dbInitDmapCtl()
3981 * NAME: dbGetL2AGSize()/ujfs_getagl2size()
3986 * nblocks - Number of blocks in aggregate
4000 m = ((u64) 1 << (64 - 1)); in dbGetL2AGSize()
4001 for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) { in dbGetL2AGSize()
4011 return (l2sz - L2MAXAG); in dbGetL2AGSize()
4016 * NAME: dbMapFileSizeToMapSize()
4018 * FUNCTION: compute number of blocks the block allocation map file
4019 * can cover from the map file size;
4021 * RETURNS: Number of blocks which can be covered by this block map file;
4025 * maximum number of map pages at each level including control pages
4031 * convert number of map pages to the zero origin top dmapctl level
4039 struct super_block *sb = ipbmap->i_sb; in dbMapFileSizeToMapSize()
4045 nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize; in dbMapFileSizeToMapSize()
4046 npages = nblocks >> JFS_SBI(sb)->l2nbperpage; in dbMapFileSizeToMapSize()
4054 npages--; /* skip the first global control page */ in dbMapFileSizeToMapSize()
4055 /* skip higher level control pages above top level covered by map */ in dbMapFileSizeToMapSize()
4056 npages -= (2 - level); in dbMapFileSizeToMapSize()
4057 npages--; /* skip top level's control page */ in dbMapFileSizeToMapSize()
4058 for (i = level; i >= 0; i--) { in dbMapFileSizeToMapSize()
4068 npages--; in dbMapFileSizeToMapSize()