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 #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
19 #define _XOPEN_SOURCE 600
20 #define _DARWIN_C_SOURCE
21 #define _FILE_OFFSET_BITS 64
22 #ifndef _LARGEFILE_SOURCE
23 #define _LARGEFILE_SOURCE
25 #ifndef _LARGEFILE64_SOURCE
26 #define _LARGEFILE64_SOURCE
45 #include <sys/utsname.h>
48 #include <sys/types.h>
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_SYS_MOUNT_H
54 #include <sys/mount.h>
56 #ifdef HAVE_SYS_PRCTL_H
57 #include <sys/prctl.h>
59 #define PR_GET_DUMPABLE 3
64 #if HAVE_SYS_RESOURCE_H
65 #include <sys/resource.h>
67 #if HAVE_LINUX_FALLOC_H
68 #include <linux/falloc.h>
71 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
72 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
82 * For checking structure magic numbers...
85 #define EXT2_CHECK_MAGIC(struct, code) \
86 if ((struct)->magic != (code)) return (code)
90 unsigned long long block;
97 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
98 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
100 struct unix_private_data {
107 struct unix_cache cache[CACHE_SIZE];
109 struct struct_io_stats io_stats;
112 #define IS_ALIGNED(n, align) ((((uintptr_t) n) & \
113 ((uintptr_t) ((align)-1))) == 0)
115 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
117 errcode_t retval = 0;
119 struct unix_private_data *data;
121 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
122 data = (struct unix_private_data *) channel->private_data;
123 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
126 *stats = &data->io_stats;
131 static char *safe_getenv(const char *arg)
133 if ((getuid() != geteuid()) || (getgid() != getegid()))
136 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
139 #if (defined(linux) && defined(SYS_prctl))
140 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
145 #if defined(HAVE_SECURE_GETENV)
146 return secure_getenv(arg);
147 #elif defined(HAVE___SECURE_GETENV)
148 return __secure_getenv(arg);
155 * Here are the raw I/O functions
157 static errcode_t raw_read_blk(io_channel channel,
158 struct unix_private_data *data,
159 unsigned long long block,
160 int count, void *bufv)
164 ext2_loff_t location;
166 unsigned char *buf = bufv;
167 ssize_t really_read = 0;
169 size = (count < 0) ? -count : count * channel->block_size;
170 data->io_stats.bytes_read += size;
171 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
173 if (data->flags & IO_FLAG_FORCE_BOUNCE) {
174 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
175 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
182 /* Try an aligned pread */
183 if ((channel->align == 0) ||
184 (IS_ALIGNED(buf, channel->align) &&
185 IS_ALIGNED(size, channel->align))) {
186 actual = pread64(data->dev, buf, size, location);
192 /* Try an aligned pread */
193 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
194 ((channel->align == 0) ||
195 (IS_ALIGNED(buf, channel->align) &&
196 IS_ALIGNED(size, channel->align)))) {
197 actual = pread(data->dev, buf, size, location);
202 #endif /* HAVE_PREAD */
204 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
205 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
208 if ((channel->align == 0) ||
209 (IS_ALIGNED(buf, channel->align) &&
210 IS_ALIGNED(size, channel->align))) {
211 actual = read(data->dev, buf, size);
212 if (actual != size) {
218 retval = EXT2_ET_SHORT_READ;
225 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
226 (unsigned long) size);
230 * The buffer or size which we're trying to read isn't aligned
231 * to the O_DIRECT rules, so we need to do this the hard way...
235 actual = read(data->dev, data->bounce, channel->block_size);
236 if (actual != channel->block_size) {
237 actual = really_read;
243 if (size > channel->block_size)
244 actual = channel->block_size;
245 memcpy(buf, data->bounce, actual);
246 really_read += actual;
253 if (actual >= 0 && actual < size)
254 memset((char *) buf+actual, 0, size-actual);
255 if (channel->read_error)
256 retval = (channel->read_error)(channel, block, count, buf,
257 size, actual, retval);
261 static errcode_t raw_write_blk(io_channel channel,
262 struct unix_private_data *data,
263 unsigned long long block,
264 int count, const void *bufv)
267 ext2_loff_t location;
270 const unsigned char *buf = bufv;
273 size = channel->block_size;
278 size = count * channel->block_size;
280 data->io_stats.bytes_written += size;
282 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
284 if (data->flags & IO_FLAG_FORCE_BOUNCE) {
285 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
286 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
293 /* Try an aligned pwrite */
294 if ((channel->align == 0) ||
295 (IS_ALIGNED(buf, channel->align) &&
296 IS_ALIGNED(size, channel->align))) {
297 actual = pwrite64(data->dev, buf, size, location);
302 /* Try an aligned pwrite */
303 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
304 ((channel->align == 0) ||
305 (IS_ALIGNED(buf, channel->align) &&
306 IS_ALIGNED(size, channel->align)))) {
307 actual = pwrite(data->dev, buf, size, location);
311 #endif /* HAVE_PWRITE */
313 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
314 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
318 if ((channel->align == 0) ||
319 (IS_ALIGNED(buf, channel->align) &&
320 IS_ALIGNED(size, channel->align))) {
321 actual = write(data->dev, buf, size);
326 if (actual != size) {
328 retval = EXT2_ET_SHORT_WRITE;
335 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
336 (unsigned long) size);
339 * The buffer or size which we're trying to write isn't aligned
340 * to the O_DIRECT rules, so we need to do this the hard way...
344 if (size < channel->block_size) {
345 actual = read(data->dev, data->bounce,
346 channel->block_size);
347 if (actual != channel->block_size) {
352 memset((char *) data->bounce + actual, 0,
353 channel->block_size - actual);
357 if (size > channel->block_size)
358 actual = channel->block_size;
359 memcpy(data->bounce, buf, actual);
360 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
361 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
364 actual = write(data->dev, data->bounce, channel->block_size);
369 if (actual != channel->block_size)
378 if (channel->write_error)
379 retval = (channel->write_error)(channel, block, count, buf,
380 size, actual, retval);
386 * Here we implement the cache functions
389 /* Allocate the cache buffers */
390 static errcode_t alloc_cache(io_channel channel,
391 struct unix_private_data *data)
394 struct unix_cache *cache;
397 data->access_time = 0;
398 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
400 cache->access_time = 0;
404 ext2fs_free_mem(&cache->buf);
405 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
409 if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
411 ext2fs_free_mem(&data->bounce);
412 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
417 /* Free the cache buffers */
418 static void free_cache(struct unix_private_data *data)
420 struct unix_cache *cache;
423 data->access_time = 0;
424 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
426 cache->access_time = 0;
430 ext2fs_free_mem(&cache->buf);
433 ext2fs_free_mem(&data->bounce);
438 * Try to find a block in the cache. If the block is not found, and
439 * eldest is a non-zero pointer, then fill in eldest with the cache
440 * entry to that should be reused.
442 static struct unix_cache *find_cached_block(struct unix_private_data *data,
443 unsigned long long block,
444 struct unix_cache **eldest)
446 struct unix_cache *cache, *unused_cache, *oldest_cache;
449 unused_cache = oldest_cache = 0;
450 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
451 if (!cache->in_use) {
453 unused_cache = cache;
456 if (cache->block == block) {
457 cache->access_time = ++data->access_time;
461 (cache->access_time < oldest_cache->access_time))
462 oldest_cache = cache;
465 *eldest = (unused_cache) ? unused_cache : oldest_cache;
470 * Reuse a particular cache entry for another block.
472 static void reuse_cache(io_channel channel, struct unix_private_data *data,
473 struct unix_cache *cache, unsigned long long block)
475 if (cache->dirty && cache->in_use)
476 raw_write_blk(channel, data, cache->block, 1, cache->buf);
480 cache->block = block;
481 cache->access_time = ++data->access_time;
485 * Flush all of the blocks in the cache
487 static errcode_t flush_cached_blocks(io_channel channel,
488 struct unix_private_data *data,
492 struct unix_cache *cache;
493 errcode_t retval, retval2;
497 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
507 retval = raw_write_blk(channel, data,
508 cache->block, 1, cache->buf);
516 #endif /* NO_IO_CACHE */
519 #ifndef BLKDISCARDZEROES
520 #define BLKDISCARDZEROES _IO(0x12,124)
524 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
527 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
528 return open64(pathname, flags, mode);
530 return open64(pathname, flags);
532 return open(pathname, flags, mode);
534 return open(pathname, flags);
538 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
540 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
541 return stat64(path, buf);
543 return stat(path, buf);
547 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
549 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
550 return fstat64(fd, buf);
552 return fstat(fd, buf);
557 static errcode_t unix_open_channel(const char *name, int fd,
558 int flags, io_channel *channel,
561 io_channel io = NULL;
562 struct unix_private_data *data = NULL;
564 ext2fs_struct_stat st;
569 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
570 flags |= IO_FLAG_FORCE_BOUNCE;
572 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
575 memset(io, 0, sizeof(struct struct_io_channel));
576 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
577 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
581 io->manager = io_mgr;
582 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
586 strcpy(io->name, name);
587 io->private_data = data;
588 io->block_size = 1024;
593 memset(data, 0, sizeof(struct unix_private_data));
594 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
595 data->io_stats.num_fields = 2;
599 #if defined(O_DIRECT)
600 if (flags & IO_FLAG_DIRECT_IO)
601 io->align = ext2fs_get_dio_alignment(data->dev);
602 #elif defined(F_NOCACHE)
603 if (flags & IO_FLAG_DIRECT_IO)
608 * If the device is really a block device, then set the
609 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
610 * because we are going to use punch hole instead of discard
611 * and if it succeed, subsequent read from sparse area returns
614 if (ext2fs_fstat(data->dev, &st) == 0) {
615 if (ext2fsP_is_disk_device(st.st_mode))
616 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
618 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
621 #ifdef BLKDISCARDZEROES
624 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
626 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
630 #if defined(__CYGWIN__)
632 * Some operating systems require that the buffers be aligned,
633 * regardless of O_DIRECT
639 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
640 if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
641 int dio_align = ext2fs_get_dio_alignment(fd);
643 if (io->align < dio_align)
644 io->align = dio_align;
648 if ((retval = alloc_cache(io, data)))
652 if (flags & IO_FLAG_RW) {
656 /* Is the block device actually writable? */
657 error = ioctl(data->dev, BLKROGET, &readonly);
658 if (!error && readonly) {
667 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
668 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
670 #define RLIM_INFINITY (~0UL)
673 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
674 * block devices are wrongly getting hit by the filesize
675 * limit. This workaround isn't perfect, since it won't work
676 * if glibc wasn't built against 2.2 header files. (Sigh.)
679 if ((flags & IO_FLAG_RW) &&
681 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
682 (ut.release[2] == '4') && (ut.release[3] == '.') &&
683 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
684 (ut.release[5] < '8')) &&
685 (ext2fs_fstat(data->dev, &st) == 0) &&
686 (ext2fsP_is_disk_device(st.st_mode))) {
689 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
690 setrlimit(RLIMIT_FSIZE, &rlim);
691 getrlimit(RLIMIT_FSIZE, &rlim);
692 if (((unsigned long) rlim.rlim_cur) <
693 ((unsigned long) rlim.rlim_max)) {
694 rlim.rlim_cur = rlim.rlim_max;
695 setrlimit(RLIMIT_FSIZE, &rlim);
707 ext2fs_free_mem(&data);
711 ext2fs_free_mem(&io->name);
713 ext2fs_free_mem(&io);
718 static errcode_t unixfd_open(const char *str_fd, int flags,
725 #if defined(HAVE_FCNTL)
726 fd_flags = fcntl(fd, F_GETFD);
731 if (fd_flags & O_RDWR)
733 if (fd_flags & O_EXCL)
734 flags |= IO_FLAG_EXCLUSIVE;
735 #if defined(O_DIRECT)
736 if (fd_flags & O_DIRECT)
737 flags |= IO_FLAG_DIRECT_IO;
739 #endif /* HAVE_FCNTL */
741 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
744 static errcode_t unix_open(const char *name, int flags,
751 return EXT2_ET_BAD_DEVICE_NAME;
753 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
754 if (flags & IO_FLAG_EXCLUSIVE)
755 open_flags |= O_EXCL;
756 #if defined(O_DIRECT)
757 if (flags & IO_FLAG_DIRECT_IO)
758 open_flags |= O_DIRECT;
760 fd = ext2fs_open_file(name, open_flags, 0);
763 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
764 if (flags & IO_FLAG_DIRECT_IO) {
765 if (fcntl(fd, F_NOCACHE, 1) < 0)
769 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
772 static errcode_t unix_close(io_channel channel)
774 struct unix_private_data *data;
775 errcode_t retval = 0;
777 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
778 data = (struct unix_private_data *) channel->private_data;
779 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
781 if (--channel->refcount > 0)
785 retval = flush_cached_blocks(channel, data, 0);
788 if (close(data->dev) < 0)
792 ext2fs_free_mem(&channel->private_data);
794 ext2fs_free_mem(&channel->name);
795 ext2fs_free_mem(&channel);
799 static errcode_t unix_set_blksize(io_channel channel, int blksize)
801 struct unix_private_data *data;
804 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
805 data = (struct unix_private_data *) channel->private_data;
806 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
808 if (channel->block_size != blksize) {
810 if ((retval = flush_cached_blocks(channel, data, 0)))
814 channel->block_size = blksize;
816 if ((retval = alloc_cache(channel, data)))
822 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
823 int count, void *buf)
825 struct unix_private_data *data;
826 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
831 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
832 data = (struct unix_private_data *) channel->private_data;
833 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
836 return raw_read_blk(channel, data, block, count, buf);
839 * If we're doing an odd-sized read or a very large read,
840 * flush out the cache and then do a direct read.
842 if (count < 0 || count > WRITE_DIRECT_SIZE) {
843 if ((retval = flush_cached_blocks(channel, data, 0)))
845 return raw_read_blk(channel, data, block, count, buf);
850 /* If it's in the cache, use it! */
851 if ((cache = find_cached_block(data, block, &reuse[0]))) {
853 printf("Using cached block %lu\n", block);
855 memcpy(cp, cache->buf, channel->block_size);
858 cp += channel->block_size;
863 * Special case where we read directly into the
864 * cache buffer; important in the O_DIRECT case
867 reuse_cache(channel, data, cache, block);
868 if ((retval = raw_read_blk(channel, data, block, 1,
873 memcpy(cp, cache->buf, channel->block_size);
878 * Find the number of uncached blocks so we can do a
879 * single read request
881 for (i=1; i < count; i++)
882 if (find_cached_block(data, block+i, &reuse[i]))
885 printf("Reading %d blocks starting at %lu\n", i, block);
887 if ((retval = raw_read_blk(channel, data, block, i, cp)))
890 /* Save the results in the cache */
891 for (j=0; j < i; j++) {
894 reuse_cache(channel, data, cache, block++);
895 memcpy(cache->buf, cp, channel->block_size);
896 cp += channel->block_size;
900 #endif /* NO_IO_CACHE */
903 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
904 int count, void *buf)
906 return unix_read_blk64(channel, block, count, buf);
909 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
910 int count, const void *buf)
912 struct unix_private_data *data;
913 struct unix_cache *cache, *reuse;
914 errcode_t retval = 0;
918 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
919 data = (struct unix_private_data *) channel->private_data;
920 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
923 return raw_write_blk(channel, data, block, count, buf);
926 * If we're doing an odd-sized write or a very large write,
927 * flush out the cache completely and then do a direct write.
929 if (count < 0 || count > WRITE_DIRECT_SIZE) {
930 if ((retval = flush_cached_blocks(channel, data, 1)))
932 return raw_write_blk(channel, data, block, count, buf);
936 * For a moderate-sized multi-block write, first force a write
937 * if we're in write-through cache mode, and then fill the
938 * cache with the blocks.
940 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
942 retval = raw_write_blk(channel, data, block, count, buf);
946 cache = find_cached_block(data, block, &reuse);
949 reuse_cache(channel, data, cache, block);
951 if (cache->buf != cp)
952 memcpy(cache->buf, cp, channel->block_size);
953 cache->dirty = !writethrough;
956 cp += channel->block_size;
959 #endif /* NO_IO_CACHE */
962 static errcode_t unix_cache_readahead(io_channel channel,
963 unsigned long long block,
964 unsigned long long count)
966 #ifdef POSIX_FADV_WILLNEED
967 struct unix_private_data *data;
969 data = (struct unix_private_data *)channel->private_data;
970 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
971 return posix_fadvise(data->dev,
972 (ext2_loff_t)block * channel->block_size + data->offset,
973 (ext2_loff_t)count * channel->block_size,
974 POSIX_FADV_WILLNEED);
976 return EXT2_ET_OP_NOT_SUPPORTED;
980 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
981 int count, const void *buf)
983 return unix_write_blk64(channel, block, count, buf);
986 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
987 int size, const void *buf)
989 struct unix_private_data *data;
990 errcode_t retval = 0;
993 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
994 data = (struct unix_private_data *) channel->private_data;
995 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
997 if (channel->align != 0) {
999 printf("unix_write_byte: O_DIRECT fallback\n");
1001 return EXT2_ET_UNIMPLEMENTED;
1006 * Flush out the cache completely
1008 if ((retval = flush_cached_blocks(channel, data, 1)))
1012 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
1015 actual = write(data->dev, buf, size);
1019 return EXT2_ET_SHORT_WRITE;
1025 * Flush data buffers to disk.
1027 static errcode_t unix_flush(io_channel channel)
1029 struct unix_private_data *data;
1030 errcode_t retval = 0;
1032 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1033 data = (struct unix_private_data *) channel->private_data;
1034 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1037 retval = flush_cached_blocks(channel, data, 0);
1040 if (!retval && fsync(data->dev) != 0)
1046 static errcode_t unix_set_option(io_channel channel, const char *option,
1049 struct unix_private_data *data;
1050 unsigned long long tmp;
1053 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1054 data = (struct unix_private_data *) channel->private_data;
1055 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1057 if (!strcmp(option, "offset")) {
1059 return EXT2_ET_INVALID_ARGUMENT;
1061 tmp = strtoull(arg, &end, 0);
1063 return EXT2_ET_INVALID_ARGUMENT;
1065 if (data->offset < 0)
1066 return EXT2_ET_INVALID_ARGUMENT;
1069 return EXT2_ET_INVALID_ARGUMENT;
1072 #if defined(__linux__) && !defined(BLKDISCARD)
1073 #define BLKDISCARD _IO(0x12,119)
1076 static errcode_t unix_discard(io_channel channel, unsigned long long block,
1077 unsigned long long count)
1079 struct unix_private_data *data;
1082 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1083 data = (struct unix_private_data *) channel->private_data;
1084 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1086 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1090 range[0] = (__u64)(block) * channel->block_size + data->offset;
1091 range[1] = (__u64)(count) * channel->block_size;
1093 ret = ioctl(data->dev, BLKDISCARD, &range);
1098 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1100 * If we are not on block device, try to use punch hole
1101 * to reclaim free space.
1103 ret = fallocate(data->dev,
1104 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1105 (off_t)(block) * channel->block_size + data->offset,
1106 (off_t)(count) * channel->block_size);
1112 if (errno == EOPNOTSUPP)
1118 return EXT2_ET_UNIMPLEMENTED;
1121 /* parameters might not be used if OS doesn't support zeroout */
1122 #pragma GCC diagnostic push
1123 #pragma GCC diagnostic ignored "-Wunused-parameter"
1124 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1125 unsigned long long count)
1127 struct unix_private_data *data;
1130 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1131 data = (struct unix_private_data *) channel->private_data;
1132 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1134 if (safe_getenv("UNIX_IO_NOZEROOUT"))
1137 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1138 /* Not implemented until the BLKZEROOUT mess is fixed */
1141 /* Regular file, try to use truncate/punch/zero. */
1142 struct stat statbuf;
1147 * If we're trying to zero a range past the end of the file,
1148 * extend the file size, then truncate everything.
1150 ret = fstat(data->dev, &statbuf);
1153 if ((unsigned long long) statbuf.st_size <
1154 (block + count) * channel->block_size + data->offset) {
1155 ret = ftruncate(data->dev,
1156 (block + count) * channel->block_size + data->offset);
1160 #if defined(HAVE_FALLOCATE) && (defined(FALLOC_FL_ZERO_RANGE) || \
1161 (defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)))
1162 #if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1163 ret = fallocate(data->dev,
1164 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1165 (off_t)(block) * channel->block_size + data->offset,
1166 (off_t)(count) * channel->block_size);
1170 #ifdef FALLOC_FL_ZERO_RANGE
1171 ret = fallocate(data->dev,
1172 FALLOC_FL_ZERO_RANGE,
1173 (off_t)(block) * channel->block_size + data->offset,
1174 (off_t)(count) * channel->block_size);
1178 #endif /* HAVE_FALLOCATE && (ZERO_RANGE || (PUNCH_HOLE && KEEP_SIZE)) */
1182 if (errno == EOPNOTSUPP)
1188 return EXT2_ET_UNIMPLEMENTED;
1190 #pragma GCC diagnostic pop
1192 static struct struct_io_manager struct_unix_manager = {
1193 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1194 .name = "Unix I/O Manager",
1196 .close = unix_close,
1197 .set_blksize = unix_set_blksize,
1198 .read_blk = unix_read_blk,
1199 .write_blk = unix_write_blk,
1200 .flush = unix_flush,
1201 .write_byte = unix_write_byte,
1202 .set_option = unix_set_option,
1203 .get_stats = unix_get_stats,
1204 .read_blk64 = unix_read_blk64,
1205 .write_blk64 = unix_write_blk64,
1206 .discard = unix_discard,
1207 .cache_readahead = unix_cache_readahead,
1208 .zeroout = unix_zeroout,
1211 io_manager unix_io_manager = &struct_unix_manager;
1213 static struct struct_io_manager struct_unixfd_manager = {
1214 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1215 .name = "Unix fd I/O Manager",
1216 .open = unixfd_open,
1217 .close = unix_close,
1218 .set_blksize = unix_set_blksize,
1219 .read_blk = unix_read_blk,
1220 .write_blk = unix_write_blk,
1221 .flush = unix_flush,
1222 .write_byte = unix_write_byte,
1223 .set_option = unix_set_option,
1224 .get_stats = unix_get_stats,
1225 .read_blk64 = unix_read_blk64,
1226 .write_blk64 = unix_write_blk64,
1227 .discard = unix_discard,
1228 .cache_readahead = unix_cache_readahead,
1229 .zeroout = unix_zeroout,
1232 io_manager unixfd_io_manager = &struct_unixfd_manager;