1 // File system implementation. Five layers: 2 // + Blocks: allocator for raw disk blocks. 3 // + Log: crash recovery for multi-step updates. 4 // + Files: inode allocator, reading, writing, metadata. 5 // + Directories: inode with special contents (list of other inodes!) 6 // + Names: paths like /usr/rtm/xv6/fs.c for convenient naming. 7 // 8 // This file contains the low-level file system manipulation 9 // routines. The (higher-level) system call implementations 10 // are in sysfile.c. 11 12 #include "types.h" 13 #include "defs.h" 14 #include "param.h" 15 #include "stat.h" 16 #include "mmu.h" 17 #include "proc.h" 18 #include "spinlock.h" 19 #include "sleeplock.h" 20 #include "fs.h" 21 #include "buf.h" 22 #include "file.h" 23 24 #define min(a, b) ((a) < (b) ? (a) : (b)) 25 static void itrunc(struct inode*); 26 // there should be one superblock per disk device, but we run with 27 // only one device 28 struct superblock sb; 29 30 // Read the super block. 31 void 32 readsb(int dev, struct superblock *sb) 33 { 34 struct buf *bp; 35 36 bp = bread(dev, 1); 37 memmove(sb, bp->data, sizeof(*sb)); 38 brelse(bp); 39 } 40 41 // Zero a block. 42 static void 43 bzero(int dev, int bno) 44 { 45 struct buf *bp; 46 47 bp = bread(dev, bno); 48 memset(bp->data, 0, BSIZE); 49 log_write(bp); 50 brelse(bp); 51 } 52 53 // Blocks. 54 55 // Allocate a zeroed disk block. 56 static uint 57 balloc(uint dev) 58 { 59 int b, bi, m; 60 struct buf *bp; 61 62 bp = 0; 63 for(b = 0; b < sb.size; b += BPB){ 64 bp = bread(dev, BBLOCK(b, sb)); 65 for(bi = 0; bi < BPB && b + bi < sb.size; bi++){ 66 m = 1 << (bi % 8); 67 if((bp->data[bi/8] & m) == 0){ // Is block free? 68 bp->data[bi/8] |= m; // Mark block in use. 69 log_write(bp); 70 brelse(bp); 71 bzero(dev, b + bi); 72 return b + bi; 73 } 74 } 75 brelse(bp); 76 } 77 panic("balloc: out of blocks"); 78 } 79 80 // Free a disk block. 81 static void 82 bfree(int dev, uint b) 83 { 84 struct buf *bp; 85 int bi, m; 86 87 readsb(dev, &sb); 88 bp = bread(dev, BBLOCK(b, sb)); 89 bi = b % BPB; 90 m = 1 << (bi % 8); 91 if((bp->data[bi/8] & m) == 0) 92 panic("freeing free block"); 93 bp->data[bi/8] &= ~m; 94 log_write(bp); 95 brelse(bp); 96 } 97 98 // Inodes. 99 // 100 // An inode describes a single unnamed file. 101 // The inode disk structure holds metadata: the file's type, 102 // its size, the number of links referring to it, and the 103 // list of blocks holding the file's content. 104 // 105 // The inodes are laid out sequentially on disk at 106 // sb.startinode. Each inode has a number, indicating its 107 // position on the disk. 108 // 109 // The kernel keeps a cache of in-use inodes in memory 110 // to provide a place for synchronizing access 111 // to inodes used by multiple processes. The cached 112 // inodes include book-keeping information that is 113 // not stored on disk: ip->ref and ip->flags. 114 // 115 // An inode and its in-memory representation go through a 116 // sequence of states before they can be used by the 117 // rest of the file system code. 118 // 119 // * Allocation: an inode is allocated if its type (on disk) 120 // is non-zero. ialloc() allocates, and iput() frees if 121 // the reference and link counts have fallen to zero. 122 // 123 // * Referencing in cache: an entry in the inode cache 124 // is free if ip->ref is zero. Otherwise ip->ref tracks 125 // the number of in-memory pointers to the entry (open 126 // files and current directories). iget() finds or 127 // creates a cache entry and increments its ref; iput() 128 // decrements ref. 129 // 130 // * Valid: the information (type, size, &c) in an inode 131 // cache entry is only correct when the I_VALID bit 132 // is set in ip->flags. ilock() reads the inode from 133 // the disk and sets I_VALID, while iput() clears 134 // I_VALID if ip->ref has fallen to zero. 135 // 136 // * Locked: file system code may only examine and modify 137 // the information in an inode and its content if it 138 // has first locked the inode. 139 // 140 // Thus a typical sequence is: 141 // ip = iget(dev, inum) 142 // ilock(ip) 143 // ... examine and modify ip->xxx ... 144 // iunlock(ip) 145 // iput(ip) 146 // 147 // ilock() is separate from iget() so that system calls can 148 // get a long-term reference to an inode (as for an open file) 149 // and only lock it for short periods (e.g., in read()). 150 // The separation also helps avoid deadlock and races during 151 // pathname lookup. iget() increments ip->ref so that the inode 152 // stays cached and pointers to it remain valid. 153 // 154 // Many internal file system functions expect the caller to 155 // have locked the inodes involved; this lets callers create 156 // multi-step atomic operations. 157 158 struct { 159 struct spinlock lock; 160 struct inode inode[NINODE]; 161 } icache; 162 163 void 164 iinit(int dev) 165 { 166 int i = 0; 167 168 initlock(&icache.lock, "icache"); 169 for(i = 0; i < NINODE; i++) { 170 initsleeplock(&icache.inode[i].lock, "inode"); 171 } 172 173 readsb(dev, &sb); 174 cprintf("sb: size %d nblocks %d ninodes %d nlog %d logstart %d\ 175 inodestart %d bmap start %d\n", sb.size, sb.nblocks, 176 sb.ninodes, sb.nlog, sb.logstart, sb.inodestart, 177 sb.bmapstart); 178 } 179 180 static struct inode* iget(uint dev, uint inum); 181 182 //PAGEBREAK! 183 // Allocate a new inode with the given type on device dev. 184 // A free inode has a type of zero. 185 struct inode* 186 ialloc(uint dev, short type) 187 { 188 int inum; 189 struct buf *bp; 190 struct dinode *dip; 191 192 for(inum = 1; inum < sb.ninodes; inum++){ 193 bp = bread(dev, IBLOCK(inum, sb)); 194 dip = (struct dinode*)bp->data + inum%IPB; 195 if(dip->type == 0){ // a free inode 196 memset(dip, 0, sizeof(*dip)); 197 dip->type = type; 198 log_write(bp); // mark it allocated on the disk 199 brelse(bp); 200 return iget(dev, inum); 201 } 202 brelse(bp); 203 } 204 panic("ialloc: no inodes"); 205 } 206 207 // Copy a modified in-memory inode to disk. 208 void 209 iupdate(struct inode *ip) 210 { 211 struct buf *bp; 212 struct dinode *dip; 213 214 bp = bread(ip->dev, IBLOCK(ip->inum, sb)); 215 dip = (struct dinode*)bp->data + ip->inum%IPB; 216 dip->type = ip->type; 217 dip->major = ip->major; 218 dip->minor = ip->minor; 219 dip->nlink = ip->nlink; 220 dip->size = ip->size; 221 memmove(dip->addrs, ip->addrs, sizeof(ip->addrs)); 222 log_write(bp); 223 brelse(bp); 224 } 225 226 // Find the inode with number inum on device dev 227 // and return the in-memory copy. Does not lock 228 // the inode and does not read it from disk. 229 static struct inode* 230 iget(uint dev, uint inum) 231 { 232 struct inode *ip, *empty; 233 234 acquire(&icache.lock); 235 236 // Is the inode already cached? 237 empty = 0; 238 for(ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++){ 239 if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){ 240 ip->ref++; 241 release(&icache.lock); 242 return ip; 243 } 244 if(empty == 0 && ip->ref == 0) // Remember empty slot. 245 empty = ip; 246 } 247 248 // Recycle an inode cache entry. 249 if(empty == 0) 250 panic("iget: no inodes"); 251 252 ip = empty; 253 ip->dev = dev; 254 ip->inum = inum; 255 ip->ref = 1; 256 ip->flags = 0; 257 release(&icache.lock); 258 259 return ip; 260 } 261 262 // Increment reference count for ip. 263 // Returns ip to enable ip = idup(ip1) idiom. 264 struct inode* 265 idup(struct inode *ip) 266 { 267 acquire(&icache.lock); 268 ip->ref++; 269 release(&icache.lock); 270 return ip; 271 } 272 273 // Lock the given inode. 274 // Reads the inode from disk if necessary. 275 void 276 ilock(struct inode *ip) 277 { 278 struct buf *bp; 279 struct dinode *dip; 280 281 if(ip == 0 || ip->ref < 1) 282 panic("ilock"); 283 284 acquiresleep(&ip->lock); 285 286 if((ip->flags & I_VALID) == 0){ 287 bp = bread(ip->dev, IBLOCK(ip->inum, sb)); 288 dip = (struct dinode*)bp->data + ip->inum%IPB; 289 ip->type = dip->type; 290 ip->major = dip->major; 291 ip->minor = dip->minor; 292 ip->nlink = dip->nlink; 293 ip->size = dip->size; 294 memmove(ip->addrs, dip->addrs, sizeof(ip->addrs)); 295 brelse(bp); 296 ip->flags |= I_VALID; 297 if(ip->type == 0) 298 panic("ilock: no type"); 299 } 300 } 301 302 // Unlock the given inode. 303 void 304 iunlock(struct inode *ip) 305 { 306 if(ip == 0 || !holdingsleep(&ip->lock) || ip->ref < 1) 307 panic("iunlock"); 308 309 releasesleep(&ip->lock); 310 } 311 312 // Drop a reference to an in-memory inode. 313 // If that was the last reference, the inode cache entry can 314 // be recycled. 315 // If that was the last reference and the inode has no links 316 // to it, free the inode (and its content) on disk. 317 // All calls to iput() must be inside a transaction in 318 // case it has to free the inode. 319 void 320 iput(struct inode *ip) 321 { 322 acquire(&icache.lock); 323 if(ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0){ 324 // inode has no links and no other references: truncate and free. 325 acquiresleep(&ip->lock); 326 release(&icache.lock); 327 itrunc(ip); 328 ip->type = 0; 329 iupdate(ip); 330 acquire(&icache.lock); 331 ip->flags = 0; 332 releasesleep(&ip->lock); 333 } 334 ip->ref--; 335 release(&icache.lock); 336 } 337 338 // Common idiom: unlock, then put. 339 void 340 iunlockput(struct inode *ip) 341 { 342 iunlock(ip); 343 iput(ip); 344 } 345 346 //PAGEBREAK! 347 // Inode content 348 // 349 // The content (data) associated with each inode is stored 350 // in blocks on the disk. The first NDIRECT block numbers 351 // are listed in ip->addrs[]. The next NINDIRECT blocks are 352 // listed in block ip->addrs[NDIRECT]. 353 354 // Return the disk block address of the nth block in inode ip. 355 // If there is no such block, bmap allocates one. 356 static uint 357 bmap(struct inode *ip, uint bn) 358 { 359 uint addr, *a; 360 struct buf *bp; 361 362 if(bn < NDIRECT){ 363 if((addr = ip->addrs[bn]) == 0) 364 ip->addrs[bn] = addr = balloc(ip->dev); 365 return addr; 366 } 367 bn -= NDIRECT; 368 369 if(bn < NINDIRECT){ 370 // Load indirect block, allocating if necessary. 371 if((addr = ip->addrs[NDIRECT]) == 0) 372 ip->addrs[NDIRECT] = addr = balloc(ip->dev); 373 bp = bread(ip->dev, addr); 374 a = (uint*)bp->data; 375 if((addr = a[bn]) == 0){ 376 a[bn] = addr = balloc(ip->dev); 377 log_write(bp); 378 } 379 brelse(bp); 380 return addr; 381 } 382 383 panic("bmap: out of range"); 384 } 385 386 // Truncate inode (discard contents). 387 // Only called when the inode has no links 388 // to it (no directory entries referring to it) 389 // and has no in-memory reference to it (is 390 // not an open file or current directory). 391 static void 392 itrunc(struct inode *ip) 393 { 394 int i, j; 395 struct buf *bp; 396 uint *a; 397 398 for(i = 0; i < NDIRECT; i++){ 399 if(ip->addrs[i]){ 400 bfree(ip->dev, ip->addrs[i]); 401 ip->addrs[i] = 0; 402 } 403 } 404 405 if(ip->addrs[NDIRECT]){ 406 bp = bread(ip->dev, ip->addrs[NDIRECT]); 407 a = (uint*)bp->data; 408 for(j = 0; j < NINDIRECT; j++){ 409 if(a[j]) 410 bfree(ip->dev, a[j]); 411 } 412 brelse(bp); 413 bfree(ip->dev, ip->addrs[NDIRECT]); 414 ip->addrs[NDIRECT] = 0; 415 } 416 417 ip->size = 0; 418 iupdate(ip); 419 } 420 421 // Copy stat information from inode. 422 void 423 stati(struct inode *ip, struct stat *st) 424 { 425 st->dev = ip->dev; 426 st->ino = ip->inum; 427 st->type = ip->type; 428 st->nlink = ip->nlink; 429 st->size = ip->size; 430 } 431 432 //PAGEBREAK! 433 // Read data from inode. 434 int 435 readi(struct inode *ip, char *dst, uint off, uint n) 436 { 437 uint tot, m; 438 struct buf *bp; 439 440 if(ip->type == T_DEV){ 441 if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read) 442 return -1; 443 return devsw[ip->major].read(ip, dst, n); 444 } 445 446 if(off > ip->size || off + n < off) 447 return -1; 448 if(off + n > ip->size) 449 n = ip->size - off; 450 451 for(tot=0; tot<n; tot+=m, off+=m, dst+=m){ 452 bp = bread(ip->dev, bmap(ip, off/BSIZE)); 453 m = min(n - tot, BSIZE - off%BSIZE); 454 /* 455 cprintf("data off %d:\n", off); 456 for (int j = 0; j < min(m, 10); j++) { 457 cprintf("%x ", bp->data[off%BSIZE+j]); 458 } 459 cprintf("\n"); 460 */ 461 memmove(dst, bp->data + off%BSIZE, m); 462 brelse(bp); 463 } 464 return n; 465 } 466 467 // PAGEBREAK! 468 // Write data to inode. 469 int 470 writei(struct inode *ip, char *src, uint off, uint n) 471 { 472 uint tot, m; 473 struct buf *bp; 474 475 if(ip->type == T_DEV){ 476 if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write) 477 return -1; 478 return devsw[ip->major].write(ip, src, n); 479 } 480 481 if(off > ip->size || off + n < off) 482 return -1; 483 if(off + n > MAXFILE*BSIZE) 484 return -1; 485 486 for(tot=0; tot<n; tot+=m, off+=m, src+=m){ 487 bp = bread(ip->dev, bmap(ip, off/BSIZE)); 488 m = min(n - tot, BSIZE - off%BSIZE); 489 memmove(bp->data + off%BSIZE, src, m); 490 log_write(bp); 491 brelse(bp); 492 } 493 494 if(n > 0 && off > ip->size){ 495 ip->size = off; 496 iupdate(ip); 497 } 498 return n; 499 } 500 501 //PAGEBREAK! 502 // Directories 503 504 int 505 namecmp(const char *s, const char *t) 506 { 507 return strncmp(s, t, DIRSIZ); 508 } 509 510 // Look for a directory entry in a directory. 511 // If found, set *poff to byte offset of entry. 512 struct inode* 513 dirlookup(struct inode *dp, char *name, uint *poff) 514 { 515 uint off, inum; 516 struct dirent de; 517 518 if(dp->type != T_DIR) 519 panic("dirlookup not DIR"); 520 521 for(off = 0; off < dp->size; off += sizeof(de)){ 522 if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 523 panic("dirlookup read"); 524 if(de.inum == 0) 525 continue; 526 if(namecmp(name, de.name) == 0){ 527 // entry matches path element 528 if(poff) 529 *poff = off; 530 inum = de.inum; 531 return iget(dp->dev, inum); 532 } 533 } 534 535 return 0; 536 } 537 538 // Write a new directory entry (name, inum) into the directory dp. 539 int 540 dirlink(struct inode *dp, char *name, uint inum) 541 { 542 int off; 543 struct dirent de; 544 struct inode *ip; 545 546 // Check that name is not present. 547 if((ip = dirlookup(dp, name, 0)) != 0){ 548 iput(ip); 549 return -1; 550 } 551 552 // Look for an empty dirent. 553 for(off = 0; off < dp->size; off += sizeof(de)){ 554 if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 555 panic("dirlink read"); 556 if(de.inum == 0) 557 break; 558 } 559 560 strncpy(de.name, name, DIRSIZ); 561 de.inum = inum; 562 if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 563 panic("dirlink"); 564 565 return 0; 566 } 567 568 //PAGEBREAK! 569 // Paths 570 571 // Copy the next path element from path into name. 572 // Return a pointer to the element following the copied one. 573 // The returned path has no leading slashes, 574 // so the caller can check *path=='\0' to see if the name is the last one. 575 // If no name to remove, return 0. 576 // 577 // Examples: 578 // skipelem("a/bb/c", name) = "bb/c", setting name = "a" 579 // skipelem("///a//bb", name) = "bb", setting name = "a" 580 // skipelem("a", name) = "", setting name = "a" 581 // skipelem("", name) = skipelem("////", name) = 0 582 // 583 static char* 584 skipelem(char *path, char *name) 585 { 586 char *s; 587 int len; 588 589 while(*path == '/') 590 path++; 591 if(*path == 0) 592 return 0; 593 s = path; 594 while(*path != '/' && *path != 0) 595 path++; 596 len = path - s; 597 if(len >= DIRSIZ) 598 memmove(name, s, DIRSIZ); 599 else { 600 memmove(name, s, len); 601 name[len] = 0; 602 } 603 while(*path == '/') 604 path++; 605 return path; 606 } 607 608 // Look up and return the inode for a path name. 609 // If parent != 0, return the inode for the parent and copy the final 610 // path element into name, which must have room for DIRSIZ bytes. 611 // Must be called inside a transaction since it calls iput(). 612 static struct inode* 613 namex(char *path, int nameiparent, char *name) 614 { 615 struct inode *ip, *next; 616 617 if(*path == '/') 618 ip = iget(ROOTDEV, ROOTINO); 619 else 620 ip = idup(proc->cwd); 621 622 while((path = skipelem(path, name)) != 0){ 623 ilock(ip); 624 if(ip->type != T_DIR){ 625 iunlockput(ip); 626 return 0; 627 } 628 if(nameiparent && *path == '\0'){ 629 // Stop one level early. 630 iunlock(ip); 631 return ip; 632 } 633 if((next = dirlookup(ip, name, 0)) == 0){ 634 iunlockput(ip); 635 return 0; 636 } 637 iunlockput(ip); 638 ip = next; 639 } 640 if(nameiparent){ 641 iput(ip); 642 return 0; 643 } 644 return ip; 645 } 646 647 struct inode* 648 namei(char *path) 649 { 650 char name[DIRSIZ]; 651 return namex(path, 0, name); 652 } 653 654 struct inode* 655 nameiparent(char *path, char *name) 656 { 657 return namex(path, 1, name); 658 } 659