1bcca6c6bSrsc // File system implementation. 2bcca6c6bSrsc // 3bcca6c6bSrsc // Four layers: 4bcca6c6bSrsc // + Blocks: allocator for raw disk blocks. 5bcca6c6bSrsc // + Files: inode allocator, reading, writing, metadata. 6bcca6c6bSrsc // + Directories: inode with special contents (list of other inodes!) 7bcca6c6bSrsc // + Names: paths like /usr/rtm/xv6/fs.c for convenient naming. 8bcca6c6bSrsc // 94d39b633Srsc // Disk layout is: superblock, inodes, block in-use bitmap, data blocks. 10eaea18cbSrsc // 11eaea18cbSrsc // This file contains the low-level file system manipulation 12eaea18cbSrsc // routines. The (higher-level) system call implementations 13eaea18cbSrsc // are in sysfile.c. 14bcca6c6bSrsc 1511a9947fSrtm #include "types.h" 161f544842Skaashoek #include "stat.h" 1711a9947fSrtm #include "param.h" 1811a9947fSrtm #include "x86.h" 1911a9947fSrtm #include "mmu.h" 2011a9947fSrtm #include "proc.h" 2111a9947fSrtm #include "defs.h" 2211a9947fSrtm #include "spinlock.h" 2311a9947fSrtm #include "buf.h" 2411a9947fSrtm #include "fs.h" 2511a9947fSrtm #include "fsvar.h" 266fa5ffb5Skaashoek #include "dev.h" 2711a9947fSrtm 28bcca6c6bSrsc #define min(a, b) ((a) < (b) ? (a) : (b)) 29fbf91039Srsc static void itrunc(struct inode*); 3011a9947fSrtm 31bcca6c6bSrsc // Blocks. 325be0039cSrtm 33f5527388Srsc // Allocate a disk block. 3424111398Skaashoek static uint 3524111398Skaashoek balloc(uint dev) 3624111398Skaashoek { 377d4aef6cSrsc int b, bi, m, ninodes, size; 3824111398Skaashoek struct buf *bp; 3924111398Skaashoek struct superblock *sb; 4024111398Skaashoek 4124111398Skaashoek bp = bread(dev, 1); 4224111398Skaashoek sb = (struct superblock*) bp->data; 4324111398Skaashoek size = sb->size; 4424111398Skaashoek ninodes = sb->ninodes; 4524111398Skaashoek 4624111398Skaashoek for(b = 0; b < size; b++) { 4724111398Skaashoek if(b % BPB == 0) { 4824111398Skaashoek brelse(bp); 4924111398Skaashoek bp = bread(dev, BBLOCK(b, ninodes)); 5024111398Skaashoek } 5124111398Skaashoek bi = b % BPB; 5224111398Skaashoek m = 0x1 << (bi % 8); 5324111398Skaashoek if((bp->data[bi/8] & m) == 0) { // is block free? 54902b13f5Srtm bp->data[bi/8] |= m; 55eaea18cbSrsc bwrite(bp); // mark it allocated on disk 5628d9ef04Skaashoek brelse(bp); 5724111398Skaashoek return b; 5824111398Skaashoek } 597d4aef6cSrsc } 607d4aef6cSrsc panic("balloc: out of blocks"); 617d4aef6cSrsc } 6224111398Skaashoek 63bb207a1dSrsc // Free a disk block. 6428d9ef04Skaashoek static void 6528d9ef04Skaashoek bfree(int dev, uint b) 6628d9ef04Skaashoek { 6728d9ef04Skaashoek struct buf *bp; 6828d9ef04Skaashoek struct superblock *sb; 697d4aef6cSrsc int bi, m, ninodes; 7028d9ef04Skaashoek 7128d9ef04Skaashoek bp = bread(dev, 1); 7228d9ef04Skaashoek sb = (struct superblock*) bp->data; 7328d9ef04Skaashoek ninodes = sb->ninodes; 7428d9ef04Skaashoek brelse(bp); 7528d9ef04Skaashoek 76c372e8dcSkaashoek bp = bread(dev, b); 77c372e8dcSkaashoek memset(bp->data, 0, BSIZE); 78eaea18cbSrsc bwrite(bp); 79c372e8dcSkaashoek brelse(bp); 80c372e8dcSkaashoek 8128d9ef04Skaashoek bp = bread(dev, BBLOCK(b, ninodes)); 8228d9ef04Skaashoek bi = b % BPB; 837d4aef6cSrsc m = 0x1 << (bi % 8); 84902b13f5Srtm if((bp->data[bi/8] & m) == 0) 85902b13f5Srtm panic("freeing free block"); 867d4aef6cSrsc bp->data[bi/8] &= ~m; 87eaea18cbSrsc bwrite(bp); // mark it free on disk 8828d9ef04Skaashoek brelse(bp); 8928d9ef04Skaashoek } 9024111398Skaashoek 91bcca6c6bSrsc // Inodes 92bcca6c6bSrsc // 93bcca6c6bSrsc // The inodes are laid out sequentially on disk immediately after 94bcca6c6bSrsc // the superblock. The kernel keeps a cache of the in-use 95bcca6c6bSrsc // on-disk structures to provide a place for synchronizing access 96bcca6c6bSrsc // to inodes shared between multiple processes. 97bcca6c6bSrsc // 98902b13f5Srtm // ip->ref counts the number of pointer references to this cached 99bcca6c6bSrsc // inode; references are typically kept in struct file and in cp->cwd. 100bcca6c6bSrsc // When ip->ref falls to zero, the inode is no longer cached. 101bcca6c6bSrsc // It is an error to use an inode without holding a reference to it. 102bcca6c6bSrsc // 103902b13f5Srtm // Inodes can be locked with I_BUSY (like bufs and B_BUSY). 104eaea18cbSrsc // Processes are only allowed to read and write inode 105eaea18cbSrsc // metadata and contents when holding the inode's lock. 106902b13f5Srtm // Because inode locks are held during disk accesses, 107902b13f5Srtm // they are implemented using a flag rather than with 108902b13f5Srtm // spin locks. Callers are responsible for locking 109eaea18cbSrsc // inodes before passing them to routines in this file; leaving 110eaea18cbSrsc // this responsibility with the caller makes it possible for them 111eaea18cbSrsc // to create arbitrarily-sized atomic operations. 112eaea18cbSrsc // 113eaea18cbSrsc // To give maximum control over locking to the callers, 114eaea18cbSrsc // the routines in this file that return inode pointers 115eaea18cbSrsc // return pointers to *unlocked* inodes. It is the callers' 116902b13f5Srtm // responsibility to lock them before using them. A non-zero 117902b13f5Srtm // ip->ref keeps these unlocked inodes in the cache. 118bcca6c6bSrsc 119bcca6c6bSrsc struct { 120bcca6c6bSrsc struct spinlock lock; 121bcca6c6bSrsc struct inode inode[NINODE]; 122bcca6c6bSrsc } icache; 123bcca6c6bSrsc 124bcca6c6bSrsc void 125bcca6c6bSrsc iinit(void) 126bcca6c6bSrsc { 127bcca6c6bSrsc initlock(&icache.lock, "icache.lock"); 128bcca6c6bSrsc } 129bcca6c6bSrsc 130f5527388Srsc // Find the inode with number inum on device dev 131eaea18cbSrsc // and return the in-memory copy. h 132*07090dd7Srsc static struct inode* 13311a9947fSrtm iget(uint dev, uint inum) 13411a9947fSrtm { 135bcca6c6bSrsc struct inode *ip, *empty; 13611a9947fSrtm 137bcca6c6bSrsc acquire(&icache.lock); 13811a9947fSrtm 139bcca6c6bSrsc // Try for cached inode. 140bcca6c6bSrsc empty = 0; 141bcca6c6bSrsc for(ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++){ 1420d6bbd31Srsc if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){ 1430d6bbd31Srsc ip->ref++; 144bcca6c6bSrsc release(&icache.lock); 145*07090dd7Srsc return ip; 14611a9947fSrtm } 147bcca6c6bSrsc if(empty == 0 && ip->ref == 0) // Remember empty slot. 148bcca6c6bSrsc empty = ip; 14911a9947fSrtm } 15011a9947fSrtm 151bcca6c6bSrsc // Allocate fresh inode. 152bcca6c6bSrsc if(empty == 0) 15332eea766Srsc panic("iget: no inodes"); 15411a9947fSrtm 155bcca6c6bSrsc ip = empty; 156bcca6c6bSrsc ip->dev = dev; 157bcca6c6bSrsc ip->inum = inum; 158bcca6c6bSrsc ip->ref = 1; 159f32f3638Srsc ip->flags = 0; 160bcca6c6bSrsc release(&icache.lock); 16111a9947fSrtm 162*07090dd7Srsc return ip; 163f32f3638Srsc } 164f32f3638Srsc 165eaea18cbSrsc // Increment reference count for ip. 166eaea18cbSrsc // Returns ip to enable ip = idup(ip1) idiom. 167*07090dd7Srsc struct inode* 168*07090dd7Srsc idup(struct inode *ip) 169f32f3638Srsc { 170eaea18cbSrsc acquire(&icache.lock); 171eaea18cbSrsc ip->ref++; 172eaea18cbSrsc release(&icache.lock); 173*07090dd7Srsc return ip; 174f32f3638Srsc } 175f32f3638Srsc 176f32f3638Srsc // Lock the given inode. 177*07090dd7Srsc void 178*07090dd7Srsc ilock(struct inode *ip) 179f32f3638Srsc { 180f32f3638Srsc struct buf *bp; 181f32f3638Srsc struct dinode *dip; 182eaea18cbSrsc 183*07090dd7Srsc if(ip == 0 || ip->ref < 1) 184*07090dd7Srsc panic("ilock"); 185f32f3638Srsc 186f32f3638Srsc acquire(&icache.lock); 187f32f3638Srsc while(ip->flags & I_BUSY) 188f32f3638Srsc sleep(ip, &icache.lock); 189f32f3638Srsc ip->flags |= I_BUSY; 190f32f3638Srsc release(&icache.lock); 191f32f3638Srsc 192f32f3638Srsc if(!(ip->flags & I_VALID)){ 193f32f3638Srsc bp = bread(ip->dev, IBLOCK(ip->inum)); 194f32f3638Srsc dip = &((struct dinode*)(bp->data))[ip->inum % IPB]; 195bcca6c6bSrsc ip->type = dip->type; 196bcca6c6bSrsc ip->major = dip->major; 197bcca6c6bSrsc ip->minor = dip->minor; 198bcca6c6bSrsc ip->nlink = dip->nlink; 199bcca6c6bSrsc ip->size = dip->size; 200bcca6c6bSrsc memmove(ip->addrs, dip->addrs, sizeof(ip->addrs)); 20111a9947fSrtm brelse(bp); 202f32f3638Srsc ip->flags |= I_VALID; 203eaea18cbSrsc if(ip->type == 0) 204eaea18cbSrsc panic("ilock: no type"); 20511a9947fSrtm } 206bcca6c6bSrsc } 207bcca6c6bSrsc 208bcca6c6bSrsc // Unlock the given inode. 209*07090dd7Srsc void 210bcca6c6bSrsc iunlock(struct inode *ip) 211bcca6c6bSrsc { 212*07090dd7Srsc if(ip == 0 || !(ip->flags & I_BUSY) || ip->ref < 1) 213bcca6c6bSrsc panic("iunlock"); 214bcca6c6bSrsc 215bcca6c6bSrsc acquire(&icache.lock); 216f32f3638Srsc ip->flags &= ~I_BUSY; 217bcca6c6bSrsc wakeup(ip); 218bcca6c6bSrsc release(&icache.lock); 219bcca6c6bSrsc } 220bcca6c6bSrsc 221f32f3638Srsc // Caller holds reference to unlocked ip. Drop reference. 222bcca6c6bSrsc void 223*07090dd7Srsc iput(struct inode *ip) 224bcca6c6bSrsc { 225f32f3638Srsc acquire(&icache.lock); 226f32f3638Srsc if(ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0) { 227f32f3638Srsc // inode is no longer used: truncate and free inode. 228f32f3638Srsc if(ip->flags & I_BUSY) 229eaea18cbSrsc panic("iput busy"); 230f32f3638Srsc ip->flags |= I_BUSY; 231f32f3638Srsc release(&icache.lock); 232f32f3638Srsc // XXX convince rsc that no one will come find this inode. 233f32f3638Srsc itrunc(ip); 234f32f3638Srsc ip->type = 0; 235f32f3638Srsc iupdate(ip); 236f32f3638Srsc acquire(&icache.lock); 237f32f3638Srsc ip->flags &= ~I_BUSY; 238f32f3638Srsc } 239f32f3638Srsc ip->ref--; 240f32f3638Srsc release(&icache.lock); 241bcca6c6bSrsc } 242bcca6c6bSrsc 243*07090dd7Srsc void 244*07090dd7Srsc iunlockput(struct inode *ip) 245*07090dd7Srsc { 246*07090dd7Srsc iunlock(ip); 247*07090dd7Srsc iput(ip); 248*07090dd7Srsc } 249*07090dd7Srsc 250bcca6c6bSrsc // Allocate a new inode with the given type on device dev. 251*07090dd7Srsc struct inode* 252e8d11c2eSkaashoek ialloc(uint dev, short type) 253e8d11c2eSkaashoek { 254f32f3638Srsc int inum, ninodes; 255f32f3638Srsc struct buf *bp; 2567d4aef6cSrsc struct dinode *dip; 257e8d11c2eSkaashoek struct superblock *sb; 258e8d11c2eSkaashoek 259e8d11c2eSkaashoek bp = bread(dev, 1); 26024111398Skaashoek sb = (struct superblock*)bp->data; 261e8d11c2eSkaashoek ninodes = sb->ninodes; 262e8d11c2eSkaashoek brelse(bp); 263e8d11c2eSkaashoek 264e8d11c2eSkaashoek for(inum = 1; inum < ninodes; inum++) { // loop over inode blocks 26524111398Skaashoek bp = bread(dev, IBLOCK(inum)); 266e8d11c2eSkaashoek dip = &((struct dinode*)(bp->data))[inum % IPB]; 267e8d11c2eSkaashoek if(dip->type == 0) { // a free inode 2682aa4c3bcSrtm memset(dip, 0, sizeof(*dip)); 269e8d11c2eSkaashoek dip->type = type; 270eaea18cbSrsc bwrite(bp); // mark it allocated on the disk 271e8d11c2eSkaashoek brelse(bp); 272f32f3638Srsc return iget(dev, inum); 273e8d11c2eSkaashoek } 27495c07f82Srsc brelse(bp); 27595c07f82Srsc } 27695c07f82Srsc panic("ialloc: no inodes"); 27795c07f82Srsc } 278e8d11c2eSkaashoek 279bcca6c6bSrsc // Copy inode, which has changed, from memory to disk. 280eaea18cbSrsc void 281bcca6c6bSrsc iupdate(struct inode *ip) 282bcca6c6bSrsc { 283bcca6c6bSrsc struct buf *bp; 284bcca6c6bSrsc struct dinode *dip; 285bcca6c6bSrsc 286bcca6c6bSrsc bp = bread(ip->dev, IBLOCK(ip->inum)); 287bcca6c6bSrsc dip = &((struct dinode*)(bp->data))[ip->inum % IPB]; 288bcca6c6bSrsc dip->type = ip->type; 289bcca6c6bSrsc dip->major = ip->major; 290bcca6c6bSrsc dip->minor = ip->minor; 291bcca6c6bSrsc dip->nlink = ip->nlink; 292bcca6c6bSrsc dip->size = ip->size; 293bcca6c6bSrsc memmove(dip->addrs, ip->addrs, sizeof(ip->addrs)); 294eaea18cbSrsc bwrite(bp); 295bcca6c6bSrsc brelse(bp); 296bcca6c6bSrsc } 297bcca6c6bSrsc 298bcca6c6bSrsc // Inode contents 299bcca6c6bSrsc // 300bcca6c6bSrsc // The contents (data) associated with each inode is stored 301bcca6c6bSrsc // in a sequence of blocks on the disk. The first NDIRECT blocks 302bcca6c6bSrsc // are stored in ip->addrs[]. The next NINDIRECT blocks are 303bcca6c6bSrsc // listed in the block ip->addrs[INDIRECT]. 3049d3fb671Srtm 305bb207a1dSrsc // Return the disk block address of the nth block in inode ip. 306eaea18cbSrsc // If there is no such block, alloc controls whether one is allocated. 307eaea18cbSrsc static uint 308bcca6c6bSrsc bmap(struct inode *ip, uint bn, int alloc) 30922bac2cbSkaashoek { 310bcca6c6bSrsc uint addr, *a; 311bcca6c6bSrsc struct buf *bp; 31222bac2cbSkaashoek 313ea2909b6Skaashoek if(bn < NDIRECT) { 314bcca6c6bSrsc if((addr = ip->addrs[bn]) == 0) { 315bcca6c6bSrsc if(!alloc) 316bcca6c6bSrsc return -1; 317bcca6c6bSrsc ip->addrs[bn] = addr = balloc(ip->dev); 318ea2909b6Skaashoek } 319bcca6c6bSrsc return addr; 320bcca6c6bSrsc } 321bcca6c6bSrsc bn -= NDIRECT; 322bcca6c6bSrsc 323bcca6c6bSrsc if(bn < NINDIRECT) { 324bcca6c6bSrsc // Load indirect block, allocating if necessary. 325bcca6c6bSrsc if((addr = ip->addrs[INDIRECT]) == 0) { 326bcca6c6bSrsc if(!alloc) 327bcca6c6bSrsc return -1; 328bcca6c6bSrsc ip->addrs[INDIRECT] = addr = balloc(ip->dev); 329bcca6c6bSrsc } 330bcca6c6bSrsc bp = bread(ip->dev, addr); 331bcca6c6bSrsc a = (uint*)bp->data; 332bcca6c6bSrsc 333bcca6c6bSrsc if((addr = a[bn]) == 0) { 334bcca6c6bSrsc if(!alloc) { 335bcca6c6bSrsc brelse(bp); 336bcca6c6bSrsc return -1; 337bcca6c6bSrsc } 338bcca6c6bSrsc a[bn] = addr = balloc(ip->dev); 339eaea18cbSrsc bwrite(bp); 340bcca6c6bSrsc } 341bcca6c6bSrsc brelse(bp); 342bcca6c6bSrsc return addr; 34322bac2cbSkaashoek } 34422bac2cbSkaashoek 345bcca6c6bSrsc panic("bmap: out of range"); 346bcca6c6bSrsc } 347bcca6c6bSrsc 348eaea18cbSrsc // PAGEBREAK: 30 349bcca6c6bSrsc // Truncate inode (discard contents). 350fbf91039Srsc static void 3512aa4c3bcSrtm itrunc(struct inode *ip) 35222bac2cbSkaashoek { 353ea2909b6Skaashoek int i, j; 354bcca6c6bSrsc struct buf *bp; 3557d4aef6cSrsc uint *a; 35622bac2cbSkaashoek 357bcca6c6bSrsc for(i = 0; i < NDIRECT; i++) { 358bcca6c6bSrsc if(ip->addrs[i]) { 35922bac2cbSkaashoek bfree(ip->dev, ip->addrs[i]); 36022bac2cbSkaashoek ip->addrs[i] = 0; 36122bac2cbSkaashoek } 36222bac2cbSkaashoek } 363bcca6c6bSrsc 364bcca6c6bSrsc if(ip->addrs[INDIRECT]) { 365bcca6c6bSrsc bp = bread(ip->dev, ip->addrs[INDIRECT]); 366bcca6c6bSrsc a = (uint*)bp->data; 367bcca6c6bSrsc for(j = 0; j < NINDIRECT; j++) { 368bcca6c6bSrsc if(a[j]) 369bcca6c6bSrsc bfree(ip->dev, a[j]); 370bcca6c6bSrsc } 371bcca6c6bSrsc brelse(bp); 372bcca6c6bSrsc ip->addrs[INDIRECT] = 0; 373bcca6c6bSrsc } 374bcca6c6bSrsc 37522bac2cbSkaashoek ip->size = 0; 37622bac2cbSkaashoek iupdate(ip); 37722bac2cbSkaashoek } 37822bac2cbSkaashoek 379bb207a1dSrsc // Copy stat information from inode. 380e958c538Skaashoek void 3811f544842Skaashoek stati(struct inode *ip, struct stat *st) 3821f544842Skaashoek { 3831dca3afbSrsc st->dev = ip->dev; 3841dca3afbSrsc st->ino = ip->inum; 3851dca3afbSrsc st->type = ip->type; 3861dca3afbSrsc st->nlink = ip->nlink; 3871dca3afbSrsc st->size = ip->size; 3881f544842Skaashoek } 3891f544842Skaashoek 390eaea18cbSrsc //PAGEBREAK! 391bb207a1dSrsc // Read data from inode. 392c59361f1Srtm int 39317a85657Srtm readi(struct inode *ip, char *dst, uint off, uint n) 394c59361f1Srtm { 395bcca6c6bSrsc uint tot, m; 396c59361f1Srtm struct buf *bp; 397c59361f1Srtm 398939f9edeSkaashoek if(ip->type == T_DEV) { 3991dca3afbSrsc if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read) 400939f9edeSkaashoek return -1; 4011dca3afbSrsc return devsw[ip->major].read(ip->minor, dst, n); 402939f9edeSkaashoek } 403939f9edeSkaashoek 404bcca6c6bSrsc if(off + n < off) 405bcca6c6bSrsc return -1; 406bcca6c6bSrsc if(off + n > ip->size) 407bcca6c6bSrsc n = ip->size - off; 408bcca6c6bSrsc 409bcca6c6bSrsc for(tot=0; tot<n; tot+=m, off+=m, dst+=m) { 410bcca6c6bSrsc bp = bread(ip->dev, bmap(ip, off/BSIZE, 0)); 411bcca6c6bSrsc m = min(n - tot, BSIZE - off%BSIZE); 412bcca6c6bSrsc memmove(dst, bp->data + off%BSIZE, m); 413c59361f1Srtm brelse(bp); 414c59361f1Srtm } 415bcca6c6bSrsc return n; 416ea2909b6Skaashoek } 417ea2909b6Skaashoek 418eaea18cbSrsc // PAGEBREAK! 419bb207a1dSrsc // Write data to inode. 420ea2909b6Skaashoek int 421bcca6c6bSrsc writei(struct inode *ip, char *src, uint off, uint n) 4226fa5ffb5Skaashoek { 423bcca6c6bSrsc uint tot, m; 4247d4aef6cSrsc struct buf *bp; 4257d4aef6cSrsc 4266fa5ffb5Skaashoek if(ip->type == T_DEV) { 4271dca3afbSrsc if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write) 428939f9edeSkaashoek return -1; 429bcca6c6bSrsc return devsw[ip->major].write(ip->minor, src, n); 4307d4aef6cSrsc } 4317d4aef6cSrsc 432bcca6c6bSrsc if(off + n < off) 433bcca6c6bSrsc return -1; 434bcca6c6bSrsc if(off + n > MAXFILE*BSIZE) 435bcca6c6bSrsc n = MAXFILE*BSIZE - off; 436bcca6c6bSrsc 437bcca6c6bSrsc for(tot=0; tot<n; tot+=m, off+=m, src+=m) { 438bcca6c6bSrsc bp = bread(ip->dev, bmap(ip, off/BSIZE, 1)); 439bcca6c6bSrsc m = min(n - tot, BSIZE - off%BSIZE); 440bcca6c6bSrsc memmove(bp->data + off%BSIZE, src, m); 441eaea18cbSrsc bwrite(bp); 44228d9ef04Skaashoek brelse(bp); 44328d9ef04Skaashoek } 444bcca6c6bSrsc 445bcca6c6bSrsc if(n > 0 && off > ip->size) { 44648b82470Srsc ip->size = off; 44728d9ef04Skaashoek iupdate(ip); 44828d9ef04Skaashoek } 449bcca6c6bSrsc return n; 4506fa5ffb5Skaashoek } 4516fa5ffb5Skaashoek 452eaea18cbSrsc //PAGEBREAK! 453bcca6c6bSrsc // Directories 454bcca6c6bSrsc 455eaea18cbSrsc int 456fbf91039Srsc namecmp(const char *s, const char *t) 457fbf91039Srsc { 458fbf91039Srsc int i; 459fbf91039Srsc 460fbf91039Srsc for(i=0; i<DIRSIZ; i++){ 461fbf91039Srsc if(s[i] != t[i]) 462fbf91039Srsc return s[i] - t[i]; 463fbf91039Srsc if(s[i] == 0) 464fbf91039Srsc break; 465fbf91039Srsc } 466fbf91039Srsc return 0; 467fbf91039Srsc } 468fbf91039Srsc 469bcca6c6bSrsc // Look for a directory entry in a directory. 470eaea18cbSrsc // If found, set *poff to byte offset of entry. 47120365348Srtm // Caller must have already locked dp. 472*07090dd7Srsc struct inode* 473fbf91039Srsc dirlookup(struct inode *dp, char *name, uint *poff) 474bcca6c6bSrsc { 475f32f3638Srsc uint off, inum; 476bcca6c6bSrsc struct buf *bp; 477bcca6c6bSrsc struct dirent *de; 478bcca6c6bSrsc 479bcca6c6bSrsc if(dp->type != T_DIR) 48020365348Srtm panic("dirlookup not DIR"); 481bcca6c6bSrsc 482bcca6c6bSrsc for(off = 0; off < dp->size; off += BSIZE){ 483bcca6c6bSrsc bp = bread(dp->dev, bmap(dp, off / BSIZE, 0)); 484bcca6c6bSrsc for(de = (struct dirent*) bp->data; 485bcca6c6bSrsc de < (struct dirent*) (bp->data + BSIZE); 486bcca6c6bSrsc de++){ 487bcca6c6bSrsc if(de->inum == 0) 488bcca6c6bSrsc continue; 489fbf91039Srsc if(namecmp(name, de->name) == 0){ 490bcca6c6bSrsc // entry matches path element 491e2a620daSrsc if(poff) 492bcca6c6bSrsc *poff = off + (uchar*)de - bp->data; 493f32f3638Srsc inum = de->inum; 494bcca6c6bSrsc brelse(bp); 495f32f3638Srsc return iget(dp->dev, inum); 496f32f3638Srsc } 497f32f3638Srsc } 498f32f3638Srsc brelse(bp); 499f32f3638Srsc } 500bcca6c6bSrsc return 0; 501bcca6c6bSrsc } 502bcca6c6bSrsc 503eaea18cbSrsc // Copy one name to another. 504eaea18cbSrsc static void 505eaea18cbSrsc namecpy(char *s, const char *t) 506eaea18cbSrsc { 507eaea18cbSrsc int i; 508eaea18cbSrsc 509eaea18cbSrsc for(i=0; i<DIRSIZ && t[i]; i++) 510eaea18cbSrsc s[i] = t[i]; 511eaea18cbSrsc for(; i<DIRSIZ; i++) 512eaea18cbSrsc s[i] = 0; 513eaea18cbSrsc } 514eaea18cbSrsc 515bcca6c6bSrsc // Write a new directory entry (name, ino) into the directory dp. 516eaea18cbSrsc int 517fbf91039Srsc dirlink(struct inode *dp, char *name, uint ino) 518bcca6c6bSrsc { 519e2a620daSrsc int off; 520bcca6c6bSrsc struct dirent de; 521*07090dd7Srsc struct inode *ip; 522f32f3638Srsc 523eaea18cbSrsc // Check that name is not present. 524*07090dd7Srsc if((ip = dirlookup(dp, name, 0)) != 0){ 525*07090dd7Srsc iput(ip); 526f32f3638Srsc return -1; 527f32f3638Srsc } 528bcca6c6bSrsc 529bcca6c6bSrsc // Look for an empty dirent. 530bcca6c6bSrsc for(off = 0; off < dp->size; off += sizeof(de)){ 531bcca6c6bSrsc if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 532bcca6c6bSrsc panic("dirwrite read"); 533bcca6c6bSrsc if(de.inum == 0) 534bcca6c6bSrsc break; 535bcca6c6bSrsc } 536bcca6c6bSrsc 537fbf91039Srsc namecpy(de.name, name); 538bcca6c6bSrsc de.inum = ino; 539bcca6c6bSrsc if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 540bcca6c6bSrsc panic("dirwrite"); 541f32f3638Srsc 542f32f3638Srsc return 0; 543bcca6c6bSrsc } 544bcca6c6bSrsc 545bcca6c6bSrsc // Paths 546bcca6c6bSrsc 547eaea18cbSrsc // Copy the next path element from path into name. 548eaea18cbSrsc // Return a pointer to the element following the copied one. 549eaea18cbSrsc // The returned path has no leading slashes, 550eaea18cbSrsc // so the caller can check *path=='\0' to see if the name is the last one. 551eaea18cbSrsc // If no name to remove, return 0. 552ab5c2dbbSrsc // 553ab5c2dbbSrsc // Examples: 554eaea18cbSrsc // skipelem("a/bb/c", name) = "bb/c", setting name = "a" 55520365348Srtm // skipelem("///a/bb", name) = "bb", setting name="a" 556eaea18cbSrsc // skipelem("", name) = skipelem("////", name) = 0 557ab5c2dbbSrsc // 558ab5c2dbbSrsc static char* 559fbf91039Srsc skipelem(char *path, char *name) 560ab5c2dbbSrsc { 561fbf91039Srsc char *s; 562fbf91039Srsc int len; 563fbf91039Srsc 564ab5c2dbbSrsc while(*path == '/') 565ab5c2dbbSrsc path++; 566ab5c2dbbSrsc if(*path == 0) 567ab5c2dbbSrsc return 0; 568fbf91039Srsc s = path; 569ab5c2dbbSrsc while(*path != '/' && *path != 0) 570ab5c2dbbSrsc path++; 571fbf91039Srsc len = path - s; 572fbf91039Srsc if(len >= DIRSIZ) 573fbf91039Srsc memmove(name, s, DIRSIZ); 574fbf91039Srsc else{ 575fbf91039Srsc memmove(name, s, len); 576fbf91039Srsc name[len] = 0; 577fbf91039Srsc } 578ab5c2dbbSrsc while(*path == '/') 579ab5c2dbbSrsc path++; 580ab5c2dbbSrsc return path; 581ab5c2dbbSrsc } 582ab5c2dbbSrsc 583eaea18cbSrsc // Look up and return the inode for a path name. 584eaea18cbSrsc // If parent is set, return the inode for the parent 585eaea18cbSrsc // and write the final path element to name, which 586eaea18cbSrsc // should have room for DIRSIZ bytes. 587*07090dd7Srsc static struct inode* 588fbf91039Srsc _namei(char *path, int parent, char *name) 5899d3fb671Srtm { 590*07090dd7Srsc struct inode *ip, *next; 591f32f3638Srsc uint off; 5929d3fb671Srtm 593ab5c2dbbSrsc if(*path == '/') 594*07090dd7Srsc ip = iget(ROOTDEV, 1); 595f32f3638Srsc else 596*07090dd7Srsc ip = idup(cp->cwd); 5979d3fb671Srtm 598fbf91039Srsc while((path = skipelem(path, name)) != 0){ 599*07090dd7Srsc ilock(ip); 600*07090dd7Srsc if(ip->type != T_DIR){ 601*07090dd7Srsc iunlockput(ip); 602eaea18cbSrsc return 0; 603eaea18cbSrsc } 604ab5c2dbbSrsc 605e2a620daSrsc if(parent && *path == '\0'){ 606e2a620daSrsc // Stop one level early. 607*07090dd7Srsc iunlock(ip); 608*07090dd7Srsc return ip; 609ab5c2dbbSrsc } 610ab5c2dbbSrsc 611*07090dd7Srsc if((next = dirlookup(ip, name, &off)) == 0){ 612*07090dd7Srsc iunlockput(ip); 613eaea18cbSrsc return 0; 614eaea18cbSrsc } 615*07090dd7Srsc iunlockput(ip); 616*07090dd7Srsc ip = next; 617ab5c2dbbSrsc } 61820365348Srtm if(parent){ 619*07090dd7Srsc iput(ip); 6205051da6dSrtm return 0; 62120365348Srtm } 622*07090dd7Srsc return ip; 6230633b971Skaashoek } 6249d3fb671Srtm 625*07090dd7Srsc struct inode* 626e2a620daSrsc namei(char *path) 627e2a620daSrsc { 628fbf91039Srsc char name[DIRSIZ]; 629fbf91039Srsc return _namei(path, 0, name); 630e2a620daSrsc } 631e2a620daSrsc 632*07090dd7Srsc struct inode* 633fbf91039Srsc nameiparent(char *path, char *name) 634e2a620daSrsc { 635fbf91039Srsc return _namei(path, 1, name); 636e2a620daSrsc } 637