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