2009-12-20 21:41:50 +01:00
|
|
|
#include "fs.h"
|
|
|
|
#include "buf.h"
|
|
|
|
#include "inode.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-03-07 15:46:21 +01:00
|
|
|
static struct buf *new_block(dev_t dev, pino_t inum);
|
2009-12-20 21:41:50 +01:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* buf_pool *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void buf_pool(void)
|
2009-12-20 21:41:50 +01:00
|
|
|
{
|
|
|
|
/* Initialize the buffer pool. */
|
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
front = NULL;
|
|
|
|
rear = NULL;
|
2009-12-20 21:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* get_block *
|
|
|
|
*===========================================================================*/
|
2013-03-07 15:46:21 +01:00
|
|
|
struct buf *get_block(dev_t dev, pino_t inum)
|
2009-12-20 21:41:50 +01:00
|
|
|
{
|
2010-04-13 12:58:41 +02:00
|
|
|
struct buf *bp = front;
|
2009-12-20 21:41:50 +01:00
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
while(bp != NULL) {
|
2012-02-13 16:28:04 +01:00
|
|
|
if (bp->b_dev == dev && bp->b_num == inum) {
|
|
|
|
bp->b_count++;
|
|
|
|
return(bp);
|
|
|
|
}
|
|
|
|
bp = bp->b_next;
|
2009-12-20 21:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Buffer was not found. Try to allocate a new one */
|
|
|
|
return new_block(dev, inum);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* new_block *
|
|
|
|
*===========================================================================*/
|
2013-03-07 15:46:21 +01:00
|
|
|
static struct buf *new_block(dev_t dev, pino_t inum)
|
2009-12-20 21:41:50 +01:00
|
|
|
{
|
|
|
|
/* Allocate a new buffer and add it to the double linked buffer list */
|
|
|
|
struct buf *bp;
|
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
bp = malloc(sizeof(struct buf));
|
2009-12-20 21:41:50 +01:00
|
|
|
if (bp == NULL) {
|
|
|
|
err_code = ENOSPC;
|
2012-02-13 16:28:04 +01:00
|
|
|
return(NULL);
|
2009-12-20 21:41:50 +01:00
|
|
|
}
|
|
|
|
bp->b_num = inum;
|
|
|
|
bp->b_dev = dev;
|
|
|
|
bp->b_bytes = 0;
|
|
|
|
bp->b_count = 1;
|
|
|
|
memset(bp->b_data, 0 , PIPE_BUF);
|
2012-02-13 16:28:04 +01:00
|
|
|
|
2009-12-20 21:41:50 +01:00
|
|
|
/* Add at the end of the buffer */
|
2010-05-10 15:26:00 +02:00
|
|
|
if (front == NULL) { /* Empty list? */
|
2012-02-13 16:28:04 +01:00
|
|
|
front = bp;
|
|
|
|
bp->b_prev = NULL;
|
2009-12-20 21:41:50 +01:00
|
|
|
} else {
|
2012-02-13 16:28:04 +01:00
|
|
|
rear->b_next = bp;
|
|
|
|
bp->b_prev = rear;
|
2009-12-20 21:41:50 +01:00
|
|
|
}
|
2010-05-10 15:26:00 +02:00
|
|
|
bp->b_next = NULL;
|
2009-12-20 21:41:50 +01:00
|
|
|
rear = bp;
|
|
|
|
|
|
|
|
return(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* put_block *
|
|
|
|
*===========================================================================*/
|
2013-03-07 15:46:21 +01:00
|
|
|
void put_block(dev_t dev, pino_t inum)
|
2009-12-20 21:41:50 +01:00
|
|
|
{
|
|
|
|
struct buf *bp;
|
|
|
|
|
|
|
|
bp = get_block(dev, inum);
|
2010-05-10 15:26:00 +02:00
|
|
|
if (bp == NULL) return; /* We didn't find the block. Nothing to put. */
|
2009-12-20 21:41:50 +01:00
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
bp->b_count--; /* Compensate for above 'get_block'. */
|
2009-12-20 21:41:50 +01:00
|
|
|
if (--bp->b_count > 0) return;
|
|
|
|
|
|
|
|
/* Cut bp out of the loop */
|
2010-05-10 15:26:00 +02:00
|
|
|
if (bp->b_prev == NULL)
|
2012-02-13 16:28:04 +01:00
|
|
|
front = bp->b_next;
|
2009-12-20 21:41:50 +01:00
|
|
|
else
|
2012-02-13 16:28:04 +01:00
|
|
|
bp->b_prev->b_next = bp->b_next;
|
2009-12-20 21:41:50 +01:00
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
if (bp->b_next == NULL)
|
2012-02-13 16:28:04 +01:00
|
|
|
rear = bp->b_prev;
|
2009-12-20 21:41:50 +01:00
|
|
|
else
|
2012-02-13 16:28:04 +01:00
|
|
|
bp->b_next->b_prev = bp->b_prev;
|
2009-12-20 21:41:50 +01:00
|
|
|
|
|
|
|
/* Buffer administration is done. Now it's safe to free up bp. */
|
|
|
|
free(bp);
|
|
|
|
}
|