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
36 #include <sys/utsname.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
41 #ifdef HAVE_SYS_MOUNT_H
42 #include <sys/mount.h>
48 #include <sys/types.h>
50 #if HAVE_SYS_RESOURCE_H
51 #include <sys/resource.h>
53 #if HAVE_LINUX_FALLOC_H
54 #include <linux/falloc.h>
57 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
58 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
61 #if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET)
62 #define BLKSSZGET _IO(0x12,104)/* get block device sector size */
71 * For checking structure magic numbers...
74 #define EXT2_CHECK_MAGIC(struct, code) \
75 if ((struct)->magic != (code)) return (code)
86 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
87 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
89 struct unix_private_data {
96 struct unix_cache cache[CACHE_SIZE];
98 struct struct_io_stats io_stats;
101 #define IS_ALIGNED(n, align) ((((unsigned long) n) & \
102 ((unsigned long) ((align)-1))) == 0)
104 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
105 static errcode_t unix_close(io_channel channel);
106 static errcode_t unix_set_blksize(io_channel channel, int blksize);
107 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
108 int count, void *data);
109 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
110 int count, const void *data);
111 static errcode_t unix_flush(io_channel channel);
112 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
113 int size, const void *data);
114 static errcode_t unix_set_option(io_channel channel, const char *option,
116 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
118 static void reuse_cache(io_channel channel, struct unix_private_data *data,
119 struct unix_cache *cache, unsigned long long block);
120 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
121 int count, void *data);
122 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
123 int count, const void *data);
124 static errcode_t unix_discard(io_channel channel, unsigned long long block,
125 unsigned long long count);
127 static struct struct_io_manager struct_unix_manager = {
128 EXT2_ET_MAGIC_IO_MANAGER,
144 io_manager unix_io_manager = &struct_unix_manager;
146 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
148 errcode_t retval = 0;
150 struct unix_private_data *data;
152 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
153 data = (struct unix_private_data *) channel->private_data;
154 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
157 *stats = &data->io_stats;
163 * Here are the raw I/O functions
165 static errcode_t raw_read_blk(io_channel channel,
166 struct unix_private_data *data,
167 unsigned long long block,
168 int count, void *bufv)
172 ext2_loff_t location;
174 unsigned char *buf = bufv;
176 size = (count < 0) ? -count : count * channel->block_size;
177 data->io_stats.bytes_read += size;
178 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
179 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
180 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
183 if ((data->align == 0) ||
184 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
185 actual = read(data->dev, buf, size);
186 if (actual != size) {
190 retval = EXT2_ET_SHORT_READ;
197 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
198 (unsigned long) size);
202 * The buffer or size which we're trying to read isn't aligned
203 * to the O_DIRECT rules, so we need to do this the hard way...
206 actual = read(data->dev, data->bounce, channel->block_size);
207 if (actual != channel->block_size)
210 if (size > channel->block_size)
211 actual = channel->block_size;
212 memcpy(buf, data->bounce, actual);
219 memset((char *) buf+actual, 0, size-actual);
220 if (channel->read_error)
221 retval = (channel->read_error)(channel, block, count, buf,
222 size, actual, retval);
226 static errcode_t raw_write_blk(io_channel channel,
227 struct unix_private_data *data,
228 unsigned long long block,
229 int count, const void *bufv)
232 ext2_loff_t location;
235 const unsigned char *buf = bufv;
238 size = channel->block_size;
243 size = count * channel->block_size;
245 data->io_stats.bytes_written += size;
247 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
248 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
249 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
253 if ((data->align == 0) ||
254 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
255 actual = write(data->dev, buf, size);
256 if (actual != size) {
258 retval = EXT2_ET_SHORT_WRITE;
265 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
266 (unsigned long) size);
269 * The buffer or size which we're trying to write isn't aligned
270 * to the O_DIRECT rules, so we need to do this the hard way...
273 if (size < channel->block_size) {
274 actual = read(data->dev, data->bounce,
275 channel->block_size);
276 if (actual != channel->block_size) {
277 retval = EXT2_ET_SHORT_READ;
282 if (size > channel->block_size)
283 actual = channel->block_size;
284 memcpy(data->bounce, buf, actual);
285 actual = write(data->dev, data->bounce, channel->block_size);
286 if (actual != channel->block_size)
294 if (channel->write_error)
295 retval = (channel->write_error)(channel, block, count, buf,
296 size, actual, retval);
302 * Here we implement the cache functions
305 /* Allocate the cache buffers */
306 static errcode_t alloc_cache(io_channel channel,
307 struct unix_private_data *data)
310 struct unix_cache *cache;
313 data->access_time = 0;
314 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
316 cache->access_time = 0;
320 ext2fs_free_mem(&cache->buf);
321 retval = ext2fs_get_memalign(channel->block_size,
322 data->align, &cache->buf);
328 ext2fs_free_mem(&data->bounce);
329 retval = ext2fs_get_memalign(channel->block_size, data->align,
335 /* Free the cache buffers */
336 static void free_cache(struct unix_private_data *data)
338 struct unix_cache *cache;
341 data->access_time = 0;
342 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
344 cache->access_time = 0;
348 ext2fs_free_mem(&cache->buf);
351 ext2fs_free_mem(&data->bounce);
356 * Try to find a block in the cache. If the block is not found, and
357 * eldest is a non-zero pointer, then fill in eldest with the cache
358 * entry to that should be reused.
360 static struct unix_cache *find_cached_block(struct unix_private_data *data,
361 unsigned long long block,
362 struct unix_cache **eldest)
364 struct unix_cache *cache, *unused_cache, *oldest_cache;
367 unused_cache = oldest_cache = 0;
368 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
369 if (!cache->in_use) {
371 unused_cache = cache;
374 if (cache->block == block) {
375 cache->access_time = ++data->access_time;
379 (cache->access_time < oldest_cache->access_time))
380 oldest_cache = cache;
383 *eldest = (unused_cache) ? unused_cache : oldest_cache;
388 * Reuse a particular cache entry for another block.
390 static void reuse_cache(io_channel channel, struct unix_private_data *data,
391 struct unix_cache *cache, unsigned long long block)
393 if (cache->dirty && cache->in_use)
394 raw_write_blk(channel, data, cache->block, 1, cache->buf);
398 cache->block = block;
399 cache->access_time = ++data->access_time;
403 * Flush all of the blocks in the cache
405 static errcode_t flush_cached_blocks(io_channel channel,
406 struct unix_private_data *data,
410 struct unix_cache *cache;
411 errcode_t retval, retval2;
415 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
425 retval = raw_write_blk(channel, data,
426 cache->block, 1, cache->buf);
434 #endif /* NO_IO_CACHE */
437 #ifndef BLKDISCARDZEROES
438 #define BLKDISCARDZEROES _IO(0x12,124)
442 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
444 io_channel io = NULL;
445 struct unix_private_data *data = NULL;
447 int open_flags, zeroes = 0;
448 ext2fs_struct_stat st;
454 return EXT2_ET_BAD_DEVICE_NAME;
455 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
458 memset(io, 0, sizeof(struct struct_io_channel));
459 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
460 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
464 io->manager = unix_io_manager;
465 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
469 strcpy(io->name, name);
470 io->private_data = data;
471 io->block_size = 1024;
476 memset(data, 0, sizeof(struct unix_private_data));
477 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
478 data->io_stats.num_fields = 2;
480 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
481 if (flags & IO_FLAG_EXCLUSIVE)
482 open_flags |= O_EXCL;
484 if (flags & IO_FLAG_DIRECT_IO)
485 open_flags |= O_DIRECT;
489 data->dev = ext2fs_open_file(io->name, open_flags, 0);
496 * If the device is really a block device, then set the
497 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
498 * because we are going to use punch hole instead of discard
499 * and if it succeed, subsequent read from sparse area returns
502 if (ext2fs_stat(io->name, &st) == 0) {
503 if (S_ISBLK(st.st_mode))
504 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
506 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
510 if (flags & IO_FLAG_DIRECT_IO) {
511 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
512 data->align = io->block_size;
516 #ifdef BLKDISCARDZEROES
517 ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
519 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
522 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
524 * Some operating systems require that the buffers be aligned,
525 * regardless of O_DIRECT
531 if ((retval = alloc_cache(io, data)))
535 if (flags & IO_FLAG_RW) {
539 /* Is the block device actually writable? */
540 error = ioctl(data->dev, BLKROGET, &readonly);
541 if (!error && readonly) {
551 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
552 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
554 #define RLIM_INFINITY (~0UL)
557 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
558 * block devices are wrongly getting hit by the filesize
559 * limit. This workaround isn't perfect, since it won't work
560 * if glibc wasn't built against 2.2 header files. (Sigh.)
563 if ((flags & IO_FLAG_RW) &&
565 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
566 (ut.release[2] == '4') && (ut.release[3] == '.') &&
567 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
568 (ut.release[5] < '8')) &&
569 (ext2fs_stat(io->name, &st) == 0) &&
570 (S_ISBLK(st.st_mode))) {
573 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
574 setrlimit(RLIMIT_FSIZE, &rlim);
575 getrlimit(RLIMIT_FSIZE, &rlim);
576 if (((unsigned long) rlim.rlim_cur) <
577 ((unsigned long) rlim.rlim_max)) {
578 rlim.rlim_cur = rlim.rlim_max;
579 setrlimit(RLIMIT_FSIZE, &rlim);
589 ext2fs_free_mem(&data);
592 ext2fs_free_mem(&io);
596 static errcode_t unix_close(io_channel channel)
598 struct unix_private_data *data;
599 errcode_t retval = 0;
601 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
602 data = (struct unix_private_data *) channel->private_data;
603 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
605 if (--channel->refcount > 0)
609 retval = flush_cached_blocks(channel, data, 0);
612 if (close(data->dev) < 0)
616 ext2fs_free_mem(&channel->private_data);
618 ext2fs_free_mem(&channel->name);
619 ext2fs_free_mem(&channel);
623 static errcode_t unix_set_blksize(io_channel channel, int blksize)
625 struct unix_private_data *data;
628 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
629 data = (struct unix_private_data *) channel->private_data;
630 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
632 if (channel->block_size != blksize) {
634 if ((retval = flush_cached_blocks(channel, data, 0)))
638 channel->block_size = blksize;
640 if ((retval = alloc_cache(channel, data)))
647 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
648 int count, void *buf)
650 struct unix_private_data *data;
651 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
656 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
657 data = (struct unix_private_data *) channel->private_data;
658 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
661 return raw_read_blk(channel, data, block, count, buf);
664 * If we're doing an odd-sized read or a very large read,
665 * flush out the cache and then do a direct read.
667 if (count < 0 || count > WRITE_DIRECT_SIZE) {
668 if ((retval = flush_cached_blocks(channel, data, 0)))
670 return raw_read_blk(channel, data, block, count, buf);
675 /* If it's in the cache, use it! */
676 if ((cache = find_cached_block(data, block, &reuse[0]))) {
678 printf("Using cached block %lu\n", block);
680 memcpy(cp, cache->buf, channel->block_size);
683 cp += channel->block_size;
688 * Special case where we read directly into the
689 * cache buffer; important in the O_DIRECT case
692 reuse_cache(channel, data, cache, block);
693 if ((retval = raw_read_blk(channel, data, block, 1,
698 memcpy(cp, cache->buf, channel->block_size);
703 * Find the number of uncached blocks so we can do a
704 * single read request
706 for (i=1; i < count; i++)
707 if (find_cached_block(data, block+i, &reuse[i]))
710 printf("Reading %d blocks starting at %lu\n", i, block);
712 if ((retval = raw_read_blk(channel, data, block, i, cp)))
715 /* Save the results in the cache */
716 for (j=0; j < i; j++) {
719 reuse_cache(channel, data, cache, block++);
720 memcpy(cache->buf, cp, channel->block_size);
721 cp += channel->block_size;
725 #endif /* NO_IO_CACHE */
728 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
729 int count, void *buf)
731 return unix_read_blk64(channel, block, count, buf);
734 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
735 int count, const void *buf)
737 struct unix_private_data *data;
738 struct unix_cache *cache, *reuse;
739 errcode_t retval = 0;
743 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
744 data = (struct unix_private_data *) channel->private_data;
745 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
748 return raw_write_blk(channel, data, block, count, buf);
751 * If we're doing an odd-sized write or a very large write,
752 * flush out the cache completely and then do a direct write.
754 if (count < 0 || count > WRITE_DIRECT_SIZE) {
755 if ((retval = flush_cached_blocks(channel, data, 1)))
757 return raw_write_blk(channel, data, block, count, buf);
761 * For a moderate-sized multi-block write, first force a write
762 * if we're in write-through cache mode, and then fill the
763 * cache with the blocks.
765 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
767 retval = raw_write_blk(channel, data, block, count, buf);
771 cache = find_cached_block(data, block, &reuse);
774 reuse_cache(channel, data, cache, block);
776 memcpy(cache->buf, cp, channel->block_size);
777 cache->dirty = !writethrough;
780 cp += channel->block_size;
783 #endif /* NO_IO_CACHE */
786 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
787 int count, const void *buf)
789 return unix_write_blk64(channel, block, count, buf);
792 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
793 int size, const void *buf)
795 struct unix_private_data *data;
796 errcode_t retval = 0;
799 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
800 data = (struct unix_private_data *) channel->private_data;
801 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
803 if (data->align != 0) {
805 printf("unix_write_byte: O_DIRECT fallback\n");
807 return EXT2_ET_UNIMPLEMENTED;
812 * Flush out the cache completely
814 if ((retval = flush_cached_blocks(channel, data, 1)))
818 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
821 actual = write(data->dev, buf, size);
823 return EXT2_ET_SHORT_WRITE;
829 * Flush data buffers to disk.
831 static errcode_t unix_flush(io_channel channel)
833 struct unix_private_data *data;
834 errcode_t retval = 0;
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);
841 retval = flush_cached_blocks(channel, data, 0);
847 static errcode_t unix_set_option(io_channel channel, const char *option,
850 struct unix_private_data *data;
851 unsigned long long tmp;
854 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
855 data = (struct unix_private_data *) channel->private_data;
856 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
858 if (!strcmp(option, "offset")) {
860 return EXT2_ET_INVALID_ARGUMENT;
862 tmp = strtoull(arg, &end, 0);
864 return EXT2_ET_INVALID_ARGUMENT;
866 if (data->offset < 0)
867 return EXT2_ET_INVALID_ARGUMENT;
870 return EXT2_ET_INVALID_ARGUMENT;
873 #if defined(__linux__) && !defined(BLKDISCARD)
874 #define BLKDISCARD _IO(0x12,119)
877 static errcode_t unix_discard(io_channel channel, unsigned long long block,
878 unsigned long long count)
880 struct unix_private_data *data;
884 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
885 data = (struct unix_private_data *) channel->private_data;
886 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
888 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
890 range[0] = (__uint64_t)(block) * channel->block_size;
891 range[1] = (__uint64_t)(count) * channel->block_size;
893 ret = ioctl(data->dev, BLKDISCARD, &range);
898 #ifdef FALLOC_FL_PUNCH_HOLE
900 * If we are not on block device, try to use punch hole
901 * to reclaim free space.
903 ret = fallocate(data->dev,
904 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
905 (off_t)(block) * channel->block_size,
906 (off_t)(count) * channel->block_size);
912 if (errno == EOPNOTSUPP)
918 return EXT2_ET_UNIMPLEMENTED;