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 || 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()
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()
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()
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
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()
1732 jfs_error(bmp->db_ipbmap->i_sb, in dbFindCtl()
1734 return -EIO; in dbFindCtl()
1736 return -ENOSPC; in dbFindCtl()
1777 * group whose size is equal to the number of blocks per dmap.
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
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()
1837 assert((blkno & (BPERDMAP - 1)) == 0); in dbAllocCtl()
1841 for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) { in dbAllocCtl()
1844 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); in dbAllocCtl()
1845 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1847 rc = -EIO; in dbAllocCtl()
1850 dp = (struct dmap *) mp->data; in dbAllocCtl()
1854 if (dp->tree.stree[ROOT] != L2BPERDMAP) { in dbAllocCtl()
1856 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocCtl()
1858 rc = -EIO; in dbAllocCtl()
1893 for (n = nblocks - n, b = blkno; n > 0; in dbAllocCtl()
1894 n -= BPERDMAP, b += BPERDMAP) { in dbAllocCtl()
1897 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); in dbAllocCtl()
1898 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAllocCtl()
1903 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocCtl()
1907 dp = (struct dmap *) mp->data; in dbAllocCtl()
1916 jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n"); in dbAllocCtl()
1940 * mp - pointer to bmap descriptor
1941 * dp - pointer to dmap to attempt to allocate blocks from.
1942 * l2nb - log2 number of contiguous block desired.
1943 * nblocks - actual number of contiguous block desired.
1944 * results - on successful return, set to the starting block number
1948 * 0 - success
1949 * -ENOSPC - insufficient disk resources
1950 * -EIO - i/o error
1969 if (dbFindLeaf((dmtree_t *) &dp->tree, l2nb, &leafidx, false)) in dbAllocDmapLev()
1970 return -ENOSPC; in dbAllocDmapLev()
1973 return -EIO; in dbAllocDmapLev()
1978 blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD); in dbAllocDmapLev()
1980 /* if not all bits of the dmap word are free, get the starting in dbAllocDmapLev()
1981 * bit number within the dmap word of the required string of free in dbAllocDmapLev()
1982 * bits and adjust the block number with this value. in dbAllocDmapLev()
1984 if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN) in dbAllocDmapLev()
1985 blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb); in dbAllocDmapLev()
2011 * bmp - pointer to bmap descriptor
2012 * dp - pointer to dmap to allocate the block range from.
2013 * blkno - starting block number of the block to be allocated.
2014 * nblocks - number of blocks to be allocated.
2017 * 0 - success
2018 * -EIO - i/o error
2031 oldroot = dp->tree.stree[ROOT]; in dbAllocDmap()
2033 /* allocate the specified (blocks) bits */ in dbAllocDmap()
2037 if (dp->tree.stree[ROOT] == oldroot) in dbAllocDmap()
2044 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0))) in dbAllocDmap()
2066 * bmp - pointer to bmap descriptor
2067 * dp - pointer to dmap to free the block range from.
2068 * blkno - starting block number of the block to be freed.
2069 * nblocks - number of blocks to be freed.
2072 * 0 - success
2073 * -EIO - i/o error
2081 int rc = 0, word; in dbFreeDmap() local
2086 oldroot = dp->tree.stree[ROOT]; in dbFreeDmap()
2088 /* free the specified (blocks) bits */ in dbFreeDmap()
2092 if (rc || (dp->tree.stree[ROOT] == oldroot)) in dbFreeDmap()
2099 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) { in dbFreeDmap()
2100 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD; in dbFreeDmap()
2107 if (dp->tree.stree[word] == NOFREE) in dbFreeDmap()
2108 dbBackSplit((dmtree_t *)&dp->tree, word, false); in dbFreeDmap()
2124 * updates the bits of the working map and causes the adjustment
2126 * leaves to reflect the bits allocated. it also causes the
2130 * bmp - pointer to bmap descriptor
2131 * dp - pointer to dmap to allocate bits from.
2132 * blkno - starting block number of the bits to be allocated.
2133 * nblocks - number of bits to be allocated.
2142 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno; in dbAllocBits() local
2143 dmtree_t *tp = (dmtree_t *) & dp->tree; in dbAllocBits()
2148 leaf = dp->tree.stree + LEAFIND; in dbAllocBits()
2150 /* determine the bit number and word within the dmap of the in dbAllocBits()
2153 dbitno = blkno & (BPERDMAP - 1); in dbAllocBits()
2154 word = dbitno >> L2DBWORD; in dbAllocBits()
2159 /* allocate the bits of the dmap's words corresponding to the block in dbAllocBits()
2160 * range. not all bits of the first and last words may be contained in dbAllocBits()
2163 * (a single pass), allocating the bits of interest by hand and in dbAllocBits()
2164 * updating the leaf corresponding to the dmap word. a single pass in dbAllocBits()
2166 * specified range. within this pass, the bits of all fully contained in dbAllocBits()
2172 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocBits()
2173 /* determine the bit number within the word and in dbAllocBits()
2174 * the number of bits within the word. in dbAllocBits()
2176 wbitno = dbitno & (DBWORD - 1); in dbAllocBits()
2177 nb = min(rembits, DBWORD - wbitno); in dbAllocBits()
2179 /* check if only part of a word is to be allocated. in dbAllocBits()
2182 /* allocate (set to 1) the appropriate bits within in dbAllocBits()
2183 * this dmap word. in dbAllocBits()
2185 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) in dbAllocBits()
2188 /* update the leaf for this dmap word. in addition in dbAllocBits()
2189 * to setting the leaf value to the binary buddy max in dbAllocBits()
2190 * of the updated dmap word, dbSplit() will split in dbAllocBits()
2193 dbSplit(tp, word, BUDMIN, in dbAllocBits()
2194 dbMaxBud((u8 *)&dp->wmap[word]), false); in dbAllocBits()
2196 word += 1; in dbAllocBits()
2200 * words and allocate (set to 1) the bits of these in dbAllocBits()
2204 memset(&dp->wmap[word], (int) ONES, nwords * 4); in dbAllocBits()
2206 /* determine how many bits. in dbAllocBits()
2213 for (; nwords > 0; nwords -= nw) { in dbAllocBits()
2214 if (leaf[word] < BUDMIN) { in dbAllocBits()
2215 jfs_error(bmp->db_ipbmap->i_sb, in dbAllocBits()
2222 * of bits being allocated and the l2 number in dbAllocBits()
2223 * of bits currently described by this leaf. in dbAllocBits()
2225 size = min_t(int, leaf[word], in dbAllocBits()
2234 dbSplit(tp, word, size, NOFREE, false); in dbAllocBits()
2238 word += nw; in dbAllocBits()
2244 le32_add_cpu(&dp->nfree, -nblocks); in dbAllocBits()
2250 * group is the new max. in dbAllocBits()
2252 agno = blkno >> bmp->db_agl2size; in dbAllocBits()
2253 if (agno > bmp->db_maxag) in dbAllocBits()
2254 bmp->db_maxag = agno; in dbAllocBits()
2257 bmp->db_agfree[agno] -= nblocks; in dbAllocBits()
2258 bmp->db_nfree -= nblocks; in dbAllocBits()
2271 * updates the bits of the working map and causes the adjustment
2273 * leaves to reflect the bits freed. it also causes the dmap's
2277 * bmp - pointer to bmap descriptor
2278 * dp - pointer to dmap to free bits from.
2279 * blkno - starting block number of the bits to be freed.
2280 * nblocks - number of bits to be freed.
2289 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno; in dbFreeBits() local
2290 dmtree_t *tp = (dmtree_t *) & dp->tree; in dbFreeBits()
2294 /* determine the bit number and word within the dmap of the in dbFreeBits()
2297 dbitno = blkno & (BPERDMAP - 1); in dbFreeBits()
2298 word = dbitno >> L2DBWORD; in dbFreeBits()
2304 /* free the bits of the dmaps words corresponding to the block range. in dbFreeBits()
2305 * not all bits of the first and last words may be contained within in dbFreeBits()
2308 * (a single pass), freeing the bits of interest by hand and updating in dbFreeBits()
2309 * the leaf corresponding to the dmap word. a single pass will be used in dbFreeBits()
2311 * within this pass, the bits of all fully contained dmap words will in dbFreeBits()
2321 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbFreeBits()
2322 /* determine the bit number within the word and in dbFreeBits()
2323 * the number of bits within the word. in dbFreeBits()
2325 wbitno = dbitno & (DBWORD - 1); in dbFreeBits()
2326 nb = min(rembits, DBWORD - wbitno); in dbFreeBits()
2328 /* check if only part of a word is to be freed. in dbFreeBits()
2331 /* free (zero) the appropriate bits within this in dbFreeBits()
2332 * dmap word. in dbFreeBits()
2334 dp->wmap[word] &= in dbFreeBits()
2335 cpu_to_le32(~(ONES << (DBWORD - nb) in dbFreeBits()
2338 /* update the leaf for this dmap word. in dbFreeBits()
2340 rc = dbJoin(tp, word, in dbFreeBits()
2341 dbMaxBud((u8 *)&dp->wmap[word]), false); in dbFreeBits()
2345 word += 1; in dbFreeBits()
2349 * words and free (zero) the bits of these words. in dbFreeBits()
2352 memset(&dp->wmap[word], 0, nwords * 4); in dbFreeBits()
2354 /* determine how many bits. in dbFreeBits()
2361 for (; nwords > 0; nwords -= nw) { in dbFreeBits()
2364 * of bits being freed and the l2 (max) number in dbFreeBits()
2365 * of bits that can be described by this leaf. in dbFreeBits()
2369 (word, L2LPERDMAP, BUDMIN), in dbFreeBits()
2374 rc = dbJoin(tp, word, size, false); in dbFreeBits()
2381 word += nw; in dbFreeBits()
2388 le32_add_cpu(&dp->nfree, nblocks); in dbFreeBits()
2395 agno = blkno >> bmp->db_agl2size; in dbFreeBits()
2396 bmp->db_nfree += nblocks; in dbFreeBits()
2397 bmp->db_agfree[agno] += nblocks; in dbFreeBits()
2404 if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) || in dbFreeBits()
2405 (agno == bmp->db_numag - 1 && in dbFreeBits()
2406 bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) { in dbFreeBits()
2407 while (bmp->db_maxag > 0) { in dbFreeBits()
2408 bmp->db_maxag -= 1; in dbFreeBits()
2409 if (bmp->db_agfree[bmp->db_maxag] != in dbFreeBits()
2410 bmp->db_agsize) in dbFreeBits()
2414 /* re-establish the allocation group preference if the in dbFreeBits()
2418 if (bmp->db_agpref > bmp->db_maxag) in dbFreeBits()
2419 bmp->db_agpref = bmp->db_maxag; in dbFreeBits()
2453 * bmp - pointer to bmap descriptor
2454 * blkno - the first block of a block range within a dmap. it is
2457 * newval - the new value of the lower level dmap or dmap control
2459 * alloc - 'true' if adjustment is due to an allocation.
2460 * level - current level of dmap control page (i.e. L0, L1, L2) to
2464 * 0 - success
2465 * -EIO - i/o error
2482 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level); in dbAdjCtl()
2483 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); in dbAdjCtl()
2485 return -EIO; in dbAdjCtl()
2486 dcp = (struct dmapctl *) mp->data; in dbAdjCtl()
2488 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) { in dbAdjCtl()
2489 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n"); in dbAdjCtl()
2491 return -EIO; in dbAdjCtl()
2497 leafno = BLKTOCTLLEAF(blkno, dcp->budmin); in dbAdjCtl()
2498 ti = leafno + le32_to_cpu(dcp->leafidx); in dbAdjCtl()
2503 oldval = dcp->stree[ti]; in dbAdjCtl()
2504 oldroot = dcp->stree[ROOT]; in dbAdjCtl()
2531 oldval = dcp->stree[ti]; in dbAdjCtl()
2533 dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval, true); in dbAdjCtl()
2549 if (dcp->stree[ROOT] != oldroot) { in dbAdjCtl()
2553 if (level < bmp->db_maxlevel) { in dbAdjCtl()
2558 dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc, in dbAdjCtl()
2574 if (dcp->stree[ti] == NOFREE) in dbAdjCtl()
2578 dcp->budmin, oldval, true); in dbAdjCtl()
2591 assert(level == bmp->db_maxlevel); in dbAdjCtl()
2592 if (bmp->db_maxfreebud != oldroot) { in dbAdjCtl()
2593 jfs_error(bmp->db_ipbmap->i_sb, in dbAdjCtl()
2596 bmp->db_maxfreebud = dcp->stree[ROOT]; in dbAdjCtl()
2616 * tp - pointer to the tree containing the leaf.
2617 * leafno - the number of the leaf to be updated.
2618 * splitsz - the size the binary buddy system starting at the leaf
2620 * newval - the new value for the leaf.
2630 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbSplit()
2634 if (leaf[leafno] > tp->dmt_budmin) { in dbSplit()
2638 * - 1 in l2) and the corresponding buddy size. in dbSplit()
2640 cursz = leaf[leafno] - 1; in dbSplit()
2641 budsz = BUDSIZE(cursz, tp->dmt_budmin); in dbSplit()
2652 cursz -= 1; in dbSplit()
2684 * tp - pointer to the tree containing the leaf.
2685 * leafno - the number of the leaf to be updated.
2695 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbBackSplit()
2710 LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs), in dbBackSplit()
2711 tp->dmt_budmin); in dbBackSplit()
2717 budsz = BUDSIZE(size, tp->dmt_budmin); in dbBackSplit()
2726 if (bsz >= le32_to_cpu(tp->dmt_nleafs)) { in dbBackSplit()
2728 return -EIO; in dbBackSplit()
2741 cursz = leaf[bud] - 1; in dbBackSplit()
2750 return -EIO; in dbBackSplit()
2760 * the leaf with other leaves of the dmtree into a multi-leaf
2764 * tp - pointer to the tree containing the leaf.
2765 * leafno - the number of the leaf to be updated.
2766 * newval - the new value for the leaf.
2777 if (newval >= tp->dmt_budmin) { in dbJoin()
2780 leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); in dbJoin()
2795 budsz = BUDSIZE(newval, tp->dmt_budmin); in dbJoin()
2799 while (budsz < le32_to_cpu(tp->dmt_nleafs)) { in dbJoin()
2812 return -EIO; in dbJoin()
2858 * tp - pointer to the tree to be adjusted.
2859 * leafno - the number of the leaf to be updated.
2860 * newval - the new value for the leaf.
2867 int max, size; in dbAdjTree() local
2873 lp = leafno + le32_to_cpu(tp->dmt_leafidx); in dbAdjTree()
2881 if (tp->dmt_stree[lp] == newval) in dbAdjTree()
2886 tp->dmt_stree[lp] = newval; in dbAdjTree()
2890 for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) { in dbAdjTree()
2894 lp = ((lp - 1) & ~0x03) + 1; in dbAdjTree()
2898 pp = (lp - 1) >> 2; in dbAdjTree()
2902 max = TREEMAX(&tp->dmt_stree[lp]); in dbAdjTree()
2907 if (tp->dmt_stree[pp] == max) in dbAdjTree()
2912 tp->dmt_stree[pp] = max; in dbAdjTree()
2914 /* parent becomes leaf for next go-round. in dbAdjTree()
2933 * tp - pointer to the tree to be searched.
2934 * l2nb - log2 number of free blocks to search for.
2935 * leafidx - return pointer to be set to the index of the leaf
2938 * is_ctl - determines if the tree is of type ctl
2941 * 0 - success
2942 * -ENOSPC - insufficient free blocks.
2955 if (l2nb > tp->dmt_stree[ROOT]) in dbFindLeaf()
2956 return -ENOSPC; in dbFindLeaf()
2962 for (k = le32_to_cpu(tp->dmt_height), ti = 1; in dbFindLeaf()
2963 k > 0; k--, ti = ((ti + n) << 2) + 1) { in dbFindLeaf()
2972 return -ENOSPC; in dbFindLeaf()
2973 if (l2nb <= tp->dmt_stree[x + n]) in dbFindLeaf()
2982 if (le32_to_cpu(tp->dmt_leafidx) >= max_idx) in dbFindLeaf()
2983 return -ENOSPC; in dbFindLeaf()
2988 *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx); in dbFindLeaf()
2997 * FUNCTION: find a specified number of binary buddy free bits within a
2998 * dmap bitmap word value.
3001 * bits at (1 << l2nb) alignments within the value.
3004 * word - dmap bitmap word value.
3005 * l2nb - number of free bits specified as a log2 number.
3008 * starting bit number of free bits.
3010 static int dbFindBits(u32 word, int l2nb) in dbFindBits() argument
3015 /* get the number of bits. in dbFindBits()
3020 /* complement the word so we can use a mask (i.e. 0s represent in dbFindBits()
3021 * free bits) and compute the mask. in dbFindBits()
3023 word = ~word; in dbFindBits()
3024 mask = ONES << (DBWORD - nb); in dbFindBits()
3026 /* scan the word for nb free bits at nb alignments. in dbFindBits()
3029 if ((mask & word) == mask) in dbFindBits()
3045 * bits within 32-bits of the map.
3048 * cp - pointer to the 32-bit value.
3051 * largest binary buddy of free bits within a dmap word.
3057 /* check if the wmap word is all free. if so, the in dbMaxBud()
3063 /* check if the wmap word is half free. if so, the in dbMaxBud()
3064 * free buddy size is BUDMIN-1. in dbMaxBud()
3067 return (BUDMIN - 1); in dbMaxBud()
3070 * size thru table lookup using quarters of the wmap word. in dbMaxBud()
3072 tmp1 = max(budtab[cp[2]], budtab[cp[3]]); in dbMaxBud()
3073 tmp2 = max(budtab[cp[0]], budtab[cp[1]]); in dbMaxBud()
3074 return (max(tmp1, tmp2)); in dbMaxBud()
3079 * NAME: cnttz(uint word)
3081 * FUNCTION: determine the number of trailing zeros within a 32-bit
3085 * value - 32-bit value to be examined.
3090 static int cnttz(u32 word) in cnttz() argument
3094 for (n = 0; n < 32; n++, word >>= 1) { in cnttz()
3095 if (word & 0x01) in cnttz()
3106 * FUNCTION: determine the number of leading zeros within a 32-bit
3110 * value - 32-bit value to be examined.
3135 * nb - number of blocks
3145 mask = (s64) 1 << (64 - 1); in blkstol2()
3147 /* count the leading bits. in blkstol2()
3155 l2nb = (64 - 1) - l2nb; in blkstol2()
3180 * ip - pointer to in-core inode;
3181 * blkno - starting block number to be freed.
3182 * nblocks - number of blocks to be freed.
3185 * 0 - success
3186 * -EIO - i/o error
3194 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; in dbAllocBottomUp()
3195 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; in dbAllocBottomUp()
3200 ASSERT(nblocks <= bmp->db_mapsize - blkno); in dbAllocBottomUp()
3206 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { in dbAllocBottomUp()
3213 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); in dbAllocBottomUp()
3217 return -EIO; in dbAllocBottomUp()
3219 dp = (struct dmap *) mp->data; in dbAllocBottomUp()
3224 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1))); in dbAllocBottomUp()
3247 int dbitno, word, rembits, nb, nwords, wbitno, agno; in dbAllocDmapBU() local
3249 struct dmaptree *tp = (struct dmaptree *) & dp->tree; in dbAllocDmapBU()
3254 oldroot = tp->stree[ROOT]; in dbAllocDmapBU()
3256 /* determine the bit number and word within the dmap of the in dbAllocDmapBU()
3259 dbitno = blkno & (BPERDMAP - 1); in dbAllocDmapBU()
3260 word = dbitno >> L2DBWORD; in dbAllocDmapBU()
3265 /* allocate the bits of the dmap's words corresponding to the block in dbAllocDmapBU()
3266 * range. not all bits of the first and last words may be contained in dbAllocDmapBU()
3269 * (a single pass), allocating the bits of interest by hand and in dbAllocDmapBU()
3270 * updating the leaf corresponding to the dmap word. a single pass in dbAllocDmapBU()
3272 * specified range. within this pass, the bits of all fully contained in dbAllocDmapBU()
3278 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { in dbAllocDmapBU()
3279 /* determine the bit number within the word and in dbAllocDmapBU()
3280 * the number of bits within the word. in dbAllocDmapBU()
3282 wbitno = dbitno & (DBWORD - 1); in dbAllocDmapBU()
3283 nb = min(rembits, DBWORD - wbitno); in dbAllocDmapBU()
3285 /* check if only part of a word is to be allocated. in dbAllocDmapBU()
3288 /* allocate (set to 1) the appropriate bits within in dbAllocDmapBU()
3289 * this dmap word. in dbAllocDmapBU()
3291 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) in dbAllocDmapBU()
3294 word++; in dbAllocDmapBU()
3298 * words and allocate (set to 1) the bits of these in dbAllocDmapBU()
3302 memset(&dp->wmap[word], (int) ONES, nwords * 4); in dbAllocDmapBU()
3304 /* determine how many bits */ in dbAllocDmapBU()
3306 word += nwords; in dbAllocDmapBU()
3311 le32_add_cpu(&dp->nfree, -nblocks); in dbAllocDmapBU()
3320 * if this allocation group is the new max. in dbAllocDmapBU()
3322 agno = blkno >> bmp->db_agl2size; in dbAllocDmapBU()
3323 if (agno > bmp->db_maxag) in dbAllocDmapBU()
3324 bmp->db_maxag = agno; in dbAllocDmapBU()
3327 bmp->db_agfree[agno] -= nblocks; in dbAllocDmapBU()
3328 bmp->db_nfree -= nblocks; in dbAllocDmapBU()
3333 if (tp->stree[ROOT] == oldroot) in dbAllocDmapBU()
3340 if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0))) in dbAllocDmapBU()
3355 * L1---------------------------------L1
3357 * L0---------L0---------L0 L0---------L0---------L0
3362 * <---old---><----------------------------extend----------------------->
3366 struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb); in dbExtendFS()
3367 int nbperpage = sbi->nbperpage; in dbExtendFS()
3375 struct bmap *bmp = sbi->bmap; in dbExtendFS()
3392 bmp->db_mapsize = newsize; in dbExtendFS()
3393 bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize); in dbExtendFS()
3397 oldl2agsize = bmp->db_agl2size; in dbExtendFS()
3399 bmp->db_agl2size = l2agsize; in dbExtendFS()
3400 bmp->db_agsize = 1 << l2agsize; in dbExtendFS()
3403 agno = bmp->db_numag; in dbExtendFS()
3404 bmp->db_numag = newsize >> l2agsize; in dbExtendFS()
3405 bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0; in dbExtendFS()
3412 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn; in dbExtendFS()
3417 k = 1 << (l2agsize - oldl2agsize); in dbExtendFS()
3418 ag_rem = bmp->db_agfree[0]; /* save agfree[0] */ in dbExtendFS()
3420 bmp->db_agfree[n] = 0; /* init collection point */ in dbExtendFS()
3425 bmp->db_agfree[n] += bmp->db_agfree[i]; in dbExtendFS()
3428 bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */ in dbExtendFS()
3431 bmp->db_agfree[n] = 0; in dbExtendFS()
3437 bmp->db_maxag = bmp->db_maxag / k; in dbExtendFS()
3450 jfs_error(ipbmap->i_sb, "L2 page could not be read\n"); in dbExtendFS()
3451 return -EIO; in dbExtendFS()
3453 l2dcp = (struct dmapctl *) l2mp->data; in dbExtendFS()
3457 l2leaf = l2dcp->stree + CTLLEAFIND + k; in dbExtendFS()
3458 p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */ in dbExtendFS()
3466 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */ in dbExtendFS()
3470 l1dcp = (struct dmapctl *) l1mp->data; in dbExtendFS()
3473 j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE; in dbExtendFS()
3474 l1leaf = l1dcp->stree + CTLLEAFIND + j; in dbExtendFS()
3475 p = BLKTOL0(blkno, sbi->l2nbperpage); in dbExtendFS()
3483 l1dcp = (struct dmapctl *) l1mp->data; in dbExtendFS()
3487 l1leaf = l1dcp->stree + CTLLEAFIND; in dbExtendFS()
3497 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */ in dbExtendFS()
3502 l0dcp = (struct dmapctl *) l0mp->data; in dbExtendFS()
3505 i = (blkno & (MAXL0SIZE - 1)) >> in dbExtendFS()
3507 l0leaf = l0dcp->stree + CTLLEAFIND + i; in dbExtendFS()
3509 sbi->l2nbperpage); in dbExtendFS()
3517 l0dcp = (struct dmapctl *) l0mp->data; in dbExtendFS()
3521 l0leaf = l0dcp->stree + CTLLEAFIND; in dbExtendFS()
3533 if ((n = blkno & (BPERDMAP - 1))) { in dbExtendFS()
3539 n = min(nblocks, (s64)BPERDMAP - n); in dbExtendFS()
3550 dp = (struct dmap *) mp->data; in dbExtendFS()
3553 bmp->db_nfree += n; in dbExtendFS()
3554 agno = le64_to_cpu(dp->start) >> l2agsize; in dbExtendFS()
3555 bmp->db_agfree[agno] += n; in dbExtendFS()
3563 nblocks -= n; in dbExtendFS()
3584 bmp->db_maxfreebud = *l1leaf; in dbExtendFS()
3608 bmp->db_maxfreebud = *l2leaf; in dbExtendFS()
3615 jfs_error(ipbmap->i_sb, "function has not returned as expected\n"); in dbExtendFS()
3622 return -EIO; in dbExtendFS()
3638 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; in dbFinalizeBmap()
3653 actags = bmp->db_maxag + 1; in dbFinalizeBmap()
3654 inactags = bmp->db_numag - actags; in dbFinalizeBmap()
3655 ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */ in dbFinalizeBmap()
3663 ((inactags - 1) << bmp->db_agl2size) + ag_rem in dbFinalizeBmap()
3664 : inactags << bmp->db_agl2size; in dbFinalizeBmap()
3670 actfree = bmp->db_nfree - inactfree; in dbFinalizeBmap()
3674 * re-establish the preferred group as the leftmost in dbFinalizeBmap()
3677 if (bmp->db_agfree[bmp->db_agpref] < avgfree) { in dbFinalizeBmap()
3678 for (bmp->db_agpref = 0; bmp->db_agpref < actags; in dbFinalizeBmap()
3679 bmp->db_agpref++) { in dbFinalizeBmap()
3680 if (bmp->db_agfree[bmp->db_agpref] >= avgfree) in dbFinalizeBmap()
3683 if (bmp->db_agpref >= bmp->db_numag) { in dbFinalizeBmap()
3684 jfs_error(ipbmap->i_sb, in dbFinalizeBmap()
3696 bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize); in dbFinalizeBmap()
3698 bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL); in dbFinalizeBmap()
3699 bmp->db_agheight = l2nl >> 1; in dbFinalizeBmap()
3700 bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1)); in dbFinalizeBmap()
3701 for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0; in dbFinalizeBmap()
3702 i--) { in dbFinalizeBmap()
3703 bmp->db_agstart += n; in dbFinalizeBmap()
3722 * dp - pointer to page of map
3723 * nblocks - number of blocks this page
3732 blkno = Blkno & (BPERDMAP - 1); in dbInitDmap()
3735 dp->nblocks = dp->nfree = cpu_to_le32(nblocks); in dbInitDmap()
3736 dp->start = cpu_to_le64(Blkno); in dbInitDmap()
3739 memset(&dp->wmap[0], 0, LPERDMAP * 4); in dbInitDmap()
3740 memset(&dp->pmap[0], 0, LPERDMAP * 4); in dbInitDmap()
3744 le32_add_cpu(&dp->nblocks, nblocks); in dbInitDmap()
3745 le32_add_cpu(&dp->nfree, nblocks); in dbInitDmap()
3748 /* word number containing start block number */ in dbInitDmap()
3752 * free the bits corresponding to the block range (ZEROS): in dbInitDmap()
3753 * note: not all bits of the first and last words may be contained in dbInitDmap()
3756 for (r = nblocks; r > 0; r -= nb, blkno += nb) { in dbInitDmap()
3757 /* number of bits preceding range to be freed in the word */ in dbInitDmap()
3758 b = blkno & (DBWORD - 1); in dbInitDmap()
3759 /* number of bits to free in the word */ in dbInitDmap()
3760 nb = min(r, DBWORD - b); in dbInitDmap()
3762 /* is partial word to be freed ? */ in dbInitDmap()
3764 /* free (set to 0) from the bitmap word */ in dbInitDmap()
3765 dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) in dbInitDmap()
3767 dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) in dbInitDmap()
3770 /* skip the word freed */ in dbInitDmap()
3775 memset(&dp->wmap[w], 0, nw * 4); in dbInitDmap()
3776 memset(&dp->pmap[w], 0, nw * 4); in dbInitDmap()
3785 * mark bits following the range to be freed (non-existing in dbInitDmap()
3792 /* the first word beyond the end of existing blocks */ in dbInitDmap()
3795 /* does nblocks fall on a 32-bit boundary ? */ in dbInitDmap()
3796 b = blkno & (DBWORD - 1); in dbInitDmap()
3798 /* mark a partial word allocated */ in dbInitDmap()
3799 dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b); in dbInitDmap()
3805 dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES); in dbInitDmap()
3823 * dp - dmap to complete
3824 * blkno - starting block number for this dmap
3825 * treemax - will be filled in with max free for this dmap
3827 * RETURNS: max free string at the root of the tree
3836 tp = &dp->tree; in dbInitDmapTree()
3837 tp->nleafs = cpu_to_le32(LPERDMAP); in dbInitDmapTree()
3838 tp->l2nleafs = cpu_to_le32(L2LPERDMAP); in dbInitDmapTree()
3839 tp->leafidx = cpu_to_le32(LEAFIND); in dbInitDmapTree()
3840 tp->height = cpu_to_le32(4); in dbInitDmapTree()
3841 tp->budmin = BUDMIN; in dbInitDmapTree()
3843 /* init each leaf from corresponding wmap word: in dbInitDmapTree()
3844 * note: leaf is set to NOFREE(-1) if all blocks of corresponding in dbInitDmapTree()
3845 * bitmap word are allocated. in dbInitDmapTree()
3847 cp = tp->stree + le32_to_cpu(tp->leafidx); in dbInitDmapTree()
3849 *cp++ = dbMaxBud((u8 *) & dp->wmap[i]); in dbInitDmapTree()
3862 * from corresponding bitmap word or root of summary tree
3868 * cp - Pointer to the root of the tree
3869 * l2leaves- Number of leaf nodes as a power of 2
3870 * l2min - Number of blocks that can be covered by a leaf
3873 * RETURNS: max free string at the root of the tree
3881 tp = dtp->stree; in dbInitTree()
3884 l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin; in dbInitTree()
3892 * the combination will result in the left-most buddy leaf having in dbInitTree()
3900 for (l2free = dtp->budmin, bsize = 1; l2free < l2max; in dbInitTree()
3906 for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx); in dbInitTree()
3907 i < le32_to_cpu(dtp->nleafs); in dbInitTree()
3909 /* coalesce if both adjacent buddies are max free */ in dbInitTree()
3912 *(cp + bsize) = -1; /* right give left */ in dbInitTree()
3927 for (child = le32_to_cpu(dtp->leafidx), in dbInitTree()
3928 nparent = le32_to_cpu(dtp->nleafs) >> 2; in dbInitTree()
3931 parent = (child - 1) >> 2; in dbInitTree()
3954 dcp->nleafs = cpu_to_le32(LPERCTL); in dbInitDmapCtl()
3955 dcp->l2nleafs = cpu_to_le32(L2LPERCTL); in dbInitDmapCtl()
3956 dcp->leafidx = cpu_to_le32(CTLLEAFIND); in dbInitDmapCtl()
3957 dcp->height = cpu_to_le32(5); in dbInitDmapCtl()
3958 dcp->budmin = L2BPERDMAP + L2LPERCTL * level; in dbInitDmapCtl()
3965 cp = &dcp->stree[CTLLEAFIND + i]; in dbInitDmapCtl()
3980 * nblocks - Number of blocks in aggregate
3994 m = ((u64) 1 << (64 - 1)); in dbGetL2AGSize()
3995 for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) { in dbGetL2AGSize()
4005 return (l2sz - L2MAXAG); in dbGetL2AGSize()
4033 struct super_block *sb = ipbmap->i_sb; in dbMapFileSizeToMapSize()
4039 nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize; in dbMapFileSizeToMapSize()
4040 npages = nblocks >> JFS_SBI(sb)->l2nbperpage; in dbMapFileSizeToMapSize()
4048 npages--; /* skip the first global control page */ in dbMapFileSizeToMapSize()
4050 npages -= (2 - level); in dbMapFileSizeToMapSize()
4051 npages--; /* skip top level's control page */ in dbMapFileSizeToMapSize()
4052 for (i = level; i >= 0; i--) { in dbMapFileSizeToMapSize()
4062 npages--; in dbMapFileSizeToMapSize()