1a5fbfe41SRobert Morris // File system implementation. Five layers: 2bcca6c6bSrsc // + Blocks: allocator for raw disk blocks. 3a5fbfe41SRobert Morris // + Log: crash recovery for multi-step updates. 4bcca6c6bSrsc // + Files: inode allocator, reading, writing, metadata. 5bcca6c6bSrsc // + Directories: inode with special contents (list of other inodes!) 6bcca6c6bSrsc // + Names: paths like /usr/rtm/xv6/fs.c for convenient naming. 7bcca6c6bSrsc // 8eaea18cbSrsc // This file contains the low-level file system manipulation 9eaea18cbSrsc // routines. The (higher-level) system call implementations 10eaea18cbSrsc // are in sysfile.c. 11bcca6c6bSrsc 1211a9947fSrtm #include "types.h" 13558ab49fSrsc #include "defs.h" 1411a9947fSrtm #include "param.h" 15558ab49fSrsc #include "stat.h" 1611a9947fSrtm #include "mmu.h" 1711a9947fSrtm #include "proc.h" 1811a9947fSrtm #include "spinlock.h" 196670d3b5SFrans Kaashoek #include "sleeplock.h" 2011a9947fSrtm #include "fs.h" 21c24ac5d7SFrans Kaashoek #include "buf.h" 220aef8914SRuss Cox #include "file.h" 2311a9947fSrtm 24bcca6c6bSrsc #define min(a, b) ((a) < (b) ? (a) : (b)) 25fbf91039Srsc static void itrunc(struct inode*); 26b7fed77bSFrans Kaashoek // there should be one superblock per disk device, but we run with 27b7fed77bSFrans Kaashoek // only one device 28b7fed77bSFrans Kaashoek struct superblock sb; 2911a9947fSrtm 30a505fd66Srsc // Read the super block. 3113a96baeSFrans Kaashoek void 32a505fd66Srsc readsb(int dev, struct superblock *sb) 33a505fd66Srsc { 34a505fd66Srsc struct buf *bp; 35a505fd66Srsc 36a505fd66Srsc bp = bread(dev, 1); 37a505fd66Srsc memmove(sb, bp->data, sizeof(*sb)); 38a505fd66Srsc brelse(bp); 39a505fd66Srsc } 40a505fd66Srsc 41a505fd66Srsc // Zero a block. 42a505fd66Srsc static void 43a505fd66Srsc bzero(int dev, int bno) 44a505fd66Srsc { 45a505fd66Srsc struct buf *bp; 46a505fd66Srsc 47a505fd66Srsc bp = bread(dev, bno); 48a505fd66Srsc memset(bp->data, 0, BSIZE); 495053dd6aSRobert Morris log_write(bp); 50a505fd66Srsc brelse(bp); 51a505fd66Srsc } 52a505fd66Srsc 53bcca6c6bSrsc // Blocks. 545be0039cSrtm 555053dd6aSRobert Morris // Allocate a zeroed disk block. 5624111398Skaashoek static uint 5724111398Skaashoek balloc(uint dev) 5824111398Skaashoek { 59a505fd66Srsc int b, bi, m; 6024111398Skaashoek struct buf *bp; 6124111398Skaashoek 62a505fd66Srsc bp = 0; 63a505fd66Srsc for(b = 0; b < sb.size; b += BPB){ 648320d61bSFrans Kaashoek bp = bread(dev, BBLOCK(b, sb)); 65a5fbfe41SRobert Morris for(bi = 0; bi < BPB && b + bi < sb.size; bi++){ 66a505fd66Srsc m = 1 << (bi % 8); 67a505fd66Srsc if((bp->data[bi/8] & m) == 0){ // Is block free? 68a5fbfe41SRobert Morris bp->data[bi/8] |= m; // Mark block in use. 6913a96baeSFrans Kaashoek log_write(bp); 7024111398Skaashoek brelse(bp); 715053dd6aSRobert Morris bzero(dev, b + bi); 72a505fd66Srsc return b + bi; 7324111398Skaashoek } 74a505fd66Srsc } 7528d9ef04Skaashoek brelse(bp); 767d4aef6cSrsc } 777d4aef6cSrsc panic("balloc: out of blocks"); 787d4aef6cSrsc } 7924111398Skaashoek 80bb207a1dSrsc // Free a disk block. 8128d9ef04Skaashoek static void 8228d9ef04Skaashoek bfree(int dev, uint b) 8328d9ef04Skaashoek { 8428d9ef04Skaashoek struct buf *bp; 85a505fd66Srsc int bi, m; 8628d9ef04Skaashoek 87a505fd66Srsc readsb(dev, &sb); 888320d61bSFrans Kaashoek bp = bread(dev, BBLOCK(b, sb)); 8928d9ef04Skaashoek bi = b % BPB; 90a505fd66Srsc m = 1 << (bi % 8); 91902b13f5Srtm if((bp->data[bi/8] & m) == 0) 92902b13f5Srtm panic("freeing free block"); 93a5fbfe41SRobert Morris bp->data[bi/8] &= ~m; 9413a96baeSFrans Kaashoek log_write(bp); 9528d9ef04Skaashoek brelse(bp); 9628d9ef04Skaashoek } 9724111398Skaashoek 986c34f97cSrsc // Inodes. 996c34f97cSrsc // 100a5fbfe41SRobert Morris // An inode describes a single unnamed file. 101a5fbfe41SRobert Morris // The inode disk structure holds metadata: the file's type, 102a5fbfe41SRobert Morris // its size, the number of links referring to it, and the 103a5fbfe41SRobert Morris // list of blocks holding the file's content. 104bcca6c6bSrsc // 1058320d61bSFrans Kaashoek // The inodes are laid out sequentially on disk at 1068320d61bSFrans Kaashoek // sb.startinode. Each inode has a number, indicating its 107a5fbfe41SRobert Morris // position on the disk. 108a5fbfe41SRobert Morris // 109a5fbfe41SRobert Morris // The kernel keeps a cache of in-use inodes in memory 110a5fbfe41SRobert Morris // to provide a place for synchronizing access 111a5fbfe41SRobert Morris // to inodes used by multiple processes. The cached 112a5fbfe41SRobert Morris // inodes include book-keeping information that is 1133375df50SRobert Morris // not stored on disk: ip->ref and ip->valid. 114bcca6c6bSrsc // 1159cec455bSRobert Morris // An inode and its in-memory representation go through a 11638eee5bcSRobert Morris // sequence of states before they can be used by the 11738eee5bcSRobert Morris // rest of the file system code. 118bcca6c6bSrsc // 11938eee5bcSRobert Morris // * Allocation: an inode is allocated if its type (on disk) 1209cec455bSRobert Morris // is non-zero. ialloc() allocates, and iput() frees if 1219cec455bSRobert Morris // the reference and link counts have fallen to zero. 122eaea18cbSrsc // 12338eee5bcSRobert Morris // * Referencing in cache: an entry in the inode cache 12438eee5bcSRobert Morris // is free if ip->ref is zero. Otherwise ip->ref tracks 12538eee5bcSRobert Morris // the number of in-memory pointers to the entry (open 1269cec455bSRobert Morris // files and current directories). iget() finds or 1279cec455bSRobert Morris // creates a cache entry and increments its ref; iput() 1289cec455bSRobert Morris // decrements ref. 129a5fbfe41SRobert Morris // 13038eee5bcSRobert Morris // * Valid: the information (type, size, &c) in an inode 1313375df50SRobert Morris // cache entry is only correct when ip->valid is 1. 1323375df50SRobert Morris // ilock() reads the inode from 1333375df50SRobert Morris // the disk and sets ip->valid, while iput() clears 1343375df50SRobert Morris // ip->valid if ip->ref has fallen to zero. 135a5fbfe41SRobert Morris // 13638eee5bcSRobert Morris // * Locked: file system code may only examine and modify 13738eee5bcSRobert Morris // the information in an inode and its content if it 138dec637bcSFrans Kaashoek // has first locked the inode. 13938eee5bcSRobert Morris // 14038eee5bcSRobert Morris // Thus a typical sequence is: 14138eee5bcSRobert Morris // ip = iget(dev, inum) 14238eee5bcSRobert Morris // ilock(ip) 14338eee5bcSRobert Morris // ... examine and modify ip->xxx ... 14438eee5bcSRobert Morris // iunlock(ip) 14538eee5bcSRobert Morris // iput(ip) 14638eee5bcSRobert Morris // 14738eee5bcSRobert Morris // ilock() is separate from iget() so that system calls can 14838eee5bcSRobert Morris // get a long-term reference to an inode (as for an open file) 14938eee5bcSRobert Morris // and only lock it for short periods (e.g., in read()). 15038eee5bcSRobert Morris // The separation also helps avoid deadlock and races during 15138eee5bcSRobert Morris // pathname lookup. iget() increments ip->ref so that the inode 15238eee5bcSRobert Morris // stays cached and pointers to it remain valid. 15338eee5bcSRobert Morris // 15438eee5bcSRobert Morris // Many internal file system functions expect the caller to 15538eee5bcSRobert Morris // have locked the inodes involved; this lets callers create 15638eee5bcSRobert Morris // multi-step atomic operations. 157*1c7aa960SRobert Morris // 158*1c7aa960SRobert Morris // The icache.lock spin-lock defends ip->ref, ip->dev, and ip->inum. 159*1c7aa960SRobert Morris // Since ip->ref indicates whether an icache entry is free, the 160*1c7aa960SRobert Morris // icache.lock defends icache allocation. icache.lock also defends 161*1c7aa960SRobert Morris // all fields of an unallocated icache entry, during allocation. 162*1c7aa960SRobert Morris // 163*1c7aa960SRobert Morris // An ip->lock sleep-lock defends all ip-> fields other than ref, 164*1c7aa960SRobert Morris // dev, and inum. One must hold ip->lock in order to 165*1c7aa960SRobert Morris // read or write that inode's ip->valid, ip->size, ip->type, &c. 166bcca6c6bSrsc 167bcca6c6bSrsc struct { 168bcca6c6bSrsc struct spinlock lock; 169bcca6c6bSrsc struct inode inode[NINODE]; 170bcca6c6bSrsc } icache; 171bcca6c6bSrsc 172bcca6c6bSrsc void 1738320d61bSFrans Kaashoek iinit(int dev) 174bcca6c6bSrsc { 175dec637bcSFrans Kaashoek int i = 0; 176dec637bcSFrans Kaashoek 17734295f46Srsc initlock(&icache.lock, "icache"); 178dec637bcSFrans Kaashoek for(i = 0; i < NINODE; i++) { 179dec637bcSFrans Kaashoek initsleeplock(&icache.inode[i].lock, "inode"); 180dec637bcSFrans Kaashoek } 181dec637bcSFrans Kaashoek 1828320d61bSFrans Kaashoek readsb(dev, &sb); 183b7fed77bSFrans Kaashoek cprintf("sb: size %d nblocks %d ninodes %d nlog %d logstart %d\ 184b7fed77bSFrans Kaashoek inodestart %d bmap start %d\n", sb.size, sb.nblocks, 185b7fed77bSFrans Kaashoek sb.ninodes, sb.nlog, sb.logstart, sb.inodestart, 186b7fed77bSFrans Kaashoek sb.bmapstart); 187bcca6c6bSrsc } 188bcca6c6bSrsc 189f9a06440SRuss Cox static struct inode* iget(uint dev, uint inum); 190f9a06440SRuss Cox 191f9a06440SRuss Cox //PAGEBREAK! 192f9a06440SRuss Cox // Allocate a new inode with the given type on device dev. 193a5fbfe41SRobert Morris // A free inode has a type of zero. 194f9a06440SRuss Cox struct inode* 195f9a06440SRuss Cox ialloc(uint dev, short type) 196f9a06440SRuss Cox { 197f9a06440SRuss Cox int inum; 198f9a06440SRuss Cox struct buf *bp; 199f9a06440SRuss Cox struct dinode *dip; 200a5fbfe41SRobert Morris 201a5fbfe41SRobert Morris for(inum = 1; inum < sb.ninodes; inum++){ 2028320d61bSFrans Kaashoek bp = bread(dev, IBLOCK(inum, sb)); 203f9a06440SRuss Cox dip = (struct dinode*)bp->data + inum%IPB; 204f9a06440SRuss Cox if(dip->type == 0){ // a free inode 205f9a06440SRuss Cox memset(dip, 0, sizeof(*dip)); 206f9a06440SRuss Cox dip->type = type; 20713a96baeSFrans Kaashoek log_write(bp); // mark it allocated on the disk 208f9a06440SRuss Cox brelse(bp); 209f9a06440SRuss Cox return iget(dev, inum); 210f9a06440SRuss Cox } 211f9a06440SRuss Cox brelse(bp); 212f9a06440SRuss Cox } 213f9a06440SRuss Cox panic("ialloc: no inodes"); 214f9a06440SRuss Cox } 215f9a06440SRuss Cox 21638eee5bcSRobert Morris // Copy a modified in-memory inode to disk. 217*1c7aa960SRobert Morris // Caller must hold ip->lock. 218f9a06440SRuss Cox void 219f9a06440SRuss Cox iupdate(struct inode *ip) 220f9a06440SRuss Cox { 221f9a06440SRuss Cox struct buf *bp; 222f9a06440SRuss Cox struct dinode *dip; 223f9a06440SRuss Cox 2248320d61bSFrans Kaashoek bp = bread(ip->dev, IBLOCK(ip->inum, sb)); 225f9a06440SRuss Cox dip = (struct dinode*)bp->data + ip->inum%IPB; 226f9a06440SRuss Cox dip->type = ip->type; 227f9a06440SRuss Cox dip->major = ip->major; 228f9a06440SRuss Cox dip->minor = ip->minor; 229f9a06440SRuss Cox dip->nlink = ip->nlink; 230f9a06440SRuss Cox dip->size = ip->size; 231f9a06440SRuss Cox memmove(dip->addrs, ip->addrs, sizeof(ip->addrs)); 23213a96baeSFrans Kaashoek log_write(bp); 233f9a06440SRuss Cox brelse(bp); 234f9a06440SRuss Cox } 235f9a06440SRuss Cox 236f5527388Srsc // Find the inode with number inum on device dev 23738eee5bcSRobert Morris // and return the in-memory copy. Does not lock 23838eee5bcSRobert Morris // the inode and does not read it from disk. 23907090dd7Srsc static struct inode* 24011a9947fSrtm iget(uint dev, uint inum) 24111a9947fSrtm { 242bcca6c6bSrsc struct inode *ip, *empty; 24311a9947fSrtm 244bcca6c6bSrsc acquire(&icache.lock); 24511a9947fSrtm 24638eee5bcSRobert Morris // Is the inode already cached? 247bcca6c6bSrsc empty = 0; 248bcca6c6bSrsc for(ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++){ 2490d6bbd31Srsc if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){ 2500d6bbd31Srsc ip->ref++; 251bcca6c6bSrsc release(&icache.lock); 25207090dd7Srsc return ip; 25311a9947fSrtm } 254bcca6c6bSrsc if(empty == 0 && ip->ref == 0) // Remember empty slot. 255bcca6c6bSrsc empty = ip; 25611a9947fSrtm } 25711a9947fSrtm 25838eee5bcSRobert Morris // Recycle an inode cache entry. 259bcca6c6bSrsc if(empty == 0) 26032eea766Srsc panic("iget: no inodes"); 26111a9947fSrtm 262bcca6c6bSrsc ip = empty; 263bcca6c6bSrsc ip->dev = dev; 264bcca6c6bSrsc ip->inum = inum; 265bcca6c6bSrsc ip->ref = 1; 2663375df50SRobert Morris ip->valid = 0; 267bcca6c6bSrsc release(&icache.lock); 26811a9947fSrtm 26907090dd7Srsc return ip; 270f32f3638Srsc } 271f32f3638Srsc 272eaea18cbSrsc // Increment reference count for ip. 273eaea18cbSrsc // Returns ip to enable ip = idup(ip1) idiom. 27407090dd7Srsc struct inode* 27507090dd7Srsc idup(struct inode *ip) 276f32f3638Srsc { 277eaea18cbSrsc acquire(&icache.lock); 278eaea18cbSrsc ip->ref++; 279eaea18cbSrsc release(&icache.lock); 28007090dd7Srsc return ip; 281f32f3638Srsc } 282f32f3638Srsc 2831ddfbbb1SFrans Kaashoek // Lock the given inode. 28438eee5bcSRobert Morris // Reads the inode from disk if necessary. 28507090dd7Srsc void 28607090dd7Srsc ilock(struct inode *ip) 287f32f3638Srsc { 288f32f3638Srsc struct buf *bp; 289f32f3638Srsc struct dinode *dip; 290eaea18cbSrsc 29107090dd7Srsc if(ip == 0 || ip->ref < 1) 29207090dd7Srsc panic("ilock"); 293f32f3638Srsc 294dec637bcSFrans Kaashoek acquiresleep(&ip->lock); 295f32f3638Srsc 2963375df50SRobert Morris if(ip->valid == 0){ 2978320d61bSFrans Kaashoek bp = bread(ip->dev, IBLOCK(ip->inum, sb)); 2983341e30fSrsc dip = (struct dinode*)bp->data + ip->inum%IPB; 299bcca6c6bSrsc ip->type = dip->type; 300bcca6c6bSrsc ip->major = dip->major; 301bcca6c6bSrsc ip->minor = dip->minor; 302bcca6c6bSrsc ip->nlink = dip->nlink; 303bcca6c6bSrsc ip->size = dip->size; 304bcca6c6bSrsc memmove(ip->addrs, dip->addrs, sizeof(ip->addrs)); 30511a9947fSrtm brelse(bp); 3063375df50SRobert Morris ip->valid = 1; 307eaea18cbSrsc if(ip->type == 0) 308eaea18cbSrsc panic("ilock: no type"); 30911a9947fSrtm } 310bcca6c6bSrsc } 311bcca6c6bSrsc 312bcca6c6bSrsc // Unlock the given inode. 31307090dd7Srsc void 314bcca6c6bSrsc iunlock(struct inode *ip) 315bcca6c6bSrsc { 316dec637bcSFrans Kaashoek if(ip == 0 || !holdingsleep(&ip->lock) || ip->ref < 1) 317bcca6c6bSrsc panic("iunlock"); 318bcca6c6bSrsc 319dec637bcSFrans Kaashoek releasesleep(&ip->lock); 320bcca6c6bSrsc } 321bcca6c6bSrsc 32238eee5bcSRobert Morris // Drop a reference to an in-memory inode. 32338eee5bcSRobert Morris // If that was the last reference, the inode cache entry can 32438eee5bcSRobert Morris // be recycled. 32538eee5bcSRobert Morris // If that was the last reference and the inode has no links 32638eee5bcSRobert Morris // to it, free the inode (and its content) on disk. 3272c565472SRobert Morris // All calls to iput() must be inside a transaction in 3282c565472SRobert Morris // case it has to free the inode. 329bcca6c6bSrsc void 33007090dd7Srsc iput(struct inode *ip) 331bcca6c6bSrsc { 332f32f3638Srsc acquire(&icache.lock); 33370d912b3SRobert Morris if(ip->ref == 1){ 334aba8423cSRobert Morris acquiresleep(&ip->lock); 33570d912b3SRobert Morris if(ip->valid && ip->nlink == 0){ 33670d912b3SRobert Morris // inode has no links and no other references: truncate and free. 337f32f3638Srsc release(&icache.lock); 338f32f3638Srsc itrunc(ip); 339f32f3638Srsc ip->type = 0; 340f32f3638Srsc iupdate(ip); 3413375df50SRobert Morris ip->valid = 0; 34270d912b3SRobert Morris acquire(&icache.lock); 34370d912b3SRobert Morris } 344aba8423cSRobert Morris releasesleep(&ip->lock); 345f32f3638Srsc } 346f32f3638Srsc ip->ref--; 347f32f3638Srsc release(&icache.lock); 348bcca6c6bSrsc } 349bcca6c6bSrsc 3507895178dSrsc // Common idiom: unlock, then put. 35107090dd7Srsc void 35207090dd7Srsc iunlockput(struct inode *ip) 35307090dd7Srsc { 35407090dd7Srsc iunlock(ip); 35507090dd7Srsc iput(ip); 35607090dd7Srsc } 35707090dd7Srsc 3588d2e9a48Srsc //PAGEBREAK! 35938eee5bcSRobert Morris // Inode content 360bcca6c6bSrsc // 36138eee5bcSRobert Morris // The content (data) associated with each inode is stored 36238eee5bcSRobert Morris // in blocks on the disk. The first NDIRECT block numbers 3637895178dSrsc // are listed in ip->addrs[]. The next NINDIRECT blocks are 36438eee5bcSRobert Morris // listed in block ip->addrs[NDIRECT]. 3659d3fb671Srtm 366bb207a1dSrsc // Return the disk block address of the nth block in inode ip. 36713ae8808Srsc // If there is no such block, bmap allocates one. 368eaea18cbSrsc static uint 36913ae8808Srsc bmap(struct inode *ip, uint bn) 37022bac2cbSkaashoek { 371bcca6c6bSrsc uint addr, *a; 372bcca6c6bSrsc struct buf *bp; 37322bac2cbSkaashoek 374ea2909b6Skaashoek if(bn < NDIRECT){ 37513ae8808Srsc if((addr = ip->addrs[bn]) == 0) 376bcca6c6bSrsc ip->addrs[bn] = addr = balloc(ip->dev); 377bcca6c6bSrsc return addr; 378bcca6c6bSrsc } 379bcca6c6bSrsc bn -= NDIRECT; 380bcca6c6bSrsc 381bcca6c6bSrsc if(bn < NINDIRECT){ 382bcca6c6bSrsc // Load indirect block, allocating if necessary. 38313ae8808Srsc if((addr = ip->addrs[NDIRECT]) == 0) 384ba6cd8a6Srsc ip->addrs[NDIRECT] = addr = balloc(ip->dev); 385bcca6c6bSrsc bp = bread(ip->dev, addr); 386bcca6c6bSrsc a = (uint*)bp->data; 387bcca6c6bSrsc if((addr = a[bn]) == 0){ 388bcca6c6bSrsc a[bn] = addr = balloc(ip->dev); 38913a96baeSFrans Kaashoek log_write(bp); 390bcca6c6bSrsc } 391bcca6c6bSrsc brelse(bp); 392bcca6c6bSrsc return addr; 39322bac2cbSkaashoek } 39422bac2cbSkaashoek 395bcca6c6bSrsc panic("bmap: out of range"); 396bcca6c6bSrsc } 397bcca6c6bSrsc 398bcca6c6bSrsc // Truncate inode (discard contents). 39938eee5bcSRobert Morris // Only called when the inode has no links 40038eee5bcSRobert Morris // to it (no directory entries referring to it) 40138eee5bcSRobert Morris // and has no in-memory reference to it (is 40238eee5bcSRobert Morris // not an open file or current directory). 403fbf91039Srsc static void 4042aa4c3bcSrtm itrunc(struct inode *ip) 40522bac2cbSkaashoek { 406ea2909b6Skaashoek int i, j; 407bcca6c6bSrsc struct buf *bp; 4087d4aef6cSrsc uint *a; 40922bac2cbSkaashoek 410bcca6c6bSrsc for(i = 0; i < NDIRECT; i++){ 411bcca6c6bSrsc if(ip->addrs[i]){ 41222bac2cbSkaashoek bfree(ip->dev, ip->addrs[i]); 41322bac2cbSkaashoek ip->addrs[i] = 0; 41422bac2cbSkaashoek } 41522bac2cbSkaashoek } 416bcca6c6bSrsc 417ba6cd8a6Srsc if(ip->addrs[NDIRECT]){ 418ba6cd8a6Srsc bp = bread(ip->dev, ip->addrs[NDIRECT]); 419bcca6c6bSrsc a = (uint*)bp->data; 420bcca6c6bSrsc for(j = 0; j < NINDIRECT; j++){ 421bcca6c6bSrsc if(a[j]) 422bcca6c6bSrsc bfree(ip->dev, a[j]); 423bcca6c6bSrsc } 424bcca6c6bSrsc brelse(bp); 425ba6cd8a6Srsc bfree(ip->dev, ip->addrs[NDIRECT]); 426ba6cd8a6Srsc ip->addrs[NDIRECT] = 0; 427bcca6c6bSrsc } 428bcca6c6bSrsc 42922bac2cbSkaashoek ip->size = 0; 43022bac2cbSkaashoek iupdate(ip); 43122bac2cbSkaashoek } 43222bac2cbSkaashoek 433bb207a1dSrsc // Copy stat information from inode. 434*1c7aa960SRobert Morris // Caller must hold ip->lock. 435e958c538Skaashoek void 4361f544842Skaashoek stati(struct inode *ip, struct stat *st) 4371f544842Skaashoek { 4381dca3afbSrsc st->dev = ip->dev; 4391dca3afbSrsc st->ino = ip->inum; 4401dca3afbSrsc st->type = ip->type; 4411dca3afbSrsc st->nlink = ip->nlink; 4421dca3afbSrsc st->size = ip->size; 4431f544842Skaashoek } 4441f544842Skaashoek 445eaea18cbSrsc //PAGEBREAK! 446bb207a1dSrsc // Read data from inode. 447*1c7aa960SRobert Morris // Caller must hold ip->lock. 448c59361f1Srtm int 44917a85657Srtm readi(struct inode *ip, char *dst, uint off, uint n) 450c59361f1Srtm { 451bcca6c6bSrsc uint tot, m; 452c59361f1Srtm struct buf *bp; 453c59361f1Srtm 454939f9edeSkaashoek if(ip->type == T_DEV){ 4551dca3afbSrsc if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read) 456939f9edeSkaashoek return -1; 457d844f0f9Srsc return devsw[ip->major].read(ip, dst, n); 458939f9edeSkaashoek } 459939f9edeSkaashoek 4607895178dSrsc if(off > ip->size || off + n < off) 461bcca6c6bSrsc return -1; 462bcca6c6bSrsc if(off + n > ip->size) 463bcca6c6bSrsc n = ip->size - off; 464bcca6c6bSrsc 465bcca6c6bSrsc for(tot=0; tot<n; tot+=m, off+=m, dst+=m){ 46613ae8808Srsc bp = bread(ip->dev, bmap(ip, off/BSIZE)); 467bcca6c6bSrsc m = min(n - tot, BSIZE - off%BSIZE); 468bcca6c6bSrsc memmove(dst, bp->data + off%BSIZE, m); 469c59361f1Srtm brelse(bp); 470c59361f1Srtm } 471bcca6c6bSrsc return n; 472ea2909b6Skaashoek } 473ea2909b6Skaashoek 474eaea18cbSrsc // PAGEBREAK! 475bb207a1dSrsc // Write data to inode. 476*1c7aa960SRobert Morris // Caller must hold ip->lock. 477ea2909b6Skaashoek int 478bcca6c6bSrsc writei(struct inode *ip, char *src, uint off, uint n) 4796fa5ffb5Skaashoek { 480bcca6c6bSrsc uint tot, m; 4817d4aef6cSrsc struct buf *bp; 4827d4aef6cSrsc 4836fa5ffb5Skaashoek if(ip->type == T_DEV){ 4841dca3afbSrsc if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write) 485939f9edeSkaashoek return -1; 486d844f0f9Srsc return devsw[ip->major].write(ip, src, n); 4877d4aef6cSrsc } 4887d4aef6cSrsc 4891ddfbbb1SFrans Kaashoek if(off > ip->size || off + n < off) 490bcca6c6bSrsc return -1; 4911ddfbbb1SFrans Kaashoek if(off + n > MAXFILE*BSIZE) 4922e590463SRobert Morris return -1; 493bcca6c6bSrsc 494bcca6c6bSrsc for(tot=0; tot<n; tot+=m, off+=m, src+=m){ 49513ae8808Srsc bp = bread(ip->dev, bmap(ip, off/BSIZE)); 496bcca6c6bSrsc m = min(n - tot, BSIZE - off%BSIZE); 497bcca6c6bSrsc memmove(bp->data + off%BSIZE, src, m); 4982e590463SRobert Morris log_write(bp); 49928d9ef04Skaashoek brelse(bp); 50028d9ef04Skaashoek } 501bcca6c6bSrsc 502bcca6c6bSrsc if(n > 0 && off > ip->size){ 50348b82470Srsc ip->size = off; 50428d9ef04Skaashoek iupdate(ip); 50528d9ef04Skaashoek } 506bcca6c6bSrsc return n; 5076fa5ffb5Skaashoek } 5086fa5ffb5Skaashoek 509eaea18cbSrsc //PAGEBREAK! 510bcca6c6bSrsc // Directories 511bcca6c6bSrsc 512eaea18cbSrsc int 513fbf91039Srsc namecmp(const char *s, const char *t) 514fbf91039Srsc { 515766ba5ccSrsc return strncmp(s, t, DIRSIZ); 516fbf91039Srsc } 517fbf91039Srsc 518bcca6c6bSrsc // Look for a directory entry in a directory. 519eaea18cbSrsc // If found, set *poff to byte offset of entry. 52007090dd7Srsc struct inode* 521fbf91039Srsc dirlookup(struct inode *dp, char *name, uint *poff) 522bcca6c6bSrsc { 523f32f3638Srsc uint off, inum; 524327cc21fSRobert Morris struct dirent de; 525bcca6c6bSrsc 526bcca6c6bSrsc if(dp->type != T_DIR) 52720365348Srtm panic("dirlookup not DIR"); 528bcca6c6bSrsc 529327cc21fSRobert Morris for(off = 0; off < dp->size; off += sizeof(de)){ 530327cc21fSRobert Morris if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 53161f26e3cSGrant Wu panic("dirlookup read"); 532327cc21fSRobert Morris if(de.inum == 0) 533bcca6c6bSrsc continue; 534327cc21fSRobert Morris if(namecmp(name, de.name) == 0){ 535bcca6c6bSrsc // entry matches path element 536e2a620daSrsc if(poff) 537327cc21fSRobert Morris *poff = off; 538327cc21fSRobert Morris inum = de.inum; 539f32f3638Srsc return iget(dp->dev, inum); 540f32f3638Srsc } 541f32f3638Srsc } 542327cc21fSRobert Morris 543bcca6c6bSrsc return 0; 544bcca6c6bSrsc } 545bcca6c6bSrsc 54613ae8808Srsc // Write a new directory entry (name, inum) into the directory dp. 547eaea18cbSrsc int 54813ae8808Srsc dirlink(struct inode *dp, char *name, uint inum) 549bcca6c6bSrsc { 550e2a620daSrsc int off; 551bcca6c6bSrsc struct dirent de; 55207090dd7Srsc struct inode *ip; 553f32f3638Srsc 554eaea18cbSrsc // Check that name is not present. 55507090dd7Srsc if((ip = dirlookup(dp, name, 0)) != 0){ 55607090dd7Srsc iput(ip); 557f32f3638Srsc return -1; 558f32f3638Srsc } 559bcca6c6bSrsc 560bcca6c6bSrsc // Look for an empty dirent. 561bcca6c6bSrsc for(off = 0; off < dp->size; off += sizeof(de)){ 562bcca6c6bSrsc if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 5637895178dSrsc panic("dirlink read"); 564bcca6c6bSrsc if(de.inum == 0) 565bcca6c6bSrsc break; 566bcca6c6bSrsc } 567bcca6c6bSrsc 568766ba5ccSrsc strncpy(de.name, name, DIRSIZ); 56913ae8808Srsc de.inum = inum; 570bcca6c6bSrsc if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 5717895178dSrsc panic("dirlink"); 572f32f3638Srsc 573f32f3638Srsc return 0; 574bcca6c6bSrsc } 575bcca6c6bSrsc 5768d2e9a48Srsc //PAGEBREAK! 577bcca6c6bSrsc // Paths 578bcca6c6bSrsc 579eaea18cbSrsc // Copy the next path element from path into name. 580eaea18cbSrsc // Return a pointer to the element following the copied one. 581eaea18cbSrsc // The returned path has no leading slashes, 582eaea18cbSrsc // so the caller can check *path=='\0' to see if the name is the last one. 583eaea18cbSrsc // If no name to remove, return 0. 584ab5c2dbbSrsc // 585ab5c2dbbSrsc // Examples: 586eaea18cbSrsc // skipelem("a/bb/c", name) = "bb/c", setting name = "a" 5877895178dSrsc // skipelem("///a//bb", name) = "bb", setting name = "a" 58813ae8808Srsc // skipelem("a", name) = "", setting name = "a" 589eaea18cbSrsc // skipelem("", name) = skipelem("////", name) = 0 590ab5c2dbbSrsc // 591ab5c2dbbSrsc static char* 592fbf91039Srsc skipelem(char *path, char *name) 593ab5c2dbbSrsc { 594fbf91039Srsc char *s; 595fbf91039Srsc int len; 596fbf91039Srsc 597ab5c2dbbSrsc while(*path == '/') 598ab5c2dbbSrsc path++; 599ab5c2dbbSrsc if(*path == 0) 600ab5c2dbbSrsc return 0; 601fbf91039Srsc s = path; 602ab5c2dbbSrsc while(*path != '/' && *path != 0) 603ab5c2dbbSrsc path++; 604fbf91039Srsc len = path - s; 605fbf91039Srsc if(len >= DIRSIZ) 606fbf91039Srsc memmove(name, s, DIRSIZ); 607fbf91039Srsc else { 608fbf91039Srsc memmove(name, s, len); 609fbf91039Srsc name[len] = 0; 610fbf91039Srsc } 611ab5c2dbbSrsc while(*path == '/') 612ab5c2dbbSrsc path++; 613ab5c2dbbSrsc return path; 614ab5c2dbbSrsc } 615ab5c2dbbSrsc 616eaea18cbSrsc // Look up and return the inode for a path name. 617bf2932a6Srsc // If parent != 0, return the inode for the parent and copy the final 618bf2932a6Srsc // path element into name, which must have room for DIRSIZ bytes. 6192c565472SRobert Morris // Must be called inside a transaction since it calls iput(). 62007090dd7Srsc static struct inode* 621f9a06440SRuss Cox namex(char *path, int nameiparent, char *name) 6229d3fb671Srtm { 62307090dd7Srsc struct inode *ip, *next; 6249d3fb671Srtm 625ab5c2dbbSrsc if(*path == '/') 6262ce40d70Srtm ip = iget(ROOTDEV, ROOTINO); 627f32f3638Srsc else 628abf847a0SFrans Kaashoek ip = idup(myproc()->cwd); 6299d3fb671Srtm 630fbf91039Srsc while((path = skipelem(path, name)) != 0){ 63107090dd7Srsc ilock(ip); 63207090dd7Srsc if(ip->type != T_DIR){ 63307090dd7Srsc iunlockput(ip); 634eaea18cbSrsc return 0; 635eaea18cbSrsc } 636f9a06440SRuss Cox if(nameiparent && *path == '\0'){ 637e2a620daSrsc // Stop one level early. 63807090dd7Srsc iunlock(ip); 63907090dd7Srsc return ip; 640ab5c2dbbSrsc } 6417895178dSrsc if((next = dirlookup(ip, name, 0)) == 0){ 64207090dd7Srsc iunlockput(ip); 643eaea18cbSrsc return 0; 644eaea18cbSrsc } 64507090dd7Srsc iunlockput(ip); 64607090dd7Srsc ip = next; 647ab5c2dbbSrsc } 648f9a06440SRuss Cox if(nameiparent){ 64907090dd7Srsc iput(ip); 6505051da6dSrtm return 0; 65120365348Srtm } 65207090dd7Srsc return ip; 6530633b971Skaashoek } 6549d3fb671Srtm 6557895178dSrsc struct inode* 6567895178dSrsc namei(char *path) 6577895178dSrsc { 6587895178dSrsc char name[DIRSIZ]; 65913ae8808Srsc return namex(path, 0, name); 6607895178dSrsc } 661bf2932a6Srsc 6627895178dSrsc struct inode* 6637895178dSrsc nameiparent(char *path, char *name) 6647895178dSrsc { 66513ae8808Srsc return namex(path, 1, name); 6667895178dSrsc } 667