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 fd_flags = fcntl(fd, F_GETFD);
726 if (fd_flags & O_RDWR)
728 if (fd_flags & O_EXCL)
729 flags |= IO_FLAG_EXCLUSIVE;
730 #if defined(O_DIRECT)
731 if (fd_flags & O_DIRECT)
732 flags |= IO_FLAG_DIRECT_IO;
735 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
738 static errcode_t unix_open(const char *name, int flags,
745 return EXT2_ET_BAD_DEVICE_NAME;
747 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
748 if (flags & IO_FLAG_EXCLUSIVE)
749 open_flags |= O_EXCL;
750 #if defined(O_DIRECT)
751 if (flags & IO_FLAG_DIRECT_IO)
752 open_flags |= O_DIRECT;
754 fd = ext2fs_open_file(name, open_flags, 0);
757 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
758 if (flags & IO_FLAG_DIRECT_IO) {
759 if (fcntl(fd, F_NOCACHE, 1) < 0)
763 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
766 static errcode_t unix_close(io_channel channel)
768 struct unix_private_data *data;
769 errcode_t retval = 0;
771 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
772 data = (struct unix_private_data *) channel->private_data;
773 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
775 if (--channel->refcount > 0)
779 retval = flush_cached_blocks(channel, data, 0);
782 if (close(data->dev) < 0)
786 ext2fs_free_mem(&channel->private_data);
788 ext2fs_free_mem(&channel->name);
789 ext2fs_free_mem(&channel);
793 static errcode_t unix_set_blksize(io_channel channel, int blksize)
795 struct unix_private_data *data;
798 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
799 data = (struct unix_private_data *) channel->private_data;
800 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
802 if (channel->block_size != blksize) {
804 if ((retval = flush_cached_blocks(channel, data, 0)))
808 channel->block_size = blksize;
810 if ((retval = alloc_cache(channel, data)))
816 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
817 int count, void *buf)
819 struct unix_private_data *data;
820 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
825 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
826 data = (struct unix_private_data *) channel->private_data;
827 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
830 return raw_read_blk(channel, data, block, count, buf);
833 * If we're doing an odd-sized read or a very large read,
834 * flush out the cache and then do a direct read.
836 if (count < 0 || count > WRITE_DIRECT_SIZE) {
837 if ((retval = flush_cached_blocks(channel, data, 0)))
839 return raw_read_blk(channel, data, block, count, buf);
844 /* If it's in the cache, use it! */
845 if ((cache = find_cached_block(data, block, &reuse[0]))) {
847 printf("Using cached block %lu\n", block);
849 memcpy(cp, cache->buf, channel->block_size);
852 cp += channel->block_size;
857 * Special case where we read directly into the
858 * cache buffer; important in the O_DIRECT case
861 reuse_cache(channel, data, cache, block);
862 if ((retval = raw_read_blk(channel, data, block, 1,
867 memcpy(cp, cache->buf, channel->block_size);
872 * Find the number of uncached blocks so we can do a
873 * single read request
875 for (i=1; i < count; i++)
876 if (find_cached_block(data, block+i, &reuse[i]))
879 printf("Reading %d blocks starting at %lu\n", i, block);
881 if ((retval = raw_read_blk(channel, data, block, i, cp)))
884 /* Save the results in the cache */
885 for (j=0; j < i; j++) {
888 reuse_cache(channel, data, cache, block++);
889 memcpy(cache->buf, cp, channel->block_size);
890 cp += channel->block_size;
894 #endif /* NO_IO_CACHE */
897 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
898 int count, void *buf)
900 return unix_read_blk64(channel, block, count, buf);
903 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
904 int count, const void *buf)
906 struct unix_private_data *data;
907 struct unix_cache *cache, *reuse;
908 errcode_t retval = 0;
912 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
913 data = (struct unix_private_data *) channel->private_data;
914 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
917 return raw_write_blk(channel, data, block, count, buf);
920 * If we're doing an odd-sized write or a very large write,
921 * flush out the cache completely and then do a direct write.
923 if (count < 0 || count > WRITE_DIRECT_SIZE) {
924 if ((retval = flush_cached_blocks(channel, data, 1)))
926 return raw_write_blk(channel, data, block, count, buf);
930 * For a moderate-sized multi-block write, first force a write
931 * if we're in write-through cache mode, and then fill the
932 * cache with the blocks.
934 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
936 retval = raw_write_blk(channel, data, block, count, buf);
940 cache = find_cached_block(data, block, &reuse);
943 reuse_cache(channel, data, cache, block);
945 if (cache->buf != cp)
946 memcpy(cache->buf, cp, channel->block_size);
947 cache->dirty = !writethrough;
950 cp += channel->block_size;
953 #endif /* NO_IO_CACHE */
956 static errcode_t unix_cache_readahead(io_channel channel,
957 unsigned long long block,
958 unsigned long long count)
960 #ifdef POSIX_FADV_WILLNEED
961 struct unix_private_data *data;
963 data = (struct unix_private_data *)channel->private_data;
964 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
965 return posix_fadvise(data->dev,
966 (ext2_loff_t)block * channel->block_size + data->offset,
967 (ext2_loff_t)count * channel->block_size,
968 POSIX_FADV_WILLNEED);
970 return EXT2_ET_OP_NOT_SUPPORTED;
974 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
975 int count, const void *buf)
977 return unix_write_blk64(channel, block, count, buf);
980 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
981 int size, const void *buf)
983 struct unix_private_data *data;
984 errcode_t retval = 0;
987 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
988 data = (struct unix_private_data *) channel->private_data;
989 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
991 if (channel->align != 0) {
993 printf("unix_write_byte: O_DIRECT fallback\n");
995 return EXT2_ET_UNIMPLEMENTED;
1000 * Flush out the cache completely
1002 if ((retval = flush_cached_blocks(channel, data, 1)))
1006 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
1009 actual = write(data->dev, buf, size);
1013 return EXT2_ET_SHORT_WRITE;
1019 * Flush data buffers to disk.
1021 static errcode_t unix_flush(io_channel channel)
1023 struct unix_private_data *data;
1024 errcode_t retval = 0;
1026 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1027 data = (struct unix_private_data *) channel->private_data;
1028 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1031 retval = flush_cached_blocks(channel, data, 0);
1037 static errcode_t unix_set_option(io_channel channel, const char *option,
1040 struct unix_private_data *data;
1041 unsigned long long tmp;
1044 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1045 data = (struct unix_private_data *) channel->private_data;
1046 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1048 if (!strcmp(option, "offset")) {
1050 return EXT2_ET_INVALID_ARGUMENT;
1052 tmp = strtoull(arg, &end, 0);
1054 return EXT2_ET_INVALID_ARGUMENT;
1056 if (data->offset < 0)
1057 return EXT2_ET_INVALID_ARGUMENT;
1060 return EXT2_ET_INVALID_ARGUMENT;
1063 #if defined(__linux__) && !defined(BLKDISCARD)
1064 #define BLKDISCARD _IO(0x12,119)
1067 static errcode_t unix_discard(io_channel channel, unsigned long long block,
1068 unsigned long long count)
1070 struct unix_private_data *data;
1073 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1074 data = (struct unix_private_data *) channel->private_data;
1075 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1077 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1081 range[0] = (__u64)(block) * channel->block_size + data->offset;
1082 range[1] = (__u64)(count) * channel->block_size;
1084 ret = ioctl(data->dev, BLKDISCARD, &range);
1089 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1091 * If we are not on block device, try to use punch hole
1092 * to reclaim free space.
1094 ret = fallocate(data->dev,
1095 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1096 (off_t)(block) * channel->block_size + data->offset,
1097 (off_t)(count) * channel->block_size);
1103 if (errno == EOPNOTSUPP)
1109 return EXT2_ET_UNIMPLEMENTED;
1112 /* parameters might not be used if OS doesn't support zeroout */
1113 #pragma GCC diagnostic push
1114 #pragma GCC diagnostic ignored "-Wunused-parameter"
1115 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1116 unsigned long long count)
1118 struct unix_private_data *data;
1121 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1122 data = (struct unix_private_data *) channel->private_data;
1123 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1125 if (safe_getenv("UNIX_IO_NOZEROOUT"))
1128 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1129 /* Not implemented until the BLKZEROOUT mess is fixed */
1132 /* Regular file, try to use truncate/punch/zero. */
1133 struct stat statbuf;
1138 * If we're trying to zero a range past the end of the file,
1139 * extend the file size, then truncate everything.
1141 ret = fstat(data->dev, &statbuf);
1144 if ((unsigned long long) statbuf.st_size <
1145 (block + count) * channel->block_size + data->offset) {
1146 ret = ftruncate(data->dev,
1147 (block + count) * channel->block_size + data->offset);
1151 #if defined(HAVE_FALLOCATE) && (defined(FALLOC_FL_ZERO_RANGE) || \
1152 (defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)))
1153 #if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1154 ret = fallocate(data->dev,
1155 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1156 (off_t)(block) * channel->block_size + data->offset,
1157 (off_t)(count) * channel->block_size);
1161 #ifdef FALLOC_FL_ZERO_RANGE
1162 ret = fallocate(data->dev,
1163 FALLOC_FL_ZERO_RANGE,
1164 (off_t)(block) * channel->block_size + data->offset,
1165 (off_t)(count) * channel->block_size);
1169 #endif /* HAVE_FALLOCATE && (ZERO_RANGE || (PUNCH_HOLE && KEEP_SIZE)) */
1173 if (errno == EOPNOTSUPP)
1179 return EXT2_ET_UNIMPLEMENTED;
1181 #pragma GCC diagnostic pop
1183 static struct struct_io_manager struct_unix_manager = {
1184 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1185 .name = "Unix I/O Manager",
1187 .close = unix_close,
1188 .set_blksize = unix_set_blksize,
1189 .read_blk = unix_read_blk,
1190 .write_blk = unix_write_blk,
1191 .flush = unix_flush,
1192 .write_byte = unix_write_byte,
1193 .set_option = unix_set_option,
1194 .get_stats = unix_get_stats,
1195 .read_blk64 = unix_read_blk64,
1196 .write_blk64 = unix_write_blk64,
1197 .discard = unix_discard,
1198 .cache_readahead = unix_cache_readahead,
1199 .zeroout = unix_zeroout,
1202 io_manager unix_io_manager = &struct_unix_manager;
1204 static struct struct_io_manager struct_unixfd_manager = {
1205 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1206 .name = "Unix fd I/O Manager",
1207 .open = unixfd_open,
1208 .close = unix_close,
1209 .set_blksize = unix_set_blksize,
1210 .read_blk = unix_read_blk,
1211 .write_blk = unix_write_blk,
1212 .flush = unix_flush,
1213 .write_byte = unix_write_byte,
1214 .set_option = unix_set_option,
1215 .get_stats = unix_get_stats,
1216 .read_blk64 = unix_read_blk64,
1217 .write_blk64 = unix_write_blk64,
1218 .discard = unix_discard,
1219 .cache_readahead = unix_cache_readahead,
1220 .zeroout = unix_zeroout,
1223 io_manager unixfd_io_manager = &struct_unixfd_manager;