/* block to be freed better be within the mapsize. */ if (unlikely((blkno == 0) || (blkno + nblocks > bmp->db_mapsize))) {
IREAD_UNLOCK(ipbmap);
printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
(unsignedlonglong) blkno,
(unsignedlonglong) nblocks);
jfs_error(ip->i_sb, "block to be freed is outside the map\n"); return -EIO;
}
/** *TRIMtheblocks,whenmountedwithdiscardoption
*/ if (JFS_SBI(sb)->flag & JFS_DISCARD) if (JFS_SBI(sb)->minblks_trim <= nblocks)
jfs_issue_discard(ipbmap, blkno, nblocks);
/* *freetheblocksadmapatatime.
*/
mp = NULL; for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { /* release previous dmap if any */ if (mp) {
write_metapage(mp);
}
/* get the buffer for the current dmap. */
lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
mp = read_metapage(ipbmap, lblkno, PSIZE, 0); if (mp == NULL) {
IREAD_UNLOCK(ipbmap); return -EIO;
}
dp = (struct dmap *) mp->data;
/* determine the number of blocks to be freed from *thisdmap.
*/
nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
/* free the blocks. */ if ((rc = dbFreeDmap(bmp, dp, blkno, nb))) {
jfs_error(ip->i_sb, "error in block map\n");
release_metapage(mp);
IREAD_UNLOCK(ipbmap); return (rc);
}
}
/* write the last buffer. */ if (mp)
write_metapage(mp);
/* *updatetheblockstateadmapatatime.
*/
mp = NULL;
lastlblkno = 0; for (rem = nblocks; rem > 0; rem -= nblks, blkno += nblks) { /* get the buffer for the current dmap. */
lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); if (lblkno != lastlblkno) { if (mp) {
write_metapage(mp);
}
/* determine the bit number and word within the dmap of *thestartingblock.alsodeterminehowmanyblocks *aretobeupdatedwithinthisdmap.
*/
dbitno = blkno & (BPERDMAP - 1);
word = dbitno >> L2DBWORD;
nblks = min(rem, (s64)BPERDMAP - dbitno);
/* update the bits of the dmap words. the first and last *wordsmayonlyhaveasubsetoftheirbitsupdated.if *thisisthecase,we'llworkagainstthatword(i.e. *partialfirstand/orlast)onlyinasinglepass.a *singlepasswillalsobeusedtoupdateallwordsthat *aretohavealltheirbitsupdated.
*/ for (rbits = nblks; rbits > 0;
rbits -= nbits, dbitno += nbits) { /* determine the bit number within the word and *thenumberofbitswithintheword.
*/
wbitno = dbitno & (DBWORD - 1);
nbits = min(rbits, DBWORD - wbitno);
/* check if only part of the word is to be updated. */ if (nbits < DBWORD) { /* update (free or allocate) the bits *inthisword.
*/
mask =
(ONES << (DBWORD - nbits) >> wbitno); if (free)
dp->pmap[word] &=
cpu_to_le32(~mask); else
dp->pmap[word] |=
cpu_to_le32(mask);
word += 1;
} else { /* one or more words are to have all *theirbitsupdated.determinehow *manywordsandhowmanybits.
*/
nwords = rbits >> L2DBWORD;
nbits = nwords << L2DBWORD;
/* update (free or allocate) the bits *inthesewords.
*/ if (free)
memset(&dp->pmap[word], 0,
nwords * 4); else
memset(&dp->pmap[word], (int) ONES,
nwords * 4);
word += nwords;
}
}
/* *updatedmaplsn
*/ if (lblkno == lastlblkno) continue;
/* From the last preferred ag, find the next one with at least *averagefreespace.
*/ for (i = 0 ; i < bmp->db_numag; i++, agpref++) { if (agpref >= bmp->db_numag)
agpref = 0;
if (atomic_read(&bmp->db_active[agpref])) /* open file is currently growing in this ag */ continue; if (bmp->db_agfree[agpref] >= avgfree) { /* Return this one */
bmp->db_agpref = agpref; goto unlock;
} elseif (bmp->db_agfree[agpref] > hwm) { /* Less than avg. freespace, but best so far */
hwm = bmp->db_agfree[agpref];
next_best = agpref;
}
}
/* assert that nblocks is valid */
assert(nblocks > 0);
/* get the log2 number of blocks to be allocated. *ifthenumberofblocksisnotalog2multiple, *itwillberoundeduptothenextlog2multiple.
*/
l2nb = BLKSTOL2(nblocks);
bmp = JFS_SBI(ip->i_sb)->bmap;
mapSize = bmp->db_mapsize;
/* the hint should be within the map */ if (hint >= mapSize) {
jfs_error(ip->i_sb, "the hint is outside the map\n"); return -EIO;
}
/* if the number of blocks to be allocated is greater than the *allocationgroupsize,trytoallocateanywhere.
*/ if (l2nb > bmp->db_agl2size) {
IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
rc = dbAllocAny(bmp, nblocks, l2nb, results);
goto write_unlock;
}
/* *Ifnohint,letdbNextAGrecommendanallocationgroup
*/ if (hint == 0) goto pref_ag;
/* we would like to allocate close to the hint. adjust the *hinttotheblockfollowingthehintsincetheallocators *willstartlookingforfreespacestartingatthispoint.
*/
blkno = hint + 1;
if (blkno >= bmp->db_mapsize) goto pref_ag;
agno = blkno >> bmp->db_agl2size;
/* check if blkno crosses over into a new allocation group. *ifso,checkifweshouldallowallocationswithinthis *allocationgroup.
*/ if ((blkno & (bmp->db_agsize - 1)) == 0) /* check if the AG is currently being written to. *ifso,calldbNextAG()tofindanon-busy *AGwithsufficientfreespace.
*/ if (atomic_read(&bmp->db_active[agno])) goto pref_ag;
/* check if the allocation request size can be satisfied from a *singledmap.ifso,trytoallocatefromthedmapcontaining *thehintusingatieredstrategy.
*/ if (nblocks <= BPERDMAP) {
IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
/* get the buffer for the dmap containing the hint.
*/
rc = -EIO;
lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
mp = read_metapage(ipbmap, lblkno, PSIZE, 0); if (mp == NULL) goto read_unlock;
dp = (struct dmap *) mp->data;
/* first, try to satisfy the allocation request with the *blocksbeginningatthehint.
*/ if ((rc = dbAllocNext(bmp, dp, blkno, (int) nblocks))
!= -ENOSPC) { if (rc == 0) {
*results = blkno;
mark_metapage_dirty(mp);
}
/* next, try to satisfy the allocation request with blocks *nearthehint.
*/ if ((rc =
dbAllocNear(bmp, dp, blkno, (int) nblocks, l2nb, results))
!= -ENOSPC) { if (rc == 0)
mark_metapage_dirty(mp);
release_metapage(mp); goto read_unlock;
}
/* try to satisfy the allocation request with blocks within *thesamedmapasthehint.
*/ if ((rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results))
!= -ENOSPC) { if (rc == 0)
mark_metapage_dirty(mp);
release_metapage(mp); goto read_unlock;
}
release_metapage(mp);
IREAD_UNLOCK(ipbmap);
}
/* try to satisfy the allocation request with blocks within *thesameallocationgroupasthehint.
*/
IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP); if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC) goto write_unlock;
/* Try to allocate within this allocation group. if that fails, try to *allocateanywhereinthemap.
*/ if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC)
rc = dbAllocAny(bmp, nblocks, l2nb, results);
/* try to extend the allocation in place.
*/ if ((rc = dbExtend(ip, blkno, nblocks, addnblocks)) == 0) {
*results = blkno; return (0);
} else { if (rc != -ENOSPC) return (rc);
}
/* could not extend the allocation in place, so allocate a *newsetofblocksfortheentirerequest(i.e.trytoget *arangeofcontiguousblockslargeenoughtocoverthe *existingallocationplustheadditionalblocks.)
*/ return (dbAlloc
(ip, blkno + nblocks - 1, addnblocks + nblocks, results));
}
/* get the last block of the current allocation */
lastblkno = blkno + nblocks - 1;
/* determine the block number of the block following *theexistingallocation.
*/
extblkno = lastblkno + 1;
IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
/* better be within the file system */
bmp = sbi->bmap; if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) {
IREAD_UNLOCK(ipbmap);
jfs_error(ip->i_sb, "the block is outside the filesystem\n"); return -EIO;
}
/* we'll attempt to extend the current allocation in place by *allocatingtheadditionalblocksastheblocksimmediately *followingthecurrentallocation.weonlytrytoextendthe *currentallocationinplaceifthenumberofadditionalblocks *canfitintoadmap,thelastblockofthecurrentallocation *isnotthelastblockofthefilesystem,andthestartofthe *inplaceextensionisnotonanallocationgroupboundary.
*/ if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize ||
(extblkno & (bmp->db_agsize - 1)) == 0) {
IREAD_UNLOCK(ipbmap); return -ENOSPC;
}
/* get the buffer for the dmap containing the first block *oftheextension.
*/
lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage);
mp = read_metapage(ipbmap, lblkno, PSIZE, 0); if (mp == NULL) {
IREAD_UNLOCK(ipbmap); return -EIO;
}
dp = (struct dmap *) mp->data;
/* try to allocate the blocks immediately following the *currentallocation.
*/
rc = dbAllocNext(bmp, dp, extblkno, (int) addnblocks);
IREAD_UNLOCK(ipbmap);
/* were we successful ? */ if (rc == 0)
write_metapage(mp); else /* we were not successful */
release_metapage(mp);
/* pick up a pointer to the leaves of the dmap tree.
*/
leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
/* determine the bit number and word within the dmap of the *startingblock.
*/
dbitno = blkno & (BPERDMAP - 1);
word = dbitno >> L2DBWORD;
/* check if the specified block range is contained within *thisdmap.
*/ if (dbitno + nblocks > BPERDMAP) return -ENOSPC;
/* check if the starting leaf indicates that anything *isfree.
*/ if (leaf[word] == NOFREE) return -ENOSPC;
/* check the dmaps words corresponding to block range to see *iftheblockrangeisfree.notallbitsofthefirstand *lastwordsmaybecontainedwithintheblockrange.ifthis *isthecase,we'llworkagainstthosewords(i.e.partialfirst *and/orlast)onanindividualbasis(asinglepass)andexamine *theactualbitstodetermineiftheyarefree.asinglepass *willbeusedforalldmapwordsfullycontainedwithinthe *specifiedrange.withinthispass,theleavesofthedmap *treewillbeexaminedtodetermineiftheblocksarefree.a *singleleafmaydescribethefreespaceofmultipledmap *words,sowemayvisitonlyasubsetoftheactualleaves *correspondingtothedmapwordsoftheblockrange.
*/ for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { /* determine the bit number within the word and *thenumberofbitswithintheword.
*/
wbitno = dbitno & (DBWORD - 1);
nb = min(rembits, DBWORD - wbitno);
/* check if only part of the word is to be examined.
*/ if (nb < DBWORD) { /* check if the bits are free.
*/
mask = (ONES << (DBWORD - nb) >> wbitno); if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask) return -ENOSPC;
word += 1;
} else { /* one or more dmap words are fully contained *withintheblockrange.determinehowmany *wordsandhowmanybits.
*/
nwords = rembits >> L2DBWORD;
nb = nwords << L2DBWORD;
/* now examine the appropriate leaves to determine *iftheblocksarefree.
*/ while (nwords > 0) { /* does the leaf describe any free space ?
*/ if (leaf[word] < BUDMIN) return -ENOSPC;
/* determine the l2 number of bits provided *bythisleaf.
*/
l2size =
min_t(int, leaf[word], NLSTOL2BSZ(nwords));
/* determine how many words were handled.
*/
nw = BUDSIZE(l2size, BUDMIN);
/* determine the word within the dmap that holds the hint *(i.e.blkno).also,determinethelastwordinthedmap *thatwe'llincludeinourexamination.
*/
word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
lword = min(word + 4, LPERDMAP);
/* examine the leaves for sufficient free space.
*/ for (; word < lword; word++) { /* does the leaf describe sufficient free space ?
*/ if (leaf[word] < l2nb) continue;
/* determine the block number within the file system *ofthefirstblockdescribedbythisdmapword.
*/
blkno = le64_to_cpu(dp->start) + (word << L2DBWORD);
/* if not all bits of the dmap word are free, get the *startingbitnumberwithinthedmapwordoftherequired *stringoffreebitsandadjusttheblocknumberwiththe *value.
*/ if (leaf[word] < BUDMIN)
blkno +=
dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb);
/* allocate the blocks.
*/ if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
*results = blkno;
/* allocation request should not be for more than the *allocationgroupsize.
*/ if (l2nb > bmp->db_agl2size) {
jfs_error(bmp->db_ipbmap->i_sb, "allocation request is larger than the allocation group size\n"); return -EIO;
}
/* determine the starting block number of the allocation *group.
*/
blkno = (s64) agno << bmp->db_agl2size;
/* check if the allocation group size is the minimum allocation *groupsizeoriftheallocationgroupiscompletelyfree.if *theallocationgroupsizeistheminimumsizeofBPERDMAP(i.e. *1dmap),thereisnoneedtosearchthedmapcontrolpage(below) *thatfullydescribestheallocationgroupsincetheallocation *groupisalreadyfullydescribedbyadmap.inthiscase,we *justcalldbAllocCtl()tosearchthedmaptreeandallocatethe *requiredspaceifavailable. * *iftheallocationgroupiscompletelyfree,dbAllocCtl()is *alsocalledtoallocatetherequiredspace.thisisdonefor *tworeasons.first,itmakesnosensesearchingthedmapcontrol *pagesforfreespacewhenweknowthatfreespaceexists.second, *thedmapcontrolpagesmayindicatethattheallocationgroup *hasnofreespaceiftheallocationgroupispart(notthefirst *part)ofalargerbinarybuddysystem.
*/ if (bmp->db_agsize == BPERDMAP
|| bmp->db_agfree[agno] == bmp->db_agsize) {
rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results); if ((rc == -ENOSPC) &&
(bmp->db_agfree[agno] == bmp->db_agsize)) {
printk(KERN_ERR "blkno = %Lx, blocks = %Lx\n",
(unsignedlonglong) blkno,
(unsignedlonglong) nblocks);
jfs_error(bmp->db_ipbmap->i_sb, "dbAllocCtl failed in free AG\n");
} return (rc);
}
/* the buffer for the dmap control page that fully describes the *allocationgroup.
*/
lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel);
mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); if (mp == NULL) return -EIO;
dcp = (struct dmapctl *) mp->data;
budmin = dcp->budmin;
/* search the subtree(s) of the dmap control page that describes *theallocationgroup,lookingforsufficientfreespace.tobegin, *determinehowmanyallocationgroupsarerepresentedinadmap *controlpageatthecontrolpagelevel(i.e.L0,L1,L2)that *fullydescribesanallocationgroup.next,determinethestarting *treeindexofthisallocationgroupwithinthecontrolpage.
*/
agperlev =
(1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth;
ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1));
if (ti < 0 || ti >= le32_to_cpu(dcp->nleafs)) {
jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n");
release_metapage(mp); return -EIO;
}
/* dmap control page trees fan-out by 4 and a single allocation *groupmaybedescribedby1or2subtreeswithintheaglevel *dmapcontrolpage,dependingupontheagsize.examinetheag's *subtreesforsufficientfreespace,startingwiththeleftmost *subtree.
*/ for (i = 0; i < bmp->db_agwidth; i++, ti++) { /* is there sufficient free space ?
*/ if (l2nb > dcp->stree[ti]) continue;
/* sufficient free space found in a subtree. now search down *thesubtreetofindtheleftmostleafthatdescribesthis *freespace.
*/ for (k = bmp->db_agheight; k > 0; k--) { for (n = 0, m = (ti << 2) + 1; n < 4; n++) { if (l2nb <= dcp->stree[m + n]) {
ti = m + n; break;
}
} if (n == 4) {
jfs_error(bmp->db_ipbmap->i_sb, "failed descending stree\n");
release_metapage(mp); return -EIO;
}
}
/* determine the block number within the file system *thatcorrespondstothisleaf.
*/ if (bmp->db_aglevel == 2)
blkno = 0; elseif (bmp->db_aglevel == 1)
blkno &= ~(MAXL1SIZE - 1); else/* bmp->db_aglevel == 0 */
blkno &= ~(MAXL0SIZE - 1);
/* release the buffer in preparation for going down *thenextlevelofdmapcontrolpages.
*/
release_metapage(mp);
/* check if we need to continue to search down the lower *leveldmapcontrolpages.weneedtoifthenumberof *blocksrequiredislessthanmaximumnumberofblocks *describedatthenextlowerlevel.
*/ if (l2nb < budmin) {
/* search the lower level dmap control pages to get *thestartingblocknumberofthedmapthat *containsorstartsoffthefreespace.
*/ if ((rc =
dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1,
&blkno))) { if (rc == -ENOSPC) {
jfs_error(bmp->db_ipbmap->i_sb, "control page inconsistent\n"); return -EIO;
} return (rc);
}
}
/* allocate the blocks.
*/
rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results); if (rc == -ENOSPC) {
jfs_error(bmp->db_ipbmap->i_sb, "unable to allocate blocks\n");
rc = -EIO;
} return (rc);
}
/* no space in the allocation group. release the buffer and *return-ENOSPC.
*/
release_metapage(mp);
/* starting with the top level dmap control page, search *downthedmapcontrollevelsforsufficientfreespace. *iffreespaceisfound,dbFindCtl()returnsthestarting *blocknumberofthedmapthatcontainsorstartsoffthe *rangeoffreespace.
*/ if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno))) return (rc);
/* allocate the blocks.
*/
rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results); if (rc == -ENOSPC) {
jfs_error(bmp->db_ipbmap->i_sb, "unable to allocate blocks\n"); return -EIO;
} return (rc);
}
/* the whole ag is free, trim now */ if (bmp->db_agfree[agno] == 0) break;
/* give a hint for the next while */
nblocks = bmp->db_agfree[agno]; continue;
} elseif (rc == -ENOSPC) { /* search for next smaller log2 block */
l2nb = BLKSTOL2(nblocks) - 1; if (unlikely(l2nb < 0)) break;
nblocks = 1LL << l2nb;
} else { /* Trim any already allocated blocks */
jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n"); break;
}
/* check, if our trim array is full */ if (unlikely(count >= range_cnt - 1)) break;
}
IWRITE_UNLOCK(ipbmap);
tt->nblocks = 0; /* mark the current end */ for (tt = totrim; tt->nblocks != 0; tt++) { /* when mounted with online discard, dbFree() will
* call jfs_issue_discard() itself */ if (!(JFS_SBI(sb)->flag & JFS_DISCARD))
jfs_issue_discard(ip, tt->blkno, tt->nblocks);
dbFree(ip, tt->blkno, tt->nblocks);
trimmed += tt->nblocks;
}
kfree(totrim);
/* starting at the specified dmap control page level and block *number,searchdownthedmapcontrollevelsforthestarting *blocknumberofadmappagethatcontainsorstartsoff *sufficientfreeblocks.
*/ for (lev = level, b = *blkno; lev >= 0; lev--) { /* get the buffer of the dmap control page for the block *numberandlevel(i.e.L0,L1,L2).
*/
lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev);
mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); if (mp == NULL) return -EIO;
dcp = (struct dmapctl *) mp->data;
budmin = dcp->budmin;
/* search the tree within the dmap control page for *sufficientfreespace.ifsufficientfreespaceisfound, *dbFindLeaf()returnstheindexoftheleafatwhich *freespacewasfound.
*/
rc = dbFindLeaf((dmtree_t *) dcp, l2nb, &leafidx, true);
/* release the buffer.
*/
release_metapage(mp);
/* space found ?
*/ if (rc) { if (lev != level) {
jfs_error(bmp->db_ipbmap->i_sb, "dmap inconsistent\n"); return -EIO;
} return -ENOSPC;
}
/* adjust the block number to reflect the location within *thedmapcontrolpage(i.e.theleaf)atwhichfree *spacewasfound.
*/
b += (((s64) leafidx) << budmin);
/* we stop the search at this dmap control page level if *thenumberofblocksrequiredisgreaterthanorequal *tothemaximumnumberofblocksdescribedatthenext *(lower)level.
*/ if (l2nb >= budmin) break;
}
/* check if the allocation request is confined to a single dmap.
*/ if (l2nb <= L2BPERDMAP) { /* get the buffer for the dmap.
*/
lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); if (mp == NULL) return -EIO;
dp = (struct dmap *) mp->data;
if (dp->tree.budmin < 0) {
release_metapage(mp); return -EIO;
}
/* try to allocate the blocks.
*/
rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results); if (rc == 0)
mark_metapage_dirty(mp);
release_metapage(mp);
return (rc);
}
/* allocation request involving multiple dmaps. it must start on *admapboundary.
*/
assert((blkno & (BPERDMAP - 1)) == 0);
/* allocate the blocks dmap by dmap.
*/ for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) { /* get the buffer for the dmap.
*/
lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); if (mp == NULL) {
rc = -EIO; goto backout;
}
dp = (struct dmap *) mp->data;
/* the dmap better be all free.
*/ if (dp->tree.stree[ROOT] != L2BPERDMAP) {
release_metapage(mp);
jfs_error(bmp->db_ipbmap->i_sb, "the dmap is not all free\n");
rc = -EIO; goto backout;
}
/* determine how many blocks to allocate from this dmap.
*/
nb = min_t(s64, n, BPERDMAP);
/* allocate the blocks from the dmap.
*/ if ((rc = dbAllocDmap(bmp, dp, b, nb))) {
release_metapage(mp); goto backout;
}
/* write the buffer.
*/
write_metapage(mp);
}
/* set the results (starting block number) and return.
*/
*results = blkno; return (0);
/* something failed in handling an allocation request involving *multipledmaps.we'lltrytocleanupbybackingoutany *allocationthathasalreadyhappenedforthisrequest.if *wefailinbackingouttheallocation,we'llmarkthefile *systemtoindicatethatblockshavebeenleaked.
*/
backout:
/* try to backout the allocations dmap by dmap.
*/ for (n = nblocks - n, b = blkno; n > 0;
n -= BPERDMAP, b += BPERDMAP) { /* get the buffer for this dmap.
*/
lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); if (mp == NULL) { /* could not back out. mark the file system *toindicatethatwehaveleakedblocks.
*/
jfs_error(bmp->db_ipbmap->i_sb, "I/O Error: Block Leakage\n"); continue;
}
dp = (struct dmap *) mp->data;
/* free the blocks is this dmap.
*/ if (dbFreeDmap(bmp, dp, b, BPERDMAP)) { /* could not back out. mark the file system *toindicatethatwehaveleakedblocks.
*/
release_metapage(mp);
jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n"); continue;
}
/* can't be more than a dmaps worth of blocks */
assert(l2nb <= L2BPERDMAP);
/* search the tree within the dmap page for sufficient *freespace.ifsufficientfreespaceisfound,dbFindLeaf() *returnstheindexoftheleafatwhichfreespacewasfound.
*/ if (dbFindLeaf((dmtree_t *) &dp->tree, l2nb, &leafidx, false)) return -ENOSPC;
if (leafidx < 0) return -EIO;
/* determine the block number within the file system corresponding *totheleafatwhichfreespacewasfound.
*/
blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD);
/* if not all bits of the dmap word are free, get the starting *bitnumberwithinthedmapwordoftherequiredstringoffree *bitsandadjusttheblocknumberwiththisvalue.
*/ if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN)
blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb);
/* allocate the blocks */ if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
*results = blkno;
/* if the root has not changed, done. */ if (dp->tree.stree[ROOT] == oldroot) return (0);
/* root changed. bubble the change up to the dmap control pages. *iftheadjustmentoftheupperlevelcontrolpagesfails, *backoutthebitallocation(thusmakingeverythingconsistent).
*/ if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0)))
dbFreeBits(bmp, dp, blkno, nblocks);
/* if error or the root has not changed, done. */ if (rc || (dp->tree.stree[ROOT] == oldroot)) return (rc);
/* root changed. bubble the change up to the dmap control pages. *iftheadjustmentoftheupperlevelcontrolpagesfails, *backoutthedeallocation.
*/ if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) {
word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
/* as part of backing out the deallocation, we will have *tobacksplitthedmaptreeifthedeallocationcaused *thefreedblockstobecomepartofalargerbinarybuddy *system.
*/ if (dp->tree.stree[word] == NOFREE)
dbBackSplit((dmtree_t *)&dp->tree, word, false);
/* pick up a pointer to the leaves of the dmap tree */
leaf = dp->tree.stree + LEAFIND;
/* determine the bit number and word within the dmap of the *startingblock.
*/
dbitno = blkno & (BPERDMAP - 1);
word = dbitno >> L2DBWORD;
/* block range better be within the dmap */
assert(dbitno + nblocks <= BPERDMAP);
/* allocate the bits of the dmap's words corresponding to the block *range.notallbitsofthefirstandlastwordsmaybecontained *withintheblockrange.ifthisisthecase,we'llworkagainst *thosewords(i.e.partialfirstand/orlast)onanindividualbasis *(asinglepass),allocatingthebitsofinterestbyhandand *updatingtheleafcorrespondingtothedmapword.asinglepass *willbeusedforalldmapwordsfullycontainedwithinthe *specifiedrange.withinthispass,thebitsofallfullycontained *dmapwordswillbemarkedasfreeinasingleshotandtheleaves *willbeupdated.asingleleafmaydescribethefreespaceof *multipledmapwords,sowemayupdateonlyasubsetoftheactual *leavescorrespondingtothedmapwordsoftheblockrange.
*/ for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { /* determine the bit number within the word and *thenumberofbitswithintheword.
*/
wbitno = dbitno & (DBWORD - 1);
nb = min(rembits, DBWORD - wbitno);
/* check if only part of a word is to be allocated.
*/ if (nb < DBWORD) { /* allocate (set to 1) the appropriate bits within *thisdmapword.
*/
dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
>> wbitno);
/* update the leaf for this dmap word. in addition *tosettingtheleafvaluetothebinarybuddymax *oftheupdateddmapword,dbSplit()willsplit *thebinarysystemoftheleavesifneedbe.
*/
dbSplit(tp, word, BUDMIN,
dbMaxBud((u8 *)&dp->wmap[word]), false);
word += 1;
} else { /* one or more dmap words are fully contained *withintheblockrange.determinehowmany *wordsandallocate(setto1)thebitsofthese *words.
*/
nwords = rembits >> L2DBWORD;
memset(&dp->wmap[word], (int) ONES, nwords * 4);
/* determine how many bits.
*/
nb = nwords << L2DBWORD;
/* now update the appropriate leaves to reflect *theallocatedwords.
*/ for (; nwords > 0; nwords -= nw) { if (leaf[word] < BUDMIN) {
jfs_error(bmp->db_ipbmap->i_sb, "leaf page corrupt\n"); break;
}
/* determine what the leaf value should be *updatedtoastheminimumofthel2number *ofbitsbeingallocatedandthel2number *ofbitscurrentlydescribedbythisleaf.
*/
size = min_t(int, leaf[word],
NLSTOL2BSZ(nwords));
/* update the leaf to reflect the allocation. *inadditiontosettingtheleafvalueto *NOFREE,dbSplit()willsplitthebinary *systemoftheleavestoreflectthecurrent *allocation(size).
*/
dbSplit(tp, word, size, NOFREE, false);
/* get the number of dmap words handled */
nw = BUDSIZE(size, BUDMIN);
word += nw;
}
}
}
/* update the free count for this dmap */
le32_add_cpu(&dp->nfree, -nblocks);
BMAP_LOCK(bmp);
/* if this allocation group is completely free, *updatethemaximumallocationgroupnumberifthisallocation *groupisthenewmax.
*/
agno = blkno >> bmp->db_agl2size; if (agno > bmp->db_maxag)
bmp->db_maxag = agno;
/* update the free count for the allocation group and map */
bmp->db_agfree[agno] -= nblocks;
bmp->db_nfree -= nblocks;
/* determine the bit number and word within the dmap of the *startingblock.
*/
dbitno = blkno & (BPERDMAP - 1);
word = dbitno >> L2DBWORD;
/* block range better be within the dmap.
*/
assert(dbitno + nblocks <= BPERDMAP);
/* free the bits of the dmaps words corresponding to the block range. *notallbitsofthefirstandlastwordsmaybecontainedwithin *theblockrange.ifthisisthecase,we'llworkagainstthose *words(i.e.partialfirstand/orlast)onanindividualbasis *(asinglepass),freeingthebitsofinterestbyhandandupdating *theleafcorrespondingtothedmapword.asinglepasswillbeused *foralldmapwordsfullycontainedwithinthespecifiedrange. *withinthispass,thebitsofallfullycontaineddmapwordswill *bemarkedasfreeinasingleshotandtheleaveswillbeupdated.a *singleleafmaydescribethefreespaceofmultipledmapwords, *sowemayupdateonlyasubsetoftheactualleavescorresponding *tothedmapwordsoftheblockrange. * *dbJoin()isusedtoupdateleafvaluesandwilljointhebinary *buddysystemoftheleavesifthenewleafvaluesindicatethis *shouldbedone.
*/ for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { /* determine the bit number within the word and *thenumberofbitswithintheword.
*/
wbitno = dbitno & (DBWORD - 1);
nb = min(rembits, DBWORD - wbitno);
/* check if only part of a word is to be freed.
*/ if (nb < DBWORD) { /* free (zero) the appropriate bits within this *dmapword.
*/
dp->wmap[word] &=
cpu_to_le32(~(ONES << (DBWORD - nb)
>> wbitno));
/* update the leaf for this dmap word.
*/
rc = dbJoin(tp, word,
dbMaxBud((u8 *)&dp->wmap[word]), false); if (rc) return rc;
word += 1;
} else { /* one or more dmap words are fully contained *withintheblockrange.determinehowmany *wordsandfree(zero)thebitsofthesewords.
*/
nwords = rembits >> L2DBWORD;
memset(&dp->wmap[word], 0, nwords * 4);
/* determine how many bits.
*/
nb = nwords << L2DBWORD;
/* now update the appropriate leaves to reflect *thefreedwords.
*/ for (; nwords > 0; nwords -= nw) { /* determine what the leaf value should be *updatedtoastheminimumofthel2number *ofbitsbeingfreedandthel2(max)number *ofbitsthatcanbedescribedbythisleaf.
*/
size =
min(LITOL2BSZ
(word, L2LPERDMAP, BUDMIN),
NLSTOL2BSZ(nwords));
/* update the leaf.
*/
rc = dbJoin(tp, word, size, false); if (rc) return rc;
/* get the number of dmap words handled.
*/
nw = BUDSIZE(size, BUDMIN);
word += nw;
}
}
}
/* update the free count for this dmap.
*/
le32_add_cpu(&dp->nfree, nblocks);
BMAP_LOCK(bmp);
/* update the free count for the allocation group and *map.
*/
agno = blkno >> bmp->db_agl2size;
bmp->db_nfree += nblocks;
bmp->db_agfree[agno] += nblocks;
/* check if this allocation group is not completely free and *ifitiscurrentlythemaximum(rightmost)allocationgroup. *ifso,establishthenewmaximumallocationgroupnumberby *searchingleftforthefirstallocationgroupwithallocation.
*/ if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) ||
(agno == bmp->db_numag - 1 &&
bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) { while (bmp->db_maxag > 0) {
bmp->db_maxag -= 1; if (bmp->db_agfree[bmp->db_maxag] !=
bmp->db_agsize) break;
}
/* re-establish the allocation group preference if the *currentpreferenceisrightofthemaximumallocation *group.
*/ if (bmp->db_agpref > bmp->db_maxag)
bmp->db_agpref = bmp->db_maxag;
}
/* get the buffer for the dmap control page for the specified *blocknumberandcontrolpagelevel.
*/
lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level);
mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); if (mp == NULL) return -EIO;
dcp = (struct dmapctl *) mp->data;
/* determine the leaf number corresponding to the block and *theindexwithinthedmapcontroltree.
*/
leafno = BLKTOCTLLEAF(blkno, dcp->budmin);
ti = leafno + le32_to_cpu(dcp->leafidx);
/* save the current leaf value and the current root level (i.e. *maximuml2freestringdescribedbythisdmapctl).
*/
oldval = dcp->stree[ti];
oldroot = dcp->stree[ROOT];
/* check if this is a control page update for an allocation. *ifso,updatetheleaftoreflectthenewleafvalueusing *dbSplit();otherwise(deallocation),usedbJoin()toupdate *theleafwiththenewvalue.inadditiontoupdatingthe *leaf,dbSplit()willalsosplitthebinarybuddysystemof *theleaves,ifrequired,andbubblenewvalueswithinthe *dmapctltree,ifrequired.similarly,dbJoin()willjoin *thebinarybuddysystemofleavesandbubblenewvaluesup *thedmapctltreeasrequiredbythenewleafvalue.
*/ if (alloc) { /* check if we are in the middle of a binary buddy *system.thishappenswhenweareperformingthe *firstallocationoutofanallocationgroupthat *ispart(notthefirstpart)ofalargerbinary *buddysystem.ifweareinthemiddle,backsplit *thesystempriortocallingdbSplit()whichassumes *thatitisatthefrontofabinarybuddysystem.
*/ if (oldval == NOFREE) {
rc = dbBackSplit((dmtree_t *)dcp, leafno, true); if (rc) {
release_metapage(mp); return rc;
}
oldval = dcp->stree[ti];
}
dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval, true);
} else {
rc = dbJoin((dmtree_t *) dcp, leafno, newval, true); if (rc) {
release_metapage(mp); return rc;
}
}
/* check if the root of the current dmap control page changed due *totheupdateandifthecurrentdmapcontrolpageisnotat *thecurrenttoplevel(i.e.L0,L1,L2)ofthemap.ifso(i.e. *rootchangedandthisisnotthetoplevel),callthisroutine *again(recursion)forthenexthigherlevelofthemappingto *reflectthechangeinrootforthecurrentdmapcontrolpage.
*/ if (dcp->stree[ROOT] != oldroot) { /* are we below the top level of the map. if so, *bubbletherootuptothenexthigherlevel.
*/ if (level < bmp->db_maxlevel) { /* bubble up the new root of this dmap control page to *thenextlevel.
*/ if ((rc =
dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc,
level + 1))) { /* something went wrong in bubbling up the new *rootvalue,sobackoutthechangestothe *currentdmapcontrolpage.
*/ if (alloc) {
dbJoin((dmtree_t *) dcp, leafno,
oldval, true);
} else { /* the dbJoin() above might have *causedalargerbinarybuddysystem *toformandwemaynowbeinthe *middleofit.ifthisisthecase, *backsplitthebuddies.
*/ if (dcp->stree[ti] == NOFREE)
dbBackSplit((dmtree_t *)
dcp, leafno, true);
dbSplit((dmtree_t *) dcp, leafno,
dcp->budmin, oldval, true);
}
/* release the buffer and return the error.
*/
release_metapage(mp); return (rc);
}
} else { /* we're at the top level of the map. update *thebmapcontrolpagetoreflectthesize *ofthemaximumfreebuddysystem.
*/
assert(level == bmp->db_maxlevel); if (bmp->db_maxfreebud != oldroot) {
jfs_error(bmp->db_ipbmap->i_sb, "the maximum free buddy is not the old root\n");
}
bmp->db_maxfreebud = dcp->stree[ROOT];
}
}
/* write the buffer.
*/
write_metapage(mp);
return (0);
}
/* *NAME:dbSplit() * *FUNCTION:updatetheleafofadmtreewithanewvalue,splitting *theleaffromthebinarybuddysystemofthedmtree's *leaves,asrequired. * *PARAMETERS: *tp-pointertothetreecontainingtheleaf. *leafno-thenumberoftheleaftobeupdated. *splitsz-thesizethebinarybuddysystemstartingattheleaf *mustbesplitto,specifiedasthelog2numberofblocks. *newval-thenewvaluefortheleaf. * *RETURNVALUES:none * *serialization:IREAD_LOCK(ipbmap)orIWRITE_LOCK(ipbmap)heldonentry/exit;
*/ staticvoid dbSplit(dmtree_t *tp, int leafno, int splitsz, int newval, bool is_ctl)
{ int budsz; int cursz;
s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
/* check if the leaf needs to be split.
*/ if (leaf[leafno] > tp->dmt_budmin) { /* the split occurs by cutting the buddy system in half *atthespecifiedleafuntilwereachthespecified *size.pickupthestartingsplitsize(currentsize *-1inl2)andthecorrespondingbuddysize.
*/
cursz = leaf[leafno] - 1;
budsz = BUDSIZE(cursz, tp->dmt_budmin);
/* split until we reach the specified size.
*/ while (cursz >= splitsz) { /* update the buddy's leaf with its new value.
*/
dbAdjTree(tp, leafno ^ budsz, cursz, is_ctl);
/* on to the next size and buddy.
*/
cursz -= 1;
budsz >>= 1;
}
}
/* adjust the dmap tree to reflect the specified leaf's new *value.
*/
dbAdjTree(tp, leafno, newval, is_ctl);
}
/* leaf should be part (not first part) of a binary *buddysystem.
*/
assert(leaf[leafno] == NOFREE);
/* the back split is accomplished by iteratively finding the leaf *thatstartsthebuddysystemthatcontainsthespecifiedleafand *splittingthatsystemintwo.thisiterationcontinuesuntil *thespecifiedleafbecomesthestartofabuddysystem. * *determinemaximumpossiblel2sizeforthespecifiedleaf.
*/
size =
LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs),
tp->dmt_budmin);
/* determine the number of leaves covered by this size. this *isthebuddysizethatwewillstartwithaswesearchfor *thebuddysystemthatcontainsthespecifiedleaf.
*/
budsz = BUDSIZE(size, tp->dmt_budmin);
/* back split.
*/ while (leaf[leafno] == NOFREE) { /* find the leftmost buddy leaf.
*/ for (w = leafno, bsz = budsz;; bsz <<= 1,
w = (w < bud) ? w : bud) { if (bsz >= le32_to_cpu(tp->dmt_nleafs)) {
jfs_err("JFS: block map error in dbBackSplit"); return -EIO;
}
/* determine the buddy.
*/
bud = w ^ bsz;
/* check if this buddy is the start of the system.
*/ if (leaf[bud] != NOFREE) { /* split the leaf at the start of the *systemintwo.
*/
cursz = leaf[bud] - 1;
dbSplit(tp, bud, cursz, cursz, is_ctl); break;
}
}
}
if (leaf[leafno] != size) {
jfs_err("JFS: wrong leaf value in dbBackSplit"); return -EIO;
} return0;
}
/* *NAME:dbJoin() * *FUNCTION:updatetheleafofadmtreewithanewvalue,joining *theleafwithotherleavesofthedmtreeintoamulti-leaf *binarybuddysystem,asrequired. * *PARAMETERS: *tp-pointertothetreecontainingtheleaf. *leafno-thenumberoftheleaftobeupdated. *newval-thenewvaluefortheleaf. * *RETURNVALUES:none
*/ staticint dbJoin(dmtree_t *tp, int leafno, int newval, bool is_ctl)
{ int budsz, buddy;
s8 *leaf;
/* can the new leaf value require a join with other leaves ?
*/ if (newval >= tp->dmt_budmin) { /* pickup a pointer to the leaves of the tree.
*/
leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
/* try to join the specified leaf into a large binary *buddysystem.thejoinproceedsbyattemptingtojoin *thespecifiedleafnowithitsbuddy(leaf)atnewvalue. *ifthejoinoccurs,weattempttojointheleftleaf *ofthejoinedbuddieswithitsbuddyatnewvalue+1. *wecontinuetojoinuntilwefindabuddythatcannotbe *joined(doesnothaveavalueequaltothesizeofthe *lastjoin)oruntilallleaveshavebeenjoinedintoa *singlesystem. * *getthebuddysize(numberofwordscovered)of *thenewvalue.
*/
budsz = BUDSIZE(newval, tp->dmt_budmin);
/* try to join.
*/ while (budsz < le32_to_cpu(tp->dmt_nleafs)) { /* get the buddy leaf.
*/
buddy = leafno ^ budsz;
/* if the leaf's new value is greater than its *buddy'svalue,wejoinnomore.
*/ if (newval > leaf[buddy]) break;
/* It shouldn't be less */ if (newval < leaf[buddy]) return -EIO;
/* check which (leafno or buddy) is the left buddy. *theleftbuddygetstoclaimtheblocksresulting *fromthejoinwhiletherightgetstoclaimnone. *theleftbuddyisalsoeligibletoparticipatein *ajoinatthenexthigherlevelwhiletheright *isnot. *
*/ if (leafno < buddy) { /* leafno is the left buddy.
*/
dbAdjTree(tp, buddy, NOFREE, is_ctl);
} else { /* buddy is the left buddy and becomes *leafno.
*/
dbAdjTree(tp, leafno, NOFREE, is_ctl);
leafno = buddy;
}
/* on to try the next join.
*/
newval += 1;
budsz <<= 1;
}
}
/* update the leaf value.
*/
dbAdjTree(tp, leafno, newval, is_ctl);
return0;
}
/* *NAME:dbAdjTree() * *FUNCTION:updatealeafofadmtreewithanewvalue,adjusting *thedmtree,asrequired,toreflectthenewleafvalue. *thecombinationofanybuddiesmustalreadybedonebefore *thisiscalled. * *PARAMETERS: *tp-pointertothetreetobeadjusted. *leafno-thenumberoftheleaftobeupdated. *newval-thenewvaluefortheleaf. * *RETURNVALUES:none
*/ staticvoid dbAdjTree(dmtree_t *tp, int leafno, int newval, bool is_ctl)
{ int lp, pp, k; int max, size;
size = is_ctl ? CTLTREESIZE : TREESIZE;
/* pick up the index of the leaf for this leafno.
*/
lp = leafno + le32_to_cpu(tp->dmt_leafidx);
if (WARN_ON_ONCE(lp >= size || lp < 0)) return;
/* is the current value the same as the old value ? if so, *thereisnothingtodo.
*/ if (tp->dmt_stree[lp] == newval) return;
/* set the new value.
*/
tp->dmt_stree[lp] = newval;
/* bubble the new value up the tree as required.
*/ for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) { if (lp == 0) break;
/* get the index of the first leaf of the 4 leaf *groupcontainingthespecifiedleaf(leafno).
*/
lp = ((lp - 1) & ~0x03) + 1;
/* get the index of the parent of this 4 leaf group.
*/
pp = (lp - 1) >> 2;
/* determine the maximum of the 4 leaves.
*/
max = TREEMAX(&tp->dmt_stree[lp]);
/* if the maximum of the 4 is the same as the *parent'svalue,we'redone.
*/ if (tp->dmt_stree[pp] == max) break;
/* parent gets new value.
*/
tp->dmt_stree[pp] = max;
/* parent becomes leaf for next go-round.
*/
lp = pp;
}
}
/* *NAME:dbFindLeaf() * *FUNCTION:searchadmtree_tforsufficientfreeblocks,returning *theindexofaleafdescribingthefreeblocksif *sufficientfreeblocksarefound. * *thesearchstartsatthetopofthedmtree_ttreeand *proceedsdownthetreetotheleftmostleafwithsufficient *freespace. * *PARAMETERS: *tp-pointertothetreetobesearched. *l2nb-log2numberoffreeblockstosearchfor. *leafidx-returnpointertobesettotheindexoftheleaf *describingatleastl2nbfreeblocksifsufficient *freeblocksarefound. *is_ctl-determinesifthetreeisoftypectl * *RETURNVALUES: *0-success *-ENOSPC-insufficientfreeblocks.
*/ staticint dbFindLeaf(dmtree_t *tp, int l2nb, int *leafidx, bool is_ctl)
{ int ti, n = 0, k, x = 0; int max_size, max_idx;
/* first check the root of the tree to see if there is *sufficientfreespace.
*/ if (l2nb > tp->dmt_stree[ROOT]) return -ENOSPC;
/* sufficient free space available. now search down the tree *startingatthenextlevelfortheleftmostleafthat *describessufficientfreespace.
*/ for (k = le32_to_cpu(tp->dmt_height), ti = 1;
k > 0; k--, ti = ((ti + n) << 2) + 1) { /* search the four nodes at this level, starting from *theleft.
*/ for (x = ti, n = 0; n < 4; n++) { /* sufficient free space found. move to the next *level(orquitifthisisthelastlevel).
*/ if (x + n > max_size) return -ENOSPC; if (l2nb <= tp->dmt_stree[x + n]) break;
}
/* better have found something since the higher *levelsofthetreesaiditwashere.
*/
assert(n < 4);
} if (le32_to_cpu(tp->dmt_leafidx) >= max_idx) return -ENOSPC;
/* set the return to the leftmost leaf describing sufficient *freespace.
*/
*leafidx = x + n - le32_to_cpu(tp->dmt_leafidx);
return (0);
}
/* *NAME:dbFindBits() * *FUNCTION:findaspecifiednumberofbinarybuddyfreebitswithina *dmapbitmapwordvalue. * *thisroutinesearchesthebitmapvaluefor(1<<l2nb)free *bitsat(1<<l2nb)alignmentswithinthevalue. * *PARAMETERS: *word-dmapbitmapwordvalue. *l2nb-numberoffreebitsspecifiedasalog2number. * *RETURNVALUES: *startingbitnumberoffreebits.
*/ staticint dbFindBits(u32 word, int l2nb)
{ int bitno, nb;
u32 mask;
/* get the number of bits.
*/
nb = 1 << l2nb;
assert(nb <= DBWORD);
/* complement the word so we can use a mask (i.e. 0s represent *freebits)andcomputethemask.
*/
word = ~word;
mask = ONES << (DBWORD - nb);
/* scan the word for nb free bits at nb alignments.
*/ for (bitno = 0; mask != 0; bitno += nb, mask = (mask >> nb)) { if ((mask & word) == mask) break;
}
/* block to be allocated better be within the mapsize. */
ASSERT(nblocks <= bmp->db_mapsize - blkno);
/* *allocatetheblocksadmapatatime.
*/
mp = NULL; for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { /* release previous dmap if any */ if (mp) {
write_metapage(mp);
}
/* get the buffer for the current dmap. */
lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
mp = read_metapage(ipbmap, lblkno, PSIZE, 0); if (mp == NULL) {
IREAD_UNLOCK(ipbmap); return -EIO;
}
dp = (struct dmap *) mp->data;
/* determine the number of blocks to be allocated from *thisdmap.
*/
nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
/* allocate the blocks. */ if ((rc = dbAllocDmapBU(bmp, dp, blkno, nb))) {
release_metapage(mp);
IREAD_UNLOCK(ipbmap); return (rc);
}
}
/* write the last buffer. */
write_metapage(mp);
IREAD_UNLOCK(ipbmap);
return (0);
}
staticint dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno, int nblocks)
{ int rc; int dbitno, word, rembits, nb, nwords, wbitno, agno;
s8 oldroot; struct dmaptree *tp = (struct dmaptree *) & dp->tree;
/* save the current value of the root (i.e. maximum free string) *ofthedmaptree.
*/
oldroot = tp->stree[ROOT];
/* determine the bit number and word within the dmap of the *startingblock.
*/
dbitno = blkno & (BPERDMAP - 1);
word = dbitno >> L2DBWORD;
/* block range better be within the dmap */
assert(dbitno + nblocks <= BPERDMAP);
/* allocate the bits of the dmap's words corresponding to the block *range.notallbitsofthefirstandlastwordsmaybecontained *withintheblockrange.ifthisisthecase,we'llworkagainst *thosewords(i.e.partialfirstand/orlast)onanindividualbasis *(asinglepass),allocatingthebitsofinterestbyhandand *updatingtheleafcorrespondingtothedmapword.asinglepass *willbeusedforalldmapwordsfullycontainedwithinthe *specifiedrange.withinthispass,thebitsofallfullycontained *dmapwordswillbemarkedasfreeinasingleshotandtheleaves *willbeupdated.asingleleafmaydescribethefreespaceof *multipledmapwords,sowemayupdateonlyasubsetoftheactual *leavescorrespondingtothedmapwordsoftheblockrange.
*/ for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { /* determine the bit number within the word and *thenumberofbitswithintheword.
*/
wbitno = dbitno & (DBWORD - 1);
nb = min(rembits, DBWORD - wbitno);
/* check if only part of a word is to be allocated.
*/ if (nb < DBWORD) { /* allocate (set to 1) the appropriate bits within *thisdmapword.
*/
dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
>> wbitno);
word++;
} else { /* one or more dmap words are fully contained *withintheblockrange.determinehowmany *wordsandallocate(setto1)thebitsofthese *words.
*/
nwords = rembits >> L2DBWORD;
memset(&dp->wmap[word], (int) ONES, nwords * 4);
/* determine how many bits */
nb = nwords << L2DBWORD;
word += nwords;
}
}
/* update the free count for this dmap */
le32_add_cpu(&dp->nfree, -nblocks);
/* reconstruct summary tree */
dbInitDmapTree(dp);
BMAP_LOCK(bmp);
/* if this allocation group is completely free, *updatethehighestactiveallocationgroupnumber *ifthisallocationgroupisthenewmax.
*/
agno = blkno >> bmp->db_agl2size; if (agno > bmp->db_maxag)
bmp->db_maxag = agno;
/* update the free count for the allocation group and map */
bmp->db_agfree[agno] -= nblocks;
bmp->db_nfree -= nblocks;
BMAP_UNLOCK(bmp);
/* if the root has not changed, done. */ if (tp->stree[ROOT] == oldroot) return (0);
/* root changed. bubble the change up to the dmap control pages. *iftheadjustmentoftheupperlevelcontrolpagesfails, *backoutthebitallocation(thusmakingeverythingconsistent).
*/ if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0)))
dbFreeBits(bmp, dp, blkno, nblocks);
if (nblocks)
l1leaf++; /* continue for next L0 */ else { /* more than 1 L0 ? */ if (j > 0) break; /* build L1 page */ else { /* summarize in global bmap page */
bmp->db_maxfreebud = *l1leaf;
release_metapage(l1mp);
release_metapage(l2mp); goto finalize;
}
}
} /* for each L0 in a L1 */
if (nblocks)
l2leaf++; /* continue for next L1 */ else { /* more than 1 L1 ? */ if (k > 0) break; /* build L2 page */ else { /* summarize in global bmap page */
bmp->db_maxfreebud = *l2leaf;
release_metapage(l2mp); goto finalize;
}
}
} /* for each L1 in a L2 */
jfs_error(ipbmap->i_sb, "function has not returned as expected\n");
errout: if (l0mp)
release_metapage(l0mp); if (l1mp)
release_metapage(l1mp);
release_metapage(l2mp); return -EIO;
/* *finalizebmapcontrolpage
*/
finalize:
return0;
}
/* *dbFinalizeBmap()
*/ void dbFinalizeBmap(struct inode *ipbmap)
{ struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; int actags, inactags, l2nl;
s64 ag_rem, actfree, inactfree, avgfree; int i, n;
/* *finalizebmapcontrolpage
*/ //finalize: /* *computedb_agpref:preferredagtoallocatefrom *(theleftmostagwithaveragefreespaceinit);
*/ //agpref: /* get the number of active ags and inactive ags */
actags = bmp->db_maxag + 1;
inactags = bmp->db_numag - actags;
ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */
/* determine how many blocks are in the inactive allocation *groups.indoingthis,wemustaccountforthefactthat *therightmostgroupmightbeapartialgroup(i.e.file *systemsizeisnotamultipleofthegroupsize).
*/
inactfree = (inactags && ag_rem) ?
(((s64)inactags - 1) << bmp->db_agl2size) + ag_rem
: ((s64)inactags << bmp->db_agl2size);
/* determine how many free blocks are in the active *allocationgroupsplustheaveragenumberoffreeblocks *withintheactiveags.
*/
actfree = bmp->db_nfree - inactfree;
avgfree = (u32) actfree / (u32) actags;
/* if the preferred allocation group has not average free space. *re-establishthepreferredgroupastheleftmost *groupwithaveragefreespace.
*/ if (bmp->db_agfree[bmp->db_agpref] < avgfree) { for (bmp->db_agpref = 0; bmp->db_agpref < actags;
bmp->db_agpref++) { if (bmp->db_agfree[bmp->db_agpref] >= avgfree) break;
} if (bmp->db_agpref >= bmp->db_numag) {
jfs_error(ipbmap->i_sb, "cannot find ag with average freespace\n");
}
}
/* word number containing start block number */
w = blkno >> L2DBWORD;
/* *freethebitscorrespondingtotheblockrange(ZEROS): *note:notallbitsofthefirstandlastwordsmaybecontained *withintheblockrange.
*/ for (r = nblocks; r > 0; r -= nb, blkno += nb) { /* number of bits preceding range to be freed in the word */
b = blkno & (DBWORD - 1); /* number of bits to free in the word */
nb = min(r, DBWORD - b);
/* is partial word to be freed ? */ if (nb < DBWORD) { /* free (set to 0) from the bitmap word */
dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
>> b));
dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
>> b));
/* skip the word freed */
w++;
} else { /* free (set to 0) contiguous bitmap words */
nw = r >> L2DBWORD;
memset(&dp->wmap[w], 0, nw * 4);
memset(&dp->pmap[w], 0, nw * 4);
/* skip the words freed */
nb = nw << L2DBWORD;
w += nw;
}
}
/* the first word beyond the end of existing blocks */
w = blkno >> L2DBWORD;
/* does nblocks fall on a 32-bit boundary ? */
b = blkno & (DBWORD - 1); if (b) { /* mark a partial word allocated */
dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b);
w++;
}
/* set the rest of the words in the page to allocated (ONES) */ for (i = w; i < LPERDMAP; i++)
dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES);
/* scan each adjacent buddy pair at current buddy size */ for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx);
i < le32_to_cpu(dtp->nleafs);
i += nextb, cp += nextb) { /* coalesce if both adjacent buddies are max free */ if (*cp == l2free && *(cp + bsize) == l2free) {
*cp = l2free + 1; /* left take right */
*(cp + bsize) = -1; /* right give left */
}
}
}
/* *bubblesummaryinformationofleavesupthetree. * *Startingattheleafnodelevel,thefournodesdescribedby *thehigherlevelparentnodearecomparedforamaximumfreeand *thismaximumbecomesthevalueoftheparentnode. *whenalllowerlevelnodesareprocessedinthisfashionthen *moveuptothenextlevel(parentbecomesalowerlevelnode)and *continuetheprocessforthatlevel.
*/ for (child = le32_to_cpu(dtp->leafidx),
nparent = le32_to_cpu(dtp->nleafs) >> 2;
nparent > 0; nparent >>= 2, child = parent) { /* get index of 1st node of parent level */
parent = (child - 1) >> 2;
/* set the value of the parent node as the maximum *ofthefournodesofthecurrentlevel.
*/ for (i = 0, cp = tp + child, cp1 = tp + parent;
i < nparent; i++, cp += 4, cp1++)
*cp1 = TREEMAX(cp);
}
return (*tp);
}
/* *dbInitDmapCtl() * *function:initializedmapctlpage
*/ staticint dbInitDmapCtl(struct dmapctl * dcp, int level, int i)
{ /* start leaf index not covered by range */
s8 *cp;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.