packages: don't put oss on cd.
[minix.git] / servers / pfs / buffer.c
blobe745e419e7d6e6ef00c9895e19890d76706e19c3
1 #include "fs.h"
2 #include "buf.h"
3 #include "inode.h"
4 #include <sys/types.h>
5 #include <stdlib.h>
6 #include <string.h>
8 FORWARD _PROTOTYPE( struct buf *new_block, (dev_t dev, ino_t inum) );
10 /*===========================================================================*
11 * buf_pool *
12 *===========================================================================*/
13 PUBLIC void buf_pool(void)
15 /* Initialize the buffer pool. */
17 front = NULL;
18 rear = NULL;
23 /*===========================================================================*
24 * get_block *
25 *===========================================================================*/
26 PUBLIC struct buf *get_block(dev_t dev, ino_t inum)
28 struct buf *bp = front;
30 while(bp != NULL) {
31 if (bp->b_dev == dev && bp->b_num == inum) {
32 bp->b_count++;
33 return(bp);
35 bp = bp->b_next;
38 /* Buffer was not found. Try to allocate a new one */
39 return new_block(dev, inum);
43 /*===========================================================================*
44 * new_block *
45 *===========================================================================*/
46 PRIVATE struct buf *new_block(dev_t dev, ino_t inum)
48 /* Allocate a new buffer and add it to the double linked buffer list */
49 struct buf *bp;
51 bp = malloc(sizeof(struct buf));
52 if (bp == NULL) {
53 err_code = ENOSPC;
54 return(NULL);
56 bp->b_num = inum;
57 bp->b_dev = dev;
58 bp->b_bytes = 0;
59 bp->b_count = 1;
60 memset(bp->b_data, 0 , PIPE_BUF);
62 /* Add at the end of the buffer */
63 if (front == NULL) { /* Empty list? */
64 front = bp;
65 bp->b_prev = NULL;
66 } else {
67 rear->b_next = bp;
68 bp->b_prev = rear;
70 bp->b_next = NULL;
71 rear = bp;
73 return(bp);
77 /*===========================================================================*
78 * put_block *
79 *===========================================================================*/
80 PUBLIC void put_block(dev_t dev, ino_t inum)
82 struct buf *bp;
84 bp = get_block(dev, inum);
85 if (bp == NULL) return; /* We didn't find the block. Nothing to put. */
87 bp->b_count--; /* Compensate for above 'get_block'. */
88 if (--bp->b_count > 0) return;
90 /* Cut bp out of the loop */
91 if (bp->b_prev == NULL)
92 front = bp->b_next;
93 else
94 bp->b_prev->b_next = bp->b_next;
96 if (bp->b_next == NULL)
97 rear = bp->b_prev;
98 else
99 bp->b_next->b_prev = bp->b_prev;
101 /* Buffer administration is done. Now it's safe to free up bp. */
102 free(bp);