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). */
67 * For checking structure magic numbers...
70 #define EXT2_CHECK_MAGIC(struct, code) \
71 if ((struct)->magic != (code)) return (code)
75 unsigned long long block;
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 ((channel->align == 0) ||
180 (IS_ALIGNED(buf, channel->align) &&
181 IS_ALIGNED(size, channel->align))) {
182 actual = read(data->dev, buf, size);
183 if (actual != size) {
187 retval = EXT2_ET_SHORT_READ;
194 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
195 (unsigned long) size);
199 * The buffer or size which we're trying to read isn't aligned
200 * to the O_DIRECT rules, so we need to do this the hard way...
203 actual = read(data->dev, data->bounce, channel->block_size);
204 if (actual != channel->block_size)
207 if (size > channel->block_size)
208 actual = channel->block_size;
209 memcpy(buf, data->bounce, actual);
216 memset((char *) buf+actual, 0, size-actual);
217 if (channel->read_error)
218 retval = (channel->read_error)(channel, block, count, buf,
219 size, actual, retval);
223 static errcode_t raw_write_blk(io_channel channel,
224 struct unix_private_data *data,
225 unsigned long long block,
226 int count, const void *bufv)
229 ext2_loff_t location;
232 const unsigned char *buf = bufv;
235 size = channel->block_size;
240 size = count * channel->block_size;
242 data->io_stats.bytes_written += size;
244 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
245 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
246 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
250 if ((channel->align == 0) ||
251 (IS_ALIGNED(buf, channel->align) &&
252 IS_ALIGNED(size, channel->align))) {
253 actual = write(data->dev, buf, size);
254 if (actual != size) {
256 retval = EXT2_ET_SHORT_WRITE;
263 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
264 (unsigned long) size);
267 * The buffer or size which we're trying to write isn't aligned
268 * to the O_DIRECT rules, so we need to do this the hard way...
271 if (size < channel->block_size) {
272 actual = read(data->dev, data->bounce,
273 channel->block_size);
274 if (actual != channel->block_size) {
275 retval = EXT2_ET_SHORT_READ;
280 if (size > channel->block_size)
281 actual = channel->block_size;
282 memcpy(data->bounce, buf, actual);
283 actual = write(data->dev, data->bounce, channel->block_size);
284 if (actual != channel->block_size)
292 if (channel->write_error)
293 retval = (channel->write_error)(channel, block, count, buf,
294 size, actual, retval);
300 * Here we implement the cache functions
303 /* Allocate the cache buffers */
304 static errcode_t alloc_cache(io_channel channel,
305 struct unix_private_data *data)
308 struct unix_cache *cache;
311 data->access_time = 0;
312 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
314 cache->access_time = 0;
318 ext2fs_free_mem(&cache->buf);
319 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
323 if (channel->align) {
325 ext2fs_free_mem(&data->bounce);
326 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
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 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
441 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
442 return open64(pathname, flags, mode);
444 return open64(pathname, flags);
446 return open(pathname, flags, mode);
448 return open(pathname, flags);
452 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
454 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
455 return stat64(path, buf);
457 return stat(path, buf);
461 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
463 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
464 return fstat64(fd, buf);
466 return fstat(fd, buf);
470 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
472 io_channel io = NULL;
473 struct unix_private_data *data = NULL;
477 ext2fs_struct_stat st;
483 return EXT2_ET_BAD_DEVICE_NAME;
484 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
487 memset(io, 0, sizeof(struct struct_io_channel));
488 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
489 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
493 io->manager = unix_io_manager;
494 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
498 strcpy(io->name, name);
499 io->private_data = data;
500 io->block_size = 1024;
505 memset(data, 0, sizeof(struct unix_private_data));
506 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
507 data->io_stats.num_fields = 2;
510 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
511 if (flags & IO_FLAG_EXCLUSIVE)
512 open_flags |= O_EXCL;
513 #if defined(O_DIRECT)
514 if (flags & IO_FLAG_DIRECT_IO) {
515 open_flags |= O_DIRECT;
516 io->align = ext2fs_get_dio_alignment(data->dev);
518 #elif defined(F_NOCACHE)
519 if (flags & IO_FLAG_DIRECT_IO) {
520 f_nocache = F_NOCACHE;
526 data->dev = ext2fs_open_file(io->name, open_flags, 0);
532 if (fcntl(data->dev, f_nocache, 1) < 0) {
539 * If the device is really a block device, then set the
540 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
541 * because we are going to use punch hole instead of discard
542 * and if it succeed, subsequent read from sparse area returns
545 if (ext2fs_stat(io->name, &st) == 0) {
546 if (S_ISBLK(st.st_mode))
547 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
549 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
552 #ifdef BLKDISCARDZEROES
555 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
557 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
561 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
563 * Some operating systems require that the buffers be aligned,
564 * regardless of O_DIRECT
571 if ((retval = alloc_cache(io, data)))
575 if (flags & IO_FLAG_RW) {
579 /* Is the block device actually writable? */
580 error = ioctl(data->dev, BLKROGET, &readonly);
581 if (!error && readonly) {
590 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
591 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
593 #define RLIM_INFINITY (~0UL)
596 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
597 * block devices are wrongly getting hit by the filesize
598 * limit. This workaround isn't perfect, since it won't work
599 * if glibc wasn't built against 2.2 header files. (Sigh.)
602 if ((flags & IO_FLAG_RW) &&
604 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
605 (ut.release[2] == '4') && (ut.release[3] == '.') &&
606 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
607 (ut.release[5] < '8')) &&
608 (ext2fs_stat(io->name, &st) == 0) &&
609 (S_ISBLK(st.st_mode))) {
612 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
613 setrlimit(RLIMIT_FSIZE, &rlim);
614 getrlimit(RLIMIT_FSIZE, &rlim);
615 if (((unsigned long) rlim.rlim_cur) <
616 ((unsigned long) rlim.rlim_max)) {
617 rlim.rlim_cur = rlim.rlim_max;
618 setrlimit(RLIMIT_FSIZE, &rlim);
630 ext2fs_free_mem(&data);
634 ext2fs_free_mem(&io->name);
636 ext2fs_free_mem(&io);
641 static errcode_t unix_close(io_channel channel)
643 struct unix_private_data *data;
644 errcode_t retval = 0;
646 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
647 data = (struct unix_private_data *) channel->private_data;
648 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
650 if (--channel->refcount > 0)
654 retval = flush_cached_blocks(channel, data, 0);
657 if (close(data->dev) < 0)
661 ext2fs_free_mem(&channel->private_data);
663 ext2fs_free_mem(&channel->name);
664 ext2fs_free_mem(&channel);
668 static errcode_t unix_set_blksize(io_channel channel, int blksize)
670 struct unix_private_data *data;
673 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
674 data = (struct unix_private_data *) channel->private_data;
675 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
677 if (channel->block_size != blksize) {
679 if ((retval = flush_cached_blocks(channel, data, 0)))
683 channel->block_size = blksize;
685 if ((retval = alloc_cache(channel, data)))
692 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
693 int count, void *buf)
695 struct unix_private_data *data;
696 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
701 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
702 data = (struct unix_private_data *) channel->private_data;
703 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
706 return raw_read_blk(channel, data, block, count, buf);
709 * If we're doing an odd-sized read or a very large read,
710 * flush out the cache and then do a direct read.
712 if (count < 0 || count > WRITE_DIRECT_SIZE) {
713 if ((retval = flush_cached_blocks(channel, data, 0)))
715 return raw_read_blk(channel, data, block, count, buf);
720 /* If it's in the cache, use it! */
721 if ((cache = find_cached_block(data, block, &reuse[0]))) {
723 printf("Using cached block %lu\n", block);
725 memcpy(cp, cache->buf, channel->block_size);
728 cp += channel->block_size;
733 * Special case where we read directly into the
734 * cache buffer; important in the O_DIRECT case
737 reuse_cache(channel, data, cache, block);
738 if ((retval = raw_read_blk(channel, data, block, 1,
743 memcpy(cp, cache->buf, channel->block_size);
748 * Find the number of uncached blocks so we can do a
749 * single read request
751 for (i=1; i < count; i++)
752 if (find_cached_block(data, block+i, &reuse[i]))
755 printf("Reading %d blocks starting at %lu\n", i, block);
757 if ((retval = raw_read_blk(channel, data, block, i, cp)))
760 /* Save the results in the cache */
761 for (j=0; j < i; j++) {
764 reuse_cache(channel, data, cache, block++);
765 memcpy(cache->buf, cp, channel->block_size);
766 cp += channel->block_size;
770 #endif /* NO_IO_CACHE */
773 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
774 int count, void *buf)
776 return unix_read_blk64(channel, block, count, buf);
779 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
780 int count, const void *buf)
782 struct unix_private_data *data;
783 struct unix_cache *cache, *reuse;
784 errcode_t retval = 0;
788 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
789 data = (struct unix_private_data *) channel->private_data;
790 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
793 return raw_write_blk(channel, data, block, count, buf);
796 * If we're doing an odd-sized write or a very large write,
797 * flush out the cache completely and then do a direct write.
799 if (count < 0 || count > WRITE_DIRECT_SIZE) {
800 if ((retval = flush_cached_blocks(channel, data, 1)))
802 return raw_write_blk(channel, data, block, count, buf);
806 * For a moderate-sized multi-block write, first force a write
807 * if we're in write-through cache mode, and then fill the
808 * cache with the blocks.
810 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
812 retval = raw_write_blk(channel, data, block, count, buf);
816 cache = find_cached_block(data, block, &reuse);
819 reuse_cache(channel, data, cache, block);
821 memcpy(cache->buf, cp, channel->block_size);
822 cache->dirty = !writethrough;
825 cp += channel->block_size;
828 #endif /* NO_IO_CACHE */
831 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
832 int count, const void *buf)
834 return unix_write_blk64(channel, block, count, buf);
837 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
838 int size, const void *buf)
840 struct unix_private_data *data;
841 errcode_t retval = 0;
844 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
845 data = (struct unix_private_data *) channel->private_data;
846 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
848 if (channel->align != 0) {
850 printf("unix_write_byte: O_DIRECT fallback\n");
852 return EXT2_ET_UNIMPLEMENTED;
857 * Flush out the cache completely
859 if ((retval = flush_cached_blocks(channel, data, 1)))
863 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
866 actual = write(data->dev, buf, size);
868 return EXT2_ET_SHORT_WRITE;
874 * Flush data buffers to disk.
876 static errcode_t unix_flush(io_channel channel)
878 struct unix_private_data *data;
879 errcode_t retval = 0;
881 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
882 data = (struct unix_private_data *) channel->private_data;
883 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
886 retval = flush_cached_blocks(channel, data, 0);
892 static errcode_t unix_set_option(io_channel channel, const char *option,
895 struct unix_private_data *data;
896 unsigned long long tmp;
899 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
900 data = (struct unix_private_data *) channel->private_data;
901 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
903 if (!strcmp(option, "offset")) {
905 return EXT2_ET_INVALID_ARGUMENT;
907 tmp = strtoull(arg, &end, 0);
909 return EXT2_ET_INVALID_ARGUMENT;
911 if (data->offset < 0)
912 return EXT2_ET_INVALID_ARGUMENT;
915 return EXT2_ET_INVALID_ARGUMENT;
918 #if defined(__linux__) && !defined(BLKDISCARD)
919 #define BLKDISCARD _IO(0x12,119)
922 static errcode_t unix_discard(io_channel channel, unsigned long long block,
923 unsigned long long count)
925 struct unix_private_data *data;
928 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
929 data = (struct unix_private_data *) channel->private_data;
930 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
932 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
936 range[0] = (__uint64_t)(block) * channel->block_size;
937 range[1] = (__uint64_t)(count) * channel->block_size;
939 ret = ioctl(data->dev, BLKDISCARD, &range);
944 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
946 * If we are not on block device, try to use punch hole
947 * to reclaim free space.
949 ret = fallocate(data->dev,
950 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
951 (off_t)(block) * channel->block_size,
952 (off_t)(count) * channel->block_size);
958 if (errno == EOPNOTSUPP)
964 return EXT2_ET_UNIMPLEMENTED;