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;
283 ext2fs_free_mem(&cache->buf);
290 * Try to find a block in the cache. If the block is not found, and
291 * eldest is a non-zero pointer, then fill in eldest with the cache
292 * entry to that should be reused.
294 static struct unix_cache *find_cached_block(struct unix_private_data *data,
296 struct unix_cache **eldest)
298 struct unix_cache *cache, *unused_cache, *oldest_cache;
301 unused_cache = oldest_cache = 0;
302 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
303 if (!cache->in_use) {
305 unused_cache = cache;
308 if (cache->block == block) {
309 cache->access_time = ++data->access_time;
313 (cache->access_time < oldest_cache->access_time))
314 oldest_cache = cache;
317 *eldest = (unused_cache) ? unused_cache : oldest_cache;
322 * Reuse a particular cache entry for another block.
324 static void reuse_cache(io_channel channel, struct unix_private_data *data,
325 struct unix_cache *cache, unsigned long block)
327 if (cache->dirty && cache->in_use)
328 raw_write_blk(channel, data, cache->block, 1, cache->buf);
332 cache->block = block;
333 cache->access_time = ++data->access_time;
337 * Flush all of the blocks in the cache
339 static errcode_t flush_cached_blocks(io_channel channel,
340 struct unix_private_data *data,
344 struct unix_cache *cache;
345 errcode_t retval, retval2;
349 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
359 retval = raw_write_blk(channel, data,
360 cache->block, 1, cache->buf);
368 #endif /* NO_IO_CACHE */
370 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
372 io_channel io = NULL;
373 struct unix_private_data *data = NULL;
382 return EXT2_ET_BAD_DEVICE_NAME;
383 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
386 memset(io, 0, sizeof(struct struct_io_channel));
387 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
388 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
392 io->manager = unix_io_manager;
393 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
397 strcpy(io->name, name);
398 io->private_data = data;
399 io->block_size = 1024;
404 memset(data, 0, sizeof(struct unix_private_data));
405 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
407 if ((retval = alloc_cache(io, data)))
410 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
412 data->dev = open64(io->name, open_flags);
414 data->dev = open(io->name, open_flags);
423 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
424 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
426 #define RLIM_INFINITY (~0UL)
429 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
430 * block devices are wrongly getting hit by the filesize
431 * limit. This workaround isn't perfect, since it won't work
432 * if glibc wasn't built against 2.2 header files. (Sigh.)
435 if ((flags & IO_FLAG_RW) &&
437 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
438 (ut.release[2] == '4') && (ut.release[3] == '.') &&
439 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
440 (ut.release[5] < '8')) &&
441 (fstat(data->dev, &st) == 0) &&
442 (S_ISBLK(st.st_mode))) {
445 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
446 setrlimit(RLIMIT_FSIZE, &rlim);
447 getrlimit(RLIMIT_FSIZE, &rlim);
448 if (((unsigned long) rlim.rlim_cur) <
449 ((unsigned long) rlim.rlim_max)) {
450 rlim.rlim_cur = rlim.rlim_max;
451 setrlimit(RLIMIT_FSIZE, &rlim);
461 ext2fs_free_mem(&data);
463 ext2fs_free_mem(&io);
467 static errcode_t unix_close(io_channel channel)
469 struct unix_private_data *data;
470 errcode_t retval = 0;
472 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
473 data = (struct unix_private_data *) channel->private_data;
474 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
476 if (--channel->refcount > 0)
480 retval = flush_cached_blocks(channel, data, 0);
483 if (close(data->dev) < 0)
487 ext2fs_free_mem(&channel->private_data);
488 ext2fs_free_mem(&channel->name);
489 ext2fs_free_mem(&channel);
493 static errcode_t unix_set_blksize(io_channel channel, int blksize)
495 struct unix_private_data *data;
498 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
499 data = (struct unix_private_data *) channel->private_data;
500 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
502 if (channel->block_size != blksize) {
504 if ((retval = flush_cached_blocks(channel, data, 0)))
508 channel->block_size = blksize;
510 if ((retval = alloc_cache(channel, data)))
517 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
518 int count, void *buf)
520 struct unix_private_data *data;
521 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
526 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
527 data = (struct unix_private_data *) channel->private_data;
528 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
531 return raw_read_blk(channel, data, block, count, buf);
534 * If we're doing an odd-sized read or a very large read,
535 * flush out the cache and then do a direct read.
537 if (count < 0 || count > WRITE_DIRECT_SIZE) {
538 if ((retval = flush_cached_blocks(channel, data, 0)))
540 return raw_read_blk(channel, data, block, count, buf);
545 /* If it's in the cache, use it! */
546 if ((cache = find_cached_block(data, block, &reuse[0]))) {
548 printf("Using cached block %d\n", block);
550 memcpy(cp, cache->buf, channel->block_size);
553 cp += channel->block_size;
557 * Find the number of uncached blocks so we can do a
558 * single read request
560 for (i=1; i < count; i++)
561 if (find_cached_block(data, block+i, &reuse[i]))
564 printf("Reading %d blocks starting at %d\n", i, block);
566 if ((retval = raw_read_blk(channel, data, block, i, cp)))
569 /* Save the results in the cache */
570 for (j=0; j < i; j++) {
573 reuse_cache(channel, data, cache, block++);
574 memcpy(cache->buf, cp, channel->block_size);
575 cp += channel->block_size;
579 #endif /* NO_IO_CACHE */
582 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
583 int count, const void *buf)
585 struct unix_private_data *data;
586 struct unix_cache *cache, *reuse;
587 errcode_t retval = 0;
591 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
592 data = (struct unix_private_data *) channel->private_data;
593 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
596 return raw_write_blk(channel, data, block, count, buf);
599 * If we're doing an odd-sized write or a very large write,
600 * flush out the cache completely and then do a direct write.
602 if (count < 0 || count > WRITE_DIRECT_SIZE) {
603 if ((retval = flush_cached_blocks(channel, data, 1)))
605 return raw_write_blk(channel, data, block, count, buf);
609 * For a moderate-sized multi-block write, first force a write
610 * if we're in write-through cache mode, and then fill the
611 * cache with the blocks.
613 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
615 retval = raw_write_blk(channel, data, block, count, buf);
619 cache = find_cached_block(data, block, &reuse);
622 reuse_cache(channel, data, cache, block);
624 memcpy(cache->buf, cp, channel->block_size);
625 cache->dirty = !writethrough;
628 cp += channel->block_size;
631 #endif /* NO_IO_CACHE */
634 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
635 int size, const void *buf)
637 struct unix_private_data *data;
638 errcode_t retval = 0;
641 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
642 data = (struct unix_private_data *) channel->private_data;
643 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
647 * Flush out the cache completely
649 if ((retval = flush_cached_blocks(channel, data, 1)))
653 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
656 actual = write(data->dev, buf, size);
658 return EXT2_ET_SHORT_WRITE;
664 * Flush data buffers to disk.
666 static errcode_t unix_flush(io_channel channel)
668 struct unix_private_data *data;
669 errcode_t retval = 0;
671 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
672 data = (struct unix_private_data *) channel->private_data;
673 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
676 retval = flush_cached_blocks(channel, data, 0);
682 static errcode_t unix_set_option(io_channel channel, const char *option,
685 struct unix_private_data *data;
689 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
690 data = (struct unix_private_data *) channel->private_data;
691 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
693 if (!strcmp(option, "offset")) {
695 return EXT2_ET_INVALID_ARGUMENT;
697 tmp = strtoul(arg, &end, 0);
699 return EXT2_ET_INVALID_ARGUMENT;
703 return EXT2_ET_INVALID_ARGUMENT;