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>
33 #include <sys/resource.h>
39 * For checking structure magic numbers...
42 #define EXT2_CHECK_MAGIC(struct, code) \
43 if ((struct)->magic != (code)) return (code)
54 #define WRITE_VIA_CACHE_SIZE 4 /* Must be smaller than CACHE_SIZE */
56 struct unix_private_data {
61 struct unix_cache cache[CACHE_SIZE];
64 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
65 static errcode_t unix_close(io_channel channel);
66 static errcode_t unix_set_blksize(io_channel channel, int blksize);
67 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
68 int count, void *data);
69 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
70 int count, const void *data);
71 static errcode_t unix_flush(io_channel channel);
72 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
73 int size, const void *data);
75 static struct struct_io_manager struct_unix_manager = {
76 EXT2_ET_MAGIC_IO_MANAGER,
87 io_manager unix_io_manager = &struct_unix_manager;
90 * Here are the raw I/O functions
92 static errcode_t raw_read_blk(io_channel channel,
93 struct unix_private_data *data,
102 size = (count < 0) ? -count : count * channel->block_size;
103 location = (ext2_loff_t) block * channel->block_size;
104 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
105 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
108 actual = read(data->dev, buf, size);
109 if (actual != size) {
112 retval = EXT2_ET_SHORT_READ;
118 memset((char *) buf+actual, 0, size-actual);
119 if (channel->read_error)
120 retval = (channel->read_error)(channel, block, count, buf,
121 size, actual, retval);
125 static errcode_t raw_write_blk(io_channel channel,
126 struct unix_private_data *data,
128 int count, const void *buf)
131 ext2_loff_t location;
136 size = channel->block_size;
141 size = count * channel->block_size;
144 location = (ext2_loff_t) block * channel->block_size;
145 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
146 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
150 actual = write(data->dev, buf, size);
151 if (actual != size) {
152 retval = EXT2_ET_SHORT_WRITE;
158 if (channel->write_error)
159 retval = (channel->write_error)(channel, block, count, buf,
160 size, actual, retval);
166 * Here we implement the cache functions
169 /* Allocate the cache buffers */
170 static errcode_t alloc_cache(io_channel channel,
171 struct unix_private_data *data)
174 struct unix_cache *cache;
177 data->access_time = 0;
178 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
180 cache->access_time = 0;
183 if ((retval = ext2fs_get_mem(channel->block_size,
184 (void **) &cache->buf)))
190 /* Free the cache buffers */
191 static void free_cache(io_channel channel,
192 struct unix_private_data *data)
194 struct unix_cache *cache;
197 data->access_time = 0;
198 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
200 cache->access_time = 0;
204 ext2fs_free_mem((void **) &cache->buf);
210 * Try to find a block in the cache. If get_cache is non-zero, then
211 * if the block isn't in the cache, evict the oldest block in the
212 * cache and create a new cache entry for the requested block.
214 static struct unix_cache *find_cached_block(io_channel channel,
215 struct unix_private_data *data,
219 struct unix_cache *cache, *unused_cache, *oldest_cache;
222 unused_cache = oldest_cache = 0;
223 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
224 if (!cache->in_use) {
225 unused_cache = cache;
228 if (cache->block == block) {
229 cache->access_time = ++data->access_time;
233 (cache->access_time < oldest_cache->access_time))
234 oldest_cache = cache;
240 * Try to allocate cache slot.
243 cache = unused_cache;
245 cache = oldest_cache;
247 raw_write_blk(channel, data,
248 cache->block, 1, cache->buf);
251 cache->block = block;
252 cache->access_time = ++data->access_time;
257 * Flush all of the blocks in the cache
259 static errcode_t flush_cached_blocks(io_channel channel,
260 struct unix_private_data *data,
264 struct unix_cache *cache;
265 errcode_t retval, retval2;
269 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
279 retval = raw_write_blk(channel, data,
280 cache->block, 1, cache->buf);
291 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
293 io_channel io = NULL;
294 struct unix_private_data *data = NULL;
300 return EXT2_ET_BAD_DEVICE_NAME;
301 retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
305 memset(io, 0, sizeof(struct struct_io_channel));
306 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
307 retval = ext2fs_get_mem(sizeof(struct unix_private_data),
312 io->manager = unix_io_manager;
313 retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
317 strcpy(io->name, name);
318 io->private_data = data;
319 io->block_size = 1024;
324 memset(data, 0, sizeof(struct unix_private_data));
325 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
327 if ((retval = alloc_cache(io, data)))
330 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
332 data->dev = open64(name, open_flags);
334 data->dev = open(name, open_flags);
341 * Work around a bug in 2.4.10+ kernels where writes to block
342 * devices are wrongly getting hit by the filesize limit.
344 if ((flags & IO_FLAG_RW) &&
345 (fstat(data->dev, &st) == 0) &&
346 (S_ISBLK(st.st_mode))) {
349 rlim.rlim_cur = rlim.rlim_max = ((unsigned long)(~0UL));
350 setrlimit(RLIMIT_FSIZE, &rlim);
351 getrlimit(RLIMIT_FSIZE, &rlim);
352 if (((unsigned long) rlim.rlim_cur) <
353 ((unsigned long) rlim.rlim_max)) {
354 rlim.rlim_cur = rlim.rlim_max;
355 setrlimit(RLIMIT_FSIZE, &rlim);
363 free_cache(io, data);
364 ext2fs_free_mem((void **) &data);
367 ext2fs_free_mem((void **) &io);
371 static errcode_t unix_close(io_channel channel)
373 struct unix_private_data *data;
374 errcode_t retval = 0;
376 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
377 data = (struct unix_private_data *) channel->private_data;
378 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
380 if (--channel->refcount > 0)
383 retval = flush_cached_blocks(channel, data, 0);
385 if (close(data->dev) < 0)
387 free_cache(channel, data);
388 if (channel->private_data)
389 ext2fs_free_mem((void **) &channel->private_data);
391 ext2fs_free_mem((void **) &channel->name);
392 ext2fs_free_mem((void **) &channel);
396 static errcode_t unix_set_blksize(io_channel channel, int blksize)
398 struct unix_private_data *data;
401 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
402 data = (struct unix_private_data *) channel->private_data;
403 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
405 if (channel->block_size != blksize) {
406 if ((retval = flush_cached_blocks(channel, data, 0)))
409 channel->block_size = blksize;
410 free_cache(channel, data);
411 if ((retval = alloc_cache(channel, data)))
418 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
419 int count, void *buf)
421 struct unix_private_data *data;
422 struct unix_cache *cache;
427 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
428 data = (struct unix_private_data *) channel->private_data;
429 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
432 * If we're doing an odd-sized read, flush out the cache and
433 * then do a direct read.
436 if ((retval = flush_cached_blocks(channel, data, 0)))
438 return raw_read_blk(channel, data, block, count, buf);
443 /* If it's in the cache, use it! */
444 if ((cache = find_cached_block(channel, data, block, 0))) {
446 printf("Using cached block %d\n", block);
448 memcpy(cp, cache->buf, channel->block_size);
451 cp += channel->block_size;
455 * Find the number of uncached blocks so we can do a
456 * single read request
458 for (i=1; i < count; i++)
459 if (find_cached_block(channel, data, block+i, 0))
462 printf("Reading %d blocks starting at %d\n", i, block);
464 if ((retval = raw_read_blk(channel, data, block, i, cp)))
467 /* Save the results in the cache */
468 for (j=0; j < i; j++) {
470 cache = find_cached_block(channel, data, block++, 1);
472 memcpy(cache->buf, cp, channel->block_size);
473 cp += channel->block_size;
479 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
480 int count, const void *buf)
482 struct unix_private_data *data;
483 struct unix_cache *cache;
484 errcode_t retval = 0, retval2;
488 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
489 data = (struct unix_private_data *) channel->private_data;
490 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
493 * If we're doing an odd-sized write or a very large write,
494 * flush out the cache completely and then do a direct write.
496 if (count < 0 || count > WRITE_VIA_CACHE_SIZE) {
497 if ((retval = flush_cached_blocks(channel, data, 1)))
499 return raw_write_blk(channel, data, block, count, buf);
503 * For a moderate-sized multi-block write, first force a write
504 * if we're in write-through cache mode, and then fill the
505 * cache with the blocks.
507 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
509 retval = raw_write_blk(channel, data, block, count, buf);
513 cache = find_cached_block(channel, data, block, 1);
516 * Oh shit, we couldn't get cache descriptor.
517 * Force the write directly.
519 if ((retval2 = raw_write_blk(channel, data, block,
523 memcpy(cache->buf, cp, channel->block_size);
524 cache->dirty = !writethrough;
528 cp += channel->block_size;
533 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
534 int size, const void *buf)
536 struct unix_private_data *data;
537 errcode_t retval = 0;
540 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
541 data = (struct unix_private_data *) channel->private_data;
542 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
545 * Flush out the cache completely
547 if ((retval = flush_cached_blocks(channel, data, 1)))
550 if (lseek(data->dev, offset, SEEK_SET) < 0)
553 actual = write(data->dev, buf, size);
555 return EXT2_ET_SHORT_WRITE;
561 * Flush data buffers to disk.
563 static errcode_t unix_flush(io_channel channel)
565 struct unix_private_data *data;
566 errcode_t retval = 0;
568 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
569 data = (struct unix_private_data *) channel->private_data;
570 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
572 retval = flush_cached_blocks(channel, data, 0);