xref: /xv6-public/fs.c (revision f9a06440)
1a505fd66Srsc // File system implementation.  Four layers:
2bcca6c6bSrsc //   + Blocks: allocator for raw disk blocks.
3bcca6c6bSrsc //   + Files: inode allocator, reading, writing, metadata.
4bcca6c6bSrsc //   + Directories: inode with special contents (list of other inodes!)
5bcca6c6bSrsc //   + Names: paths like /usr/rtm/xv6/fs.c for convenient naming.
6bcca6c6bSrsc //
74d39b633Srsc // Disk layout is: superblock, inodes, block in-use bitmap, data blocks.
8eaea18cbSrsc //
9eaea18cbSrsc // This file contains the low-level file system manipulation
10eaea18cbSrsc // routines.  The (higher-level) system call implementations
11eaea18cbSrsc // are in sysfile.c.
12bcca6c6bSrsc 
1311a9947fSrtm #include "types.h"
14558ab49fSrsc #include "defs.h"
1511a9947fSrtm #include "param.h"
16558ab49fSrsc #include "stat.h"
1711a9947fSrtm #include "mmu.h"
1811a9947fSrtm #include "proc.h"
1911a9947fSrtm #include "spinlock.h"
2011a9947fSrtm #include "buf.h"
2111a9947fSrtm #include "fs.h"
2211a9947fSrtm #include "fsvar.h"
236fa5ffb5Skaashoek #include "dev.h"
2411a9947fSrtm 
25bcca6c6bSrsc #define min(a, b) ((a) < (b) ? (a) : (b))
26fbf91039Srsc static void itrunc(struct inode*);
2711a9947fSrtm 
28a505fd66Srsc // Read the super block.
29a505fd66Srsc static void
30a505fd66Srsc readsb(int dev, struct superblock *sb)
31a505fd66Srsc {
32a505fd66Srsc   struct buf *bp;
33a505fd66Srsc 
34a505fd66Srsc   bp = bread(dev, 1);
35a505fd66Srsc   memmove(sb, bp->data, sizeof(*sb));
36a505fd66Srsc   brelse(bp);
37a505fd66Srsc }
38a505fd66Srsc 
39a505fd66Srsc // Zero a block.
40a505fd66Srsc static void
41a505fd66Srsc bzero(int dev, int bno)
42a505fd66Srsc {
43a505fd66Srsc   struct buf *bp;
44a505fd66Srsc 
45a505fd66Srsc   bp = bread(dev, bno);
46a505fd66Srsc   memset(bp->data, 0, BSIZE);
47a505fd66Srsc   bwrite(bp);
48a505fd66Srsc   brelse(bp);
49a505fd66Srsc }
50a505fd66Srsc 
51bcca6c6bSrsc // Blocks.
525be0039cSrtm 
53f5527388Srsc // Allocate a disk block.
5424111398Skaashoek static uint
5524111398Skaashoek balloc(uint dev)
5624111398Skaashoek {
57a505fd66Srsc   int b, bi, m;
5824111398Skaashoek   struct buf *bp;
59a505fd66Srsc   struct superblock sb;
6024111398Skaashoek 
61a505fd66Srsc   bp = 0;
62a505fd66Srsc   readsb(dev, &sb);
63a505fd66Srsc   for(b = 0; b < sb.size; b += BPB){
64a505fd66Srsc     bp = bread(dev, BBLOCK(b, sb.ninodes));
65a505fd66Srsc     for(bi = 0; bi < BPB; bi++){
66a505fd66Srsc       m = 1 << (bi % 8);
67a505fd66Srsc       if((bp->data[bi/8] & m) == 0){  // Is block free?
68a505fd66Srsc         bp->data[bi/8] |= m;  // Mark block in use on disk.
69a505fd66Srsc         bwrite(bp);
7024111398Skaashoek         brelse(bp);
71a505fd66Srsc         return b + bi;
7224111398Skaashoek       }
73a505fd66Srsc     }
7428d9ef04Skaashoek     brelse(bp);
757d4aef6cSrsc   }
767d4aef6cSrsc   panic("balloc: out of blocks");
777d4aef6cSrsc }
7824111398Skaashoek 
79bb207a1dSrsc // Free a disk block.
8028d9ef04Skaashoek static void
8128d9ef04Skaashoek bfree(int dev, uint b)
8228d9ef04Skaashoek {
8328d9ef04Skaashoek   struct buf *bp;
84a505fd66Srsc   struct superblock sb;
85a505fd66Srsc   int bi, m;
8628d9ef04Skaashoek 
87a505fd66Srsc   bzero(dev, b);
8828d9ef04Skaashoek 
89a505fd66Srsc   readsb(dev, &sb);
90a505fd66Srsc   bp = bread(dev, BBLOCK(b, sb.ninodes));
9128d9ef04Skaashoek   bi = b % BPB;
92a505fd66Srsc   m = 1 << (bi % 8);
93902b13f5Srtm   if((bp->data[bi/8] & m) == 0)
94902b13f5Srtm     panic("freeing free block");
95a505fd66Srsc   bp->data[bi/8] &= ~m;  // Mark block free on disk.
96a505fd66Srsc   bwrite(bp);
9728d9ef04Skaashoek   brelse(bp);
9828d9ef04Skaashoek }
9924111398Skaashoek 
1006c34f97cSrsc // Inodes.
1016c34f97cSrsc //
1026c34f97cSrsc // An inode is a single, unnamed file in the file system.
1036c34f97cSrsc // The inode disk structure holds metadata (the type, device numbers,
1046c34f97cSrsc // and data size) along with a list of blocks where the associated
1056c34f97cSrsc // data can be found.
106bcca6c6bSrsc //
107bcca6c6bSrsc // The inodes are laid out sequentially on disk immediately after
108bcca6c6bSrsc // the superblock.  The kernel keeps a cache of the in-use
109bcca6c6bSrsc // on-disk structures to provide a place for synchronizing access
110bcca6c6bSrsc // to inodes shared between multiple processes.
111bcca6c6bSrsc //
112902b13f5Srtm // ip->ref counts the number of pointer references to this cached
113bcca6c6bSrsc // inode; references are typically kept in struct file and in cp->cwd.
114bcca6c6bSrsc // When ip->ref falls to zero, the inode is no longer cached.
115bcca6c6bSrsc // It is an error to use an inode without holding a reference to it.
116bcca6c6bSrsc //
117eaea18cbSrsc // Processes are only allowed to read and write inode
1186c34f97cSrsc // metadata and contents when holding the inode's lock,
1196c34f97cSrsc // represented by the I_BUSY flag in the in-memory copy.
120902b13f5Srtm // Because inode locks are held during disk accesses,
121902b13f5Srtm // they are implemented using a flag rather than with
122902b13f5Srtm // spin locks.  Callers are responsible for locking
123eaea18cbSrsc // inodes before passing them to routines in this file; leaving
124eaea18cbSrsc // this responsibility with the caller makes it possible for them
125eaea18cbSrsc // to create arbitrarily-sized atomic operations.
126eaea18cbSrsc //
127eaea18cbSrsc // To give maximum control over locking to the callers,
128eaea18cbSrsc // the routines in this file that return inode pointers
129eaea18cbSrsc // return pointers to *unlocked* inodes.  It is the callers'
130902b13f5Srtm // responsibility to lock them before using them.  A non-zero
131902b13f5Srtm // ip->ref keeps these unlocked inodes in the cache.
132bcca6c6bSrsc 
133bcca6c6bSrsc struct {
134bcca6c6bSrsc   struct spinlock lock;
135bcca6c6bSrsc   struct inode inode[NINODE];
136bcca6c6bSrsc } icache;
137bcca6c6bSrsc 
138bcca6c6bSrsc void
139bcca6c6bSrsc iinit(void)
140bcca6c6bSrsc {
14134295f46Srsc   initlock(&icache.lock, "icache");
142bcca6c6bSrsc }
143bcca6c6bSrsc 
144*f9a06440SRuss Cox static struct inode* iget(uint dev, uint inum);
145*f9a06440SRuss Cox 
146*f9a06440SRuss Cox //PAGEBREAK!
147*f9a06440SRuss Cox // Allocate a new inode with the given type on device dev.
148*f9a06440SRuss Cox struct inode*
149*f9a06440SRuss Cox ialloc(uint dev, short type)
150*f9a06440SRuss Cox {
151*f9a06440SRuss Cox   int inum;
152*f9a06440SRuss Cox   struct buf *bp;
153*f9a06440SRuss Cox   struct dinode *dip;
154*f9a06440SRuss Cox   struct superblock sb;
155*f9a06440SRuss Cox 
156*f9a06440SRuss Cox   readsb(dev, &sb);
157*f9a06440SRuss Cox   for(inum = 1; inum < sb.ninodes; inum++){  // loop over inode blocks
158*f9a06440SRuss Cox     bp = bread(dev, IBLOCK(inum));
159*f9a06440SRuss Cox     dip = (struct dinode*)bp->data + inum%IPB;
160*f9a06440SRuss Cox     if(dip->type == 0){  // a free inode
161*f9a06440SRuss Cox       memset(dip, 0, sizeof(*dip));
162*f9a06440SRuss Cox       dip->type = type;
163*f9a06440SRuss Cox       bwrite(bp);   // mark it allocated on the disk
164*f9a06440SRuss Cox       brelse(bp);
165*f9a06440SRuss Cox       return iget(dev, inum);
166*f9a06440SRuss Cox     }
167*f9a06440SRuss Cox     brelse(bp);
168*f9a06440SRuss Cox   }
169*f9a06440SRuss Cox   panic("ialloc: no inodes");
170*f9a06440SRuss Cox }
171*f9a06440SRuss Cox 
172*f9a06440SRuss Cox // Copy inode, which has changed, from memory to disk.
173*f9a06440SRuss Cox void
174*f9a06440SRuss Cox iupdate(struct inode *ip)
175*f9a06440SRuss Cox {
176*f9a06440SRuss Cox   struct buf *bp;
177*f9a06440SRuss Cox   struct dinode *dip;
178*f9a06440SRuss Cox 
179*f9a06440SRuss Cox   bp = bread(ip->dev, IBLOCK(ip->inum));
180*f9a06440SRuss Cox   dip = (struct dinode*)bp->data + ip->inum%IPB;
181*f9a06440SRuss Cox   dip->type = ip->type;
182*f9a06440SRuss Cox   dip->major = ip->major;
183*f9a06440SRuss Cox   dip->minor = ip->minor;
184*f9a06440SRuss Cox   dip->nlink = ip->nlink;
185*f9a06440SRuss Cox   dip->size = ip->size;
186*f9a06440SRuss Cox   memmove(dip->addrs, ip->addrs, sizeof(ip->addrs));
187*f9a06440SRuss Cox   bwrite(bp);
188*f9a06440SRuss Cox   brelse(bp);
189*f9a06440SRuss Cox }
190*f9a06440SRuss Cox 
191f5527388Srsc // Find the inode with number inum on device dev
1927895178dSrsc // and return the in-memory copy.
19307090dd7Srsc static struct inode*
19411a9947fSrtm iget(uint dev, uint inum)
19511a9947fSrtm {
196bcca6c6bSrsc   struct inode *ip, *empty;
19711a9947fSrtm 
198bcca6c6bSrsc   acquire(&icache.lock);
19911a9947fSrtm 
200bcca6c6bSrsc   // Try for cached inode.
201bcca6c6bSrsc   empty = 0;
202bcca6c6bSrsc   for(ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++){
2030d6bbd31Srsc     if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){
2040d6bbd31Srsc       ip->ref++;
205bcca6c6bSrsc       release(&icache.lock);
20607090dd7Srsc       return ip;
20711a9947fSrtm     }
208bcca6c6bSrsc     if(empty == 0 && ip->ref == 0)    // Remember empty slot.
209bcca6c6bSrsc       empty = ip;
21011a9947fSrtm   }
21111a9947fSrtm 
212bcca6c6bSrsc   // Allocate fresh inode.
213bcca6c6bSrsc   if(empty == 0)
21432eea766Srsc     panic("iget: no inodes");
21511a9947fSrtm 
216bcca6c6bSrsc   ip = empty;
217bcca6c6bSrsc   ip->dev = dev;
218bcca6c6bSrsc   ip->inum = inum;
219bcca6c6bSrsc   ip->ref = 1;
220f32f3638Srsc   ip->flags = 0;
221bcca6c6bSrsc   release(&icache.lock);
22211a9947fSrtm 
22307090dd7Srsc   return ip;
224f32f3638Srsc }
225f32f3638Srsc 
226eaea18cbSrsc // Increment reference count for ip.
227eaea18cbSrsc // Returns ip to enable ip = idup(ip1) idiom.
22807090dd7Srsc struct inode*
22907090dd7Srsc idup(struct inode *ip)
230f32f3638Srsc {
231eaea18cbSrsc   acquire(&icache.lock);
232eaea18cbSrsc   ip->ref++;
233eaea18cbSrsc   release(&icache.lock);
23407090dd7Srsc   return ip;
235f32f3638Srsc }
236f32f3638Srsc 
237f32f3638Srsc // Lock the given inode.
23807090dd7Srsc void
23907090dd7Srsc ilock(struct inode *ip)
240f32f3638Srsc {
241f32f3638Srsc   struct buf *bp;
242f32f3638Srsc   struct dinode *dip;
243eaea18cbSrsc 
24407090dd7Srsc   if(ip == 0 || ip->ref < 1)
24507090dd7Srsc     panic("ilock");
246f32f3638Srsc 
247f32f3638Srsc   acquire(&icache.lock);
248f32f3638Srsc   while(ip->flags & I_BUSY)
249f32f3638Srsc     sleep(ip, &icache.lock);
250f32f3638Srsc   ip->flags |= I_BUSY;
251f32f3638Srsc   release(&icache.lock);
252f32f3638Srsc 
253f32f3638Srsc   if(!(ip->flags & I_VALID)){
254f32f3638Srsc     bp = bread(ip->dev, IBLOCK(ip->inum));
2553341e30fSrsc     dip = (struct dinode*)bp->data + ip->inum%IPB;
256bcca6c6bSrsc     ip->type = dip->type;
257bcca6c6bSrsc     ip->major = dip->major;
258bcca6c6bSrsc     ip->minor = dip->minor;
259bcca6c6bSrsc     ip->nlink = dip->nlink;
260bcca6c6bSrsc     ip->size = dip->size;
261bcca6c6bSrsc     memmove(ip->addrs, dip->addrs, sizeof(ip->addrs));
26211a9947fSrtm     brelse(bp);
263f32f3638Srsc     ip->flags |= I_VALID;
264eaea18cbSrsc     if(ip->type == 0)
265eaea18cbSrsc       panic("ilock: no type");
26611a9947fSrtm   }
267bcca6c6bSrsc }
268bcca6c6bSrsc 
269bcca6c6bSrsc // Unlock the given inode.
27007090dd7Srsc void
271bcca6c6bSrsc iunlock(struct inode *ip)
272bcca6c6bSrsc {
27307090dd7Srsc   if(ip == 0 || !(ip->flags & I_BUSY) || ip->ref < 1)
274bcca6c6bSrsc     panic("iunlock");
275bcca6c6bSrsc 
276bcca6c6bSrsc   acquire(&icache.lock);
277ce72cadbSrsc   ip->flags &= ~I_BUSY;
278bcca6c6bSrsc   wakeup(ip);
279bcca6c6bSrsc   release(&icache.lock);
280bcca6c6bSrsc }
281bcca6c6bSrsc 
282f32f3638Srsc // Caller holds reference to unlocked ip.  Drop reference.
283bcca6c6bSrsc void
28407090dd7Srsc iput(struct inode *ip)
285bcca6c6bSrsc {
286f32f3638Srsc   acquire(&icache.lock);
287f32f3638Srsc   if(ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0){
288f32f3638Srsc     // inode is no longer used: truncate and free inode.
289f32f3638Srsc     if(ip->flags & I_BUSY)
290eaea18cbSrsc       panic("iput busy");
291f32f3638Srsc     ip->flags |= I_BUSY;
292f32f3638Srsc     release(&icache.lock);
293f32f3638Srsc     itrunc(ip);
294f32f3638Srsc     ip->type = 0;
295f32f3638Srsc     iupdate(ip);
296f32f3638Srsc     acquire(&icache.lock);
297ce72cadbSrsc     ip->flags = 0;
2988970df0cSrsc     wakeup(ip);
299f32f3638Srsc   }
300f32f3638Srsc   ip->ref--;
301f32f3638Srsc   release(&icache.lock);
302bcca6c6bSrsc }
303bcca6c6bSrsc 
3047895178dSrsc // Common idiom: unlock, then put.
30507090dd7Srsc void
30607090dd7Srsc iunlockput(struct inode *ip)
30707090dd7Srsc {
30807090dd7Srsc   iunlock(ip);
30907090dd7Srsc   iput(ip);
31007090dd7Srsc }
31107090dd7Srsc 
3128d2e9a48Srsc //PAGEBREAK!
313bcca6c6bSrsc // Inode contents
314bcca6c6bSrsc //
315bcca6c6bSrsc // The contents (data) associated with each inode is stored
316bcca6c6bSrsc // in a sequence of blocks on the disk.  The first NDIRECT blocks
3177895178dSrsc // are listed in ip->addrs[].  The next NINDIRECT blocks are
318bcca6c6bSrsc // listed in the block ip->addrs[INDIRECT].
3199d3fb671Srtm 
320bb207a1dSrsc // Return the disk block address of the nth block in inode ip.
32113ae8808Srsc // If there is no such block, bmap allocates one.
322eaea18cbSrsc static uint
32313ae8808Srsc bmap(struct inode *ip, uint bn)
32422bac2cbSkaashoek {
325bcca6c6bSrsc   uint addr, *a;
326bcca6c6bSrsc   struct buf *bp;
32722bac2cbSkaashoek 
328ea2909b6Skaashoek   if(bn < NDIRECT){
32913ae8808Srsc     if((addr = ip->addrs[bn]) == 0)
330bcca6c6bSrsc       ip->addrs[bn] = addr = balloc(ip->dev);
331bcca6c6bSrsc     return addr;
332bcca6c6bSrsc   }
333bcca6c6bSrsc   bn -= NDIRECT;
334bcca6c6bSrsc 
335bcca6c6bSrsc   if(bn < NINDIRECT){
336bcca6c6bSrsc     // Load indirect block, allocating if necessary.
33713ae8808Srsc     if((addr = ip->addrs[NDIRECT]) == 0)
338ba6cd8a6Srsc       ip->addrs[NDIRECT] = addr = balloc(ip->dev);
339bcca6c6bSrsc     bp = bread(ip->dev, addr);
340bcca6c6bSrsc     a = (uint*)bp->data;
341bcca6c6bSrsc     if((addr = a[bn]) == 0){
342bcca6c6bSrsc       a[bn] = addr = balloc(ip->dev);
343eaea18cbSrsc       bwrite(bp);
344bcca6c6bSrsc     }
345bcca6c6bSrsc     brelse(bp);
346bcca6c6bSrsc     return addr;
34722bac2cbSkaashoek   }
34822bac2cbSkaashoek 
349bcca6c6bSrsc   panic("bmap: out of range");
350bcca6c6bSrsc }
351bcca6c6bSrsc 
352bcca6c6bSrsc // Truncate inode (discard contents).
3538eb20827Srtm // Only called after the last dirent referring
3548eb20827Srtm // to this inode has been erased on disk.
355fbf91039Srsc static void
3562aa4c3bcSrtm itrunc(struct inode *ip)
35722bac2cbSkaashoek {
358ea2909b6Skaashoek   int i, j;
359bcca6c6bSrsc   struct buf *bp;
3607d4aef6cSrsc   uint *a;
36122bac2cbSkaashoek 
362bcca6c6bSrsc   for(i = 0; i < NDIRECT; i++){
363bcca6c6bSrsc     if(ip->addrs[i]){
36422bac2cbSkaashoek       bfree(ip->dev, ip->addrs[i]);
36522bac2cbSkaashoek       ip->addrs[i] = 0;
36622bac2cbSkaashoek     }
36722bac2cbSkaashoek   }
368bcca6c6bSrsc 
369ba6cd8a6Srsc   if(ip->addrs[NDIRECT]){
370ba6cd8a6Srsc     bp = bread(ip->dev, ip->addrs[NDIRECT]);
371bcca6c6bSrsc     a = (uint*)bp->data;
372bcca6c6bSrsc     for(j = 0; j < NINDIRECT; j++){
373bcca6c6bSrsc       if(a[j])
374bcca6c6bSrsc         bfree(ip->dev, a[j]);
375bcca6c6bSrsc     }
376bcca6c6bSrsc     brelse(bp);
377ba6cd8a6Srsc     bfree(ip->dev, ip->addrs[NDIRECT]);
378ba6cd8a6Srsc     ip->addrs[NDIRECT] = 0;
379bcca6c6bSrsc   }
380bcca6c6bSrsc 
38122bac2cbSkaashoek   ip->size = 0;
38222bac2cbSkaashoek   iupdate(ip);
38322bac2cbSkaashoek }
38422bac2cbSkaashoek 
385bb207a1dSrsc // Copy stat information from inode.
386e958c538Skaashoek void
3871f544842Skaashoek stati(struct inode *ip, struct stat *st)
3881f544842Skaashoek {
3891dca3afbSrsc   st->dev = ip->dev;
3901dca3afbSrsc   st->ino = ip->inum;
3911dca3afbSrsc   st->type = ip->type;
3921dca3afbSrsc   st->nlink = ip->nlink;
3931dca3afbSrsc   st->size = ip->size;
3941f544842Skaashoek }
3951f544842Skaashoek 
396eaea18cbSrsc //PAGEBREAK!
397bb207a1dSrsc // Read data from inode.
398c59361f1Srtm int
39917a85657Srtm readi(struct inode *ip, char *dst, uint off, uint n)
400c59361f1Srtm {
401bcca6c6bSrsc   uint tot, m;
402c59361f1Srtm   struct buf *bp;
403c59361f1Srtm 
404939f9edeSkaashoek   if(ip->type == T_DEV){
4051dca3afbSrsc     if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read)
406939f9edeSkaashoek       return -1;
407d844f0f9Srsc     return devsw[ip->major].read(ip, dst, n);
408939f9edeSkaashoek   }
409939f9edeSkaashoek 
4107895178dSrsc   if(off > ip->size || off + n < off)
411bcca6c6bSrsc     return -1;
412bcca6c6bSrsc   if(off + n > ip->size)
413bcca6c6bSrsc     n = ip->size - off;
414bcca6c6bSrsc 
415bcca6c6bSrsc   for(tot=0; tot<n; tot+=m, off+=m, dst+=m){
41613ae8808Srsc     bp = bread(ip->dev, bmap(ip, off/BSIZE));
417bcca6c6bSrsc     m = min(n - tot, BSIZE - off%BSIZE);
418bcca6c6bSrsc     memmove(dst, bp->data + off%BSIZE, m);
419c59361f1Srtm     brelse(bp);
420c59361f1Srtm   }
421bcca6c6bSrsc   return n;
422ea2909b6Skaashoek }
423ea2909b6Skaashoek 
424eaea18cbSrsc // PAGEBREAK!
425bb207a1dSrsc // Write data to inode.
426ea2909b6Skaashoek int
427bcca6c6bSrsc writei(struct inode *ip, char *src, uint off, uint n)
4286fa5ffb5Skaashoek {
429bcca6c6bSrsc   uint tot, m;
4307d4aef6cSrsc   struct buf *bp;
4317d4aef6cSrsc 
4326fa5ffb5Skaashoek   if(ip->type == T_DEV){
4331dca3afbSrsc     if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write)
434939f9edeSkaashoek       return -1;
435d844f0f9Srsc     return devsw[ip->major].write(ip, src, n);
4367d4aef6cSrsc   }
4377d4aef6cSrsc 
43813ae8808Srsc   if(off > ip->size || off + n < off)
439bcca6c6bSrsc     return -1;
440bcca6c6bSrsc   if(off + n > MAXFILE*BSIZE)
441bcca6c6bSrsc     n = MAXFILE*BSIZE - off;
442bcca6c6bSrsc 
443bcca6c6bSrsc   for(tot=0; tot<n; tot+=m, off+=m, src+=m){
44413ae8808Srsc     bp = bread(ip->dev, bmap(ip, off/BSIZE));
445bcca6c6bSrsc     m = min(n - tot, BSIZE - off%BSIZE);
446bcca6c6bSrsc     memmove(bp->data + off%BSIZE, src, m);
447eaea18cbSrsc     bwrite(bp);
44828d9ef04Skaashoek     brelse(bp);
44928d9ef04Skaashoek   }
450bcca6c6bSrsc 
451bcca6c6bSrsc   if(n > 0 && off > ip->size){
45248b82470Srsc     ip->size = off;
45328d9ef04Skaashoek     iupdate(ip);
45428d9ef04Skaashoek   }
455bcca6c6bSrsc   return n;
4566fa5ffb5Skaashoek }
4576fa5ffb5Skaashoek 
458eaea18cbSrsc //PAGEBREAK!
459bcca6c6bSrsc // Directories
460bcca6c6bSrsc 
461eaea18cbSrsc int
462fbf91039Srsc namecmp(const char *s, const char *t)
463fbf91039Srsc {
464766ba5ccSrsc   return strncmp(s, t, DIRSIZ);
465fbf91039Srsc }
466fbf91039Srsc 
467bcca6c6bSrsc // Look for a directory entry in a directory.
468eaea18cbSrsc // If found, set *poff to byte offset of entry.
46920365348Srtm // Caller must have already locked dp.
47007090dd7Srsc struct inode*
471fbf91039Srsc dirlookup(struct inode *dp, char *name, uint *poff)
472bcca6c6bSrsc {
473f32f3638Srsc   uint off, inum;
474bcca6c6bSrsc   struct buf *bp;
475bcca6c6bSrsc   struct dirent *de;
476bcca6c6bSrsc 
477bcca6c6bSrsc   if(dp->type != T_DIR)
47820365348Srtm     panic("dirlookup not DIR");
479bcca6c6bSrsc 
480bcca6c6bSrsc   for(off = 0; off < dp->size; off += BSIZE){
48113ae8808Srsc     bp = bread(dp->dev, bmap(dp, off / BSIZE));
482bcca6c6bSrsc     for(de = (struct dirent*)bp->data;
483bcca6c6bSrsc         de < (struct dirent*)(bp->data + BSIZE);
484bcca6c6bSrsc         de++){
485bcca6c6bSrsc       if(de->inum == 0)
486bcca6c6bSrsc         continue;
487fbf91039Srsc       if(namecmp(name, de->name) == 0){
488bcca6c6bSrsc         // entry matches path element
489e2a620daSrsc         if(poff)
490bcca6c6bSrsc           *poff = off + (uchar*)de - bp->data;
491f32f3638Srsc         inum = de->inum;
492bcca6c6bSrsc         brelse(bp);
493f32f3638Srsc         return iget(dp->dev, inum);
494f32f3638Srsc       }
495f32f3638Srsc     }
496f32f3638Srsc     brelse(bp);
497f32f3638Srsc   }
498bcca6c6bSrsc   return 0;
499bcca6c6bSrsc }
500bcca6c6bSrsc 
50113ae8808Srsc // Write a new directory entry (name, inum) into the directory dp.
502eaea18cbSrsc int
50313ae8808Srsc dirlink(struct inode *dp, char *name, uint inum)
504bcca6c6bSrsc {
505e2a620daSrsc   int off;
506bcca6c6bSrsc   struct dirent de;
50707090dd7Srsc   struct inode *ip;
508f32f3638Srsc 
509eaea18cbSrsc   // Check that name is not present.
51007090dd7Srsc   if((ip = dirlookup(dp, name, 0)) != 0){
51107090dd7Srsc     iput(ip);
512f32f3638Srsc     return -1;
513f32f3638Srsc   }
514bcca6c6bSrsc 
515bcca6c6bSrsc   // Look for an empty dirent.
516bcca6c6bSrsc   for(off = 0; off < dp->size; off += sizeof(de)){
517bcca6c6bSrsc     if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
5187895178dSrsc       panic("dirlink read");
519bcca6c6bSrsc     if(de.inum == 0)
520bcca6c6bSrsc       break;
521bcca6c6bSrsc   }
522bcca6c6bSrsc 
523766ba5ccSrsc   strncpy(de.name, name, DIRSIZ);
52413ae8808Srsc   de.inum = inum;
525bcca6c6bSrsc   if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
5267895178dSrsc     panic("dirlink");
527f32f3638Srsc 
528f32f3638Srsc   return 0;
529bcca6c6bSrsc }
530bcca6c6bSrsc 
5318d2e9a48Srsc //PAGEBREAK!
532bcca6c6bSrsc // Paths
533bcca6c6bSrsc 
534eaea18cbSrsc // Copy the next path element from path into name.
535eaea18cbSrsc // Return a pointer to the element following the copied one.
536eaea18cbSrsc // The returned path has no leading slashes,
537eaea18cbSrsc // so the caller can check *path=='\0' to see if the name is the last one.
538eaea18cbSrsc // If no name to remove, return 0.
539ab5c2dbbSrsc //
540ab5c2dbbSrsc // Examples:
541eaea18cbSrsc //   skipelem("a/bb/c", name) = "bb/c", setting name = "a"
5427895178dSrsc //   skipelem("///a//bb", name) = "bb", setting name = "a"
54313ae8808Srsc //   skipelem("a", name) = "", setting name = "a"
544eaea18cbSrsc //   skipelem("", name) = skipelem("////", name) = 0
545ab5c2dbbSrsc //
546ab5c2dbbSrsc static char*
547fbf91039Srsc skipelem(char *path, char *name)
548ab5c2dbbSrsc {
549fbf91039Srsc   char *s;
550fbf91039Srsc   int len;
551fbf91039Srsc 
552ab5c2dbbSrsc   while(*path == '/')
553ab5c2dbbSrsc     path++;
554ab5c2dbbSrsc   if(*path == 0)
555ab5c2dbbSrsc     return 0;
556fbf91039Srsc   s = path;
557ab5c2dbbSrsc   while(*path != '/' && *path != 0)
558ab5c2dbbSrsc     path++;
559fbf91039Srsc   len = path - s;
560fbf91039Srsc   if(len >= DIRSIZ)
561fbf91039Srsc     memmove(name, s, DIRSIZ);
562fbf91039Srsc   else {
563fbf91039Srsc     memmove(name, s, len);
564fbf91039Srsc     name[len] = 0;
565fbf91039Srsc   }
566ab5c2dbbSrsc   while(*path == '/')
567ab5c2dbbSrsc     path++;
568ab5c2dbbSrsc   return path;
569ab5c2dbbSrsc }
570ab5c2dbbSrsc 
571eaea18cbSrsc // Look up and return the inode for a path name.
572bf2932a6Srsc // If parent != 0, return the inode for the parent and copy the final
573bf2932a6Srsc // path element into name, which must have room for DIRSIZ bytes.
57407090dd7Srsc static struct inode*
575*f9a06440SRuss Cox namex(char *path, int nameiparent, char *name)
5769d3fb671Srtm {
57707090dd7Srsc   struct inode *ip, *next;
5789d3fb671Srtm 
579ab5c2dbbSrsc   if(*path == '/')
5802ce40d70Srtm     ip = iget(ROOTDEV, ROOTINO);
581f32f3638Srsc   else
58207090dd7Srsc     ip = idup(cp->cwd);
5839d3fb671Srtm 
584fbf91039Srsc   while((path = skipelem(path, name)) != 0){
58507090dd7Srsc     ilock(ip);
58607090dd7Srsc     if(ip->type != T_DIR){
58707090dd7Srsc       iunlockput(ip);
588eaea18cbSrsc       return 0;
589eaea18cbSrsc     }
590*f9a06440SRuss Cox     if(nameiparent && *path == '\0'){
591e2a620daSrsc       // Stop one level early.
59207090dd7Srsc       iunlock(ip);
59307090dd7Srsc       return ip;
594ab5c2dbbSrsc     }
5957895178dSrsc     if((next = dirlookup(ip, name, 0)) == 0){
59607090dd7Srsc       iunlockput(ip);
597eaea18cbSrsc       return 0;
598eaea18cbSrsc     }
59907090dd7Srsc     iunlockput(ip);
60007090dd7Srsc     ip = next;
601ab5c2dbbSrsc   }
602*f9a06440SRuss Cox   if(nameiparent){
60307090dd7Srsc     iput(ip);
6045051da6dSrtm     return 0;
60520365348Srtm   }
60607090dd7Srsc   return ip;
6070633b971Skaashoek }
6089d3fb671Srtm 
6097895178dSrsc struct inode*
6107895178dSrsc namei(char *path)
6117895178dSrsc {
6127895178dSrsc   char name[DIRSIZ];
61313ae8808Srsc   return namex(path, 0, name);
6147895178dSrsc }
615bf2932a6Srsc 
6167895178dSrsc struct inode*
6177895178dSrsc nameiparent(char *path, char *name)
6187895178dSrsc {
61913ae8808Srsc   return namex(path, 1, name);
6207895178dSrsc }
621