1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) International Business Machines Corp., 2000-2004 4 * Portions Copyright (C) Tino Reichardt, 2012 5 */ 6 7 #include <linux/fs.h> 8 #include <linux/slab.h> 9 #include "jfs_incore.h" 10 #include "jfs_superblock.h" 11 #include "jfs_dmap.h" 12 #include "jfs_imap.h" 13 #include "jfs_lock.h" 14 #include "jfs_metapage.h" 15 #include "jfs_debug.h" 16 #include "jfs_discard.h" 17 18 /* 19 * SERIALIZATION of the Block Allocation Map. 20 * 21 * the working state of the block allocation map is accessed in 22 * two directions: 23 * 24 * 1) allocation and free requests that start at the dmap 25 * level and move up through the dmap control pages (i.e. 26 * the vast majority of requests). 27 * 28 * 2) allocation requests that start at dmap control page 29 * level and work down towards the dmaps. 30 * 31 * the serialization scheme used here is as follows. 32 * 33 * requests which start at the bottom are serialized against each 34 * other through buffers and each requests holds onto its buffers 35 * as it works it way up from a single dmap to the required level 36 * of dmap control page. 37 * requests that start at the top are serialized against each other 38 * and request that start from the bottom by the multiple read/single 39 * write inode lock of the bmap inode. requests starting at the top 40 * take this lock in write mode while request starting at the bottom 41 * take the lock in read mode. a single top-down request may proceed 42 * exclusively while multiple bottoms-up requests may proceed 43 * simultaneously (under the protection of busy buffers). 44 * 45 * in addition to information found in dmaps and dmap control pages, 46 * the working state of the block allocation map also includes read/ 47 * write information maintained in the bmap descriptor (i.e. total 48 * free block count, allocation group level free block counts). 49 * a single exclusive lock (BMAP_LOCK) is used to guard this information 50 * in the face of multiple-bottoms up requests. 51 * (lock ordering: IREAD_LOCK, BMAP_LOCK); 52 * 53 * accesses to the persistent state of the block allocation map (limited 54 * to the persistent bitmaps in dmaps) is guarded by (busy) buffers. 55 */ 56 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) 60 61 /* 62 * forward references 63 */ 64 static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno, 65 int nblocks); 66 static void dbSplit(dmtree_t *tp, int leafno, int splitsz, int newval, bool is_ctl); 67 static int dbBackSplit(dmtree_t *tp, int leafno, bool is_ctl); 68 static int dbJoin(dmtree_t *tp, int leafno, int newval, bool is_ctl); 69 static void dbAdjTree(dmtree_t *tp, int leafno, int newval, bool is_ctl); 70 static int dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc, 71 int level); 72 static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results); 73 static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno, 74 int nblocks); 75 static int dbAllocNear(struct bmap * bmp, struct dmap * dp, s64 blkno, 76 int nblocks, 77 int l2nb, s64 * results); 78 static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno, 79 int nblocks); 80 static int dbAllocDmapLev(struct bmap * bmp, struct dmap * dp, int nblocks, 81 int l2nb, 82 s64 * results); 83 static int dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb, 84 s64 * results); 85 static int dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, 86 s64 * results); 87 static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks); 88 static int dbFindBits(u32 word, int l2nb); 89 static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno); 90 static int dbFindLeaf(dmtree_t *tp, int l2nb, int *leafidx, bool is_ctl); 91 static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno, 92 int nblocks); 93 static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno, 94 int nblocks); 95 static int dbMaxBud(u8 * cp); 96 static int blkstol2(s64 nb); 97 98 static int cntlz(u32 value); 99 static int cnttz(u32 word); 100 101 static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno, 102 int nblocks); 103 static int dbInitDmap(struct dmap * dp, s64 blkno, int nblocks); 104 static int dbInitDmapTree(struct dmap * dp); 105 static int dbInitTree(struct dmaptree * dtp); 106 static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i); 107 static int dbGetL2AGSize(s64 nblocks); 108 109 /* 110 * buddy table 111 * 112 * table used for determining buddy sizes within characters of 113 * dmap bitmap words. the characters themselves serve as indexes 114 * into the table, with the table elements yielding the maximum 115 * binary buddy of free bits within the character. 116 */ 117 static const s8 budtab[256] = { 118 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 119 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 120 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 121 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 122 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 123 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 124 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 125 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 126 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 127 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 128 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 129 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 130 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 131 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 132 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 133 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1 134 }; 135 136 /* 137 * check_dmapctl - Validate integrity of a dmapctl structure 138 * @dcp: Pointer to the dmapctl structure to check 139 * 140 * Return: true if valid, false if corrupted 141 */ 142 static bool check_dmapctl(struct dmapctl *dcp) 143 { 144 s8 budmin = dcp->budmin; 145 u32 nleafs, l2nleafs, leafidx, height; 146 int i; 147 148 nleafs = le32_to_cpu(dcp->nleafs); 149 /* Check basic field ranges */ 150 if (unlikely(nleafs > LPERCTL)) { 151 jfs_err("dmapctl: invalid nleafs %u (max %u)", 152 nleafs, LPERCTL); 153 return false; 154 } 155 156 l2nleafs = le32_to_cpu(dcp->l2nleafs); 157 if (unlikely(l2nleafs > L2LPERCTL)) { 158 jfs_err("dmapctl: invalid l2nleafs %u (max %u)", 159 l2nleafs, L2LPERCTL); 160 return false; 161 } 162 163 /* Verify nleafs matches l2nleafs (must be power of two) */ 164 if (unlikely((1U << l2nleafs) != nleafs)) { 165 jfs_err("dmapctl: nleafs %u != 2^%u", 166 nleafs, l2nleafs); 167 return false; 168 } 169 170 leafidx = le32_to_cpu(dcp->leafidx); 171 /* Check leaf index matches expected position */ 172 if (unlikely(leafidx != CTLLEAFIND)) { 173 jfs_err("dmapctl: invalid leafidx %u (expected %u)", 174 leafidx, CTLLEAFIND); 175 return false; 176 } 177 178 height = le32_to_cpu(dcp->height); 179 /* Check tree height is within valid range */ 180 if (unlikely(height > (L2LPERCTL >> 1))) { 181 jfs_err("dmapctl: invalid height %u (max %u)", 182 height, L2LPERCTL >> 1); 183 return false; 184 } 185 186 /* Check budmin is valid (cannot be NOFREE for non-empty tree) */ 187 if (budmin == NOFREE) { 188 if (unlikely(nleafs > 0)) { 189 jfs_err("dmapctl: budmin is NOFREE but nleafs %u", 190 nleafs); 191 return false; 192 } 193 } else if (unlikely(budmin < BUDMIN)) { 194 jfs_err("dmapctl: invalid budmin %d (min %d)", 195 budmin, BUDMIN); 196 return false; 197 } 198 199 /* Check leaf nodes fit within stree array */ 200 if (unlikely(leafidx + nleafs > CTLTREESIZE)) { 201 jfs_err("dmapctl: leaf range exceeds stree size (end %u > %u)", 202 leafidx + nleafs, CTLTREESIZE); 203 return false; 204 } 205 206 /* Check leaf nodes have valid values */ 207 for (i = leafidx; i < leafidx + nleafs; i++) { 208 s8 val = dcp->stree[i]; 209 210 if (unlikely(val < NOFREE)) { 211 jfs_err("dmapctl: invalid leaf value %d at index %d", 212 val, i); 213 return false; 214 } else if (unlikely(val > 31)) { 215 jfs_err("dmapctl: leaf value %d too large at index %d", val, i); 216 return false; 217 } 218 } 219 220 return true; 221 } 222 223 /* 224 * NAME: dbMount() 225 * 226 * FUNCTION: initializate the block allocation map. 227 * 228 * memory is allocated for the in-core bmap descriptor and 229 * the in-core descriptor is initialized from disk. 230 * 231 * PARAMETERS: 232 * ipbmap - pointer to in-core inode for the block map. 233 * 234 * RETURN VALUES: 235 * 0 - success 236 * -ENOMEM - insufficient memory 237 * -EIO - i/o error 238 * -EINVAL - wrong bmap data 239 */ 240 int dbMount(struct inode *ipbmap) 241 { 242 struct bmap *bmp; 243 struct dbmap_disk *dbmp_le; 244 struct metapage *mp; 245 int i, err; 246 247 /* 248 * allocate/initialize the in-memory bmap descriptor 249 */ 250 /* allocate memory for the in-memory bmap descriptor */ 251 bmp = kmalloc_obj(struct bmap); 252 if (bmp == NULL) 253 return -ENOMEM; 254 255 /* read the on-disk bmap descriptor. */ 256 mp = read_metapage(ipbmap, 257 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage, 258 PSIZE, 0); 259 if (mp == NULL) { 260 err = -EIO; 261 goto err_kfree_bmp; 262 } 263 264 /* copy the on-disk bmap descriptor to its in-memory version. */ 265 dbmp_le = (struct dbmap_disk *) mp->data; 266 bmp->db_mapsize = le64_to_cpu(dbmp_le->dn_mapsize); 267 bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree); 268 bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage); 269 bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag); 270 bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel); 271 bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag); 272 bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref); 273 bmp->db_aglevel = le32_to_cpu(dbmp_le->dn_aglevel); 274 bmp->db_agheight = le32_to_cpu(dbmp_le->dn_agheight); 275 bmp->db_agwidth = le32_to_cpu(dbmp_le->dn_agwidth); 276 bmp->db_agstart = le32_to_cpu(dbmp_le->dn_agstart); 277 bmp->db_agl2size = le32_to_cpu(dbmp_le->dn_agl2size); 278 279 if ((bmp->db_l2nbperpage > L2PSIZE - L2MINBLOCKSIZE) || 280 (bmp->db_l2nbperpage < 0) || 281 !bmp->db_numag || (bmp->db_numag > MAXAG) || 282 (bmp->db_maxag >= MAXAG) || (bmp->db_maxag < 0) || 283 (bmp->db_agpref >= MAXAG) || (bmp->db_agpref < 0) || 284 (bmp->db_agheight < 0) || (bmp->db_agheight > (L2LPERCTL >> 1)) || 285 (bmp->db_agwidth < 1) || (bmp->db_agwidth > (LPERCTL / MAXAG)) || 286 (bmp->db_agwidth > (1 << (L2LPERCTL - (bmp->db_agheight << 1)))) || 287 (bmp->db_agstart < 0) || 288 (bmp->db_agstart > (CTLTREESIZE - 1 - bmp->db_agwidth * (MAXAG - 1))) || 289 (bmp->db_agl2size > L2MAXL2SIZE - L2MAXAG) || 290 (bmp->db_agl2size < 0) || 291 ((bmp->db_mapsize - 1) >> bmp->db_agl2size) > MAXAG) { 292 err = -EINVAL; 293 goto err_release_metapage; 294 } 295 296 for (i = 0; i < MAXAG; i++) 297 bmp->db_agfree[i] = le64_to_cpu(dbmp_le->dn_agfree[i]); 298 bmp->db_agsize = le64_to_cpu(dbmp_le->dn_agsize); 299 bmp->db_maxfreebud = dbmp_le->dn_maxfreebud; 300 301 /* release the buffer. */ 302 release_metapage(mp); 303 304 /* bind the bmap inode and the bmap descriptor to each other. */ 305 bmp->db_ipbmap = ipbmap; 306 JFS_SBI(ipbmap->i_sb)->bmap = bmp; 307 308 memset(bmp->db_active, 0, sizeof(bmp->db_active)); 309 310 /* 311 * allocate/initialize the bmap lock 312 */ 313 BMAP_LOCK_INIT(bmp); 314 315 return (0); 316 317 err_release_metapage: 318 release_metapage(mp); 319 err_kfree_bmp: 320 kfree(bmp); 321 return err; 322 } 323 324 325 /* 326 * NAME: dbUnmount() 327 * 328 * FUNCTION: terminate the block allocation map in preparation for 329 * file system unmount. 330 * 331 * the in-core bmap descriptor is written to disk and 332 * the memory for this descriptor is freed. 333 * 334 * PARAMETERS: 335 * ipbmap - pointer to in-core inode for the block map. 336 * 337 * RETURN VALUES: 338 * 0 - success 339 * -EIO - i/o error 340 */ 341 int dbUnmount(struct inode *ipbmap, int mounterror) 342 { 343 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; 344 345 if (!(mounterror || isReadOnly(ipbmap))) 346 dbSync(ipbmap); 347 348 /* 349 * Invalidate the page cache buffers 350 */ 351 truncate_inode_pages(ipbmap->i_mapping, 0); 352 353 /* free the memory for the in-memory bmap. */ 354 kfree(bmp); 355 JFS_SBI(ipbmap->i_sb)->bmap = NULL; 356 357 return (0); 358 } 359 360 /* 361 * dbSync() 362 */ 363 int dbSync(struct inode *ipbmap) 364 { 365 struct dbmap_disk *dbmp_le; 366 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; 367 struct metapage *mp; 368 int i; 369 370 /* 371 * write bmap global control page 372 */ 373 /* get the buffer for the on-disk bmap descriptor. */ 374 mp = read_metapage(ipbmap, 375 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage, 376 PSIZE, 0); 377 if (mp == NULL) { 378 jfs_err("dbSync: read_metapage failed!"); 379 return -EIO; 380 } 381 /* copy the in-memory version of the bmap to the on-disk version */ 382 dbmp_le = (struct dbmap_disk *) mp->data; 383 dbmp_le->dn_mapsize = cpu_to_le64(bmp->db_mapsize); 384 dbmp_le->dn_nfree = cpu_to_le64(bmp->db_nfree); 385 dbmp_le->dn_l2nbperpage = cpu_to_le32(bmp->db_l2nbperpage); 386 dbmp_le->dn_numag = cpu_to_le32(bmp->db_numag); 387 dbmp_le->dn_maxlevel = cpu_to_le32(bmp->db_maxlevel); 388 dbmp_le->dn_maxag = cpu_to_le32(bmp->db_maxag); 389 dbmp_le->dn_agpref = cpu_to_le32(bmp->db_agpref); 390 dbmp_le->dn_aglevel = cpu_to_le32(bmp->db_aglevel); 391 dbmp_le->dn_agheight = cpu_to_le32(bmp->db_agheight); 392 dbmp_le->dn_agwidth = cpu_to_le32(bmp->db_agwidth); 393 dbmp_le->dn_agstart = cpu_to_le32(bmp->db_agstart); 394 dbmp_le->dn_agl2size = cpu_to_le32(bmp->db_agl2size); 395 for (i = 0; i < MAXAG; i++) 396 dbmp_le->dn_agfree[i] = cpu_to_le64(bmp->db_agfree[i]); 397 dbmp_le->dn_agsize = cpu_to_le64(bmp->db_agsize); 398 dbmp_le->dn_maxfreebud = bmp->db_maxfreebud; 399 400 /* write the buffer */ 401 write_metapage(mp); 402 403 /* 404 * write out dirty pages of bmap 405 */ 406 filemap_write_and_wait(ipbmap->i_mapping); 407 408 diWriteSpecial(ipbmap, 0); 409 410 return (0); 411 } 412 413 /* 414 * NAME: dbFree() 415 * 416 * FUNCTION: free the specified block range from the working block 417 * allocation map. 418 * 419 * the blocks will be free from the working map one dmap 420 * at a time. 421 * 422 * PARAMETERS: 423 * ip - pointer to in-core inode; 424 * blkno - starting block number to be freed. 425 * nblocks - number of blocks to be freed. 426 * 427 * RETURN VALUES: 428 * 0 - success 429 * -EIO - i/o error 430 */ 431 int dbFree(struct inode *ip, s64 blkno, s64 nblocks) 432 { 433 struct metapage *mp; 434 struct dmap *dp; 435 int nb, rc; 436 s64 lblkno, rem; 437 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; 438 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; 439 struct super_block *sb = ipbmap->i_sb; 440 441 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP); 442 443 /* block to be freed better be within the mapsize. */ 444 if (unlikely((blkno == 0) || (blkno + nblocks > bmp->db_mapsize))) { 445 IREAD_UNLOCK(ipbmap); 446 printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n", 447 (unsigned long long) blkno, 448 (unsigned long long) nblocks); 449 jfs_error(ip->i_sb, "block to be freed is outside the map\n"); 450 return -EIO; 451 } 452 453 /** 454 * TRIM the blocks, when mounted with discard option 455 */ 456 if (JFS_SBI(sb)->flag & JFS_DISCARD) 457 if (JFS_SBI(sb)->minblks_trim <= nblocks) 458 jfs_issue_discard(ipbmap, blkno, nblocks); 459 460 /* 461 * free the blocks a dmap at a time. 462 */ 463 mp = NULL; 464 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { 465 /* release previous dmap if any */ 466 if (mp) { 467 write_metapage(mp); 468 } 469 470 /* get the buffer for the current dmap. */ 471 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); 472 mp = read_metapage(ipbmap, lblkno, PSIZE, 0); 473 if (mp == NULL) { 474 IREAD_UNLOCK(ipbmap); 475 return -EIO; 476 } 477 dp = (struct dmap *) mp->data; 478 479 /* determine the number of blocks to be freed from 480 * this dmap. 481 */ 482 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1))); 483 484 /* free the blocks. */ 485 if ((rc = dbFreeDmap(bmp, dp, blkno, nb))) { 486 jfs_error(ip->i_sb, "error in block map\n"); 487 release_metapage(mp); 488 IREAD_UNLOCK(ipbmap); 489 return (rc); 490 } 491 } 492 493 /* write the last buffer. */ 494 if (mp) 495 write_metapage(mp); 496 497 IREAD_UNLOCK(ipbmap); 498 499 return (0); 500 } 501 502 503 /* 504 * NAME: dbUpdatePMap() 505 * 506 * FUNCTION: update the allocation state (free or allocate) of the 507 * specified block range in the persistent block allocation map. 508 * 509 * the blocks will be updated in the persistent map one 510 * dmap at a time. 511 * 512 * PARAMETERS: 513 * ipbmap - pointer to in-core inode for the block map. 514 * free - 'true' if block range is to be freed from the persistent 515 * map; 'false' if it is to be allocated. 516 * blkno - starting block number of the range. 517 * nblocks - number of contiguous blocks in the range. 518 * tblk - transaction block; 519 * 520 * RETURN VALUES: 521 * 0 - success 522 * -EIO - i/o error 523 */ 524 int 525 dbUpdatePMap(struct inode *ipbmap, 526 int free, s64 blkno, s64 nblocks, struct tblock * tblk) 527 { 528 int nblks, dbitno, wbitno, rbits; 529 int word, nbits, nwords; 530 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; 531 s64 lblkno, rem, lastlblkno; 532 u32 mask; 533 struct dmap *dp; 534 struct metapage *mp; 535 struct jfs_log *log; 536 int lsn, difft, diffp; 537 unsigned long flags; 538 539 /* the blocks better be within the mapsize. */ 540 if (blkno + nblocks > bmp->db_mapsize) { 541 printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n", 542 (unsigned long long) blkno, 543 (unsigned long long) nblocks); 544 jfs_error(ipbmap->i_sb, "blocks are outside the map\n"); 545 return -EIO; 546 } 547 548 /* compute delta of transaction lsn from log syncpt */ 549 lsn = tblk->lsn; 550 log = (struct jfs_log *) JFS_SBI(tblk->sb)->log; 551 logdiff(difft, lsn, log); 552 553 /* 554 * update the block state a dmap at a time. 555 */ 556 mp = NULL; 557 lastlblkno = 0; 558 for (rem = nblocks; rem > 0; rem -= nblks, blkno += nblks) { 559 /* get the buffer for the current dmap. */ 560 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); 561 if (lblkno != lastlblkno) { 562 if (mp) { 563 write_metapage(mp); 564 } 565 566 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 567 0); 568 if (mp == NULL) 569 return -EIO; 570 metapage_wait_for_io(mp); 571 } 572 dp = (struct dmap *) mp->data; 573 574 /* determine the bit number and word within the dmap of 575 * the starting block. also determine how many blocks 576 * are to be updated within this dmap. 577 */ 578 dbitno = blkno & (BPERDMAP - 1); 579 word = dbitno >> L2DBWORD; 580 nblks = min(rem, (s64)BPERDMAP - dbitno); 581 582 /* update the bits of the dmap words. the first and last 583 * words may only have a subset of their bits updated. if 584 * this is the case, we'll work against that word (i.e. 585 * partial first and/or last) only in a single pass. a 586 * single pass will also be used to update all words that 587 * are to have all their bits updated. 588 */ 589 for (rbits = nblks; rbits > 0; 590 rbits -= nbits, dbitno += nbits) { 591 /* determine the bit number within the word and 592 * the number of bits within the word. 593 */ 594 wbitno = dbitno & (DBWORD - 1); 595 nbits = min(rbits, DBWORD - wbitno); 596 597 /* check if only part of the word is to be updated. */ 598 if (nbits < DBWORD) { 599 /* update (free or allocate) the bits 600 * in this word. 601 */ 602 mask = 603 (ONES << (DBWORD - nbits) >> wbitno); 604 if (free) 605 dp->pmap[word] &= 606 cpu_to_le32(~mask); 607 else 608 dp->pmap[word] |= 609 cpu_to_le32(mask); 610 611 word += 1; 612 } else { 613 /* one or more words are to have all 614 * their bits updated. determine how 615 * many words and how many bits. 616 */ 617 nwords = rbits >> L2DBWORD; 618 nbits = nwords << L2DBWORD; 619 620 /* update (free or allocate) the bits 621 * in these words. 622 */ 623 if (free) 624 memset(&dp->pmap[word], 0, 625 nwords * 4); 626 else 627 memset(&dp->pmap[word], (int) ONES, 628 nwords * 4); 629 630 word += nwords; 631 } 632 } 633 634 /* 635 * update dmap lsn 636 */ 637 if (lblkno == lastlblkno) 638 continue; 639 640 lastlblkno = lblkno; 641 642 LOGSYNC_LOCK(log, flags); 643 if (mp->lsn != 0) { 644 /* inherit older/smaller lsn */ 645 logdiff(diffp, mp->lsn, log); 646 if (difft < diffp) { 647 mp->lsn = lsn; 648 649 /* move bp after tblock in logsync list */ 650 list_move(&mp->synclist, &tblk->synclist); 651 } 652 653 /* inherit younger/larger clsn */ 654 logdiff(difft, tblk->clsn, log); 655 logdiff(diffp, mp->clsn, log); 656 if (difft > diffp) 657 mp->clsn = tblk->clsn; 658 } else { 659 mp->log = log; 660 mp->lsn = lsn; 661 662 /* insert bp after tblock in logsync list */ 663 log->count++; 664 list_add(&mp->synclist, &tblk->synclist); 665 666 mp->clsn = tblk->clsn; 667 } 668 LOGSYNC_UNLOCK(log, flags); 669 } 670 671 /* write the last buffer. */ 672 if (mp) { 673 write_metapage(mp); 674 } 675 676 return (0); 677 } 678 679 680 /* 681 * NAME: dbNextAG() 682 * 683 * FUNCTION: find the preferred allocation group for new allocations. 684 * 685 * Within the allocation groups, we maintain a preferred 686 * allocation group which consists of a group with at least 687 * average free space. It is the preferred group that we target 688 * new inode allocation towards. The tie-in between inode 689 * allocation and block allocation occurs as we allocate the 690 * first (data) block of an inode and specify the inode (block) 691 * as the allocation hint for this block. 692 * 693 * We try to avoid having more than one open file growing in 694 * an allocation group, as this will lead to fragmentation. 695 * This differs from the old OS/2 method of trying to keep 696 * empty ags around for large allocations. 697 * 698 * PARAMETERS: 699 * ipbmap - pointer to in-core inode for the block map. 700 * 701 * RETURN VALUES: 702 * the preferred allocation group number. 703 */ 704 int dbNextAG(struct inode *ipbmap) 705 { 706 s64 avgfree; 707 int agpref; 708 s64 hwm = 0; 709 int i; 710 int next_best = -1; 711 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; 712 713 BMAP_LOCK(bmp); 714 715 /* determine the average number of free blocks within the ags. */ 716 avgfree = (u32)bmp->db_nfree / bmp->db_numag; 717 718 /* 719 * if the current preferred ag does not have an active allocator 720 * and has at least average freespace, return it 721 */ 722 agpref = bmp->db_agpref; 723 if ((atomic_read(&bmp->db_active[agpref]) == 0) && 724 (bmp->db_agfree[agpref] >= avgfree)) 725 goto unlock; 726 727 /* From the last preferred ag, find the next one with at least 728 * average free space. 729 */ 730 for (i = 0 ; i < bmp->db_numag; i++, agpref++) { 731 if (agpref >= bmp->db_numag) 732 agpref = 0; 733 734 if (atomic_read(&bmp->db_active[agpref])) 735 /* open file is currently growing in this ag */ 736 continue; 737 if (bmp->db_agfree[agpref] >= avgfree) { 738 /* Return this one */ 739 bmp->db_agpref = agpref; 740 goto unlock; 741 } else if (bmp->db_agfree[agpref] > hwm) { 742 /* Less than avg. freespace, but best so far */ 743 hwm = bmp->db_agfree[agpref]; 744 next_best = agpref; 745 } 746 } 747 748 /* 749 * If no inactive ag was found with average freespace, use the 750 * next best 751 */ 752 if (next_best != -1) 753 bmp->db_agpref = next_best; 754 /* else leave db_agpref unchanged */ 755 unlock: 756 BMAP_UNLOCK(bmp); 757 758 /* return the preferred group. 759 */ 760 return (bmp->db_agpref); 761 } 762 763 /* 764 * NAME: dbAlloc() 765 * 766 * FUNCTION: attempt to allocate a specified number of contiguous free 767 * blocks from the working allocation block map. 768 * 769 * the block allocation policy uses hints and a multi-step 770 * approach. 771 * 772 * for allocation requests smaller than the number of blocks 773 * per dmap, we first try to allocate the new blocks 774 * immediately following the hint. if these blocks are not 775 * available, we try to allocate blocks near the hint. if 776 * no blocks near the hint are available, we next try to 777 * allocate within the same dmap as contains the hint. 778 * 779 * if no blocks are available in the dmap or the allocation 780 * request is larger than the dmap size, we try to allocate 781 * within the same allocation group as contains the hint. if 782 * this does not succeed, we finally try to allocate anywhere 783 * within the aggregate. 784 * 785 * we also try to allocate anywhere within the aggregate 786 * for allocation requests larger than the allocation group 787 * size or requests that specify no hint value. 788 * 789 * PARAMETERS: 790 * ip - pointer to in-core inode; 791 * hint - allocation hint. 792 * nblocks - number of contiguous blocks in the range. 793 * results - on successful return, set to the starting block number 794 * of the newly allocated contiguous range. 795 * 796 * RETURN VALUES: 797 * 0 - success 798 * -ENOSPC - insufficient disk resources 799 * -EIO - i/o error 800 */ 801 int dbAlloc(struct inode *ip, s64 hint, s64 nblocks, s64 * results) 802 { 803 int rc, agno; 804 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; 805 struct bmap *bmp; 806 struct metapage *mp; 807 s64 lblkno, blkno; 808 struct dmap *dp; 809 int l2nb; 810 s64 mapSize; 811 int writers; 812 813 /* assert that nblocks is valid */ 814 assert(nblocks > 0); 815 816 /* get the log2 number of blocks to be allocated. 817 * if the number of blocks is not a log2 multiple, 818 * it will be rounded up to the next log2 multiple. 819 */ 820 l2nb = BLKSTOL2(nblocks); 821 822 bmp = JFS_SBI(ip->i_sb)->bmap; 823 824 mapSize = bmp->db_mapsize; 825 826 /* the hint should be within the map */ 827 if (hint >= mapSize) { 828 jfs_error(ip->i_sb, "the hint is outside the map\n"); 829 return -EIO; 830 } 831 832 /* if the number of blocks to be allocated is greater than the 833 * allocation group size, try to allocate anywhere. 834 */ 835 if (l2nb > bmp->db_agl2size) { 836 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP); 837 838 rc = dbAllocAny(bmp, nblocks, l2nb, results); 839 840 goto write_unlock; 841 } 842 843 /* 844 * If no hint, let dbNextAG recommend an allocation group 845 */ 846 if (hint == 0) 847 goto pref_ag; 848 849 /* we would like to allocate close to the hint. adjust the 850 * hint to the block following the hint since the allocators 851 * will start looking for free space starting at this point. 852 */ 853 blkno = hint + 1; 854 855 if (blkno >= bmp->db_mapsize) 856 goto pref_ag; 857 858 agno = blkno >> bmp->db_agl2size; 859 860 /* check if blkno crosses over into a new allocation group. 861 * if so, check if we should allow allocations within this 862 * allocation group. 863 */ 864 if ((blkno & (bmp->db_agsize - 1)) == 0) 865 /* check if the AG is currently being written to. 866 * if so, call dbNextAG() to find a non-busy 867 * AG with sufficient free space. 868 */ 869 if (atomic_read(&bmp->db_active[agno])) 870 goto pref_ag; 871 872 /* check if the allocation request size can be satisfied from a 873 * single dmap. if so, try to allocate from the dmap containing 874 * the hint using a tiered strategy. 875 */ 876 if (nblocks <= BPERDMAP) { 877 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP); 878 879 /* get the buffer for the dmap containing the hint. 880 */ 881 rc = -EIO; 882 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); 883 mp = read_metapage(ipbmap, lblkno, PSIZE, 0); 884 if (mp == NULL) 885 goto read_unlock; 886 887 dp = (struct dmap *) mp->data; 888 889 /* first, try to satisfy the allocation request with the 890 * blocks beginning at the hint. 891 */ 892 if ((rc = dbAllocNext(bmp, dp, blkno, (int) nblocks)) 893 != -ENOSPC) { 894 if (rc == 0) { 895 *results = blkno; 896 mark_metapage_dirty(mp); 897 } 898 899 release_metapage(mp); 900 goto read_unlock; 901 } 902 903 writers = atomic_read(&bmp->db_active[agno]); 904 if ((writers > 1) || 905 ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) { 906 /* 907 * Someone else is writing in this allocation 908 * group. To avoid fragmenting, try another ag 909 */ 910 release_metapage(mp); 911 IREAD_UNLOCK(ipbmap); 912 goto pref_ag; 913 } 914 915 /* next, try to satisfy the allocation request with blocks 916 * near the hint. 917 */ 918 if ((rc = 919 dbAllocNear(bmp, dp, blkno, (int) nblocks, l2nb, results)) 920 != -ENOSPC) { 921 if (rc == 0) 922 mark_metapage_dirty(mp); 923 924 release_metapage(mp); 925 goto read_unlock; 926 } 927 928 /* try to satisfy the allocation request with blocks within 929 * the same dmap as the hint. 930 */ 931 if ((rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results)) 932 != -ENOSPC) { 933 if (rc == 0) 934 mark_metapage_dirty(mp); 935 936 release_metapage(mp); 937 goto read_unlock; 938 } 939 940 release_metapage(mp); 941 IREAD_UNLOCK(ipbmap); 942 } 943 944 /* try to satisfy the allocation request with blocks within 945 * the same allocation group as the hint. 946 */ 947 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP); 948 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC) 949 goto write_unlock; 950 951 IWRITE_UNLOCK(ipbmap); 952 953 954 pref_ag: 955 /* 956 * Let dbNextAG recommend a preferred allocation group 957 */ 958 agno = dbNextAG(ipbmap); 959 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP); 960 961 /* Try to allocate within this allocation group. if that fails, try to 962 * allocate anywhere in the map. 963 */ 964 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC) 965 rc = dbAllocAny(bmp, nblocks, l2nb, results); 966 967 write_unlock: 968 IWRITE_UNLOCK(ipbmap); 969 970 return (rc); 971 972 read_unlock: 973 IREAD_UNLOCK(ipbmap); 974 975 return (rc); 976 } 977 978 /* 979 * NAME: dbReAlloc() 980 * 981 * FUNCTION: attempt to extend a current allocation by a specified 982 * number of blocks. 983 * 984 * this routine attempts to satisfy the allocation request 985 * by first trying to extend the existing allocation in 986 * place by allocating the additional blocks as the blocks 987 * immediately following the current allocation. if these 988 * blocks are not available, this routine will attempt to 989 * allocate a new set of contiguous blocks large enough 990 * to cover the existing allocation plus the additional 991 * number of blocks required. 992 * 993 * PARAMETERS: 994 * ip - pointer to in-core inode requiring allocation. 995 * blkno - starting block of the current allocation. 996 * nblocks - number of contiguous blocks within the current 997 * allocation. 998 * addnblocks - number of blocks to add to the allocation. 999 * results - on successful return, set to the starting block number 1000 * of the existing allocation if the existing allocation 1001 * was extended in place or to a newly allocated contiguous 1002 * range if the existing allocation could not be extended 1003 * in place. 1004 * 1005 * RETURN VALUES: 1006 * 0 - success 1007 * -ENOSPC - insufficient disk resources 1008 * -EIO - i/o error 1009 */ 1010 int 1011 dbReAlloc(struct inode *ip, 1012 s64 blkno, s64 nblocks, s64 addnblocks, s64 * results) 1013 { 1014 int rc; 1015 1016 /* try to extend the allocation in place. 1017 */ 1018 if ((rc = dbExtend(ip, blkno, nblocks, addnblocks)) == 0) { 1019 *results = blkno; 1020 return (0); 1021 } else { 1022 if (rc != -ENOSPC) 1023 return (rc); 1024 } 1025 1026 /* could not extend the allocation in place, so allocate a 1027 * new set of blocks for the entire request (i.e. try to get 1028 * a range of contiguous blocks large enough to cover the 1029 * existing allocation plus the additional blocks.) 1030 */ 1031 return (dbAlloc 1032 (ip, blkno + nblocks - 1, addnblocks + nblocks, results)); 1033 } 1034 1035 1036 /* 1037 * NAME: dbExtend() 1038 * 1039 * FUNCTION: attempt to extend a current allocation by a specified 1040 * number of blocks. 1041 * 1042 * this routine attempts to satisfy the allocation request 1043 * by first trying to extend the existing allocation in 1044 * place by allocating the additional blocks as the blocks 1045 * immediately following the current allocation. 1046 * 1047 * PARAMETERS: 1048 * ip - pointer to in-core inode requiring allocation. 1049 * blkno - starting block of the current allocation. 1050 * nblocks - number of contiguous blocks within the current 1051 * allocation. 1052 * addnblocks - number of blocks to add to the allocation. 1053 * 1054 * RETURN VALUES: 1055 * 0 - success 1056 * -ENOSPC - insufficient disk resources 1057 * -EIO - i/o error 1058 */ 1059 static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks) 1060 { 1061 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb); 1062 s64 lblkno, lastblkno, extblkno; 1063 uint rel_block; 1064 struct metapage *mp; 1065 struct dmap *dp; 1066 int rc; 1067 struct inode *ipbmap = sbi->ipbmap; 1068 struct bmap *bmp; 1069 1070 /* 1071 * We don't want a non-aligned extent to cross a page boundary 1072 */ 1073 if (((rel_block = blkno & (sbi->nbperpage - 1))) && 1074 (rel_block + nblocks + addnblocks > sbi->nbperpage)) 1075 return -ENOSPC; 1076 1077 /* get the last block of the current allocation */ 1078 lastblkno = blkno + nblocks - 1; 1079 1080 /* determine the block number of the block following 1081 * the existing allocation. 1082 */ 1083 extblkno = lastblkno + 1; 1084 1085 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP); 1086 1087 /* better be within the file system */ 1088 bmp = sbi->bmap; 1089 if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) { 1090 IREAD_UNLOCK(ipbmap); 1091 jfs_error(ip->i_sb, "the block is outside the filesystem\n"); 1092 return -EIO; 1093 } 1094 1095 /* we'll attempt to extend the current allocation in place by 1096 * allocating the additional blocks as the blocks immediately 1097 * following the current allocation. we only try to extend the 1098 * current allocation in place if the number of additional blocks 1099 * can fit into a dmap, the last block of the current allocation 1100 * is not the last block of the file system, and the start of the 1101 * inplace extension is not on an allocation group boundary. 1102 */ 1103 if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize || 1104 (extblkno & (bmp->db_agsize - 1)) == 0) { 1105 IREAD_UNLOCK(ipbmap); 1106 return -ENOSPC; 1107 } 1108 1109 /* get the buffer for the dmap containing the first block 1110 * of the extension. 1111 */ 1112 lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage); 1113 mp = read_metapage(ipbmap, lblkno, PSIZE, 0); 1114 if (mp == NULL) { 1115 IREAD_UNLOCK(ipbmap); 1116 return -EIO; 1117 } 1118 1119 dp = (struct dmap *) mp->data; 1120 1121 /* try to allocate the blocks immediately following the 1122 * current allocation. 1123 */ 1124 rc = dbAllocNext(bmp, dp, extblkno, (int) addnblocks); 1125 1126 IREAD_UNLOCK(ipbmap); 1127 1128 /* were we successful ? */ 1129 if (rc == 0) 1130 write_metapage(mp); 1131 else 1132 /* we were not successful */ 1133 release_metapage(mp); 1134 1135 return (rc); 1136 } 1137 1138 1139 /* 1140 * NAME: dbAllocNext() 1141 * 1142 * FUNCTION: attempt to allocate the blocks of the specified block 1143 * range within a dmap. 1144 * 1145 * PARAMETERS: 1146 * bmp - pointer to bmap descriptor 1147 * dp - pointer to dmap. 1148 * blkno - starting block number of the range. 1149 * nblocks - number of contiguous free blocks of the range. 1150 * 1151 * RETURN VALUES: 1152 * 0 - success 1153 * -ENOSPC - insufficient disk resources 1154 * -EIO - i/o error 1155 * 1156 * serialization: IREAD_LOCK(ipbmap) held on entry/exit; 1157 */ 1158 static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno, 1159 int nblocks) 1160 { 1161 int dbitno, word, rembits, nb, nwords, wbitno, nw; 1162 int l2size; 1163 s8 *leaf; 1164 u32 mask; 1165 1166 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) { 1167 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n"); 1168 return -EIO; 1169 } 1170 1171 /* pick up a pointer to the leaves of the dmap tree. 1172 */ 1173 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx); 1174 1175 /* determine the bit number and word within the dmap of the 1176 * starting block. 1177 */ 1178 dbitno = blkno & (BPERDMAP - 1); 1179 word = dbitno >> L2DBWORD; 1180 1181 /* check if the specified block range is contained within 1182 * this dmap. 1183 */ 1184 if (dbitno + nblocks > BPERDMAP) 1185 return -ENOSPC; 1186 1187 /* check if the starting leaf indicates that anything 1188 * is free. 1189 */ 1190 if (leaf[word] == NOFREE) 1191 return -ENOSPC; 1192 1193 /* check the dmaps words corresponding to block range to see 1194 * if the block range is free. not all bits of the first and 1195 * last words may be contained within the block range. if this 1196 * is the case, we'll work against those words (i.e. partial first 1197 * and/or last) on an individual basis (a single pass) and examine 1198 * the actual bits to determine if they are free. a single pass 1199 * will be used for all dmap words fully contained within the 1200 * specified range. within this pass, the leaves of the dmap 1201 * tree will be examined to determine if the blocks are free. a 1202 * single leaf may describe the free space of multiple dmap 1203 * words, so we may visit only a subset of the actual leaves 1204 * corresponding to the dmap words of the block range. 1205 */ 1206 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { 1207 /* determine the bit number within the word and 1208 * the number of bits within the word. 1209 */ 1210 wbitno = dbitno & (DBWORD - 1); 1211 nb = min(rembits, DBWORD - wbitno); 1212 1213 /* check if only part of the word is to be examined. 1214 */ 1215 if (nb < DBWORD) { 1216 /* check if the bits are free. 1217 */ 1218 mask = (ONES << (DBWORD - nb) >> wbitno); 1219 if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask) 1220 return -ENOSPC; 1221 1222 word += 1; 1223 } else { 1224 /* one or more dmap words are fully contained 1225 * within the block range. determine how many 1226 * words and how many bits. 1227 */ 1228 nwords = rembits >> L2DBWORD; 1229 nb = nwords << L2DBWORD; 1230 1231 /* now examine the appropriate leaves to determine 1232 * if the blocks are free. 1233 */ 1234 while (nwords > 0) { 1235 /* does the leaf describe any free space ? 1236 */ 1237 if (leaf[word] < BUDMIN) 1238 return -ENOSPC; 1239 1240 /* determine the l2 number of bits provided 1241 * by this leaf. 1242 */ 1243 l2size = 1244 min_t(int, leaf[word], NLSTOL2BSZ(nwords)); 1245 1246 /* determine how many words were handled. 1247 */ 1248 nw = BUDSIZE(l2size, BUDMIN); 1249 1250 nwords -= nw; 1251 word += nw; 1252 } 1253 } 1254 } 1255 1256 /* allocate the blocks. 1257 */ 1258 return (dbAllocDmap(bmp, dp, blkno, nblocks)); 1259 } 1260 1261 1262 /* 1263 * NAME: dbAllocNear() 1264 * 1265 * FUNCTION: attempt to allocate a number of contiguous free blocks near 1266 * a specified block (hint) within a dmap. 1267 * 1268 * starting with the dmap leaf that covers the hint, we'll 1269 * check the next four contiguous leaves for sufficient free 1270 * space. if sufficient free space is found, we'll allocate 1271 * the desired free space. 1272 * 1273 * PARAMETERS: 1274 * bmp - pointer to bmap descriptor 1275 * dp - pointer to dmap. 1276 * blkno - block number to allocate near. 1277 * nblocks - actual number of contiguous free blocks desired. 1278 * l2nb - log2 number of contiguous free blocks desired. 1279 * results - on successful return, set to the starting block number 1280 * of the newly allocated range. 1281 * 1282 * RETURN VALUES: 1283 * 0 - success 1284 * -ENOSPC - insufficient disk resources 1285 * -EIO - i/o error 1286 * 1287 * serialization: IREAD_LOCK(ipbmap) held on entry/exit; 1288 */ 1289 static int 1290 dbAllocNear(struct bmap * bmp, 1291 struct dmap * dp, s64 blkno, int nblocks, int l2nb, s64 * results) 1292 { 1293 int word, lword, rc; 1294 s8 *leaf; 1295 1296 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) { 1297 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmap page\n"); 1298 return -EIO; 1299 } 1300 1301 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx); 1302 1303 /* determine the word within the dmap that holds the hint 1304 * (i.e. blkno). also, determine the last word in the dmap 1305 * that we'll include in our examination. 1306 */ 1307 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD; 1308 lword = min(word + 4, LPERDMAP); 1309 1310 /* examine the leaves for sufficient free space. 1311 */ 1312 for (; word < lword; word++) { 1313 /* does the leaf describe sufficient free space ? 1314 */ 1315 if (leaf[word] < l2nb) 1316 continue; 1317 1318 /* determine the block number within the file system 1319 * of the first block described by this dmap word. 1320 */ 1321 blkno = le64_to_cpu(dp->start) + (word << L2DBWORD); 1322 1323 /* if not all bits of the dmap word are free, get the 1324 * starting bit number within the dmap word of the required 1325 * string of free bits and adjust the block number with the 1326 * value. 1327 */ 1328 if (leaf[word] < BUDMIN) 1329 blkno += 1330 dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb); 1331 1332 /* allocate the blocks. 1333 */ 1334 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0) 1335 *results = blkno; 1336 1337 return (rc); 1338 } 1339 1340 return -ENOSPC; 1341 } 1342 1343 1344 /* 1345 * NAME: dbAllocAG() 1346 * 1347 * FUNCTION: attempt to allocate the specified number of contiguous 1348 * free blocks within the specified allocation group. 1349 * 1350 * unless the allocation group size is equal to the number 1351 * of blocks per dmap, the dmap control pages will be used to 1352 * find the required free space, if available. we start the 1353 * search at the highest dmap control page level which 1354 * distinctly describes the allocation group's free space 1355 * (i.e. the highest level at which the allocation group's 1356 * free space is not mixed in with that of any other group). 1357 * in addition, we start the search within this level at a 1358 * height of the dmapctl dmtree at which the nodes distinctly 1359 * describe the allocation group's free space. at this height, 1360 * the allocation group's free space may be represented by 1 1361 * or two sub-trees, depending on the allocation group size. 1362 * we search the top nodes of these subtrees left to right for 1363 * sufficient free space. if sufficient free space is found, 1364 * the subtree is searched to find the leftmost leaf that 1365 * has free space. once we have made it to the leaf, we 1366 * move the search to the next lower level dmap control page 1367 * corresponding to this leaf. we continue down the dmap control 1368 * pages until we find the dmap that contains or starts the 1369 * sufficient free space and we allocate at this dmap. 1370 * 1371 * if the allocation group size is equal to the dmap size, 1372 * we'll start at the dmap corresponding to the allocation 1373 * group and attempt the allocation at this level. 1374 * 1375 * the dmap control page search is also not performed if the 1376 * allocation group is completely free and we go to the first 1377 * dmap of the allocation group to do the allocation. this is 1378 * done because the allocation group may be part (not the first 1379 * part) of a larger binary buddy system, causing the dmap 1380 * control pages to indicate no free space (NOFREE) within 1381 * the allocation group. 1382 * 1383 * PARAMETERS: 1384 * bmp - pointer to bmap descriptor 1385 * agno - allocation group number. 1386 * nblocks - actual number of contiguous free blocks desired. 1387 * l2nb - log2 number of contiguous free blocks desired. 1388 * results - on successful return, set to the starting block number 1389 * of the newly allocated range. 1390 * 1391 * RETURN VALUES: 1392 * 0 - success 1393 * -ENOSPC - insufficient disk resources 1394 * -EIO - i/o error 1395 * 1396 * note: IWRITE_LOCK(ipmap) held on entry/exit; 1397 */ 1398 static int 1399 dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb, s64 * results) 1400 { 1401 struct metapage *mp; 1402 struct dmapctl *dcp; 1403 int rc, ti, i, k, m, n, agperlev; 1404 s64 blkno, lblkno; 1405 int budmin; 1406 1407 /* allocation request should not be for more than the 1408 * allocation group size. 1409 */ 1410 if (l2nb > bmp->db_agl2size) { 1411 jfs_error(bmp->db_ipbmap->i_sb, 1412 "allocation request is larger than the allocation group size\n"); 1413 return -EIO; 1414 } 1415 1416 /* determine the starting block number of the allocation 1417 * group. 1418 */ 1419 blkno = (s64) agno << bmp->db_agl2size; 1420 1421 /* check if the allocation group size is the minimum allocation 1422 * group size or if the allocation group is completely free. if 1423 * the allocation group size is the minimum size of BPERDMAP (i.e. 1424 * 1 dmap), there is no need to search the dmap control page (below) 1425 * that fully describes the allocation group since the allocation 1426 * group is already fully described by a dmap. in this case, we 1427 * just call dbAllocCtl() to search the dmap tree and allocate the 1428 * required space if available. 1429 * 1430 * if the allocation group is completely free, dbAllocCtl() is 1431 * also called to allocate the required space. this is done for 1432 * two reasons. first, it makes no sense searching the dmap control 1433 * pages for free space when we know that free space exists. second, 1434 * the dmap control pages may indicate that the allocation group 1435 * has no free space if the allocation group is part (not the first 1436 * part) of a larger binary buddy system. 1437 */ 1438 if (bmp->db_agsize == BPERDMAP 1439 || bmp->db_agfree[agno] == bmp->db_agsize) { 1440 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results); 1441 if ((rc == -ENOSPC) && 1442 (bmp->db_agfree[agno] == bmp->db_agsize)) { 1443 printk(KERN_ERR "blkno = %Lx, blocks = %Lx\n", 1444 (unsigned long long) blkno, 1445 (unsigned long long) nblocks); 1446 jfs_error(bmp->db_ipbmap->i_sb, 1447 "dbAllocCtl failed in free AG\n"); 1448 } 1449 return (rc); 1450 } 1451 1452 /* the buffer for the dmap control page that fully describes the 1453 * allocation group. 1454 */ 1455 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel); 1456 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); 1457 if (mp == NULL) 1458 return -EIO; 1459 dcp = (struct dmapctl *) mp->data; 1460 budmin = dcp->budmin; 1461 1462 if (unlikely(!check_dmapctl(dcp))) { 1463 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n"); 1464 release_metapage(mp); 1465 return -EIO; 1466 } 1467 1468 /* search the subtree(s) of the dmap control page that describes 1469 * the allocation group, looking for sufficient free space. to begin, 1470 * determine how many allocation groups are represented in a dmap 1471 * control page at the control page level (i.e. L0, L1, L2) that 1472 * fully describes an allocation group. next, determine the starting 1473 * tree index of this allocation group within the control page. 1474 */ 1475 agperlev = 1476 (1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth; 1477 ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1)); 1478 1479 if (ti < 0 || ti >= le32_to_cpu(dcp->nleafs)) { 1480 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n"); 1481 release_metapage(mp); 1482 return -EIO; 1483 } 1484 1485 /* dmap control page trees fan-out by 4 and a single allocation 1486 * group may be described by 1 or 2 subtrees within the ag level 1487 * dmap control page, depending upon the ag size. examine the ag's 1488 * subtrees for sufficient free space, starting with the leftmost 1489 * subtree. 1490 */ 1491 for (i = 0; i < bmp->db_agwidth; i++, ti++) { 1492 /* is there sufficient free space ? 1493 */ 1494 if (l2nb > dcp->stree[ti]) 1495 continue; 1496 1497 /* sufficient free space found in a subtree. now search down 1498 * the subtree to find the leftmost leaf that describes this 1499 * free space. 1500 */ 1501 for (k = bmp->db_agheight; k > 0; k--) { 1502 for (n = 0, m = (ti << 2) + 1; n < 4; n++) { 1503 if (l2nb <= dcp->stree[m + n]) { 1504 ti = m + n; 1505 break; 1506 } 1507 } 1508 if (n == 4) { 1509 jfs_error(bmp->db_ipbmap->i_sb, 1510 "failed descending stree\n"); 1511 release_metapage(mp); 1512 return -EIO; 1513 } 1514 } 1515 1516 /* determine the block number within the file system 1517 * that corresponds to this leaf. 1518 */ 1519 if (bmp->db_aglevel == 2) 1520 blkno = 0; 1521 else if (bmp->db_aglevel == 1) 1522 blkno &= ~(MAXL1SIZE - 1); 1523 else /* bmp->db_aglevel == 0 */ 1524 blkno &= ~(MAXL0SIZE - 1); 1525 1526 blkno += 1527 ((s64) (ti - le32_to_cpu(dcp->leafidx))) << budmin; 1528 1529 /* release the buffer in preparation for going down 1530 * the next level of dmap control pages. 1531 */ 1532 release_metapage(mp); 1533 1534 /* check if we need to continue to search down the lower 1535 * level dmap control pages. we need to if the number of 1536 * blocks required is less than maximum number of blocks 1537 * described at the next lower level. 1538 */ 1539 if (l2nb < budmin) { 1540 1541 /* search the lower level dmap control pages to get 1542 * the starting block number of the dmap that 1543 * contains or starts off the free space. 1544 */ 1545 if ((rc = 1546 dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1, 1547 &blkno))) { 1548 if (rc == -ENOSPC) { 1549 jfs_error(bmp->db_ipbmap->i_sb, 1550 "control page inconsistent\n"); 1551 return -EIO; 1552 } 1553 return (rc); 1554 } 1555 } 1556 1557 /* allocate the blocks. 1558 */ 1559 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results); 1560 if (rc == -ENOSPC) { 1561 jfs_error(bmp->db_ipbmap->i_sb, 1562 "unable to allocate blocks\n"); 1563 rc = -EIO; 1564 } 1565 return (rc); 1566 } 1567 1568 /* no space in the allocation group. release the buffer and 1569 * return -ENOSPC. 1570 */ 1571 release_metapage(mp); 1572 1573 return -ENOSPC; 1574 } 1575 1576 1577 /* 1578 * NAME: dbAllocAny() 1579 * 1580 * FUNCTION: attempt to allocate the specified number of contiguous 1581 * free blocks anywhere in the file system. 1582 * 1583 * dbAllocAny() attempts to find the sufficient free space by 1584 * searching down the dmap control pages, starting with the 1585 * highest level (i.e. L0, L1, L2) control page. if free space 1586 * large enough to satisfy the desired free space is found, the 1587 * desired free space is allocated. 1588 * 1589 * PARAMETERS: 1590 * bmp - pointer to bmap descriptor 1591 * nblocks - actual number of contiguous free blocks desired. 1592 * l2nb - log2 number of contiguous free blocks desired. 1593 * results - on successful return, set to the starting block number 1594 * of the newly allocated range. 1595 * 1596 * RETURN VALUES: 1597 * 0 - success 1598 * -ENOSPC - insufficient disk resources 1599 * -EIO - i/o error 1600 * 1601 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit; 1602 */ 1603 static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results) 1604 { 1605 int rc; 1606 s64 blkno = 0; 1607 1608 /* starting with the top level dmap control page, search 1609 * down the dmap control levels for sufficient free space. 1610 * if free space is found, dbFindCtl() returns the starting 1611 * block number of the dmap that contains or starts off the 1612 * range of free space. 1613 */ 1614 if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno))) 1615 return (rc); 1616 1617 /* allocate the blocks. 1618 */ 1619 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results); 1620 if (rc == -ENOSPC) { 1621 jfs_error(bmp->db_ipbmap->i_sb, "unable to allocate blocks\n"); 1622 return -EIO; 1623 } 1624 return (rc); 1625 } 1626 1627 1628 /* 1629 * NAME: dbDiscardAG() 1630 * 1631 * FUNCTION: attempt to discard (TRIM) all free blocks of specific AG 1632 * 1633 * algorithm: 1634 * 1) allocate blocks, as large as possible and save them 1635 * while holding IWRITE_LOCK on ipbmap 1636 * 2) trim all these saved block/length values 1637 * 3) mark the blocks free again 1638 * 1639 * benefit: 1640 * - we work only on one ag at some time, minimizing how long we 1641 * need to lock ipbmap 1642 * - reading / writing the fs is possible most time, even on 1643 * trimming 1644 * 1645 * downside: 1646 * - we write two times to the dmapctl and dmap pages 1647 * - but for me, this seems the best way, better ideas? 1648 * /TR 2012 1649 * 1650 * PARAMETERS: 1651 * ip - pointer to in-core inode 1652 * agno - ag to trim 1653 * minlen - minimum value of contiguous blocks 1654 * 1655 * RETURN VALUES: 1656 * s64 - actual number of blocks trimmed 1657 */ 1658 s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen) 1659 { 1660 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; 1661 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; 1662 s64 nblocks, blkno; 1663 u64 trimmed = 0; 1664 int rc, l2nb; 1665 struct super_block *sb = ipbmap->i_sb; 1666 1667 struct range2trim { 1668 u64 blkno; 1669 u64 nblocks; 1670 } *totrim, *tt; 1671 1672 /* max blkno / nblocks pairs to trim */ 1673 int count = 0, range_cnt; 1674 u64 max_ranges; 1675 1676 /* prevent others from writing new stuff here, while trimming */ 1677 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP); 1678 1679 nblocks = bmp->db_agfree[agno]; 1680 max_ranges = nblocks; 1681 do_div(max_ranges, minlen); 1682 range_cnt = min_t(u64, max_ranges + 1, 32 * 1024); 1683 totrim = kmalloc_objs(struct range2trim, range_cnt, GFP_NOFS); 1684 if (totrim == NULL) { 1685 jfs_error(bmp->db_ipbmap->i_sb, "no memory for trim array\n"); 1686 IWRITE_UNLOCK(ipbmap); 1687 return 0; 1688 } 1689 1690 tt = totrim; 1691 while (nblocks >= minlen) { 1692 l2nb = BLKSTOL2(nblocks); 1693 1694 /* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */ 1695 rc = dbAllocAG(bmp, agno, nblocks, l2nb, &blkno); 1696 if (rc == 0) { 1697 tt->blkno = blkno; 1698 tt->nblocks = nblocks; 1699 tt++; count++; 1700 1701 /* the whole ag is free, trim now */ 1702 if (bmp->db_agfree[agno] == 0) 1703 break; 1704 1705 /* give a hint for the next while */ 1706 nblocks = bmp->db_agfree[agno]; 1707 continue; 1708 } else if (rc == -ENOSPC) { 1709 /* search for next smaller log2 block */ 1710 l2nb = BLKSTOL2(nblocks) - 1; 1711 if (unlikely(l2nb < 0)) 1712 break; 1713 nblocks = 1LL << l2nb; 1714 } else { 1715 /* Trim any already allocated blocks */ 1716 jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n"); 1717 break; 1718 } 1719 1720 /* check, if our trim array is full */ 1721 if (unlikely(count >= range_cnt - 1)) 1722 break; 1723 } 1724 IWRITE_UNLOCK(ipbmap); 1725 1726 tt->nblocks = 0; /* mark the current end */ 1727 for (tt = totrim; tt->nblocks != 0; tt++) { 1728 /* when mounted with online discard, dbFree() will 1729 * call jfs_issue_discard() itself */ 1730 if (!(JFS_SBI(sb)->flag & JFS_DISCARD)) 1731 jfs_issue_discard(ip, tt->blkno, tt->nblocks); 1732 dbFree(ip, tt->blkno, tt->nblocks); 1733 trimmed += tt->nblocks; 1734 } 1735 kfree(totrim); 1736 1737 return trimmed; 1738 } 1739 1740 /* 1741 * NAME: dbFindCtl() 1742 * 1743 * FUNCTION: starting at a specified dmap control page level and block 1744 * number, search down the dmap control levels for a range of 1745 * contiguous free blocks large enough to satisfy an allocation 1746 * request for the specified number of free blocks. 1747 * 1748 * if sufficient contiguous free blocks are found, this routine 1749 * returns the starting block number within a dmap page that 1750 * contains or starts a range of contiqious free blocks that 1751 * is sufficient in size. 1752 * 1753 * PARAMETERS: 1754 * bmp - pointer to bmap descriptor 1755 * level - starting dmap control page level. 1756 * l2nb - log2 number of contiguous free blocks desired. 1757 * *blkno - on entry, starting block number for conducting the search. 1758 * on successful return, the first block within a dmap page 1759 * that contains or starts a range of contiguous free blocks. 1760 * 1761 * RETURN VALUES: 1762 * 0 - success 1763 * -ENOSPC - insufficient disk resources 1764 * -EIO - i/o error 1765 * 1766 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit; 1767 */ 1768 static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno) 1769 { 1770 int rc, leafidx, lev; 1771 s64 b, lblkno; 1772 struct dmapctl *dcp; 1773 int budmin; 1774 struct metapage *mp; 1775 1776 /* starting at the specified dmap control page level and block 1777 * number, search down the dmap control levels for the starting 1778 * block number of a dmap page that contains or starts off 1779 * sufficient free blocks. 1780 */ 1781 for (lev = level, b = *blkno; lev >= 0; lev--) { 1782 /* get the buffer of the dmap control page for the block 1783 * number and level (i.e. L0, L1, L2). 1784 */ 1785 lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev); 1786 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); 1787 if (mp == NULL) 1788 return -EIO; 1789 dcp = (struct dmapctl *) mp->data; 1790 budmin = dcp->budmin; 1791 1792 if (unlikely(!check_dmapctl(dcp))) { 1793 jfs_error(bmp->db_ipbmap->i_sb, 1794 "Corrupt dmapctl page\n"); 1795 release_metapage(mp); 1796 return -EIO; 1797 } 1798 1799 /* search the tree within the dmap control page for 1800 * sufficient free space. if sufficient free space is found, 1801 * dbFindLeaf() returns the index of the leaf at which 1802 * free space was found. 1803 */ 1804 rc = dbFindLeaf((dmtree_t *) dcp, l2nb, &leafidx, true); 1805 1806 /* release the buffer. 1807 */ 1808 release_metapage(mp); 1809 1810 /* space found ? 1811 */ 1812 if (rc) { 1813 if (lev != level) { 1814 jfs_error(bmp->db_ipbmap->i_sb, 1815 "dmap inconsistent\n"); 1816 return -EIO; 1817 } 1818 return -ENOSPC; 1819 } 1820 1821 /* adjust the block number to reflect the location within 1822 * the dmap control page (i.e. the leaf) at which free 1823 * space was found. 1824 */ 1825 b += (((s64) leafidx) << budmin); 1826 1827 /* we stop the search at this dmap control page level if 1828 * the number of blocks required is greater than or equal 1829 * to the maximum number of blocks described at the next 1830 * (lower) level. 1831 */ 1832 if (l2nb >= budmin) 1833 break; 1834 } 1835 1836 *blkno = b; 1837 return (0); 1838 } 1839 1840 1841 /* 1842 * NAME: dbAllocCtl() 1843 * 1844 * FUNCTION: attempt to allocate a specified number of contiguous 1845 * blocks starting within a specific dmap. 1846 * 1847 * this routine is called by higher level routines that search 1848 * the dmap control pages above the actual dmaps for contiguous 1849 * free space. the result of successful searches by these 1850 * routines are the starting block numbers within dmaps, with 1851 * the dmaps themselves containing the desired contiguous free 1852 * space or starting a contiguous free space of desired size 1853 * that is made up of the blocks of one or more dmaps. these 1854 * calls should not fail due to insufficent resources. 1855 * 1856 * this routine is called in some cases where it is not known 1857 * whether it will fail due to insufficient resources. more 1858 * specifically, this occurs when allocating from an allocation 1859 * group whose size is equal to the number of blocks per dmap. 1860 * in this case, the dmap control pages are not examined prior 1861 * to calling this routine (to save pathlength) and the call 1862 * might fail. 1863 * 1864 * for a request size that fits within a dmap, this routine relies 1865 * upon the dmap's dmtree to find the requested contiguous free 1866 * space. for request sizes that are larger than a dmap, the 1867 * requested free space will start at the first block of the 1868 * first dmap (i.e. blkno). 1869 * 1870 * PARAMETERS: 1871 * bmp - pointer to bmap descriptor 1872 * nblocks - actual number of contiguous free blocks to allocate. 1873 * l2nb - log2 number of contiguous free blocks to allocate. 1874 * blkno - starting block number of the dmap to start the allocation 1875 * from. 1876 * results - on successful return, set to the starting block number 1877 * of the newly allocated range. 1878 * 1879 * RETURN VALUES: 1880 * 0 - success 1881 * -ENOSPC - insufficient disk resources 1882 * -EIO - i/o error 1883 * 1884 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit; 1885 */ 1886 static int 1887 dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, s64 * results) 1888 { 1889 int rc, nb; 1890 s64 b, lblkno, n; 1891 struct metapage *mp; 1892 struct dmap *dp; 1893 1894 /* check if the allocation request is confined to a single dmap. 1895 */ 1896 if (l2nb <= L2BPERDMAP) { 1897 /* get the buffer for the dmap. 1898 */ 1899 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); 1900 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); 1901 if (mp == NULL) 1902 return -EIO; 1903 dp = (struct dmap *) mp->data; 1904 1905 if (dp->tree.budmin < 0) { 1906 release_metapage(mp); 1907 return -EIO; 1908 } 1909 1910 /* try to allocate the blocks. 1911 */ 1912 rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results); 1913 if (rc == 0) 1914 mark_metapage_dirty(mp); 1915 1916 release_metapage(mp); 1917 1918 return (rc); 1919 } 1920 1921 /* allocation request involving multiple dmaps. it must start on 1922 * a dmap boundary. 1923 */ 1924 assert((blkno & (BPERDMAP - 1)) == 0); 1925 1926 /* allocate the blocks dmap by dmap. 1927 */ 1928 for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) { 1929 /* get the buffer for the dmap. 1930 */ 1931 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); 1932 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); 1933 if (mp == NULL) { 1934 rc = -EIO; 1935 goto backout; 1936 } 1937 dp = (struct dmap *) mp->data; 1938 1939 /* the dmap better be all free. 1940 */ 1941 if (dp->tree.stree[ROOT] != L2BPERDMAP) { 1942 release_metapage(mp); 1943 jfs_error(bmp->db_ipbmap->i_sb, 1944 "the dmap is not all free\n"); 1945 rc = -EIO; 1946 goto backout; 1947 } 1948 1949 /* determine how many blocks to allocate from this dmap. 1950 */ 1951 nb = min_t(s64, n, BPERDMAP); 1952 1953 /* allocate the blocks from the dmap. 1954 */ 1955 if ((rc = dbAllocDmap(bmp, dp, b, nb))) { 1956 release_metapage(mp); 1957 goto backout; 1958 } 1959 1960 /* write the buffer. 1961 */ 1962 write_metapage(mp); 1963 } 1964 1965 /* set the results (starting block number) and return. 1966 */ 1967 *results = blkno; 1968 return (0); 1969 1970 /* something failed in handling an allocation request involving 1971 * multiple dmaps. we'll try to clean up by backing out any 1972 * allocation that has already happened for this request. if 1973 * we fail in backing out the allocation, we'll mark the file 1974 * system to indicate that blocks have been leaked. 1975 */ 1976 backout: 1977 1978 /* try to backout the allocations dmap by dmap. 1979 */ 1980 for (n = nblocks - n, b = blkno; n > 0; 1981 n -= BPERDMAP, b += BPERDMAP) { 1982 /* get the buffer for this dmap. 1983 */ 1984 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage); 1985 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); 1986 if (mp == NULL) { 1987 /* could not back out. mark the file system 1988 * to indicate that we have leaked blocks. 1989 */ 1990 jfs_error(bmp->db_ipbmap->i_sb, 1991 "I/O Error: Block Leakage\n"); 1992 continue; 1993 } 1994 dp = (struct dmap *) mp->data; 1995 1996 /* free the blocks is this dmap. 1997 */ 1998 if (dbFreeDmap(bmp, dp, b, BPERDMAP)) { 1999 /* could not back out. mark the file system 2000 * to indicate that we have leaked blocks. 2001 */ 2002 release_metapage(mp); 2003 jfs_error(bmp->db_ipbmap->i_sb, "Block Leakage\n"); 2004 continue; 2005 } 2006 2007 /* write the buffer. 2008 */ 2009 write_metapage(mp); 2010 } 2011 2012 return (rc); 2013 } 2014 2015 2016 /* 2017 * NAME: dbAllocDmapLev() 2018 * 2019 * FUNCTION: attempt to allocate a specified number of contiguous blocks 2020 * from a specified dmap. 2021 * 2022 * this routine checks if the contiguous blocks are available. 2023 * if so, nblocks of blocks are allocated; otherwise, ENOSPC is 2024 * returned. 2025 * 2026 * PARAMETERS: 2027 * mp - pointer to bmap descriptor 2028 * dp - pointer to dmap to attempt to allocate blocks from. 2029 * l2nb - log2 number of contiguous block desired. 2030 * nblocks - actual number of contiguous block desired. 2031 * results - on successful return, set to the starting block number 2032 * of the newly allocated range. 2033 * 2034 * RETURN VALUES: 2035 * 0 - success 2036 * -ENOSPC - insufficient disk resources 2037 * -EIO - i/o error 2038 * 2039 * serialization: IREAD_LOCK(ipbmap), e.g., from dbAlloc(), or 2040 * IWRITE_LOCK(ipbmap), e.g., dbAllocCtl(), held on entry/exit; 2041 */ 2042 static int 2043 dbAllocDmapLev(struct bmap * bmp, 2044 struct dmap * dp, int nblocks, int l2nb, s64 * results) 2045 { 2046 s64 blkno; 2047 int leafidx, rc; 2048 2049 /* can't be more than a dmaps worth of blocks */ 2050 assert(l2nb <= L2BPERDMAP); 2051 2052 /* search the tree within the dmap page for sufficient 2053 * free space. if sufficient free space is found, dbFindLeaf() 2054 * returns the index of the leaf at which free space was found. 2055 */ 2056 if (dbFindLeaf((dmtree_t *) &dp->tree, l2nb, &leafidx, false)) 2057 return -ENOSPC; 2058 2059 if (leafidx < 0) 2060 return -EIO; 2061 2062 /* determine the block number within the file system corresponding 2063 * to the leaf at which free space was found. 2064 */ 2065 blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD); 2066 2067 /* if not all bits of the dmap word are free, get the starting 2068 * bit number within the dmap word of the required string of free 2069 * bits and adjust the block number with this value. 2070 */ 2071 if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN) 2072 blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb); 2073 2074 /* allocate the blocks */ 2075 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0) 2076 *results = blkno; 2077 2078 return (rc); 2079 } 2080 2081 2082 /* 2083 * NAME: dbAllocDmap() 2084 * 2085 * FUNCTION: adjust the disk allocation map to reflect the allocation 2086 * of a specified block range within a dmap. 2087 * 2088 * this routine allocates the specified blocks from the dmap 2089 * through a call to dbAllocBits(). if the allocation of the 2090 * block range causes the maximum string of free blocks within 2091 * the dmap to change (i.e. the value of the root of the dmap's 2092 * dmtree), this routine will cause this change to be reflected 2093 * up through the appropriate levels of the dmap control pages 2094 * by a call to dbAdjCtl() for the L0 dmap control page that 2095 * covers this dmap. 2096 * 2097 * PARAMETERS: 2098 * bmp - pointer to bmap descriptor 2099 * dp - pointer to dmap to allocate the block range from. 2100 * blkno - starting block number of the block to be allocated. 2101 * nblocks - number of blocks to be allocated. 2102 * 2103 * RETURN VALUES: 2104 * 0 - success 2105 * -EIO - i/o error 2106 * 2107 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit; 2108 */ 2109 static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno, 2110 int nblocks) 2111 { 2112 s8 oldroot; 2113 int rc; 2114 2115 /* save the current value of the root (i.e. maximum free string) 2116 * of the dmap tree. 2117 */ 2118 oldroot = dp->tree.stree[ROOT]; 2119 2120 /* allocate the specified (blocks) bits */ 2121 dbAllocBits(bmp, dp, blkno, nblocks); 2122 2123 /* if the root has not changed, done. */ 2124 if (dp->tree.stree[ROOT] == oldroot) 2125 return (0); 2126 2127 /* root changed. bubble the change up to the dmap control pages. 2128 * if the adjustment of the upper level control pages fails, 2129 * backout the bit allocation (thus making everything consistent). 2130 */ 2131 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0))) 2132 dbFreeBits(bmp, dp, blkno, nblocks); 2133 2134 return (rc); 2135 } 2136 2137 2138 /* 2139 * NAME: dbFreeDmap() 2140 * 2141 * FUNCTION: adjust the disk allocation map to reflect the allocation 2142 * of a specified block range within a dmap. 2143 * 2144 * this routine frees the specified blocks from the dmap through 2145 * a call to dbFreeBits(). if the deallocation of the block range 2146 * causes the maximum string of free blocks within the dmap to 2147 * change (i.e. the value of the root of the dmap's dmtree), this 2148 * routine will cause this change to be reflected up through the 2149 * appropriate levels of the dmap control pages by a call to 2150 * dbAdjCtl() for the L0 dmap control page that covers this dmap. 2151 * 2152 * PARAMETERS: 2153 * bmp - pointer to bmap descriptor 2154 * dp - pointer to dmap to free the block range from. 2155 * blkno - starting block number of the block to be freed. 2156 * nblocks - number of blocks to be freed. 2157 * 2158 * RETURN VALUES: 2159 * 0 - success 2160 * -EIO - i/o error 2161 * 2162 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit; 2163 */ 2164 static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno, 2165 int nblocks) 2166 { 2167 s8 oldroot; 2168 int rc = 0, word; 2169 2170 /* save the current value of the root (i.e. maximum free string) 2171 * of the dmap tree. 2172 */ 2173 oldroot = dp->tree.stree[ROOT]; 2174 2175 /* free the specified (blocks) bits */ 2176 rc = dbFreeBits(bmp, dp, blkno, nblocks); 2177 2178 /* if error or the root has not changed, done. */ 2179 if (rc || (dp->tree.stree[ROOT] == oldroot)) 2180 return (rc); 2181 2182 /* root changed. bubble the change up to the dmap control pages. 2183 * if the adjustment of the upper level control pages fails, 2184 * backout the deallocation. 2185 */ 2186 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) { 2187 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD; 2188 2189 /* as part of backing out the deallocation, we will have 2190 * to back split the dmap tree if the deallocation caused 2191 * the freed blocks to become part of a larger binary buddy 2192 * system. 2193 */ 2194 if (dp->tree.stree[word] == NOFREE) 2195 dbBackSplit((dmtree_t *)&dp->tree, word, false); 2196 2197 dbAllocBits(bmp, dp, blkno, nblocks); 2198 } 2199 2200 return (rc); 2201 } 2202 2203 2204 /* 2205 * NAME: dbAllocBits() 2206 * 2207 * FUNCTION: allocate a specified block range from a dmap. 2208 * 2209 * this routine updates the dmap to reflect the working 2210 * state allocation of the specified block range. it directly 2211 * updates the bits of the working map and causes the adjustment 2212 * of the binary buddy system described by the dmap's dmtree 2213 * leaves to reflect the bits allocated. it also causes the 2214 * dmap's dmtree, as a whole, to reflect the allocated range. 2215 * 2216 * PARAMETERS: 2217 * bmp - pointer to bmap descriptor 2218 * dp - pointer to dmap to allocate bits from. 2219 * blkno - starting block number of the bits to be allocated. 2220 * nblocks - number of bits to be allocated. 2221 * 2222 * RETURN VALUES: none 2223 * 2224 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit; 2225 */ 2226 static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno, 2227 int nblocks) 2228 { 2229 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno; 2230 dmtree_t *tp = (dmtree_t *) & dp->tree; 2231 int size; 2232 s8 *leaf; 2233 2234 /* pick up a pointer to the leaves of the dmap tree */ 2235 leaf = dp->tree.stree + LEAFIND; 2236 2237 /* determine the bit number and word within the dmap of the 2238 * starting block. 2239 */ 2240 dbitno = blkno & (BPERDMAP - 1); 2241 word = dbitno >> L2DBWORD; 2242 2243 /* block range better be within the dmap */ 2244 assert(dbitno + nblocks <= BPERDMAP); 2245 2246 /* allocate the bits of the dmap's words corresponding to the block 2247 * range. not all bits of the first and last words may be contained 2248 * within the block range. if this is the case, we'll work against 2249 * those words (i.e. partial first and/or last) on an individual basis 2250 * (a single pass), allocating the bits of interest by hand and 2251 * updating the leaf corresponding to the dmap word. a single pass 2252 * will be used for all dmap words fully contained within the 2253 * specified range. within this pass, the bits of all fully contained 2254 * dmap words will be marked as free in a single shot and the leaves 2255 * will be updated. a single leaf may describe the free space of 2256 * multiple dmap words, so we may update only a subset of the actual 2257 * leaves corresponding to the dmap words of the block range. 2258 */ 2259 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { 2260 /* determine the bit number within the word and 2261 * the number of bits within the word. 2262 */ 2263 wbitno = dbitno & (DBWORD - 1); 2264 nb = min(rembits, DBWORD - wbitno); 2265 2266 /* check if only part of a word is to be allocated. 2267 */ 2268 if (nb < DBWORD) { 2269 /* allocate (set to 1) the appropriate bits within 2270 * this dmap word. 2271 */ 2272 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) 2273 >> wbitno); 2274 2275 /* update the leaf for this dmap word. in addition 2276 * to setting the leaf value to the binary buddy max 2277 * of the updated dmap word, dbSplit() will split 2278 * the binary system of the leaves if need be. 2279 */ 2280 dbSplit(tp, word, BUDMIN, 2281 dbMaxBud((u8 *)&dp->wmap[word]), false); 2282 2283 word += 1; 2284 } else { 2285 /* one or more dmap words are fully contained 2286 * within the block range. determine how many 2287 * words and allocate (set to 1) the bits of these 2288 * words. 2289 */ 2290 nwords = rembits >> L2DBWORD; 2291 memset(&dp->wmap[word], (int) ONES, nwords * 4); 2292 2293 /* determine how many bits. 2294 */ 2295 nb = nwords << L2DBWORD; 2296 2297 /* now update the appropriate leaves to reflect 2298 * the allocated words. 2299 */ 2300 for (; nwords > 0; nwords -= nw) { 2301 if (leaf[word] < BUDMIN) { 2302 jfs_error(bmp->db_ipbmap->i_sb, 2303 "leaf page corrupt\n"); 2304 break; 2305 } 2306 2307 /* determine what the leaf value should be 2308 * updated to as the minimum of the l2 number 2309 * of bits being allocated and the l2 number 2310 * of bits currently described by this leaf. 2311 */ 2312 size = min_t(int, leaf[word], 2313 NLSTOL2BSZ(nwords)); 2314 2315 /* update the leaf to reflect the allocation. 2316 * in addition to setting the leaf value to 2317 * NOFREE, dbSplit() will split the binary 2318 * system of the leaves to reflect the current 2319 * allocation (size). 2320 */ 2321 dbSplit(tp, word, size, NOFREE, false); 2322 2323 /* get the number of dmap words handled */ 2324 nw = BUDSIZE(size, BUDMIN); 2325 word += nw; 2326 } 2327 } 2328 } 2329 2330 /* update the free count for this dmap */ 2331 le32_add_cpu(&dp->nfree, -nblocks); 2332 2333 BMAP_LOCK(bmp); 2334 2335 /* if this allocation group is completely free, 2336 * update the maximum allocation group number if this allocation 2337 * group is the new max. 2338 */ 2339 agno = blkno >> bmp->db_agl2size; 2340 if (agno > bmp->db_maxag) 2341 bmp->db_maxag = agno; 2342 2343 /* update the free count for the allocation group and map */ 2344 bmp->db_agfree[agno] -= nblocks; 2345 bmp->db_nfree -= nblocks; 2346 2347 BMAP_UNLOCK(bmp); 2348 } 2349 2350 2351 /* 2352 * NAME: dbFreeBits() 2353 * 2354 * FUNCTION: free a specified block range from a dmap. 2355 * 2356 * this routine updates the dmap to reflect the working 2357 * state allocation of the specified block range. it directly 2358 * updates the bits of the working map and causes the adjustment 2359 * of the binary buddy system described by the dmap's dmtree 2360 * leaves to reflect the bits freed. it also causes the dmap's 2361 * dmtree, as a whole, to reflect the deallocated range. 2362 * 2363 * PARAMETERS: 2364 * bmp - pointer to bmap descriptor 2365 * dp - pointer to dmap to free bits from. 2366 * blkno - starting block number of the bits to be freed. 2367 * nblocks - number of bits to be freed. 2368 * 2369 * RETURN VALUES: 0 for success 2370 * 2371 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit; 2372 */ 2373 static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno, 2374 int nblocks) 2375 { 2376 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno; 2377 dmtree_t *tp = (dmtree_t *) & dp->tree; 2378 int rc = 0; 2379 int size; 2380 2381 /* determine the bit number and word within the dmap of the 2382 * starting block. 2383 */ 2384 dbitno = blkno & (BPERDMAP - 1); 2385 word = dbitno >> L2DBWORD; 2386 2387 /* block range better be within the dmap. 2388 */ 2389 assert(dbitno + nblocks <= BPERDMAP); 2390 2391 /* free the bits of the dmaps words corresponding to the block range. 2392 * not all bits of the first and last words may be contained within 2393 * the block range. if this is the case, we'll work against those 2394 * words (i.e. partial first and/or last) on an individual basis 2395 * (a single pass), freeing the bits of interest by hand and updating 2396 * the leaf corresponding to the dmap word. a single pass will be used 2397 * for all dmap words fully contained within the specified range. 2398 * within this pass, the bits of all fully contained dmap words will 2399 * be marked as free in a single shot and the leaves will be updated. a 2400 * single leaf may describe the free space of multiple dmap words, 2401 * so we may update only a subset of the actual leaves corresponding 2402 * to the dmap words of the block range. 2403 * 2404 * dbJoin() is used to update leaf values and will join the binary 2405 * buddy system of the leaves if the new leaf values indicate this 2406 * should be done. 2407 */ 2408 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { 2409 /* determine the bit number within the word and 2410 * the number of bits within the word. 2411 */ 2412 wbitno = dbitno & (DBWORD - 1); 2413 nb = min(rembits, DBWORD - wbitno); 2414 2415 /* check if only part of a word is to be freed. 2416 */ 2417 if (nb < DBWORD) { 2418 /* free (zero) the appropriate bits within this 2419 * dmap word. 2420 */ 2421 dp->wmap[word] &= 2422 cpu_to_le32(~(ONES << (DBWORD - nb) 2423 >> wbitno)); 2424 2425 /* update the leaf for this dmap word. 2426 */ 2427 rc = dbJoin(tp, word, 2428 dbMaxBud((u8 *)&dp->wmap[word]), false); 2429 if (rc) 2430 return rc; 2431 2432 word += 1; 2433 } else { 2434 /* one or more dmap words are fully contained 2435 * within the block range. determine how many 2436 * words and free (zero) the bits of these words. 2437 */ 2438 nwords = rembits >> L2DBWORD; 2439 memset(&dp->wmap[word], 0, nwords * 4); 2440 2441 /* determine how many bits. 2442 */ 2443 nb = nwords << L2DBWORD; 2444 2445 /* now update the appropriate leaves to reflect 2446 * the freed words. 2447 */ 2448 for (; nwords > 0; nwords -= nw) { 2449 /* determine what the leaf value should be 2450 * updated to as the minimum of the l2 number 2451 * of bits being freed and the l2 (max) number 2452 * of bits that can be described by this leaf. 2453 */ 2454 size = 2455 min(LITOL2BSZ 2456 (word, L2LPERDMAP, BUDMIN), 2457 NLSTOL2BSZ(nwords)); 2458 2459 /* update the leaf. 2460 */ 2461 rc = dbJoin(tp, word, size, false); 2462 if (rc) 2463 return rc; 2464 2465 /* get the number of dmap words handled. 2466 */ 2467 nw = BUDSIZE(size, BUDMIN); 2468 word += nw; 2469 } 2470 } 2471 } 2472 2473 /* update the free count for this dmap. 2474 */ 2475 le32_add_cpu(&dp->nfree, nblocks); 2476 2477 BMAP_LOCK(bmp); 2478 2479 /* update the free count for the allocation group and 2480 * map. 2481 */ 2482 agno = blkno >> bmp->db_agl2size; 2483 bmp->db_nfree += nblocks; 2484 bmp->db_agfree[agno] += nblocks; 2485 2486 /* check if this allocation group is not completely free and 2487 * if it is currently the maximum (rightmost) allocation group. 2488 * if so, establish the new maximum allocation group number by 2489 * searching left for the first allocation group with allocation. 2490 */ 2491 if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) || 2492 (agno == bmp->db_numag - 1 && 2493 bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) { 2494 while (bmp->db_maxag > 0) { 2495 bmp->db_maxag -= 1; 2496 if (bmp->db_agfree[bmp->db_maxag] != 2497 bmp->db_agsize) 2498 break; 2499 } 2500 2501 /* re-establish the allocation group preference if the 2502 * current preference is right of the maximum allocation 2503 * group. 2504 */ 2505 if (bmp->db_agpref > bmp->db_maxag) 2506 bmp->db_agpref = bmp->db_maxag; 2507 } 2508 2509 BMAP_UNLOCK(bmp); 2510 2511 return 0; 2512 } 2513 2514 2515 /* 2516 * NAME: dbAdjCtl() 2517 * 2518 * FUNCTION: adjust a dmap control page at a specified level to reflect 2519 * the change in a lower level dmap or dmap control page's 2520 * maximum string of free blocks (i.e. a change in the root 2521 * of the lower level object's dmtree) due to the allocation 2522 * or deallocation of a range of blocks with a single dmap. 2523 * 2524 * on entry, this routine is provided with the new value of 2525 * the lower level dmap or dmap control page root and the 2526 * starting block number of the block range whose allocation 2527 * or deallocation resulted in the root change. this range 2528 * is respresented by a single leaf of the current dmapctl 2529 * and the leaf will be updated with this value, possibly 2530 * causing a binary buddy system within the leaves to be 2531 * split or joined. the update may also cause the dmapctl's 2532 * dmtree to be updated. 2533 * 2534 * if the adjustment of the dmap control page, itself, causes its 2535 * root to change, this change will be bubbled up to the next dmap 2536 * control level by a recursive call to this routine, specifying 2537 * the new root value and the next dmap control page level to 2538 * be adjusted. 2539 * PARAMETERS: 2540 * bmp - pointer to bmap descriptor 2541 * blkno - the first block of a block range within a dmap. it is 2542 * the allocation or deallocation of this block range that 2543 * requires the dmap control page to be adjusted. 2544 * newval - the new value of the lower level dmap or dmap control 2545 * page root. 2546 * alloc - 'true' if adjustment is due to an allocation. 2547 * level - current level of dmap control page (i.e. L0, L1, L2) to 2548 * be adjusted. 2549 * 2550 * RETURN VALUES: 2551 * 0 - success 2552 * -EIO - i/o error 2553 * 2554 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit; 2555 */ 2556 static int 2557 dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc, int level) 2558 { 2559 struct metapage *mp; 2560 s8 oldroot; 2561 int oldval; 2562 s64 lblkno; 2563 struct dmapctl *dcp; 2564 int rc, leafno, ti; 2565 2566 /* get the buffer for the dmap control page for the specified 2567 * block number and control page level. 2568 */ 2569 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level); 2570 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0); 2571 if (mp == NULL) 2572 return -EIO; 2573 dcp = (struct dmapctl *) mp->data; 2574 2575 if (unlikely(!check_dmapctl(dcp))) { 2576 jfs_error(bmp->db_ipbmap->i_sb, "Corrupt dmapctl page\n"); 2577 release_metapage(mp); 2578 return -EIO; 2579 } 2580 2581 /* determine the leaf number corresponding to the block and 2582 * the index within the dmap control tree. 2583 */ 2584 leafno = BLKTOCTLLEAF(blkno, dcp->budmin); 2585 ti = leafno + le32_to_cpu(dcp->leafidx); 2586 2587 /* save the current leaf value and the current root level (i.e. 2588 * maximum l2 free string described by this dmapctl). 2589 */ 2590 oldval = dcp->stree[ti]; 2591 oldroot = dcp->stree[ROOT]; 2592 2593 /* check if this is a control page update for an allocation. 2594 * if so, update the leaf to reflect the new leaf value using 2595 * dbSplit(); otherwise (deallocation), use dbJoin() to update 2596 * the leaf with the new value. in addition to updating the 2597 * leaf, dbSplit() will also split the binary buddy system of 2598 * the leaves, if required, and bubble new values within the 2599 * dmapctl tree, if required. similarly, dbJoin() will join 2600 * the binary buddy system of leaves and bubble new values up 2601 * the dmapctl tree as required by the new leaf value. 2602 */ 2603 if (alloc) { 2604 /* check if we are in the middle of a binary buddy 2605 * system. this happens when we are performing the 2606 * first allocation out of an allocation group that 2607 * is part (not the first part) of a larger binary 2608 * buddy system. if we are in the middle, back split 2609 * the system prior to calling dbSplit() which assumes 2610 * that it is at the front of a binary buddy system. 2611 */ 2612 if (oldval == NOFREE) { 2613 rc = dbBackSplit((dmtree_t *)dcp, leafno, true); 2614 if (rc) { 2615 release_metapage(mp); 2616 return rc; 2617 } 2618 oldval = dcp->stree[ti]; 2619 } 2620 dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval, true); 2621 } else { 2622 rc = dbJoin((dmtree_t *) dcp, leafno, newval, true); 2623 if (rc) { 2624 release_metapage(mp); 2625 return rc; 2626 } 2627 } 2628 2629 /* check if the root of the current dmap control page changed due 2630 * to the update and if the current dmap control page is not at 2631 * the current top level (i.e. L0, L1, L2) of the map. if so (i.e. 2632 * root changed and this is not the top level), call this routine 2633 * again (recursion) for the next higher level of the mapping to 2634 * reflect the change in root for the current dmap control page. 2635 */ 2636 if (dcp->stree[ROOT] != oldroot) { 2637 /* are we below the top level of the map. if so, 2638 * bubble the root up to the next higher level. 2639 */ 2640 if (level < bmp->db_maxlevel) { 2641 /* bubble up the new root of this dmap control page to 2642 * the next level. 2643 */ 2644 if ((rc = 2645 dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc, 2646 level + 1))) { 2647 /* something went wrong in bubbling up the new 2648 * root value, so backout the changes to the 2649 * current dmap control page. 2650 */ 2651 if (alloc) { 2652 dbJoin((dmtree_t *) dcp, leafno, 2653 oldval, true); 2654 } else { 2655 /* the dbJoin() above might have 2656 * caused a larger binary buddy system 2657 * to form and we may now be in the 2658 * middle of it. if this is the case, 2659 * back split the buddies. 2660 */ 2661 if (dcp->stree[ti] == NOFREE) 2662 dbBackSplit((dmtree_t *) 2663 dcp, leafno, true); 2664 dbSplit((dmtree_t *) dcp, leafno, 2665 dcp->budmin, oldval, true); 2666 } 2667 2668 /* release the buffer and return the error. 2669 */ 2670 release_metapage(mp); 2671 return (rc); 2672 } 2673 } else { 2674 /* we're at the top level of the map. update 2675 * the bmap control page to reflect the size 2676 * of the maximum free buddy system. 2677 */ 2678 assert(level == bmp->db_maxlevel); 2679 if (bmp->db_maxfreebud != oldroot) { 2680 jfs_error(bmp->db_ipbmap->i_sb, 2681 "the maximum free buddy is not the old root\n"); 2682 } 2683 bmp->db_maxfreebud = dcp->stree[ROOT]; 2684 } 2685 } 2686 2687 /* write the buffer. 2688 */ 2689 write_metapage(mp); 2690 2691 return (0); 2692 } 2693 2694 2695 /* 2696 * NAME: dbSplit() 2697 * 2698 * FUNCTION: update the leaf of a dmtree with a new value, splitting 2699 * the leaf from the binary buddy system of the dmtree's 2700 * leaves, as required. 2701 * 2702 * PARAMETERS: 2703 * tp - pointer to the tree containing the leaf. 2704 * leafno - the number of the leaf to be updated. 2705 * splitsz - the size the binary buddy system starting at the leaf 2706 * must be split to, specified as the log2 number of blocks. 2707 * newval - the new value for the leaf. 2708 * 2709 * RETURN VALUES: none 2710 * 2711 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit; 2712 */ 2713 static void dbSplit(dmtree_t *tp, int leafno, int splitsz, int newval, bool is_ctl) 2714 { 2715 int budsz; 2716 int cursz; 2717 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); 2718 2719 /* check if the leaf needs to be split. 2720 */ 2721 if (leaf[leafno] > tp->dmt_budmin) { 2722 /* the split occurs by cutting the buddy system in half 2723 * at the specified leaf until we reach the specified 2724 * size. pick up the starting split size (current size 2725 * - 1 in l2) and the corresponding buddy size. 2726 */ 2727 cursz = leaf[leafno] - 1; 2728 budsz = BUDSIZE(cursz, tp->dmt_budmin); 2729 2730 /* split until we reach the specified size. 2731 */ 2732 while (cursz >= splitsz) { 2733 /* update the buddy's leaf with its new value. 2734 */ 2735 dbAdjTree(tp, leafno ^ budsz, cursz, is_ctl); 2736 2737 /* on to the next size and buddy. 2738 */ 2739 cursz -= 1; 2740 budsz >>= 1; 2741 } 2742 } 2743 2744 /* adjust the dmap tree to reflect the specified leaf's new 2745 * value. 2746 */ 2747 dbAdjTree(tp, leafno, newval, is_ctl); 2748 } 2749 2750 2751 /* 2752 * NAME: dbBackSplit() 2753 * 2754 * FUNCTION: back split the binary buddy system of dmtree leaves 2755 * that hold a specified leaf until the specified leaf 2756 * starts its own binary buddy system. 2757 * 2758 * the allocators typically perform allocations at the start 2759 * of binary buddy systems and dbSplit() is used to accomplish 2760 * any required splits. in some cases, however, allocation 2761 * may occur in the middle of a binary system and requires a 2762 * back split, with the split proceeding out from the middle of 2763 * the system (less efficient) rather than the start of the 2764 * system (more efficient). the cases in which a back split 2765 * is required are rare and are limited to the first allocation 2766 * within an allocation group which is a part (not first part) 2767 * of a larger binary buddy system and a few exception cases 2768 * in which a previous join operation must be backed out. 2769 * 2770 * PARAMETERS: 2771 * tp - pointer to the tree containing the leaf. 2772 * leafno - the number of the leaf to be updated. 2773 * 2774 * RETURN VALUES: none 2775 * 2776 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit; 2777 */ 2778 static int dbBackSplit(dmtree_t *tp, int leafno, bool is_ctl) 2779 { 2780 int budsz, bud, w, bsz, size; 2781 int cursz; 2782 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); 2783 2784 /* leaf should be part (not first part) of a binary 2785 * buddy system. 2786 */ 2787 assert(leaf[leafno] == NOFREE); 2788 2789 /* the back split is accomplished by iteratively finding the leaf 2790 * that starts the buddy system that contains the specified leaf and 2791 * splitting that system in two. this iteration continues until 2792 * the specified leaf becomes the start of a buddy system. 2793 * 2794 * determine maximum possible l2 size for the specified leaf. 2795 */ 2796 size = 2797 LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs), 2798 tp->dmt_budmin); 2799 2800 /* determine the number of leaves covered by this size. this 2801 * is the buddy size that we will start with as we search for 2802 * the buddy system that contains the specified leaf. 2803 */ 2804 budsz = BUDSIZE(size, tp->dmt_budmin); 2805 2806 /* back split. 2807 */ 2808 while (leaf[leafno] == NOFREE) { 2809 /* find the leftmost buddy leaf. 2810 */ 2811 for (w = leafno, bsz = budsz;; bsz <<= 1, 2812 w = (w < bud) ? w : bud) { 2813 if (bsz >= le32_to_cpu(tp->dmt_nleafs)) { 2814 jfs_err("JFS: block map error in dbBackSplit"); 2815 return -EIO; 2816 } 2817 2818 /* determine the buddy. 2819 */ 2820 bud = w ^ bsz; 2821 2822 /* check if this buddy is the start of the system. 2823 */ 2824 if (leaf[bud] != NOFREE) { 2825 /* split the leaf at the start of the 2826 * system in two. 2827 */ 2828 cursz = leaf[bud] - 1; 2829 dbSplit(tp, bud, cursz, cursz, is_ctl); 2830 break; 2831 } 2832 } 2833 } 2834 2835 if (leaf[leafno] != size) { 2836 jfs_err("JFS: wrong leaf value in dbBackSplit"); 2837 return -EIO; 2838 } 2839 return 0; 2840 } 2841 2842 2843 /* 2844 * NAME: dbJoin() 2845 * 2846 * FUNCTION: update the leaf of a dmtree with a new value, joining 2847 * the leaf with other leaves of the dmtree into a multi-leaf 2848 * binary buddy system, as required. 2849 * 2850 * PARAMETERS: 2851 * tp - pointer to the tree containing the leaf. 2852 * leafno - the number of the leaf to be updated. 2853 * newval - the new value for the leaf. 2854 * 2855 * RETURN VALUES: none 2856 */ 2857 static int dbJoin(dmtree_t *tp, int leafno, int newval, bool is_ctl) 2858 { 2859 int budsz, buddy; 2860 s8 *leaf; 2861 2862 /* can the new leaf value require a join with other leaves ? 2863 */ 2864 if (newval >= tp->dmt_budmin) { 2865 /* pickup a pointer to the leaves of the tree. 2866 */ 2867 leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx); 2868 2869 /* try to join the specified leaf into a large binary 2870 * buddy system. the join proceeds by attempting to join 2871 * the specified leafno with its buddy (leaf) at new value. 2872 * if the join occurs, we attempt to join the left leaf 2873 * of the joined buddies with its buddy at new value + 1. 2874 * we continue to join until we find a buddy that cannot be 2875 * joined (does not have a value equal to the size of the 2876 * last join) or until all leaves have been joined into a 2877 * single system. 2878 * 2879 * get the buddy size (number of words covered) of 2880 * the new value. 2881 */ 2882 budsz = BUDSIZE(newval, tp->dmt_budmin); 2883 2884 /* try to join. 2885 */ 2886 while (budsz < le32_to_cpu(tp->dmt_nleafs)) { 2887 /* get the buddy leaf. 2888 */ 2889 buddy = leafno ^ budsz; 2890 2891 /* if the leaf's new value is greater than its 2892 * buddy's value, we join no more. 2893 */ 2894 if (newval > leaf[buddy]) 2895 break; 2896 2897 /* It shouldn't be less */ 2898 if (newval < leaf[buddy]) 2899 return -EIO; 2900 2901 /* check which (leafno or buddy) is the left buddy. 2902 * the left buddy gets to claim the blocks resulting 2903 * from the join while the right gets to claim none. 2904 * the left buddy is also eligible to participate in 2905 * a join at the next higher level while the right 2906 * is not. 2907 * 2908 */ 2909 if (leafno < buddy) { 2910 /* leafno is the left buddy. 2911 */ 2912 dbAdjTree(tp, buddy, NOFREE, is_ctl); 2913 } else { 2914 /* buddy is the left buddy and becomes 2915 * leafno. 2916 */ 2917 dbAdjTree(tp, leafno, NOFREE, is_ctl); 2918 leafno = buddy; 2919 } 2920 2921 /* on to try the next join. 2922 */ 2923 newval += 1; 2924 budsz <<= 1; 2925 } 2926 } 2927 2928 /* update the leaf value. 2929 */ 2930 dbAdjTree(tp, leafno, newval, is_ctl); 2931 2932 return 0; 2933 } 2934 2935 2936 /* 2937 * NAME: dbAdjTree() 2938 * 2939 * FUNCTION: update a leaf of a dmtree with a new value, adjusting 2940 * the dmtree, as required, to reflect the new leaf value. 2941 * the combination of any buddies must already be done before 2942 * this is called. 2943 * 2944 * PARAMETERS: 2945 * tp - pointer to the tree to be adjusted. 2946 * leafno - the number of the leaf to be updated. 2947 * newval - the new value for the leaf. 2948 * 2949 * RETURN VALUES: none 2950 */ 2951 static void dbAdjTree(dmtree_t *tp, int leafno, int newval, bool is_ctl) 2952 { 2953 int lp, pp, k; 2954 int max, size; 2955 2956 size = is_ctl ? CTLTREESIZE : TREESIZE; 2957 2958 /* pick up the index of the leaf for this leafno. 2959 */ 2960 lp = leafno + le32_to_cpu(tp->dmt_leafidx); 2961 2962 if (WARN_ON_ONCE(lp >= size || lp < 0)) 2963 return; 2964 2965 /* is the current value the same as the old value ? if so, 2966 * there is nothing to do. 2967 */ 2968 if (tp->dmt_stree[lp] == newval) 2969 return; 2970 2971 /* set the new value. 2972 */ 2973 tp->dmt_stree[lp] = newval; 2974 2975 /* bubble the new value up the tree as required. 2976 */ 2977 for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) { 2978 if (lp == 0) 2979 break; 2980 2981 /* get the index of the first leaf of the 4 leaf 2982 * group containing the specified leaf (leafno). 2983 */ 2984 lp = ((lp - 1) & ~0x03) + 1; 2985 2986 /* get the index of the parent of this 4 leaf group. 2987 */ 2988 pp = (lp - 1) >> 2; 2989 2990 /* determine the maximum of the 4 leaves. 2991 */ 2992 max = TREEMAX(&tp->dmt_stree[lp]); 2993 2994 /* if the maximum of the 4 is the same as the 2995 * parent's value, we're done. 2996 */ 2997 if (tp->dmt_stree[pp] == max) 2998 break; 2999 3000 /* parent gets new value. 3001 */ 3002 tp->dmt_stree[pp] = max; 3003 3004 /* parent becomes leaf for next go-round. 3005 */ 3006 lp = pp; 3007 } 3008 } 3009 3010 3011 /* 3012 * NAME: dbFindLeaf() 3013 * 3014 * FUNCTION: search a dmtree_t for sufficient free blocks, returning 3015 * the index of a leaf describing the free blocks if 3016 * sufficient free blocks are found. 3017 * 3018 * the search starts at the top of the dmtree_t tree and 3019 * proceeds down the tree to the leftmost leaf with sufficient 3020 * free space. 3021 * 3022 * PARAMETERS: 3023 * tp - pointer to the tree to be searched. 3024 * l2nb - log2 number of free blocks to search for. 3025 * leafidx - return pointer to be set to the index of the leaf 3026 * describing at least l2nb free blocks if sufficient 3027 * free blocks are found. 3028 * is_ctl - determines if the tree is of type ctl 3029 * 3030 * RETURN VALUES: 3031 * 0 - success 3032 * -ENOSPC - insufficient free blocks. 3033 */ 3034 static int dbFindLeaf(dmtree_t *tp, int l2nb, int *leafidx, bool is_ctl) 3035 { 3036 int ti, n = 0, k, x = 0; 3037 int max_size, max_idx; 3038 3039 max_size = is_ctl ? CTLTREESIZE : TREESIZE; 3040 max_idx = is_ctl ? LPERCTL : LPERDMAP; 3041 3042 /* first check the root of the tree to see if there is 3043 * sufficient free space. 3044 */ 3045 if (l2nb > tp->dmt_stree[ROOT]) 3046 return -ENOSPC; 3047 3048 /* sufficient free space available. now search down the tree 3049 * starting at the next level for the leftmost leaf that 3050 * describes sufficient free space. 3051 */ 3052 for (k = le32_to_cpu(tp->dmt_height), ti = 1; 3053 k > 0; k--, ti = ((ti + n) << 2) + 1) { 3054 /* search the four nodes at this level, starting from 3055 * the left. 3056 */ 3057 for (x = ti, n = 0; n < 4; n++) { 3058 /* sufficient free space found. move to the next 3059 * level (or quit if this is the last level). 3060 */ 3061 if (x + n > max_size) 3062 return -ENOSPC; 3063 if (l2nb <= tp->dmt_stree[x + n]) 3064 break; 3065 } 3066 3067 /* better have found something since the higher 3068 * levels of the tree said it was here. 3069 */ 3070 assert(n < 4); 3071 } 3072 if (le32_to_cpu(tp->dmt_leafidx) >= max_idx) 3073 return -ENOSPC; 3074 3075 /* set the return to the leftmost leaf describing sufficient 3076 * free space. 3077 */ 3078 *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx); 3079 3080 return (0); 3081 } 3082 3083 3084 /* 3085 * NAME: dbFindBits() 3086 * 3087 * FUNCTION: find a specified number of binary buddy free bits within a 3088 * dmap bitmap word value. 3089 * 3090 * this routine searches the bitmap value for (1 << l2nb) free 3091 * bits at (1 << l2nb) alignments within the value. 3092 * 3093 * PARAMETERS: 3094 * word - dmap bitmap word value. 3095 * l2nb - number of free bits specified as a log2 number. 3096 * 3097 * RETURN VALUES: 3098 * starting bit number of free bits. 3099 */ 3100 static int dbFindBits(u32 word, int l2nb) 3101 { 3102 int bitno, nb; 3103 u32 mask; 3104 3105 /* get the number of bits. 3106 */ 3107 nb = 1 << l2nb; 3108 assert(nb <= DBWORD); 3109 3110 /* complement the word so we can use a mask (i.e. 0s represent 3111 * free bits) and compute the mask. 3112 */ 3113 word = ~word; 3114 mask = ONES << (DBWORD - nb); 3115 3116 /* scan the word for nb free bits at nb alignments. 3117 */ 3118 for (bitno = 0; mask != 0; bitno += nb, mask = (mask >> nb)) { 3119 if ((mask & word) == mask) 3120 break; 3121 } 3122 3123 ASSERT(bitno < 32); 3124 3125 /* return the bit number. 3126 */ 3127 return (bitno); 3128 } 3129 3130 3131 /* 3132 * NAME: dbMaxBud(u8 *cp) 3133 * 3134 * FUNCTION: determine the largest binary buddy string of free 3135 * bits within 32-bits of the map. 3136 * 3137 * PARAMETERS: 3138 * cp - pointer to the 32-bit value. 3139 * 3140 * RETURN VALUES: 3141 * largest binary buddy of free bits within a dmap word. 3142 */ 3143 static int dbMaxBud(u8 * cp) 3144 { 3145 signed char tmp1, tmp2; 3146 3147 /* check if the wmap word is all free. if so, the 3148 * free buddy size is BUDMIN. 3149 */ 3150 if (*((uint *) cp) == 0) 3151 return (BUDMIN); 3152 3153 /* check if the wmap word is half free. if so, the 3154 * free buddy size is BUDMIN-1. 3155 */ 3156 if (*((u16 *) cp) == 0 || *((u16 *) cp + 1) == 0) 3157 return (BUDMIN - 1); 3158 3159 /* not all free or half free. determine the free buddy 3160 * size thru table lookup using quarters of the wmap word. 3161 */ 3162 tmp1 = max(budtab[cp[2]], budtab[cp[3]]); 3163 tmp2 = max(budtab[cp[0]], budtab[cp[1]]); 3164 return (max(tmp1, tmp2)); 3165 } 3166 3167 3168 /* 3169 * NAME: cnttz(uint word) 3170 * 3171 * FUNCTION: determine the number of trailing zeros within a 32-bit 3172 * value. 3173 * 3174 * PARAMETERS: 3175 * value - 32-bit value to be examined. 3176 * 3177 * RETURN VALUES: 3178 * count of trailing zeros 3179 */ 3180 static int cnttz(u32 word) 3181 { 3182 int n; 3183 3184 for (n = 0; n < 32; n++, word >>= 1) { 3185 if (word & 0x01) 3186 break; 3187 } 3188 3189 return (n); 3190 } 3191 3192 3193 /* 3194 * NAME: cntlz(u32 value) 3195 * 3196 * FUNCTION: determine the number of leading zeros within a 32-bit 3197 * value. 3198 * 3199 * PARAMETERS: 3200 * value - 32-bit value to be examined. 3201 * 3202 * RETURN VALUES: 3203 * count of leading zeros 3204 */ 3205 static int cntlz(u32 value) 3206 { 3207 int n; 3208 3209 for (n = 0; n < 32; n++, value <<= 1) { 3210 if (value & HIGHORDER) 3211 break; 3212 } 3213 return (n); 3214 } 3215 3216 3217 /* 3218 * NAME: blkstol2(s64 nb) 3219 * 3220 * FUNCTION: convert a block count to its log2 value. if the block 3221 * count is not a l2 multiple, it is rounded up to the next 3222 * larger l2 multiple. 3223 * 3224 * PARAMETERS: 3225 * nb - number of blocks 3226 * 3227 * RETURN VALUES: 3228 * log2 number of blocks 3229 */ 3230 static int blkstol2(s64 nb) 3231 { 3232 int l2nb; 3233 s64 mask; /* meant to be signed */ 3234 3235 mask = (s64) 1 << (64 - 1); 3236 3237 /* count the leading bits. 3238 */ 3239 for (l2nb = 0; l2nb < 64; l2nb++, mask >>= 1) { 3240 /* leading bit found. 3241 */ 3242 if (nb & mask) { 3243 /* determine the l2 value. 3244 */ 3245 l2nb = (64 - 1) - l2nb; 3246 3247 /* check if we need to round up. 3248 */ 3249 if (~mask & nb) 3250 l2nb++; 3251 3252 return (l2nb); 3253 } 3254 } 3255 assert(0); 3256 return 0; /* fix compiler warning */ 3257 } 3258 3259 3260 /* 3261 * NAME: dbAllocBottomUp() 3262 * 3263 * FUNCTION: alloc the specified block range from the working block 3264 * allocation map. 3265 * 3266 * the blocks will be alloc from the working map one dmap 3267 * at a time. 3268 * 3269 * PARAMETERS: 3270 * ip - pointer to in-core inode; 3271 * blkno - starting block number to be freed. 3272 * nblocks - number of blocks to be freed. 3273 * 3274 * RETURN VALUES: 3275 * 0 - success 3276 * -EIO - i/o error 3277 */ 3278 int dbAllocBottomUp(struct inode *ip, s64 blkno, s64 nblocks) 3279 { 3280 struct metapage *mp; 3281 struct dmap *dp; 3282 int nb, rc; 3283 s64 lblkno, rem; 3284 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap; 3285 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap; 3286 3287 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP); 3288 3289 /* block to be allocated better be within the mapsize. */ 3290 ASSERT(nblocks <= bmp->db_mapsize - blkno); 3291 3292 /* 3293 * allocate the blocks a dmap at a time. 3294 */ 3295 mp = NULL; 3296 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) { 3297 /* release previous dmap if any */ 3298 if (mp) { 3299 write_metapage(mp); 3300 } 3301 3302 /* get the buffer for the current dmap. */ 3303 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage); 3304 mp = read_metapage(ipbmap, lblkno, PSIZE, 0); 3305 if (mp == NULL) { 3306 IREAD_UNLOCK(ipbmap); 3307 return -EIO; 3308 } 3309 dp = (struct dmap *) mp->data; 3310 3311 /* determine the number of blocks to be allocated from 3312 * this dmap. 3313 */ 3314 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1))); 3315 3316 /* allocate the blocks. */ 3317 if ((rc = dbAllocDmapBU(bmp, dp, blkno, nb))) { 3318 release_metapage(mp); 3319 IREAD_UNLOCK(ipbmap); 3320 return (rc); 3321 } 3322 } 3323 3324 /* write the last buffer. */ 3325 write_metapage(mp); 3326 3327 IREAD_UNLOCK(ipbmap); 3328 3329 return (0); 3330 } 3331 3332 3333 static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno, 3334 int nblocks) 3335 { 3336 int rc; 3337 int dbitno, word, rembits, nb, nwords, wbitno, agno; 3338 s8 oldroot; 3339 struct dmaptree *tp = (struct dmaptree *) & dp->tree; 3340 3341 /* save the current value of the root (i.e. maximum free string) 3342 * of the dmap tree. 3343 */ 3344 oldroot = tp->stree[ROOT]; 3345 3346 /* determine the bit number and word within the dmap of the 3347 * starting block. 3348 */ 3349 dbitno = blkno & (BPERDMAP - 1); 3350 word = dbitno >> L2DBWORD; 3351 3352 /* block range better be within the dmap */ 3353 assert(dbitno + nblocks <= BPERDMAP); 3354 3355 /* allocate the bits of the dmap's words corresponding to the block 3356 * range. not all bits of the first and last words may be contained 3357 * within the block range. if this is the case, we'll work against 3358 * those words (i.e. partial first and/or last) on an individual basis 3359 * (a single pass), allocating the bits of interest by hand and 3360 * updating the leaf corresponding to the dmap word. a single pass 3361 * will be used for all dmap words fully contained within the 3362 * specified range. within this pass, the bits of all fully contained 3363 * dmap words will be marked as free in a single shot and the leaves 3364 * will be updated. a single leaf may describe the free space of 3365 * multiple dmap words, so we may update only a subset of the actual 3366 * leaves corresponding to the dmap words of the block range. 3367 */ 3368 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) { 3369 /* determine the bit number within the word and 3370 * the number of bits within the word. 3371 */ 3372 wbitno = dbitno & (DBWORD - 1); 3373 nb = min(rembits, DBWORD - wbitno); 3374 3375 /* check if only part of a word is to be allocated. 3376 */ 3377 if (nb < DBWORD) { 3378 /* allocate (set to 1) the appropriate bits within 3379 * this dmap word. 3380 */ 3381 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb) 3382 >> wbitno); 3383 3384 word++; 3385 } else { 3386 /* one or more dmap words are fully contained 3387 * within the block range. determine how many 3388 * words and allocate (set to 1) the bits of these 3389 * words. 3390 */ 3391 nwords = rembits >> L2DBWORD; 3392 memset(&dp->wmap[word], (int) ONES, nwords * 4); 3393 3394 /* determine how many bits */ 3395 nb = nwords << L2DBWORD; 3396 word += nwords; 3397 } 3398 } 3399 3400 /* update the free count for this dmap */ 3401 le32_add_cpu(&dp->nfree, -nblocks); 3402 3403 /* reconstruct summary tree */ 3404 dbInitDmapTree(dp); 3405 3406 BMAP_LOCK(bmp); 3407 3408 /* if this allocation group is completely free, 3409 * update the highest active allocation group number 3410 * if this allocation group is the new max. 3411 */ 3412 agno = blkno >> bmp->db_agl2size; 3413 if (agno > bmp->db_maxag) 3414 bmp->db_maxag = agno; 3415 3416 /* update the free count for the allocation group and map */ 3417 bmp->db_agfree[agno] -= nblocks; 3418 bmp->db_nfree -= nblocks; 3419 3420 BMAP_UNLOCK(bmp); 3421 3422 /* if the root has not changed, done. */ 3423 if (tp->stree[ROOT] == oldroot) 3424 return (0); 3425 3426 /* root changed. bubble the change up to the dmap control pages. 3427 * if the adjustment of the upper level control pages fails, 3428 * backout the bit allocation (thus making everything consistent). 3429 */ 3430 if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0))) 3431 dbFreeBits(bmp, dp, blkno, nblocks); 3432 3433 return (rc); 3434 } 3435 3436 3437 /* 3438 * NAME: dbExtendFS() 3439 * 3440 * FUNCTION: extend bmap from blkno for nblocks; 3441 * dbExtendFS() updates bmap ready for dbAllocBottomUp(); 3442 * 3443 * L2 3444 * | 3445 * L1---------------------------------L1 3446 * | | 3447 * L0---------L0---------L0 L0---------L0---------L0 3448 * | | | | | | 3449 * d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,.,dm; 3450 * L2L1L0d0,...,dnL0d0,...,dnL0d0,...,dnL1L0d0,...,dnL0d0,...,dnL0d0,..dm 3451 * 3452 * <---old---><----------------------------extend-----------------------> 3453 */ 3454 int dbExtendFS(struct inode *ipbmap, s64 blkno, s64 nblocks) 3455 { 3456 struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb); 3457 int nbperpage = sbi->nbperpage; 3458 int i, i0 = true, j, j0 = true, k, n; 3459 s64 newsize; 3460 s64 p; 3461 struct metapage *mp, *l2mp, *l1mp = NULL, *l0mp = NULL; 3462 struct dmapctl *l2dcp, *l1dcp, *l0dcp; 3463 struct dmap *dp; 3464 s8 *l0leaf, *l1leaf, *l2leaf; 3465 struct bmap *bmp = sbi->bmap; 3466 int agno, l2agsize, oldl2agsize; 3467 s64 ag_rem; 3468 3469 newsize = blkno + nblocks; 3470 3471 jfs_info("dbExtendFS: blkno:%Ld nblocks:%Ld newsize:%Ld", 3472 (long long) blkno, (long long) nblocks, (long long) newsize); 3473 3474 /* 3475 * initialize bmap control page. 3476 * 3477 * all the data in bmap control page should exclude 3478 * the mkfs hidden dmap page. 3479 */ 3480 3481 /* update mapsize */ 3482 bmp->db_mapsize = newsize; 3483 bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize); 3484 3485 /* compute new AG size */ 3486 l2agsize = dbGetL2AGSize(newsize); 3487 oldl2agsize = bmp->db_agl2size; 3488 3489 bmp->db_agl2size = l2agsize; 3490 bmp->db_agsize = (s64)1 << l2agsize; 3491 3492 /* compute new number of AG */ 3493 agno = bmp->db_numag; 3494 bmp->db_numag = newsize >> l2agsize; 3495 bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0; 3496 3497 /* 3498 * reconfigure db_agfree[] 3499 * from old AG configuration to new AG configuration; 3500 * 3501 * coalesce contiguous k (newAGSize/oldAGSize) AGs; 3502 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn; 3503 * note: new AG size = old AG size * (2**x). 3504 */ 3505 if (l2agsize == oldl2agsize) 3506 goto extend; 3507 k = 1 << (l2agsize - oldl2agsize); 3508 ag_rem = bmp->db_agfree[0]; /* save agfree[0] */ 3509 for (i = 0, n = 0; i < agno; n++) { 3510 bmp->db_agfree[n] = 0; /* init collection point */ 3511 3512 /* coalesce contiguous k AGs; */ 3513 for (j = 0; j < k && i < agno; j++, i++) { 3514 /* merge AGi to AGn */ 3515 bmp->db_agfree[n] += bmp->db_agfree[i]; 3516 } 3517 } 3518 bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */ 3519 3520 for (; n < MAXAG; n++) 3521 bmp->db_agfree[n] = 0; 3522 3523 /* 3524 * update highest active ag number 3525 */ 3526 3527 bmp->db_maxag = bmp->db_maxag / k; 3528 3529 /* 3530 * extend bmap 3531 * 3532 * update bit maps and corresponding level control pages; 3533 * global control page db_nfree, db_agfree[agno], db_maxfreebud; 3534 */ 3535 extend: 3536 /* get L2 page */ 3537 p = BMAPBLKNO + nbperpage; /* L2 page */ 3538 l2mp = read_metapage(ipbmap, p, PSIZE, 0); 3539 if (!l2mp) { 3540 jfs_error(ipbmap->i_sb, "L2 page could not be read\n"); 3541 return -EIO; 3542 } 3543 l2dcp = (struct dmapctl *) l2mp->data; 3544 if (unlikely(!check_dmapctl(l2dcp))) { 3545 jfs_error(ipbmap->i_sb, "Corrupt dmapctl page\n"); 3546 release_metapage(l2mp); 3547 return -EIO; 3548 } 3549 3550 /* compute start L1 */ 3551 k = blkno >> L2MAXL1SIZE; 3552 l2leaf = l2dcp->stree + CTLLEAFIND + k; 3553 p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */ 3554 3555 /* 3556 * extend each L1 in L2 3557 */ 3558 for (; k < LPERCTL; k++, p += nbperpage) { 3559 /* get L1 page */ 3560 if (j0) { 3561 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */ 3562 l1mp = read_metapage(ipbmap, p, PSIZE, 0); 3563 if (l1mp == NULL) 3564 goto errout; 3565 l1dcp = (struct dmapctl *) l1mp->data; 3566 if (unlikely(!check_dmapctl(l1dcp))) { 3567 jfs_error(ipbmap->i_sb, "Corrupt dmapctl page\n"); 3568 goto errout; 3569 } 3570 3571 /* compute start L0 */ 3572 j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE; 3573 l1leaf = l1dcp->stree + CTLLEAFIND + j; 3574 p = BLKTOL0(blkno, sbi->l2nbperpage); 3575 j0 = false; 3576 } else { 3577 /* assign/init L1 page */ 3578 l1mp = get_metapage(ipbmap, p, PSIZE, 0); 3579 if (l1mp == NULL) 3580 goto errout; 3581 3582 l1dcp = (struct dmapctl *) l1mp->data; 3583 if (unlikely(!check_dmapctl(l1dcp))) { 3584 jfs_error(ipbmap->i_sb, "Corrupt dmapctl page\n"); 3585 goto errout; 3586 } 3587 3588 /* compute start L0 */ 3589 j = 0; 3590 l1leaf = l1dcp->stree + CTLLEAFIND; 3591 p += nbperpage; /* 1st L0 of L1.k */ 3592 } 3593 3594 /* 3595 * extend each L0 in L1 3596 */ 3597 for (; j < LPERCTL; j++) { 3598 /* get L0 page */ 3599 if (i0) { 3600 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */ 3601 3602 l0mp = read_metapage(ipbmap, p, PSIZE, 0); 3603 if (l0mp == NULL) 3604 goto errout; 3605 l0dcp = (struct dmapctl *) l0mp->data; 3606 if (unlikely(!check_dmapctl(l0dcp))) { 3607 jfs_error(ipbmap->i_sb, "Corrupt dmapctl page\n"); 3608 goto errout; 3609 } 3610 3611 /* compute start dmap */ 3612 i = (blkno & (MAXL0SIZE - 1)) >> 3613 L2BPERDMAP; 3614 l0leaf = l0dcp->stree + CTLLEAFIND + i; 3615 p = BLKTODMAP(blkno, 3616 sbi->l2nbperpage); 3617 i0 = false; 3618 } else { 3619 /* assign/init L0 page */ 3620 l0mp = get_metapage(ipbmap, p, PSIZE, 0); 3621 if (l0mp == NULL) 3622 goto errout; 3623 3624 l0dcp = (struct dmapctl *) l0mp->data; 3625 if (unlikely(!check_dmapctl(l0dcp))) { 3626 jfs_error(ipbmap->i_sb, "Corrupt dmapctl page\n"); 3627 goto errout; 3628 } 3629 3630 /* compute start dmap */ 3631 i = 0; 3632 l0leaf = l0dcp->stree + CTLLEAFIND; 3633 p += nbperpage; /* 1st dmap of L0.j */ 3634 } 3635 3636 /* 3637 * extend each dmap in L0 3638 */ 3639 for (; i < LPERCTL; i++) { 3640 /* 3641 * reconstruct the dmap page, and 3642 * initialize corresponding parent L0 leaf 3643 */ 3644 if ((n = blkno & (BPERDMAP - 1))) { 3645 /* read in dmap page: */ 3646 mp = read_metapage(ipbmap, p, 3647 PSIZE, 0); 3648 if (mp == NULL) 3649 goto errout; 3650 n = min(nblocks, (s64)BPERDMAP - n); 3651 } else { 3652 /* assign/init dmap page */ 3653 mp = read_metapage(ipbmap, p, 3654 PSIZE, 0); 3655 if (mp == NULL) 3656 goto errout; 3657 3658 n = min_t(s64, nblocks, BPERDMAP); 3659 } 3660 3661 dp = (struct dmap *) mp->data; 3662 *l0leaf = dbInitDmap(dp, blkno, n); 3663 3664 bmp->db_nfree += n; 3665 agno = le64_to_cpu(dp->start) >> l2agsize; 3666 bmp->db_agfree[agno] += n; 3667 3668 write_metapage(mp); 3669 3670 l0leaf++; 3671 p += nbperpage; 3672 3673 blkno += n; 3674 nblocks -= n; 3675 if (nblocks == 0) 3676 break; 3677 } /* for each dmap in a L0 */ 3678 3679 /* 3680 * build current L0 page from its leaves, and 3681 * initialize corresponding parent L1 leaf 3682 */ 3683 *l1leaf = dbInitDmapCtl(l0dcp, 0, ++i); 3684 write_metapage(l0mp); 3685 l0mp = NULL; 3686 3687 if (nblocks) 3688 l1leaf++; /* continue for next L0 */ 3689 else { 3690 /* more than 1 L0 ? */ 3691 if (j > 0) 3692 break; /* build L1 page */ 3693 else { 3694 /* summarize in global bmap page */ 3695 bmp->db_maxfreebud = *l1leaf; 3696 release_metapage(l1mp); 3697 release_metapage(l2mp); 3698 goto finalize; 3699 } 3700 } 3701 } /* for each L0 in a L1 */ 3702 3703 /* 3704 * build current L1 page from its leaves, and 3705 * initialize corresponding parent L2 leaf 3706 */ 3707 *l2leaf = dbInitDmapCtl(l1dcp, 1, ++j); 3708 write_metapage(l1mp); 3709 l1mp = NULL; 3710 3711 if (nblocks) 3712 l2leaf++; /* continue for next L1 */ 3713 else { 3714 /* more than 1 L1 ? */ 3715 if (k > 0) 3716 break; /* build L2 page */ 3717 else { 3718 /* summarize in global bmap page */ 3719 bmp->db_maxfreebud = *l2leaf; 3720 release_metapage(l2mp); 3721 goto finalize; 3722 } 3723 } 3724 } /* for each L1 in a L2 */ 3725 3726 jfs_error(ipbmap->i_sb, "function has not returned as expected\n"); 3727 errout: 3728 if (l0mp) 3729 release_metapage(l0mp); 3730 if (l1mp) 3731 release_metapage(l1mp); 3732 release_metapage(l2mp); 3733 return -EIO; 3734 3735 /* 3736 * finalize bmap control page 3737 */ 3738 finalize: 3739 3740 return 0; 3741 } 3742 3743 3744 /* 3745 * dbFinalizeBmap() 3746 */ 3747 void dbFinalizeBmap(struct inode *ipbmap) 3748 { 3749 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap; 3750 int actags, inactags, l2nl; 3751 s64 ag_rem, actfree, inactfree, avgfree; 3752 int i, n; 3753 3754 /* 3755 * finalize bmap control page 3756 */ 3757 //finalize: 3758 /* 3759 * compute db_agpref: preferred ag to allocate from 3760 * (the leftmost ag with average free space in it); 3761 */ 3762 //agpref: 3763 /* get the number of active ags and inactive ags */ 3764 actags = bmp->db_maxag + 1; 3765 inactags = bmp->db_numag - actags; 3766 ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */ 3767 3768 /* determine how many blocks are in the inactive allocation 3769 * groups. in doing this, we must account for the fact that 3770 * the rightmost group might be a partial group (i.e. file 3771 * system size is not a multiple of the group size). 3772 */ 3773 inactfree = (inactags && ag_rem) ? 3774 (((s64)inactags - 1) << bmp->db_agl2size) + ag_rem 3775 : ((s64)inactags << bmp->db_agl2size); 3776 3777 /* determine how many free blocks are in the active 3778 * allocation groups plus the average number of free blocks 3779 * within the active ags. 3780 */ 3781 actfree = bmp->db_nfree - inactfree; 3782 avgfree = (u32) actfree / (u32) actags; 3783 3784 /* if the preferred allocation group has not average free space. 3785 * re-establish the preferred group as the leftmost 3786 * group with average free space. 3787 */ 3788 if (bmp->db_agfree[bmp->db_agpref] < avgfree) { 3789 for (bmp->db_agpref = 0; bmp->db_agpref < actags; 3790 bmp->db_agpref++) { 3791 if (bmp->db_agfree[bmp->db_agpref] >= avgfree) 3792 break; 3793 } 3794 if (bmp->db_agpref >= bmp->db_numag) { 3795 jfs_error(ipbmap->i_sb, 3796 "cannot find ag with average freespace\n"); 3797 } 3798 } 3799 3800 /* 3801 * compute db_aglevel, db_agheight, db_width, db_agstart: 3802 * an ag is covered in aglevel dmapctl summary tree, 3803 * at agheight level height (from leaf) with agwidth number of nodes 3804 * each, which starts at agstart index node of the smmary tree node 3805 * array; 3806 */ 3807 bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize); 3808 l2nl = 3809 bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL); 3810 bmp->db_agheight = l2nl >> 1; 3811 bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1)); 3812 for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0; 3813 i--) { 3814 bmp->db_agstart += n; 3815 n <<= 2; 3816 } 3817 3818 } 3819 3820 3821 /* 3822 * NAME: dbInitDmap()/ujfs_idmap_page() 3823 * 3824 * FUNCTION: initialize working/persistent bitmap of the dmap page 3825 * for the specified number of blocks: 3826 * 3827 * at entry, the bitmaps had been initialized as free (ZEROS); 3828 * The number of blocks will only account for the actually 3829 * existing blocks. Blocks which don't actually exist in 3830 * the aggregate will be marked as allocated (ONES); 3831 * 3832 * PARAMETERS: 3833 * dp - pointer to page of map 3834 * nblocks - number of blocks this page 3835 * 3836 * RETURNS: NONE 3837 */ 3838 static int dbInitDmap(struct dmap * dp, s64 Blkno, int nblocks) 3839 { 3840 int blkno, w, b, r, nw, nb, i; 3841 3842 /* starting block number within the dmap */ 3843 blkno = Blkno & (BPERDMAP - 1); 3844 3845 if (blkno == 0) { 3846 dp->nblocks = dp->nfree = cpu_to_le32(nblocks); 3847 dp->start = cpu_to_le64(Blkno); 3848 3849 if (nblocks == BPERDMAP) { 3850 memset(&dp->wmap[0], 0, LPERDMAP * 4); 3851 memset(&dp->pmap[0], 0, LPERDMAP * 4); 3852 goto initTree; 3853 } 3854 } else { 3855 le32_add_cpu(&dp->nblocks, nblocks); 3856 le32_add_cpu(&dp->nfree, nblocks); 3857 } 3858 3859 /* word number containing start block number */ 3860 w = blkno >> L2DBWORD; 3861 3862 /* 3863 * free the bits corresponding to the block range (ZEROS): 3864 * note: not all bits of the first and last words may be contained 3865 * within the block range. 3866 */ 3867 for (r = nblocks; r > 0; r -= nb, blkno += nb) { 3868 /* number of bits preceding range to be freed in the word */ 3869 b = blkno & (DBWORD - 1); 3870 /* number of bits to free in the word */ 3871 nb = min(r, DBWORD - b); 3872 3873 /* is partial word to be freed ? */ 3874 if (nb < DBWORD) { 3875 /* free (set to 0) from the bitmap word */ 3876 dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) 3877 >> b)); 3878 dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb) 3879 >> b)); 3880 3881 /* skip the word freed */ 3882 w++; 3883 } else { 3884 /* free (set to 0) contiguous bitmap words */ 3885 nw = r >> L2DBWORD; 3886 memset(&dp->wmap[w], 0, nw * 4); 3887 memset(&dp->pmap[w], 0, nw * 4); 3888 3889 /* skip the words freed */ 3890 nb = nw << L2DBWORD; 3891 w += nw; 3892 } 3893 } 3894 3895 /* 3896 * mark bits following the range to be freed (non-existing 3897 * blocks) as allocated (ONES) 3898 */ 3899 3900 if (blkno == BPERDMAP) 3901 goto initTree; 3902 3903 /* the first word beyond the end of existing blocks */ 3904 w = blkno >> L2DBWORD; 3905 3906 /* does nblocks fall on a 32-bit boundary ? */ 3907 b = blkno & (DBWORD - 1); 3908 if (b) { 3909 /* mark a partial word allocated */ 3910 dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b); 3911 w++; 3912 } 3913 3914 /* set the rest of the words in the page to allocated (ONES) */ 3915 for (i = w; i < LPERDMAP; i++) 3916 dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES); 3917 3918 /* 3919 * init tree 3920 */ 3921 initTree: 3922 return (dbInitDmapTree(dp)); 3923 } 3924 3925 3926 /* 3927 * NAME: dbInitDmapTree()/ujfs_complete_dmap() 3928 * 3929 * FUNCTION: initialize summary tree of the specified dmap: 3930 * 3931 * at entry, bitmap of the dmap has been initialized; 3932 * 3933 * PARAMETERS: 3934 * dp - dmap to complete 3935 * blkno - starting block number for this dmap 3936 * treemax - will be filled in with max free for this dmap 3937 * 3938 * RETURNS: max free string at the root of the tree 3939 */ 3940 static int dbInitDmapTree(struct dmap * dp) 3941 { 3942 struct dmaptree *tp; 3943 s8 *cp; 3944 int i; 3945 3946 /* init fixed info of tree */ 3947 tp = &dp->tree; 3948 tp->nleafs = cpu_to_le32(LPERDMAP); 3949 tp->l2nleafs = cpu_to_le32(L2LPERDMAP); 3950 tp->leafidx = cpu_to_le32(LEAFIND); 3951 tp->height = cpu_to_le32(4); 3952 tp->budmin = BUDMIN; 3953 3954 /* init each leaf from corresponding wmap word: 3955 * note: leaf is set to NOFREE(-1) if all blocks of corresponding 3956 * bitmap word are allocated. 3957 */ 3958 cp = tp->stree + le32_to_cpu(tp->leafidx); 3959 for (i = 0; i < LPERDMAP; i++) 3960 *cp++ = dbMaxBud((u8 *) & dp->wmap[i]); 3961 3962 /* build the dmap's binary buddy summary tree */ 3963 return (dbInitTree(tp)); 3964 } 3965 3966 3967 /* 3968 * NAME: dbInitTree()/ujfs_adjtree() 3969 * 3970 * FUNCTION: initialize binary buddy summary tree of a dmap or dmapctl. 3971 * 3972 * at entry, the leaves of the tree has been initialized 3973 * from corresponding bitmap word or root of summary tree 3974 * of the child control page; 3975 * configure binary buddy system at the leaf level, then 3976 * bubble up the values of the leaf nodes up the tree. 3977 * 3978 * PARAMETERS: 3979 * cp - Pointer to the root of the tree 3980 * l2leaves- Number of leaf nodes as a power of 2 3981 * l2min - Number of blocks that can be covered by a leaf 3982 * as a power of 2 3983 * 3984 * RETURNS: max free string at the root of the tree 3985 */ 3986 static int dbInitTree(struct dmaptree * dtp) 3987 { 3988 int l2max, l2free, bsize, nextb, i; 3989 int child, parent, nparent; 3990 s8 *tp, *cp, *cp1; 3991 3992 tp = dtp->stree; 3993 3994 /* Determine the maximum free string possible for the leaves */ 3995 l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin; 3996 3997 /* 3998 * configure the leaf level into binary buddy system 3999 * 4000 * Try to combine buddies starting with a buddy size of 1 4001 * (i.e. two leaves). At a buddy size of 1 two buddy leaves 4002 * can be combined if both buddies have a maximum free of l2min; 4003 * the combination will result in the left-most buddy leaf having 4004 * a maximum free of l2min+1. 4005 * After processing all buddies for a given size, process buddies 4006 * at the next higher buddy size (i.e. current size * 2) and 4007 * the next maximum free (current free + 1). 4008 * This continues until the maximum possible buddy combination 4009 * yields maximum free. 4010 */ 4011 for (l2free = dtp->budmin, bsize = 1; l2free < l2max; 4012 l2free++, bsize = nextb) { 4013 /* get next buddy size == current buddy pair size */ 4014 nextb = bsize << 1; 4015 4016 /* scan each adjacent buddy pair at current buddy size */ 4017 for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx); 4018 i < le32_to_cpu(dtp->nleafs); 4019 i += nextb, cp += nextb) { 4020 /* coalesce if both adjacent buddies are max free */ 4021 if (*cp == l2free && *(cp + bsize) == l2free) { 4022 *cp = l2free + 1; /* left take right */ 4023 *(cp + bsize) = -1; /* right give left */ 4024 } 4025 } 4026 } 4027 4028 /* 4029 * bubble summary information of leaves up the tree. 4030 * 4031 * Starting at the leaf node level, the four nodes described by 4032 * the higher level parent node are compared for a maximum free and 4033 * this maximum becomes the value of the parent node. 4034 * when all lower level nodes are processed in this fashion then 4035 * move up to the next level (parent becomes a lower level node) and 4036 * continue the process for that level. 4037 */ 4038 for (child = le32_to_cpu(dtp->leafidx), 4039 nparent = le32_to_cpu(dtp->nleafs) >> 2; 4040 nparent > 0; nparent >>= 2, child = parent) { 4041 /* get index of 1st node of parent level */ 4042 parent = (child - 1) >> 2; 4043 4044 /* set the value of the parent node as the maximum 4045 * of the four nodes of the current level. 4046 */ 4047 for (i = 0, cp = tp + child, cp1 = tp + parent; 4048 i < nparent; i++, cp += 4, cp1++) 4049 *cp1 = TREEMAX(cp); 4050 } 4051 4052 return (*tp); 4053 } 4054 4055 4056 /* 4057 * dbInitDmapCtl() 4058 * 4059 * function: initialize dmapctl page 4060 */ 4061 static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i) 4062 { /* start leaf index not covered by range */ 4063 s8 *cp; 4064 4065 dcp->nleafs = cpu_to_le32(LPERCTL); 4066 dcp->l2nleafs = cpu_to_le32(L2LPERCTL); 4067 dcp->leafidx = cpu_to_le32(CTLLEAFIND); 4068 dcp->height = cpu_to_le32(5); 4069 dcp->budmin = L2BPERDMAP + L2LPERCTL * level; 4070 4071 /* 4072 * initialize the leaves of current level that were not covered 4073 * by the specified input block range (i.e. the leaves have no 4074 * low level dmapctl or dmap). 4075 */ 4076 cp = &dcp->stree[CTLLEAFIND + i]; 4077 for (; i < LPERCTL; i++) 4078 *cp++ = NOFREE; 4079 4080 /* build the dmap's binary buddy summary tree */ 4081 return (dbInitTree((struct dmaptree *) dcp)); 4082 } 4083 4084 4085 /* 4086 * NAME: dbGetL2AGSize()/ujfs_getagl2size() 4087 * 4088 * FUNCTION: Determine log2(allocation group size) from aggregate size 4089 * 4090 * PARAMETERS: 4091 * nblocks - Number of blocks in aggregate 4092 * 4093 * RETURNS: log2(allocation group size) in aggregate blocks 4094 */ 4095 static int dbGetL2AGSize(s64 nblocks) 4096 { 4097 s64 sz; 4098 s64 m; 4099 int l2sz; 4100 4101 if (nblocks < BPERDMAP * MAXAG) 4102 return (L2BPERDMAP); 4103 4104 /* round up aggregate size to power of 2 */ 4105 m = ((u64) 1 << (64 - 1)); 4106 for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) { 4107 if (m & nblocks) 4108 break; 4109 } 4110 4111 sz = (s64) 1 << l2sz; 4112 if (sz < nblocks) 4113 l2sz += 1; 4114 4115 /* agsize = roundupSize/max_number_of_ag */ 4116 return (l2sz - L2MAXAG); 4117 } 4118 4119 4120 /* 4121 * NAME: dbMapFileSizeToMapSize() 4122 * 4123 * FUNCTION: compute number of blocks the block allocation map file 4124 * can cover from the map file size; 4125 * 4126 * RETURNS: Number of blocks which can be covered by this block map file; 4127 */ 4128 4129 /* 4130 * maximum number of map pages at each level including control pages 4131 */ 4132 #define MAXL0PAGES (1 + LPERCTL) 4133 #define MAXL1PAGES (1 + LPERCTL * MAXL0PAGES) 4134 4135 /* 4136 * convert number of map pages to the zero origin top dmapctl level 4137 */ 4138 #define BMAPPGTOLEV(npages) \ 4139 (((npages) <= 3 + MAXL0PAGES) ? 0 : \ 4140 ((npages) <= 2 + MAXL1PAGES) ? 1 : 2) 4141 4142 s64 dbMapFileSizeToMapSize(struct inode * ipbmap) 4143 { 4144 struct super_block *sb = ipbmap->i_sb; 4145 s64 nblocks; 4146 s64 npages, ndmaps; 4147 int level, i; 4148 int complete, factor; 4149 4150 nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize; 4151 npages = nblocks >> JFS_SBI(sb)->l2nbperpage; 4152 level = BMAPPGTOLEV(npages); 4153 4154 /* At each level, accumulate the number of dmap pages covered by 4155 * the number of full child levels below it; 4156 * repeat for the last incomplete child level. 4157 */ 4158 ndmaps = 0; 4159 npages--; /* skip the first global control page */ 4160 /* skip higher level control pages above top level covered by map */ 4161 npages -= (2 - level); 4162 npages--; /* skip top level's control page */ 4163 for (i = level; i >= 0; i--) { 4164 factor = 4165 (i == 2) ? MAXL1PAGES : ((i == 1) ? MAXL0PAGES : 1); 4166 complete = (u32) npages / factor; 4167 ndmaps += complete * ((i == 2) ? LPERCTL * LPERCTL : 4168 ((i == 1) ? LPERCTL : 1)); 4169 4170 /* pages in last/incomplete child */ 4171 npages = (u32) npages % factor; 4172 /* skip incomplete child's level control page */ 4173 npages--; 4174 } 4175 4176 /* convert the number of dmaps into the number of blocks 4177 * which can be covered by the dmaps; 4178 */ 4179 nblocks = ndmaps << L2BPERDMAP; 4180 4181 return (nblocks); 4182 } 4183