1 /* 2 * Copyright (C) International Business Machines Corp., 2000-2004 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12 * the GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 /* 20 * jfs_imap.c: inode allocation map manager 21 * 22 * Serialization: 23 * Each AG has a simple lock which is used to control the serialization of 24 * the AG level lists. This lock should be taken first whenever an AG 25 * level list will be modified or accessed. 26 * 27 * Each IAG is locked by obtaining the buffer for the IAG page. 28 * 29 * There is also a inode lock for the inode map inode. A read lock needs to 30 * be taken whenever an IAG is read from the map or the global level 31 * information is read. A write lock needs to be taken whenever the global 32 * level information is modified or an atomic operation needs to be used. 33 * 34 * If more than one IAG is read at one time, the read lock may not 35 * be given up until all of the IAG's are read. Otherwise, a deadlock 36 * may occur when trying to obtain the read lock while another thread 37 * holding the read lock is waiting on the IAG already being held. 38 * 39 * The control page of the inode map is read into memory by diMount(). 40 * Thereafter it should only be modified in memory and then it will be 41 * written out when the filesystem is unmounted by diUnmount(). 42 */ 43 44 #include <linux/fs.h> 45 #include <linux/buffer_head.h> 46 #include <linux/pagemap.h> 47 #include <linux/quotaops.h> 48 49 #include "jfs_incore.h" 50 #include "jfs_inode.h" 51 #include "jfs_filsys.h" 52 #include "jfs_dinode.h" 53 #include "jfs_dmap.h" 54 #include "jfs_imap.h" 55 #include "jfs_metapage.h" 56 #include "jfs_superblock.h" 57 #include "jfs_debug.h" 58 59 /* 60 * imap locks 61 */ 62 /* iag free list lock */ 63 #define IAGFREE_LOCK_INIT(imap) init_MUTEX(&imap->im_freelock) 64 #define IAGFREE_LOCK(imap) down(&imap->im_freelock) 65 #define IAGFREE_UNLOCK(imap) up(&imap->im_freelock) 66 67 /* per ag iag list locks */ 68 #define AG_LOCK_INIT(imap,index) init_MUTEX(&(imap->im_aglock[index])) 69 #define AG_LOCK(imap,agno) down(&imap->im_aglock[agno]) 70 #define AG_UNLOCK(imap,agno) up(&imap->im_aglock[agno]) 71 72 /* 73 * forward references 74 */ 75 static int diAllocAG(struct inomap *, int, boolean_t, struct inode *); 76 static int diAllocAny(struct inomap *, int, boolean_t, struct inode *); 77 static int diAllocBit(struct inomap *, struct iag *, int); 78 static int diAllocExt(struct inomap *, int, struct inode *); 79 static int diAllocIno(struct inomap *, int, struct inode *); 80 static int diFindFree(u32, int); 81 static int diNewExt(struct inomap *, struct iag *, int); 82 static int diNewIAG(struct inomap *, int *, int, struct metapage **); 83 static void duplicateIXtree(struct super_block *, s64, int, s64 *); 84 85 static int diIAGRead(struct inomap * imap, int, struct metapage **); 86 static int copy_from_dinode(struct dinode *, struct inode *); 87 static void copy_to_dinode(struct dinode *, struct inode *); 88 89 /* 90 * debug code for double-checking inode map 91 */ 92 /* #define _JFS_DEBUG_IMAP 1 */ 93 94 #ifdef _JFS_DEBUG_IMAP 95 #define DBG_DIINIT(imap) DBGdiInit(imap) 96 #define DBG_DIALLOC(imap, ino) DBGdiAlloc(imap, ino) 97 #define DBG_DIFREE(imap, ino) DBGdiFree(imap, ino) 98 99 static void *DBGdiInit(struct inomap * imap); 100 static void DBGdiAlloc(struct inomap * imap, ino_t ino); 101 static void DBGdiFree(struct inomap * imap, ino_t ino); 102 #else 103 #define DBG_DIINIT(imap) 104 #define DBG_DIALLOC(imap, ino) 105 #define DBG_DIFREE(imap, ino) 106 #endif /* _JFS_DEBUG_IMAP */ 107 108 /* 109 * NAME: diMount() 110 * 111 * FUNCTION: initialize the incore inode map control structures for 112 * a fileset or aggregate init time. 113 * 114 * the inode map's control structure (dinomap) is 115 * brought in from disk and placed in virtual memory. 116 * 117 * PARAMETERS: 118 * ipimap - pointer to inode map inode for the aggregate or fileset. 119 * 120 * RETURN VALUES: 121 * 0 - success 122 * -ENOMEM - insufficient free virtual memory. 123 * -EIO - i/o error. 124 */ 125 int diMount(struct inode *ipimap) 126 { 127 struct inomap *imap; 128 struct metapage *mp; 129 int index; 130 struct dinomap_disk *dinom_le; 131 132 /* 133 * allocate/initialize the in-memory inode map control structure 134 */ 135 /* allocate the in-memory inode map control structure. */ 136 imap = (struct inomap *) kmalloc(sizeof(struct inomap), GFP_KERNEL); 137 if (imap == NULL) { 138 jfs_err("diMount: kmalloc returned NULL!"); 139 return -ENOMEM; 140 } 141 142 /* read the on-disk inode map control structure. */ 143 144 mp = read_metapage(ipimap, 145 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage, 146 PSIZE, 0); 147 if (mp == NULL) { 148 kfree(imap); 149 return -EIO; 150 } 151 152 /* copy the on-disk version to the in-memory version. */ 153 dinom_le = (struct dinomap_disk *) mp->data; 154 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag); 155 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag); 156 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos)); 157 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree)); 158 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext); 159 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext); 160 for (index = 0; index < MAXAG; index++) { 161 imap->im_agctl[index].inofree = 162 le32_to_cpu(dinom_le->in_agctl[index].inofree); 163 imap->im_agctl[index].extfree = 164 le32_to_cpu(dinom_le->in_agctl[index].extfree); 165 imap->im_agctl[index].numinos = 166 le32_to_cpu(dinom_le->in_agctl[index].numinos); 167 imap->im_agctl[index].numfree = 168 le32_to_cpu(dinom_le->in_agctl[index].numfree); 169 } 170 171 /* release the buffer. */ 172 release_metapage(mp); 173 174 /* 175 * allocate/initialize inode allocation map locks 176 */ 177 /* allocate and init iag free list lock */ 178 IAGFREE_LOCK_INIT(imap); 179 180 /* allocate and init ag list locks */ 181 for (index = 0; index < MAXAG; index++) { 182 AG_LOCK_INIT(imap, index); 183 } 184 185 /* bind the inode map inode and inode map control structure 186 * to each other. 187 */ 188 imap->im_ipimap = ipimap; 189 JFS_IP(ipimap)->i_imap = imap; 190 191 // DBG_DIINIT(imap); 192 193 return (0); 194 } 195 196 197 /* 198 * NAME: diUnmount() 199 * 200 * FUNCTION: write to disk the incore inode map control structures for 201 * a fileset or aggregate at unmount time. 202 * 203 * PARAMETERS: 204 * ipimap - pointer to inode map inode for the aggregate or fileset. 205 * 206 * RETURN VALUES: 207 * 0 - success 208 * -ENOMEM - insufficient free virtual memory. 209 * -EIO - i/o error. 210 */ 211 int diUnmount(struct inode *ipimap, int mounterror) 212 { 213 struct inomap *imap = JFS_IP(ipimap)->i_imap; 214 215 /* 216 * update the on-disk inode map control structure 217 */ 218 219 if (!(mounterror || isReadOnly(ipimap))) 220 diSync(ipimap); 221 222 /* 223 * Invalidate the page cache buffers 224 */ 225 truncate_inode_pages(ipimap->i_mapping, 0); 226 227 /* 228 * free in-memory control structure 229 */ 230 kfree(imap); 231 232 return (0); 233 } 234 235 236 /* 237 * diSync() 238 */ 239 int diSync(struct inode *ipimap) 240 { 241 struct dinomap_disk *dinom_le; 242 struct inomap *imp = JFS_IP(ipimap)->i_imap; 243 struct metapage *mp; 244 int index; 245 246 /* 247 * write imap global conrol page 248 */ 249 /* read the on-disk inode map control structure */ 250 mp = get_metapage(ipimap, 251 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage, 252 PSIZE, 0); 253 if (mp == NULL) { 254 jfs_err("diSync: get_metapage failed!"); 255 return -EIO; 256 } 257 258 /* copy the in-memory version to the on-disk version */ 259 dinom_le = (struct dinomap_disk *) mp->data; 260 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag); 261 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag); 262 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos)); 263 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree)); 264 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext); 265 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext); 266 for (index = 0; index < MAXAG; index++) { 267 dinom_le->in_agctl[index].inofree = 268 cpu_to_le32(imp->im_agctl[index].inofree); 269 dinom_le->in_agctl[index].extfree = 270 cpu_to_le32(imp->im_agctl[index].extfree); 271 dinom_le->in_agctl[index].numinos = 272 cpu_to_le32(imp->im_agctl[index].numinos); 273 dinom_le->in_agctl[index].numfree = 274 cpu_to_le32(imp->im_agctl[index].numfree); 275 } 276 277 /* write out the control structure */ 278 write_metapage(mp); 279 280 /* 281 * write out dirty pages of imap 282 */ 283 filemap_fdatawrite(ipimap->i_mapping); 284 filemap_fdatawait(ipimap->i_mapping); 285 286 diWriteSpecial(ipimap, 0); 287 288 return (0); 289 } 290 291 292 /* 293 * NAME: diRead() 294 * 295 * FUNCTION: initialize an incore inode from disk. 296 * 297 * on entry, the specifed incore inode should itself 298 * specify the disk inode number corresponding to the 299 * incore inode (i.e. i_number should be initialized). 300 * 301 * this routine handles incore inode initialization for 302 * both "special" and "regular" inodes. special inodes 303 * are those required early in the mount process and 304 * require special handling since much of the file system 305 * is not yet initialized. these "special" inodes are 306 * identified by a NULL inode map inode pointer and are 307 * actually initialized by a call to diReadSpecial(). 308 * 309 * for regular inodes, the iag describing the disk inode 310 * is read from disk to determine the inode extent address 311 * for the disk inode. with the inode extent address in 312 * hand, the page of the extent that contains the disk 313 * inode is read and the disk inode is copied to the 314 * incore inode. 315 * 316 * PARAMETERS: 317 * ip - pointer to incore inode to be initialized from disk. 318 * 319 * RETURN VALUES: 320 * 0 - success 321 * -EIO - i/o error. 322 * -ENOMEM - insufficient memory 323 * 324 */ 325 int diRead(struct inode *ip) 326 { 327 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb); 328 int iagno, ino, extno, rc; 329 struct inode *ipimap; 330 struct dinode *dp; 331 struct iag *iagp; 332 struct metapage *mp; 333 s64 blkno, agstart; 334 struct inomap *imap; 335 int block_offset; 336 int inodes_left; 337 uint pageno; 338 int rel_inode; 339 340 jfs_info("diRead: ino = %ld", ip->i_ino); 341 342 ipimap = sbi->ipimap; 343 JFS_IP(ip)->ipimap = ipimap; 344 345 /* determine the iag number for this inode (number) */ 346 iagno = INOTOIAG(ip->i_ino); 347 348 /* read the iag */ 349 imap = JFS_IP(ipimap)->i_imap; 350 IREAD_LOCK(ipimap); 351 rc = diIAGRead(imap, iagno, &mp); 352 IREAD_UNLOCK(ipimap); 353 if (rc) { 354 jfs_err("diRead: diIAGRead returned %d", rc); 355 return (rc); 356 } 357 358 iagp = (struct iag *) mp->data; 359 360 /* determine inode extent that holds the disk inode */ 361 ino = ip->i_ino & (INOSPERIAG - 1); 362 extno = ino >> L2INOSPEREXT; 363 364 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) || 365 (addressPXD(&iagp->inoext[extno]) == 0)) { 366 release_metapage(mp); 367 return -ESTALE; 368 } 369 370 /* get disk block number of the page within the inode extent 371 * that holds the disk inode. 372 */ 373 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage); 374 375 /* get the ag for the iag */ 376 agstart = le64_to_cpu(iagp->agstart); 377 378 release_metapage(mp); 379 380 rel_inode = (ino & (INOSPERPAGE - 1)); 381 pageno = blkno >> sbi->l2nbperpage; 382 383 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) { 384 /* 385 * OS/2 didn't always align inode extents on page boundaries 386 */ 387 inodes_left = 388 (sbi->nbperpage - block_offset) << sbi->l2niperblk; 389 390 if (rel_inode < inodes_left) 391 rel_inode += block_offset << sbi->l2niperblk; 392 else { 393 pageno += 1; 394 rel_inode -= inodes_left; 395 } 396 } 397 398 /* read the page of disk inode */ 399 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1); 400 if (mp == 0) { 401 jfs_err("diRead: read_metapage failed"); 402 return -EIO; 403 } 404 405 /* locate the the disk inode requested */ 406 dp = (struct dinode *) mp->data; 407 dp += rel_inode; 408 409 if (ip->i_ino != le32_to_cpu(dp->di_number)) { 410 jfs_error(ip->i_sb, "diRead: i_ino != di_number"); 411 rc = -EIO; 412 } else if (le32_to_cpu(dp->di_nlink) == 0) 413 rc = -ESTALE; 414 else 415 /* copy the disk inode to the in-memory inode */ 416 rc = copy_from_dinode(dp, ip); 417 418 release_metapage(mp); 419 420 /* set the ag for the inode */ 421 JFS_IP(ip)->agno = BLKTOAG(agstart, sbi); 422 JFS_IP(ip)->active_ag = -1; 423 424 return (rc); 425 } 426 427 428 /* 429 * NAME: diReadSpecial() 430 * 431 * FUNCTION: initialize a 'special' inode from disk. 432 * 433 * this routines handles aggregate level inodes. The 434 * inode cache cannot differentiate between the 435 * aggregate inodes and the filesystem inodes, so we 436 * handle these here. We don't actually use the aggregate 437 * inode map, since these inodes are at a fixed location 438 * and in some cases the aggregate inode map isn't initialized 439 * yet. 440 * 441 * PARAMETERS: 442 * sb - filesystem superblock 443 * inum - aggregate inode number 444 * secondary - 1 if secondary aggregate inode table 445 * 446 * RETURN VALUES: 447 * new inode - success 448 * NULL - i/o error. 449 */ 450 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary) 451 { 452 struct jfs_sb_info *sbi = JFS_SBI(sb); 453 uint address; 454 struct dinode *dp; 455 struct inode *ip; 456 struct metapage *mp; 457 458 ip = new_inode(sb); 459 if (ip == NULL) { 460 jfs_err("diReadSpecial: new_inode returned NULL!"); 461 return ip; 462 } 463 464 if (secondary) { 465 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage; 466 JFS_IP(ip)->ipimap = sbi->ipaimap2; 467 } else { 468 address = AITBL_OFF >> L2PSIZE; 469 JFS_IP(ip)->ipimap = sbi->ipaimap; 470 } 471 472 ASSERT(inum < INOSPEREXT); 473 474 ip->i_ino = inum; 475 476 address += inum >> 3; /* 8 inodes per 4K page */ 477 478 /* read the page of fixed disk inode (AIT) in raw mode */ 479 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1); 480 if (mp == NULL) { 481 ip->i_nlink = 1; /* Don't want iput() deleting it */ 482 iput(ip); 483 return (NULL); 484 } 485 486 /* get the pointer to the disk inode of interest */ 487 dp = (struct dinode *) (mp->data); 488 dp += inum % 8; /* 8 inodes per 4K page */ 489 490 /* copy on-disk inode to in-memory inode */ 491 if ((copy_from_dinode(dp, ip)) != 0) { 492 /* handle bad return by returning NULL for ip */ 493 ip->i_nlink = 1; /* Don't want iput() deleting it */ 494 iput(ip); 495 /* release the page */ 496 release_metapage(mp); 497 return (NULL); 498 499 } 500 501 ip->i_mapping->a_ops = &jfs_metapage_aops; 502 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS); 503 504 /* Allocations to metadata inodes should not affect quotas */ 505 ip->i_flags |= S_NOQUOTA; 506 507 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) { 508 sbi->gengen = le32_to_cpu(dp->di_gengen); 509 sbi->inostamp = le32_to_cpu(dp->di_inostamp); 510 } 511 512 /* release the page */ 513 release_metapage(mp); 514 515 return (ip); 516 } 517 518 /* 519 * NAME: diWriteSpecial() 520 * 521 * FUNCTION: Write the special inode to disk 522 * 523 * PARAMETERS: 524 * ip - special inode 525 * secondary - 1 if secondary aggregate inode table 526 * 527 * RETURN VALUES: none 528 */ 529 530 void diWriteSpecial(struct inode *ip, int secondary) 531 { 532 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb); 533 uint address; 534 struct dinode *dp; 535 ino_t inum = ip->i_ino; 536 struct metapage *mp; 537 538 ip->i_state &= ~I_DIRTY; 539 540 if (secondary) 541 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage; 542 else 543 address = AITBL_OFF >> L2PSIZE; 544 545 ASSERT(inum < INOSPEREXT); 546 547 address += inum >> 3; /* 8 inodes per 4K page */ 548 549 /* read the page of fixed disk inode (AIT) in raw mode */ 550 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1); 551 if (mp == NULL) { 552 jfs_err("diWriteSpecial: failed to read aggregate inode " 553 "extent!"); 554 return; 555 } 556 557 /* get the pointer to the disk inode of interest */ 558 dp = (struct dinode *) (mp->data); 559 dp += inum % 8; /* 8 inodes per 4K page */ 560 561 /* copy on-disk inode to in-memory inode */ 562 copy_to_dinode(dp, ip); 563 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288); 564 565 if (inum == FILESYSTEM_I) 566 dp->di_gengen = cpu_to_le32(sbi->gengen); 567 568 /* write the page */ 569 write_metapage(mp); 570 } 571 572 /* 573 * NAME: diFreeSpecial() 574 * 575 * FUNCTION: Free allocated space for special inode 576 */ 577 void diFreeSpecial(struct inode *ip) 578 { 579 if (ip == NULL) { 580 jfs_err("diFreeSpecial called with NULL ip!"); 581 return; 582 } 583 filemap_fdatawrite(ip->i_mapping); 584 filemap_fdatawait(ip->i_mapping); 585 truncate_inode_pages(ip->i_mapping, 0); 586 iput(ip); 587 } 588 589 590 591 /* 592 * NAME: diWrite() 593 * 594 * FUNCTION: write the on-disk inode portion of the in-memory inode 595 * to its corresponding on-disk inode. 596 * 597 * on entry, the specifed incore inode should itself 598 * specify the disk inode number corresponding to the 599 * incore inode (i.e. i_number should be initialized). 600 * 601 * the inode contains the inode extent address for the disk 602 * inode. with the inode extent address in hand, the 603 * page of the extent that contains the disk inode is 604 * read and the disk inode portion of the incore inode 605 * is copied to the disk inode. 606 * 607 * PARAMETERS: 608 * tid - transacation id 609 * ip - pointer to incore inode to be written to the inode extent. 610 * 611 * RETURN VALUES: 612 * 0 - success 613 * -EIO - i/o error. 614 */ 615 int diWrite(tid_t tid, struct inode *ip) 616 { 617 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb); 618 struct jfs_inode_info *jfs_ip = JFS_IP(ip); 619 int rc = 0; 620 s32 ino; 621 struct dinode *dp; 622 s64 blkno; 623 int block_offset; 624 int inodes_left; 625 struct metapage *mp; 626 uint pageno; 627 int rel_inode; 628 int dioffset; 629 struct inode *ipimap; 630 uint type; 631 lid_t lid; 632 struct tlock *ditlck, *tlck; 633 struct linelock *dilinelock, *ilinelock; 634 struct lv *lv; 635 int n; 636 637 ipimap = jfs_ip->ipimap; 638 639 ino = ip->i_ino & (INOSPERIAG - 1); 640 641 if (!addressPXD(&(jfs_ip->ixpxd)) || 642 (lengthPXD(&(jfs_ip->ixpxd)) != 643 JFS_IP(ipimap)->i_imap->im_nbperiext)) { 644 jfs_error(ip->i_sb, "diWrite: ixpxd invalid"); 645 return -EIO; 646 } 647 648 /* 649 * read the page of disk inode containing the specified inode: 650 */ 651 /* compute the block address of the page */ 652 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage); 653 654 rel_inode = (ino & (INOSPERPAGE - 1)); 655 pageno = blkno >> sbi->l2nbperpage; 656 657 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) { 658 /* 659 * OS/2 didn't always align inode extents on page boundaries 660 */ 661 inodes_left = 662 (sbi->nbperpage - block_offset) << sbi->l2niperblk; 663 664 if (rel_inode < inodes_left) 665 rel_inode += block_offset << sbi->l2niperblk; 666 else { 667 pageno += 1; 668 rel_inode -= inodes_left; 669 } 670 } 671 /* read the page of disk inode */ 672 retry: 673 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1); 674 if (mp == 0) 675 return -EIO; 676 677 /* get the pointer to the disk inode */ 678 dp = (struct dinode *) mp->data; 679 dp += rel_inode; 680 681 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE; 682 683 /* 684 * acquire transaction lock on the on-disk inode; 685 * N.B. tlock is acquired on ipimap not ip; 686 */ 687 if ((ditlck = 688 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL) 689 goto retry; 690 dilinelock = (struct linelock *) & ditlck->lock; 691 692 /* 693 * copy btree root from in-memory inode to on-disk inode 694 * 695 * (tlock is taken from inline B+-tree root in in-memory 696 * inode when the B+-tree root is updated, which is pointed 697 * by jfs_ip->blid as well as being on tx tlock list) 698 * 699 * further processing of btree root is based on the copy 700 * in in-memory inode, where txLog() will log from, and, 701 * for xtree root, txUpdateMap() will update map and reset 702 * XAD_NEW bit; 703 */ 704 705 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) { 706 /* 707 * This is the special xtree inside the directory for storing 708 * the directory table 709 */ 710 xtpage_t *p, *xp; 711 xad_t *xad; 712 713 jfs_ip->xtlid = 0; 714 tlck = lid_to_tlock(lid); 715 assert(tlck->type & tlckXTREE); 716 tlck->type |= tlckBTROOT; 717 tlck->mp = mp; 718 ilinelock = (struct linelock *) & tlck->lock; 719 720 /* 721 * copy xtree root from inode to dinode: 722 */ 723 p = &jfs_ip->i_xtroot; 724 xp = (xtpage_t *) &dp->di_dirtable; 725 lv = ilinelock->lv; 726 for (n = 0; n < ilinelock->index; n++, lv++) { 727 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset], 728 lv->length << L2XTSLOTSIZE); 729 } 730 731 /* reset on-disk (metadata page) xtree XAD_NEW bit */ 732 xad = &xp->xad[XTENTRYSTART]; 733 for (n = XTENTRYSTART; 734 n < le16_to_cpu(xp->header.nextindex); n++, xad++) 735 if (xad->flag & (XAD_NEW | XAD_EXTENDED)) 736 xad->flag &= ~(XAD_NEW | XAD_EXTENDED); 737 } 738 739 if ((lid = jfs_ip->blid) == 0) 740 goto inlineData; 741 jfs_ip->blid = 0; 742 743 tlck = lid_to_tlock(lid); 744 type = tlck->type; 745 tlck->type |= tlckBTROOT; 746 tlck->mp = mp; 747 ilinelock = (struct linelock *) & tlck->lock; 748 749 /* 750 * regular file: 16 byte (XAD slot) granularity 751 */ 752 if (type & tlckXTREE) { 753 xtpage_t *p, *xp; 754 xad_t *xad; 755 756 /* 757 * copy xtree root from inode to dinode: 758 */ 759 p = &jfs_ip->i_xtroot; 760 xp = &dp->di_xtroot; 761 lv = ilinelock->lv; 762 for (n = 0; n < ilinelock->index; n++, lv++) { 763 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset], 764 lv->length << L2XTSLOTSIZE); 765 } 766 767 /* reset on-disk (metadata page) xtree XAD_NEW bit */ 768 xad = &xp->xad[XTENTRYSTART]; 769 for (n = XTENTRYSTART; 770 n < le16_to_cpu(xp->header.nextindex); n++, xad++) 771 if (xad->flag & (XAD_NEW | XAD_EXTENDED)) 772 xad->flag &= ~(XAD_NEW | XAD_EXTENDED); 773 } 774 /* 775 * directory: 32 byte (directory entry slot) granularity 776 */ 777 else if (type & tlckDTREE) { 778 dtpage_t *p, *xp; 779 780 /* 781 * copy dtree root from inode to dinode: 782 */ 783 p = (dtpage_t *) &jfs_ip->i_dtroot; 784 xp = (dtpage_t *) & dp->di_dtroot; 785 lv = ilinelock->lv; 786 for (n = 0; n < ilinelock->index; n++, lv++) { 787 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset], 788 lv->length << L2DTSLOTSIZE); 789 } 790 } else { 791 jfs_err("diWrite: UFO tlock"); 792 } 793 794 inlineData: 795 /* 796 * copy inline symlink from in-memory inode to on-disk inode 797 */ 798 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) { 799 lv = & dilinelock->lv[dilinelock->index]; 800 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE; 801 lv->length = 2; 802 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE); 803 dilinelock->index++; 804 } 805 /* 806 * copy inline data from in-memory inode to on-disk inode: 807 * 128 byte slot granularity 808 */ 809 if (test_cflag(COMMIT_Inlineea, ip)) { 810 lv = & dilinelock->lv[dilinelock->index]; 811 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE; 812 lv->length = 1; 813 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE); 814 dilinelock->index++; 815 816 clear_cflag(COMMIT_Inlineea, ip); 817 } 818 819 /* 820 * lock/copy inode base: 128 byte slot granularity 821 */ 822 // baseDinode: 823 lv = & dilinelock->lv[dilinelock->index]; 824 lv->offset = dioffset >> L2INODESLOTSIZE; 825 copy_to_dinode(dp, ip); 826 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) { 827 lv->length = 2; 828 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96); 829 } else 830 lv->length = 1; 831 dilinelock->index++; 832 833 #ifdef _JFS_FASTDASD 834 /* 835 * We aren't logging changes to the DASD used in directory inodes, 836 * but we need to write them to disk. If we don't unmount cleanly, 837 * mount will recalculate the DASD used. 838 */ 839 if (S_ISDIR(ip->i_mode) 840 && (ip->i_ipmnt->i_mntflag & JFS_DASD_ENABLED)) 841 memcpy(&dp->di_DASD, &ip->i_DASD, sizeof(struct dasd)); 842 #endif /* _JFS_FASTDASD */ 843 844 /* release the buffer holding the updated on-disk inode. 845 * the buffer will be later written by commit processing. 846 */ 847 write_metapage(mp); 848 849 return (rc); 850 } 851 852 853 /* 854 * NAME: diFree(ip) 855 * 856 * FUNCTION: free a specified inode from the inode working map 857 * for a fileset or aggregate. 858 * 859 * if the inode to be freed represents the first (only) 860 * free inode within the iag, the iag will be placed on 861 * the ag free inode list. 862 * 863 * freeing the inode will cause the inode extent to be 864 * freed if the inode is the only allocated inode within 865 * the extent. in this case all the disk resource backing 866 * up the inode extent will be freed. in addition, the iag 867 * will be placed on the ag extent free list if the extent 868 * is the first free extent in the iag. if freeing the 869 * extent also means that no free inodes will exist for 870 * the iag, the iag will also be removed from the ag free 871 * inode list. 872 * 873 * the iag describing the inode will be freed if the extent 874 * is to be freed and it is the only backed extent within 875 * the iag. in this case, the iag will be removed from the 876 * ag free extent list and ag free inode list and placed on 877 * the inode map's free iag list. 878 * 879 * a careful update approach is used to provide consistency 880 * in the face of updates to multiple buffers. under this 881 * approach, all required buffers are obtained before making 882 * any updates and are held until all updates are complete. 883 * 884 * PARAMETERS: 885 * ip - inode to be freed. 886 * 887 * RETURN VALUES: 888 * 0 - success 889 * -EIO - i/o error. 890 */ 891 int diFree(struct inode *ip) 892 { 893 int rc; 894 ino_t inum = ip->i_ino; 895 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp; 896 struct metapage *mp, *amp, *bmp, *cmp, *dmp; 897 int iagno, ino, extno, bitno, sword, agno; 898 int back, fwd; 899 u32 bitmap, mask; 900 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap; 901 struct inomap *imap = JFS_IP(ipimap)->i_imap; 902 pxd_t freepxd; 903 tid_t tid; 904 struct inode *iplist[3]; 905 struct tlock *tlck; 906 struct pxd_lock *pxdlock; 907 908 /* 909 * This is just to suppress compiler warnings. The same logic that 910 * references these variables is used to initialize them. 911 */ 912 aiagp = biagp = ciagp = diagp = NULL; 913 914 /* get the iag number containing the inode. 915 */ 916 iagno = INOTOIAG(inum); 917 918 /* make sure that the iag is contained within 919 * the map. 920 */ 921 if (iagno >= imap->im_nextiag) { 922 dump_mem("imap", imap, 32); 923 jfs_error(ip->i_sb, 924 "diFree: inum = %d, iagno = %d, nextiag = %d", 925 (uint) inum, iagno, imap->im_nextiag); 926 return -EIO; 927 } 928 929 /* get the allocation group for this ino. 930 */ 931 agno = JFS_IP(ip)->agno; 932 933 /* Lock the AG specific inode map information 934 */ 935 AG_LOCK(imap, agno); 936 937 /* Obtain read lock in imap inode. Don't release it until we have 938 * read all of the IAG's that we are going to. 939 */ 940 IREAD_LOCK(ipimap); 941 942 /* read the iag. 943 */ 944 if ((rc = diIAGRead(imap, iagno, &mp))) { 945 IREAD_UNLOCK(ipimap); 946 AG_UNLOCK(imap, agno); 947 return (rc); 948 } 949 iagp = (struct iag *) mp->data; 950 951 /* get the inode number and extent number of the inode within 952 * the iag and the inode number within the extent. 953 */ 954 ino = inum & (INOSPERIAG - 1); 955 extno = ino >> L2INOSPEREXT; 956 bitno = ino & (INOSPEREXT - 1); 957 mask = HIGHORDER >> bitno; 958 959 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) { 960 jfs_error(ip->i_sb, 961 "diFree: wmap shows inode already free"); 962 } 963 964 if (!addressPXD(&iagp->inoext[extno])) { 965 release_metapage(mp); 966 IREAD_UNLOCK(ipimap); 967 AG_UNLOCK(imap, agno); 968 jfs_error(ip->i_sb, "diFree: invalid inoext"); 969 return -EIO; 970 } 971 972 /* compute the bitmap for the extent reflecting the freed inode. 973 */ 974 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask; 975 976 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) { 977 release_metapage(mp); 978 IREAD_UNLOCK(ipimap); 979 AG_UNLOCK(imap, agno); 980 jfs_error(ip->i_sb, "diFree: numfree > numinos"); 981 return -EIO; 982 } 983 /* 984 * inode extent still has some inodes or below low water mark: 985 * keep the inode extent; 986 */ 987 if (bitmap || 988 imap->im_agctl[agno].numfree < 96 || 989 (imap->im_agctl[agno].numfree < 288 && 990 (((imap->im_agctl[agno].numfree * 100) / 991 imap->im_agctl[agno].numinos) <= 25))) { 992 /* if the iag currently has no free inodes (i.e., 993 * the inode being freed is the first free inode of iag), 994 * insert the iag at head of the inode free list for the ag. 995 */ 996 if (iagp->nfreeinos == 0) { 997 /* check if there are any iags on the ag inode 998 * free list. if so, read the first one so that 999 * we can link the current iag onto the list at 1000 * the head. 1001 */ 1002 if ((fwd = imap->im_agctl[agno].inofree) >= 0) { 1003 /* read the iag that currently is the head 1004 * of the list. 1005 */ 1006 if ((rc = diIAGRead(imap, fwd, &))) { 1007 IREAD_UNLOCK(ipimap); 1008 AG_UNLOCK(imap, agno); 1009 release_metapage(mp); 1010 return (rc); 1011 } 1012 aiagp = (struct iag *) amp->data; 1013 1014 /* make current head point back to the iag. 1015 */ 1016 aiagp->inofreeback = cpu_to_le32(iagno); 1017 1018 write_metapage(amp); 1019 } 1020 1021 /* iag points forward to current head and iag 1022 * becomes the new head of the list. 1023 */ 1024 iagp->inofreefwd = 1025 cpu_to_le32(imap->im_agctl[agno].inofree); 1026 iagp->inofreeback = cpu_to_le32(-1); 1027 imap->im_agctl[agno].inofree = iagno; 1028 } 1029 IREAD_UNLOCK(ipimap); 1030 1031 /* update the free inode summary map for the extent if 1032 * freeing the inode means the extent will now have free 1033 * inodes (i.e., the inode being freed is the first free 1034 * inode of extent), 1035 */ 1036 if (iagp->wmap[extno] == cpu_to_le32(ONES)) { 1037 sword = extno >> L2EXTSPERSUM; 1038 bitno = extno & (EXTSPERSUM - 1); 1039 iagp->inosmap[sword] &= 1040 cpu_to_le32(~(HIGHORDER >> bitno)); 1041 } 1042 1043 /* update the bitmap. 1044 */ 1045 iagp->wmap[extno] = cpu_to_le32(bitmap); 1046 DBG_DIFREE(imap, inum); 1047 1048 /* update the free inode counts at the iag, ag and 1049 * map level. 1050 */ 1051 iagp->nfreeinos = 1052 cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1); 1053 imap->im_agctl[agno].numfree += 1; 1054 atomic_inc(&imap->im_numfree); 1055 1056 /* release the AG inode map lock 1057 */ 1058 AG_UNLOCK(imap, agno); 1059 1060 /* write the iag */ 1061 write_metapage(mp); 1062 1063 return (0); 1064 } 1065 1066 1067 /* 1068 * inode extent has become free and above low water mark: 1069 * free the inode extent; 1070 */ 1071 1072 /* 1073 * prepare to update iag list(s) (careful update step 1) 1074 */ 1075 amp = bmp = cmp = dmp = NULL; 1076 fwd = back = -1; 1077 1078 /* check if the iag currently has no free extents. if so, 1079 * it will be placed on the head of the ag extent free list. 1080 */ 1081 if (iagp->nfreeexts == 0) { 1082 /* check if the ag extent free list has any iags. 1083 * if so, read the iag at the head of the list now. 1084 * this (head) iag will be updated later to reflect 1085 * the addition of the current iag at the head of 1086 * the list. 1087 */ 1088 if ((fwd = imap->im_agctl[agno].extfree) >= 0) { 1089 if ((rc = diIAGRead(imap, fwd, &))) 1090 goto error_out; 1091 aiagp = (struct iag *) amp->data; 1092 } 1093 } else { 1094 /* iag has free extents. check if the addition of a free 1095 * extent will cause all extents to be free within this 1096 * iag. if so, the iag will be removed from the ag extent 1097 * free list and placed on the inode map's free iag list. 1098 */ 1099 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) { 1100 /* in preparation for removing the iag from the 1101 * ag extent free list, read the iags preceeding 1102 * and following the iag on the ag extent free 1103 * list. 1104 */ 1105 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) { 1106 if ((rc = diIAGRead(imap, fwd, &))) 1107 goto error_out; 1108 aiagp = (struct iag *) amp->data; 1109 } 1110 1111 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) { 1112 if ((rc = diIAGRead(imap, back, &bmp))) 1113 goto error_out; 1114 biagp = (struct iag *) bmp->data; 1115 } 1116 } 1117 } 1118 1119 /* remove the iag from the ag inode free list if freeing 1120 * this extent cause the iag to have no free inodes. 1121 */ 1122 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) { 1123 int inofreeback = le32_to_cpu(iagp->inofreeback); 1124 int inofreefwd = le32_to_cpu(iagp->inofreefwd); 1125 1126 /* in preparation for removing the iag from the 1127 * ag inode free list, read the iags preceeding 1128 * and following the iag on the ag inode free 1129 * list. before reading these iags, we must make 1130 * sure that we already don't have them in hand 1131 * from up above, since re-reading an iag (buffer) 1132 * we are currently holding would cause a deadlock. 1133 */ 1134 if (inofreefwd >= 0) { 1135 1136 if (inofreefwd == fwd) 1137 ciagp = (struct iag *) amp->data; 1138 else if (inofreefwd == back) 1139 ciagp = (struct iag *) bmp->data; 1140 else { 1141 if ((rc = 1142 diIAGRead(imap, inofreefwd, &cmp))) 1143 goto error_out; 1144 ciagp = (struct iag *) cmp->data; 1145 } 1146 assert(ciagp != NULL); 1147 } 1148 1149 if (inofreeback >= 0) { 1150 if (inofreeback == fwd) 1151 diagp = (struct iag *) amp->data; 1152 else if (inofreeback == back) 1153 diagp = (struct iag *) bmp->data; 1154 else { 1155 if ((rc = 1156 diIAGRead(imap, inofreeback, &dmp))) 1157 goto error_out; 1158 diagp = (struct iag *) dmp->data; 1159 } 1160 assert(diagp != NULL); 1161 } 1162 } 1163 1164 IREAD_UNLOCK(ipimap); 1165 1166 /* 1167 * invalidate any page of the inode extent freed from buffer cache; 1168 */ 1169 freepxd = iagp->inoext[extno]; 1170 invalidate_pxd_metapages(ip, freepxd); 1171 1172 /* 1173 * update iag list(s) (careful update step 2) 1174 */ 1175 /* add the iag to the ag extent free list if this is the 1176 * first free extent for the iag. 1177 */ 1178 if (iagp->nfreeexts == 0) { 1179 if (fwd >= 0) 1180 aiagp->extfreeback = cpu_to_le32(iagno); 1181 1182 iagp->extfreefwd = 1183 cpu_to_le32(imap->im_agctl[agno].extfree); 1184 iagp->extfreeback = cpu_to_le32(-1); 1185 imap->im_agctl[agno].extfree = iagno; 1186 } else { 1187 /* remove the iag from the ag extent list if all extents 1188 * are now free and place it on the inode map iag free list. 1189 */ 1190 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) { 1191 if (fwd >= 0) 1192 aiagp->extfreeback = iagp->extfreeback; 1193 1194 if (back >= 0) 1195 biagp->extfreefwd = iagp->extfreefwd; 1196 else 1197 imap->im_agctl[agno].extfree = 1198 le32_to_cpu(iagp->extfreefwd); 1199 1200 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1); 1201 1202 IAGFREE_LOCK(imap); 1203 iagp->iagfree = cpu_to_le32(imap->im_freeiag); 1204 imap->im_freeiag = iagno; 1205 IAGFREE_UNLOCK(imap); 1206 } 1207 } 1208 1209 /* remove the iag from the ag inode free list if freeing 1210 * this extent causes the iag to have no free inodes. 1211 */ 1212 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) { 1213 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) 1214 ciagp->inofreeback = iagp->inofreeback; 1215 1216 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) 1217 diagp->inofreefwd = iagp->inofreefwd; 1218 else 1219 imap->im_agctl[agno].inofree = 1220 le32_to_cpu(iagp->inofreefwd); 1221 1222 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1); 1223 } 1224 1225 /* update the inode extent address and working map 1226 * to reflect the free extent. 1227 * the permanent map should have been updated already 1228 * for the inode being freed. 1229 */ 1230 if (iagp->pmap[extno] != 0) { 1231 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free"); 1232 } 1233 iagp->wmap[extno] = 0; 1234 DBG_DIFREE(imap, inum); 1235 PXDlength(&iagp->inoext[extno], 0); 1236 PXDaddress(&iagp->inoext[extno], 0); 1237 1238 /* update the free extent and free inode summary maps 1239 * to reflect the freed extent. 1240 * the inode summary map is marked to indicate no inodes 1241 * available for the freed extent. 1242 */ 1243 sword = extno >> L2EXTSPERSUM; 1244 bitno = extno & (EXTSPERSUM - 1); 1245 mask = HIGHORDER >> bitno; 1246 iagp->inosmap[sword] |= cpu_to_le32(mask); 1247 iagp->extsmap[sword] &= cpu_to_le32(~mask); 1248 1249 /* update the number of free inodes and number of free extents 1250 * for the iag. 1251 */ 1252 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1253 (INOSPEREXT - 1)); 1254 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1); 1255 1256 /* update the number of free inodes and backed inodes 1257 * at the ag and inode map level. 1258 */ 1259 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1); 1260 imap->im_agctl[agno].numinos -= INOSPEREXT; 1261 atomic_sub(INOSPEREXT - 1, &imap->im_numfree); 1262 atomic_sub(INOSPEREXT, &imap->im_numinos); 1263 1264 if (amp) 1265 write_metapage(amp); 1266 if (bmp) 1267 write_metapage(bmp); 1268 if (cmp) 1269 write_metapage(cmp); 1270 if (dmp) 1271 write_metapage(dmp); 1272 1273 /* 1274 * start transaction to update block allocation map 1275 * for the inode extent freed; 1276 * 1277 * N.B. AG_LOCK is released and iag will be released below, and 1278 * other thread may allocate inode from/reusing the ixad freed 1279 * BUT with new/different backing inode extent from the extent 1280 * to be freed by the transaction; 1281 */ 1282 tid = txBegin(ipimap->i_sb, COMMIT_FORCE); 1283 down(&JFS_IP(ipimap)->commit_sem); 1284 1285 /* acquire tlock of the iag page of the freed ixad 1286 * to force the page NOHOMEOK (even though no data is 1287 * logged from the iag page) until NOREDOPAGE|FREEXTENT log 1288 * for the free of the extent is committed; 1289 * write FREEXTENT|NOREDOPAGE log record 1290 * N.B. linelock is overlaid as freed extent descriptor; 1291 */ 1292 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE); 1293 pxdlock = (struct pxd_lock *) & tlck->lock; 1294 pxdlock->flag = mlckFREEPXD; 1295 pxdlock->pxd = freepxd; 1296 pxdlock->index = 1; 1297 1298 write_metapage(mp); 1299 1300 iplist[0] = ipimap; 1301 1302 /* 1303 * logredo needs the IAG number and IAG extent index in order 1304 * to ensure that the IMap is consistent. The least disruptive 1305 * way to pass these values through to the transaction manager 1306 * is in the iplist array. 1307 * 1308 * It's not pretty, but it works. 1309 */ 1310 iplist[1] = (struct inode *) (size_t)iagno; 1311 iplist[2] = (struct inode *) (size_t)extno; 1312 1313 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE); 1314 1315 txEnd(tid); 1316 up(&JFS_IP(ipimap)->commit_sem); 1317 1318 /* unlock the AG inode map information */ 1319 AG_UNLOCK(imap, agno); 1320 1321 return (0); 1322 1323 error_out: 1324 IREAD_UNLOCK(ipimap); 1325 1326 if (amp) 1327 release_metapage(amp); 1328 if (bmp) 1329 release_metapage(bmp); 1330 if (cmp) 1331 release_metapage(cmp); 1332 if (dmp) 1333 release_metapage(dmp); 1334 1335 AG_UNLOCK(imap, agno); 1336 1337 release_metapage(mp); 1338 1339 return (rc); 1340 } 1341 1342 /* 1343 * There are several places in the diAlloc* routines where we initialize 1344 * the inode. 1345 */ 1346 static inline void 1347 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp) 1348 { 1349 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb); 1350 struct jfs_inode_info *jfs_ip = JFS_IP(ip); 1351 1352 ip->i_ino = (iagno << L2INOSPERIAG) + ino; 1353 DBG_DIALLOC(JFS_IP(ipimap)->i_imap, ip->i_ino); 1354 jfs_ip->ixpxd = iagp->inoext[extno]; 1355 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi); 1356 jfs_ip->active_ag = -1; 1357 } 1358 1359 1360 /* 1361 * NAME: diAlloc(pip,dir,ip) 1362 * 1363 * FUNCTION: allocate a disk inode from the inode working map 1364 * for a fileset or aggregate. 1365 * 1366 * PARAMETERS: 1367 * pip - pointer to incore inode for the parent inode. 1368 * dir - TRUE if the new disk inode is for a directory. 1369 * ip - pointer to a new inode 1370 * 1371 * RETURN VALUES: 1372 * 0 - success. 1373 * -ENOSPC - insufficient disk resources. 1374 * -EIO - i/o error. 1375 */ 1376 int diAlloc(struct inode *pip, boolean_t dir, struct inode *ip) 1377 { 1378 int rc, ino, iagno, addext, extno, bitno, sword; 1379 int nwords, rem, i, agno; 1380 u32 mask, inosmap, extsmap; 1381 struct inode *ipimap; 1382 struct metapage *mp; 1383 ino_t inum; 1384 struct iag *iagp; 1385 struct inomap *imap; 1386 1387 /* get the pointers to the inode map inode and the 1388 * corresponding imap control structure. 1389 */ 1390 ipimap = JFS_SBI(pip->i_sb)->ipimap; 1391 imap = JFS_IP(ipimap)->i_imap; 1392 JFS_IP(ip)->ipimap = ipimap; 1393 JFS_IP(ip)->fileset = FILESYSTEM_I; 1394 1395 /* for a directory, the allocation policy is to start 1396 * at the ag level using the preferred ag. 1397 */ 1398 if (dir == TRUE) { 1399 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap); 1400 AG_LOCK(imap, agno); 1401 goto tryag; 1402 } 1403 1404 /* for files, the policy starts off by trying to allocate from 1405 * the same iag containing the parent disk inode: 1406 * try to allocate the new disk inode close to the parent disk 1407 * inode, using parent disk inode number + 1 as the allocation 1408 * hint. (we use a left-to-right policy to attempt to avoid 1409 * moving backward on the disk.) compute the hint within the 1410 * file system and the iag. 1411 */ 1412 1413 /* get the ag number of this iag */ 1414 agno = JFS_IP(pip)->agno; 1415 1416 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) { 1417 /* 1418 * There is an open file actively growing. We want to 1419 * allocate new inodes from a different ag to avoid 1420 * fragmentation problems. 1421 */ 1422 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap); 1423 AG_LOCK(imap, agno); 1424 goto tryag; 1425 } 1426 1427 inum = pip->i_ino + 1; 1428 ino = inum & (INOSPERIAG - 1); 1429 1430 /* back off the the hint if it is outside of the iag */ 1431 if (ino == 0) 1432 inum = pip->i_ino; 1433 1434 /* lock the AG inode map information */ 1435 AG_LOCK(imap, agno); 1436 1437 /* Get read lock on imap inode */ 1438 IREAD_LOCK(ipimap); 1439 1440 /* get the iag number and read the iag */ 1441 iagno = INOTOIAG(inum); 1442 if ((rc = diIAGRead(imap, iagno, &mp))) { 1443 IREAD_UNLOCK(ipimap); 1444 AG_UNLOCK(imap, agno); 1445 return (rc); 1446 } 1447 iagp = (struct iag *) mp->data; 1448 1449 /* determine if new inode extent is allowed to be added to the iag. 1450 * new inode extent can be added to the iag if the ag 1451 * has less than 32 free disk inodes and the iag has free extents. 1452 */ 1453 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts); 1454 1455 /* 1456 * try to allocate from the IAG 1457 */ 1458 /* check if the inode may be allocated from the iag 1459 * (i.e. the inode has free inodes or new extent can be added). 1460 */ 1461 if (iagp->nfreeinos || addext) { 1462 /* determine the extent number of the hint. 1463 */ 1464 extno = ino >> L2INOSPEREXT; 1465 1466 /* check if the extent containing the hint has backed 1467 * inodes. if so, try to allocate within this extent. 1468 */ 1469 if (addressPXD(&iagp->inoext[extno])) { 1470 bitno = ino & (INOSPEREXT - 1); 1471 if ((bitno = 1472 diFindFree(le32_to_cpu(iagp->wmap[extno]), 1473 bitno)) 1474 < INOSPEREXT) { 1475 ino = (extno << L2INOSPEREXT) + bitno; 1476 1477 /* a free inode (bit) was found within this 1478 * extent, so allocate it. 1479 */ 1480 rc = diAllocBit(imap, iagp, ino); 1481 IREAD_UNLOCK(ipimap); 1482 if (rc) { 1483 assert(rc == -EIO); 1484 } else { 1485 /* set the results of the allocation 1486 * and write the iag. 1487 */ 1488 diInitInode(ip, iagno, ino, extno, 1489 iagp); 1490 mark_metapage_dirty(mp); 1491 } 1492 release_metapage(mp); 1493 1494 /* free the AG lock and return. 1495 */ 1496 AG_UNLOCK(imap, agno); 1497 return (rc); 1498 } 1499 1500 if (!addext) 1501 extno = 1502 (extno == 1503 EXTSPERIAG - 1) ? 0 : extno + 1; 1504 } 1505 1506 /* 1507 * no free inodes within the extent containing the hint. 1508 * 1509 * try to allocate from the backed extents following 1510 * hint or, if appropriate (i.e. addext is true), allocate 1511 * an extent of free inodes at or following the extent 1512 * containing the hint. 1513 * 1514 * the free inode and free extent summary maps are used 1515 * here, so determine the starting summary map position 1516 * and the number of words we'll have to examine. again, 1517 * the approach is to allocate following the hint, so we 1518 * might have to initially ignore prior bits of the summary 1519 * map that represent extents prior to the extent containing 1520 * the hint and later revisit these bits. 1521 */ 1522 bitno = extno & (EXTSPERSUM - 1); 1523 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1; 1524 sword = extno >> L2EXTSPERSUM; 1525 1526 /* mask any prior bits for the starting words of the 1527 * summary map. 1528 */ 1529 mask = ONES << (EXTSPERSUM - bitno); 1530 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask; 1531 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask; 1532 1533 /* scan the free inode and free extent summary maps for 1534 * free resources. 1535 */ 1536 for (i = 0; i < nwords; i++) { 1537 /* check if this word of the free inode summary 1538 * map describes an extent with free inodes. 1539 */ 1540 if (~inosmap) { 1541 /* an extent with free inodes has been 1542 * found. determine the extent number 1543 * and the inode number within the extent. 1544 */ 1545 rem = diFindFree(inosmap, 0); 1546 extno = (sword << L2EXTSPERSUM) + rem; 1547 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 1548 0); 1549 if (rem >= INOSPEREXT) { 1550 IREAD_UNLOCK(ipimap); 1551 release_metapage(mp); 1552 AG_UNLOCK(imap, agno); 1553 jfs_error(ip->i_sb, 1554 "diAlloc: can't find free bit " 1555 "in wmap"); 1556 return EIO; 1557 } 1558 1559 /* determine the inode number within the 1560 * iag and allocate the inode from the 1561 * map. 1562 */ 1563 ino = (extno << L2INOSPEREXT) + rem; 1564 rc = diAllocBit(imap, iagp, ino); 1565 IREAD_UNLOCK(ipimap); 1566 if (rc) 1567 assert(rc == -EIO); 1568 else { 1569 /* set the results of the allocation 1570 * and write the iag. 1571 */ 1572 diInitInode(ip, iagno, ino, extno, 1573 iagp); 1574 mark_metapage_dirty(mp); 1575 } 1576 release_metapage(mp); 1577 1578 /* free the AG lock and return. 1579 */ 1580 AG_UNLOCK(imap, agno); 1581 return (rc); 1582 1583 } 1584 1585 /* check if we may allocate an extent of free 1586 * inodes and whether this word of the free 1587 * extents summary map describes a free extent. 1588 */ 1589 if (addext && ~extsmap) { 1590 /* a free extent has been found. determine 1591 * the extent number. 1592 */ 1593 rem = diFindFree(extsmap, 0); 1594 extno = (sword << L2EXTSPERSUM) + rem; 1595 1596 /* allocate an extent of free inodes. 1597 */ 1598 if ((rc = diNewExt(imap, iagp, extno))) { 1599 /* if there is no disk space for a 1600 * new extent, try to allocate the 1601 * disk inode from somewhere else. 1602 */ 1603 if (rc == -ENOSPC) 1604 break; 1605 1606 assert(rc == -EIO); 1607 } else { 1608 /* set the results of the allocation 1609 * and write the iag. 1610 */ 1611 diInitInode(ip, iagno, 1612 extno << L2INOSPEREXT, 1613 extno, iagp); 1614 mark_metapage_dirty(mp); 1615 } 1616 release_metapage(mp); 1617 /* free the imap inode & the AG lock & return. 1618 */ 1619 IREAD_UNLOCK(ipimap); 1620 AG_UNLOCK(imap, agno); 1621 return (rc); 1622 } 1623 1624 /* move on to the next set of summary map words. 1625 */ 1626 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1; 1627 inosmap = le32_to_cpu(iagp->inosmap[sword]); 1628 extsmap = le32_to_cpu(iagp->extsmap[sword]); 1629 } 1630 } 1631 /* unlock imap inode */ 1632 IREAD_UNLOCK(ipimap); 1633 1634 /* nothing doing in this iag, so release it. */ 1635 release_metapage(mp); 1636 1637 tryag: 1638 /* 1639 * try to allocate anywhere within the same AG as the parent inode. 1640 */ 1641 rc = diAllocAG(imap, agno, dir, ip); 1642 1643 AG_UNLOCK(imap, agno); 1644 1645 if (rc != -ENOSPC) 1646 return (rc); 1647 1648 /* 1649 * try to allocate in any AG. 1650 */ 1651 return (diAllocAny(imap, agno, dir, ip)); 1652 } 1653 1654 1655 /* 1656 * NAME: diAllocAG(imap,agno,dir,ip) 1657 * 1658 * FUNCTION: allocate a disk inode from the allocation group. 1659 * 1660 * this routine first determines if a new extent of free 1661 * inodes should be added for the allocation group, with 1662 * the current request satisfied from this extent. if this 1663 * is the case, an attempt will be made to do just that. if 1664 * this attempt fails or it has been determined that a new 1665 * extent should not be added, an attempt is made to satisfy 1666 * the request by allocating an existing (backed) free inode 1667 * from the allocation group. 1668 * 1669 * PRE CONDITION: Already have the AG lock for this AG. 1670 * 1671 * PARAMETERS: 1672 * imap - pointer to inode map control structure. 1673 * agno - allocation group to allocate from. 1674 * dir - TRUE if the new disk inode is for a directory. 1675 * ip - pointer to the new inode to be filled in on successful return 1676 * with the disk inode number allocated, its extent address 1677 * and the start of the ag. 1678 * 1679 * RETURN VALUES: 1680 * 0 - success. 1681 * -ENOSPC - insufficient disk resources. 1682 * -EIO - i/o error. 1683 */ 1684 static int 1685 diAllocAG(struct inomap * imap, int agno, boolean_t dir, struct inode *ip) 1686 { 1687 int rc, addext, numfree, numinos; 1688 1689 /* get the number of free and the number of backed disk 1690 * inodes currently within the ag. 1691 */ 1692 numfree = imap->im_agctl[agno].numfree; 1693 numinos = imap->im_agctl[agno].numinos; 1694 1695 if (numfree > numinos) { 1696 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos"); 1697 return -EIO; 1698 } 1699 1700 /* determine if we should allocate a new extent of free inodes 1701 * within the ag: for directory inodes, add a new extent 1702 * if there are a small number of free inodes or number of free 1703 * inodes is a small percentage of the number of backed inodes. 1704 */ 1705 if (dir == TRUE) 1706 addext = (numfree < 64 || 1707 (numfree < 256 1708 && ((numfree * 100) / numinos) <= 20)); 1709 else 1710 addext = (numfree == 0); 1711 1712 /* 1713 * try to allocate a new extent of free inodes. 1714 */ 1715 if (addext) { 1716 /* if free space is not avaliable for this new extent, try 1717 * below to allocate a free and existing (already backed) 1718 * inode from the ag. 1719 */ 1720 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC) 1721 return (rc); 1722 } 1723 1724 /* 1725 * try to allocate an existing free inode from the ag. 1726 */ 1727 return (diAllocIno(imap, agno, ip)); 1728 } 1729 1730 1731 /* 1732 * NAME: diAllocAny(imap,agno,dir,iap) 1733 * 1734 * FUNCTION: allocate a disk inode from any other allocation group. 1735 * 1736 * this routine is called when an allocation attempt within 1737 * the primary allocation group has failed. if attempts to 1738 * allocate an inode from any allocation group other than the 1739 * specified primary group. 1740 * 1741 * PARAMETERS: 1742 * imap - pointer to inode map control structure. 1743 * agno - primary allocation group (to avoid). 1744 * dir - TRUE if the new disk inode is for a directory. 1745 * ip - pointer to a new inode to be filled in on successful return 1746 * with the disk inode number allocated, its extent address 1747 * and the start of the ag. 1748 * 1749 * RETURN VALUES: 1750 * 0 - success. 1751 * -ENOSPC - insufficient disk resources. 1752 * -EIO - i/o error. 1753 */ 1754 static int 1755 diAllocAny(struct inomap * imap, int agno, boolean_t dir, struct inode *ip) 1756 { 1757 int ag, rc; 1758 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag; 1759 1760 1761 /* try to allocate from the ags following agno up to 1762 * the maximum ag number. 1763 */ 1764 for (ag = agno + 1; ag <= maxag; ag++) { 1765 AG_LOCK(imap, ag); 1766 1767 rc = diAllocAG(imap, ag, dir, ip); 1768 1769 AG_UNLOCK(imap, ag); 1770 1771 if (rc != -ENOSPC) 1772 return (rc); 1773 } 1774 1775 /* try to allocate from the ags in front of agno. 1776 */ 1777 for (ag = 0; ag < agno; ag++) { 1778 AG_LOCK(imap, ag); 1779 1780 rc = diAllocAG(imap, ag, dir, ip); 1781 1782 AG_UNLOCK(imap, ag); 1783 1784 if (rc != -ENOSPC) 1785 return (rc); 1786 } 1787 1788 /* no free disk inodes. 1789 */ 1790 return -ENOSPC; 1791 } 1792 1793 1794 /* 1795 * NAME: diAllocIno(imap,agno,ip) 1796 * 1797 * FUNCTION: allocate a disk inode from the allocation group's free 1798 * inode list, returning an error if this free list is 1799 * empty (i.e. no iags on the list). 1800 * 1801 * allocation occurs from the first iag on the list using 1802 * the iag's free inode summary map to find the leftmost 1803 * free inode in the iag. 1804 * 1805 * PRE CONDITION: Already have AG lock for this AG. 1806 * 1807 * PARAMETERS: 1808 * imap - pointer to inode map control structure. 1809 * agno - allocation group. 1810 * ip - pointer to new inode to be filled in on successful return 1811 * with the disk inode number allocated, its extent address 1812 * and the start of the ag. 1813 * 1814 * RETURN VALUES: 1815 * 0 - success. 1816 * -ENOSPC - insufficient disk resources. 1817 * -EIO - i/o error. 1818 */ 1819 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip) 1820 { 1821 int iagno, ino, rc, rem, extno, sword; 1822 struct metapage *mp; 1823 struct iag *iagp; 1824 1825 /* check if there are iags on the ag's free inode list. 1826 */ 1827 if ((iagno = imap->im_agctl[agno].inofree) < 0) 1828 return -ENOSPC; 1829 1830 /* obtain read lock on imap inode */ 1831 IREAD_LOCK(imap->im_ipimap); 1832 1833 /* read the iag at the head of the list. 1834 */ 1835 if ((rc = diIAGRead(imap, iagno, &mp))) { 1836 IREAD_UNLOCK(imap->im_ipimap); 1837 return (rc); 1838 } 1839 iagp = (struct iag *) mp->data; 1840 1841 /* better be free inodes in this iag if it is on the 1842 * list. 1843 */ 1844 if (!iagp->nfreeinos) { 1845 IREAD_UNLOCK(imap->im_ipimap); 1846 release_metapage(mp); 1847 jfs_error(ip->i_sb, 1848 "diAllocIno: nfreeinos = 0, but iag on freelist"); 1849 return -EIO; 1850 } 1851 1852 /* scan the free inode summary map to find an extent 1853 * with free inodes. 1854 */ 1855 for (sword = 0;; sword++) { 1856 if (sword >= SMAPSZ) { 1857 IREAD_UNLOCK(imap->im_ipimap); 1858 release_metapage(mp); 1859 jfs_error(ip->i_sb, 1860 "diAllocIno: free inode not found in summary map"); 1861 return -EIO; 1862 } 1863 1864 if (~iagp->inosmap[sword]) 1865 break; 1866 } 1867 1868 /* found a extent with free inodes. determine 1869 * the extent number. 1870 */ 1871 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0); 1872 if (rem >= EXTSPERSUM) { 1873 IREAD_UNLOCK(imap->im_ipimap); 1874 release_metapage(mp); 1875 jfs_error(ip->i_sb, "diAllocIno: no free extent found"); 1876 return -EIO; 1877 } 1878 extno = (sword << L2EXTSPERSUM) + rem; 1879 1880 /* find the first free inode in the extent. 1881 */ 1882 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0); 1883 if (rem >= INOSPEREXT) { 1884 IREAD_UNLOCK(imap->im_ipimap); 1885 release_metapage(mp); 1886 jfs_error(ip->i_sb, "diAllocIno: free inode not found"); 1887 return -EIO; 1888 } 1889 1890 /* compute the inode number within the iag. 1891 */ 1892 ino = (extno << L2INOSPEREXT) + rem; 1893 1894 /* allocate the inode. 1895 */ 1896 rc = diAllocBit(imap, iagp, ino); 1897 IREAD_UNLOCK(imap->im_ipimap); 1898 if (rc) { 1899 release_metapage(mp); 1900 return (rc); 1901 } 1902 1903 /* set the results of the allocation and write the iag. 1904 */ 1905 diInitInode(ip, iagno, ino, extno, iagp); 1906 write_metapage(mp); 1907 1908 return (0); 1909 } 1910 1911 1912 /* 1913 * NAME: diAllocExt(imap,agno,ip) 1914 * 1915 * FUNCTION: add a new extent of free inodes to an iag, allocating 1916 * an inode from this extent to satisfy the current allocation 1917 * request. 1918 * 1919 * this routine first tries to find an existing iag with free 1920 * extents through the ag free extent list. if list is not 1921 * empty, the head of the list will be selected as the home 1922 * of the new extent of free inodes. otherwise (the list is 1923 * empty), a new iag will be allocated for the ag to contain 1924 * the extent. 1925 * 1926 * once an iag has been selected, the free extent summary map 1927 * is used to locate a free extent within the iag and diNewExt() 1928 * is called to initialize the extent, with initialization 1929 * including the allocation of the first inode of the extent 1930 * for the purpose of satisfying this request. 1931 * 1932 * PARAMETERS: 1933 * imap - pointer to inode map control structure. 1934 * agno - allocation group number. 1935 * ip - pointer to new inode to be filled in on successful return 1936 * with the disk inode number allocated, its extent address 1937 * and the start of the ag. 1938 * 1939 * RETURN VALUES: 1940 * 0 - success. 1941 * -ENOSPC - insufficient disk resources. 1942 * -EIO - i/o error. 1943 */ 1944 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip) 1945 { 1946 int rem, iagno, sword, extno, rc; 1947 struct metapage *mp; 1948 struct iag *iagp; 1949 1950 /* check if the ag has any iags with free extents. if not, 1951 * allocate a new iag for the ag. 1952 */ 1953 if ((iagno = imap->im_agctl[agno].extfree) < 0) { 1954 /* If successful, diNewIAG will obtain the read lock on the 1955 * imap inode. 1956 */ 1957 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) { 1958 return (rc); 1959 } 1960 iagp = (struct iag *) mp->data; 1961 1962 /* set the ag number if this a brand new iag 1963 */ 1964 iagp->agstart = 1965 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap)); 1966 } else { 1967 /* read the iag. 1968 */ 1969 IREAD_LOCK(imap->im_ipimap); 1970 if ((rc = diIAGRead(imap, iagno, &mp))) { 1971 IREAD_UNLOCK(imap->im_ipimap); 1972 jfs_error(ip->i_sb, "diAllocExt: error reading iag"); 1973 return rc; 1974 } 1975 iagp = (struct iag *) mp->data; 1976 } 1977 1978 /* using the free extent summary map, find a free extent. 1979 */ 1980 for (sword = 0;; sword++) { 1981 if (sword >= SMAPSZ) { 1982 release_metapage(mp); 1983 IREAD_UNLOCK(imap->im_ipimap); 1984 jfs_error(ip->i_sb, 1985 "diAllocExt: free ext summary map not found"); 1986 return -EIO; 1987 } 1988 if (~iagp->extsmap[sword]) 1989 break; 1990 } 1991 1992 /* determine the extent number of the free extent. 1993 */ 1994 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0); 1995 if (rem >= EXTSPERSUM) { 1996 release_metapage(mp); 1997 IREAD_UNLOCK(imap->im_ipimap); 1998 jfs_error(ip->i_sb, "diAllocExt: free extent not found"); 1999 return -EIO; 2000 } 2001 extno = (sword << L2EXTSPERSUM) + rem; 2002 2003 /* initialize the new extent. 2004 */ 2005 rc = diNewExt(imap, iagp, extno); 2006 IREAD_UNLOCK(imap->im_ipimap); 2007 if (rc) { 2008 /* something bad happened. if a new iag was allocated, 2009 * place it back on the inode map's iag free list, and 2010 * clear the ag number information. 2011 */ 2012 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) { 2013 IAGFREE_LOCK(imap); 2014 iagp->iagfree = cpu_to_le32(imap->im_freeiag); 2015 imap->im_freeiag = iagno; 2016 IAGFREE_UNLOCK(imap); 2017 } 2018 write_metapage(mp); 2019 return (rc); 2020 } 2021 2022 /* set the results of the allocation and write the iag. 2023 */ 2024 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp); 2025 2026 write_metapage(mp); 2027 2028 return (0); 2029 } 2030 2031 2032 /* 2033 * NAME: diAllocBit(imap,iagp,ino) 2034 * 2035 * FUNCTION: allocate a backed inode from an iag. 2036 * 2037 * this routine performs the mechanics of allocating a 2038 * specified inode from a backed extent. 2039 * 2040 * if the inode to be allocated represents the last free 2041 * inode within the iag, the iag will be removed from the 2042 * ag free inode list. 2043 * 2044 * a careful update approach is used to provide consistency 2045 * in the face of updates to multiple buffers. under this 2046 * approach, all required buffers are obtained before making 2047 * any updates and are held all are updates are complete. 2048 * 2049 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on 2050 * this AG. Must have read lock on imap inode. 2051 * 2052 * PARAMETERS: 2053 * imap - pointer to inode map control structure. 2054 * iagp - pointer to iag. 2055 * ino - inode number to be allocated within the iag. 2056 * 2057 * RETURN VALUES: 2058 * 0 - success. 2059 * -ENOSPC - insufficient disk resources. 2060 * -EIO - i/o error. 2061 */ 2062 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino) 2063 { 2064 int extno, bitno, agno, sword, rc; 2065 struct metapage *amp = NULL, *bmp = NULL; 2066 struct iag *aiagp = NULL, *biagp = NULL; 2067 u32 mask; 2068 2069 /* check if this is the last free inode within the iag. 2070 * if so, it will have to be removed from the ag free 2071 * inode list, so get the iags preceeding and following 2072 * it on the list. 2073 */ 2074 if (iagp->nfreeinos == cpu_to_le32(1)) { 2075 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) { 2076 if ((rc = 2077 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd), 2078 &))) 2079 return (rc); 2080 aiagp = (struct iag *) amp->data; 2081 } 2082 2083 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) { 2084 if ((rc = 2085 diIAGRead(imap, 2086 le32_to_cpu(iagp->inofreeback), 2087 &bmp))) { 2088 if (amp) 2089 release_metapage(amp); 2090 return (rc); 2091 } 2092 biagp = (struct iag *) bmp->data; 2093 } 2094 } 2095 2096 /* get the ag number, extent number, inode number within 2097 * the extent. 2098 */ 2099 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb)); 2100 extno = ino >> L2INOSPEREXT; 2101 bitno = ino & (INOSPEREXT - 1); 2102 2103 /* compute the mask for setting the map. 2104 */ 2105 mask = HIGHORDER >> bitno; 2106 2107 /* the inode should be free and backed. 2108 */ 2109 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) || 2110 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) || 2111 (addressPXD(&iagp->inoext[extno]) == 0)) { 2112 if (amp) 2113 release_metapage(amp); 2114 if (bmp) 2115 release_metapage(bmp); 2116 2117 jfs_error(imap->im_ipimap->i_sb, 2118 "diAllocBit: iag inconsistent"); 2119 return -EIO; 2120 } 2121 2122 /* mark the inode as allocated in the working map. 2123 */ 2124 iagp->wmap[extno] |= cpu_to_le32(mask); 2125 2126 /* check if all inodes within the extent are now 2127 * allocated. if so, update the free inode summary 2128 * map to reflect this. 2129 */ 2130 if (iagp->wmap[extno] == cpu_to_le32(ONES)) { 2131 sword = extno >> L2EXTSPERSUM; 2132 bitno = extno & (EXTSPERSUM - 1); 2133 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno); 2134 } 2135 2136 /* if this was the last free inode in the iag, remove the 2137 * iag from the ag free inode list. 2138 */ 2139 if (iagp->nfreeinos == cpu_to_le32(1)) { 2140 if (amp) { 2141 aiagp->inofreeback = iagp->inofreeback; 2142 write_metapage(amp); 2143 } 2144 2145 if (bmp) { 2146 biagp->inofreefwd = iagp->inofreefwd; 2147 write_metapage(bmp); 2148 } else { 2149 imap->im_agctl[agno].inofree = 2150 le32_to_cpu(iagp->inofreefwd); 2151 } 2152 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1); 2153 } 2154 2155 /* update the free inode count at the iag, ag, inode 2156 * map levels. 2157 */ 2158 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1); 2159 imap->im_agctl[agno].numfree -= 1; 2160 atomic_dec(&imap->im_numfree); 2161 2162 return (0); 2163 } 2164 2165 2166 /* 2167 * NAME: diNewExt(imap,iagp,extno) 2168 * 2169 * FUNCTION: initialize a new extent of inodes for an iag, allocating 2170 * the first inode of the extent for use for the current 2171 * allocation request. 2172 * 2173 * disk resources are allocated for the new extent of inodes 2174 * and the inodes themselves are initialized to reflect their 2175 * existence within the extent (i.e. their inode numbers and 2176 * inode extent addresses are set) and their initial state 2177 * (mode and link count are set to zero). 2178 * 2179 * if the iag is new, it is not yet on an ag extent free list 2180 * but will now be placed on this list. 2181 * 2182 * if the allocation of the new extent causes the iag to 2183 * have no free extent, the iag will be removed from the 2184 * ag extent free list. 2185 * 2186 * if the iag has no free backed inodes, it will be placed 2187 * on the ag free inode list, since the addition of the new 2188 * extent will now cause it to have free inodes. 2189 * 2190 * a careful update approach is used to provide consistency 2191 * (i.e. list consistency) in the face of updates to multiple 2192 * buffers. under this approach, all required buffers are 2193 * obtained before making any updates and are held until all 2194 * updates are complete. 2195 * 2196 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on 2197 * this AG. Must have read lock on imap inode. 2198 * 2199 * PARAMETERS: 2200 * imap - pointer to inode map control structure. 2201 * iagp - pointer to iag. 2202 * extno - extent number. 2203 * 2204 * RETURN VALUES: 2205 * 0 - success. 2206 * -ENOSPC - insufficient disk resources. 2207 * -EIO - i/o error. 2208 */ 2209 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno) 2210 { 2211 int agno, iagno, fwd, back, freei = 0, sword, rc; 2212 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL; 2213 struct metapage *amp, *bmp, *cmp, *dmp; 2214 struct inode *ipimap; 2215 s64 blkno, hint; 2216 int i, j; 2217 u32 mask; 2218 ino_t ino; 2219 struct dinode *dp; 2220 struct jfs_sb_info *sbi; 2221 2222 /* better have free extents. 2223 */ 2224 if (!iagp->nfreeexts) { 2225 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents"); 2226 return -EIO; 2227 } 2228 2229 /* get the inode map inode. 2230 */ 2231 ipimap = imap->im_ipimap; 2232 sbi = JFS_SBI(ipimap->i_sb); 2233 2234 amp = bmp = cmp = NULL; 2235 2236 /* get the ag and iag numbers for this iag. 2237 */ 2238 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi); 2239 iagno = le32_to_cpu(iagp->iagnum); 2240 2241 /* check if this is the last free extent within the 2242 * iag. if so, the iag must be removed from the ag 2243 * free extent list, so get the iags preceeding and 2244 * following the iag on this list. 2245 */ 2246 if (iagp->nfreeexts == cpu_to_le32(1)) { 2247 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) { 2248 if ((rc = diIAGRead(imap, fwd, &))) 2249 return (rc); 2250 aiagp = (struct iag *) amp->data; 2251 } 2252 2253 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) { 2254 if ((rc = diIAGRead(imap, back, &bmp))) 2255 goto error_out; 2256 biagp = (struct iag *) bmp->data; 2257 } 2258 } else { 2259 /* the iag has free extents. if all extents are free 2260 * (as is the case for a newly allocated iag), the iag 2261 * must be added to the ag free extent list, so get 2262 * the iag at the head of the list in preparation for 2263 * adding this iag to this list. 2264 */ 2265 fwd = back = -1; 2266 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) { 2267 if ((fwd = imap->im_agctl[agno].extfree) >= 0) { 2268 if ((rc = diIAGRead(imap, fwd, &))) 2269 goto error_out; 2270 aiagp = (struct iag *) amp->data; 2271 } 2272 } 2273 } 2274 2275 /* check if the iag has no free inodes. if so, the iag 2276 * will have to be added to the ag free inode list, so get 2277 * the iag at the head of the list in preparation for 2278 * adding this iag to this list. in doing this, we must 2279 * check if we already have the iag at the head of 2280 * the list in hand. 2281 */ 2282 if (iagp->nfreeinos == 0) { 2283 freei = imap->im_agctl[agno].inofree; 2284 2285 if (freei >= 0) { 2286 if (freei == fwd) { 2287 ciagp = aiagp; 2288 } else if (freei == back) { 2289 ciagp = biagp; 2290 } else { 2291 if ((rc = diIAGRead(imap, freei, &cmp))) 2292 goto error_out; 2293 ciagp = (struct iag *) cmp->data; 2294 } 2295 if (ciagp == NULL) { 2296 jfs_error(imap->im_ipimap->i_sb, 2297 "diNewExt: ciagp == NULL"); 2298 rc = -EIO; 2299 goto error_out; 2300 } 2301 } 2302 } 2303 2304 /* allocate disk space for the inode extent. 2305 */ 2306 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0)) 2307 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1; 2308 else 2309 hint = addressPXD(&iagp->inoext[extno - 1]) + 2310 lengthPXD(&iagp->inoext[extno - 1]) - 1; 2311 2312 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno))) 2313 goto error_out; 2314 2315 /* compute the inode number of the first inode within the 2316 * extent. 2317 */ 2318 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT); 2319 2320 /* initialize the inodes within the newly allocated extent a 2321 * page at a time. 2322 */ 2323 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) { 2324 /* get a buffer for this page of disk inodes. 2325 */ 2326 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1); 2327 if (dmp == NULL) { 2328 rc = -EIO; 2329 goto error_out; 2330 } 2331 dp = (struct dinode *) dmp->data; 2332 2333 /* initialize the inode number, mode, link count and 2334 * inode extent address. 2335 */ 2336 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) { 2337 dp->di_inostamp = cpu_to_le32(sbi->inostamp); 2338 dp->di_number = cpu_to_le32(ino); 2339 dp->di_fileset = cpu_to_le32(FILESYSTEM_I); 2340 dp->di_mode = 0; 2341 dp->di_nlink = 0; 2342 PXDaddress(&(dp->di_ixpxd), blkno); 2343 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext); 2344 } 2345 write_metapage(dmp); 2346 } 2347 2348 /* if this is the last free extent within the iag, remove the 2349 * iag from the ag free extent list. 2350 */ 2351 if (iagp->nfreeexts == cpu_to_le32(1)) { 2352 if (fwd >= 0) 2353 aiagp->extfreeback = iagp->extfreeback; 2354 2355 if (back >= 0) 2356 biagp->extfreefwd = iagp->extfreefwd; 2357 else 2358 imap->im_agctl[agno].extfree = 2359 le32_to_cpu(iagp->extfreefwd); 2360 2361 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1); 2362 } else { 2363 /* if the iag has all free extents (newly allocated iag), 2364 * add the iag to the ag free extent list. 2365 */ 2366 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) { 2367 if (fwd >= 0) 2368 aiagp->extfreeback = cpu_to_le32(iagno); 2369 2370 iagp->extfreefwd = cpu_to_le32(fwd); 2371 iagp->extfreeback = cpu_to_le32(-1); 2372 imap->im_agctl[agno].extfree = iagno; 2373 } 2374 } 2375 2376 /* if the iag has no free inodes, add the iag to the 2377 * ag free inode list. 2378 */ 2379 if (iagp->nfreeinos == 0) { 2380 if (freei >= 0) 2381 ciagp->inofreeback = cpu_to_le32(iagno); 2382 2383 iagp->inofreefwd = 2384 cpu_to_le32(imap->im_agctl[agno].inofree); 2385 iagp->inofreeback = cpu_to_le32(-1); 2386 imap->im_agctl[agno].inofree = iagno; 2387 } 2388 2389 /* initialize the extent descriptor of the extent. */ 2390 PXDlength(&iagp->inoext[extno], imap->im_nbperiext); 2391 PXDaddress(&iagp->inoext[extno], blkno); 2392 2393 /* initialize the working and persistent map of the extent. 2394 * the working map will be initialized such that 2395 * it indicates the first inode of the extent is allocated. 2396 */ 2397 iagp->wmap[extno] = cpu_to_le32(HIGHORDER); 2398 iagp->pmap[extno] = 0; 2399 2400 /* update the free inode and free extent summary maps 2401 * for the extent to indicate the extent has free inodes 2402 * and no longer represents a free extent. 2403 */ 2404 sword = extno >> L2EXTSPERSUM; 2405 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1)); 2406 iagp->extsmap[sword] |= cpu_to_le32(mask); 2407 iagp->inosmap[sword] &= cpu_to_le32(~mask); 2408 2409 /* update the free inode and free extent counts for the 2410 * iag. 2411 */ 2412 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 2413 (INOSPEREXT - 1)); 2414 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1); 2415 2416 /* update the free and backed inode counts for the ag. 2417 */ 2418 imap->im_agctl[agno].numfree += (INOSPEREXT - 1); 2419 imap->im_agctl[agno].numinos += INOSPEREXT; 2420 2421 /* update the free and backed inode counts for the inode map. 2422 */ 2423 atomic_add(INOSPEREXT - 1, &imap->im_numfree); 2424 atomic_add(INOSPEREXT, &imap->im_numinos); 2425 2426 /* write the iags. 2427 */ 2428 if (amp) 2429 write_metapage(amp); 2430 if (bmp) 2431 write_metapage(bmp); 2432 if (cmp) 2433 write_metapage(cmp); 2434 2435 return (0); 2436 2437 error_out: 2438 2439 /* release the iags. 2440 */ 2441 if (amp) 2442 release_metapage(amp); 2443 if (bmp) 2444 release_metapage(bmp); 2445 if (cmp) 2446 release_metapage(cmp); 2447 2448 return (rc); 2449 } 2450 2451 2452 /* 2453 * NAME: diNewIAG(imap,iagnop,agno) 2454 * 2455 * FUNCTION: allocate a new iag for an allocation group. 2456 * 2457 * first tries to allocate the iag from the inode map 2458 * iagfree list: 2459 * if the list has free iags, the head of the list is removed 2460 * and returned to satisfy the request. 2461 * if the inode map's iag free list is empty, the inode map 2462 * is extended to hold a new iag. this new iag is initialized 2463 * and returned to satisfy the request. 2464 * 2465 * PARAMETERS: 2466 * imap - pointer to inode map control structure. 2467 * iagnop - pointer to an iag number set with the number of the 2468 * newly allocated iag upon successful return. 2469 * agno - allocation group number. 2470 * bpp - Buffer pointer to be filled in with new IAG's buffer 2471 * 2472 * RETURN VALUES: 2473 * 0 - success. 2474 * -ENOSPC - insufficient disk resources. 2475 * -EIO - i/o error. 2476 * 2477 * serialization: 2478 * AG lock held on entry/exit; 2479 * write lock on the map is held inside; 2480 * read lock on the map is held on successful completion; 2481 * 2482 * note: new iag transaction: 2483 * . synchronously write iag; 2484 * . write log of xtree and inode of imap; 2485 * . commit; 2486 * . synchronous write of xtree (right to left, bottom to top); 2487 * . at start of logredo(): init in-memory imap with one additional iag page; 2488 * . at end of logredo(): re-read imap inode to determine 2489 * new imap size; 2490 */ 2491 static int 2492 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp) 2493 { 2494 int rc; 2495 int iagno, i, xlen; 2496 struct inode *ipimap; 2497 struct super_block *sb; 2498 struct jfs_sb_info *sbi; 2499 struct metapage *mp; 2500 struct iag *iagp; 2501 s64 xaddr = 0; 2502 s64 blkno; 2503 tid_t tid; 2504 #ifdef _STILL_TO_PORT 2505 xad_t xad; 2506 #endif /* _STILL_TO_PORT */ 2507 struct inode *iplist[1]; 2508 2509 /* pick up pointers to the inode map and mount inodes */ 2510 ipimap = imap->im_ipimap; 2511 sb = ipimap->i_sb; 2512 sbi = JFS_SBI(sb); 2513 2514 /* acquire the free iag lock */ 2515 IAGFREE_LOCK(imap); 2516 2517 /* if there are any iags on the inode map free iag list, 2518 * allocate the iag from the head of the list. 2519 */ 2520 if (imap->im_freeiag >= 0) { 2521 /* pick up the iag number at the head of the list */ 2522 iagno = imap->im_freeiag; 2523 2524 /* determine the logical block number of the iag */ 2525 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage); 2526 } else { 2527 /* no free iags. the inode map will have to be extented 2528 * to include a new iag. 2529 */ 2530 2531 /* acquire inode map lock */ 2532 IWRITE_LOCK(ipimap); 2533 2534 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) { 2535 IWRITE_UNLOCK(ipimap); 2536 IAGFREE_UNLOCK(imap); 2537 jfs_error(imap->im_ipimap->i_sb, 2538 "diNewIAG: ipimap->i_size is wrong"); 2539 return -EIO; 2540 } 2541 2542 2543 /* get the next avaliable iag number */ 2544 iagno = imap->im_nextiag; 2545 2546 /* make sure that we have not exceeded the maximum inode 2547 * number limit. 2548 */ 2549 if (iagno > (MAXIAGS - 1)) { 2550 /* release the inode map lock */ 2551 IWRITE_UNLOCK(ipimap); 2552 2553 rc = -ENOSPC; 2554 goto out; 2555 } 2556 2557 /* 2558 * synchronously append new iag page. 2559 */ 2560 /* determine the logical address of iag page to append */ 2561 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage); 2562 2563 /* Allocate extent for new iag page */ 2564 xlen = sbi->nbperpage; 2565 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) { 2566 /* release the inode map lock */ 2567 IWRITE_UNLOCK(ipimap); 2568 2569 goto out; 2570 } 2571 2572 /* 2573 * start transaction of update of the inode map 2574 * addressing structure pointing to the new iag page; 2575 */ 2576 tid = txBegin(sb, COMMIT_FORCE); 2577 down(&JFS_IP(ipimap)->commit_sem); 2578 2579 /* update the inode map addressing structure to point to it */ 2580 if ((rc = 2581 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) { 2582 txEnd(tid); 2583 up(&JFS_IP(ipimap)->commit_sem); 2584 /* Free the blocks allocated for the iag since it was 2585 * not successfully added to the inode map 2586 */ 2587 dbFree(ipimap, xaddr, (s64) xlen); 2588 2589 /* release the inode map lock */ 2590 IWRITE_UNLOCK(ipimap); 2591 2592 goto out; 2593 } 2594 2595 /* update the inode map's inode to reflect the extension */ 2596 ipimap->i_size += PSIZE; 2597 inode_add_bytes(ipimap, PSIZE); 2598 2599 /* assign a buffer for the page */ 2600 mp = get_metapage(ipimap, blkno, PSIZE, 0); 2601 if (!mp) { 2602 /* 2603 * This is very unlikely since we just created the 2604 * extent, but let's try to handle it correctly 2605 */ 2606 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE, 2607 COMMIT_PWMAP); 2608 2609 txAbort(tid, 0); 2610 txEnd(tid); 2611 2612 /* release the inode map lock */ 2613 IWRITE_UNLOCK(ipimap); 2614 2615 rc = -EIO; 2616 goto out; 2617 } 2618 iagp = (struct iag *) mp->data; 2619 2620 /* init the iag */ 2621 memset(iagp, 0, sizeof(struct iag)); 2622 iagp->iagnum = cpu_to_le32(iagno); 2623 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1); 2624 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1); 2625 iagp->iagfree = cpu_to_le32(-1); 2626 iagp->nfreeinos = 0; 2627 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG); 2628 2629 /* initialize the free inode summary map (free extent 2630 * summary map initialization handled by bzero). 2631 */ 2632 for (i = 0; i < SMAPSZ; i++) 2633 iagp->inosmap[i] = cpu_to_le32(ONES); 2634 2635 /* 2636 * Write and sync the metapage 2637 */ 2638 flush_metapage(mp); 2639 2640 /* 2641 * txCommit(COMMIT_FORCE) will synchronously write address 2642 * index pages and inode after commit in careful update order 2643 * of address index pages (right to left, bottom up); 2644 */ 2645 iplist[0] = ipimap; 2646 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE); 2647 2648 txEnd(tid); 2649 up(&JFS_IP(ipimap)->commit_sem); 2650 2651 duplicateIXtree(sb, blkno, xlen, &xaddr); 2652 2653 /* update the next avaliable iag number */ 2654 imap->im_nextiag += 1; 2655 2656 /* Add the iag to the iag free list so we don't lose the iag 2657 * if a failure happens now. 2658 */ 2659 imap->im_freeiag = iagno; 2660 2661 /* Until we have logredo working, we want the imap inode & 2662 * control page to be up to date. 2663 */ 2664 diSync(ipimap); 2665 2666 /* release the inode map lock */ 2667 IWRITE_UNLOCK(ipimap); 2668 } 2669 2670 /* obtain read lock on map */ 2671 IREAD_LOCK(ipimap); 2672 2673 /* read the iag */ 2674 if ((rc = diIAGRead(imap, iagno, &mp))) { 2675 IREAD_UNLOCK(ipimap); 2676 rc = -EIO; 2677 goto out; 2678 } 2679 iagp = (struct iag *) mp->data; 2680 2681 /* remove the iag from the iag free list */ 2682 imap->im_freeiag = le32_to_cpu(iagp->iagfree); 2683 iagp->iagfree = cpu_to_le32(-1); 2684 2685 /* set the return iag number and buffer pointer */ 2686 *iagnop = iagno; 2687 *mpp = mp; 2688 2689 out: 2690 /* release the iag free lock */ 2691 IAGFREE_UNLOCK(imap); 2692 2693 return (rc); 2694 } 2695 2696 /* 2697 * NAME: diIAGRead() 2698 * 2699 * FUNCTION: get the buffer for the specified iag within a fileset 2700 * or aggregate inode map. 2701 * 2702 * PARAMETERS: 2703 * imap - pointer to inode map control structure. 2704 * iagno - iag number. 2705 * bpp - point to buffer pointer to be filled in on successful 2706 * exit. 2707 * 2708 * SERIALIZATION: 2709 * must have read lock on imap inode 2710 * (When called by diExtendFS, the filesystem is quiesced, therefore 2711 * the read lock is unnecessary.) 2712 * 2713 * RETURN VALUES: 2714 * 0 - success. 2715 * -EIO - i/o error. 2716 */ 2717 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp) 2718 { 2719 struct inode *ipimap = imap->im_ipimap; 2720 s64 blkno; 2721 2722 /* compute the logical block number of the iag. */ 2723 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage); 2724 2725 /* read the iag. */ 2726 *mpp = read_metapage(ipimap, blkno, PSIZE, 0); 2727 if (*mpp == NULL) { 2728 return -EIO; 2729 } 2730 2731 return (0); 2732 } 2733 2734 /* 2735 * NAME: diFindFree() 2736 * 2737 * FUNCTION: find the first free bit in a word starting at 2738 * the specified bit position. 2739 * 2740 * PARAMETERS: 2741 * word - word to be examined. 2742 * start - starting bit position. 2743 * 2744 * RETURN VALUES: 2745 * bit position of first free bit in the word or 32 if 2746 * no free bits were found. 2747 */ 2748 static int diFindFree(u32 word, int start) 2749 { 2750 int bitno; 2751 assert(start < 32); 2752 /* scan the word for the first free bit. */ 2753 for (word <<= start, bitno = start; bitno < 32; 2754 bitno++, word <<= 1) { 2755 if ((word & HIGHORDER) == 0) 2756 break; 2757 } 2758 return (bitno); 2759 } 2760 2761 /* 2762 * NAME: diUpdatePMap() 2763 * 2764 * FUNCTION: Update the persistent map in an IAG for the allocation or 2765 * freeing of the specified inode. 2766 * 2767 * PRE CONDITIONS: Working map has already been updated for allocate. 2768 * 2769 * PARAMETERS: 2770 * ipimap - Incore inode map inode 2771 * inum - Number of inode to mark in permanent map 2772 * is_free - If TRUE indicates inode should be marked freed, otherwise 2773 * indicates inode should be marked allocated. 2774 * 2775 * RETURN VALUES: 2776 * 0 for success 2777 */ 2778 int 2779 diUpdatePMap(struct inode *ipimap, 2780 unsigned long inum, boolean_t is_free, struct tblock * tblk) 2781 { 2782 int rc; 2783 struct iag *iagp; 2784 struct metapage *mp; 2785 int iagno, ino, extno, bitno; 2786 struct inomap *imap; 2787 u32 mask; 2788 struct jfs_log *log; 2789 int lsn, difft, diffp; 2790 unsigned long flags; 2791 2792 imap = JFS_IP(ipimap)->i_imap; 2793 /* get the iag number containing the inode */ 2794 iagno = INOTOIAG(inum); 2795 /* make sure that the iag is contained within the map */ 2796 if (iagno >= imap->im_nextiag) { 2797 jfs_error(ipimap->i_sb, 2798 "diUpdatePMap: the iag is outside the map"); 2799 return -EIO; 2800 } 2801 /* read the iag */ 2802 IREAD_LOCK(ipimap); 2803 rc = diIAGRead(imap, iagno, &mp); 2804 IREAD_UNLOCK(ipimap); 2805 if (rc) 2806 return (rc); 2807 metapage_wait_for_io(mp); 2808 iagp = (struct iag *) mp->data; 2809 /* get the inode number and extent number of the inode within 2810 * the iag and the inode number within the extent. 2811 */ 2812 ino = inum & (INOSPERIAG - 1); 2813 extno = ino >> L2INOSPEREXT; 2814 bitno = ino & (INOSPEREXT - 1); 2815 mask = HIGHORDER >> bitno; 2816 /* 2817 * mark the inode free in persistent map: 2818 */ 2819 if (is_free == TRUE) { 2820 /* The inode should have been allocated both in working 2821 * map and in persistent map; 2822 * the inode will be freed from working map at the release 2823 * of last reference release; 2824 */ 2825 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) { 2826 jfs_error(ipimap->i_sb, 2827 "diUpdatePMap: inode %ld not marked as " 2828 "allocated in wmap!", inum); 2829 } 2830 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) { 2831 jfs_error(ipimap->i_sb, 2832 "diUpdatePMap: inode %ld not marked as " 2833 "allocated in pmap!", inum); 2834 } 2835 /* update the bitmap for the extent of the freed inode */ 2836 iagp->pmap[extno] &= cpu_to_le32(~mask); 2837 } 2838 /* 2839 * mark the inode allocated in persistent map: 2840 */ 2841 else { 2842 /* The inode should be already allocated in the working map 2843 * and should be free in persistent map; 2844 */ 2845 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) { 2846 release_metapage(mp); 2847 jfs_error(ipimap->i_sb, 2848 "diUpdatePMap: the inode is not allocated in " 2849 "the working map"); 2850 return -EIO; 2851 } 2852 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) { 2853 release_metapage(mp); 2854 jfs_error(ipimap->i_sb, 2855 "diUpdatePMap: the inode is not free in the " 2856 "persistent map"); 2857 return -EIO; 2858 } 2859 /* update the bitmap for the extent of the allocated inode */ 2860 iagp->pmap[extno] |= cpu_to_le32(mask); 2861 } 2862 /* 2863 * update iag lsn 2864 */ 2865 lsn = tblk->lsn; 2866 log = JFS_SBI(tblk->sb)->log; 2867 if (mp->lsn != 0) { 2868 /* inherit older/smaller lsn */ 2869 logdiff(difft, lsn, log); 2870 logdiff(diffp, mp->lsn, log); 2871 LOGSYNC_LOCK(log, flags); 2872 if (difft < diffp) { 2873 mp->lsn = lsn; 2874 /* move mp after tblock in logsync list */ 2875 list_move(&mp->synclist, &tblk->synclist); 2876 } 2877 /* inherit younger/larger clsn */ 2878 assert(mp->clsn); 2879 logdiff(difft, tblk->clsn, log); 2880 logdiff(diffp, mp->clsn, log); 2881 if (difft > diffp) 2882 mp->clsn = tblk->clsn; 2883 LOGSYNC_UNLOCK(log, flags); 2884 } else { 2885 mp->log = log; 2886 mp->lsn = lsn; 2887 /* insert mp after tblock in logsync list */ 2888 LOGSYNC_LOCK(log, flags); 2889 log->count++; 2890 list_add(&mp->synclist, &tblk->synclist); 2891 mp->clsn = tblk->clsn; 2892 LOGSYNC_UNLOCK(log, flags); 2893 } 2894 write_metapage(mp); 2895 return (0); 2896 } 2897 2898 /* 2899 * diExtendFS() 2900 * 2901 * function: update imap for extendfs(); 2902 * 2903 * note: AG size has been increased s.t. each k old contiguous AGs are 2904 * coalesced into a new AG; 2905 */ 2906 int diExtendFS(struct inode *ipimap, struct inode *ipbmap) 2907 { 2908 int rc, rcx = 0; 2909 struct inomap *imap = JFS_IP(ipimap)->i_imap; 2910 struct iag *iagp = NULL, *hiagp = NULL; 2911 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap; 2912 struct metapage *bp, *hbp; 2913 int i, n, head; 2914 int numinos, xnuminos = 0, xnumfree = 0; 2915 s64 agstart; 2916 2917 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d", 2918 imap->im_nextiag, atomic_read(&imap->im_numinos), 2919 atomic_read(&imap->im_numfree)); 2920 2921 /* 2922 * reconstruct imap 2923 * 2924 * coalesce contiguous k (newAGSize/oldAGSize) AGs; 2925 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn; 2926 * note: new AG size = old AG size * (2**x). 2927 */ 2928 2929 /* init per AG control information im_agctl[] */ 2930 for (i = 0; i < MAXAG; i++) { 2931 imap->im_agctl[i].inofree = -1; 2932 imap->im_agctl[i].extfree = -1; 2933 imap->im_agctl[i].numinos = 0; /* number of backed inodes */ 2934 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */ 2935 } 2936 2937 /* 2938 * process each iag page of the map. 2939 * 2940 * rebuild AG Free Inode List, AG Free Inode Extent List; 2941 */ 2942 for (i = 0; i < imap->im_nextiag; i++) { 2943 if ((rc = diIAGRead(imap, i, &bp))) { 2944 rcx = rc; 2945 continue; 2946 } 2947 iagp = (struct iag *) bp->data; 2948 if (le32_to_cpu(iagp->iagnum) != i) { 2949 release_metapage(bp); 2950 jfs_error(ipimap->i_sb, 2951 "diExtendFs: unexpected value of iagnum"); 2952 return -EIO; 2953 } 2954 2955 /* leave free iag in the free iag list */ 2956 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) { 2957 release_metapage(bp); 2958 continue; 2959 } 2960 2961 /* agstart that computes to the same ag is treated as same; */ 2962 agstart = le64_to_cpu(iagp->agstart); 2963 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */ 2964 n = agstart >> mp->db_agl2size; 2965 2966 /* compute backed inodes */ 2967 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts)) 2968 << L2INOSPEREXT; 2969 if (numinos > 0) { 2970 /* merge AG backed inodes */ 2971 imap->im_agctl[n].numinos += numinos; 2972 xnuminos += numinos; 2973 } 2974 2975 /* if any backed free inodes, insert at AG free inode list */ 2976 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) { 2977 if ((head = imap->im_agctl[n].inofree) == -1) { 2978 iagp->inofreefwd = cpu_to_le32(-1); 2979 iagp->inofreeback = cpu_to_le32(-1); 2980 } else { 2981 if ((rc = diIAGRead(imap, head, &hbp))) { 2982 rcx = rc; 2983 goto nextiag; 2984 } 2985 hiagp = (struct iag *) hbp->data; 2986 hiagp->inofreeback = iagp->iagnum; 2987 iagp->inofreefwd = cpu_to_le32(head); 2988 iagp->inofreeback = cpu_to_le32(-1); 2989 write_metapage(hbp); 2990 } 2991 2992 imap->im_agctl[n].inofree = 2993 le32_to_cpu(iagp->iagnum); 2994 2995 /* merge AG backed free inodes */ 2996 imap->im_agctl[n].numfree += 2997 le32_to_cpu(iagp->nfreeinos); 2998 xnumfree += le32_to_cpu(iagp->nfreeinos); 2999 } 3000 3001 /* if any free extents, insert at AG free extent list */ 3002 if (le32_to_cpu(iagp->nfreeexts) > 0) { 3003 if ((head = imap->im_agctl[n].extfree) == -1) { 3004 iagp->extfreefwd = cpu_to_le32(-1); 3005 iagp->extfreeback = cpu_to_le32(-1); 3006 } else { 3007 if ((rc = diIAGRead(imap, head, &hbp))) { 3008 rcx = rc; 3009 goto nextiag; 3010 } 3011 hiagp = (struct iag *) hbp->data; 3012 hiagp->extfreeback = iagp->iagnum; 3013 iagp->extfreefwd = cpu_to_le32(head); 3014 iagp->extfreeback = cpu_to_le32(-1); 3015 write_metapage(hbp); 3016 } 3017 3018 imap->im_agctl[n].extfree = 3019 le32_to_cpu(iagp->iagnum); 3020 } 3021 3022 nextiag: 3023 write_metapage(bp); 3024 } 3025 3026 if (xnuminos != atomic_read(&imap->im_numinos) || 3027 xnumfree != atomic_read(&imap->im_numfree)) { 3028 jfs_error(ipimap->i_sb, 3029 "diExtendFs: numinos or numfree incorrect"); 3030 return -EIO; 3031 } 3032 3033 return rcx; 3034 } 3035 3036 3037 /* 3038 * duplicateIXtree() 3039 * 3040 * serialization: IWRITE_LOCK held on entry/exit 3041 * 3042 * note: shadow page with regular inode (rel.2); 3043 */ 3044 static void duplicateIXtree(struct super_block *sb, s64 blkno, 3045 int xlen, s64 *xaddr) 3046 { 3047 struct jfs_superblock *j_sb; 3048 struct buffer_head *bh; 3049 struct inode *ip; 3050 tid_t tid; 3051 3052 /* if AIT2 ipmap2 is bad, do not try to update it */ 3053 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */ 3054 return; 3055 ip = diReadSpecial(sb, FILESYSTEM_I, 1); 3056 if (ip == NULL) { 3057 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT; 3058 if (readSuper(sb, &bh)) 3059 return; 3060 j_sb = (struct jfs_superblock *)bh->b_data; 3061 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT); 3062 3063 mark_buffer_dirty(bh); 3064 sync_dirty_buffer(bh); 3065 brelse(bh); 3066 return; 3067 } 3068 3069 /* start transaction */ 3070 tid = txBegin(sb, COMMIT_FORCE); 3071 /* update the inode map addressing structure to point to it */ 3072 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) { 3073 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT; 3074 txAbort(tid, 1); 3075 goto cleanup; 3076 3077 } 3078 /* update the inode map's inode to reflect the extension */ 3079 ip->i_size += PSIZE; 3080 inode_add_bytes(ip, PSIZE); 3081 txCommit(tid, 1, &ip, COMMIT_FORCE); 3082 cleanup: 3083 txEnd(tid); 3084 diFreeSpecial(ip); 3085 } 3086 3087 /* 3088 * NAME: copy_from_dinode() 3089 * 3090 * FUNCTION: Copies inode info from disk inode to in-memory inode 3091 * 3092 * RETURN VALUES: 3093 * 0 - success 3094 * -ENOMEM - insufficient memory 3095 */ 3096 static int copy_from_dinode(struct dinode * dip, struct inode *ip) 3097 { 3098 struct jfs_inode_info *jfs_ip = JFS_IP(ip); 3099 3100 jfs_ip->fileset = le32_to_cpu(dip->di_fileset); 3101 jfs_ip->mode2 = le32_to_cpu(dip->di_mode); 3102 3103 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff; 3104 ip->i_nlink = le32_to_cpu(dip->di_nlink); 3105 ip->i_uid = le32_to_cpu(dip->di_uid); 3106 ip->i_gid = le32_to_cpu(dip->di_gid); 3107 ip->i_size = le64_to_cpu(dip->di_size); 3108 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec); 3109 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec); 3110 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec); 3111 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec); 3112 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec); 3113 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec); 3114 ip->i_blksize = ip->i_sb->s_blocksize; 3115 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks)); 3116 ip->i_generation = le32_to_cpu(dip->di_gen); 3117 3118 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */ 3119 jfs_ip->acl = dip->di_acl; /* as are dxd's */ 3120 jfs_ip->ea = dip->di_ea; 3121 jfs_ip->next_index = le32_to_cpu(dip->di_next_index); 3122 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec); 3123 jfs_ip->acltype = le32_to_cpu(dip->di_acltype); 3124 3125 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) { 3126 jfs_ip->dev = le32_to_cpu(dip->di_rdev); 3127 ip->i_rdev = new_decode_dev(jfs_ip->dev); 3128 } 3129 3130 if (S_ISDIR(ip->i_mode)) { 3131 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384); 3132 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) { 3133 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288); 3134 } else 3135 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128); 3136 3137 /* Zero the in-memory-only stuff */ 3138 jfs_ip->cflag = 0; 3139 jfs_ip->btindex = 0; 3140 jfs_ip->btorder = 0; 3141 jfs_ip->bxflag = 0; 3142 jfs_ip->blid = 0; 3143 jfs_ip->atlhead = 0; 3144 jfs_ip->atltail = 0; 3145 jfs_ip->xtlid = 0; 3146 return (0); 3147 } 3148 3149 /* 3150 * NAME: copy_to_dinode() 3151 * 3152 * FUNCTION: Copies inode info from in-memory inode to disk inode 3153 */ 3154 static void copy_to_dinode(struct dinode * dip, struct inode *ip) 3155 { 3156 struct jfs_inode_info *jfs_ip = JFS_IP(ip); 3157 3158 dip->di_fileset = cpu_to_le32(jfs_ip->fileset); 3159 dip->di_inostamp = cpu_to_le32(JFS_SBI(ip->i_sb)->inostamp); 3160 dip->di_number = cpu_to_le32(ip->i_ino); 3161 dip->di_gen = cpu_to_le32(ip->i_generation); 3162 dip->di_size = cpu_to_le64(ip->i_size); 3163 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks)); 3164 dip->di_nlink = cpu_to_le32(ip->i_nlink); 3165 dip->di_uid = cpu_to_le32(ip->i_uid); 3166 dip->di_gid = cpu_to_le32(ip->i_gid); 3167 /* 3168 * mode2 is only needed for storing the higher order bits. 3169 * Trust i_mode for the lower order ones 3170 */ 3171 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) | ip->i_mode); 3172 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec); 3173 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec); 3174 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec); 3175 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec); 3176 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec); 3177 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec); 3178 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */ 3179 dip->di_acl = jfs_ip->acl; /* as are dxd's */ 3180 dip->di_ea = jfs_ip->ea; 3181 dip->di_next_index = cpu_to_le32(jfs_ip->next_index); 3182 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime); 3183 dip->di_otime.tv_nsec = 0; 3184 dip->di_acltype = cpu_to_le32(jfs_ip->acltype); 3185 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) 3186 dip->di_rdev = cpu_to_le32(jfs_ip->dev); 3187 } 3188 3189 #ifdef _JFS_DEBUG_IMAP 3190 /* 3191 * DBGdiInit() 3192 */ 3193 static void *DBGdiInit(struct inomap * imap) 3194 { 3195 u32 *dimap; 3196 int size; 3197 size = 64 * 1024; 3198 if ((dimap = (u32 *) xmalloc(size, L2PSIZE, kernel_heap)) == NULL) 3199 assert(0); 3200 bzero((void *) dimap, size); 3201 imap->im_DBGdimap = dimap; 3202 } 3203 3204 /* 3205 * DBGdiAlloc() 3206 */ 3207 static void DBGdiAlloc(struct inomap * imap, ino_t ino) 3208 { 3209 u32 *dimap = imap->im_DBGdimap; 3210 int w, b; 3211 u32 m; 3212 w = ino >> 5; 3213 b = ino & 31; 3214 m = 0x80000000 >> b; 3215 assert(w < 64 * 256); 3216 if (dimap[w] & m) { 3217 printk("DEBUG diAlloc: duplicate alloc ino:0x%x\n", ino); 3218 } 3219 dimap[w] |= m; 3220 } 3221 3222 /* 3223 * DBGdiFree() 3224 */ 3225 static void DBGdiFree(struct inomap * imap, ino_t ino) 3226 { 3227 u32 *dimap = imap->im_DBGdimap; 3228 int w, b; 3229 u32 m; 3230 w = ino >> 5; 3231 b = ino & 31; 3232 m = 0x80000000 >> b; 3233 assert(w < 64 * 256); 3234 if ((dimap[w] & m) == 0) { 3235 printk("DEBUG diFree: duplicate free ino:0x%x\n", ino); 3236 } 3237 dimap[w] &= ~m; 3238 } 3239 3240 static void dump_cp(struct inomap * ipimap, char *function, int line) 3241 { 3242 printk("\n* ********* *\nControl Page %s %d\n", function, line); 3243 printk("FreeIAG %d\tNextIAG %d\n", ipimap->im_freeiag, 3244 ipimap->im_nextiag); 3245 printk("NumInos %d\tNumFree %d\n", 3246 atomic_read(&ipimap->im_numinos), 3247 atomic_read(&ipimap->im_numfree)); 3248 printk("AG InoFree %d\tAG ExtFree %d\n", 3249 ipimap->im_agctl[0].inofree, ipimap->im_agctl[0].extfree); 3250 printk("AG NumInos %d\tAG NumFree %d\n", 3251 ipimap->im_agctl[0].numinos, ipimap->im_agctl[0].numfree); 3252 } 3253 3254 static void dump_iag(struct iag * iag, char *function, int line) 3255 { 3256 printk("\n* ********* *\nIAG %s %d\n", function, line); 3257 printk("IagNum %d\tIAG Free %d\n", le32_to_cpu(iag->iagnum), 3258 le32_to_cpu(iag->iagfree)); 3259 printk("InoFreeFwd %d\tInoFreeBack %d\n", 3260 le32_to_cpu(iag->inofreefwd), 3261 le32_to_cpu(iag->inofreeback)); 3262 printk("ExtFreeFwd %d\tExtFreeBack %d\n", 3263 le32_to_cpu(iag->extfreefwd), 3264 le32_to_cpu(iag->extfreeback)); 3265 printk("NFreeInos %d\tNFreeExts %d\n", le32_to_cpu(iag->nfreeinos), 3266 le32_to_cpu(iag->nfreeexts)); 3267 } 3268 #endif /* _JFS_DEBUG_IMAP */ 3269