xref: /xv6-public/fs.c (revision 0aef8914)
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"
22*0aef8914SRuss Cox #include "file.h"
2311a9947fSrtm 
24bcca6c6bSrsc #define min(a, b) ((a) < (b) ? (a) : (b))
25fbf91039Srsc static void itrunc(struct inode*);
2611a9947fSrtm 
27a505fd66Srsc // Read the super block.
28a505fd66Srsc static void
29a505fd66Srsc readsb(int dev, struct superblock *sb)
30a505fd66Srsc {
31a505fd66Srsc   struct buf *bp;
32a505fd66Srsc 
33a505fd66Srsc   bp = bread(dev, 1);
34a505fd66Srsc   memmove(sb, bp->data, sizeof(*sb));
35a505fd66Srsc   brelse(bp);
36a505fd66Srsc }
37a505fd66Srsc 
38a505fd66Srsc // Zero a block.
39a505fd66Srsc static void
40a505fd66Srsc bzero(int dev, int bno)
41a505fd66Srsc {
42a505fd66Srsc   struct buf *bp;
43a505fd66Srsc 
44a505fd66Srsc   bp = bread(dev, bno);
45a505fd66Srsc   memset(bp->data, 0, BSIZE);
46a505fd66Srsc   bwrite(bp);
47a505fd66Srsc   brelse(bp);
48a505fd66Srsc }
49a505fd66Srsc 
50bcca6c6bSrsc // Blocks.
515be0039cSrtm 
52f5527388Srsc // Allocate a disk block.
5324111398Skaashoek static uint
5424111398Skaashoek balloc(uint dev)
5524111398Skaashoek {
56a505fd66Srsc   int b, bi, m;
5724111398Skaashoek   struct buf *bp;
58a505fd66Srsc   struct superblock sb;
5924111398Skaashoek 
60a505fd66Srsc   bp = 0;
61a505fd66Srsc   readsb(dev, &sb);
62a505fd66Srsc   for(b = 0; b < sb.size; b += BPB){
63a505fd66Srsc     bp = bread(dev, BBLOCK(b, sb.ninodes));
64a505fd66Srsc     for(bi = 0; bi < BPB; bi++){
65a505fd66Srsc       m = 1 << (bi % 8);
66a505fd66Srsc       if((bp->data[bi/8] & m) == 0){  // Is block free?
67a505fd66Srsc         bp->data[bi/8] |= m;  // Mark block in use on disk.
68a505fd66Srsc         bwrite(bp);
6924111398Skaashoek         brelse(bp);
70a505fd66Srsc         return b + bi;
7124111398Skaashoek       }
72a505fd66Srsc     }
7328d9ef04Skaashoek     brelse(bp);
747d4aef6cSrsc   }
757d4aef6cSrsc   panic("balloc: out of blocks");
767d4aef6cSrsc }
7724111398Skaashoek 
78bb207a1dSrsc // Free a disk block.
7928d9ef04Skaashoek static void
8028d9ef04Skaashoek bfree(int dev, uint b)
8128d9ef04Skaashoek {
8228d9ef04Skaashoek   struct buf *bp;
83a505fd66Srsc   struct superblock sb;
84a505fd66Srsc   int bi, m;
8528d9ef04Skaashoek 
86a505fd66Srsc   bzero(dev, b);
8728d9ef04Skaashoek 
88a505fd66Srsc   readsb(dev, &sb);
89a505fd66Srsc   bp = bread(dev, BBLOCK(b, sb.ninodes));
9028d9ef04Skaashoek   bi = b % BPB;
91a505fd66Srsc   m = 1 << (bi % 8);
92902b13f5Srtm   if((bp->data[bi/8] & m) == 0)
93902b13f5Srtm     panic("freeing free block");
94a505fd66Srsc   bp->data[bi/8] &= ~m;  // Mark block free on disk.
95a505fd66Srsc   bwrite(bp);
9628d9ef04Skaashoek   brelse(bp);
9728d9ef04Skaashoek }
9824111398Skaashoek 
996c34f97cSrsc // Inodes.
1006c34f97cSrsc //
1016c34f97cSrsc // An inode is a single, unnamed file in the file system.
1026c34f97cSrsc // The inode disk structure holds metadata (the type, device numbers,
1036c34f97cSrsc // and data size) along with a list of blocks where the associated
1046c34f97cSrsc // data can be found.
105bcca6c6bSrsc //
106bcca6c6bSrsc // The inodes are laid out sequentially on disk immediately after
107bcca6c6bSrsc // the superblock.  The kernel keeps a cache of the in-use
108bcca6c6bSrsc // on-disk structures to provide a place for synchronizing access
109bcca6c6bSrsc // to inodes shared between multiple processes.
110bcca6c6bSrsc //
111902b13f5Srtm // ip->ref counts the number of pointer references to this cached
112bcca6c6bSrsc // inode; references are typically kept in struct file and in cp->cwd.
113bcca6c6bSrsc // When ip->ref falls to zero, the inode is no longer cached.
114bcca6c6bSrsc // It is an error to use an inode without holding a reference to it.
115bcca6c6bSrsc //
116eaea18cbSrsc // Processes are only allowed to read and write inode
1176c34f97cSrsc // metadata and contents when holding the inode's lock,
1186c34f97cSrsc // represented by the I_BUSY flag in the in-memory copy.
119902b13f5Srtm // Because inode locks are held during disk accesses,
120902b13f5Srtm // they are implemented using a flag rather than with
121902b13f5Srtm // spin locks.  Callers are responsible for locking
122eaea18cbSrsc // inodes before passing them to routines in this file; leaving
123eaea18cbSrsc // this responsibility with the caller makes it possible for them
124eaea18cbSrsc // to create arbitrarily-sized atomic operations.
125eaea18cbSrsc //
126eaea18cbSrsc // To give maximum control over locking to the callers,
127eaea18cbSrsc // the routines in this file that return inode pointers
128eaea18cbSrsc // return pointers to *unlocked* inodes.  It is the callers'
129902b13f5Srtm // responsibility to lock them before using them.  A non-zero
130902b13f5Srtm // ip->ref keeps these unlocked inodes in the cache.
131bcca6c6bSrsc 
132bcca6c6bSrsc struct {
133bcca6c6bSrsc   struct spinlock lock;
134bcca6c6bSrsc   struct inode inode[NINODE];
135bcca6c6bSrsc } icache;
136bcca6c6bSrsc 
137bcca6c6bSrsc void
138bcca6c6bSrsc iinit(void)
139bcca6c6bSrsc {
14034295f46Srsc   initlock(&icache.lock, "icache");
141bcca6c6bSrsc }
142bcca6c6bSrsc 
143f9a06440SRuss Cox static struct inode* iget(uint dev, uint inum);
144f9a06440SRuss Cox 
145f9a06440SRuss Cox //PAGEBREAK!
146f9a06440SRuss Cox // Allocate a new inode with the given type on device dev.
147f9a06440SRuss Cox struct inode*
148f9a06440SRuss Cox ialloc(uint dev, short type)
149f9a06440SRuss Cox {
150f9a06440SRuss Cox   int inum;
151f9a06440SRuss Cox   struct buf *bp;
152f9a06440SRuss Cox   struct dinode *dip;
153f9a06440SRuss Cox   struct superblock sb;
154f9a06440SRuss Cox 
155f9a06440SRuss Cox   readsb(dev, &sb);
156f9a06440SRuss Cox   for(inum = 1; inum < sb.ninodes; inum++){  // loop over inode blocks
157f9a06440SRuss Cox     bp = bread(dev, IBLOCK(inum));
158f9a06440SRuss Cox     dip = (struct dinode*)bp->data + inum%IPB;
159f9a06440SRuss Cox     if(dip->type == 0){  // a free inode
160f9a06440SRuss Cox       memset(dip, 0, sizeof(*dip));
161f9a06440SRuss Cox       dip->type = type;
162f9a06440SRuss Cox       bwrite(bp);   // mark it allocated on the disk
163f9a06440SRuss Cox       brelse(bp);
164f9a06440SRuss Cox       return iget(dev, inum);
165f9a06440SRuss Cox     }
166f9a06440SRuss Cox     brelse(bp);
167f9a06440SRuss Cox   }
168f9a06440SRuss Cox   panic("ialloc: no inodes");
169f9a06440SRuss Cox }
170f9a06440SRuss Cox 
171f9a06440SRuss Cox // Copy inode, which has changed, from memory to disk.
172f9a06440SRuss Cox void
173f9a06440SRuss Cox iupdate(struct inode *ip)
174f9a06440SRuss Cox {
175f9a06440SRuss Cox   struct buf *bp;
176f9a06440SRuss Cox   struct dinode *dip;
177f9a06440SRuss Cox 
178f9a06440SRuss Cox   bp = bread(ip->dev, IBLOCK(ip->inum));
179f9a06440SRuss Cox   dip = (struct dinode*)bp->data + ip->inum%IPB;
180f9a06440SRuss Cox   dip->type = ip->type;
181f9a06440SRuss Cox   dip->major = ip->major;
182f9a06440SRuss Cox   dip->minor = ip->minor;
183f9a06440SRuss Cox   dip->nlink = ip->nlink;
184f9a06440SRuss Cox   dip->size = ip->size;
185f9a06440SRuss Cox   memmove(dip->addrs, ip->addrs, sizeof(ip->addrs));
186f9a06440SRuss Cox   bwrite(bp);
187f9a06440SRuss Cox   brelse(bp);
188f9a06440SRuss Cox }
189f9a06440SRuss Cox 
190f5527388Srsc // Find the inode with number inum on device dev
1917895178dSrsc // and return the in-memory copy.
19207090dd7Srsc static struct inode*
19311a9947fSrtm iget(uint dev, uint inum)
19411a9947fSrtm {
195bcca6c6bSrsc   struct inode *ip, *empty;
19611a9947fSrtm 
197bcca6c6bSrsc   acquire(&icache.lock);
19811a9947fSrtm 
199bcca6c6bSrsc   // Try for cached inode.
200bcca6c6bSrsc   empty = 0;
201bcca6c6bSrsc   for(ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++){
2020d6bbd31Srsc     if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){
2030d6bbd31Srsc       ip->ref++;
204bcca6c6bSrsc       release(&icache.lock);
20507090dd7Srsc       return ip;
20611a9947fSrtm     }
207bcca6c6bSrsc     if(empty == 0 && ip->ref == 0)    // Remember empty slot.
208bcca6c6bSrsc       empty = ip;
20911a9947fSrtm   }
21011a9947fSrtm 
211bcca6c6bSrsc   // Allocate fresh inode.
212bcca6c6bSrsc   if(empty == 0)
21332eea766Srsc     panic("iget: no inodes");
21411a9947fSrtm 
215bcca6c6bSrsc   ip = empty;
216bcca6c6bSrsc   ip->dev = dev;
217bcca6c6bSrsc   ip->inum = inum;
218bcca6c6bSrsc   ip->ref = 1;
219f32f3638Srsc   ip->flags = 0;
220bcca6c6bSrsc   release(&icache.lock);
22111a9947fSrtm 
22207090dd7Srsc   return ip;
223f32f3638Srsc }
224f32f3638Srsc 
225eaea18cbSrsc // Increment reference count for ip.
226eaea18cbSrsc // Returns ip to enable ip = idup(ip1) idiom.
22707090dd7Srsc struct inode*
22807090dd7Srsc idup(struct inode *ip)
229f32f3638Srsc {
230eaea18cbSrsc   acquire(&icache.lock);
231eaea18cbSrsc   ip->ref++;
232eaea18cbSrsc   release(&icache.lock);
23307090dd7Srsc   return ip;
234f32f3638Srsc }
235f32f3638Srsc 
236f32f3638Srsc // Lock the given inode.
23707090dd7Srsc void
23807090dd7Srsc ilock(struct inode *ip)
239f32f3638Srsc {
240f32f3638Srsc   struct buf *bp;
241f32f3638Srsc   struct dinode *dip;
242eaea18cbSrsc 
24307090dd7Srsc   if(ip == 0 || ip->ref < 1)
24407090dd7Srsc     panic("ilock");
245f32f3638Srsc 
246f32f3638Srsc   acquire(&icache.lock);
247f32f3638Srsc   while(ip->flags & I_BUSY)
248f32f3638Srsc     sleep(ip, &icache.lock);
249f32f3638Srsc   ip->flags |= I_BUSY;
250f32f3638Srsc   release(&icache.lock);
251f32f3638Srsc 
252f32f3638Srsc   if(!(ip->flags & I_VALID)){
253f32f3638Srsc     bp = bread(ip->dev, IBLOCK(ip->inum));
2543341e30fSrsc     dip = (struct dinode*)bp->data + ip->inum%IPB;
255bcca6c6bSrsc     ip->type = dip->type;
256bcca6c6bSrsc     ip->major = dip->major;
257bcca6c6bSrsc     ip->minor = dip->minor;
258bcca6c6bSrsc     ip->nlink = dip->nlink;
259bcca6c6bSrsc     ip->size = dip->size;
260bcca6c6bSrsc     memmove(ip->addrs, dip->addrs, sizeof(ip->addrs));
26111a9947fSrtm     brelse(bp);
262f32f3638Srsc     ip->flags |= I_VALID;
263eaea18cbSrsc     if(ip->type == 0)
264eaea18cbSrsc       panic("ilock: no type");
26511a9947fSrtm   }
266bcca6c6bSrsc }
267bcca6c6bSrsc 
268bcca6c6bSrsc // Unlock the given inode.
26907090dd7Srsc void
270bcca6c6bSrsc iunlock(struct inode *ip)
271bcca6c6bSrsc {
27207090dd7Srsc   if(ip == 0 || !(ip->flags & I_BUSY) || ip->ref < 1)
273bcca6c6bSrsc     panic("iunlock");
274bcca6c6bSrsc 
275bcca6c6bSrsc   acquire(&icache.lock);
276ce72cadbSrsc   ip->flags &= ~I_BUSY;
277bcca6c6bSrsc   wakeup(ip);
278bcca6c6bSrsc   release(&icache.lock);
279bcca6c6bSrsc }
280bcca6c6bSrsc 
281f32f3638Srsc // Caller holds reference to unlocked ip.  Drop reference.
282bcca6c6bSrsc void
28307090dd7Srsc iput(struct inode *ip)
284bcca6c6bSrsc {
285f32f3638Srsc   acquire(&icache.lock);
286f32f3638Srsc   if(ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0){
287f32f3638Srsc     // inode is no longer used: truncate and free inode.
288f32f3638Srsc     if(ip->flags & I_BUSY)
289eaea18cbSrsc       panic("iput busy");
290f32f3638Srsc     ip->flags |= I_BUSY;
291f32f3638Srsc     release(&icache.lock);
292f32f3638Srsc     itrunc(ip);
293f32f3638Srsc     ip->type = 0;
294f32f3638Srsc     iupdate(ip);
295f32f3638Srsc     acquire(&icache.lock);
296ce72cadbSrsc     ip->flags = 0;
2978970df0cSrsc     wakeup(ip);
298f32f3638Srsc   }
299f32f3638Srsc   ip->ref--;
300f32f3638Srsc   release(&icache.lock);
301bcca6c6bSrsc }
302bcca6c6bSrsc 
3037895178dSrsc // Common idiom: unlock, then put.
30407090dd7Srsc void
30507090dd7Srsc iunlockput(struct inode *ip)
30607090dd7Srsc {
30707090dd7Srsc   iunlock(ip);
30807090dd7Srsc   iput(ip);
30907090dd7Srsc }
31007090dd7Srsc 
3118d2e9a48Srsc //PAGEBREAK!
312bcca6c6bSrsc // Inode contents
313bcca6c6bSrsc //
314bcca6c6bSrsc // The contents (data) associated with each inode is stored
315bcca6c6bSrsc // in a sequence of blocks on the disk.  The first NDIRECT blocks
3167895178dSrsc // are listed in ip->addrs[].  The next NINDIRECT blocks are
317bcca6c6bSrsc // listed in the block ip->addrs[INDIRECT].
3189d3fb671Srtm 
319bb207a1dSrsc // Return the disk block address of the nth block in inode ip.
32013ae8808Srsc // If there is no such block, bmap allocates one.
321eaea18cbSrsc static uint
32213ae8808Srsc bmap(struct inode *ip, uint bn)
32322bac2cbSkaashoek {
324bcca6c6bSrsc   uint addr, *a;
325bcca6c6bSrsc   struct buf *bp;
32622bac2cbSkaashoek 
327ea2909b6Skaashoek   if(bn < NDIRECT){
32813ae8808Srsc     if((addr = ip->addrs[bn]) == 0)
329bcca6c6bSrsc       ip->addrs[bn] = addr = balloc(ip->dev);
330bcca6c6bSrsc     return addr;
331bcca6c6bSrsc   }
332bcca6c6bSrsc   bn -= NDIRECT;
333bcca6c6bSrsc 
334bcca6c6bSrsc   if(bn < NINDIRECT){
335bcca6c6bSrsc     // Load indirect block, allocating if necessary.
33613ae8808Srsc     if((addr = ip->addrs[NDIRECT]) == 0)
337ba6cd8a6Srsc       ip->addrs[NDIRECT] = addr = balloc(ip->dev);
338bcca6c6bSrsc     bp = bread(ip->dev, addr);
339bcca6c6bSrsc     a = (uint*)bp->data;
340bcca6c6bSrsc     if((addr = a[bn]) == 0){
341bcca6c6bSrsc       a[bn] = addr = balloc(ip->dev);
342eaea18cbSrsc       bwrite(bp);
343bcca6c6bSrsc     }
344bcca6c6bSrsc     brelse(bp);
345bcca6c6bSrsc     return addr;
34622bac2cbSkaashoek   }
34722bac2cbSkaashoek 
348bcca6c6bSrsc   panic("bmap: out of range");
349bcca6c6bSrsc }
350bcca6c6bSrsc 
351bcca6c6bSrsc // Truncate inode (discard contents).
3528eb20827Srtm // Only called after the last dirent referring
3538eb20827Srtm // to this inode has been erased on disk.
354fbf91039Srsc static void
3552aa4c3bcSrtm itrunc(struct inode *ip)
35622bac2cbSkaashoek {
357ea2909b6Skaashoek   int i, j;
358bcca6c6bSrsc   struct buf *bp;
3597d4aef6cSrsc   uint *a;
36022bac2cbSkaashoek 
361bcca6c6bSrsc   for(i = 0; i < NDIRECT; i++){
362bcca6c6bSrsc     if(ip->addrs[i]){
36322bac2cbSkaashoek       bfree(ip->dev, ip->addrs[i]);
36422bac2cbSkaashoek       ip->addrs[i] = 0;
36522bac2cbSkaashoek     }
36622bac2cbSkaashoek   }
367bcca6c6bSrsc 
368ba6cd8a6Srsc   if(ip->addrs[NDIRECT]){
369ba6cd8a6Srsc     bp = bread(ip->dev, ip->addrs[NDIRECT]);
370bcca6c6bSrsc     a = (uint*)bp->data;
371bcca6c6bSrsc     for(j = 0; j < NINDIRECT; j++){
372bcca6c6bSrsc       if(a[j])
373bcca6c6bSrsc         bfree(ip->dev, a[j]);
374bcca6c6bSrsc     }
375bcca6c6bSrsc     brelse(bp);
376ba6cd8a6Srsc     bfree(ip->dev, ip->addrs[NDIRECT]);
377ba6cd8a6Srsc     ip->addrs[NDIRECT] = 0;
378bcca6c6bSrsc   }
379bcca6c6bSrsc 
38022bac2cbSkaashoek   ip->size = 0;
38122bac2cbSkaashoek   iupdate(ip);
38222bac2cbSkaashoek }
38322bac2cbSkaashoek 
384bb207a1dSrsc // Copy stat information from inode.
385e958c538Skaashoek void
3861f544842Skaashoek stati(struct inode *ip, struct stat *st)
3871f544842Skaashoek {
3881dca3afbSrsc   st->dev = ip->dev;
3891dca3afbSrsc   st->ino = ip->inum;
3901dca3afbSrsc   st->type = ip->type;
3911dca3afbSrsc   st->nlink = ip->nlink;
3921dca3afbSrsc   st->size = ip->size;
3931f544842Skaashoek }
3941f544842Skaashoek 
395eaea18cbSrsc //PAGEBREAK!
396bb207a1dSrsc // Read data from inode.
397c59361f1Srtm int
39817a85657Srtm readi(struct inode *ip, char *dst, uint off, uint n)
399c59361f1Srtm {
400bcca6c6bSrsc   uint tot, m;
401c59361f1Srtm   struct buf *bp;
402c59361f1Srtm 
403939f9edeSkaashoek   if(ip->type == T_DEV){
4041dca3afbSrsc     if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read)
405939f9edeSkaashoek       return -1;
406d844f0f9Srsc     return devsw[ip->major].read(ip, dst, n);
407939f9edeSkaashoek   }
408939f9edeSkaashoek 
4097895178dSrsc   if(off > ip->size || off + n < off)
410bcca6c6bSrsc     return -1;
411bcca6c6bSrsc   if(off + n > ip->size)
412bcca6c6bSrsc     n = ip->size - off;
413bcca6c6bSrsc 
414bcca6c6bSrsc   for(tot=0; tot<n; tot+=m, off+=m, dst+=m){
41513ae8808Srsc     bp = bread(ip->dev, bmap(ip, off/BSIZE));
416bcca6c6bSrsc     m = min(n - tot, BSIZE - off%BSIZE);
417bcca6c6bSrsc     memmove(dst, bp->data + off%BSIZE, m);
418c59361f1Srtm     brelse(bp);
419c59361f1Srtm   }
420bcca6c6bSrsc   return n;
421ea2909b6Skaashoek }
422ea2909b6Skaashoek 
423eaea18cbSrsc // PAGEBREAK!
424bb207a1dSrsc // Write data to inode.
425ea2909b6Skaashoek int
426bcca6c6bSrsc writei(struct inode *ip, char *src, uint off, uint n)
4276fa5ffb5Skaashoek {
428bcca6c6bSrsc   uint tot, m;
4297d4aef6cSrsc   struct buf *bp;
4307d4aef6cSrsc 
4316fa5ffb5Skaashoek   if(ip->type == T_DEV){
4321dca3afbSrsc     if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write)
433939f9edeSkaashoek       return -1;
434d844f0f9Srsc     return devsw[ip->major].write(ip, src, n);
4357d4aef6cSrsc   }
4367d4aef6cSrsc 
43713ae8808Srsc   if(off > ip->size || off + n < off)
438bcca6c6bSrsc     return -1;
439bcca6c6bSrsc   if(off + n > MAXFILE*BSIZE)
440bcca6c6bSrsc     n = MAXFILE*BSIZE - off;
441bcca6c6bSrsc 
442bcca6c6bSrsc   for(tot=0; tot<n; tot+=m, off+=m, src+=m){
44313ae8808Srsc     bp = bread(ip->dev, bmap(ip, off/BSIZE));
444bcca6c6bSrsc     m = min(n - tot, BSIZE - off%BSIZE);
445bcca6c6bSrsc     memmove(bp->data + off%BSIZE, src, m);
446eaea18cbSrsc     bwrite(bp);
44728d9ef04Skaashoek     brelse(bp);
44828d9ef04Skaashoek   }
449bcca6c6bSrsc 
450bcca6c6bSrsc   if(n > 0 && off > ip->size){
45148b82470Srsc     ip->size = off;
45228d9ef04Skaashoek     iupdate(ip);
45328d9ef04Skaashoek   }
454bcca6c6bSrsc   return n;
4556fa5ffb5Skaashoek }
4566fa5ffb5Skaashoek 
457eaea18cbSrsc //PAGEBREAK!
458bcca6c6bSrsc // Directories
459bcca6c6bSrsc 
460eaea18cbSrsc int
461fbf91039Srsc namecmp(const char *s, const char *t)
462fbf91039Srsc {
463766ba5ccSrsc   return strncmp(s, t, DIRSIZ);
464fbf91039Srsc }
465fbf91039Srsc 
466bcca6c6bSrsc // Look for a directory entry in a directory.
467eaea18cbSrsc // If found, set *poff to byte offset of entry.
46820365348Srtm // Caller must have already locked dp.
46907090dd7Srsc struct inode*
470fbf91039Srsc dirlookup(struct inode *dp, char *name, uint *poff)
471bcca6c6bSrsc {
472f32f3638Srsc   uint off, inum;
473bcca6c6bSrsc   struct buf *bp;
474bcca6c6bSrsc   struct dirent *de;
475bcca6c6bSrsc 
476bcca6c6bSrsc   if(dp->type != T_DIR)
47720365348Srtm     panic("dirlookup not DIR");
478bcca6c6bSrsc 
479bcca6c6bSrsc   for(off = 0; off < dp->size; off += BSIZE){
48013ae8808Srsc     bp = bread(dp->dev, bmap(dp, off / BSIZE));
481bcca6c6bSrsc     for(de = (struct dirent*)bp->data;
482bcca6c6bSrsc         de < (struct dirent*)(bp->data + BSIZE);
483bcca6c6bSrsc         de++){
484bcca6c6bSrsc       if(de->inum == 0)
485bcca6c6bSrsc         continue;
486fbf91039Srsc       if(namecmp(name, de->name) == 0){
487bcca6c6bSrsc         // entry matches path element
488e2a620daSrsc         if(poff)
489bcca6c6bSrsc           *poff = off + (uchar*)de - bp->data;
490f32f3638Srsc         inum = de->inum;
491bcca6c6bSrsc         brelse(bp);
492f32f3638Srsc         return iget(dp->dev, inum);
493f32f3638Srsc       }
494f32f3638Srsc     }
495f32f3638Srsc     brelse(bp);
496f32f3638Srsc   }
497bcca6c6bSrsc   return 0;
498bcca6c6bSrsc }
499bcca6c6bSrsc 
50013ae8808Srsc // Write a new directory entry (name, inum) into the directory dp.
501eaea18cbSrsc int
50213ae8808Srsc dirlink(struct inode *dp, char *name, uint inum)
503bcca6c6bSrsc {
504e2a620daSrsc   int off;
505bcca6c6bSrsc   struct dirent de;
50607090dd7Srsc   struct inode *ip;
507f32f3638Srsc 
508eaea18cbSrsc   // Check that name is not present.
50907090dd7Srsc   if((ip = dirlookup(dp, name, 0)) != 0){
51007090dd7Srsc     iput(ip);
511f32f3638Srsc     return -1;
512f32f3638Srsc   }
513bcca6c6bSrsc 
514bcca6c6bSrsc   // Look for an empty dirent.
515bcca6c6bSrsc   for(off = 0; off < dp->size; off += sizeof(de)){
516bcca6c6bSrsc     if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
5177895178dSrsc       panic("dirlink read");
518bcca6c6bSrsc     if(de.inum == 0)
519bcca6c6bSrsc       break;
520bcca6c6bSrsc   }
521bcca6c6bSrsc 
522766ba5ccSrsc   strncpy(de.name, name, DIRSIZ);
52313ae8808Srsc   de.inum = inum;
524bcca6c6bSrsc   if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
5257895178dSrsc     panic("dirlink");
526f32f3638Srsc 
527f32f3638Srsc   return 0;
528bcca6c6bSrsc }
529bcca6c6bSrsc 
5308d2e9a48Srsc //PAGEBREAK!
531bcca6c6bSrsc // Paths
532bcca6c6bSrsc 
533eaea18cbSrsc // Copy the next path element from path into name.
534eaea18cbSrsc // Return a pointer to the element following the copied one.
535eaea18cbSrsc // The returned path has no leading slashes,
536eaea18cbSrsc // so the caller can check *path=='\0' to see if the name is the last one.
537eaea18cbSrsc // If no name to remove, return 0.
538ab5c2dbbSrsc //
539ab5c2dbbSrsc // Examples:
540eaea18cbSrsc //   skipelem("a/bb/c", name) = "bb/c", setting name = "a"
5417895178dSrsc //   skipelem("///a//bb", name) = "bb", setting name = "a"
54213ae8808Srsc //   skipelem("a", name) = "", setting name = "a"
543eaea18cbSrsc //   skipelem("", name) = skipelem("////", name) = 0
544ab5c2dbbSrsc //
545ab5c2dbbSrsc static char*
546fbf91039Srsc skipelem(char *path, char *name)
547ab5c2dbbSrsc {
548fbf91039Srsc   char *s;
549fbf91039Srsc   int len;
550fbf91039Srsc 
551ab5c2dbbSrsc   while(*path == '/')
552ab5c2dbbSrsc     path++;
553ab5c2dbbSrsc   if(*path == 0)
554ab5c2dbbSrsc     return 0;
555fbf91039Srsc   s = path;
556ab5c2dbbSrsc   while(*path != '/' && *path != 0)
557ab5c2dbbSrsc     path++;
558fbf91039Srsc   len = path - s;
559fbf91039Srsc   if(len >= DIRSIZ)
560fbf91039Srsc     memmove(name, s, DIRSIZ);
561fbf91039Srsc   else {
562fbf91039Srsc     memmove(name, s, len);
563fbf91039Srsc     name[len] = 0;
564fbf91039Srsc   }
565ab5c2dbbSrsc   while(*path == '/')
566ab5c2dbbSrsc     path++;
567ab5c2dbbSrsc   return path;
568ab5c2dbbSrsc }
569ab5c2dbbSrsc 
570eaea18cbSrsc // Look up and return the inode for a path name.
571bf2932a6Srsc // If parent != 0, return the inode for the parent and copy the final
572bf2932a6Srsc // path element into name, which must have room for DIRSIZ bytes.
57307090dd7Srsc static struct inode*
574f9a06440SRuss Cox namex(char *path, int nameiparent, char *name)
5759d3fb671Srtm {
57607090dd7Srsc   struct inode *ip, *next;
5779d3fb671Srtm 
578ab5c2dbbSrsc   if(*path == '/')
5792ce40d70Srtm     ip = iget(ROOTDEV, ROOTINO);
580f32f3638Srsc   else
58107090dd7Srsc     ip = idup(cp->cwd);
5829d3fb671Srtm 
583fbf91039Srsc   while((path = skipelem(path, name)) != 0){
58407090dd7Srsc     ilock(ip);
58507090dd7Srsc     if(ip->type != T_DIR){
58607090dd7Srsc       iunlockput(ip);
587eaea18cbSrsc       return 0;
588eaea18cbSrsc     }
589f9a06440SRuss Cox     if(nameiparent && *path == '\0'){
590e2a620daSrsc       // Stop one level early.
59107090dd7Srsc       iunlock(ip);
59207090dd7Srsc       return ip;
593ab5c2dbbSrsc     }
5947895178dSrsc     if((next = dirlookup(ip, name, 0)) == 0){
59507090dd7Srsc       iunlockput(ip);
596eaea18cbSrsc       return 0;
597eaea18cbSrsc     }
59807090dd7Srsc     iunlockput(ip);
59907090dd7Srsc     ip = next;
600ab5c2dbbSrsc   }
601f9a06440SRuss Cox   if(nameiparent){
60207090dd7Srsc     iput(ip);
6035051da6dSrtm     return 0;
60420365348Srtm   }
60507090dd7Srsc   return ip;
6060633b971Skaashoek }
6079d3fb671Srtm 
6087895178dSrsc struct inode*
6097895178dSrsc namei(char *path)
6107895178dSrsc {
6117895178dSrsc   char name[DIRSIZ];
61213ae8808Srsc   return namex(path, 0, name);
6137895178dSrsc }
614bf2932a6Srsc 
6157895178dSrsc struct inode*
6167895178dSrsc nameiparent(char *path, char *name)
6177895178dSrsc {
61813ae8808Srsc   return namex(path, 1, name);
6197895178dSrsc }
620