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). */
81 * For checking structure magic numbers...
84 #define EXT2_CHECK_MAGIC(struct, code) \
85 if ((struct)->magic != (code)) return (code)
89 unsigned long long block;
96 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
97 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
99 struct unix_private_data {
106 struct unix_cache cache[CACHE_SIZE];
108 struct struct_io_stats io_stats;
111 #define IS_ALIGNED(n, align) ((((uintptr_t) n) & \
112 ((uintptr_t) ((align)-1))) == 0)
114 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
116 errcode_t retval = 0;
118 struct unix_private_data *data;
120 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
121 data = (struct unix_private_data *) channel->private_data;
122 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
125 *stats = &data->io_stats;
130 static char *safe_getenv(const char *arg)
132 if ((getuid() != geteuid()) || (getgid() != getegid()))
135 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
138 #if (defined(linux) && defined(SYS_prctl))
139 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
144 #if defined(HAVE_SECURE_GETENV)
145 return secure_getenv(arg);
146 #elif defined(HAVE___SECURE_GETENV)
147 return __secure_getenv(arg);
154 * Here are the raw I/O functions
156 static errcode_t raw_read_blk(io_channel channel,
157 struct unix_private_data *data,
158 unsigned long long block,
159 int count, void *bufv)
163 ext2_loff_t location;
165 unsigned char *buf = bufv;
166 ssize_t really_read = 0;
168 size = (count < 0) ? -count : count * channel->block_size;
169 data->io_stats.bytes_read += size;
170 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
172 if (data->flags & IO_FLAG_FORCE_BOUNCE) {
173 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
174 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
181 /* Try an aligned pread */
182 if ((channel->align == 0) ||
183 (IS_ALIGNED(buf, channel->align) &&
184 IS_ALIGNED(size, channel->align))) {
185 actual = pread64(data->dev, buf, size, location);
190 /* Try an aligned pread */
191 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
192 ((channel->align == 0) ||
193 (IS_ALIGNED(buf, channel->align) &&
194 IS_ALIGNED(size, channel->align)))) {
195 actual = pread(data->dev, buf, size, location);
199 #endif /* HAVE_PREAD */
201 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
202 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
205 if ((channel->align == 0) ||
206 (IS_ALIGNED(buf, channel->align) &&
207 IS_ALIGNED(size, channel->align))) {
208 actual = read(data->dev, buf, size);
209 if (actual != size) {
215 retval = EXT2_ET_SHORT_READ;
222 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
223 (unsigned long) size);
227 * The buffer or size which we're trying to read isn't aligned
228 * to the O_DIRECT rules, so we need to do this the hard way...
232 actual = read(data->dev, data->bounce, channel->block_size);
233 if (actual != channel->block_size) {
234 actual = really_read;
240 if (size > channel->block_size)
241 actual = channel->block_size;
242 memcpy(buf, data->bounce, actual);
243 really_read += actual;
250 memset((char *) buf+actual, 0, size-actual);
251 if (channel->read_error)
252 retval = (channel->read_error)(channel, block, count, buf,
253 size, actual, retval);
257 static errcode_t raw_write_blk(io_channel channel,
258 struct unix_private_data *data,
259 unsigned long long block,
260 int count, const void *bufv)
263 ext2_loff_t location;
266 const unsigned char *buf = bufv;
269 size = channel->block_size;
274 size = count * channel->block_size;
276 data->io_stats.bytes_written += size;
278 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
280 if (data->flags & IO_FLAG_FORCE_BOUNCE) {
281 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
282 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
289 /* Try an aligned pwrite */
290 if ((channel->align == 0) ||
291 (IS_ALIGNED(buf, channel->align) &&
292 IS_ALIGNED(size, channel->align))) {
293 actual = pwrite64(data->dev, buf, size, location);
298 /* Try an aligned pwrite */
299 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
300 ((channel->align == 0) ||
301 (IS_ALIGNED(buf, channel->align) &&
302 IS_ALIGNED(size, channel->align)))) {
303 actual = pwrite(data->dev, buf, size, location);
307 #endif /* HAVE_PWRITE */
309 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
310 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
314 if ((channel->align == 0) ||
315 (IS_ALIGNED(buf, channel->align) &&
316 IS_ALIGNED(size, channel->align))) {
317 actual = write(data->dev, buf, size);
322 if (actual != size) {
324 retval = EXT2_ET_SHORT_WRITE;
331 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
332 (unsigned long) size);
335 * The buffer or size which we're trying to write isn't aligned
336 * to the O_DIRECT rules, so we need to do this the hard way...
340 if (size < channel->block_size) {
341 actual = read(data->dev, data->bounce,
342 channel->block_size);
343 if (actual != channel->block_size) {
348 memset(data->bounce + actual, 0,
349 channel->block_size - actual);
353 if (size > channel->block_size)
354 actual = channel->block_size;
355 memcpy(data->bounce, buf, actual);
356 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
357 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
360 actual = write(data->dev, data->bounce, channel->block_size);
365 if (actual != channel->block_size)
374 if (channel->write_error)
375 retval = (channel->write_error)(channel, block, count, buf,
376 size, actual, retval);
382 * Here we implement the cache functions
385 /* Allocate the cache buffers */
386 static errcode_t alloc_cache(io_channel channel,
387 struct unix_private_data *data)
390 struct unix_cache *cache;
393 data->access_time = 0;
394 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
396 cache->access_time = 0;
400 ext2fs_free_mem(&cache->buf);
401 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
405 if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
407 ext2fs_free_mem(&data->bounce);
408 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
413 /* Free the cache buffers */
414 static void free_cache(struct unix_private_data *data)
416 struct unix_cache *cache;
419 data->access_time = 0;
420 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
422 cache->access_time = 0;
426 ext2fs_free_mem(&cache->buf);
429 ext2fs_free_mem(&data->bounce);
434 * Try to find a block in the cache. If the block is not found, and
435 * eldest is a non-zero pointer, then fill in eldest with the cache
436 * entry to that should be reused.
438 static struct unix_cache *find_cached_block(struct unix_private_data *data,
439 unsigned long long block,
440 struct unix_cache **eldest)
442 struct unix_cache *cache, *unused_cache, *oldest_cache;
445 unused_cache = oldest_cache = 0;
446 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
447 if (!cache->in_use) {
449 unused_cache = cache;
452 if (cache->block == block) {
453 cache->access_time = ++data->access_time;
457 (cache->access_time < oldest_cache->access_time))
458 oldest_cache = cache;
461 *eldest = (unused_cache) ? unused_cache : oldest_cache;
466 * Reuse a particular cache entry for another block.
468 static void reuse_cache(io_channel channel, struct unix_private_data *data,
469 struct unix_cache *cache, unsigned long long block)
471 if (cache->dirty && cache->in_use)
472 raw_write_blk(channel, data, cache->block, 1, cache->buf);
476 cache->block = block;
477 cache->access_time = ++data->access_time;
481 * Flush all of the blocks in the cache
483 static errcode_t flush_cached_blocks(io_channel channel,
484 struct unix_private_data *data,
488 struct unix_cache *cache;
489 errcode_t retval, retval2;
493 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
503 retval = raw_write_blk(channel, data,
504 cache->block, 1, cache->buf);
512 #endif /* NO_IO_CACHE */
515 #ifndef BLKDISCARDZEROES
516 #define BLKDISCARDZEROES _IO(0x12,124)
520 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
523 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
524 return open64(pathname, flags, mode);
526 return open64(pathname, flags);
528 return open(pathname, flags, mode);
530 return open(pathname, flags);
534 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
536 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
537 return stat64(path, buf);
539 return stat(path, buf);
543 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
545 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
546 return fstat64(fd, buf);
548 return fstat(fd, buf);
553 static errcode_t unix_open_channel(const char *name, int fd,
554 int flags, io_channel *channel,
557 io_channel io = NULL;
558 struct unix_private_data *data = NULL;
560 ext2fs_struct_stat st;
565 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
566 flags |= IO_FLAG_FORCE_BOUNCE;
568 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
571 memset(io, 0, sizeof(struct struct_io_channel));
572 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
573 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
577 io->manager = io_mgr;
578 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
582 strcpy(io->name, name);
583 io->private_data = data;
584 io->block_size = 1024;
589 memset(data, 0, sizeof(struct unix_private_data));
590 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
591 data->io_stats.num_fields = 2;
595 #if defined(O_DIRECT)
596 if (flags & IO_FLAG_DIRECT_IO)
597 io->align = ext2fs_get_dio_alignment(data->dev);
598 #elif defined(F_NOCACHE)
599 if (flags & IO_FLAG_DIRECT_IO)
604 * If the device is really a block device, then set the
605 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
606 * because we are going to use punch hole instead of discard
607 * and if it succeed, subsequent read from sparse area returns
610 if (ext2fs_fstat(data->dev, &st) == 0) {
611 if (S_ISBLK(st.st_mode))
612 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
614 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
617 #ifdef BLKDISCARDZEROES
620 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
622 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
626 #if defined(__CYGWIN__)
628 * Some operating systems require that the buffers be aligned,
629 * regardless of O_DIRECT
635 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
636 if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
637 int dio_align = ext2fs_get_dio_alignment(fd);
639 if (io->align < dio_align)
640 io->align = dio_align;
644 if ((retval = alloc_cache(io, data)))
648 if (flags & IO_FLAG_RW) {
652 /* Is the block device actually writable? */
653 error = ioctl(data->dev, BLKROGET, &readonly);
654 if (!error && readonly) {
663 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
664 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
666 #define RLIM_INFINITY (~0UL)
669 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
670 * block devices are wrongly getting hit by the filesize
671 * limit. This workaround isn't perfect, since it won't work
672 * if glibc wasn't built against 2.2 header files. (Sigh.)
675 if ((flags & IO_FLAG_RW) &&
677 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
678 (ut.release[2] == '4') && (ut.release[3] == '.') &&
679 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
680 (ut.release[5] < '8')) &&
681 (ext2fs_fstat(data->dev, &st) == 0) &&
682 (S_ISBLK(st.st_mode))) {
685 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
686 setrlimit(RLIMIT_FSIZE, &rlim);
687 getrlimit(RLIMIT_FSIZE, &rlim);
688 if (((unsigned long) rlim.rlim_cur) <
689 ((unsigned long) rlim.rlim_max)) {
690 rlim.rlim_cur = rlim.rlim_max;
691 setrlimit(RLIMIT_FSIZE, &rlim);
703 ext2fs_free_mem(&data);
707 ext2fs_free_mem(&io->name);
709 ext2fs_free_mem(&io);
714 static errcode_t unixfd_open(const char *str_fd, int flags,
721 #if defined(HAVE_FCNTL)
722 fd_flags = fcntl(fd, F_GETFD);
727 if (fd_flags & O_RDWR)
729 if (fd_flags & O_EXCL)
730 flags |= IO_FLAG_EXCLUSIVE;
731 #if defined(O_DIRECT)
732 if (fd_flags & O_DIRECT)
733 flags |= IO_FLAG_DIRECT_IO;
735 #endif /* HAVE_FCNTL */
737 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
740 static errcode_t unix_open(const char *name, int flags,
747 return EXT2_ET_BAD_DEVICE_NAME;
749 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
750 if (flags & IO_FLAG_EXCLUSIVE)
751 open_flags |= O_EXCL;
752 #if defined(O_DIRECT)
753 if (flags & IO_FLAG_DIRECT_IO)
754 open_flags |= O_DIRECT;
756 fd = ext2fs_open_file(name, open_flags, 0);
759 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
760 if (flags & IO_FLAG_DIRECT_IO) {
761 if (fcntl(fd, F_NOCACHE, 1) < 0)
765 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
768 static errcode_t unix_close(io_channel channel)
770 struct unix_private_data *data;
771 errcode_t retval = 0;
773 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
774 data = (struct unix_private_data *) channel->private_data;
775 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
777 if (--channel->refcount > 0)
781 retval = flush_cached_blocks(channel, data, 0);
784 if (close(data->dev) < 0)
788 ext2fs_free_mem(&channel->private_data);
790 ext2fs_free_mem(&channel->name);
791 ext2fs_free_mem(&channel);
795 static errcode_t unix_set_blksize(io_channel channel, int blksize)
797 struct unix_private_data *data;
800 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
801 data = (struct unix_private_data *) channel->private_data;
802 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
804 if (channel->block_size != blksize) {
806 if ((retval = flush_cached_blocks(channel, data, 0)))
810 channel->block_size = blksize;
812 if ((retval = alloc_cache(channel, data)))
818 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
819 int count, void *buf)
821 struct unix_private_data *data;
822 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
827 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
828 data = (struct unix_private_data *) channel->private_data;
829 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
832 return raw_read_blk(channel, data, block, count, buf);
835 * If we're doing an odd-sized read or a very large read,
836 * flush out the cache and then do a direct read.
838 if (count < 0 || count > WRITE_DIRECT_SIZE) {
839 if ((retval = flush_cached_blocks(channel, data, 0)))
841 return raw_read_blk(channel, data, block, count, buf);
846 /* If it's in the cache, use it! */
847 if ((cache = find_cached_block(data, block, &reuse[0]))) {
849 printf("Using cached block %lu\n", block);
851 memcpy(cp, cache->buf, channel->block_size);
854 cp += channel->block_size;
859 * Special case where we read directly into the
860 * cache buffer; important in the O_DIRECT case
863 reuse_cache(channel, data, cache, block);
864 if ((retval = raw_read_blk(channel, data, block, 1,
869 memcpy(cp, cache->buf, channel->block_size);
874 * Find the number of uncached blocks so we can do a
875 * single read request
877 for (i=1; i < count; i++)
878 if (find_cached_block(data, block+i, &reuse[i]))
881 printf("Reading %d blocks starting at %lu\n", i, block);
883 if ((retval = raw_read_blk(channel, data, block, i, cp)))
886 /* Save the results in the cache */
887 for (j=0; j < i; j++) {
890 reuse_cache(channel, data, cache, block++);
891 memcpy(cache->buf, cp, channel->block_size);
892 cp += channel->block_size;
896 #endif /* NO_IO_CACHE */
899 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
900 int count, void *buf)
902 return unix_read_blk64(channel, block, count, buf);
905 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
906 int count, const void *buf)
908 struct unix_private_data *data;
909 struct unix_cache *cache, *reuse;
910 errcode_t retval = 0;
914 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
915 data = (struct unix_private_data *) channel->private_data;
916 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
919 return raw_write_blk(channel, data, block, count, buf);
922 * If we're doing an odd-sized write or a very large write,
923 * flush out the cache completely and then do a direct write.
925 if (count < 0 || count > WRITE_DIRECT_SIZE) {
926 if ((retval = flush_cached_blocks(channel, data, 1)))
928 return raw_write_blk(channel, data, block, count, buf);
932 * For a moderate-sized multi-block write, first force a write
933 * if we're in write-through cache mode, and then fill the
934 * cache with the blocks.
936 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
938 retval = raw_write_blk(channel, data, block, count, buf);
942 cache = find_cached_block(data, block, &reuse);
945 reuse_cache(channel, data, cache, block);
947 if (cache->buf != cp)
948 memcpy(cache->buf, cp, channel->block_size);
949 cache->dirty = !writethrough;
952 cp += channel->block_size;
955 #endif /* NO_IO_CACHE */
958 static errcode_t unix_cache_readahead(io_channel channel,
959 unsigned long long block,
960 unsigned long long count)
962 #ifdef POSIX_FADV_WILLNEED
963 struct unix_private_data *data;
965 data = (struct unix_private_data *)channel->private_data;
966 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
967 return posix_fadvise(data->dev,
968 (ext2_loff_t)block * channel->block_size + data->offset,
969 (ext2_loff_t)count * channel->block_size,
970 POSIX_FADV_WILLNEED);
972 return EXT2_ET_OP_NOT_SUPPORTED;
976 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
977 int count, const void *buf)
979 return unix_write_blk64(channel, block, count, buf);
982 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
983 int size, const void *buf)
985 struct unix_private_data *data;
986 errcode_t retval = 0;
989 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
990 data = (struct unix_private_data *) channel->private_data;
991 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
993 if (channel->align != 0) {
995 printf("unix_write_byte: O_DIRECT fallback\n");
997 return EXT2_ET_UNIMPLEMENTED;
1002 * Flush out the cache completely
1004 if ((retval = flush_cached_blocks(channel, data, 1)))
1008 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
1011 actual = write(data->dev, buf, size);
1015 return EXT2_ET_SHORT_WRITE;
1021 * Flush data buffers to disk.
1023 static errcode_t unix_flush(io_channel channel)
1025 struct unix_private_data *data;
1026 errcode_t retval = 0;
1028 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1029 data = (struct unix_private_data *) channel->private_data;
1030 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1033 retval = flush_cached_blocks(channel, data, 0);
1036 if (!retval && fsync(data->dev) != 0)
1042 static errcode_t unix_set_option(io_channel channel, const char *option,
1045 struct unix_private_data *data;
1046 unsigned long long tmp;
1049 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1050 data = (struct unix_private_data *) channel->private_data;
1051 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1053 if (!strcmp(option, "offset")) {
1055 return EXT2_ET_INVALID_ARGUMENT;
1057 tmp = strtoull(arg, &end, 0);
1059 return EXT2_ET_INVALID_ARGUMENT;
1061 if (data->offset < 0)
1062 return EXT2_ET_INVALID_ARGUMENT;
1065 return EXT2_ET_INVALID_ARGUMENT;
1068 #if defined(__linux__) && !defined(BLKDISCARD)
1069 #define BLKDISCARD _IO(0x12,119)
1072 static errcode_t unix_discard(io_channel channel, unsigned long long block,
1073 unsigned long long count)
1075 struct unix_private_data *data;
1078 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1079 data = (struct unix_private_data *) channel->private_data;
1080 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1082 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1086 range[0] = (__u64)(block) * channel->block_size + data->offset;
1087 range[1] = (__u64)(count) * channel->block_size;
1089 ret = ioctl(data->dev, BLKDISCARD, &range);
1094 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1096 * If we are not on block device, try to use punch hole
1097 * to reclaim free space.
1099 ret = fallocate(data->dev,
1100 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1101 (off_t)(block) * channel->block_size + data->offset,
1102 (off_t)(count) * channel->block_size);
1108 if (errno == EOPNOTSUPP)
1114 return EXT2_ET_UNIMPLEMENTED;
1117 /* parameters might not be used if OS doesn't support zeroout */
1118 #pragma GCC diagnostic push
1119 #pragma GCC diagnostic ignored "-Wunused-parameter"
1120 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1121 unsigned long long count)
1123 struct unix_private_data *data;
1126 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1127 data = (struct unix_private_data *) channel->private_data;
1128 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1130 if (safe_getenv("UNIX_IO_NOZEROOUT"))
1133 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1134 /* Not implemented until the BLKZEROOUT mess is fixed */
1137 /* Regular file, try to use truncate/punch/zero. */
1138 struct stat statbuf;
1143 * If we're trying to zero a range past the end of the file,
1144 * extend the file size, then truncate everything.
1146 ret = fstat(data->dev, &statbuf);
1149 if ((unsigned long long) statbuf.st_size <
1150 (block + count) * channel->block_size + data->offset) {
1151 ret = ftruncate(data->dev,
1152 (block + count) * channel->block_size + data->offset);
1156 #if defined(HAVE_FALLOCATE) && (defined(FALLOC_FL_ZERO_RANGE) || \
1157 (defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)))
1158 #if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1159 ret = fallocate(data->dev,
1160 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1161 (off_t)(block) * channel->block_size + data->offset,
1162 (off_t)(count) * channel->block_size);
1166 #ifdef FALLOC_FL_ZERO_RANGE
1167 ret = fallocate(data->dev,
1168 FALLOC_FL_ZERO_RANGE,
1169 (off_t)(block) * channel->block_size + data->offset,
1170 (off_t)(count) * channel->block_size);
1174 #endif /* HAVE_FALLOCATE && (ZERO_RANGE || (PUNCH_HOLE && KEEP_SIZE)) */
1178 if (errno == EOPNOTSUPP)
1184 return EXT2_ET_UNIMPLEMENTED;
1186 #pragma GCC diagnostic pop
1188 static struct struct_io_manager struct_unix_manager = {
1189 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1190 .name = "Unix I/O Manager",
1192 .close = unix_close,
1193 .set_blksize = unix_set_blksize,
1194 .read_blk = unix_read_blk,
1195 .write_blk = unix_write_blk,
1196 .flush = unix_flush,
1197 .write_byte = unix_write_byte,
1198 .set_option = unix_set_option,
1199 .get_stats = unix_get_stats,
1200 .read_blk64 = unix_read_blk64,
1201 .write_blk64 = unix_write_blk64,
1202 .discard = unix_discard,
1203 .cache_readahead = unix_cache_readahead,
1204 .zeroout = unix_zeroout,
1207 io_manager unix_io_manager = &struct_unix_manager;
1209 static struct struct_io_manager struct_unixfd_manager = {
1210 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1211 .name = "Unix fd I/O Manager",
1212 .open = unixfd_open,
1213 .close = unix_close,
1214 .set_blksize = unix_set_blksize,
1215 .read_blk = unix_read_blk,
1216 .write_blk = unix_write_blk,
1217 .flush = unix_flush,
1218 .write_byte = unix_write_byte,
1219 .set_option = unix_set_option,
1220 .get_stats = unix_get_stats,
1221 .read_blk64 = unix_read_blk64,
1222 .write_blk64 = unix_write_blk64,
1223 .discard = unix_discard,
1224 .cache_readahead = unix_cache_readahead,
1225 .zeroout = unix_zeroout,
1228 io_manager unixfd_io_manager = &struct_unixfd_manager;