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;
449 ext2fs_struct_stat st;
455 return EXT2_ET_BAD_DEVICE_NAME;
456 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
459 memset(io, 0, sizeof(struct struct_io_channel));
460 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
461 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
465 io->manager = unix_io_manager;
466 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
470 strcpy(io->name, name);
471 io->private_data = data;
472 io->block_size = 1024;
477 memset(data, 0, sizeof(struct unix_private_data));
478 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
479 data->io_stats.num_fields = 2;
481 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
482 if (flags & IO_FLAG_EXCLUSIVE)
483 open_flags |= O_EXCL;
484 #if defined(O_DIRECT)
485 if (flags & IO_FLAG_DIRECT_IO)
486 open_flags |= O_DIRECT;
487 #elif defined(F_NOCACHE)
488 if (flags & IO_FLAG_DIRECT_IO)
489 f_nocache = F_NOCACHE;
493 data->dev = ext2fs_open_file(io->name, open_flags, 0);
499 if (fcntl(data->dev, f_nocache, 1) < 0) {
506 * If the device is really a block device, then set the
507 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
508 * because we are going to use punch hole instead of discard
509 * and if it succeed, subsequent read from sparse area returns
512 if (ext2fs_stat(io->name, &st) == 0) {
513 if (S_ISBLK(st.st_mode))
514 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
516 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
520 if (flags & IO_FLAG_DIRECT_IO) {
521 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
522 data->align = io->block_size;
526 #ifdef BLKDISCARDZEROES
527 ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
529 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
532 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
534 * Some operating systems require that the buffers be aligned,
535 * regardless of O_DIRECT
541 if ((retval = alloc_cache(io, data)))
545 if (flags & IO_FLAG_RW) {
549 /* Is the block device actually writable? */
550 error = ioctl(data->dev, BLKROGET, &readonly);
551 if (!error && readonly) {
561 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
562 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
564 #define RLIM_INFINITY (~0UL)
567 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
568 * block devices are wrongly getting hit by the filesize
569 * limit. This workaround isn't perfect, since it won't work
570 * if glibc wasn't built against 2.2 header files. (Sigh.)
573 if ((flags & IO_FLAG_RW) &&
575 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
576 (ut.release[2] == '4') && (ut.release[3] == '.') &&
577 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
578 (ut.release[5] < '8')) &&
579 (ext2fs_stat(io->name, &st) == 0) &&
580 (S_ISBLK(st.st_mode))) {
583 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
584 setrlimit(RLIMIT_FSIZE, &rlim);
585 getrlimit(RLIMIT_FSIZE, &rlim);
586 if (((unsigned long) rlim.rlim_cur) <
587 ((unsigned long) rlim.rlim_max)) {
588 rlim.rlim_cur = rlim.rlim_max;
589 setrlimit(RLIMIT_FSIZE, &rlim);
599 ext2fs_free_mem(&data);
602 ext2fs_free_mem(&io);
606 static errcode_t unix_close(io_channel channel)
608 struct unix_private_data *data;
609 errcode_t retval = 0;
611 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
612 data = (struct unix_private_data *) channel->private_data;
613 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
615 if (--channel->refcount > 0)
619 retval = flush_cached_blocks(channel, data, 0);
622 if (close(data->dev) < 0)
626 ext2fs_free_mem(&channel->private_data);
628 ext2fs_free_mem(&channel->name);
629 ext2fs_free_mem(&channel);
633 static errcode_t unix_set_blksize(io_channel channel, int blksize)
635 struct unix_private_data *data;
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);
642 if (channel->block_size != blksize) {
644 if ((retval = flush_cached_blocks(channel, data, 0)))
648 channel->block_size = blksize;
650 if ((retval = alloc_cache(channel, data)))
657 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
658 int count, void *buf)
660 struct unix_private_data *data;
661 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
666 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
667 data = (struct unix_private_data *) channel->private_data;
668 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
671 return raw_read_blk(channel, data, block, count, buf);
674 * If we're doing an odd-sized read or a very large read,
675 * flush out the cache and then do a direct read.
677 if (count < 0 || count > WRITE_DIRECT_SIZE) {
678 if ((retval = flush_cached_blocks(channel, data, 0)))
680 return raw_read_blk(channel, data, block, count, buf);
685 /* If it's in the cache, use it! */
686 if ((cache = find_cached_block(data, block, &reuse[0]))) {
688 printf("Using cached block %lu\n", block);
690 memcpy(cp, cache->buf, channel->block_size);
693 cp += channel->block_size;
698 * Special case where we read directly into the
699 * cache buffer; important in the O_DIRECT case
702 reuse_cache(channel, data, cache, block);
703 if ((retval = raw_read_blk(channel, data, block, 1,
708 memcpy(cp, cache->buf, channel->block_size);
713 * Find the number of uncached blocks so we can do a
714 * single read request
716 for (i=1; i < count; i++)
717 if (find_cached_block(data, block+i, &reuse[i]))
720 printf("Reading %d blocks starting at %lu\n", i, block);
722 if ((retval = raw_read_blk(channel, data, block, i, cp)))
725 /* Save the results in the cache */
726 for (j=0; j < i; j++) {
729 reuse_cache(channel, data, cache, block++);
730 memcpy(cache->buf, cp, channel->block_size);
731 cp += channel->block_size;
735 #endif /* NO_IO_CACHE */
738 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
739 int count, void *buf)
741 return unix_read_blk64(channel, block, count, buf);
744 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
745 int count, const void *buf)
747 struct unix_private_data *data;
748 struct unix_cache *cache, *reuse;
749 errcode_t retval = 0;
753 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
754 data = (struct unix_private_data *) channel->private_data;
755 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
758 return raw_write_blk(channel, data, block, count, buf);
761 * If we're doing an odd-sized write or a very large write,
762 * flush out the cache completely and then do a direct write.
764 if (count < 0 || count > WRITE_DIRECT_SIZE) {
765 if ((retval = flush_cached_blocks(channel, data, 1)))
767 return raw_write_blk(channel, data, block, count, buf);
771 * For a moderate-sized multi-block write, first force a write
772 * if we're in write-through cache mode, and then fill the
773 * cache with the blocks.
775 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
777 retval = raw_write_blk(channel, data, block, count, buf);
781 cache = find_cached_block(data, block, &reuse);
784 reuse_cache(channel, data, cache, block);
786 memcpy(cache->buf, cp, channel->block_size);
787 cache->dirty = !writethrough;
790 cp += channel->block_size;
793 #endif /* NO_IO_CACHE */
796 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
797 int count, const void *buf)
799 return unix_write_blk64(channel, block, count, buf);
802 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
803 int size, const void *buf)
805 struct unix_private_data *data;
806 errcode_t retval = 0;
809 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
810 data = (struct unix_private_data *) channel->private_data;
811 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
813 if (data->align != 0) {
815 printf("unix_write_byte: O_DIRECT fallback\n");
817 return EXT2_ET_UNIMPLEMENTED;
822 * Flush out the cache completely
824 if ((retval = flush_cached_blocks(channel, data, 1)))
828 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
831 actual = write(data->dev, buf, size);
833 return EXT2_ET_SHORT_WRITE;
839 * Flush data buffers to disk.
841 static errcode_t unix_flush(io_channel channel)
843 struct unix_private_data *data;
844 errcode_t retval = 0;
846 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
847 data = (struct unix_private_data *) channel->private_data;
848 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
851 retval = flush_cached_blocks(channel, data, 0);
857 static errcode_t unix_set_option(io_channel channel, const char *option,
860 struct unix_private_data *data;
861 unsigned long long tmp;
864 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
865 data = (struct unix_private_data *) channel->private_data;
866 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
868 if (!strcmp(option, "offset")) {
870 return EXT2_ET_INVALID_ARGUMENT;
872 tmp = strtoull(arg, &end, 0);
874 return EXT2_ET_INVALID_ARGUMENT;
876 if (data->offset < 0)
877 return EXT2_ET_INVALID_ARGUMENT;
880 return EXT2_ET_INVALID_ARGUMENT;
883 #if defined(__linux__) && !defined(BLKDISCARD)
884 #define BLKDISCARD _IO(0x12,119)
887 static errcode_t unix_discard(io_channel channel, unsigned long long block,
888 unsigned long long count)
890 struct unix_private_data *data;
894 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
895 data = (struct unix_private_data *) channel->private_data;
896 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
898 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
900 range[0] = (__uint64_t)(block) * channel->block_size;
901 range[1] = (__uint64_t)(count) * channel->block_size;
903 ret = ioctl(data->dev, BLKDISCARD, &range);
908 #ifdef FALLOC_FL_PUNCH_HOLE
910 * If we are not on block device, try to use punch hole
911 * to reclaim free space.
913 ret = fallocate(data->dev,
914 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
915 (off_t)(block) * channel->block_size,
916 (off_t)(count) * channel->block_size);
922 if (errno == EOPNOTSUPP)
928 return EXT2_ET_UNIMPLEMENTED;