xref: /xv6-public/fs.c (revision b7fed77b)
1a5fbfe41SRobert Morris // File system implementation.  Five layers:
2bcca6c6bSrsc //   + Blocks: allocator for raw disk blocks.
3a5fbfe41SRobert Morris //   + Log: crash recovery for multi-step updates.
4bcca6c6bSrsc //   + Files: inode allocator, reading, writing, metadata.
5bcca6c6bSrsc //   + Directories: inode with special contents (list of other inodes!)
6bcca6c6bSrsc //   + Names: paths like /usr/rtm/xv6/fs.c for convenient naming.
7bcca6c6bSrsc //
8eaea18cbSrsc // This file contains the low-level file system manipulation
9eaea18cbSrsc // routines.  The (higher-level) system call implementations
10eaea18cbSrsc // are in sysfile.c.
11bcca6c6bSrsc 
1211a9947fSrtm #include "types.h"
13558ab49fSrsc #include "defs.h"
1411a9947fSrtm #include "param.h"
15558ab49fSrsc #include "stat.h"
1611a9947fSrtm #include "mmu.h"
1711a9947fSrtm #include "proc.h"
1811a9947fSrtm #include "spinlock.h"
1911a9947fSrtm #include "fs.h"
20c24ac5d7SFrans Kaashoek #include "buf.h"
210aef8914SRuss Cox #include "file.h"
2211a9947fSrtm 
23bcca6c6bSrsc #define min(a, b) ((a) < (b) ? (a) : (b))
24fbf91039Srsc static void itrunc(struct inode*);
25*b7fed77bSFrans Kaashoek // there should be one superblock per disk device, but we run with
26*b7fed77bSFrans Kaashoek // only one device
27*b7fed77bSFrans Kaashoek struct superblock sb;
2811a9947fSrtm 
29a505fd66Srsc // Read the super block.
3013a96baeSFrans Kaashoek void
31a505fd66Srsc readsb(int dev, struct superblock *sb)
32a505fd66Srsc {
33a505fd66Srsc   struct buf *bp;
34a505fd66Srsc 
35a505fd66Srsc   bp = bread(dev, 1);
36a505fd66Srsc   memmove(sb, bp->data, sizeof(*sb));
37a505fd66Srsc   brelse(bp);
38a505fd66Srsc }
39a505fd66Srsc 
40a505fd66Srsc // Zero a block.
41a505fd66Srsc static void
42a505fd66Srsc bzero(int dev, int bno)
43a505fd66Srsc {
44a505fd66Srsc   struct buf *bp;
45a505fd66Srsc 
46a505fd66Srsc   bp = bread(dev, bno);
47a505fd66Srsc   memset(bp->data, 0, BSIZE);
485053dd6aSRobert Morris   log_write(bp);
49a505fd66Srsc   brelse(bp);
50a505fd66Srsc }
51a505fd66Srsc 
52bcca6c6bSrsc // Blocks.
535be0039cSrtm 
545053dd6aSRobert Morris // Allocate a zeroed disk block.
5524111398Skaashoek static uint
5624111398Skaashoek balloc(uint dev)
5724111398Skaashoek {
58a505fd66Srsc   int b, bi, m;
5924111398Skaashoek   struct buf *bp;
6024111398Skaashoek 
61a505fd66Srsc   bp = 0;
62a505fd66Srsc   for(b = 0; b < sb.size; b += BPB){
638320d61bSFrans Kaashoek     bp = bread(dev, BBLOCK(b, sb));
64a5fbfe41SRobert Morris     for(bi = 0; bi < BPB && b + bi < sb.size; bi++){
65a505fd66Srsc       m = 1 << (bi % 8);
66a505fd66Srsc       if((bp->data[bi/8] & m) == 0){  // Is block free?
67a5fbfe41SRobert Morris         bp->data[bi/8] |= m;  // Mark block in use.
6813a96baeSFrans Kaashoek         log_write(bp);
6924111398Skaashoek         brelse(bp);
705053dd6aSRobert Morris         bzero(dev, b + bi);
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   int bi, m;
8528d9ef04Skaashoek 
86a505fd66Srsc   readsb(dev, &sb);
878320d61bSFrans Kaashoek   bp = bread(dev, BBLOCK(b, sb));
8828d9ef04Skaashoek   bi = b % BPB;
89a505fd66Srsc   m = 1 << (bi % 8);
90902b13f5Srtm   if((bp->data[bi/8] & m) == 0)
91902b13f5Srtm     panic("freeing free block");
92a5fbfe41SRobert Morris   bp->data[bi/8] &= ~m;
9313a96baeSFrans Kaashoek   log_write(bp);
9428d9ef04Skaashoek   brelse(bp);
9528d9ef04Skaashoek }
9624111398Skaashoek 
976c34f97cSrsc // Inodes.
986c34f97cSrsc //
99a5fbfe41SRobert Morris // An inode describes a single unnamed file.
100a5fbfe41SRobert Morris // The inode disk structure holds metadata: the file's type,
101a5fbfe41SRobert Morris // its size, the number of links referring to it, and the
102a5fbfe41SRobert Morris // list of blocks holding the file's content.
103bcca6c6bSrsc //
1048320d61bSFrans Kaashoek // The inodes are laid out sequentially on disk at
1058320d61bSFrans Kaashoek // sb.startinode. Each inode has a number, indicating its
106a5fbfe41SRobert Morris // position on the disk.
107a5fbfe41SRobert Morris //
108a5fbfe41SRobert Morris // The kernel keeps a cache of in-use inodes in memory
109a5fbfe41SRobert Morris // to provide a place for synchronizing access
110a5fbfe41SRobert Morris // to inodes used by multiple processes. The cached
111a5fbfe41SRobert Morris // inodes include book-keeping information that is
112a5fbfe41SRobert Morris // not stored on disk: ip->ref and ip->flags.
113bcca6c6bSrsc //
11438eee5bcSRobert Morris // An inode and its in-memory represtative go through a
11538eee5bcSRobert Morris // sequence of states before they can be used by the
11638eee5bcSRobert Morris // rest of the file system code.
117bcca6c6bSrsc //
11838eee5bcSRobert Morris // * Allocation: an inode is allocated if its type (on disk)
11938eee5bcSRobert Morris //   is non-zero. ialloc() allocates, iput() frees if
12038eee5bcSRobert Morris //   the link count has fallen to zero.
121eaea18cbSrsc //
12238eee5bcSRobert Morris // * Referencing in cache: an entry in the inode cache
12338eee5bcSRobert Morris //   is free if ip->ref is zero. Otherwise ip->ref tracks
12438eee5bcSRobert Morris //   the number of in-memory pointers to the entry (open
12538eee5bcSRobert Morris //   files and current directories). iget() to find or
12638eee5bcSRobert Morris //   create a cache entry and increment its ref, iput()
12738eee5bcSRobert Morris //   to decrement ref.
128a5fbfe41SRobert Morris //
12938eee5bcSRobert Morris // * Valid: the information (type, size, &c) in an inode
13038eee5bcSRobert Morris //   cache entry is only correct when the I_VALID bit
13138eee5bcSRobert Morris //   is set in ip->flags. ilock() reads the inode from
13238eee5bcSRobert Morris //   the disk and sets I_VALID, while iput() clears
13338eee5bcSRobert Morris //   I_VALID if ip->ref has fallen to zero.
134a5fbfe41SRobert Morris //
13538eee5bcSRobert Morris // * Locked: file system code may only examine and modify
13638eee5bcSRobert Morris //   the information in an inode and its content if it
13738eee5bcSRobert Morris //   has first locked the inode. The I_BUSY flag indicates
13838eee5bcSRobert Morris //   that the inode is locked. ilock() sets I_BUSY,
13938eee5bcSRobert Morris //   while iunlock clears it.
14038eee5bcSRobert Morris //
14138eee5bcSRobert Morris // Thus a typical sequence is:
14238eee5bcSRobert Morris //   ip = iget(dev, inum)
14338eee5bcSRobert Morris //   ilock(ip)
14438eee5bcSRobert Morris //   ... examine and modify ip->xxx ...
14538eee5bcSRobert Morris //   iunlock(ip)
14638eee5bcSRobert Morris //   iput(ip)
14738eee5bcSRobert Morris //
14838eee5bcSRobert Morris // ilock() is separate from iget() so that system calls can
14938eee5bcSRobert Morris // get a long-term reference to an inode (as for an open file)
15038eee5bcSRobert Morris // and only lock it for short periods (e.g., in read()).
15138eee5bcSRobert Morris // The separation also helps avoid deadlock and races during
15238eee5bcSRobert Morris // pathname lookup. iget() increments ip->ref so that the inode
15338eee5bcSRobert Morris // stays cached and pointers to it remain valid.
15438eee5bcSRobert Morris //
15538eee5bcSRobert Morris // Many internal file system functions expect the caller to
15638eee5bcSRobert Morris // have locked the inodes involved; this lets callers create
15738eee5bcSRobert Morris // multi-step atomic operations.
158bcca6c6bSrsc 
159bcca6c6bSrsc struct {
160bcca6c6bSrsc   struct spinlock lock;
161bcca6c6bSrsc   struct inode inode[NINODE];
162bcca6c6bSrsc } icache;
163bcca6c6bSrsc 
164bcca6c6bSrsc void
1658320d61bSFrans Kaashoek iinit(int dev)
166bcca6c6bSrsc {
16734295f46Srsc   initlock(&icache.lock, "icache");
1688320d61bSFrans Kaashoek   readsb(dev, &sb);
169*b7fed77bSFrans Kaashoek   cprintf("sb: size %d nblocks %d ninodes %d nlog %d logstart %d\
170*b7fed77bSFrans Kaashoek           inodestart %d bmap start %d\n", sb.size, sb.nblocks,
171*b7fed77bSFrans Kaashoek           sb.ninodes, sb.nlog, sb.logstart, sb.inodestart,
172*b7fed77bSFrans Kaashoek           sb.bmapstart);
173bcca6c6bSrsc }
174bcca6c6bSrsc 
175f9a06440SRuss Cox static struct inode* iget(uint dev, uint inum);
176f9a06440SRuss Cox 
177f9a06440SRuss Cox //PAGEBREAK!
178f9a06440SRuss Cox // Allocate a new inode with the given type on device dev.
179a5fbfe41SRobert Morris // A free inode has a type of zero.
180f9a06440SRuss Cox struct inode*
181f9a06440SRuss Cox ialloc(uint dev, short type)
182f9a06440SRuss Cox {
183f9a06440SRuss Cox   int inum;
184f9a06440SRuss Cox   struct buf *bp;
185f9a06440SRuss Cox   struct dinode *dip;
186a5fbfe41SRobert Morris 
187a5fbfe41SRobert Morris   for(inum = 1; inum < sb.ninodes; inum++){
1888320d61bSFrans Kaashoek     bp = bread(dev, IBLOCK(inum, sb));
189f9a06440SRuss Cox     dip = (struct dinode*)bp->data + inum%IPB;
190f9a06440SRuss Cox     if(dip->type == 0){  // a free inode
191f9a06440SRuss Cox       memset(dip, 0, sizeof(*dip));
192f9a06440SRuss Cox       dip->type = type;
19313a96baeSFrans Kaashoek       log_write(bp);   // mark it allocated on the disk
194f9a06440SRuss Cox       brelse(bp);
195f9a06440SRuss Cox       return iget(dev, inum);
196f9a06440SRuss Cox     }
197f9a06440SRuss Cox     brelse(bp);
198f9a06440SRuss Cox   }
199f9a06440SRuss Cox   panic("ialloc: no inodes");
200f9a06440SRuss Cox }
201f9a06440SRuss Cox 
20238eee5bcSRobert Morris // Copy a modified in-memory inode to disk.
203f9a06440SRuss Cox void
204f9a06440SRuss Cox iupdate(struct inode *ip)
205f9a06440SRuss Cox {
206f9a06440SRuss Cox   struct buf *bp;
207f9a06440SRuss Cox   struct dinode *dip;
208f9a06440SRuss Cox 
2098320d61bSFrans Kaashoek   bp = bread(ip->dev, IBLOCK(ip->inum, sb));
210f9a06440SRuss Cox   dip = (struct dinode*)bp->data + ip->inum%IPB;
211f9a06440SRuss Cox   dip->type = ip->type;
212f9a06440SRuss Cox   dip->major = ip->major;
213f9a06440SRuss Cox   dip->minor = ip->minor;
214f9a06440SRuss Cox   dip->nlink = ip->nlink;
215f9a06440SRuss Cox   dip->size = ip->size;
216f9a06440SRuss Cox   memmove(dip->addrs, ip->addrs, sizeof(ip->addrs));
21713a96baeSFrans Kaashoek   log_write(bp);
218f9a06440SRuss Cox   brelse(bp);
219f9a06440SRuss Cox }
220f9a06440SRuss Cox 
221f5527388Srsc // Find the inode with number inum on device dev
22238eee5bcSRobert Morris // and return the in-memory copy. Does not lock
22338eee5bcSRobert Morris // the inode and does not read it from disk.
22407090dd7Srsc static struct inode*
22511a9947fSrtm iget(uint dev, uint inum)
22611a9947fSrtm {
227bcca6c6bSrsc   struct inode *ip, *empty;
22811a9947fSrtm 
229bcca6c6bSrsc   acquire(&icache.lock);
23011a9947fSrtm 
23138eee5bcSRobert Morris   // Is the inode already cached?
232bcca6c6bSrsc   empty = 0;
233bcca6c6bSrsc   for(ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++){
2340d6bbd31Srsc     if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){
2350d6bbd31Srsc       ip->ref++;
236bcca6c6bSrsc       release(&icache.lock);
23707090dd7Srsc       return ip;
23811a9947fSrtm     }
239bcca6c6bSrsc     if(empty == 0 && ip->ref == 0)    // Remember empty slot.
240bcca6c6bSrsc       empty = ip;
24111a9947fSrtm   }
24211a9947fSrtm 
24338eee5bcSRobert Morris   // Recycle an inode cache entry.
244bcca6c6bSrsc   if(empty == 0)
24532eea766Srsc     panic("iget: no inodes");
24611a9947fSrtm 
247bcca6c6bSrsc   ip = empty;
248bcca6c6bSrsc   ip->dev = dev;
249bcca6c6bSrsc   ip->inum = inum;
250bcca6c6bSrsc   ip->ref = 1;
251f32f3638Srsc   ip->flags = 0;
252bcca6c6bSrsc   release(&icache.lock);
25311a9947fSrtm 
25407090dd7Srsc   return ip;
255f32f3638Srsc }
256f32f3638Srsc 
257eaea18cbSrsc // Increment reference count for ip.
258eaea18cbSrsc // Returns ip to enable ip = idup(ip1) idiom.
25907090dd7Srsc struct inode*
26007090dd7Srsc idup(struct inode *ip)
261f32f3638Srsc {
262eaea18cbSrsc   acquire(&icache.lock);
263eaea18cbSrsc   ip->ref++;
264eaea18cbSrsc   release(&icache.lock);
26507090dd7Srsc   return ip;
266f32f3638Srsc }
267f32f3638Srsc 
2681ddfbbb1SFrans Kaashoek // Lock the given inode.
26938eee5bcSRobert Morris // Reads the inode from disk if necessary.
27007090dd7Srsc void
27107090dd7Srsc ilock(struct inode *ip)
272f32f3638Srsc {
273f32f3638Srsc   struct buf *bp;
274f32f3638Srsc   struct dinode *dip;
275eaea18cbSrsc 
27607090dd7Srsc   if(ip == 0 || ip->ref < 1)
27707090dd7Srsc     panic("ilock");
278f32f3638Srsc 
279f32f3638Srsc   acquire(&icache.lock);
2801ddfbbb1SFrans Kaashoek   while(ip->flags & I_BUSY)
2811ddfbbb1SFrans Kaashoek     sleep(ip, &icache.lock);
2821ddfbbb1SFrans Kaashoek   ip->flags |= I_BUSY;
283f32f3638Srsc   release(&icache.lock);
284f32f3638Srsc 
285f32f3638Srsc   if(!(ip->flags & I_VALID)){
2868320d61bSFrans Kaashoek     bp = bread(ip->dev, IBLOCK(ip->inum, sb));
2873341e30fSrsc     dip = (struct dinode*)bp->data + ip->inum%IPB;
288bcca6c6bSrsc     ip->type = dip->type;
289bcca6c6bSrsc     ip->major = dip->major;
290bcca6c6bSrsc     ip->minor = dip->minor;
291bcca6c6bSrsc     ip->nlink = dip->nlink;
292bcca6c6bSrsc     ip->size = dip->size;
293bcca6c6bSrsc     memmove(ip->addrs, dip->addrs, sizeof(ip->addrs));
29411a9947fSrtm     brelse(bp);
295f32f3638Srsc     ip->flags |= I_VALID;
296eaea18cbSrsc     if(ip->type == 0)
297eaea18cbSrsc       panic("ilock: no type");
29811a9947fSrtm   }
299bcca6c6bSrsc }
300bcca6c6bSrsc 
301bcca6c6bSrsc // Unlock the given inode.
30207090dd7Srsc void
303bcca6c6bSrsc iunlock(struct inode *ip)
304bcca6c6bSrsc {
3051ddfbbb1SFrans Kaashoek   if(ip == 0 || !(ip->flags & I_BUSY) || ip->ref < 1)
306bcca6c6bSrsc     panic("iunlock");
307bcca6c6bSrsc 
308bcca6c6bSrsc   acquire(&icache.lock);
3091ddfbbb1SFrans Kaashoek   ip->flags &= ~I_BUSY;
3101ddfbbb1SFrans Kaashoek   wakeup(ip);
311bcca6c6bSrsc   release(&icache.lock);
312bcca6c6bSrsc }
313bcca6c6bSrsc 
31438eee5bcSRobert Morris // Drop a reference to an in-memory inode.
31538eee5bcSRobert Morris // If that was the last reference, the inode cache entry can
31638eee5bcSRobert Morris // be recycled.
31738eee5bcSRobert Morris // If that was the last reference and the inode has no links
31838eee5bcSRobert Morris // to it, free the inode (and its content) on disk.
3192c565472SRobert Morris // All calls to iput() must be inside a transaction in
3202c565472SRobert Morris // case it has to free the inode.
321bcca6c6bSrsc void
32207090dd7Srsc iput(struct inode *ip)
323bcca6c6bSrsc {
324f32f3638Srsc   acquire(&icache.lock);
325f32f3638Srsc   if(ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0){
326e2b4583dSRobert Morris     // inode has no links and no other references: truncate and free.
3271ddfbbb1SFrans Kaashoek     if(ip->flags & I_BUSY)
328eaea18cbSrsc       panic("iput busy");
3291ddfbbb1SFrans Kaashoek     ip->flags |= I_BUSY;
330f32f3638Srsc     release(&icache.lock);
331f32f3638Srsc     itrunc(ip);
332f32f3638Srsc     ip->type = 0;
333f32f3638Srsc     iupdate(ip);
334f32f3638Srsc     acquire(&icache.lock);
335ce72cadbSrsc     ip->flags = 0;
3368970df0cSrsc     wakeup(ip);
337f32f3638Srsc   }
338f32f3638Srsc   ip->ref--;
339f32f3638Srsc   release(&icache.lock);
340bcca6c6bSrsc }
341bcca6c6bSrsc 
3427895178dSrsc // Common idiom: unlock, then put.
34307090dd7Srsc void
34407090dd7Srsc iunlockput(struct inode *ip)
34507090dd7Srsc {
34607090dd7Srsc   iunlock(ip);
34707090dd7Srsc   iput(ip);
34807090dd7Srsc }
34907090dd7Srsc 
3508d2e9a48Srsc //PAGEBREAK!
35138eee5bcSRobert Morris // Inode content
352bcca6c6bSrsc //
35338eee5bcSRobert Morris // The content (data) associated with each inode is stored
35438eee5bcSRobert Morris // in blocks on the disk. The first NDIRECT block numbers
3557895178dSrsc // are listed in ip->addrs[].  The next NINDIRECT blocks are
35638eee5bcSRobert Morris // listed in block ip->addrs[NDIRECT].
3579d3fb671Srtm 
358bb207a1dSrsc // Return the disk block address of the nth block in inode ip.
35913ae8808Srsc // If there is no such block, bmap allocates one.
360eaea18cbSrsc static uint
36113ae8808Srsc bmap(struct inode *ip, uint bn)
36222bac2cbSkaashoek {
363bcca6c6bSrsc   uint addr, *a;
364bcca6c6bSrsc   struct buf *bp;
36522bac2cbSkaashoek 
366ea2909b6Skaashoek   if(bn < NDIRECT){
36713ae8808Srsc     if((addr = ip->addrs[bn]) == 0)
368bcca6c6bSrsc       ip->addrs[bn] = addr = balloc(ip->dev);
369bcca6c6bSrsc     return addr;
370bcca6c6bSrsc   }
371bcca6c6bSrsc   bn -= NDIRECT;
372bcca6c6bSrsc 
373bcca6c6bSrsc   if(bn < NINDIRECT){
374bcca6c6bSrsc     // Load indirect block, allocating if necessary.
37513ae8808Srsc     if((addr = ip->addrs[NDIRECT]) == 0)
376ba6cd8a6Srsc       ip->addrs[NDIRECT] = addr = balloc(ip->dev);
377bcca6c6bSrsc     bp = bread(ip->dev, addr);
378bcca6c6bSrsc     a = (uint*)bp->data;
379bcca6c6bSrsc     if((addr = a[bn]) == 0){
380bcca6c6bSrsc       a[bn] = addr = balloc(ip->dev);
38113a96baeSFrans Kaashoek       log_write(bp);
382bcca6c6bSrsc     }
383bcca6c6bSrsc     brelse(bp);
384bcca6c6bSrsc     return addr;
38522bac2cbSkaashoek   }
38622bac2cbSkaashoek 
387bcca6c6bSrsc   panic("bmap: out of range");
388bcca6c6bSrsc }
389bcca6c6bSrsc 
390bcca6c6bSrsc // Truncate inode (discard contents).
39138eee5bcSRobert Morris // Only called when the inode has no links
39238eee5bcSRobert Morris // to it (no directory entries referring to it)
39338eee5bcSRobert Morris // and has no in-memory reference to it (is
39438eee5bcSRobert Morris // not an open file or current directory).
395fbf91039Srsc static void
3962aa4c3bcSrtm itrunc(struct inode *ip)
39722bac2cbSkaashoek {
398ea2909b6Skaashoek   int i, j;
399bcca6c6bSrsc   struct buf *bp;
4007d4aef6cSrsc   uint *a;
40122bac2cbSkaashoek 
402bcca6c6bSrsc   for(i = 0; i < NDIRECT; i++){
403bcca6c6bSrsc     if(ip->addrs[i]){
40422bac2cbSkaashoek       bfree(ip->dev, ip->addrs[i]);
40522bac2cbSkaashoek       ip->addrs[i] = 0;
40622bac2cbSkaashoek     }
40722bac2cbSkaashoek   }
408bcca6c6bSrsc 
409ba6cd8a6Srsc   if(ip->addrs[NDIRECT]){
410ba6cd8a6Srsc     bp = bread(ip->dev, ip->addrs[NDIRECT]);
411bcca6c6bSrsc     a = (uint*)bp->data;
412bcca6c6bSrsc     for(j = 0; j < NINDIRECT; j++){
413bcca6c6bSrsc       if(a[j])
414bcca6c6bSrsc         bfree(ip->dev, a[j]);
415bcca6c6bSrsc     }
416bcca6c6bSrsc     brelse(bp);
417ba6cd8a6Srsc     bfree(ip->dev, ip->addrs[NDIRECT]);
418ba6cd8a6Srsc     ip->addrs[NDIRECT] = 0;
419bcca6c6bSrsc   }
420bcca6c6bSrsc 
42122bac2cbSkaashoek   ip->size = 0;
42222bac2cbSkaashoek   iupdate(ip);
42322bac2cbSkaashoek }
42422bac2cbSkaashoek 
425bb207a1dSrsc // Copy stat information from inode.
426e958c538Skaashoek void
4271f544842Skaashoek stati(struct inode *ip, struct stat *st)
4281f544842Skaashoek {
4291dca3afbSrsc   st->dev = ip->dev;
4301dca3afbSrsc   st->ino = ip->inum;
4311dca3afbSrsc   st->type = ip->type;
4321dca3afbSrsc   st->nlink = ip->nlink;
4331dca3afbSrsc   st->size = ip->size;
4341f544842Skaashoek }
4351f544842Skaashoek 
436eaea18cbSrsc //PAGEBREAK!
437bb207a1dSrsc // Read data from inode.
438c59361f1Srtm int
43917a85657Srtm readi(struct inode *ip, char *dst, uint off, uint n)
440c59361f1Srtm {
441bcca6c6bSrsc   uint tot, m;
442c59361f1Srtm   struct buf *bp;
443c59361f1Srtm 
444939f9edeSkaashoek   if(ip->type == T_DEV){
4451dca3afbSrsc     if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read)
446939f9edeSkaashoek       return -1;
447d844f0f9Srsc     return devsw[ip->major].read(ip, dst, n);
448939f9edeSkaashoek   }
449939f9edeSkaashoek 
4507895178dSrsc   if(off > ip->size || off + n < off)
451bcca6c6bSrsc     return -1;
452bcca6c6bSrsc   if(off + n > ip->size)
453bcca6c6bSrsc     n = ip->size - off;
454bcca6c6bSrsc 
455bcca6c6bSrsc   for(tot=0; tot<n; tot+=m, off+=m, dst+=m){
45613ae8808Srsc     bp = bread(ip->dev, bmap(ip, off/BSIZE));
457bcca6c6bSrsc     m = min(n - tot, BSIZE - off%BSIZE);
458bcca6c6bSrsc     memmove(dst, bp->data + off%BSIZE, m);
459c59361f1Srtm     brelse(bp);
460c59361f1Srtm   }
461bcca6c6bSrsc   return n;
462ea2909b6Skaashoek }
463ea2909b6Skaashoek 
464eaea18cbSrsc // PAGEBREAK!
465bb207a1dSrsc // Write data to inode.
466ea2909b6Skaashoek int
467bcca6c6bSrsc writei(struct inode *ip, char *src, uint off, uint n)
4686fa5ffb5Skaashoek {
469bcca6c6bSrsc   uint tot, m;
4707d4aef6cSrsc   struct buf *bp;
4717d4aef6cSrsc 
4726fa5ffb5Skaashoek   if(ip->type == T_DEV){
4731dca3afbSrsc     if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write)
474939f9edeSkaashoek       return -1;
475d844f0f9Srsc     return devsw[ip->major].write(ip, src, n);
4767d4aef6cSrsc   }
4777d4aef6cSrsc 
4781ddfbbb1SFrans Kaashoek   if(off > ip->size || off + n < off)
479bcca6c6bSrsc     return -1;
4801ddfbbb1SFrans Kaashoek   if(off + n > MAXFILE*BSIZE)
4812e590463SRobert Morris     return -1;
482bcca6c6bSrsc 
483bcca6c6bSrsc   for(tot=0; tot<n; tot+=m, off+=m, src+=m){
48413ae8808Srsc     bp = bread(ip->dev, bmap(ip, off/BSIZE));
485bcca6c6bSrsc     m = min(n - tot, BSIZE - off%BSIZE);
486bcca6c6bSrsc     memmove(bp->data + off%BSIZE, src, m);
4872e590463SRobert Morris     log_write(bp);
48828d9ef04Skaashoek     brelse(bp);
48928d9ef04Skaashoek   }
490bcca6c6bSrsc 
491bcca6c6bSrsc   if(n > 0 && off > ip->size){
49248b82470Srsc     ip->size = off;
49328d9ef04Skaashoek     iupdate(ip);
49428d9ef04Skaashoek   }
495bcca6c6bSrsc   return n;
4966fa5ffb5Skaashoek }
4976fa5ffb5Skaashoek 
498eaea18cbSrsc //PAGEBREAK!
499bcca6c6bSrsc // Directories
500bcca6c6bSrsc 
501eaea18cbSrsc int
502fbf91039Srsc namecmp(const char *s, const char *t)
503fbf91039Srsc {
504766ba5ccSrsc   return strncmp(s, t, DIRSIZ);
505fbf91039Srsc }
506fbf91039Srsc 
507bcca6c6bSrsc // Look for a directory entry in a directory.
508eaea18cbSrsc // If found, set *poff to byte offset of entry.
50907090dd7Srsc struct inode*
510fbf91039Srsc dirlookup(struct inode *dp, char *name, uint *poff)
511bcca6c6bSrsc {
512f32f3638Srsc   uint off, inum;
513327cc21fSRobert Morris   struct dirent de;
514bcca6c6bSrsc 
515bcca6c6bSrsc   if(dp->type != T_DIR)
51620365348Srtm     panic("dirlookup not DIR");
517bcca6c6bSrsc 
518327cc21fSRobert Morris   for(off = 0; off < dp->size; off += sizeof(de)){
519327cc21fSRobert Morris     if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
520327cc21fSRobert Morris       panic("dirlink read");
521327cc21fSRobert Morris     if(de.inum == 0)
522bcca6c6bSrsc       continue;
523327cc21fSRobert Morris     if(namecmp(name, de.name) == 0){
524bcca6c6bSrsc       // entry matches path element
525e2a620daSrsc       if(poff)
526327cc21fSRobert Morris         *poff = off;
527327cc21fSRobert Morris       inum = de.inum;
528f32f3638Srsc       return iget(dp->dev, inum);
529f32f3638Srsc     }
530f32f3638Srsc   }
531327cc21fSRobert Morris 
532bcca6c6bSrsc   return 0;
533bcca6c6bSrsc }
534bcca6c6bSrsc 
53513ae8808Srsc // Write a new directory entry (name, inum) into the directory dp.
536eaea18cbSrsc int
53713ae8808Srsc dirlink(struct inode *dp, char *name, uint inum)
538bcca6c6bSrsc {
539e2a620daSrsc   int off;
540bcca6c6bSrsc   struct dirent de;
54107090dd7Srsc   struct inode *ip;
542f32f3638Srsc 
543eaea18cbSrsc   // Check that name is not present.
54407090dd7Srsc   if((ip = dirlookup(dp, name, 0)) != 0){
54507090dd7Srsc     iput(ip);
546f32f3638Srsc     return -1;
547f32f3638Srsc   }
548bcca6c6bSrsc 
549bcca6c6bSrsc   // Look for an empty dirent.
550bcca6c6bSrsc   for(off = 0; off < dp->size; off += sizeof(de)){
551bcca6c6bSrsc     if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
5527895178dSrsc       panic("dirlink read");
553bcca6c6bSrsc     if(de.inum == 0)
554bcca6c6bSrsc       break;
555bcca6c6bSrsc   }
556bcca6c6bSrsc 
557766ba5ccSrsc   strncpy(de.name, name, DIRSIZ);
55813ae8808Srsc   de.inum = inum;
559bcca6c6bSrsc   if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
5607895178dSrsc     panic("dirlink");
561f32f3638Srsc 
562f32f3638Srsc   return 0;
563bcca6c6bSrsc }
564bcca6c6bSrsc 
5658d2e9a48Srsc //PAGEBREAK!
566bcca6c6bSrsc // Paths
567bcca6c6bSrsc 
568eaea18cbSrsc // Copy the next path element from path into name.
569eaea18cbSrsc // Return a pointer to the element following the copied one.
570eaea18cbSrsc // The returned path has no leading slashes,
571eaea18cbSrsc // so the caller can check *path=='\0' to see if the name is the last one.
572eaea18cbSrsc // If no name to remove, return 0.
573ab5c2dbbSrsc //
574ab5c2dbbSrsc // Examples:
575eaea18cbSrsc //   skipelem("a/bb/c", name) = "bb/c", setting name = "a"
5767895178dSrsc //   skipelem("///a//bb", name) = "bb", setting name = "a"
57713ae8808Srsc //   skipelem("a", name) = "", setting name = "a"
578eaea18cbSrsc //   skipelem("", name) = skipelem("////", name) = 0
579ab5c2dbbSrsc //
580ab5c2dbbSrsc static char*
581fbf91039Srsc skipelem(char *path, char *name)
582ab5c2dbbSrsc {
583fbf91039Srsc   char *s;
584fbf91039Srsc   int len;
585fbf91039Srsc 
586ab5c2dbbSrsc   while(*path == '/')
587ab5c2dbbSrsc     path++;
588ab5c2dbbSrsc   if(*path == 0)
589ab5c2dbbSrsc     return 0;
590fbf91039Srsc   s = path;
591ab5c2dbbSrsc   while(*path != '/' && *path != 0)
592ab5c2dbbSrsc     path++;
593fbf91039Srsc   len = path - s;
594fbf91039Srsc   if(len >= DIRSIZ)
595fbf91039Srsc     memmove(name, s, DIRSIZ);
596fbf91039Srsc   else {
597fbf91039Srsc     memmove(name, s, len);
598fbf91039Srsc     name[len] = 0;
599fbf91039Srsc   }
600ab5c2dbbSrsc   while(*path == '/')
601ab5c2dbbSrsc     path++;
602ab5c2dbbSrsc   return path;
603ab5c2dbbSrsc }
604ab5c2dbbSrsc 
605eaea18cbSrsc // Look up and return the inode for a path name.
606bf2932a6Srsc // If parent != 0, return the inode for the parent and copy the final
607bf2932a6Srsc // path element into name, which must have room for DIRSIZ bytes.
6082c565472SRobert Morris // Must be called inside a transaction since it calls iput().
60907090dd7Srsc static struct inode*
610f9a06440SRuss Cox namex(char *path, int nameiparent, char *name)
6119d3fb671Srtm {
61207090dd7Srsc   struct inode *ip, *next;
6139d3fb671Srtm 
614ab5c2dbbSrsc   if(*path == '/')
6152ce40d70Srtm     ip = iget(ROOTDEV, ROOTINO);
616f32f3638Srsc   else
61748755214SRuss Cox     ip = idup(proc->cwd);
6189d3fb671Srtm 
619fbf91039Srsc   while((path = skipelem(path, name)) != 0){
62007090dd7Srsc     ilock(ip);
62107090dd7Srsc     if(ip->type != T_DIR){
62207090dd7Srsc       iunlockput(ip);
623eaea18cbSrsc       return 0;
624eaea18cbSrsc     }
625f9a06440SRuss Cox     if(nameiparent && *path == '\0'){
626e2a620daSrsc       // Stop one level early.
62707090dd7Srsc       iunlock(ip);
62807090dd7Srsc       return ip;
629ab5c2dbbSrsc     }
6307895178dSrsc     if((next = dirlookup(ip, name, 0)) == 0){
63107090dd7Srsc       iunlockput(ip);
632eaea18cbSrsc       return 0;
633eaea18cbSrsc     }
63407090dd7Srsc     iunlockput(ip);
63507090dd7Srsc     ip = next;
636ab5c2dbbSrsc   }
637f9a06440SRuss Cox   if(nameiparent){
63807090dd7Srsc     iput(ip);
6395051da6dSrtm     return 0;
64020365348Srtm   }
64107090dd7Srsc   return ip;
6420633b971Skaashoek }
6439d3fb671Srtm 
6447895178dSrsc struct inode*
6457895178dSrsc namei(char *path)
6467895178dSrsc {
6477895178dSrsc   char name[DIRSIZ];
64813ae8808Srsc   return namex(path, 0, name);
6497895178dSrsc }
650bf2932a6Srsc 
6517895178dSrsc struct inode*
6527895178dSrsc nameiparent(char *path, char *name)
6537895178dSrsc {
65413ae8808Srsc   return namex(path, 1, name);
6557895178dSrsc }
656