1 /* The file system maintains a buffer cache to reduce the number of disk
2 * accesses needed. Whenever a read or write to the disk is done, a check is
3 * first made to see if the block is in the cache. This file manages the
6 * The entry points into this file are:
7 * get_block: request to fetch a block for reading or writing from cache
8 * put_block: return a block previously requested with get_block
9 * alloc_zone: allocate a new zone (to increase the length of a file)
10 * free_zone: release a zone (when a file is removed)
11 * invalidate: remove all the cache blocks on some device
14 * rw_block: read or write a block from the disk itself
18 #include <minix/com.h>
22 FORWARD
_PROTOTYPE( void rm_lru
, (struct buf
*bp
) );
23 FORWARD
_PROTOTYPE( int rw_block
, (struct buf
*, int) );
25 #define ENABLE_CACHE2 0
27 /*===========================================================================*
29 *===========================================================================*/
30 PUBLIC
struct buf
*get_block(dev
, block
, only_search
)
31 register dev_t dev
; /* on which device is the block? */
32 register block_t block
; /* which block is wanted? */
33 int only_search
; /* if NO_READ, don't read, else act normal */
35 /* Check to see if the requested block is in the block cache. If so, return
36 * a pointer to it. If not, evict some other block and fetch it (unless
37 * 'only_search' is 1). All the blocks in the cache that are not in use
38 * are linked together in a chain, with 'front' pointing to the least recently
39 * used block and 'rear' to the most recently used block. If 'only_search' is
40 * 1, the block being requested will be overwritten in its entirety, so it is
41 * only necessary to see if it is in the cache; if it is not, any free buffer
42 * will do. It is not necessary to actually read the block in from disk.
43 * If 'only_search' is PREFETCH, the block need not be read from the disk,
44 * and the device is not to be marked on the block, so callers can tell if
45 * the block returned is valid.
46 * In addition to the LRU chain, there is also a hash chain to link together
47 * blocks whose block numbers end with the same bit strings, for fast lookup.
51 register struct buf
*bp
, *prev_ptr
;
53 /* Search the hash chain for (dev, block). Do_read() can use
54 * get_block(NO_DEV ...) to get an unnamed block to fill with zeros when
55 * someone wants to read from a hole in a file, in which case this search
59 b
= (int) block
& HASH_MASK
;
61 while (bp
!= NIL_BUF
) {
62 if (bp
->b_blocknr
== block
&& bp
->b_dev
== dev
) {
63 /* Block needed has been found. */
64 if (bp
->b_count
== 0) rm_lru(bp
);
65 bp
->b_count
++; /* record that block is in use */
69 /* This block is not the one sought. */
70 bp
= bp
->b_hash
; /* move to next block on hash chain */
75 /* Desired block is not on available chain. Take oldest block ('front'). */
76 if ((bp
= front
) == NIL_BUF
) panic(__FILE__
,"all buffers in use", NR_BUFS
);
79 /* Remove the block that was just taken from its hash chain. */
80 b
= (int) bp
->b_blocknr
& HASH_MASK
;
81 prev_ptr
= buf_hash
[b
];
83 buf_hash
[b
] = bp
->b_hash
;
85 /* The block just taken is not on the front of its hash chain. */
86 while (prev_ptr
->b_hash
!= NIL_BUF
)
87 if (prev_ptr
->b_hash
== bp
) {
88 prev_ptr
->b_hash
= bp
->b_hash
; /* found it */
91 prev_ptr
= prev_ptr
->b_hash
; /* keep looking */
95 /* If the block taken is dirty, make it clean by writing it to the disk.
96 * Avoid hysteresis by flushing all other dirty blocks for the same device.
98 if (bp
->b_dev
!= NO_DEV
) {
99 if (bp
->b_dirt
== DIRTY
) flushall(bp
->b_dev
);
105 /* Fill in block's parameters and add it to the hash chain where it goes. */
106 bp
->b_dev
= dev
; /* fill in device number */
107 bp
->b_blocknr
= block
; /* fill in block number */
108 bp
->b_count
++; /* record that block is being used */
109 b
= (int) bp
->b_blocknr
& HASH_MASK
;
110 bp
->b_hash
= buf_hash
[b
];
111 buf_hash
[b
] = bp
; /* add to hash list */
113 /* Go get the requested block unless searching or prefetching. */
116 if (get_block2(bp
, only_search
)) /* in 2nd level cache */;
119 if (only_search
== PREFETCH
) bp
->b_dev
= NO_DEV
;
121 if (only_search
== NORMAL
) {
122 rw_block(bp
, READING
);
125 return(bp
); /* return the newly acquired block */
128 /*===========================================================================*
130 *===========================================================================*/
131 PUBLIC
void put_block(bp
, block_type
)
132 register struct buf
*bp
; /* pointer to the buffer to be released */
133 int block_type
; /* INODE_BLOCK, DIRECTORY_BLOCK, or whatever */
135 /* Return a block to the list of available blocks. Depending on 'block_type'
136 * it may be put on the front or rear of the LRU chain. Blocks that are
137 * expected to be needed again shortly (e.g., partially full data blocks)
138 * go on the rear; blocks that are unlikely to be needed again shortly
139 * (e.g., full data blocks) go on the front. Blocks whose loss can hurt
140 * the integrity of the file system (e.g., inode blocks) are written to
141 * disk immediately if they are dirty.
143 if (bp
== NIL_BUF
) return; /* it is easier to check here than in caller */
145 bp
->b_count
--; /* there is one use fewer now */
146 if (bp
->b_count
!= 0) return; /* block is still in use */
148 bufs_in_use
--; /* one fewer block buffers in use */
150 /* Put this block back on the LRU chain. If the ONE_SHOT bit is set in
151 * 'block_type', the block is not likely to be needed again shortly, so put
152 * it on the front of the LRU chain where it will be the first one to be
153 * taken when a free buffer is needed later.
155 if (bp
->b_dev
== DEV_RAM
|| (block_type
& ONE_SHOT
)) {
156 /* Block probably won't be needed quickly. Put it on front of chain.
157 * It will be the next block to be evicted from the cache.
159 bp
->b_prev
= NIL_BUF
;
161 if (front
== NIL_BUF
)
162 rear
= bp
; /* LRU chain was empty */
168 /* Block probably will be needed quickly. Put it on rear of chain.
169 * It will not be evicted from the cache for a long time.
172 bp
->b_next
= NIL_BUF
;
180 /* Some blocks are so important (e.g., inodes, indirect blocks) that they
181 * should be written to the disk immediately to avoid messing up the file
182 * system in the event of a crash.
184 if ((block_type
& WRITE_IMMED
) && bp
->b_dirt
==DIRTY
&& bp
->b_dev
!= NO_DEV
) {
185 rw_block(bp
, WRITING
);
189 /*===========================================================================*
191 *===========================================================================*/
192 PUBLIC zone_t
alloc_zone(dev
, z
)
193 dev_t dev
; /* device where zone wanted */
194 zone_t z
; /* try to allocate new zone near this one */
196 /* Allocate a new zone on the indicated device and return its number. */
200 struct super_block
*sp
;
202 /* Note that the routine alloc_bit() returns 1 for the lowest possible
203 * zone, which corresponds to sp->s_firstdatazone. To convert a value
204 * between the bit number, 'b', used by alloc_bit() and the zone number, 'z',
205 * stored in the inode, use the formula:
206 * z = b + sp->s_firstdatazone - 1
207 * Alloc_bit() never returns 0, since this is used for NO_BIT (failure).
211 /* If z is 0, skip initial part of the map known to be fully in use. */
212 if (z
== sp
->s_firstdatazone
) {
215 bit
= (bit_t
) z
- (sp
->s_firstdatazone
- 1);
217 b
= alloc_bit(sp
, ZMAP
, bit
);
220 major
= (int) (sp
->s_dev
>> MAJOR
) & BYTE
;
221 minor
= (int) (sp
->s_dev
>> MINOR
) & BYTE
;
222 printf("No space on device %d/%d\n", major
, minor
);
225 if (z
== sp
->s_firstdatazone
) sp
->s_zsearch
= b
; /* for next time */
226 return(sp
->s_firstdatazone
- 1 + (zone_t
) b
);
229 /*===========================================================================*
231 *===========================================================================*/
232 PUBLIC
void free_zone(dev
, numb
)
233 dev_t dev
; /* device where zone located */
234 zone_t numb
; /* zone to be returned */
238 register struct super_block
*sp
;
241 /* Locate the appropriate super_block and return bit. */
243 if (numb
< sp
->s_firstdatazone
|| numb
>= sp
->s_zones
) return;
244 bit
= (bit_t
) (numb
- (sp
->s_firstdatazone
- 1));
245 free_bit(sp
, ZMAP
, bit
);
246 if (bit
< sp
->s_zsearch
) sp
->s_zsearch
= bit
;
249 /*===========================================================================*
251 *===========================================================================*/
252 PRIVATE
int rw_block(bp
, rw_flag
)
253 register struct buf
*bp
; /* buffer pointer */
254 int rw_flag
; /* READING or WRITING */
256 /* Read or write a disk block. This is the only routine in which actual disk
257 * I/O is invoked. If an error occurs, a message is printed here, but the error
258 * is not reported to the caller. If the error occurred while purging a block
259 * from the cache, it is not clear what the caller could do about it anyway.
266 block_size
= get_block_size(bp
->b_dev
);
268 if ( (dev
= bp
->b_dev
) != NO_DEV
) {
269 pos
= (off_t
) bp
->b_blocknr
* block_size
;
270 op
= (rw_flag
== READING
? DEV_READ
: DEV_WRITE
);
271 r
= block_dev_io(op
, dev
, SELF_E
, bp
->b_data
, pos
, block_size
, 0);
272 if (r
!= block_size
) {
273 if (r
>= 0) r
= END_OF_FILE
;
274 if (r
!= END_OF_FILE
)
275 printf("MFS(%d) I/O error on device %d/%d, block %ld\n",
276 SELF_E
, (dev
>>MAJOR
)&BYTE
, (dev
>>MINOR
)&BYTE
,
279 bp
->b_dev
= NO_DEV
; /* invalidate block */
281 /* Report read errors to interested parties. */
282 if (rw_flag
== READING
) rdwt_err
= r
;
291 /*===========================================================================*
293 *===========================================================================*/
294 PUBLIC
void invalidate(device
)
295 dev_t device
; /* device whose blocks are to be purged */
297 /* Remove all the blocks belonging to some device from the cache. */
299 register struct buf
*bp
;
301 for (bp
= &buf
[0]; bp
< &buf
[NR_BUFS
]; bp
++)
302 if (bp
->b_dev
== device
) bp
->b_dev
= NO_DEV
;
309 /*===========================================================================*
311 *===========================================================================*/
312 PUBLIC
void flushall(dev
)
313 dev_t dev
; /* device to flush */
315 /* Flush all dirty blocks for one device. */
317 register struct buf
*bp
;
318 static struct buf
*dirty
[NR_BUFS
]; /* static so it isn't on stack */
321 for (bp
= &buf
[0], ndirty
= 0; bp
< &buf
[NR_BUFS
]; bp
++)
322 if (bp
->b_dirt
== DIRTY
&& bp
->b_dev
== dev
) dirty
[ndirty
++] = bp
;
323 rw_scattered(dev
, dirty
, ndirty
, WRITING
);
326 /*===========================================================================*
328 *===========================================================================*/
329 PUBLIC
void rw_scattered(dev
, bufq
, bufqsize
, rw_flag
)
330 dev_t dev
; /* major-minor device number */
331 struct buf
**bufq
; /* pointer to array of buffers */
332 int bufqsize
; /* number of buffers */
333 int rw_flag
; /* READING or WRITING */
335 /* Read or write scattered data from a device. */
337 register struct buf
*bp
;
340 register iovec_t
*iop
;
341 static iovec_t iovec
[NR_IOREQS
]; /* static so it isn't on stack */
345 block_size
= get_block_size(dev
);
347 /* (Shell) sort buffers on b_blocknr. */
351 while (gap
<= bufqsize
);
354 for (j
= gap
; j
< bufqsize
; j
++) {
356 i
>= 0 && bufq
[i
]->b_blocknr
> bufq
[i
+ gap
]->b_blocknr
;
359 bufq
[i
] = bufq
[i
+ gap
];
365 /* Set up I/O vector and do I/O. The result of dev_io is OK if everything
366 * went fine, otherwise the error code for the first failed transfer.
368 while (bufqsize
> 0) {
369 for (j
= 0, iop
= iovec
; j
< NR_IOREQS
&& j
< bufqsize
; j
++, iop
++) {
371 if (bp
->b_blocknr
!= bufq
[0]->b_blocknr
+ j
) break;
372 iop
->iov_addr
= (vir_bytes
) bp
->b_data
;
373 iop
->iov_size
= block_size
;
375 r
= block_dev_io(rw_flag
== WRITING
? DEV_SCATTER
: DEV_GATHER
,
377 (off_t
) bufq
[0]->b_blocknr
* block_size
, j
, 0);
379 /* Harvest the results. Dev_io reports the first error it may have
380 * encountered, but we only care if it's the first block that failed.
382 for (i
= 0, iop
= iovec
; i
< j
; i
++, iop
++) {
384 if (iop
->iov_size
!= 0) {
385 /* Transfer failed. An error? Do we care? */
386 if (r
!= OK
&& i
== 0) {
388 "fs: I/O error on device %d/%d, block %lu\n",
389 (dev
>>MAJOR
)&BYTE
, (dev
>>MINOR
)&BYTE
,
391 bp
->b_dev
= NO_DEV
; /* invalidate block */
395 if (rw_flag
== READING
) {
396 bp
->b_dev
= dev
; /* validate block */
397 put_block(bp
, PARTIAL_DATA_BLOCK
);
404 if (rw_flag
== READING
) {
405 /* Don't bother reading more than the device is willing to
406 * give at this time. Don't forget to release those extras.
408 while (bufqsize
> 0) {
409 put_block(*bufq
++, PARTIAL_DATA_BLOCK
);
413 if (rw_flag
== WRITING
&& i
== 0) {
414 /* We're not making progress, this means we might keep
415 * looping. Buffers remain dirty if un-written. Buffers are
416 * lost if invalidate()d or LRU-removed while dirty. This
417 * is better than keeping unwritable blocks around forever..
424 /*===========================================================================*
426 *===========================================================================*/
427 PRIVATE
void rm_lru(bp
)
430 /* Remove a block from its LRU chain. */
431 struct buf
*next_ptr
, *prev_ptr
;
434 next_ptr
= bp
->b_next
; /* successor on LRU chain */
435 prev_ptr
= bp
->b_prev
; /* predecessor on LRU chain */
436 if (prev_ptr
!= NIL_BUF
)
437 prev_ptr
->b_next
= next_ptr
;
439 front
= next_ptr
; /* this block was at front of chain */
441 if (next_ptr
!= NIL_BUF
)
442 next_ptr
->b_prev
= prev_ptr
;
444 rear
= prev_ptr
; /* this block was at rear of chain */