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 Library
14 * General Public License, version 2.
18 #define _LARGEFILE_SOURCE
19 #define _LARGEFILE64_SOURCE
33 #include <sys/utsname.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
38 #ifdef HAVE_SYS_MOUNT_H
39 #include <sys/mount.h>
45 #include <sys/types.h>
47 #if HAVE_SYS_RESOURCE_H
48 #include <sys/resource.h>
51 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
52 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
55 #if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET)
56 #define BLKSSZGET _IO(0x12,104)/* get block device sector size */
65 * For checking structure magic numbers...
68 #define EXT2_CHECK_MAGIC(struct, code) \
69 if ((struct)->magic != (code)) return (code)
80 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
81 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
83 struct unix_private_data {
90 struct unix_cache cache[CACHE_SIZE];
92 struct struct_io_stats io_stats;
95 #define IS_ALIGNED(n, align) ((((unsigned long) n) & \
96 ((unsigned long) ((align)-1))) == 0)
98 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
99 static errcode_t unix_close(io_channel channel);
100 static errcode_t unix_set_blksize(io_channel channel, int blksize);
101 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
102 int count, void *data);
103 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
104 int count, const void *data);
105 static errcode_t unix_flush(io_channel channel);
106 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
107 int size, const void *data);
108 static errcode_t unix_set_option(io_channel channel, const char *option,
110 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
112 static void reuse_cache(io_channel channel, struct unix_private_data *data,
113 struct unix_cache *cache, unsigned long long block);
114 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
115 int count, void *data);
116 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
117 int count, const void *data);
119 static struct struct_io_manager struct_unix_manager = {
120 EXT2_ET_MAGIC_IO_MANAGER,
135 io_manager unix_io_manager = &struct_unix_manager;
137 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
139 errcode_t retval = 0;
141 struct unix_private_data *data;
143 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
144 data = (struct unix_private_data *) channel->private_data;
145 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
148 *stats = &data->io_stats;
154 * Here are the raw I/O functions
156 static errcode_t raw_read_blk(io_channel channel,
157 struct unix_private_data *data,
158 unsigned long long block,
159 int count, void *buf)
163 ext2_loff_t location;
166 size = (count < 0) ? -count : count * channel->block_size;
167 data->io_stats.bytes_read += size;
168 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
169 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
170 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
173 if ((data->align == 0) ||
174 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
175 actual = read(data->dev, buf, size);
176 if (actual != size) {
180 retval = EXT2_ET_SHORT_READ;
187 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
188 (unsigned long) size);
192 * The buffer or size which we're trying to read isn't aligned
193 * to the O_DIRECT rules, so we need to do this the hard way...
196 actual = read(data->dev, data->bounce, channel->block_size);
197 if (actual != channel->block_size)
200 if (size > channel->block_size)
201 actual = channel->block_size;
202 memcpy(buf, data->bounce, actual);
209 memset((char *) buf+actual, 0, size-actual);
210 if (channel->read_error)
211 retval = (channel->read_error)(channel, block, count, buf,
212 size, actual, retval);
216 static errcode_t raw_write_blk(io_channel channel,
217 struct unix_private_data *data,
218 unsigned long long block,
219 int count, const void *buf)
222 ext2_loff_t location;
227 size = channel->block_size;
232 size = count * channel->block_size;
234 data->io_stats.bytes_written += size;
236 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
237 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
238 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
242 if ((data->align == 0) ||
243 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
244 actual = write(data->dev, buf, size);
245 if (actual != size) {
247 retval = EXT2_ET_SHORT_WRITE;
254 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
255 (unsigned long) size);
258 * The buffer or size which we're trying to write isn't aligned
259 * to the O_DIRECT rules, so we need to do this the hard way...
262 if (size < channel->block_size) {
263 actual = read(data->dev, data->bounce,
264 channel->block_size);
265 if (actual != channel->block_size) {
266 retval = EXT2_ET_SHORT_READ;
271 if (size > channel->block_size)
272 actual = channel->block_size;
273 memcpy(data->bounce, buf, actual);
274 actual = write(data->dev, data->bounce, channel->block_size);
275 if (actual != channel->block_size)
283 if (channel->write_error)
284 retval = (channel->write_error)(channel, block, count, buf,
285 size, actual, retval);
291 * Here we implement the cache functions
294 /* Allocate the cache buffers */
295 static errcode_t alloc_cache(io_channel channel,
296 struct unix_private_data *data)
299 struct unix_cache *cache;
302 data->access_time = 0;
303 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
305 cache->access_time = 0;
309 ext2fs_free_mem(&cache->buf);
310 retval = ext2fs_get_memalign(channel->block_size,
311 data->align, &cache->buf);
317 ext2fs_free_mem(&data->bounce);
318 retval = ext2fs_get_memalign(channel->block_size, data->align,
324 /* Free the cache buffers */
325 static void free_cache(struct unix_private_data *data)
327 struct unix_cache *cache;
330 data->access_time = 0;
331 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
333 cache->access_time = 0;
337 ext2fs_free_mem(&cache->buf);
340 ext2fs_free_mem(&data->bounce);
345 * Try to find a block in the cache. If the block is not found, and
346 * eldest is a non-zero pointer, then fill in eldest with the cache
347 * entry to that should be reused.
349 static struct unix_cache *find_cached_block(struct unix_private_data *data,
350 unsigned long long block,
351 struct unix_cache **eldest)
353 struct unix_cache *cache, *unused_cache, *oldest_cache;
356 unused_cache = oldest_cache = 0;
357 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
358 if (!cache->in_use) {
360 unused_cache = cache;
363 if (cache->block == block) {
364 cache->access_time = ++data->access_time;
368 (cache->access_time < oldest_cache->access_time))
369 oldest_cache = cache;
372 *eldest = (unused_cache) ? unused_cache : oldest_cache;
377 * Reuse a particular cache entry for another block.
379 static void reuse_cache(io_channel channel, struct unix_private_data *data,
380 struct unix_cache *cache, unsigned long long block)
382 if (cache->dirty && cache->in_use)
383 raw_write_blk(channel, data, cache->block, 1, cache->buf);
387 cache->block = block;
388 cache->access_time = ++data->access_time;
392 * Flush all of the blocks in the cache
394 static errcode_t flush_cached_blocks(io_channel channel,
395 struct unix_private_data *data,
399 struct unix_cache *cache;
400 errcode_t retval, retval2;
404 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
414 retval = raw_write_blk(channel, data,
415 cache->block, 1, cache->buf);
423 #endif /* NO_IO_CACHE */
425 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
427 io_channel io = NULL;
428 struct unix_private_data *data = NULL;
437 return EXT2_ET_BAD_DEVICE_NAME;
438 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
441 memset(io, 0, sizeof(struct struct_io_channel));
442 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
443 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
447 io->manager = unix_io_manager;
448 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
452 strcpy(io->name, name);
453 io->private_data = data;
454 io->block_size = 1024;
459 memset(data, 0, sizeof(struct unix_private_data));
460 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
461 data->io_stats.num_fields = 2;
463 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
464 if (flags & IO_FLAG_EXCLUSIVE)
465 open_flags |= O_EXCL;
466 if (flags & IO_FLAG_DIRECT_IO)
467 open_flags |= O_DIRECT;
471 data->dev = open64(io->name, open_flags);
473 data->dev = open(io->name, open_flags);
481 if (flags & IO_FLAG_DIRECT_IO) {
482 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
483 data->align = io->block_size;
487 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
489 * Some operating systems require that the buffers be aligned,
490 * regardless of O_DIRECT
496 if ((retval = alloc_cache(io, data)))
500 if (flags & IO_FLAG_RW) {
504 /* Is the block device actually writable? */
505 error = ioctl(data->dev, BLKROGET, &readonly);
506 if (!error && readonly) {
516 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
517 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
519 #define RLIM_INFINITY (~0UL)
522 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
523 * block devices are wrongly getting hit by the filesize
524 * limit. This workaround isn't perfect, since it won't work
525 * if glibc wasn't built against 2.2 header files. (Sigh.)
528 if ((flags & IO_FLAG_RW) &&
530 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
531 (ut.release[2] == '4') && (ut.release[3] == '.') &&
532 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
533 (ut.release[5] < '8')) &&
534 (fstat(data->dev, &st) == 0) &&
535 (S_ISBLK(st.st_mode))) {
538 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
539 setrlimit(RLIMIT_FSIZE, &rlim);
540 getrlimit(RLIMIT_FSIZE, &rlim);
541 if (((unsigned long) rlim.rlim_cur) <
542 ((unsigned long) rlim.rlim_max)) {
543 rlim.rlim_cur = rlim.rlim_max;
544 setrlimit(RLIMIT_FSIZE, &rlim);
554 ext2fs_free_mem(&data);
557 ext2fs_free_mem(&io);
561 static errcode_t unix_close(io_channel channel)
563 struct unix_private_data *data;
564 errcode_t retval = 0;
566 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
567 data = (struct unix_private_data *) channel->private_data;
568 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
570 if (--channel->refcount > 0)
574 retval = flush_cached_blocks(channel, data, 0);
577 if (close(data->dev) < 0)
581 ext2fs_free_mem(&channel->private_data);
583 ext2fs_free_mem(&channel->name);
584 ext2fs_free_mem(&channel);
588 static errcode_t unix_set_blksize(io_channel channel, int blksize)
590 struct unix_private_data *data;
593 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
594 data = (struct unix_private_data *) channel->private_data;
595 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
597 if (channel->block_size != blksize) {
599 if ((retval = flush_cached_blocks(channel, data, 0)))
603 channel->block_size = blksize;
605 if ((retval = alloc_cache(channel, data)))
612 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
613 int count, void *buf)
615 struct unix_private_data *data;
616 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
621 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
622 data = (struct unix_private_data *) channel->private_data;
623 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
626 return raw_read_blk(channel, data, block, count, buf);
629 * If we're doing an odd-sized read or a very large read,
630 * flush out the cache and then do a direct read.
632 if (count < 0 || count > WRITE_DIRECT_SIZE) {
633 if ((retval = flush_cached_blocks(channel, data, 0)))
635 return raw_read_blk(channel, data, block, count, buf);
640 /* If it's in the cache, use it! */
641 if ((cache = find_cached_block(data, block, &reuse[0]))) {
643 printf("Using cached block %lu\n", block);
645 memcpy(cp, cache->buf, channel->block_size);
648 cp += channel->block_size;
653 * Special case where we read directly into the
654 * cache buffer; important in the O_DIRECT case
657 reuse_cache(channel, data, cache, block);
658 if ((retval = raw_read_blk(channel, data, block, 1,
663 memcpy(cp, cache->buf, channel->block_size);
668 * Find the number of uncached blocks so we can do a
669 * single read request
671 for (i=1; i < count; i++)
672 if (find_cached_block(data, block+i, &reuse[i]))
675 printf("Reading %d blocks starting at %lu\n", i, block);
677 if ((retval = raw_read_blk(channel, data, block, i, cp)))
680 /* Save the results in the cache */
681 for (j=0; j < i; j++) {
684 reuse_cache(channel, data, cache, block++);
685 memcpy(cache->buf, cp, channel->block_size);
686 cp += channel->block_size;
690 #endif /* NO_IO_CACHE */
693 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
694 int count, void *buf)
696 return unix_read_blk64(channel, block, count, buf);
699 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
700 int count, const void *buf)
702 struct unix_private_data *data;
703 struct unix_cache *cache, *reuse;
704 errcode_t retval = 0;
708 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
709 data = (struct unix_private_data *) channel->private_data;
710 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
713 return raw_write_blk(channel, data, block, count, buf);
716 * If we're doing an odd-sized write or a very large write,
717 * flush out the cache completely and then do a direct write.
719 if (count < 0 || count > WRITE_DIRECT_SIZE) {
720 if ((retval = flush_cached_blocks(channel, data, 1)))
722 return raw_write_blk(channel, data, block, count, buf);
726 * For a moderate-sized multi-block write, first force a write
727 * if we're in write-through cache mode, and then fill the
728 * cache with the blocks.
730 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
732 retval = raw_write_blk(channel, data, block, count, buf);
736 cache = find_cached_block(data, block, &reuse);
739 reuse_cache(channel, data, cache, block);
741 memcpy(cache->buf, cp, channel->block_size);
742 cache->dirty = !writethrough;
745 cp += channel->block_size;
748 #endif /* NO_IO_CACHE */
751 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
752 int count, const void *buf)
754 return unix_write_blk64(channel, block, count, buf);
757 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
758 int size, const void *buf)
760 struct unix_private_data *data;
761 errcode_t retval = 0;
764 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
765 data = (struct unix_private_data *) channel->private_data;
766 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
768 if (data->align != 0) {
770 printf("unix_write_byte: O_DIRECT fallback\n");
772 return EXT2_ET_UNIMPLEMENTED;
777 * Flush out the cache completely
779 if ((retval = flush_cached_blocks(channel, data, 1)))
783 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
786 actual = write(data->dev, buf, size);
788 return EXT2_ET_SHORT_WRITE;
794 * Flush data buffers to disk.
796 static errcode_t unix_flush(io_channel channel)
798 struct unix_private_data *data;
799 errcode_t retval = 0;
801 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
802 data = (struct unix_private_data *) channel->private_data;
803 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
806 retval = flush_cached_blocks(channel, data, 0);
812 static errcode_t unix_set_option(io_channel channel, const char *option,
815 struct unix_private_data *data;
816 unsigned long long tmp;
819 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
820 data = (struct unix_private_data *) channel->private_data;
821 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
823 if (!strcmp(option, "offset")) {
825 return EXT2_ET_INVALID_ARGUMENT;
827 tmp = strtoull(arg, &end, 0);
829 return EXT2_ET_INVALID_ARGUMENT;
831 if (data->offset < 0)
832 return EXT2_ET_INVALID_ARGUMENT;
835 return EXT2_ET_INVALID_ARGUMENT;