diff --git a/kernel/driver/buf.c b/kernel/driver/buf.c index 891db33f0a59c905d9ee41cfaf0cd3f2fc83cedd..50469ae137efdc8450bf4327872d6e613931e071 100755 --- a/kernel/driver/buf.c +++ b/kernel/driver/buf.c @@ -1,108 +1,108 @@ -#include "buf.h" -#include "../sdcard/sd.h" -#include "../sdcard/disk.h" -#include "../lock/sleeplock.h" -#include "../lock/spinlock.h" -#include "../libs/print.h" - - - -struct { - struct spinlock lock; - struct buf buf[BUFNUM]; - struct buf head; -} bcache; - -void binit() -{ - struct buf *b; - bcache.head.prev = &bcache.head; - bcache.head.next = &bcache.head; - for(b = bcache.buf;b < bcache.buf+BUFNUM;b++){ - b->refcnt = 0; - b->sectorno = ~0; - b->dev = ~0; - b->next = bcache.head.next; - b->prev = &bcache.head; - createsleeplock(&b->lock,"buffer"); - bcache.head.next->prev = b; - bcache.head.next = b; - } - printf("binit!\n"); -} - -static struct buf* bget(uint dev,uint sectorno) -{ - struct buf *b; - lock(&bcache.lock); - for(b = bcache.head.next;b != &bcache.head;b = b->next){ - if(b->dev == dev && b->sectorno == sectorno){ - b->refcnt++; - unlock(&bcache.lock); - locksleep(&b->lock); - return b; - } - } - for(b = bcache.head.prev;b != &bcache.head;b = b->prev){ - if(b->refcnt == 0){ - b->dev = dev; - b->sectorno = sectorno; - b->valid = 0; - b->refcnt = 1; - unlock(&bcache.lock); - locksleep(&b->lock); - return b; - } - } - panic("bget: no buffers!"); - return NULL; -} - -struct buf* bread(uint dev,uint sectorno) -{ - struct buf *b; - b = bget(dev,sectorno); - if(b->valid == 0){ - disk_read(b); - b->valid = 1; - } - return b; -} - -void bwrite(struct buf *b) -{ - if(!holdingsleep(&b->lock)){ - panic("bwrite"); - } - disk_write(b); -} - -void brelse(struct buf *b){ - if(!holdingsleep(&b->lock)){ - panic("panic:brelse!"); - } - unlocksleep(&b->lock); - lock(&bcache.lock); - b->refcnt--; - if(b->refcnt == 0){ - b->next->prev = b->prev; - b->prev->next = b->next; - b->next = bcache.head.next; - b->prev = &bcache.head; - bcache.head.next->prev = b; - bcache.head.next = b; - } - unlock(&bcache.lock); -} - -void bpin(struct buf *b){ - lock(&bcache.lock); - b->refcnt++; - unlock(&bcache.lock); -} - -void bunpin(struct buf *b){ - lock(&bcache.lock); - b->refcnt--; - unlock(&bcache.lock); -} +#include "buf.h" +#include "../sdcard/sd.h" +#include "../sdcard/disk.h" +#include "../lock/sleeplock.h" +#include "../lock/spinlock.h" +#include "../libs/print.h" +//123 + + +struct { + struct spinlock lock; + struct buf buf[BUFNUM]; + struct buf head; +} bcache; + +void binit() +{ + struct buf *b; + bcache.head.prev = &bcache.head; + bcache.head.next = &bcache.head; + for(b = bcache.buf;b < bcache.buf+BUFNUM;b++){ + b->refcnt = 0; + b->sectorno = ~0; + b->dev = ~0; + b->next = bcache.head.next; + b->prev = &bcache.head; + createsleeplock(&b->lock,"buffer"); + bcache.head.next->prev = b; + bcache.head.next = b; + } + printf("binit!\n"); +} + +static struct buf* bget(uint dev,uint sectorno) +{ + struct buf *b; + lock(&bcache.lock); + for(b = bcache.head.next;b != &bcache.head;b = b->next){ + if(b->dev == dev && b->sectorno == sectorno){ + b->refcnt++; + unlock(&bcache.lock); + locksleep(&b->lock); + return b; + } + } + for(b = bcache.head.prev;b != &bcache.head;b = b->prev){ + if(b->refcnt == 0){ + b->dev = dev; + b->sectorno = sectorno; + b->valid = 0; + b->refcnt = 1; + unlock(&bcache.lock); + locksleep(&b->lock); + return b; + } + } + panic("bget: no buffers!"); + return NULL; +} + +struct buf* bread(uint dev,uint sectorno) +{ + struct buf *b; + b = bget(dev,sectorno); + if(b->valid == 0){ + disk_read(b); + b->valid = 1; + } + return b; +} + +void bwrite(struct buf *b) +{ + if(!holdingsleep(&b->lock)){ + panic("bwrite"); + } + disk_write(b); +} + +void brelse(struct buf *b){ + if(!holdingsleep(&b->lock)){ + panic("panic:brelse!"); + } + unlocksleep(&b->lock); + lock(&bcache.lock); + b->refcnt--; + if(b->refcnt == 0){ + b->next->prev = b->prev; + b->prev->next = b->next; + b->next = bcache.head.next; + b->prev = &bcache.head; + bcache.head.next->prev = b; + bcache.head.next = b; + } + unlock(&bcache.lock); +} + +void bpin(struct buf *b){ + lock(&bcache.lock); + b->refcnt++; + unlock(&bcache.lock); +} + +void bunpin(struct buf *b){ + lock(&bcache.lock); + b->refcnt--; + unlock(&bcache.lock); +}