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 // 11538eee5bcSRobert Morris // An inode and its in-memory represtative 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) 12038eee5bcSRobert Morris // is non-zero. ialloc() allocates, iput() frees if 12138eee5bcSRobert Morris // the link count has 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 12638eee5bcSRobert Morris // files and current directories). iget() to find or 12738eee5bcSRobert Morris // create a cache entry and increment its ref, iput() 12838eee5bcSRobert Morris // to decrement 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 138*dec637bcSFrans 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 { 166*dec637bcSFrans Kaashoek int i = 0; 167*dec637bcSFrans Kaashoek 16834295f46Srsc initlock(&icache.lock, "icache"); 169*dec637bcSFrans Kaashoek for(i = 0; i < NINODE; i++) { 170*dec637bcSFrans Kaashoek initsleeplock(&icache.inode[i].lock, "inode"); 171*dec637bcSFrans Kaashoek } 172*dec637bcSFrans 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 284*dec637bcSFrans Kaashoek acquiresleep(&ip->lock); 285f32f3638Srsc 286f32f3638Srsc if(!(ip->flags & I_VALID)){ 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 { 306*dec637bcSFrans Kaashoek if(ip == 0 || !holdingsleep(&ip->lock) || ip->ref < 1) 307bcca6c6bSrsc panic("iunlock"); 308bcca6c6bSrsc 309*dec637bcSFrans 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. 325f32f3638Srsc release(&icache.lock); 326f32f3638Srsc itrunc(ip); 327f32f3638Srsc ip->type = 0; 328f32f3638Srsc iupdate(ip); 329f32f3638Srsc acquire(&icache.lock); 330ce72cadbSrsc ip->flags = 0; 331f32f3638Srsc } 332f32f3638Srsc ip->ref--; 333f32f3638Srsc release(&icache.lock); 334bcca6c6bSrsc } 335bcca6c6bSrsc 3367895178dSrsc // Common idiom: unlock, then put. 33707090dd7Srsc void 33807090dd7Srsc iunlockput(struct inode *ip) 33907090dd7Srsc { 34007090dd7Srsc iunlock(ip); 34107090dd7Srsc iput(ip); 34207090dd7Srsc } 34307090dd7Srsc 3448d2e9a48Srsc //PAGEBREAK! 34538eee5bcSRobert Morris // Inode content 346bcca6c6bSrsc // 34738eee5bcSRobert Morris // The content (data) associated with each inode is stored 34838eee5bcSRobert Morris // in blocks on the disk. The first NDIRECT block numbers 3497895178dSrsc // are listed in ip->addrs[]. The next NINDIRECT blocks are 35038eee5bcSRobert Morris // listed in block ip->addrs[NDIRECT]. 3519d3fb671Srtm 352bb207a1dSrsc // Return the disk block address of the nth block in inode ip. 35313ae8808Srsc // If there is no such block, bmap allocates one. 354eaea18cbSrsc static uint 35513ae8808Srsc bmap(struct inode *ip, uint bn) 35622bac2cbSkaashoek { 357bcca6c6bSrsc uint addr, *a; 358bcca6c6bSrsc struct buf *bp; 35922bac2cbSkaashoek 360ea2909b6Skaashoek if(bn < NDIRECT){ 36113ae8808Srsc if((addr = ip->addrs[bn]) == 0) 362bcca6c6bSrsc ip->addrs[bn] = addr = balloc(ip->dev); 363bcca6c6bSrsc return addr; 364bcca6c6bSrsc } 365bcca6c6bSrsc bn -= NDIRECT; 366bcca6c6bSrsc 367bcca6c6bSrsc if(bn < NINDIRECT){ 368bcca6c6bSrsc // Load indirect block, allocating if necessary. 36913ae8808Srsc if((addr = ip->addrs[NDIRECT]) == 0) 370ba6cd8a6Srsc ip->addrs[NDIRECT] = addr = balloc(ip->dev); 371bcca6c6bSrsc bp = bread(ip->dev, addr); 372bcca6c6bSrsc a = (uint*)bp->data; 373bcca6c6bSrsc if((addr = a[bn]) == 0){ 374bcca6c6bSrsc a[bn] = addr = balloc(ip->dev); 37513a96baeSFrans Kaashoek log_write(bp); 376bcca6c6bSrsc } 377bcca6c6bSrsc brelse(bp); 378bcca6c6bSrsc return addr; 37922bac2cbSkaashoek } 38022bac2cbSkaashoek 381bcca6c6bSrsc panic("bmap: out of range"); 382bcca6c6bSrsc } 383bcca6c6bSrsc 384bcca6c6bSrsc // Truncate inode (discard contents). 38538eee5bcSRobert Morris // Only called when the inode has no links 38638eee5bcSRobert Morris // to it (no directory entries referring to it) 38738eee5bcSRobert Morris // and has no in-memory reference to it (is 38838eee5bcSRobert Morris // not an open file or current directory). 389fbf91039Srsc static void 3902aa4c3bcSrtm itrunc(struct inode *ip) 39122bac2cbSkaashoek { 392ea2909b6Skaashoek int i, j; 393bcca6c6bSrsc struct buf *bp; 3947d4aef6cSrsc uint *a; 39522bac2cbSkaashoek 396bcca6c6bSrsc for(i = 0; i < NDIRECT; i++){ 397bcca6c6bSrsc if(ip->addrs[i]){ 39822bac2cbSkaashoek bfree(ip->dev, ip->addrs[i]); 39922bac2cbSkaashoek ip->addrs[i] = 0; 40022bac2cbSkaashoek } 40122bac2cbSkaashoek } 402bcca6c6bSrsc 403ba6cd8a6Srsc if(ip->addrs[NDIRECT]){ 404ba6cd8a6Srsc bp = bread(ip->dev, ip->addrs[NDIRECT]); 405bcca6c6bSrsc a = (uint*)bp->data; 406bcca6c6bSrsc for(j = 0; j < NINDIRECT; j++){ 407bcca6c6bSrsc if(a[j]) 408bcca6c6bSrsc bfree(ip->dev, a[j]); 409bcca6c6bSrsc } 410bcca6c6bSrsc brelse(bp); 411ba6cd8a6Srsc bfree(ip->dev, ip->addrs[NDIRECT]); 412ba6cd8a6Srsc ip->addrs[NDIRECT] = 0; 413bcca6c6bSrsc } 414bcca6c6bSrsc 41522bac2cbSkaashoek ip->size = 0; 41622bac2cbSkaashoek iupdate(ip); 41722bac2cbSkaashoek } 41822bac2cbSkaashoek 419bb207a1dSrsc // Copy stat information from inode. 420e958c538Skaashoek void 4211f544842Skaashoek stati(struct inode *ip, struct stat *st) 4221f544842Skaashoek { 4231dca3afbSrsc st->dev = ip->dev; 4241dca3afbSrsc st->ino = ip->inum; 4251dca3afbSrsc st->type = ip->type; 4261dca3afbSrsc st->nlink = ip->nlink; 4271dca3afbSrsc st->size = ip->size; 4281f544842Skaashoek } 4291f544842Skaashoek 430eaea18cbSrsc //PAGEBREAK! 431bb207a1dSrsc // Read data from inode. 432c59361f1Srtm int 43317a85657Srtm readi(struct inode *ip, char *dst, uint off, uint n) 434c59361f1Srtm { 435bcca6c6bSrsc uint tot, m; 436c59361f1Srtm struct buf *bp; 437c59361f1Srtm 438939f9edeSkaashoek if(ip->type == T_DEV){ 4391dca3afbSrsc if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read) 440939f9edeSkaashoek return -1; 441d844f0f9Srsc return devsw[ip->major].read(ip, dst, n); 442939f9edeSkaashoek } 443939f9edeSkaashoek 4447895178dSrsc if(off > ip->size || off + n < off) 445bcca6c6bSrsc return -1; 446bcca6c6bSrsc if(off + n > ip->size) 447bcca6c6bSrsc n = ip->size - off; 448bcca6c6bSrsc 449bcca6c6bSrsc for(tot=0; tot<n; tot+=m, off+=m, dst+=m){ 45013ae8808Srsc bp = bread(ip->dev, bmap(ip, off/BSIZE)); 451bcca6c6bSrsc m = min(n - tot, BSIZE - off%BSIZE); 4526670d3b5SFrans Kaashoek /* 4536670d3b5SFrans Kaashoek cprintf("data off %d:\n", off); 4546670d3b5SFrans Kaashoek for (int j = 0; j < min(m, 10); j++) { 4556670d3b5SFrans Kaashoek cprintf("%x ", bp->data[off%BSIZE+j]); 4566670d3b5SFrans Kaashoek } 4576670d3b5SFrans Kaashoek cprintf("\n"); 4586670d3b5SFrans Kaashoek */ 459bcca6c6bSrsc memmove(dst, bp->data + off%BSIZE, m); 460c59361f1Srtm brelse(bp); 461c59361f1Srtm } 462bcca6c6bSrsc return n; 463ea2909b6Skaashoek } 464ea2909b6Skaashoek 465eaea18cbSrsc // PAGEBREAK! 466bb207a1dSrsc // Write data to inode. 467ea2909b6Skaashoek int 468bcca6c6bSrsc writei(struct inode *ip, char *src, uint off, uint n) 4696fa5ffb5Skaashoek { 470bcca6c6bSrsc uint tot, m; 4717d4aef6cSrsc struct buf *bp; 4727d4aef6cSrsc 4736fa5ffb5Skaashoek if(ip->type == T_DEV){ 4741dca3afbSrsc if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write) 475939f9edeSkaashoek return -1; 476d844f0f9Srsc return devsw[ip->major].write(ip, src, n); 4777d4aef6cSrsc } 4787d4aef6cSrsc 4791ddfbbb1SFrans Kaashoek if(off > ip->size || off + n < off) 480bcca6c6bSrsc return -1; 4811ddfbbb1SFrans Kaashoek if(off + n > MAXFILE*BSIZE) 4822e590463SRobert Morris return -1; 483bcca6c6bSrsc 484bcca6c6bSrsc for(tot=0; tot<n; tot+=m, off+=m, src+=m){ 48513ae8808Srsc bp = bread(ip->dev, bmap(ip, off/BSIZE)); 486bcca6c6bSrsc m = min(n - tot, BSIZE - off%BSIZE); 487bcca6c6bSrsc memmove(bp->data + off%BSIZE, src, m); 4882e590463SRobert Morris log_write(bp); 48928d9ef04Skaashoek brelse(bp); 49028d9ef04Skaashoek } 491bcca6c6bSrsc 492bcca6c6bSrsc if(n > 0 && off > ip->size){ 49348b82470Srsc ip->size = off; 49428d9ef04Skaashoek iupdate(ip); 49528d9ef04Skaashoek } 496bcca6c6bSrsc return n; 4976fa5ffb5Skaashoek } 4986fa5ffb5Skaashoek 499eaea18cbSrsc //PAGEBREAK! 500bcca6c6bSrsc // Directories 501bcca6c6bSrsc 502eaea18cbSrsc int 503fbf91039Srsc namecmp(const char *s, const char *t) 504fbf91039Srsc { 505766ba5ccSrsc return strncmp(s, t, DIRSIZ); 506fbf91039Srsc } 507fbf91039Srsc 508bcca6c6bSrsc // Look for a directory entry in a directory. 509eaea18cbSrsc // If found, set *poff to byte offset of entry. 51007090dd7Srsc struct inode* 511fbf91039Srsc dirlookup(struct inode *dp, char *name, uint *poff) 512bcca6c6bSrsc { 513f32f3638Srsc uint off, inum; 514327cc21fSRobert Morris struct dirent de; 515bcca6c6bSrsc 516bcca6c6bSrsc if(dp->type != T_DIR) 51720365348Srtm panic("dirlookup not DIR"); 518bcca6c6bSrsc 519327cc21fSRobert Morris for(off = 0; off < dp->size; off += sizeof(de)){ 520327cc21fSRobert Morris if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 521327cc21fSRobert Morris panic("dirlink read"); 522327cc21fSRobert Morris if(de.inum == 0) 523bcca6c6bSrsc continue; 524327cc21fSRobert Morris if(namecmp(name, de.name) == 0){ 525bcca6c6bSrsc // entry matches path element 526e2a620daSrsc if(poff) 527327cc21fSRobert Morris *poff = off; 528327cc21fSRobert Morris inum = de.inum; 529f32f3638Srsc return iget(dp->dev, inum); 530f32f3638Srsc } 531f32f3638Srsc } 532327cc21fSRobert Morris 533bcca6c6bSrsc return 0; 534bcca6c6bSrsc } 535bcca6c6bSrsc 53613ae8808Srsc // Write a new directory entry (name, inum) into the directory dp. 537eaea18cbSrsc int 53813ae8808Srsc dirlink(struct inode *dp, char *name, uint inum) 539bcca6c6bSrsc { 540e2a620daSrsc int off; 541bcca6c6bSrsc struct dirent de; 54207090dd7Srsc struct inode *ip; 543f32f3638Srsc 544eaea18cbSrsc // Check that name is not present. 54507090dd7Srsc if((ip = dirlookup(dp, name, 0)) != 0){ 54607090dd7Srsc iput(ip); 547f32f3638Srsc return -1; 548f32f3638Srsc } 549bcca6c6bSrsc 550bcca6c6bSrsc // Look for an empty dirent. 551bcca6c6bSrsc for(off = 0; off < dp->size; off += sizeof(de)){ 552bcca6c6bSrsc if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 5537895178dSrsc panic("dirlink read"); 554bcca6c6bSrsc if(de.inum == 0) 555bcca6c6bSrsc break; 556bcca6c6bSrsc } 557bcca6c6bSrsc 558766ba5ccSrsc strncpy(de.name, name, DIRSIZ); 55913ae8808Srsc de.inum = inum; 560bcca6c6bSrsc if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 5617895178dSrsc panic("dirlink"); 562f32f3638Srsc 563f32f3638Srsc return 0; 564bcca6c6bSrsc } 565bcca6c6bSrsc 5668d2e9a48Srsc //PAGEBREAK! 567bcca6c6bSrsc // Paths 568bcca6c6bSrsc 569eaea18cbSrsc // Copy the next path element from path into name. 570eaea18cbSrsc // Return a pointer to the element following the copied one. 571eaea18cbSrsc // The returned path has no leading slashes, 572eaea18cbSrsc // so the caller can check *path=='\0' to see if the name is the last one. 573eaea18cbSrsc // If no name to remove, return 0. 574ab5c2dbbSrsc // 575ab5c2dbbSrsc // Examples: 576eaea18cbSrsc // skipelem("a/bb/c", name) = "bb/c", setting name = "a" 5777895178dSrsc // skipelem("///a//bb", name) = "bb", setting name = "a" 57813ae8808Srsc // skipelem("a", name) = "", setting name = "a" 579eaea18cbSrsc // skipelem("", name) = skipelem("////", name) = 0 580ab5c2dbbSrsc // 581ab5c2dbbSrsc static char* 582fbf91039Srsc skipelem(char *path, char *name) 583ab5c2dbbSrsc { 584fbf91039Srsc char *s; 585fbf91039Srsc int len; 586fbf91039Srsc 587ab5c2dbbSrsc while(*path == '/') 588ab5c2dbbSrsc path++; 589ab5c2dbbSrsc if(*path == 0) 590ab5c2dbbSrsc return 0; 591fbf91039Srsc s = path; 592ab5c2dbbSrsc while(*path != '/' && *path != 0) 593ab5c2dbbSrsc path++; 594fbf91039Srsc len = path - s; 595fbf91039Srsc if(len >= DIRSIZ) 596fbf91039Srsc memmove(name, s, DIRSIZ); 597fbf91039Srsc else { 598fbf91039Srsc memmove(name, s, len); 599fbf91039Srsc name[len] = 0; 600fbf91039Srsc } 601ab5c2dbbSrsc while(*path == '/') 602ab5c2dbbSrsc path++; 603ab5c2dbbSrsc return path; 604ab5c2dbbSrsc } 605ab5c2dbbSrsc 606eaea18cbSrsc // Look up and return the inode for a path name. 607bf2932a6Srsc // If parent != 0, return the inode for the parent and copy the final 608bf2932a6Srsc // path element into name, which must have room for DIRSIZ bytes. 6092c565472SRobert Morris // Must be called inside a transaction since it calls iput(). 61007090dd7Srsc static struct inode* 611f9a06440SRuss Cox namex(char *path, int nameiparent, char *name) 6129d3fb671Srtm { 61307090dd7Srsc struct inode *ip, *next; 6149d3fb671Srtm 615ab5c2dbbSrsc if(*path == '/') 6162ce40d70Srtm ip = iget(ROOTDEV, ROOTINO); 617f32f3638Srsc else 61848755214SRuss Cox ip = idup(proc->cwd); 6199d3fb671Srtm 620fbf91039Srsc while((path = skipelem(path, name)) != 0){ 62107090dd7Srsc ilock(ip); 62207090dd7Srsc if(ip->type != T_DIR){ 62307090dd7Srsc iunlockput(ip); 624eaea18cbSrsc return 0; 625eaea18cbSrsc } 626f9a06440SRuss Cox if(nameiparent && *path == '\0'){ 627e2a620daSrsc // Stop one level early. 62807090dd7Srsc iunlock(ip); 62907090dd7Srsc return ip; 630ab5c2dbbSrsc } 6317895178dSrsc if((next = dirlookup(ip, name, 0)) == 0){ 63207090dd7Srsc iunlockput(ip); 633eaea18cbSrsc return 0; 634eaea18cbSrsc } 63507090dd7Srsc iunlockput(ip); 63607090dd7Srsc ip = next; 637ab5c2dbbSrsc } 638f9a06440SRuss Cox if(nameiparent){ 63907090dd7Srsc iput(ip); 6405051da6dSrtm return 0; 64120365348Srtm } 64207090dd7Srsc return ip; 6430633b971Skaashoek } 6449d3fb671Srtm 6457895178dSrsc struct inode* 6467895178dSrsc namei(char *path) 6477895178dSrsc { 6487895178dSrsc char name[DIRSIZ]; 64913ae8808Srsc return namex(path, 0, name); 6507895178dSrsc } 651bf2932a6Srsc 6527895178dSrsc struct inode* 6537895178dSrsc nameiparent(char *path, char *name) 6547895178dSrsc { 65513ae8808Srsc return namex(path, 1, name); 6567895178dSrsc } 657