xref: /xv6-public/bio.c (revision 7d7465a8)
1 // Buffer cache.
2 //
3 // The buffer cache is a linked list of buf structures holding
4 // cached copies of disk block contents.  Caching disk blocks
5 // in memory reduces the number of disk reads and also provides
6 // a synchronization point for disk blocks used by multiple processes.
7 //
8 // Interface:
9 // * To get a buffer for a particular disk block, call bread.
10 // * After changing buffer data, call bwrite to flush it to disk.
11 // * When done with the buffer, call brelse.
12 // * Do not use the buffer after calling brelse.
13 // * Only one process at a time can use a buffer,
14 //     so do not keep them longer than necessary.
15 //
16 // The implementation uses three state flags internally:
17 // * B_BUSY: the block has been returned from bread
18 //     and has not been passed back to brelse.
19 // * B_VALID: the buffer data has been initialized
20 //     with the associated disk block contents.
21 // * B_DIRTY: the buffer data has been modified
22 //     and needs to be written to disk.
23 
24 #include "types.h"
25 #include "defs.h"
26 #include "param.h"
27 #include "spinlock.h"
28 #include "buf.h"
29 
30 struct {
31   struct spinlock lock;
32   struct buf buf[NBUF];
33 
34   // Linked list of all buffers, through prev/next.
35   // head.next is most recently used.
36   struct buf head;
37 } bcache;
38 
39 void
40 binit(void)
41 {
42   struct buf *b;
43 
44   initlock(&bcache.lock, "bcache");
45 
46 //PAGEBREAK!
47   // Create linked list of buffers
48   bcache.head.prev = &bcache.head;
49   bcache.head.next = &bcache.head;
50   for(b = bcache.buf; b < bcache.buf+NBUF; b++){
51     b->next = bcache.head.next;
52     b->prev = &bcache.head;
53     b->dev = -1;
54     bcache.head.next->prev = b;
55     bcache.head.next = b;
56   }
57 }
58 
59 // Look through buffer cache for sector on device dev.
60 // If not found, allocate fresh block.
61 // In either case, return locked buffer.
62 static struct buf*
63 bget(uint dev, uint sector)
64 {
65   struct buf *b;
66 
67   acquire(&bcache.lock);
68 
69  loop:
70   // Try for cached block.
71   for(b = bcache.head.next; b != &bcache.head; b = b->next){
72     if(b->dev == dev && b->sector == sector){
73       if(!(b->flags & B_BUSY)){
74         b->flags |= B_BUSY;
75         release(&bcache.lock);
76         return b;
77       }
78       sleep(b, &bcache.lock);
79       goto loop;
80     }
81   }
82 
83   // Allocate fresh block.
84   for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
85     if((b->flags & B_BUSY) == 0){
86       b->dev = dev;
87       b->sector = sector;
88       b->flags = B_BUSY;
89       release(&bcache.lock);
90       return b;
91     }
92   }
93   panic("bget: no buffers");
94 }
95 
96 // Return a B_BUSY buf with the contents of the indicated disk sector.
97 struct buf*
98 bread(uint dev, uint sector)
99 {
100   struct buf *b;
101 
102   b = bget(dev, sector);
103   if(!(b->flags & B_VALID))
104     iderw(b);
105   return b;
106 }
107 
108 // Write b's contents to disk.  Must be locked.
109 void
110 bwrite(struct buf *b)
111 {
112   cprintf("bwrite sector %d\n", b->sector);
113   if((b->flags & B_BUSY) == 0)
114     panic("bwrite");
115   b->flags |= B_DIRTY;
116   iderw(b);
117 }
118 
119 // Release the buffer b.
120 void
121 brelse(struct buf *b)
122 {
123   if((b->flags & B_BUSY) == 0)
124     panic("brelse");
125 
126   acquire(&bcache.lock);
127 
128   b->next->prev = b->prev;
129   b->prev->next = b->next;
130   b->next = bcache.head.next;
131   b->prev = &bcache.head;
132   bcache.head.next->prev = b;
133   bcache.head.next = b;
134 
135   b->flags &= ~B_BUSY;
136   wakeup(b);
137 
138   release(&bcache.lock);
139 }
140 
141