8 bcinit(Bcache *bc, int f, int bsize)
13 * allocate space for all buffers
14 * point all buffers into outer space
20 for(b = bc->bb; b < &bc->bb[Nbcache]; b++){
25 b->data = (char *)malloc(bc->bsize);
35 * Find a buffer for block b. If it's dirty, write it out.
38 bcfind(Bcache *bc, ulong bno)
43 error("bcfind: Notabno");
47 * if we already have a buffer for this bno, use it
49 for(b = bc->bb; b < &bc->bb[Nbcache]; b++)
50 if(b->inuse && b->bno==bno)
54 * get least recently used block
59 * if dirty, write it out
62 if(bcwrite(bc, b) < 0)
63 warning("writing dirty page");
69 * allocate a buffer block for a block. it's guaranteed to be there till
70 * the next Nbcache bcread's.
73 bcalloc(Bcache *bc, ulong bno)
85 * read a block into a buffer cache. it's guaranteed to be there till
86 * the next Nbcache bcread's.
89 bcread(Bcache *bc, ulong bno)
95 if(b->bno!=bno || !b->inuse)
97 * read in the one we really want
99 if(bread(bc, bno, b->data) < 0){
109 * mark a page dirty, if it's already dirty force a write
111 * N.B: ordering is important.
114 bcmark(Bcache *bc, Bbuf *b)
132 * write out a page (and all preceding dirty ones)
135 bcwrite(Bcache *bc, Bbuf *b)
140 * write out all preceding pages
142 while(nb = bc->dfirst){
143 if(bwrite(bc, nb->bno, nb->data) < 0)
146 bc->dfirst = nb->next;
153 * write out this page
155 if(bwrite(bc, b->bno, b->data) < 0)
163 * write out all dirty pages (in order)
169 return bcwrite(bc, bc->dlast);
174 * read a block from disk
177 bread(Bcache *bc, ulong bno, void *buf)
179 uvlong x = (uvlong)bno * bc->bsize;
181 if(pread(bc->f, buf, bc->bsize, x) != bc->bsize)
187 * write a block to disk
190 bwrite(Bcache *bc, ulong bno, void *buf)
192 uvlong x = (uvlong)bno * bc->bsize;
194 if(pwrite(bc->f, buf, bc->bsize, x) != bc->bsize)