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
35 #include <sys/utsname.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
40 #ifdef HAVE_SYS_MOUNT_H
41 #include <sys/mount.h>
47 #include <sys/types.h>
49 #if HAVE_SYS_RESOURCE_H
50 #include <sys/resource.h>
53 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
54 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
57 #if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET)
58 #define BLKSSZGET _IO(0x12,104)/* get block device sector size */
67 * For checking structure magic numbers...
70 #define EXT2_CHECK_MAGIC(struct, code) \
71 if ((struct)->magic != (code)) return (code)
82 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
83 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
85 struct unix_private_data {
92 struct unix_cache cache[CACHE_SIZE];
94 struct struct_io_stats io_stats;
97 #define IS_ALIGNED(n, align) ((((unsigned long) n) & \
98 ((unsigned long) ((align)-1))) == 0)
100 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
101 static errcode_t unix_close(io_channel channel);
102 static errcode_t unix_set_blksize(io_channel channel, int blksize);
103 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
104 int count, void *data);
105 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
106 int count, const void *data);
107 static errcode_t unix_flush(io_channel channel);
108 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
109 int size, const void *data);
110 static errcode_t unix_set_option(io_channel channel, const char *option,
112 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
114 static void reuse_cache(io_channel channel, struct unix_private_data *data,
115 struct unix_cache *cache, unsigned long long block);
116 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
117 int count, void *data);
118 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
119 int count, const void *data);
120 static errcode_t unix_discard(io_channel channel, unsigned long long block,
121 unsigned long long count);
123 static struct struct_io_manager struct_unix_manager = {
124 EXT2_ET_MAGIC_IO_MANAGER,
140 io_manager unix_io_manager = &struct_unix_manager;
142 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
144 errcode_t retval = 0;
146 struct unix_private_data *data;
148 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
149 data = (struct unix_private_data *) channel->private_data;
150 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
153 *stats = &data->io_stats;
159 * Here are the raw I/O functions
161 static errcode_t raw_read_blk(io_channel channel,
162 struct unix_private_data *data,
163 unsigned long long block,
164 int count, void *bufv)
168 ext2_loff_t location;
170 unsigned char *buf = bufv;
172 size = (count < 0) ? -count : count * channel->block_size;
173 data->io_stats.bytes_read += size;
174 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
175 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
176 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
179 if ((data->align == 0) ||
180 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
181 actual = read(data->dev, buf, size);
182 if (actual != size) {
186 retval = EXT2_ET_SHORT_READ;
193 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
194 (unsigned long) size);
198 * The buffer or size which we're trying to read isn't aligned
199 * to the O_DIRECT rules, so we need to do this the hard way...
202 actual = read(data->dev, data->bounce, channel->block_size);
203 if (actual != channel->block_size)
206 if (size > channel->block_size)
207 actual = channel->block_size;
208 memcpy(buf, data->bounce, actual);
215 memset((char *) buf+actual, 0, size-actual);
216 if (channel->read_error)
217 retval = (channel->read_error)(channel, block, count, buf,
218 size, actual, retval);
222 static errcode_t raw_write_blk(io_channel channel,
223 struct unix_private_data *data,
224 unsigned long long block,
225 int count, const void *bufv)
228 ext2_loff_t location;
231 const unsigned char *buf = bufv;
234 size = channel->block_size;
239 size = count * channel->block_size;
241 data->io_stats.bytes_written += size;
243 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
244 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
245 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
249 if ((data->align == 0) ||
250 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
251 actual = write(data->dev, buf, size);
252 if (actual != size) {
254 retval = EXT2_ET_SHORT_WRITE;
261 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
262 (unsigned long) size);
265 * The buffer or size which we're trying to write isn't aligned
266 * to the O_DIRECT rules, so we need to do this the hard way...
269 if (size < channel->block_size) {
270 actual = read(data->dev, data->bounce,
271 channel->block_size);
272 if (actual != channel->block_size) {
273 retval = EXT2_ET_SHORT_READ;
278 if (size > channel->block_size)
279 actual = channel->block_size;
280 memcpy(data->bounce, buf, actual);
281 actual = write(data->dev, data->bounce, channel->block_size);
282 if (actual != channel->block_size)
290 if (channel->write_error)
291 retval = (channel->write_error)(channel, block, count, buf,
292 size, actual, retval);
298 * Here we implement the cache functions
301 /* Allocate the cache buffers */
302 static errcode_t alloc_cache(io_channel channel,
303 struct unix_private_data *data)
306 struct unix_cache *cache;
309 data->access_time = 0;
310 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
312 cache->access_time = 0;
316 ext2fs_free_mem(&cache->buf);
317 retval = ext2fs_get_memalign(channel->block_size,
318 data->align, &cache->buf);
324 ext2fs_free_mem(&data->bounce);
325 retval = ext2fs_get_memalign(channel->block_size, data->align,
331 /* Free the cache buffers */
332 static void free_cache(struct unix_private_data *data)
334 struct unix_cache *cache;
337 data->access_time = 0;
338 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
340 cache->access_time = 0;
344 ext2fs_free_mem(&cache->buf);
347 ext2fs_free_mem(&data->bounce);
352 * Try to find a block in the cache. If the block is not found, and
353 * eldest is a non-zero pointer, then fill in eldest with the cache
354 * entry to that should be reused.
356 static struct unix_cache *find_cached_block(struct unix_private_data *data,
357 unsigned long long block,
358 struct unix_cache **eldest)
360 struct unix_cache *cache, *unused_cache, *oldest_cache;
363 unused_cache = oldest_cache = 0;
364 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
365 if (!cache->in_use) {
367 unused_cache = cache;
370 if (cache->block == block) {
371 cache->access_time = ++data->access_time;
375 (cache->access_time < oldest_cache->access_time))
376 oldest_cache = cache;
379 *eldest = (unused_cache) ? unused_cache : oldest_cache;
384 * Reuse a particular cache entry for another block.
386 static void reuse_cache(io_channel channel, struct unix_private_data *data,
387 struct unix_cache *cache, unsigned long long block)
389 if (cache->dirty && cache->in_use)
390 raw_write_blk(channel, data, cache->block, 1, cache->buf);
394 cache->block = block;
395 cache->access_time = ++data->access_time;
399 * Flush all of the blocks in the cache
401 static errcode_t flush_cached_blocks(io_channel channel,
402 struct unix_private_data *data,
406 struct unix_cache *cache;
407 errcode_t retval, retval2;
411 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
421 retval = raw_write_blk(channel, data,
422 cache->block, 1, cache->buf);
430 #endif /* NO_IO_CACHE */
433 #ifndef BLKDISCARDZEROES
434 #define BLKDISCARDZEROES _IO(0x12,124)
438 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
440 io_channel io = NULL;
441 struct unix_private_data *data = NULL;
443 int open_flags, zeroes = 0;
444 ext2fs_struct_stat st;
450 return EXT2_ET_BAD_DEVICE_NAME;
451 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
454 memset(io, 0, sizeof(struct struct_io_channel));
455 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
456 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
460 io->manager = unix_io_manager;
461 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
465 strcpy(io->name, name);
466 io->private_data = data;
467 io->block_size = 1024;
472 memset(data, 0, sizeof(struct unix_private_data));
473 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
474 data->io_stats.num_fields = 2;
476 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
477 if (flags & IO_FLAG_EXCLUSIVE)
478 open_flags |= O_EXCL;
480 if (flags & IO_FLAG_DIRECT_IO)
481 open_flags |= O_DIRECT;
485 data->dev = ext2fs_open_file(io->name, open_flags);
492 if (flags & IO_FLAG_DIRECT_IO) {
493 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
494 data->align = io->block_size;
498 #ifdef BLKDISCARDZEROES
499 ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
501 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
504 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
506 * Some operating systems require that the buffers be aligned,
507 * regardless of O_DIRECT
513 if ((retval = alloc_cache(io, data)))
517 if (flags & IO_FLAG_RW) {
521 /* Is the block device actually writable? */
522 error = ioctl(data->dev, BLKROGET, &readonly);
523 if (!error && readonly) {
533 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
534 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
536 #define RLIM_INFINITY (~0UL)
539 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
540 * block devices are wrongly getting hit by the filesize
541 * limit. This workaround isn't perfect, since it won't work
542 * if glibc wasn't built against 2.2 header files. (Sigh.)
545 if ((flags & IO_FLAG_RW) &&
547 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
548 (ut.release[2] == '4') && (ut.release[3] == '.') &&
549 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
550 (ut.release[5] < '8')) &&
551 (ext2fs_stat(io->name, &st) == 0) &&
552 (S_ISBLK(st.st_mode))) {
555 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
556 setrlimit(RLIMIT_FSIZE, &rlim);
557 getrlimit(RLIMIT_FSIZE, &rlim);
558 if (((unsigned long) rlim.rlim_cur) <
559 ((unsigned long) rlim.rlim_max)) {
560 rlim.rlim_cur = rlim.rlim_max;
561 setrlimit(RLIMIT_FSIZE, &rlim);
571 ext2fs_free_mem(&data);
574 ext2fs_free_mem(&io);
578 static errcode_t unix_close(io_channel channel)
580 struct unix_private_data *data;
581 errcode_t retval = 0;
583 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
584 data = (struct unix_private_data *) channel->private_data;
585 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
587 if (--channel->refcount > 0)
591 retval = flush_cached_blocks(channel, data, 0);
594 if (close(data->dev) < 0)
598 ext2fs_free_mem(&channel->private_data);
600 ext2fs_free_mem(&channel->name);
601 ext2fs_free_mem(&channel);
605 static errcode_t unix_set_blksize(io_channel channel, int blksize)
607 struct unix_private_data *data;
610 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
611 data = (struct unix_private_data *) channel->private_data;
612 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
614 if (channel->block_size != blksize) {
616 if ((retval = flush_cached_blocks(channel, data, 0)))
620 channel->block_size = blksize;
622 if ((retval = alloc_cache(channel, data)))
629 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
630 int count, void *buf)
632 struct unix_private_data *data;
633 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
638 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
639 data = (struct unix_private_data *) channel->private_data;
640 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
643 return raw_read_blk(channel, data, block, count, buf);
646 * If we're doing an odd-sized read or a very large read,
647 * flush out the cache and then do a direct read.
649 if (count < 0 || count > WRITE_DIRECT_SIZE) {
650 if ((retval = flush_cached_blocks(channel, data, 0)))
652 return raw_read_blk(channel, data, block, count, buf);
657 /* If it's in the cache, use it! */
658 if ((cache = find_cached_block(data, block, &reuse[0]))) {
660 printf("Using cached block %lu\n", block);
662 memcpy(cp, cache->buf, channel->block_size);
665 cp += channel->block_size;
670 * Special case where we read directly into the
671 * cache buffer; important in the O_DIRECT case
674 reuse_cache(channel, data, cache, block);
675 if ((retval = raw_read_blk(channel, data, block, 1,
680 memcpy(cp, cache->buf, channel->block_size);
685 * Find the number of uncached blocks so we can do a
686 * single read request
688 for (i=1; i < count; i++)
689 if (find_cached_block(data, block+i, &reuse[i]))
692 printf("Reading %d blocks starting at %lu\n", i, block);
694 if ((retval = raw_read_blk(channel, data, block, i, cp)))
697 /* Save the results in the cache */
698 for (j=0; j < i; j++) {
701 reuse_cache(channel, data, cache, block++);
702 memcpy(cache->buf, cp, channel->block_size);
703 cp += channel->block_size;
707 #endif /* NO_IO_CACHE */
710 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
711 int count, void *buf)
713 return unix_read_blk64(channel, block, count, buf);
716 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
717 int count, const void *buf)
719 struct unix_private_data *data;
720 struct unix_cache *cache, *reuse;
721 errcode_t retval = 0;
725 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
726 data = (struct unix_private_data *) channel->private_data;
727 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
730 return raw_write_blk(channel, data, block, count, buf);
733 * If we're doing an odd-sized write or a very large write,
734 * flush out the cache completely and then do a direct write.
736 if (count < 0 || count > WRITE_DIRECT_SIZE) {
737 if ((retval = flush_cached_blocks(channel, data, 1)))
739 return raw_write_blk(channel, data, block, count, buf);
743 * For a moderate-sized multi-block write, first force a write
744 * if we're in write-through cache mode, and then fill the
745 * cache with the blocks.
747 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
749 retval = raw_write_blk(channel, data, block, count, buf);
753 cache = find_cached_block(data, block, &reuse);
756 reuse_cache(channel, data, cache, block);
758 memcpy(cache->buf, cp, channel->block_size);
759 cache->dirty = !writethrough;
762 cp += channel->block_size;
765 #endif /* NO_IO_CACHE */
768 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
769 int count, const void *buf)
771 return unix_write_blk64(channel, block, count, buf);
774 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
775 int size, const void *buf)
777 struct unix_private_data *data;
778 errcode_t retval = 0;
781 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
782 data = (struct unix_private_data *) channel->private_data;
783 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
785 if (data->align != 0) {
787 printf("unix_write_byte: O_DIRECT fallback\n");
789 return EXT2_ET_UNIMPLEMENTED;
794 * Flush out the cache completely
796 if ((retval = flush_cached_blocks(channel, data, 1)))
800 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
803 actual = write(data->dev, buf, size);
805 return EXT2_ET_SHORT_WRITE;
811 * Flush data buffers to disk.
813 static errcode_t unix_flush(io_channel channel)
815 struct unix_private_data *data;
816 errcode_t retval = 0;
818 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
819 data = (struct unix_private_data *) channel->private_data;
820 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
823 retval = flush_cached_blocks(channel, data, 0);
829 static errcode_t unix_set_option(io_channel channel, const char *option,
832 struct unix_private_data *data;
833 unsigned long long tmp;
836 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
837 data = (struct unix_private_data *) channel->private_data;
838 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
840 if (!strcmp(option, "offset")) {
842 return EXT2_ET_INVALID_ARGUMENT;
844 tmp = strtoull(arg, &end, 0);
846 return EXT2_ET_INVALID_ARGUMENT;
848 if (data->offset < 0)
849 return EXT2_ET_INVALID_ARGUMENT;
852 return EXT2_ET_INVALID_ARGUMENT;
855 #if defined(__linux__) && !defined(BLKDISCARD)
856 #define BLKDISCARD _IO(0x12,119)
859 static errcode_t unix_discard(io_channel channel, unsigned long long block,
860 unsigned long long count)
863 struct unix_private_data *data;
867 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
868 data = (struct unix_private_data *) channel->private_data;
869 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
871 range[0] = (__uint64_t)(block) * channel->block_size;
872 range[1] = (__uint64_t)(count) * channel->block_size;
874 ret = ioctl(data->dev, BLKDISCARD, &range);
879 return EXT2_ET_UNIMPLEMENTED;