2 * unix_io.c --- This is the Unix (well, really POSIX) implementation
5 * Implements a one-block write-through cache.
7 * Includes support for Windows NT support under Cygwin.
9 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
10 * 2002 by Theodore Ts'o.
13 * This file may be redistributed under the terms of the GNU Library
14 * General Public License, version 2.
18 #define _XOPEN_SOURCE 600
19 #define _DARWIN_C_SOURCE
20 #define _FILE_OFFSET_BITS 64
21 #ifndef _LARGEFILE_SOURCE
22 #define _LARGEFILE_SOURCE
24 #ifndef _LARGEFILE64_SOURCE
25 #define _LARGEFILE64_SOURCE
43 #include <sys/utsname.h>
46 #include <sys/types.h>
48 #ifdef HAVE_SYS_IOCTL_H
49 #include <sys/ioctl.h>
51 #ifdef HAVE_SYS_MOUNT_H
52 #include <sys/mount.h>
57 #if HAVE_SYS_RESOURCE_H
58 #include <sys/resource.h>
60 #if HAVE_LINUX_FALLOC_H
61 #include <linux/falloc.h>
64 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
65 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
74 * For checking structure magic numbers...
77 #define EXT2_CHECK_MAGIC(struct, code) \
78 if ((struct)->magic != (code)) return (code)
82 unsigned long long block;
89 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
90 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
92 struct unix_private_data {
99 struct unix_cache cache[CACHE_SIZE];
101 struct struct_io_stats io_stats;
104 #define IS_ALIGNED(n, align) ((((unsigned long) n) & \
105 ((unsigned long) ((align)-1))) == 0)
107 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
109 errcode_t retval = 0;
111 struct unix_private_data *data;
113 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
114 data = (struct unix_private_data *) channel->private_data;
115 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
118 *stats = &data->io_stats;
124 * Here are the raw I/O functions
126 static errcode_t raw_read_blk(io_channel channel,
127 struct unix_private_data *data,
128 unsigned long long block,
129 int count, void *bufv)
133 ext2_loff_t location;
135 unsigned char *buf = bufv;
137 size = (count < 0) ? -count : count * channel->block_size;
138 data->io_stats.bytes_read += size;
139 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
142 /* Try an aligned pread */
143 if ((channel->align == 0) ||
144 (IS_ALIGNED(buf, channel->align) &&
145 IS_ALIGNED(size, channel->align))) {
146 actual = pread64(data->dev, buf, size, location);
151 /* Try an aligned pread */
152 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
153 ((channel->align == 0) ||
154 (IS_ALIGNED(buf, channel->align) &&
155 IS_ALIGNED(size, channel->align)))) {
156 actual = pread(data->dev, buf, size, location);
160 #endif /* HAVE_PREAD */
162 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
163 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
166 if ((channel->align == 0) ||
167 (IS_ALIGNED(buf, channel->align) &&
168 IS_ALIGNED(size, channel->align))) {
169 actual = read(data->dev, buf, size);
170 if (actual != size) {
174 retval = EXT2_ET_SHORT_READ;
181 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
182 (unsigned long) size);
186 * The buffer or size which we're trying to read isn't aligned
187 * to the O_DIRECT rules, so we need to do this the hard way...
190 actual = read(data->dev, data->bounce, channel->block_size);
191 if (actual != channel->block_size)
194 if (size > channel->block_size)
195 actual = channel->block_size;
196 memcpy(buf, data->bounce, actual);
203 memset((char *) buf+actual, 0, size-actual);
204 if (channel->read_error)
205 retval = (channel->read_error)(channel, block, count, buf,
206 size, actual, retval);
210 static errcode_t raw_write_blk(io_channel channel,
211 struct unix_private_data *data,
212 unsigned long long block,
213 int count, const void *bufv)
216 ext2_loff_t location;
219 const unsigned char *buf = bufv;
222 size = channel->block_size;
227 size = count * channel->block_size;
229 data->io_stats.bytes_written += size;
231 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
234 /* Try an aligned pwrite */
235 if ((channel->align == 0) ||
236 (IS_ALIGNED(buf, channel->align) &&
237 IS_ALIGNED(size, channel->align))) {
238 actual = pwrite64(data->dev, buf, size, location);
243 /* Try an aligned pwrite */
244 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
245 ((channel->align == 0) ||
246 (IS_ALIGNED(buf, channel->align) &&
247 IS_ALIGNED(size, channel->align)))) {
248 actual = pwrite(data->dev, buf, size, location);
252 #endif /* HAVE_PWRITE */
254 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
255 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
259 if ((channel->align == 0) ||
260 (IS_ALIGNED(buf, channel->align) &&
261 IS_ALIGNED(size, channel->align))) {
262 actual = write(data->dev, buf, size);
263 if (actual != size) {
265 retval = EXT2_ET_SHORT_WRITE;
272 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
273 (unsigned long) size);
276 * The buffer or size which we're trying to write isn't aligned
277 * to the O_DIRECT rules, so we need to do this the hard way...
280 if (size < channel->block_size) {
281 actual = read(data->dev, data->bounce,
282 channel->block_size);
283 if (actual != channel->block_size) {
284 retval = EXT2_ET_SHORT_READ;
289 if (size > channel->block_size)
290 actual = channel->block_size;
291 memcpy(data->bounce, buf, actual);
292 actual = write(data->dev, data->bounce, channel->block_size);
293 if (actual != channel->block_size)
301 if (channel->write_error)
302 retval = (channel->write_error)(channel, block, count, buf,
303 size, actual, retval);
309 * Here we implement the cache functions
312 /* Allocate the cache buffers */
313 static errcode_t alloc_cache(io_channel channel,
314 struct unix_private_data *data)
317 struct unix_cache *cache;
320 data->access_time = 0;
321 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
323 cache->access_time = 0;
327 ext2fs_free_mem(&cache->buf);
328 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
332 if (channel->align) {
334 ext2fs_free_mem(&data->bounce);
335 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
340 /* Free the cache buffers */
341 static void free_cache(struct unix_private_data *data)
343 struct unix_cache *cache;
346 data->access_time = 0;
347 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
349 cache->access_time = 0;
353 ext2fs_free_mem(&cache->buf);
356 ext2fs_free_mem(&data->bounce);
361 * Try to find a block in the cache. If the block is not found, and
362 * eldest is a non-zero pointer, then fill in eldest with the cache
363 * entry to that should be reused.
365 static struct unix_cache *find_cached_block(struct unix_private_data *data,
366 unsigned long long block,
367 struct unix_cache **eldest)
369 struct unix_cache *cache, *unused_cache, *oldest_cache;
372 unused_cache = oldest_cache = 0;
373 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
374 if (!cache->in_use) {
376 unused_cache = cache;
379 if (cache->block == block) {
380 cache->access_time = ++data->access_time;
384 (cache->access_time < oldest_cache->access_time))
385 oldest_cache = cache;
388 *eldest = (unused_cache) ? unused_cache : oldest_cache;
393 * Reuse a particular cache entry for another block.
395 static void reuse_cache(io_channel channel, struct unix_private_data *data,
396 struct unix_cache *cache, unsigned long long block)
398 if (cache->dirty && cache->in_use)
399 raw_write_blk(channel, data, cache->block, 1, cache->buf);
403 cache->block = block;
404 cache->access_time = ++data->access_time;
408 * Flush all of the blocks in the cache
410 static errcode_t flush_cached_blocks(io_channel channel,
411 struct unix_private_data *data,
415 struct unix_cache *cache;
416 errcode_t retval, retval2;
420 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
430 retval = raw_write_blk(channel, data,
431 cache->block, 1, cache->buf);
439 #endif /* NO_IO_CACHE */
442 #ifndef BLKDISCARDZEROES
443 #define BLKDISCARDZEROES _IO(0x12,124)
447 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
450 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
451 return open64(pathname, flags, mode);
453 return open64(pathname, flags);
455 return open(pathname, flags, mode);
457 return open(pathname, flags);
461 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
463 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
464 return stat64(path, buf);
466 return stat(path, buf);
470 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
472 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
473 return fstat64(fd, buf);
475 return fstat(fd, buf);
479 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
481 io_channel io = NULL;
482 struct unix_private_data *data = NULL;
486 ext2fs_struct_stat st;
492 return EXT2_ET_BAD_DEVICE_NAME;
493 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
496 memset(io, 0, sizeof(struct struct_io_channel));
497 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
498 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
502 io->manager = unix_io_manager;
503 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
507 strcpy(io->name, name);
508 io->private_data = data;
509 io->block_size = 1024;
514 memset(data, 0, sizeof(struct unix_private_data));
515 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
516 data->io_stats.num_fields = 2;
519 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
520 if (flags & IO_FLAG_EXCLUSIVE)
521 open_flags |= O_EXCL;
522 #if defined(O_DIRECT)
523 if (flags & IO_FLAG_DIRECT_IO) {
524 open_flags |= O_DIRECT;
525 io->align = ext2fs_get_dio_alignment(data->dev);
527 #elif defined(F_NOCACHE)
528 if (flags & IO_FLAG_DIRECT_IO) {
529 f_nocache = F_NOCACHE;
535 data->dev = ext2fs_open_file(io->name, open_flags, 0);
541 if (fcntl(data->dev, f_nocache, 1) < 0) {
548 * If the device is really a block device, then set the
549 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
550 * because we are going to use punch hole instead of discard
551 * and if it succeed, subsequent read from sparse area returns
554 if (ext2fs_stat(io->name, &st) == 0) {
555 if (S_ISBLK(st.st_mode))
556 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
558 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
561 #ifdef BLKDISCARDZEROES
564 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
566 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
570 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
572 * Some operating systems require that the buffers be aligned,
573 * regardless of O_DIRECT
580 if ((retval = alloc_cache(io, data)))
584 if (flags & IO_FLAG_RW) {
588 /* Is the block device actually writable? */
589 error = ioctl(data->dev, BLKROGET, &readonly);
590 if (!error && readonly) {
599 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
600 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
602 #define RLIM_INFINITY (~0UL)
605 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
606 * block devices are wrongly getting hit by the filesize
607 * limit. This workaround isn't perfect, since it won't work
608 * if glibc wasn't built against 2.2 header files. (Sigh.)
611 if ((flags & IO_FLAG_RW) &&
613 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
614 (ut.release[2] == '4') && (ut.release[3] == '.') &&
615 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
616 (ut.release[5] < '8')) &&
617 (ext2fs_stat(io->name, &st) == 0) &&
618 (S_ISBLK(st.st_mode))) {
621 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
622 setrlimit(RLIMIT_FSIZE, &rlim);
623 getrlimit(RLIMIT_FSIZE, &rlim);
624 if (((unsigned long) rlim.rlim_cur) <
625 ((unsigned long) rlim.rlim_max)) {
626 rlim.rlim_cur = rlim.rlim_max;
627 setrlimit(RLIMIT_FSIZE, &rlim);
639 ext2fs_free_mem(&data);
643 ext2fs_free_mem(&io->name);
645 ext2fs_free_mem(&io);
650 static errcode_t unix_close(io_channel channel)
652 struct unix_private_data *data;
653 errcode_t retval = 0;
655 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
656 data = (struct unix_private_data *) channel->private_data;
657 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
659 if (--channel->refcount > 0)
663 retval = flush_cached_blocks(channel, data, 0);
666 if (close(data->dev) < 0)
670 ext2fs_free_mem(&channel->private_data);
672 ext2fs_free_mem(&channel->name);
673 ext2fs_free_mem(&channel);
677 static errcode_t unix_set_blksize(io_channel channel, int blksize)
679 struct unix_private_data *data;
682 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
683 data = (struct unix_private_data *) channel->private_data;
684 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
686 if (channel->block_size != blksize) {
688 if ((retval = flush_cached_blocks(channel, data, 0)))
692 channel->block_size = blksize;
694 if ((retval = alloc_cache(channel, data)))
701 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
702 int count, void *buf)
704 struct unix_private_data *data;
705 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
710 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
711 data = (struct unix_private_data *) channel->private_data;
712 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
715 return raw_read_blk(channel, data, block, count, buf);
718 * If we're doing an odd-sized read or a very large read,
719 * flush out the cache and then do a direct read.
721 if (count < 0 || count > WRITE_DIRECT_SIZE) {
722 if ((retval = flush_cached_blocks(channel, data, 0)))
724 return raw_read_blk(channel, data, block, count, buf);
729 /* If it's in the cache, use it! */
730 if ((cache = find_cached_block(data, block, &reuse[0]))) {
732 printf("Using cached block %lu\n", block);
734 memcpy(cp, cache->buf, channel->block_size);
737 cp += channel->block_size;
742 * Special case where we read directly into the
743 * cache buffer; important in the O_DIRECT case
746 reuse_cache(channel, data, cache, block);
747 if ((retval = raw_read_blk(channel, data, block, 1,
752 memcpy(cp, cache->buf, channel->block_size);
757 * Find the number of uncached blocks so we can do a
758 * single read request
760 for (i=1; i < count; i++)
761 if (find_cached_block(data, block+i, &reuse[i]))
764 printf("Reading %d blocks starting at %lu\n", i, block);
766 if ((retval = raw_read_blk(channel, data, block, i, cp)))
769 /* Save the results in the cache */
770 for (j=0; j < i; j++) {
773 reuse_cache(channel, data, cache, block++);
774 memcpy(cache->buf, cp, channel->block_size);
775 cp += channel->block_size;
779 #endif /* NO_IO_CACHE */
782 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
783 int count, void *buf)
785 return unix_read_blk64(channel, block, count, buf);
788 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
789 int count, const void *buf)
791 struct unix_private_data *data;
792 struct unix_cache *cache, *reuse;
793 errcode_t retval = 0;
797 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
798 data = (struct unix_private_data *) channel->private_data;
799 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
802 return raw_write_blk(channel, data, block, count, buf);
805 * If we're doing an odd-sized write or a very large write,
806 * flush out the cache completely and then do a direct write.
808 if (count < 0 || count > WRITE_DIRECT_SIZE) {
809 if ((retval = flush_cached_blocks(channel, data, 1)))
811 return raw_write_blk(channel, data, block, count, buf);
815 * For a moderate-sized multi-block write, first force a write
816 * if we're in write-through cache mode, and then fill the
817 * cache with the blocks.
819 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
821 retval = raw_write_blk(channel, data, block, count, buf);
825 cache = find_cached_block(data, block, &reuse);
828 reuse_cache(channel, data, cache, block);
830 if (cache->buf != cp)
831 memcpy(cache->buf, cp, channel->block_size);
832 cache->dirty = !writethrough;
835 cp += channel->block_size;
838 #endif /* NO_IO_CACHE */
841 static errcode_t unix_cache_readahead(io_channel channel,
842 unsigned long long block,
843 unsigned long long count)
845 #ifdef POSIX_FADV_WILLNEED
846 struct unix_private_data *data;
848 data = (struct unix_private_data *)channel->private_data;
849 return posix_fadvise(data->dev,
850 (ext2_loff_t)block * channel->block_size,
851 (ext2_loff_t)count * channel->block_size,
852 POSIX_FADV_WILLNEED);
854 return EXT2_ET_OP_NOT_SUPPORTED;
858 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
859 int count, const void *buf)
861 return unix_write_blk64(channel, block, count, buf);
864 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
865 int size, const void *buf)
867 struct unix_private_data *data;
868 errcode_t retval = 0;
871 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
872 data = (struct unix_private_data *) channel->private_data;
873 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
875 if (channel->align != 0) {
877 printf("unix_write_byte: O_DIRECT fallback\n");
879 return EXT2_ET_UNIMPLEMENTED;
884 * Flush out the cache completely
886 if ((retval = flush_cached_blocks(channel, data, 1)))
890 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
893 actual = write(data->dev, buf, size);
895 return EXT2_ET_SHORT_WRITE;
901 * Flush data buffers to disk.
903 static errcode_t unix_flush(io_channel channel)
905 struct unix_private_data *data;
906 errcode_t retval = 0;
908 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
909 data = (struct unix_private_data *) channel->private_data;
910 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
913 retval = flush_cached_blocks(channel, data, 0);
919 static errcode_t unix_set_option(io_channel channel, const char *option,
922 struct unix_private_data *data;
923 unsigned long long tmp;
926 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
927 data = (struct unix_private_data *) channel->private_data;
928 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
930 if (!strcmp(option, "offset")) {
932 return EXT2_ET_INVALID_ARGUMENT;
934 tmp = strtoull(arg, &end, 0);
936 return EXT2_ET_INVALID_ARGUMENT;
938 if (data->offset < 0)
939 return EXT2_ET_INVALID_ARGUMENT;
942 return EXT2_ET_INVALID_ARGUMENT;
945 #if defined(__linux__) && !defined(BLKDISCARD)
946 #define BLKDISCARD _IO(0x12,119)
949 static errcode_t unix_discard(io_channel channel, unsigned long long block,
950 unsigned long long count)
952 struct unix_private_data *data;
955 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
956 data = (struct unix_private_data *) channel->private_data;
957 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
959 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
963 range[0] = (__u64)(block) * channel->block_size;
964 range[1] = (__u64)(count) * channel->block_size;
966 ret = ioctl(data->dev, BLKDISCARD, &range);
971 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
973 * If we are not on block device, try to use punch hole
974 * to reclaim free space.
976 ret = fallocate(data->dev,
977 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
978 (off_t)(block) * channel->block_size,
979 (off_t)(count) * channel->block_size);
985 if (errno == EOPNOTSUPP)
991 return EXT2_ET_UNIMPLEMENTED;
994 /* parameters might not be used if OS doesn't support zeroout */
995 #pragma GCC diagnostic push
996 #pragma GCC diagnostic ignored "-Wunused-parameter"
997 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
998 unsigned long long count)
1000 struct unix_private_data *data;
1003 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1004 data = (struct unix_private_data *) channel->private_data;
1005 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1007 if (getenv("UNIX_IO_NOZEROOUT"))
1010 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1011 /* Not implemented until the BLKZEROOUT mess is fixed */
1014 /* Regular file, try to use truncate/punch/zero. */
1015 struct stat statbuf;
1020 * If we're trying to zero a range past the end of the file,
1021 * extend the file size, then truncate everything.
1023 ret = fstat(data->dev, &statbuf);
1026 if ((unsigned long long) statbuf.st_size < (block + count) * channel->block_size) {
1027 ret = ftruncate(data->dev,
1028 (block + count) * channel->block_size);
1032 #if defined(HAVE_FALLOCATE) && (defined(FALLOC_FL_ZERO_RANGE) || \
1033 (defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)))
1034 #if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1035 ret = fallocate(data->dev,
1036 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1037 (off_t)(block) * channel->block_size,
1038 (off_t)(count) * channel->block_size);
1042 #ifdef FALLOC_FL_ZERO_RANGE
1043 ret = fallocate(data->dev,
1044 FALLOC_FL_ZERO_RANGE,
1045 (off_t)(block) * channel->block_size,
1046 (off_t)(count) * channel->block_size);
1050 #endif /* HAVE_FALLOCATE && (ZERO_RANGE || (PUNCH_HOLE && KEEP_SIZE)) */
1054 if (errno == EOPNOTSUPP)
1060 return EXT2_ET_UNIMPLEMENTED;
1062 #pragma GCC diagnostic pop
1064 static struct struct_io_manager struct_unix_manager = {
1065 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1066 .name = "Unix I/O Manager",
1068 .close = unix_close,
1069 .set_blksize = unix_set_blksize,
1070 .read_blk = unix_read_blk,
1071 .write_blk = unix_write_blk,
1072 .flush = unix_flush,
1073 .write_byte = unix_write_byte,
1074 .set_option = unix_set_option,
1075 .get_stats = unix_get_stats,
1076 .read_blk64 = unix_read_blk64,
1077 .write_blk64 = unix_write_blk64,
1078 .discard = unix_discard,
1079 .cache_readahead = unix_cache_readahead,
1080 .zeroout = unix_zeroout,
1083 io_manager unix_io_manager = &struct_unix_manager;