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 113a5fbfe41SRobert Morris // not stored on disk: ip->ref and ip->flags. 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 13138eee5bcSRobert Morris // cache entry is only correct when the I_VALID bit 13238eee5bcSRobert Morris // is set in ip->flags. ilock() reads the inode from 13338eee5bcSRobert Morris // the disk and sets I_VALID, while iput() clears 13438eee5bcSRobert Morris // I_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. 157bcca6c6bSrsc 158bcca6c6bSrsc struct { 159bcca6c6bSrsc struct spinlock lock; 160bcca6c6bSrsc struct inode inode[NINODE]; 161bcca6c6bSrsc } icache; 162bcca6c6bSrsc 163bcca6c6bSrsc void 1648320d61bSFrans Kaashoek iinit(int dev) 165bcca6c6bSrsc { 166dec637bcSFrans Kaashoek int i = 0; 167dec637bcSFrans Kaashoek 16834295f46Srsc initlock(&icache.lock, "icache"); 169dec637bcSFrans Kaashoek for(i = 0; i < NINODE; i++) { 170dec637bcSFrans Kaashoek initsleeplock(&icache.inode[i].lock, "inode"); 171dec637bcSFrans Kaashoek } 172dec637bcSFrans Kaashoek 1738320d61bSFrans Kaashoek readsb(dev, &sb); 174b7fed77bSFrans Kaashoek cprintf("sb: size %d nblocks %d ninodes %d nlog %d logstart %d\ 175b7fed77bSFrans Kaashoek inodestart %d bmap start %d\n", sb.size, sb.nblocks, 176b7fed77bSFrans Kaashoek sb.ninodes, sb.nlog, sb.logstart, sb.inodestart, 177b7fed77bSFrans Kaashoek sb.bmapstart); 178bcca6c6bSrsc } 179bcca6c6bSrsc 180f9a06440SRuss Cox static struct inode* iget(uint dev, uint inum); 181f9a06440SRuss Cox 182f9a06440SRuss Cox //PAGEBREAK! 183f9a06440SRuss Cox // Allocate a new inode with the given type on device dev. 184a5fbfe41SRobert Morris // A free inode has a type of zero. 185f9a06440SRuss Cox struct inode* 186f9a06440SRuss Cox ialloc(uint dev, short type) 187f9a06440SRuss Cox { 188f9a06440SRuss Cox int inum; 189f9a06440SRuss Cox struct buf *bp; 190f9a06440SRuss Cox struct dinode *dip; 191a5fbfe41SRobert Morris 192a5fbfe41SRobert Morris for(inum = 1; inum < sb.ninodes; inum++){ 1938320d61bSFrans Kaashoek bp = bread(dev, IBLOCK(inum, sb)); 194f9a06440SRuss Cox dip = (struct dinode*)bp->data + inum%IPB; 195f9a06440SRuss Cox if(dip->type == 0){ // a free inode 196f9a06440SRuss Cox memset(dip, 0, sizeof(*dip)); 197f9a06440SRuss Cox dip->type = type; 19813a96baeSFrans Kaashoek log_write(bp); // mark it allocated on the disk 199f9a06440SRuss Cox brelse(bp); 200f9a06440SRuss Cox return iget(dev, inum); 201f9a06440SRuss Cox } 202f9a06440SRuss Cox brelse(bp); 203f9a06440SRuss Cox } 204f9a06440SRuss Cox panic("ialloc: no inodes"); 205f9a06440SRuss Cox } 206f9a06440SRuss Cox 20738eee5bcSRobert Morris // Copy a modified in-memory inode to disk. 208f9a06440SRuss Cox void 209f9a06440SRuss Cox iupdate(struct inode *ip) 210f9a06440SRuss Cox { 211f9a06440SRuss Cox struct buf *bp; 212f9a06440SRuss Cox struct dinode *dip; 213f9a06440SRuss Cox 2148320d61bSFrans Kaashoek bp = bread(ip->dev, IBLOCK(ip->inum, sb)); 215f9a06440SRuss Cox dip = (struct dinode*)bp->data + ip->inum%IPB; 216f9a06440SRuss Cox dip->type = ip->type; 217f9a06440SRuss Cox dip->major = ip->major; 218f9a06440SRuss Cox dip->minor = ip->minor; 219f9a06440SRuss Cox dip->nlink = ip->nlink; 220f9a06440SRuss Cox dip->size = ip->size; 221f9a06440SRuss Cox memmove(dip->addrs, ip->addrs, sizeof(ip->addrs)); 22213a96baeSFrans Kaashoek log_write(bp); 223f9a06440SRuss Cox brelse(bp); 224f9a06440SRuss Cox } 225f9a06440SRuss Cox 226f5527388Srsc // Find the inode with number inum on device dev 22738eee5bcSRobert Morris // and return the in-memory copy. Does not lock 22838eee5bcSRobert Morris // the inode and does not read it from disk. 22907090dd7Srsc static struct inode* 23011a9947fSrtm iget(uint dev, uint inum) 23111a9947fSrtm { 232bcca6c6bSrsc struct inode *ip, *empty; 23311a9947fSrtm 234bcca6c6bSrsc acquire(&icache.lock); 23511a9947fSrtm 23638eee5bcSRobert Morris // Is the inode already cached? 237bcca6c6bSrsc empty = 0; 238bcca6c6bSrsc for(ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++){ 2390d6bbd31Srsc if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){ 2400d6bbd31Srsc ip->ref++; 241bcca6c6bSrsc release(&icache.lock); 24207090dd7Srsc return ip; 24311a9947fSrtm } 244bcca6c6bSrsc if(empty == 0 && ip->ref == 0) // Remember empty slot. 245bcca6c6bSrsc empty = ip; 24611a9947fSrtm } 24711a9947fSrtm 24838eee5bcSRobert Morris // Recycle an inode cache entry. 249bcca6c6bSrsc if(empty == 0) 25032eea766Srsc panic("iget: no inodes"); 25111a9947fSrtm 252bcca6c6bSrsc ip = empty; 253bcca6c6bSrsc ip->dev = dev; 254bcca6c6bSrsc ip->inum = inum; 255bcca6c6bSrsc ip->ref = 1; 256f32f3638Srsc ip->flags = 0; 257bcca6c6bSrsc release(&icache.lock); 25811a9947fSrtm 25907090dd7Srsc return ip; 260f32f3638Srsc } 261f32f3638Srsc 262eaea18cbSrsc // Increment reference count for ip. 263eaea18cbSrsc // Returns ip to enable ip = idup(ip1) idiom. 26407090dd7Srsc struct inode* 26507090dd7Srsc idup(struct inode *ip) 266f32f3638Srsc { 267eaea18cbSrsc acquire(&icache.lock); 268eaea18cbSrsc ip->ref++; 269eaea18cbSrsc release(&icache.lock); 27007090dd7Srsc return ip; 271f32f3638Srsc } 272f32f3638Srsc 2731ddfbbb1SFrans Kaashoek // Lock the given inode. 27438eee5bcSRobert Morris // Reads the inode from disk if necessary. 27507090dd7Srsc void 27607090dd7Srsc ilock(struct inode *ip) 277f32f3638Srsc { 278f32f3638Srsc struct buf *bp; 279f32f3638Srsc struct dinode *dip; 280eaea18cbSrsc 28107090dd7Srsc if(ip == 0 || ip->ref < 1) 28207090dd7Srsc panic("ilock"); 283f32f3638Srsc 284dec637bcSFrans Kaashoek acquiresleep(&ip->lock); 285f32f3638Srsc 2869cec455bSRobert Morris if((ip->flags & I_VALID) == 0){ 2878320d61bSFrans Kaashoek bp = bread(ip->dev, IBLOCK(ip->inum, sb)); 2883341e30fSrsc dip = (struct dinode*)bp->data + ip->inum%IPB; 289bcca6c6bSrsc ip->type = dip->type; 290bcca6c6bSrsc ip->major = dip->major; 291bcca6c6bSrsc ip->minor = dip->minor; 292bcca6c6bSrsc ip->nlink = dip->nlink; 293bcca6c6bSrsc ip->size = dip->size; 294bcca6c6bSrsc memmove(ip->addrs, dip->addrs, sizeof(ip->addrs)); 29511a9947fSrtm brelse(bp); 296f32f3638Srsc ip->flags |= I_VALID; 297eaea18cbSrsc if(ip->type == 0) 298eaea18cbSrsc panic("ilock: no type"); 29911a9947fSrtm } 300bcca6c6bSrsc } 301bcca6c6bSrsc 302bcca6c6bSrsc // Unlock the given inode. 30307090dd7Srsc void 304bcca6c6bSrsc iunlock(struct inode *ip) 305bcca6c6bSrsc { 306dec637bcSFrans Kaashoek if(ip == 0 || !holdingsleep(&ip->lock) || ip->ref < 1) 307bcca6c6bSrsc panic("iunlock"); 308bcca6c6bSrsc 309dec637bcSFrans Kaashoek releasesleep(&ip->lock); 310bcca6c6bSrsc } 311bcca6c6bSrsc 31238eee5bcSRobert Morris // Drop a reference to an in-memory inode. 31338eee5bcSRobert Morris // If that was the last reference, the inode cache entry can 31438eee5bcSRobert Morris // be recycled. 31538eee5bcSRobert Morris // If that was the last reference and the inode has no links 31638eee5bcSRobert Morris // to it, free the inode (and its content) on disk. 3172c565472SRobert Morris // All calls to iput() must be inside a transaction in 3182c565472SRobert Morris // case it has to free the inode. 319bcca6c6bSrsc void 32007090dd7Srsc iput(struct inode *ip) 321bcca6c6bSrsc { 322f32f3638Srsc acquire(&icache.lock); 323f32f3638Srsc if(ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0){ 324e2b4583dSRobert Morris // inode has no links and no other references: truncate and free. 325*aba8423cSRobert Morris acquiresleep(&ip->lock); 326f32f3638Srsc release(&icache.lock); 327f32f3638Srsc itrunc(ip); 328f32f3638Srsc ip->type = 0; 329f32f3638Srsc iupdate(ip); 330f32f3638Srsc acquire(&icache.lock); 331ce72cadbSrsc ip->flags = 0; 332*aba8423cSRobert Morris releasesleep(&ip->lock); 333f32f3638Srsc } 334f32f3638Srsc ip->ref--; 335f32f3638Srsc release(&icache.lock); 336bcca6c6bSrsc } 337bcca6c6bSrsc 3387895178dSrsc // Common idiom: unlock, then put. 33907090dd7Srsc void 34007090dd7Srsc iunlockput(struct inode *ip) 34107090dd7Srsc { 34207090dd7Srsc iunlock(ip); 34307090dd7Srsc iput(ip); 34407090dd7Srsc } 34507090dd7Srsc 3468d2e9a48Srsc //PAGEBREAK! 34738eee5bcSRobert Morris // Inode content 348bcca6c6bSrsc // 34938eee5bcSRobert Morris // The content (data) associated with each inode is stored 35038eee5bcSRobert Morris // in blocks on the disk. The first NDIRECT block numbers 3517895178dSrsc // are listed in ip->addrs[]. The next NINDIRECT blocks are 35238eee5bcSRobert Morris // listed in block ip->addrs[NDIRECT]. 3539d3fb671Srtm 354bb207a1dSrsc // Return the disk block address of the nth block in inode ip. 35513ae8808Srsc // If there is no such block, bmap allocates one. 356eaea18cbSrsc static uint 35713ae8808Srsc bmap(struct inode *ip, uint bn) 35822bac2cbSkaashoek { 359bcca6c6bSrsc uint addr, *a; 360bcca6c6bSrsc struct buf *bp; 36122bac2cbSkaashoek 362ea2909b6Skaashoek if(bn < NDIRECT){ 36313ae8808Srsc if((addr = ip->addrs[bn]) == 0) 364bcca6c6bSrsc ip->addrs[bn] = addr = balloc(ip->dev); 365bcca6c6bSrsc return addr; 366bcca6c6bSrsc } 367bcca6c6bSrsc bn -= NDIRECT; 368bcca6c6bSrsc 369bcca6c6bSrsc if(bn < NINDIRECT){ 370bcca6c6bSrsc // Load indirect block, allocating if necessary. 37113ae8808Srsc if((addr = ip->addrs[NDIRECT]) == 0) 372ba6cd8a6Srsc ip->addrs[NDIRECT] = addr = balloc(ip->dev); 373bcca6c6bSrsc bp = bread(ip->dev, addr); 374bcca6c6bSrsc a = (uint*)bp->data; 375bcca6c6bSrsc if((addr = a[bn]) == 0){ 376bcca6c6bSrsc a[bn] = addr = balloc(ip->dev); 37713a96baeSFrans Kaashoek log_write(bp); 378bcca6c6bSrsc } 379bcca6c6bSrsc brelse(bp); 380bcca6c6bSrsc return addr; 38122bac2cbSkaashoek } 38222bac2cbSkaashoek 383bcca6c6bSrsc panic("bmap: out of range"); 384bcca6c6bSrsc } 385bcca6c6bSrsc 386bcca6c6bSrsc // Truncate inode (discard contents). 38738eee5bcSRobert Morris // Only called when the inode has no links 38838eee5bcSRobert Morris // to it (no directory entries referring to it) 38938eee5bcSRobert Morris // and has no in-memory reference to it (is 39038eee5bcSRobert Morris // not an open file or current directory). 391fbf91039Srsc static void 3922aa4c3bcSrtm itrunc(struct inode *ip) 39322bac2cbSkaashoek { 394ea2909b6Skaashoek int i, j; 395bcca6c6bSrsc struct buf *bp; 3967d4aef6cSrsc uint *a; 39722bac2cbSkaashoek 398bcca6c6bSrsc for(i = 0; i < NDIRECT; i++){ 399bcca6c6bSrsc if(ip->addrs[i]){ 40022bac2cbSkaashoek bfree(ip->dev, ip->addrs[i]); 40122bac2cbSkaashoek ip->addrs[i] = 0; 40222bac2cbSkaashoek } 40322bac2cbSkaashoek } 404bcca6c6bSrsc 405ba6cd8a6Srsc if(ip->addrs[NDIRECT]){ 406ba6cd8a6Srsc bp = bread(ip->dev, ip->addrs[NDIRECT]); 407bcca6c6bSrsc a = (uint*)bp->data; 408bcca6c6bSrsc for(j = 0; j < NINDIRECT; j++){ 409bcca6c6bSrsc if(a[j]) 410bcca6c6bSrsc bfree(ip->dev, a[j]); 411bcca6c6bSrsc } 412bcca6c6bSrsc brelse(bp); 413ba6cd8a6Srsc bfree(ip->dev, ip->addrs[NDIRECT]); 414ba6cd8a6Srsc ip->addrs[NDIRECT] = 0; 415bcca6c6bSrsc } 416bcca6c6bSrsc 41722bac2cbSkaashoek ip->size = 0; 41822bac2cbSkaashoek iupdate(ip); 41922bac2cbSkaashoek } 42022bac2cbSkaashoek 421bb207a1dSrsc // Copy stat information from inode. 422e958c538Skaashoek void 4231f544842Skaashoek stati(struct inode *ip, struct stat *st) 4241f544842Skaashoek { 4251dca3afbSrsc st->dev = ip->dev; 4261dca3afbSrsc st->ino = ip->inum; 4271dca3afbSrsc st->type = ip->type; 4281dca3afbSrsc st->nlink = ip->nlink; 4291dca3afbSrsc st->size = ip->size; 4301f544842Skaashoek } 4311f544842Skaashoek 432eaea18cbSrsc //PAGEBREAK! 433bb207a1dSrsc // Read data from inode. 434c59361f1Srtm int 43517a85657Srtm readi(struct inode *ip, char *dst, uint off, uint n) 436c59361f1Srtm { 437bcca6c6bSrsc uint tot, m; 438c59361f1Srtm struct buf *bp; 439c59361f1Srtm 440939f9edeSkaashoek if(ip->type == T_DEV){ 4411dca3afbSrsc if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read) 442939f9edeSkaashoek return -1; 443d844f0f9Srsc return devsw[ip->major].read(ip, dst, n); 444939f9edeSkaashoek } 445939f9edeSkaashoek 4467895178dSrsc if(off > ip->size || off + n < off) 447bcca6c6bSrsc return -1; 448bcca6c6bSrsc if(off + n > ip->size) 449bcca6c6bSrsc n = ip->size - off; 450bcca6c6bSrsc 451bcca6c6bSrsc for(tot=0; tot<n; tot+=m, off+=m, dst+=m){ 45213ae8808Srsc bp = bread(ip->dev, bmap(ip, off/BSIZE)); 453bcca6c6bSrsc m = min(n - tot, BSIZE - off%BSIZE); 4546670d3b5SFrans Kaashoek /* 4556670d3b5SFrans Kaashoek cprintf("data off %d:\n", off); 4566670d3b5SFrans Kaashoek for (int j = 0; j < min(m, 10); j++) { 4576670d3b5SFrans Kaashoek cprintf("%x ", bp->data[off%BSIZE+j]); 4586670d3b5SFrans Kaashoek } 4596670d3b5SFrans Kaashoek cprintf("\n"); 4606670d3b5SFrans Kaashoek */ 461bcca6c6bSrsc memmove(dst, bp->data + off%BSIZE, m); 462c59361f1Srtm brelse(bp); 463c59361f1Srtm } 464bcca6c6bSrsc return n; 465ea2909b6Skaashoek } 466ea2909b6Skaashoek 467eaea18cbSrsc // PAGEBREAK! 468bb207a1dSrsc // Write data to inode. 469ea2909b6Skaashoek int 470bcca6c6bSrsc writei(struct inode *ip, char *src, uint off, uint n) 4716fa5ffb5Skaashoek { 472bcca6c6bSrsc uint tot, m; 4737d4aef6cSrsc struct buf *bp; 4747d4aef6cSrsc 4756fa5ffb5Skaashoek if(ip->type == T_DEV){ 4761dca3afbSrsc if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write) 477939f9edeSkaashoek return -1; 478d844f0f9Srsc return devsw[ip->major].write(ip, src, n); 4797d4aef6cSrsc } 4807d4aef6cSrsc 4811ddfbbb1SFrans Kaashoek if(off > ip->size || off + n < off) 482bcca6c6bSrsc return -1; 4831ddfbbb1SFrans Kaashoek if(off + n > MAXFILE*BSIZE) 4842e590463SRobert Morris return -1; 485bcca6c6bSrsc 486bcca6c6bSrsc for(tot=0; tot<n; tot+=m, off+=m, src+=m){ 48713ae8808Srsc bp = bread(ip->dev, bmap(ip, off/BSIZE)); 488bcca6c6bSrsc m = min(n - tot, BSIZE - off%BSIZE); 489bcca6c6bSrsc memmove(bp->data + off%BSIZE, src, m); 4902e590463SRobert Morris log_write(bp); 49128d9ef04Skaashoek brelse(bp); 49228d9ef04Skaashoek } 493bcca6c6bSrsc 494bcca6c6bSrsc if(n > 0 && off > ip->size){ 49548b82470Srsc ip->size = off; 49628d9ef04Skaashoek iupdate(ip); 49728d9ef04Skaashoek } 498bcca6c6bSrsc return n; 4996fa5ffb5Skaashoek } 5006fa5ffb5Skaashoek 501eaea18cbSrsc //PAGEBREAK! 502bcca6c6bSrsc // Directories 503bcca6c6bSrsc 504eaea18cbSrsc int 505fbf91039Srsc namecmp(const char *s, const char *t) 506fbf91039Srsc { 507766ba5ccSrsc return strncmp(s, t, DIRSIZ); 508fbf91039Srsc } 509fbf91039Srsc 510bcca6c6bSrsc // Look for a directory entry in a directory. 511eaea18cbSrsc // If found, set *poff to byte offset of entry. 51207090dd7Srsc struct inode* 513fbf91039Srsc dirlookup(struct inode *dp, char *name, uint *poff) 514bcca6c6bSrsc { 515f32f3638Srsc uint off, inum; 516327cc21fSRobert Morris struct dirent de; 517bcca6c6bSrsc 518bcca6c6bSrsc if(dp->type != T_DIR) 51920365348Srtm panic("dirlookup not DIR"); 520bcca6c6bSrsc 521327cc21fSRobert Morris for(off = 0; off < dp->size; off += sizeof(de)){ 522327cc21fSRobert Morris if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 52361f26e3cSGrant Wu panic("dirlookup read"); 524327cc21fSRobert Morris if(de.inum == 0) 525bcca6c6bSrsc continue; 526327cc21fSRobert Morris if(namecmp(name, de.name) == 0){ 527bcca6c6bSrsc // entry matches path element 528e2a620daSrsc if(poff) 529327cc21fSRobert Morris *poff = off; 530327cc21fSRobert Morris inum = de.inum; 531f32f3638Srsc return iget(dp->dev, inum); 532f32f3638Srsc } 533f32f3638Srsc } 534327cc21fSRobert Morris 535bcca6c6bSrsc return 0; 536bcca6c6bSrsc } 537bcca6c6bSrsc 53813ae8808Srsc // Write a new directory entry (name, inum) into the directory dp. 539eaea18cbSrsc int 54013ae8808Srsc dirlink(struct inode *dp, char *name, uint inum) 541bcca6c6bSrsc { 542e2a620daSrsc int off; 543bcca6c6bSrsc struct dirent de; 54407090dd7Srsc struct inode *ip; 545f32f3638Srsc 546eaea18cbSrsc // Check that name is not present. 54707090dd7Srsc if((ip = dirlookup(dp, name, 0)) != 0){ 54807090dd7Srsc iput(ip); 549f32f3638Srsc return -1; 550f32f3638Srsc } 551bcca6c6bSrsc 552bcca6c6bSrsc // Look for an empty dirent. 553bcca6c6bSrsc for(off = 0; off < dp->size; off += sizeof(de)){ 554bcca6c6bSrsc if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 5557895178dSrsc panic("dirlink read"); 556bcca6c6bSrsc if(de.inum == 0) 557bcca6c6bSrsc break; 558bcca6c6bSrsc } 559bcca6c6bSrsc 560766ba5ccSrsc strncpy(de.name, name, DIRSIZ); 56113ae8808Srsc de.inum = inum; 562bcca6c6bSrsc if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 5637895178dSrsc panic("dirlink"); 564f32f3638Srsc 565f32f3638Srsc return 0; 566bcca6c6bSrsc } 567bcca6c6bSrsc 5688d2e9a48Srsc //PAGEBREAK! 569bcca6c6bSrsc // Paths 570bcca6c6bSrsc 571eaea18cbSrsc // Copy the next path element from path into name. 572eaea18cbSrsc // Return a pointer to the element following the copied one. 573eaea18cbSrsc // The returned path has no leading slashes, 574eaea18cbSrsc // so the caller can check *path=='\0' to see if the name is the last one. 575eaea18cbSrsc // If no name to remove, return 0. 576ab5c2dbbSrsc // 577ab5c2dbbSrsc // Examples: 578eaea18cbSrsc // skipelem("a/bb/c", name) = "bb/c", setting name = "a" 5797895178dSrsc // skipelem("///a//bb", name) = "bb", setting name = "a" 58013ae8808Srsc // skipelem("a", name) = "", setting name = "a" 581eaea18cbSrsc // skipelem("", name) = skipelem("////", name) = 0 582ab5c2dbbSrsc // 583ab5c2dbbSrsc static char* 584fbf91039Srsc skipelem(char *path, char *name) 585ab5c2dbbSrsc { 586fbf91039Srsc char *s; 587fbf91039Srsc int len; 588fbf91039Srsc 589ab5c2dbbSrsc while(*path == '/') 590ab5c2dbbSrsc path++; 591ab5c2dbbSrsc if(*path == 0) 592ab5c2dbbSrsc return 0; 593fbf91039Srsc s = path; 594ab5c2dbbSrsc while(*path != '/' && *path != 0) 595ab5c2dbbSrsc path++; 596fbf91039Srsc len = path - s; 597fbf91039Srsc if(len >= DIRSIZ) 598fbf91039Srsc memmove(name, s, DIRSIZ); 599fbf91039Srsc else { 600fbf91039Srsc memmove(name, s, len); 601fbf91039Srsc name[len] = 0; 602fbf91039Srsc } 603ab5c2dbbSrsc while(*path == '/') 604ab5c2dbbSrsc path++; 605ab5c2dbbSrsc return path; 606ab5c2dbbSrsc } 607ab5c2dbbSrsc 608eaea18cbSrsc // Look up and return the inode for a path name. 609bf2932a6Srsc // If parent != 0, return the inode for the parent and copy the final 610bf2932a6Srsc // path element into name, which must have room for DIRSIZ bytes. 6112c565472SRobert Morris // Must be called inside a transaction since it calls iput(). 61207090dd7Srsc static struct inode* 613f9a06440SRuss Cox namex(char *path, int nameiparent, char *name) 6149d3fb671Srtm { 61507090dd7Srsc struct inode *ip, *next; 6169d3fb671Srtm 617ab5c2dbbSrsc if(*path == '/') 6182ce40d70Srtm ip = iget(ROOTDEV, ROOTINO); 619f32f3638Srsc else 62048755214SRuss Cox ip = idup(proc->cwd); 6219d3fb671Srtm 622fbf91039Srsc while((path = skipelem(path, name)) != 0){ 62307090dd7Srsc ilock(ip); 62407090dd7Srsc if(ip->type != T_DIR){ 62507090dd7Srsc iunlockput(ip); 626eaea18cbSrsc return 0; 627eaea18cbSrsc } 628f9a06440SRuss Cox if(nameiparent && *path == '\0'){ 629e2a620daSrsc // Stop one level early. 63007090dd7Srsc iunlock(ip); 63107090dd7Srsc return ip; 632ab5c2dbbSrsc } 6337895178dSrsc if((next = dirlookup(ip, name, 0)) == 0){ 63407090dd7Srsc iunlockput(ip); 635eaea18cbSrsc return 0; 636eaea18cbSrsc } 63707090dd7Srsc iunlockput(ip); 63807090dd7Srsc ip = next; 639ab5c2dbbSrsc } 640f9a06440SRuss Cox if(nameiparent){ 64107090dd7Srsc iput(ip); 6425051da6dSrtm return 0; 64320365348Srtm } 64407090dd7Srsc return ip; 6450633b971Skaashoek } 6469d3fb671Srtm 6477895178dSrsc struct inode* 6487895178dSrsc namei(char *path) 6497895178dSrsc { 6507895178dSrsc char name[DIRSIZ]; 65113ae8808Srsc return namex(path, 0, name); 6527895178dSrsc } 653bf2932a6Srsc 6547895178dSrsc struct inode* 6557895178dSrsc nameiparent(char *path, char *name) 6567895178dSrsc { 65713ae8808Srsc return namex(path, 1, name); 6587895178dSrsc } 659