xref: /xv6-public/fs.c (revision 8d2e9a48)
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
13207090dd7Srsc 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);
14507090dd7Srsc       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 
16207090dd7Srsc   return ip;
163f32f3638Srsc }
164f32f3638Srsc 
165eaea18cbSrsc // Increment reference count for ip.
166eaea18cbSrsc // Returns ip to enable ip = idup(ip1) idiom.
16707090dd7Srsc struct inode*
16807090dd7Srsc idup(struct inode *ip)
169f32f3638Srsc {
170eaea18cbSrsc   acquire(&icache.lock);
171eaea18cbSrsc   ip->ref++;
172eaea18cbSrsc   release(&icache.lock);
17307090dd7Srsc   return ip;
174f32f3638Srsc }
175f32f3638Srsc 
176f32f3638Srsc // Lock the given inode.
17707090dd7Srsc void
17807090dd7Srsc ilock(struct inode *ip)
179f32f3638Srsc {
180f32f3638Srsc   struct buf *bp;
181f32f3638Srsc   struct dinode *dip;
182eaea18cbSrsc 
18307090dd7Srsc   if(ip == 0 || ip->ref < 1)
18407090dd7Srsc     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.
20907090dd7Srsc void
210bcca6c6bSrsc iunlock(struct inode *ip)
211bcca6c6bSrsc {
21207090dd7Srsc   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
22307090dd7Srsc 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 
24307090dd7Srsc void
24407090dd7Srsc iunlockput(struct inode *ip)
24507090dd7Srsc {
24607090dd7Srsc   iunlock(ip);
24707090dd7Srsc   iput(ip);
24807090dd7Srsc }
24907090dd7Srsc 
250*8d2e9a48Srsc //PAGEBREAK!
251bcca6c6bSrsc // Allocate a new inode with the given type on device dev.
25207090dd7Srsc struct inode*
253e8d11c2eSkaashoek ialloc(uint dev, short type)
254e8d11c2eSkaashoek {
255f32f3638Srsc   int inum, ninodes;
256f32f3638Srsc   struct buf *bp;
2577d4aef6cSrsc   struct dinode *dip;
258e8d11c2eSkaashoek   struct superblock *sb;
259e8d11c2eSkaashoek 
260e8d11c2eSkaashoek   bp = bread(dev, 1);
26124111398Skaashoek   sb = (struct superblock*)bp->data;
262e8d11c2eSkaashoek   ninodes = sb->ninodes;
263e8d11c2eSkaashoek   brelse(bp);
264e8d11c2eSkaashoek 
265e8d11c2eSkaashoek   for(inum = 1; inum < ninodes; inum++) {  // loop over inode blocks
26624111398Skaashoek     bp = bread(dev, IBLOCK(inum));
267e8d11c2eSkaashoek     dip = &((struct dinode*)(bp->data))[inum % IPB];
268e8d11c2eSkaashoek     if(dip->type == 0) {  // a free inode
2692aa4c3bcSrtm       memset(dip, 0, sizeof(*dip));
270e8d11c2eSkaashoek       dip->type = type;
271eaea18cbSrsc       bwrite(bp);   // mark it allocated on the disk
272e8d11c2eSkaashoek       brelse(bp);
273f32f3638Srsc       return iget(dev, inum);
274e8d11c2eSkaashoek     }
27595c07f82Srsc     brelse(bp);
27695c07f82Srsc   }
27795c07f82Srsc   panic("ialloc: no inodes");
27895c07f82Srsc }
279e8d11c2eSkaashoek 
280bcca6c6bSrsc // Copy inode, which has changed, from memory to disk.
281eaea18cbSrsc void
282bcca6c6bSrsc iupdate(struct inode *ip)
283bcca6c6bSrsc {
284bcca6c6bSrsc   struct buf *bp;
285bcca6c6bSrsc   struct dinode *dip;
286bcca6c6bSrsc 
287bcca6c6bSrsc   bp = bread(ip->dev, IBLOCK(ip->inum));
288bcca6c6bSrsc   dip = &((struct dinode*)(bp->data))[ip->inum % IPB];
289bcca6c6bSrsc   dip->type = ip->type;
290bcca6c6bSrsc   dip->major = ip->major;
291bcca6c6bSrsc   dip->minor = ip->minor;
292bcca6c6bSrsc   dip->nlink = ip->nlink;
293bcca6c6bSrsc   dip->size = ip->size;
294bcca6c6bSrsc   memmove(dip->addrs, ip->addrs, sizeof(ip->addrs));
295eaea18cbSrsc   bwrite(bp);
296bcca6c6bSrsc   brelse(bp);
297bcca6c6bSrsc }
298bcca6c6bSrsc 
299*8d2e9a48Srsc //PAGEBREAK!
300bcca6c6bSrsc // Inode contents
301bcca6c6bSrsc //
302bcca6c6bSrsc // The contents (data) associated with each inode is stored
303bcca6c6bSrsc // in a sequence of blocks on the disk.  The first NDIRECT blocks
304bcca6c6bSrsc // are stored in ip->addrs[].  The next NINDIRECT blocks are
305bcca6c6bSrsc // listed in the block ip->addrs[INDIRECT].
3069d3fb671Srtm 
307bb207a1dSrsc // Return the disk block address of the nth block in inode ip.
308eaea18cbSrsc // If there is no such block, alloc controls whether one is allocated.
309eaea18cbSrsc static uint
310bcca6c6bSrsc bmap(struct inode *ip, uint bn, int alloc)
31122bac2cbSkaashoek {
312bcca6c6bSrsc   uint addr, *a;
313bcca6c6bSrsc   struct buf *bp;
31422bac2cbSkaashoek 
315ea2909b6Skaashoek   if(bn < NDIRECT) {
316bcca6c6bSrsc     if((addr = ip->addrs[bn]) == 0) {
317bcca6c6bSrsc       if(!alloc)
318bcca6c6bSrsc         return -1;
319bcca6c6bSrsc       ip->addrs[bn] = addr = balloc(ip->dev);
320ea2909b6Skaashoek     }
321bcca6c6bSrsc     return addr;
322bcca6c6bSrsc   }
323bcca6c6bSrsc   bn -= NDIRECT;
324bcca6c6bSrsc 
325bcca6c6bSrsc   if(bn < NINDIRECT) {
326bcca6c6bSrsc     // Load indirect block, allocating if necessary.
327bcca6c6bSrsc     if((addr = ip->addrs[INDIRECT]) == 0) {
328bcca6c6bSrsc       if(!alloc)
329bcca6c6bSrsc         return -1;
330bcca6c6bSrsc       ip->addrs[INDIRECT] = addr = balloc(ip->dev);
331bcca6c6bSrsc     }
332bcca6c6bSrsc     bp = bread(ip->dev, addr);
333bcca6c6bSrsc     a = (uint*)bp->data;
334bcca6c6bSrsc 
335bcca6c6bSrsc     if((addr = a[bn]) == 0) {
336bcca6c6bSrsc       if(!alloc) {
337bcca6c6bSrsc         brelse(bp);
338bcca6c6bSrsc         return -1;
339bcca6c6bSrsc       }
340bcca6c6bSrsc       a[bn] = addr = balloc(ip->dev);
341eaea18cbSrsc       bwrite(bp);
342bcca6c6bSrsc     }
343bcca6c6bSrsc     brelse(bp);
344bcca6c6bSrsc     return addr;
34522bac2cbSkaashoek   }
34622bac2cbSkaashoek 
347bcca6c6bSrsc   panic("bmap: out of range");
348bcca6c6bSrsc }
349bcca6c6bSrsc 
350eaea18cbSrsc // PAGEBREAK: 30
351bcca6c6bSrsc // Truncate inode (discard contents).
352fbf91039Srsc static void
3532aa4c3bcSrtm itrunc(struct inode *ip)
35422bac2cbSkaashoek {
355ea2909b6Skaashoek   int i, j;
356bcca6c6bSrsc   struct buf *bp;
3577d4aef6cSrsc   uint *a;
35822bac2cbSkaashoek 
359bcca6c6bSrsc   for(i = 0; i < NDIRECT; i++) {
360bcca6c6bSrsc     if(ip->addrs[i]) {
36122bac2cbSkaashoek       bfree(ip->dev, ip->addrs[i]);
36222bac2cbSkaashoek       ip->addrs[i] = 0;
36322bac2cbSkaashoek     }
36422bac2cbSkaashoek   }
365bcca6c6bSrsc 
366bcca6c6bSrsc   if(ip->addrs[INDIRECT]) {
367bcca6c6bSrsc     bp = bread(ip->dev, ip->addrs[INDIRECT]);
368bcca6c6bSrsc     a = (uint*)bp->data;
369bcca6c6bSrsc     for(j = 0; j < NINDIRECT; j++) {
370bcca6c6bSrsc       if(a[j])
371bcca6c6bSrsc         bfree(ip->dev, a[j]);
372bcca6c6bSrsc     }
373bcca6c6bSrsc     brelse(bp);
374bcca6c6bSrsc     ip->addrs[INDIRECT] = 0;
375bcca6c6bSrsc   }
376bcca6c6bSrsc 
37722bac2cbSkaashoek   ip->size = 0;
37822bac2cbSkaashoek   iupdate(ip);
37922bac2cbSkaashoek }
38022bac2cbSkaashoek 
381bb207a1dSrsc // Copy stat information from inode.
382e958c538Skaashoek void
3831f544842Skaashoek stati(struct inode *ip, struct stat *st)
3841f544842Skaashoek {
3851dca3afbSrsc   st->dev = ip->dev;
3861dca3afbSrsc   st->ino = ip->inum;
3871dca3afbSrsc   st->type = ip->type;
3881dca3afbSrsc   st->nlink = ip->nlink;
3891dca3afbSrsc   st->size = ip->size;
3901f544842Skaashoek }
3911f544842Skaashoek 
392eaea18cbSrsc //PAGEBREAK!
393bb207a1dSrsc // Read data from inode.
394c59361f1Srtm int
39517a85657Srtm readi(struct inode *ip, char *dst, uint off, uint n)
396c59361f1Srtm {
397bcca6c6bSrsc   uint tot, m;
398c59361f1Srtm   struct buf *bp;
399c59361f1Srtm 
400939f9edeSkaashoek   if(ip->type == T_DEV) {
4011dca3afbSrsc     if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read)
402939f9edeSkaashoek       return -1;
4031dca3afbSrsc     return devsw[ip->major].read(ip->minor, dst, n);
404939f9edeSkaashoek   }
405939f9edeSkaashoek 
406bcca6c6bSrsc   if(off + n < off)
407bcca6c6bSrsc     return -1;
408bcca6c6bSrsc   if(off + n > ip->size)
409bcca6c6bSrsc     n = ip->size - off;
410bcca6c6bSrsc 
411bcca6c6bSrsc   for(tot=0; tot<n; tot+=m, off+=m, dst+=m) {
412bcca6c6bSrsc     bp = bread(ip->dev, bmap(ip, off/BSIZE, 0));
413bcca6c6bSrsc     m = min(n - tot, BSIZE - off%BSIZE);
414bcca6c6bSrsc     memmove(dst, bp->data + off%BSIZE, m);
415c59361f1Srtm     brelse(bp);
416c59361f1Srtm   }
417bcca6c6bSrsc   return n;
418ea2909b6Skaashoek }
419ea2909b6Skaashoek 
420eaea18cbSrsc // PAGEBREAK!
421bb207a1dSrsc // Write data to inode.
422ea2909b6Skaashoek int
423bcca6c6bSrsc writei(struct inode *ip, char *src, uint off, uint n)
4246fa5ffb5Skaashoek {
425bcca6c6bSrsc   uint tot, m;
4267d4aef6cSrsc   struct buf *bp;
4277d4aef6cSrsc 
4286fa5ffb5Skaashoek   if(ip->type == T_DEV) {
4291dca3afbSrsc     if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write)
430939f9edeSkaashoek       return -1;
431bcca6c6bSrsc     return devsw[ip->major].write(ip->minor, src, n);
4327d4aef6cSrsc   }
4337d4aef6cSrsc 
434bcca6c6bSrsc   if(off + n < off)
435bcca6c6bSrsc     return -1;
436bcca6c6bSrsc   if(off + n > MAXFILE*BSIZE)
437bcca6c6bSrsc     n = MAXFILE*BSIZE - off;
438bcca6c6bSrsc 
439bcca6c6bSrsc   for(tot=0; tot<n; tot+=m, off+=m, src+=m) {
440bcca6c6bSrsc     bp = bread(ip->dev, bmap(ip, off/BSIZE, 1));
441bcca6c6bSrsc     m = min(n - tot, BSIZE - off%BSIZE);
442bcca6c6bSrsc     memmove(bp->data + off%BSIZE, src, m);
443eaea18cbSrsc     bwrite(bp);
44428d9ef04Skaashoek     brelse(bp);
44528d9ef04Skaashoek   }
446bcca6c6bSrsc 
447bcca6c6bSrsc   if(n > 0 && off > ip->size) {
44848b82470Srsc     ip->size = off;
44928d9ef04Skaashoek     iupdate(ip);
45028d9ef04Skaashoek   }
451bcca6c6bSrsc   return n;
4526fa5ffb5Skaashoek }
4536fa5ffb5Skaashoek 
454eaea18cbSrsc //PAGEBREAK!
455bcca6c6bSrsc // Directories
456bcca6c6bSrsc 
457eaea18cbSrsc int
458fbf91039Srsc namecmp(const char *s, const char *t)
459fbf91039Srsc {
460766ba5ccSrsc   return strncmp(s, t, DIRSIZ);
461fbf91039Srsc }
462fbf91039Srsc 
463bcca6c6bSrsc // Look for a directory entry in a directory.
464eaea18cbSrsc // If found, set *poff to byte offset of entry.
46520365348Srtm // Caller must have already locked dp.
46607090dd7Srsc struct inode*
467fbf91039Srsc dirlookup(struct inode *dp, char *name, uint *poff)
468bcca6c6bSrsc {
469f32f3638Srsc   uint off, inum;
470bcca6c6bSrsc   struct buf *bp;
471bcca6c6bSrsc   struct dirent *de;
472bcca6c6bSrsc 
473bcca6c6bSrsc   if(dp->type != T_DIR)
47420365348Srtm     panic("dirlookup not DIR");
475bcca6c6bSrsc 
476bcca6c6bSrsc   for(off = 0; off < dp->size; off += BSIZE){
477bcca6c6bSrsc     bp = bread(dp->dev, bmap(dp, off / BSIZE, 0));
478bcca6c6bSrsc     for(de = (struct dirent*) bp->data;
479bcca6c6bSrsc         de < (struct dirent*) (bp->data + BSIZE);
480bcca6c6bSrsc         de++){
481bcca6c6bSrsc       if(de->inum == 0)
482bcca6c6bSrsc         continue;
483fbf91039Srsc       if(namecmp(name, de->name) == 0){
484bcca6c6bSrsc         // entry matches path element
485e2a620daSrsc         if(poff)
486bcca6c6bSrsc           *poff = off + (uchar*)de - bp->data;
487f32f3638Srsc         inum = de->inum;
488bcca6c6bSrsc         brelse(bp);
489f32f3638Srsc         return iget(dp->dev, inum);
490f32f3638Srsc       }
491f32f3638Srsc     }
492f32f3638Srsc     brelse(bp);
493f32f3638Srsc   }
494bcca6c6bSrsc   return 0;
495bcca6c6bSrsc }
496bcca6c6bSrsc 
497bcca6c6bSrsc // Write a new directory entry (name, ino) into the directory dp.
498eaea18cbSrsc int
499fbf91039Srsc dirlink(struct inode *dp, char *name, uint ino)
500bcca6c6bSrsc {
501e2a620daSrsc   int off;
502bcca6c6bSrsc   struct dirent de;
50307090dd7Srsc   struct inode *ip;
504f32f3638Srsc 
505eaea18cbSrsc   // Check that name is not present.
50607090dd7Srsc   if((ip = dirlookup(dp, name, 0)) != 0){
50707090dd7Srsc     iput(ip);
508f32f3638Srsc     return -1;
509f32f3638Srsc   }
510bcca6c6bSrsc 
511bcca6c6bSrsc   // Look for an empty dirent.
512bcca6c6bSrsc   for(off = 0; off < dp->size; off += sizeof(de)){
513bcca6c6bSrsc     if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
514bcca6c6bSrsc       panic("dirwrite read");
515bcca6c6bSrsc     if(de.inum == 0)
516bcca6c6bSrsc       break;
517bcca6c6bSrsc   }
518bcca6c6bSrsc 
519766ba5ccSrsc   strncpy(de.name, name, DIRSIZ);
520bcca6c6bSrsc   de.inum = ino;
521bcca6c6bSrsc   if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
522bcca6c6bSrsc     panic("dirwrite");
523f32f3638Srsc 
524f32f3638Srsc   return 0;
525bcca6c6bSrsc }
526bcca6c6bSrsc 
527*8d2e9a48Srsc //PAGEBREAK!
528bcca6c6bSrsc // Paths
529bcca6c6bSrsc 
530eaea18cbSrsc // Copy the next path element from path into name.
531eaea18cbSrsc // Return a pointer to the element following the copied one.
532eaea18cbSrsc // The returned path has no leading slashes,
533eaea18cbSrsc // so the caller can check *path=='\0' to see if the name is the last one.
534eaea18cbSrsc // If no name to remove, return 0.
535ab5c2dbbSrsc //
536ab5c2dbbSrsc // Examples:
537eaea18cbSrsc //   skipelem("a/bb/c", name) = "bb/c", setting name = "a"
53820365348Srtm //   skipelem("///a/bb", name) = "bb", setting name="a"
539eaea18cbSrsc //   skipelem("", name) = skipelem("////", name) = 0
540ab5c2dbbSrsc //
541ab5c2dbbSrsc static char*
542fbf91039Srsc skipelem(char *path, char *name)
543ab5c2dbbSrsc {
544fbf91039Srsc   char *s;
545fbf91039Srsc   int len;
546fbf91039Srsc 
547ab5c2dbbSrsc   while(*path == '/')
548ab5c2dbbSrsc     path++;
549ab5c2dbbSrsc   if(*path == 0)
550ab5c2dbbSrsc     return 0;
551fbf91039Srsc   s = path;
552ab5c2dbbSrsc   while(*path != '/' && *path != 0)
553ab5c2dbbSrsc     path++;
554fbf91039Srsc   len = path - s;
555fbf91039Srsc   if(len >= DIRSIZ)
556fbf91039Srsc     memmove(name, s, DIRSIZ);
557fbf91039Srsc   else{
558fbf91039Srsc     memmove(name, s, len);
559fbf91039Srsc     name[len] = 0;
560fbf91039Srsc   }
561ab5c2dbbSrsc   while(*path == '/')
562ab5c2dbbSrsc     path++;
563ab5c2dbbSrsc   return path;
564ab5c2dbbSrsc }
565ab5c2dbbSrsc 
566*8d2e9a48Srsc static struct inode* _namei(char*, int, char*);
567*8d2e9a48Srsc 
568*8d2e9a48Srsc struct inode*
569*8d2e9a48Srsc namei(char *path)
570*8d2e9a48Srsc {
571*8d2e9a48Srsc   char name[DIRSIZ];
572*8d2e9a48Srsc   return _namei(path, 0, name);
573*8d2e9a48Srsc }
574*8d2e9a48Srsc 
575*8d2e9a48Srsc struct inode*
576*8d2e9a48Srsc nameiparent(char *path, char *name)
577*8d2e9a48Srsc {
578*8d2e9a48Srsc   return _namei(path, 1, name);
579*8d2e9a48Srsc }
580*8d2e9a48Srsc 
581eaea18cbSrsc // Look up and return the inode for a path name.
582eaea18cbSrsc // If parent is set, return the inode for the parent
583eaea18cbSrsc // and write the final path element to name, which
584eaea18cbSrsc // should have room for DIRSIZ bytes.
58507090dd7Srsc static struct inode*
586fbf91039Srsc _namei(char *path, int parent, char *name)
5879d3fb671Srtm {
58807090dd7Srsc   struct inode *ip, *next;
589f32f3638Srsc   uint off;
5909d3fb671Srtm 
591ab5c2dbbSrsc   if(*path == '/')
59207090dd7Srsc     ip = iget(ROOTDEV, 1);
593f32f3638Srsc   else
59407090dd7Srsc     ip = idup(cp->cwd);
5959d3fb671Srtm 
596fbf91039Srsc   while((path = skipelem(path, name)) != 0){
59707090dd7Srsc     ilock(ip);
59807090dd7Srsc     if(ip->type != T_DIR){
59907090dd7Srsc       iunlockput(ip);
600eaea18cbSrsc       return 0;
601eaea18cbSrsc     }
602ab5c2dbbSrsc 
603e2a620daSrsc     if(parent && *path == '\0'){
604e2a620daSrsc       // Stop one level early.
60507090dd7Srsc       iunlock(ip);
60607090dd7Srsc       return ip;
607ab5c2dbbSrsc     }
608ab5c2dbbSrsc 
60907090dd7Srsc     if((next = dirlookup(ip, name, &off)) == 0){
61007090dd7Srsc       iunlockput(ip);
611eaea18cbSrsc       return 0;
612eaea18cbSrsc     }
61307090dd7Srsc     iunlockput(ip);
61407090dd7Srsc     ip = next;
615ab5c2dbbSrsc   }
61620365348Srtm   if(parent){
61707090dd7Srsc     iput(ip);
6185051da6dSrtm     return 0;
61920365348Srtm   }
62007090dd7Srsc   return ip;
6210633b971Skaashoek }
6229d3fb671Srtm 
623