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>
59 #if HAVE_SYS_RESOURCE_H
60 #include <sys/resource.h>
62 #if HAVE_LINUX_FALLOC_H
63 #include <linux/falloc.h>
66 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
67 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
76 * For checking structure magic numbers...
79 #define EXT2_CHECK_MAGIC(struct, code) \
80 if ((struct)->magic != (code)) return (code)
84 unsigned long long block;
91 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
92 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
94 struct unix_private_data {
101 struct unix_cache cache[CACHE_SIZE];
103 struct struct_io_stats io_stats;
106 #define IS_ALIGNED(n, align) ((((unsigned long) n) & \
107 ((unsigned long) ((align)-1))) == 0)
109 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
111 errcode_t retval = 0;
113 struct unix_private_data *data;
115 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
116 data = (struct unix_private_data *) channel->private_data;
117 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
120 *stats = &data->io_stats;
126 * Here are the raw I/O functions
128 static errcode_t raw_read_blk(io_channel channel,
129 struct unix_private_data *data,
130 unsigned long long block,
131 int count, void *bufv)
135 ext2_loff_t location;
137 unsigned char *buf = bufv;
139 size = (count < 0) ? -count : count * channel->block_size;
140 data->io_stats.bytes_read += size;
141 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
144 /* Try an aligned pread */
145 if ((channel->align == 0) ||
146 (IS_ALIGNED(buf, channel->align) &&
147 IS_ALIGNED(size, channel->align))) {
148 actual = pread64(data->dev, buf, size, location);
153 /* Try an aligned pread */
154 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
155 ((channel->align == 0) ||
156 (IS_ALIGNED(buf, channel->align) &&
157 IS_ALIGNED(size, channel->align)))) {
158 actual = pread(data->dev, buf, size, location);
162 #endif /* HAVE_PREAD */
164 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
165 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
168 if ((channel->align == 0) ||
169 (IS_ALIGNED(buf, channel->align) &&
170 IS_ALIGNED(size, channel->align))) {
171 actual = read(data->dev, buf, size);
172 if (actual != size) {
176 retval = EXT2_ET_SHORT_READ;
183 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
184 (unsigned long) size);
188 * The buffer or size which we're trying to read isn't aligned
189 * to the O_DIRECT rules, so we need to do this the hard way...
192 actual = read(data->dev, data->bounce, channel->block_size);
193 if (actual != channel->block_size)
196 if (size > channel->block_size)
197 actual = channel->block_size;
198 memcpy(buf, data->bounce, actual);
205 memset((char *) buf+actual, 0, size-actual);
206 if (channel->read_error)
207 retval = (channel->read_error)(channel, block, count, buf,
208 size, actual, retval);
212 static errcode_t raw_write_blk(io_channel channel,
213 struct unix_private_data *data,
214 unsigned long long block,
215 int count, const void *bufv)
218 ext2_loff_t location;
221 const unsigned char *buf = bufv;
224 size = channel->block_size;
229 size = count * channel->block_size;
231 data->io_stats.bytes_written += size;
233 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
236 /* Try an aligned pwrite */
237 if ((channel->align == 0) ||
238 (IS_ALIGNED(buf, channel->align) &&
239 IS_ALIGNED(size, channel->align))) {
240 actual = pwrite64(data->dev, buf, size, location);
245 /* Try an aligned pwrite */
246 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
247 ((channel->align == 0) ||
248 (IS_ALIGNED(buf, channel->align) &&
249 IS_ALIGNED(size, channel->align)))) {
250 actual = pwrite(data->dev, buf, size, location);
254 #endif /* HAVE_PWRITE */
256 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
257 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
261 if ((channel->align == 0) ||
262 (IS_ALIGNED(buf, channel->align) &&
263 IS_ALIGNED(size, channel->align))) {
264 actual = write(data->dev, buf, size);
265 if (actual != size) {
267 retval = EXT2_ET_SHORT_WRITE;
274 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
275 (unsigned long) size);
278 * The buffer or size which we're trying to write isn't aligned
279 * to the O_DIRECT rules, so we need to do this the hard way...
282 if (size < channel->block_size) {
283 actual = read(data->dev, data->bounce,
284 channel->block_size);
285 if (actual != channel->block_size) {
286 retval = EXT2_ET_SHORT_READ;
291 if (size > channel->block_size)
292 actual = channel->block_size;
293 memcpy(data->bounce, buf, actual);
294 actual = write(data->dev, data->bounce, channel->block_size);
295 if (actual != channel->block_size)
303 if (channel->write_error)
304 retval = (channel->write_error)(channel, block, count, buf,
305 size, actual, retval);
311 * Here we implement the cache functions
314 /* Allocate the cache buffers */
315 static errcode_t alloc_cache(io_channel channel,
316 struct unix_private_data *data)
319 struct unix_cache *cache;
322 data->access_time = 0;
323 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
325 cache->access_time = 0;
329 ext2fs_free_mem(&cache->buf);
330 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
334 if (channel->align) {
336 ext2fs_free_mem(&data->bounce);
337 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
342 /* Free the cache buffers */
343 static void free_cache(struct unix_private_data *data)
345 struct unix_cache *cache;
348 data->access_time = 0;
349 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
351 cache->access_time = 0;
355 ext2fs_free_mem(&cache->buf);
358 ext2fs_free_mem(&data->bounce);
363 * Try to find a block in the cache. If the block is not found, and
364 * eldest is a non-zero pointer, then fill in eldest with the cache
365 * entry to that should be reused.
367 static struct unix_cache *find_cached_block(struct unix_private_data *data,
368 unsigned long long block,
369 struct unix_cache **eldest)
371 struct unix_cache *cache, *unused_cache, *oldest_cache;
374 unused_cache = oldest_cache = 0;
375 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
376 if (!cache->in_use) {
378 unused_cache = cache;
381 if (cache->block == block) {
382 cache->access_time = ++data->access_time;
386 (cache->access_time < oldest_cache->access_time))
387 oldest_cache = cache;
390 *eldest = (unused_cache) ? unused_cache : oldest_cache;
395 * Reuse a particular cache entry for another block.
397 static void reuse_cache(io_channel channel, struct unix_private_data *data,
398 struct unix_cache *cache, unsigned long long block)
400 if (cache->dirty && cache->in_use)
401 raw_write_blk(channel, data, cache->block, 1, cache->buf);
405 cache->block = block;
406 cache->access_time = ++data->access_time;
410 * Flush all of the blocks in the cache
412 static errcode_t flush_cached_blocks(io_channel channel,
413 struct unix_private_data *data,
417 struct unix_cache *cache;
418 errcode_t retval, retval2;
422 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
432 retval = raw_write_blk(channel, data,
433 cache->block, 1, cache->buf);
441 #endif /* NO_IO_CACHE */
444 #ifndef BLKDISCARDZEROES
445 #define BLKDISCARDZEROES _IO(0x12,124)
449 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
452 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
453 return open64(pathname, flags, mode);
455 return open64(pathname, flags);
457 return open(pathname, flags, mode);
459 return open(pathname, flags);
463 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
465 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
466 return stat64(path, buf);
468 return stat(path, buf);
472 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
474 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
475 return fstat64(fd, buf);
477 return fstat(fd, buf);
481 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
483 io_channel io = NULL;
484 struct unix_private_data *data = NULL;
488 ext2fs_struct_stat st;
494 return EXT2_ET_BAD_DEVICE_NAME;
495 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
498 memset(io, 0, sizeof(struct struct_io_channel));
499 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
500 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
504 io->manager = unix_io_manager;
505 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
509 strcpy(io->name, name);
510 io->private_data = data;
511 io->block_size = 1024;
516 memset(data, 0, sizeof(struct unix_private_data));
517 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
518 data->io_stats.num_fields = 2;
521 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
522 if (flags & IO_FLAG_EXCLUSIVE)
523 open_flags |= O_EXCL;
524 #if defined(O_DIRECT)
525 if (flags & IO_FLAG_DIRECT_IO) {
526 open_flags |= O_DIRECT;
527 io->align = ext2fs_get_dio_alignment(data->dev);
529 #elif defined(F_NOCACHE)
530 if (flags & IO_FLAG_DIRECT_IO) {
531 f_nocache = F_NOCACHE;
537 data->dev = ext2fs_open_file(io->name, open_flags, 0);
543 if (fcntl(data->dev, f_nocache, 1) < 0) {
550 * If the device is really a block device, then set the
551 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
552 * because we are going to use punch hole instead of discard
553 * and if it succeed, subsequent read from sparse area returns
556 if (ext2fs_stat(io->name, &st) == 0) {
557 if (S_ISBLK(st.st_mode))
558 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
560 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
563 #ifdef BLKDISCARDZEROES
566 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
568 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
572 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
574 * Some operating systems require that the buffers be aligned,
575 * regardless of O_DIRECT
582 if ((retval = alloc_cache(io, data)))
586 if (flags & IO_FLAG_RW) {
590 /* Is the block device actually writable? */
591 error = ioctl(data->dev, BLKROGET, &readonly);
592 if (!error && readonly) {
601 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
602 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
604 #define RLIM_INFINITY (~0UL)
607 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
608 * block devices are wrongly getting hit by the filesize
609 * limit. This workaround isn't perfect, since it won't work
610 * if glibc wasn't built against 2.2 header files. (Sigh.)
613 if ((flags & IO_FLAG_RW) &&
615 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
616 (ut.release[2] == '4') && (ut.release[3] == '.') &&
617 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
618 (ut.release[5] < '8')) &&
619 (ext2fs_stat(io->name, &st) == 0) &&
620 (S_ISBLK(st.st_mode))) {
623 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
624 setrlimit(RLIMIT_FSIZE, &rlim);
625 getrlimit(RLIMIT_FSIZE, &rlim);
626 if (((unsigned long) rlim.rlim_cur) <
627 ((unsigned long) rlim.rlim_max)) {
628 rlim.rlim_cur = rlim.rlim_max;
629 setrlimit(RLIMIT_FSIZE, &rlim);
641 ext2fs_free_mem(&data);
645 ext2fs_free_mem(&io->name);
647 ext2fs_free_mem(&io);
652 static errcode_t unix_close(io_channel channel)
654 struct unix_private_data *data;
655 errcode_t retval = 0;
657 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
658 data = (struct unix_private_data *) channel->private_data;
659 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
661 if (--channel->refcount > 0)
665 retval = flush_cached_blocks(channel, data, 0);
668 if (close(data->dev) < 0)
672 ext2fs_free_mem(&channel->private_data);
674 ext2fs_free_mem(&channel->name);
675 ext2fs_free_mem(&channel);
679 static errcode_t unix_set_blksize(io_channel channel, int blksize)
681 struct unix_private_data *data;
684 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
685 data = (struct unix_private_data *) channel->private_data;
686 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
688 if (channel->block_size != blksize) {
690 if ((retval = flush_cached_blocks(channel, data, 0)))
694 channel->block_size = blksize;
696 if ((retval = alloc_cache(channel, data)))
703 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
704 int count, void *buf)
706 struct unix_private_data *data;
707 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
712 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
713 data = (struct unix_private_data *) channel->private_data;
714 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
717 return raw_read_blk(channel, data, block, count, buf);
720 * If we're doing an odd-sized read or a very large read,
721 * flush out the cache and then do a direct read.
723 if (count < 0 || count > WRITE_DIRECT_SIZE) {
724 if ((retval = flush_cached_blocks(channel, data, 0)))
726 return raw_read_blk(channel, data, block, count, buf);
731 /* If it's in the cache, use it! */
732 if ((cache = find_cached_block(data, block, &reuse[0]))) {
734 printf("Using cached block %lu\n", block);
736 memcpy(cp, cache->buf, channel->block_size);
739 cp += channel->block_size;
744 * Special case where we read directly into the
745 * cache buffer; important in the O_DIRECT case
748 reuse_cache(channel, data, cache, block);
749 if ((retval = raw_read_blk(channel, data, block, 1,
754 memcpy(cp, cache->buf, channel->block_size);
759 * Find the number of uncached blocks so we can do a
760 * single read request
762 for (i=1; i < count; i++)
763 if (find_cached_block(data, block+i, &reuse[i]))
766 printf("Reading %d blocks starting at %lu\n", i, block);
768 if ((retval = raw_read_blk(channel, data, block, i, cp)))
771 /* Save the results in the cache */
772 for (j=0; j < i; j++) {
775 reuse_cache(channel, data, cache, block++);
776 memcpy(cache->buf, cp, channel->block_size);
777 cp += channel->block_size;
781 #endif /* NO_IO_CACHE */
784 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
785 int count, void *buf)
787 return unix_read_blk64(channel, block, count, buf);
790 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
791 int count, const void *buf)
793 struct unix_private_data *data;
794 struct unix_cache *cache, *reuse;
795 errcode_t retval = 0;
799 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
800 data = (struct unix_private_data *) channel->private_data;
801 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
804 return raw_write_blk(channel, data, block, count, buf);
807 * If we're doing an odd-sized write or a very large write,
808 * flush out the cache completely and then do a direct write.
810 if (count < 0 || count > WRITE_DIRECT_SIZE) {
811 if ((retval = flush_cached_blocks(channel, data, 1)))
813 return raw_write_blk(channel, data, block, count, buf);
817 * For a moderate-sized multi-block write, first force a write
818 * if we're in write-through cache mode, and then fill the
819 * cache with the blocks.
821 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
823 retval = raw_write_blk(channel, data, block, count, buf);
827 cache = find_cached_block(data, block, &reuse);
830 reuse_cache(channel, data, cache, block);
832 if (cache->buf != cp)
833 memcpy(cache->buf, cp, channel->block_size);
834 cache->dirty = !writethrough;
837 cp += channel->block_size;
840 #endif /* NO_IO_CACHE */
843 static errcode_t unix_cache_readahead(io_channel channel,
844 unsigned long long block,
845 unsigned long long count)
847 #ifdef POSIX_FADV_WILLNEED
848 struct unix_private_data *data;
850 data = (struct unix_private_data *)channel->private_data;
851 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
852 return posix_fadvise(data->dev,
853 (ext2_loff_t)block * channel->block_size + data->offset,
854 (ext2_loff_t)count * channel->block_size,
855 POSIX_FADV_WILLNEED);
857 return EXT2_ET_OP_NOT_SUPPORTED;
861 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
862 int count, const void *buf)
864 return unix_write_blk64(channel, block, count, buf);
867 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
868 int size, const void *buf)
870 struct unix_private_data *data;
871 errcode_t retval = 0;
874 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
875 data = (struct unix_private_data *) channel->private_data;
876 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
878 if (channel->align != 0) {
880 printf("unix_write_byte: O_DIRECT fallback\n");
882 return EXT2_ET_UNIMPLEMENTED;
887 * Flush out the cache completely
889 if ((retval = flush_cached_blocks(channel, data, 1)))
893 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
896 actual = write(data->dev, buf, size);
898 return EXT2_ET_SHORT_WRITE;
904 * Flush data buffers to disk.
906 static errcode_t unix_flush(io_channel channel)
908 struct unix_private_data *data;
909 errcode_t retval = 0;
911 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
912 data = (struct unix_private_data *) channel->private_data;
913 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
916 retval = flush_cached_blocks(channel, data, 0);
922 static errcode_t unix_set_option(io_channel channel, const char *option,
925 struct unix_private_data *data;
926 unsigned long long tmp;
929 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
930 data = (struct unix_private_data *) channel->private_data;
931 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
933 if (!strcmp(option, "offset")) {
935 return EXT2_ET_INVALID_ARGUMENT;
937 tmp = strtoull(arg, &end, 0);
939 return EXT2_ET_INVALID_ARGUMENT;
941 if (data->offset < 0)
942 return EXT2_ET_INVALID_ARGUMENT;
945 return EXT2_ET_INVALID_ARGUMENT;
948 #if defined(__linux__) && !defined(BLKDISCARD)
949 #define BLKDISCARD _IO(0x12,119)
952 static errcode_t unix_discard(io_channel channel, unsigned long long block,
953 unsigned long long count)
955 struct unix_private_data *data;
958 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
959 data = (struct unix_private_data *) channel->private_data;
960 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
962 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
966 range[0] = (__u64)(block) * channel->block_size + data->offset;
967 range[1] = (__u64)(count) * channel->block_size;
969 ret = ioctl(data->dev, BLKDISCARD, &range);
974 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
976 * If we are not on block device, try to use punch hole
977 * to reclaim free space.
979 ret = fallocate(data->dev,
980 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
981 (off_t)(block) * channel->block_size + data->offset,
982 (off_t)(count) * channel->block_size);
988 if (errno == EOPNOTSUPP)
994 return EXT2_ET_UNIMPLEMENTED;
997 /* parameters might not be used if OS doesn't support zeroout */
998 #pragma GCC diagnostic push
999 #pragma GCC diagnostic ignored "-Wunused-parameter"
1000 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1001 unsigned long long count)
1003 struct unix_private_data *data;
1006 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1007 data = (struct unix_private_data *) channel->private_data;
1008 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1010 if (getenv("UNIX_IO_NOZEROOUT"))
1013 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1014 /* Not implemented until the BLKZEROOUT mess is fixed */
1017 /* Regular file, try to use truncate/punch/zero. */
1018 struct stat statbuf;
1023 * If we're trying to zero a range past the end of the file,
1024 * extend the file size, then truncate everything.
1026 ret = fstat(data->dev, &statbuf);
1029 if ((unsigned long long) statbuf.st_size <
1030 (block + count) * channel->block_size + data->offset) {
1031 ret = ftruncate(data->dev,
1032 (block + count) * channel->block_size + data->offset);
1036 #if defined(HAVE_FALLOCATE) && (defined(FALLOC_FL_ZERO_RANGE) || \
1037 (defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)))
1038 #if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1039 ret = fallocate(data->dev,
1040 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1041 (off_t)(block) * channel->block_size + data->offset,
1042 (off_t)(count) * channel->block_size);
1046 #ifdef FALLOC_FL_ZERO_RANGE
1047 ret = fallocate(data->dev,
1048 FALLOC_FL_ZERO_RANGE,
1049 (off_t)(block) * channel->block_size + data->offset,
1050 (off_t)(count) * channel->block_size);
1054 #endif /* HAVE_FALLOCATE && (ZERO_RANGE || (PUNCH_HOLE && KEEP_SIZE)) */
1058 if (errno == EOPNOTSUPP)
1064 return EXT2_ET_UNIMPLEMENTED;
1066 #pragma GCC diagnostic pop
1068 static struct struct_io_manager struct_unix_manager = {
1069 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1070 .name = "Unix I/O Manager",
1072 .close = unix_close,
1073 .set_blksize = unix_set_blksize,
1074 .read_blk = unix_read_blk,
1075 .write_blk = unix_write_blk,
1076 .flush = unix_flush,
1077 .write_byte = unix_write_byte,
1078 .set_option = unix_set_option,
1079 .get_stats = unix_get_stats,
1080 .read_blk64 = unix_read_blk64,
1081 .write_blk64 = unix_write_blk64,
1082 .discard = unix_discard,
1083 .cache_readahead = unix_cache_readahead,
1084 .zeroout = unix_zeroout,
1087 io_manager unix_io_manager = &struct_unix_manager;