2 * unix_io.c --- This is the Unix I/O interface to the I/O manager.
4 * Implements a one-block write-through cache.
6 * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
9 * This file may be redistributed under the terms of the GNU Public
14 #define _LARGEFILE_SOURCE
15 #define _LARGEFILE64_SOURCE
31 #include <sys/types.h>
38 * For checking structure magic numbers...
41 #define EXT2_CHECK_MAGIC(struct, code) \
42 if ((struct)->magic != (code)) return (code)
53 #define WRITE_VIA_CACHE_SIZE 4 /* Must be smaller than CACHE_SIZE */
55 struct unix_private_data {
60 struct unix_cache cache[CACHE_SIZE];
63 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
64 static errcode_t unix_close(io_channel channel);
65 static errcode_t unix_set_blksize(io_channel channel, int blksize);
66 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
67 int count, void *data);
68 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
69 int count, const void *data);
70 static errcode_t unix_flush(io_channel channel);
71 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
72 int size, const void *data);
74 static struct struct_io_manager struct_unix_manager = {
75 EXT2_ET_MAGIC_IO_MANAGER,
86 io_manager unix_io_manager = &struct_unix_manager;
89 * Here are the raw I/O functions
91 static errcode_t raw_read_blk(io_channel channel,
92 struct unix_private_data *data,
101 size = (count < 0) ? -count : count * channel->block_size;
102 location = (ext2_loff_t) block * channel->block_size;
103 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
104 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
107 actual = read(data->dev, buf, size);
108 if (actual != size) {
111 retval = EXT2_ET_SHORT_READ;
117 memset((char *) buf+actual, 0, size-actual);
118 if (channel->read_error)
119 retval = (channel->read_error)(channel, block, count, buf,
120 size, actual, retval);
124 static errcode_t raw_write_blk(io_channel channel,
125 struct unix_private_data *data,
127 int count, const void *buf)
130 ext2_loff_t location;
135 size = channel->block_size;
140 size = count * channel->block_size;
143 location = (ext2_loff_t) block * channel->block_size;
144 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
145 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
149 actual = write(data->dev, buf, size);
150 if (actual != size) {
151 retval = EXT2_ET_SHORT_WRITE;
157 if (channel->write_error)
158 retval = (channel->write_error)(channel, block, count, buf,
159 size, actual, retval);
165 * Here we implement the cache functions
168 /* Allocate the cache buffers */
169 static errcode_t alloc_cache(io_channel channel,
170 struct unix_private_data *data)
173 struct unix_cache *cache;
176 data->access_time = 0;
177 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
179 cache->access_time = 0;
182 if ((retval = ext2fs_get_mem(channel->block_size,
183 (void **) &cache->buf)))
189 /* Free the cache buffers */
190 static void free_cache(io_channel channel,
191 struct unix_private_data *data)
193 struct unix_cache *cache;
196 data->access_time = 0;
197 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
199 cache->access_time = 0;
203 ext2fs_free_mem((void **) &cache->buf);
209 * Try to find a block in the cache. If get_cache is non-zero, then
210 * if the block isn't in the cache, evict the oldest block in the
211 * cache and create a new cache entry for the requested block.
213 static struct unix_cache *find_cached_block(io_channel channel,
214 struct unix_private_data *data,
218 struct unix_cache *cache, *unused_cache, *oldest_cache;
221 unused_cache = oldest_cache = 0;
222 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
223 if (!cache->in_use) {
224 unused_cache = cache;
227 if (cache->block == block) {
228 cache->access_time = ++data->access_time;
232 (cache->access_time < oldest_cache->access_time))
233 oldest_cache = cache;
239 * Try to allocate cache slot.
242 cache = unused_cache;
244 cache = oldest_cache;
246 raw_write_blk(channel, data,
247 cache->block, 1, cache->buf);
250 cache->block = block;
251 cache->access_time = ++data->access_time;
256 * Flush all of the blocks in the cache
258 static errcode_t flush_cached_blocks(io_channel channel,
259 struct unix_private_data *data,
263 struct unix_cache *cache;
264 errcode_t retval, retval2;
268 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
278 retval = raw_write_blk(channel, data,
279 cache->block, 1, cache->buf);
290 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
292 io_channel io = NULL;
293 struct unix_private_data *data = NULL;
298 return EXT2_ET_BAD_DEVICE_NAME;
299 retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
303 memset(io, 0, sizeof(struct struct_io_channel));
304 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
305 retval = ext2fs_get_mem(sizeof(struct unix_private_data),
310 io->manager = unix_io_manager;
311 retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
315 strcpy(io->name, name);
316 io->private_data = data;
317 io->block_size = 1024;
322 memset(data, 0, sizeof(struct unix_private_data));
323 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
325 if ((retval = alloc_cache(io, data)))
328 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
330 data->dev = open64(name, open_flags);
332 data->dev = open(name, open_flags);
343 free_cache(io, data);
344 ext2fs_free_mem((void **) &data);
347 ext2fs_free_mem((void **) &io);
351 static errcode_t unix_close(io_channel channel)
353 struct unix_private_data *data;
354 errcode_t retval = 0;
356 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
357 data = (struct unix_private_data *) channel->private_data;
358 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
360 if (--channel->refcount > 0)
363 retval = flush_cached_blocks(channel, data, 0);
365 if (close(data->dev) < 0)
367 free_cache(channel, data);
368 if (channel->private_data)
369 ext2fs_free_mem((void **) &channel->private_data);
371 ext2fs_free_mem((void **) &channel->name);
372 ext2fs_free_mem((void **) &channel);
376 static errcode_t unix_set_blksize(io_channel channel, int blksize)
378 struct unix_private_data *data;
381 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
382 data = (struct unix_private_data *) channel->private_data;
383 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
385 if (channel->block_size != blksize) {
386 if ((retval = flush_cached_blocks(channel, data, 0)))
389 channel->block_size = blksize;
390 free_cache(channel, data);
391 if ((retval = alloc_cache(channel, data)))
398 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
399 int count, void *buf)
401 struct unix_private_data *data;
402 struct unix_cache *cache;
407 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
408 data = (struct unix_private_data *) channel->private_data;
409 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
412 * If we're doing an odd-sized read, flush out the cache and
413 * then do a direct read.
416 if ((retval = flush_cached_blocks(channel, data, 0)))
418 return raw_read_blk(channel, data, block, count, buf);
423 /* If it's in the cache, use it! */
424 if ((cache = find_cached_block(channel, data, block, 0))) {
426 printf("Using cached block %d\n", block);
428 memcpy(cp, cache->buf, channel->block_size);
431 cp += channel->block_size;
435 * Find the number of uncached blocks so we can do a
436 * single read request
438 for (i=1; i < count; i++)
439 if (find_cached_block(channel, data, block+i, 0))
442 printf("Reading %d blocks starting at %d\n", i, block);
444 if ((retval = raw_read_blk(channel, data, block, i, cp)))
447 /* Save the results in the cache */
448 for (j=0; j < i; j++) {
450 cache = find_cached_block(channel, data, block++, 1);
452 memcpy(cache->buf, cp, channel->block_size);
453 cp += channel->block_size;
459 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
460 int count, const void *buf)
462 struct unix_private_data *data;
463 struct unix_cache *cache;
464 errcode_t retval = 0, retval2;
468 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
469 data = (struct unix_private_data *) channel->private_data;
470 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
473 * If we're doing an odd-sized write or a very large write,
474 * flush out the cache completely and then do a direct write.
476 if (count < 0 || count > WRITE_VIA_CACHE_SIZE) {
477 if ((retval = flush_cached_blocks(channel, data, 1)))
479 return raw_write_blk(channel, data, block, count, buf);
483 * For a moderate-sized multi-block write, first force a write
484 * if we're in write-through cache mode, and then fill the
485 * cache with the blocks.
487 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
489 retval = raw_write_blk(channel, data, block, count, buf);
493 cache = find_cached_block(channel, data, block, 1);
496 * Oh shit, we couldn't get cache descriptor.
497 * Force the write directly.
499 if ((retval2 = raw_write_blk(channel, data, block,
503 memcpy(cache->buf, cp, channel->block_size);
504 cache->dirty = !writethrough;
508 cp += channel->block_size;
513 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
514 int size, const void *buf)
516 struct unix_private_data *data;
517 errcode_t retval = 0;
520 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
521 data = (struct unix_private_data *) channel->private_data;
522 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
525 * Flush out the cache completely
527 if ((retval = flush_cached_blocks(channel, data, 1)))
530 if (lseek(data->dev, offset, SEEK_SET) < 0)
533 actual = write(data->dev, buf, size);
535 return EXT2_ET_SHORT_WRITE;
541 * Flush data buffers to disk.
543 static errcode_t unix_flush(io_channel channel)
545 struct unix_private_data *data;
546 errcode_t retval = 0;
548 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
549 data = (struct unix_private_data *) channel->private_data;
550 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
552 retval = flush_cached_blocks(channel, data, 0);