2 * unix_io.c --- This is the Unix (well, really POSIX) implementation
5 * Implements a one-block write-through cache.
7 * Includes support for Windows NT support under Cygwin.
9 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
10 * 2002 by Theodore Ts'o.
13 * This file may be redistributed under the terms of the GNU Public
29 #include <sys/utsname.h>
35 #include <sys/types.h>
37 #if HAVE_SYS_RESOURCE_H
38 #include <sys/resource.h>
45 * For checking structure magic numbers...
48 #define EXT2_CHECK_MAGIC(struct, code) \
49 if ((struct)->magic != (code)) return (code)
60 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
61 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
63 struct unix_private_data {
69 struct unix_cache cache[CACHE_SIZE];
72 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
73 static errcode_t unix_close(io_channel channel);
74 static errcode_t unix_set_blksize(io_channel channel, int blksize);
75 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
76 int count, void *data);
77 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
78 int count, const void *data);
79 static errcode_t unix_flush(io_channel channel);
80 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
81 int size, const void *data);
82 static errcode_t unix_set_option(io_channel channel, const char *option,
85 static void reuse_cache(io_channel channel, struct unix_private_data *data,
86 struct unix_cache *cache, unsigned long block);
88 /* __FreeBSD_kernel__ is defined by GNU/kFreeBSD - the FreeBSD kernel
89 * does not know buffered block devices - everything is raw. */
90 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
91 #define NEED_BOUNCE_BUFFER
93 #undef NEED_BOUNCE_BUFFER
96 static struct struct_io_manager struct_unix_manager = {
97 EXT2_ET_MAGIC_IO_MANAGER,
105 #ifdef NEED_BOUNCE_BUFFER
113 io_manager unix_io_manager = &struct_unix_manager;
116 * Here are the raw I/O functions
118 #ifndef NEED_BOUNCE_BUFFER
119 static errcode_t raw_read_blk(io_channel channel,
120 struct unix_private_data *data,
122 int count, void *buf)
126 ext2_loff_t location;
129 size = (count < 0) ? -count : count * channel->block_size;
130 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
131 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
132 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
135 actual = read(data->dev, buf, size);
136 if (actual != size) {
139 retval = EXT2_ET_SHORT_READ;
145 memset((char *) buf+actual, 0, size-actual);
146 if (channel->read_error)
147 retval = (channel->read_error)(channel, block, count, buf,
148 size, actual, retval);
151 #else /* NEED_BOUNCE_BUFFER */
153 * Windows and FreeBSD block devices only allow sector alignment IO in offset and size
155 static errcode_t raw_read_blk(io_channel channel,
156 struct unix_private_data *data,
158 int count, void *buf)
161 size_t size, alignsize, fragment;
162 ext2_loff_t location;
163 int total = 0, actual;
164 #define BLOCKALIGN 512
165 char sector[BLOCKALIGN];
167 size = (count < 0) ? -count : count * channel->block_size;
168 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
170 printf("count=%d, size=%d, block=%d, blk_size=%d, location=%lx\n",
171 count, size, block, channel->block_size, location);
173 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
174 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
177 fragment = size % BLOCKALIGN;
178 alignsize = size - fragment;
180 actual = read(data->dev, buf, alignsize);
181 if (actual != alignsize)
185 actual = read(data->dev, sector, BLOCKALIGN);
186 if (actual != BLOCKALIGN)
188 memcpy(buf+alignsize, sector, fragment);
195 retval = EXT2_ET_SHORT_READ;
198 memset((char *) buf+total, 0, size-actual);
199 if (channel->read_error)
200 retval = (channel->read_error)(channel, block, count, buf,
201 size, actual, retval);
206 static errcode_t raw_write_blk(io_channel channel,
207 struct unix_private_data *data,
209 int count, const void *buf)
212 ext2_loff_t location;
217 size = channel->block_size;
222 size = count * channel->block_size;
225 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
226 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
227 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
231 actual = write(data->dev, buf, size);
232 if (actual != size) {
233 retval = EXT2_ET_SHORT_WRITE;
239 if (channel->write_error)
240 retval = (channel->write_error)(channel, block, count, buf,
241 size, actual, retval);
247 * Here we implement the cache functions
250 /* Allocate the cache buffers */
251 static errcode_t alloc_cache(io_channel channel,
252 struct unix_private_data *data)
255 struct unix_cache *cache;
258 data->access_time = 0;
259 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
261 cache->access_time = 0;
264 if ((retval = ext2fs_get_mem(channel->block_size,
271 /* Free the cache buffers */
272 static void free_cache(struct unix_private_data *data)
274 struct unix_cache *cache;
277 data->access_time = 0;
278 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
280 cache->access_time = 0;
284 ext2fs_free_mem(&cache->buf);
291 * Try to find a block in the cache. If the block is not found, and
292 * eldest is a non-zero pointer, then fill in eldest with the cache
293 * entry to that should be reused.
295 static struct unix_cache *find_cached_block(struct unix_private_data *data,
297 struct unix_cache **eldest)
299 struct unix_cache *cache, *unused_cache, *oldest_cache;
302 unused_cache = oldest_cache = 0;
303 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
304 if (!cache->in_use) {
306 unused_cache = cache;
309 if (cache->block == block) {
310 cache->access_time = ++data->access_time;
314 (cache->access_time < oldest_cache->access_time))
315 oldest_cache = cache;
318 *eldest = (unused_cache) ? unused_cache : oldest_cache;
323 * Reuse a particular cache entry for another block.
325 static void reuse_cache(io_channel channel, struct unix_private_data *data,
326 struct unix_cache *cache, unsigned long block)
328 if (cache->dirty && cache->in_use)
329 raw_write_blk(channel, data, cache->block, 1, cache->buf);
333 cache->block = block;
334 cache->access_time = ++data->access_time;
338 * Flush all of the blocks in the cache
340 static errcode_t flush_cached_blocks(io_channel channel,
341 struct unix_private_data *data,
345 struct unix_cache *cache;
346 errcode_t retval, retval2;
350 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
360 retval = raw_write_blk(channel, data,
361 cache->block, 1, cache->buf);
369 #endif /* NO_IO_CACHE */
371 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
373 io_channel io = NULL;
374 struct unix_private_data *data = NULL;
383 return EXT2_ET_BAD_DEVICE_NAME;
384 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
387 memset(io, 0, sizeof(struct struct_io_channel));
388 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
389 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
393 io->manager = unix_io_manager;
394 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
398 strcpy(io->name, name);
399 io->private_data = data;
400 io->block_size = 1024;
405 memset(data, 0, sizeof(struct unix_private_data));
406 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
408 if ((retval = alloc_cache(io, data)))
411 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
413 data->dev = open64(io->name, open_flags);
415 data->dev = open(io->name, open_flags);
424 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
425 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
427 #define RLIM_INFINITY (~0UL)
430 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
431 * block devices are wrongly getting hit by the filesize
432 * limit. This workaround isn't perfect, since it won't work
433 * if glibc wasn't built against 2.2 header files. (Sigh.)
436 if ((flags & IO_FLAG_RW) &&
438 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
439 (ut.release[2] == '4') && (ut.release[3] == '.') &&
440 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
441 (ut.release[5] < '8')) &&
442 (fstat(data->dev, &st) == 0) &&
443 (S_ISBLK(st.st_mode))) {
446 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
447 setrlimit(RLIMIT_FSIZE, &rlim);
448 getrlimit(RLIMIT_FSIZE, &rlim);
449 if (((unsigned long) rlim.rlim_cur) <
450 ((unsigned long) rlim.rlim_max)) {
451 rlim.rlim_cur = rlim.rlim_max;
452 setrlimit(RLIMIT_FSIZE, &rlim);
462 ext2fs_free_mem(&data);
465 ext2fs_free_mem(&io);
469 static errcode_t unix_close(io_channel channel)
471 struct unix_private_data *data;
472 errcode_t retval = 0;
474 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
475 data = (struct unix_private_data *) channel->private_data;
476 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
478 if (--channel->refcount > 0)
482 retval = flush_cached_blocks(channel, data, 0);
485 if (close(data->dev) < 0)
489 ext2fs_free_mem(&channel->private_data);
491 ext2fs_free_mem(&channel->name);
492 ext2fs_free_mem(&channel);
496 static errcode_t unix_set_blksize(io_channel channel, int blksize)
498 struct unix_private_data *data;
501 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
502 data = (struct unix_private_data *) channel->private_data;
503 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
505 if (channel->block_size != blksize) {
507 if ((retval = flush_cached_blocks(channel, data, 0)))
511 channel->block_size = blksize;
513 if ((retval = alloc_cache(channel, data)))
520 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
521 int count, void *buf)
523 struct unix_private_data *data;
524 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
529 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
530 data = (struct unix_private_data *) channel->private_data;
531 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
534 return raw_read_blk(channel, data, block, count, buf);
537 * If we're doing an odd-sized read or a very large read,
538 * flush out the cache and then do a direct read.
540 if (count < 0 || count > WRITE_DIRECT_SIZE) {
541 if ((retval = flush_cached_blocks(channel, data, 0)))
543 return raw_read_blk(channel, data, block, count, buf);
548 /* If it's in the cache, use it! */
549 if ((cache = find_cached_block(data, block, &reuse[0]))) {
551 printf("Using cached block %d\n", block);
553 memcpy(cp, cache->buf, channel->block_size);
556 cp += channel->block_size;
560 * Find the number of uncached blocks so we can do a
561 * single read request
563 for (i=1; i < count; i++)
564 if (find_cached_block(data, block+i, &reuse[i]))
567 printf("Reading %d blocks starting at %d\n", i, block);
569 if ((retval = raw_read_blk(channel, data, block, i, cp)))
572 /* Save the results in the cache */
573 for (j=0; j < i; j++) {
576 reuse_cache(channel, data, cache, block++);
577 memcpy(cache->buf, cp, channel->block_size);
578 cp += channel->block_size;
582 #endif /* NO_IO_CACHE */
585 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
586 int count, const void *buf)
588 struct unix_private_data *data;
589 struct unix_cache *cache, *reuse;
590 errcode_t retval = 0;
594 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
595 data = (struct unix_private_data *) channel->private_data;
596 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
599 return raw_write_blk(channel, data, block, count, buf);
602 * If we're doing an odd-sized write or a very large write,
603 * flush out the cache completely and then do a direct write.
605 if (count < 0 || count > WRITE_DIRECT_SIZE) {
606 if ((retval = flush_cached_blocks(channel, data, 1)))
608 return raw_write_blk(channel, data, block, count, buf);
612 * For a moderate-sized multi-block write, first force a write
613 * if we're in write-through cache mode, and then fill the
614 * cache with the blocks.
616 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
618 retval = raw_write_blk(channel, data, block, count, buf);
622 cache = find_cached_block(data, block, &reuse);
625 reuse_cache(channel, data, cache, block);
627 memcpy(cache->buf, cp, channel->block_size);
628 cache->dirty = !writethrough;
631 cp += channel->block_size;
634 #endif /* NO_IO_CACHE */
637 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
638 int size, const void *buf)
640 struct unix_private_data *data;
641 errcode_t retval = 0;
644 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
645 data = (struct unix_private_data *) channel->private_data;
646 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
650 * Flush out the cache completely
652 if ((retval = flush_cached_blocks(channel, data, 1)))
656 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
659 actual = write(data->dev, buf, size);
661 return EXT2_ET_SHORT_WRITE;
667 * Flush data buffers to disk.
669 static errcode_t unix_flush(io_channel channel)
671 struct unix_private_data *data;
672 errcode_t retval = 0;
674 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
675 data = (struct unix_private_data *) channel->private_data;
676 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
679 retval = flush_cached_blocks(channel, data, 0);
685 static errcode_t unix_set_option(io_channel channel, const char *option,
688 struct unix_private_data *data;
692 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
693 data = (struct unix_private_data *) channel->private_data;
694 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
696 if (!strcmp(option, "offset")) {
698 return EXT2_ET_INVALID_ARGUMENT;
700 tmp = strtoul(arg, &end, 0);
702 return EXT2_ET_INVALID_ARGUMENT;
706 return EXT2_ET_INVALID_ARGUMENT;