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 #define _LARGEFILE_SOURCE
22 #define _LARGEFILE64_SOURCE
39 #include <sys/utsname.h>
42 #include <sys/types.h>
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
47 #ifdef HAVE_SYS_MOUNT_H
48 #include <sys/mount.h>
53 #if HAVE_SYS_RESOURCE_H
54 #include <sys/resource.h>
56 #if HAVE_LINUX_FALLOC_H
57 #include <linux/falloc.h>
60 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
61 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
70 * For checking structure magic numbers...
73 #define EXT2_CHECK_MAGIC(struct, code) \
74 if ((struct)->magic != (code)) return (code)
78 unsigned long long block;
85 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
86 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
88 struct unix_private_data {
95 struct unix_cache cache[CACHE_SIZE];
97 struct struct_io_stats io_stats;
100 #define IS_ALIGNED(n, align) ((((unsigned long) n) & \
101 ((unsigned long) ((align)-1))) == 0)
103 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
105 errcode_t retval = 0;
107 struct unix_private_data *data;
109 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
110 data = (struct unix_private_data *) channel->private_data;
111 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
114 *stats = &data->io_stats;
120 * Here are the raw I/O functions
122 static errcode_t raw_read_blk(io_channel channel,
123 struct unix_private_data *data,
124 unsigned long long block,
125 int count, void *bufv)
129 ext2_loff_t location;
131 unsigned char *buf = bufv;
133 size = (count < 0) ? -count : count * channel->block_size;
134 data->io_stats.bytes_read += size;
135 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
138 /* Try an aligned pread */
139 if ((channel->align == 0) ||
140 (IS_ALIGNED(buf, channel->align) &&
141 IS_ALIGNED(size, channel->align))) {
142 actual = pread64(data->dev, buf, size, location);
147 /* Try an aligned pread */
148 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
149 ((channel->align == 0) ||
150 (IS_ALIGNED(buf, channel->align) &&
151 IS_ALIGNED(size, channel->align)))) {
152 actual = pread(data->dev, buf, size, location);
156 #endif /* HAVE_PREAD */
158 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
159 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
162 if ((channel->align == 0) ||
163 (IS_ALIGNED(buf, channel->align) &&
164 IS_ALIGNED(size, channel->align))) {
165 actual = read(data->dev, buf, size);
166 if (actual != size) {
170 retval = EXT2_ET_SHORT_READ;
177 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
178 (unsigned long) size);
182 * The buffer or size which we're trying to read isn't aligned
183 * to the O_DIRECT rules, so we need to do this the hard way...
186 actual = read(data->dev, data->bounce, channel->block_size);
187 if (actual != channel->block_size)
190 if (size > channel->block_size)
191 actual = channel->block_size;
192 memcpy(buf, data->bounce, actual);
199 memset((char *) buf+actual, 0, size-actual);
200 if (channel->read_error)
201 retval = (channel->read_error)(channel, block, count, buf,
202 size, actual, retval);
206 static errcode_t raw_write_blk(io_channel channel,
207 struct unix_private_data *data,
208 unsigned long long block,
209 int count, const void *bufv)
212 ext2_loff_t location;
215 const unsigned char *buf = bufv;
218 size = channel->block_size;
223 size = count * channel->block_size;
225 data->io_stats.bytes_written += size;
227 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
230 /* Try an aligned pwrite */
231 if ((channel->align == 0) ||
232 (IS_ALIGNED(buf, channel->align) &&
233 IS_ALIGNED(size, channel->align))) {
234 actual = pwrite64(data->dev, buf, size, location);
239 /* Try an aligned pwrite */
240 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
241 ((channel->align == 0) ||
242 (IS_ALIGNED(buf, channel->align) &&
243 IS_ALIGNED(size, channel->align)))) {
244 actual = pwrite(data->dev, buf, size, location);
248 #endif /* HAVE_PWRITE */
250 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
251 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
255 if ((channel->align == 0) ||
256 (IS_ALIGNED(buf, channel->align) &&
257 IS_ALIGNED(size, channel->align))) {
258 actual = write(data->dev, buf, size);
259 if (actual != size) {
261 retval = EXT2_ET_SHORT_WRITE;
268 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
269 (unsigned long) size);
272 * The buffer or size which we're trying to write isn't aligned
273 * to the O_DIRECT rules, so we need to do this the hard way...
276 if (size < channel->block_size) {
277 actual = read(data->dev, data->bounce,
278 channel->block_size);
279 if (actual != channel->block_size) {
280 retval = EXT2_ET_SHORT_READ;
285 if (size > channel->block_size)
286 actual = channel->block_size;
287 memcpy(data->bounce, buf, actual);
288 actual = write(data->dev, data->bounce, channel->block_size);
289 if (actual != channel->block_size)
297 if (channel->write_error)
298 retval = (channel->write_error)(channel, block, count, buf,
299 size, actual, retval);
305 * Here we implement the cache functions
308 /* Allocate the cache buffers */
309 static errcode_t alloc_cache(io_channel channel,
310 struct unix_private_data *data)
313 struct unix_cache *cache;
316 data->access_time = 0;
317 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
319 cache->access_time = 0;
323 ext2fs_free_mem(&cache->buf);
324 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
328 if (channel->align) {
330 ext2fs_free_mem(&data->bounce);
331 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
336 /* Free the cache buffers */
337 static void free_cache(struct unix_private_data *data)
339 struct unix_cache *cache;
342 data->access_time = 0;
343 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
345 cache->access_time = 0;
349 ext2fs_free_mem(&cache->buf);
352 ext2fs_free_mem(&data->bounce);
357 * Try to find a block in the cache. If the block is not found, and
358 * eldest is a non-zero pointer, then fill in eldest with the cache
359 * entry to that should be reused.
361 static struct unix_cache *find_cached_block(struct unix_private_data *data,
362 unsigned long long block,
363 struct unix_cache **eldest)
365 struct unix_cache *cache, *unused_cache, *oldest_cache;
368 unused_cache = oldest_cache = 0;
369 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
370 if (!cache->in_use) {
372 unused_cache = cache;
375 if (cache->block == block) {
376 cache->access_time = ++data->access_time;
380 (cache->access_time < oldest_cache->access_time))
381 oldest_cache = cache;
384 *eldest = (unused_cache) ? unused_cache : oldest_cache;
389 * Reuse a particular cache entry for another block.
391 static void reuse_cache(io_channel channel, struct unix_private_data *data,
392 struct unix_cache *cache, unsigned long long block)
394 if (cache->dirty && cache->in_use)
395 raw_write_blk(channel, data, cache->block, 1, cache->buf);
399 cache->block = block;
400 cache->access_time = ++data->access_time;
404 * Flush all of the blocks in the cache
406 static errcode_t flush_cached_blocks(io_channel channel,
407 struct unix_private_data *data,
411 struct unix_cache *cache;
412 errcode_t retval, retval2;
416 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
426 retval = raw_write_blk(channel, data,
427 cache->block, 1, cache->buf);
435 #endif /* NO_IO_CACHE */
438 #ifndef BLKDISCARDZEROES
439 #define BLKDISCARDZEROES _IO(0x12,124)
443 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
446 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
447 return open64(pathname, flags, mode);
449 return open64(pathname, flags);
451 return open(pathname, flags, mode);
453 return open(pathname, flags);
457 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
459 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
460 return stat64(path, buf);
462 return stat(path, buf);
466 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
468 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
469 return fstat64(fd, buf);
471 return fstat(fd, buf);
475 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
477 io_channel io = NULL;
478 struct unix_private_data *data = NULL;
482 ext2fs_struct_stat st;
488 return EXT2_ET_BAD_DEVICE_NAME;
489 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
492 memset(io, 0, sizeof(struct struct_io_channel));
493 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
494 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
498 io->manager = unix_io_manager;
499 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
503 strcpy(io->name, name);
504 io->private_data = data;
505 io->block_size = 1024;
510 memset(data, 0, sizeof(struct unix_private_data));
511 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
512 data->io_stats.num_fields = 2;
515 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
516 if (flags & IO_FLAG_EXCLUSIVE)
517 open_flags |= O_EXCL;
518 #if defined(O_DIRECT)
519 if (flags & IO_FLAG_DIRECT_IO) {
520 open_flags |= O_DIRECT;
521 io->align = ext2fs_get_dio_alignment(data->dev);
523 #elif defined(F_NOCACHE)
524 if (flags & IO_FLAG_DIRECT_IO) {
525 f_nocache = F_NOCACHE;
531 data->dev = ext2fs_open_file(io->name, open_flags, 0);
537 if (fcntl(data->dev, f_nocache, 1) < 0) {
544 * If the device is really a block device, then set the
545 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
546 * because we are going to use punch hole instead of discard
547 * and if it succeed, subsequent read from sparse area returns
550 if (ext2fs_stat(io->name, &st) == 0) {
551 if (S_ISBLK(st.st_mode))
552 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
554 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
557 #ifdef BLKDISCARDZEROES
560 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
562 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
566 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
568 * Some operating systems require that the buffers be aligned,
569 * regardless of O_DIRECT
576 if ((retval = alloc_cache(io, data)))
580 if (flags & IO_FLAG_RW) {
584 /* Is the block device actually writable? */
585 error = ioctl(data->dev, BLKROGET, &readonly);
586 if (!error && readonly) {
595 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
596 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
598 #define RLIM_INFINITY (~0UL)
601 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
602 * block devices are wrongly getting hit by the filesize
603 * limit. This workaround isn't perfect, since it won't work
604 * if glibc wasn't built against 2.2 header files. (Sigh.)
607 if ((flags & IO_FLAG_RW) &&
609 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
610 (ut.release[2] == '4') && (ut.release[3] == '.') &&
611 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
612 (ut.release[5] < '8')) &&
613 (ext2fs_stat(io->name, &st) == 0) &&
614 (S_ISBLK(st.st_mode))) {
617 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
618 setrlimit(RLIMIT_FSIZE, &rlim);
619 getrlimit(RLIMIT_FSIZE, &rlim);
620 if (((unsigned long) rlim.rlim_cur) <
621 ((unsigned long) rlim.rlim_max)) {
622 rlim.rlim_cur = rlim.rlim_max;
623 setrlimit(RLIMIT_FSIZE, &rlim);
635 ext2fs_free_mem(&data);
639 ext2fs_free_mem(&io->name);
641 ext2fs_free_mem(&io);
646 static errcode_t unix_close(io_channel channel)
648 struct unix_private_data *data;
649 errcode_t retval = 0;
651 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
652 data = (struct unix_private_data *) channel->private_data;
653 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
655 if (--channel->refcount > 0)
659 retval = flush_cached_blocks(channel, data, 0);
662 if (close(data->dev) < 0)
666 ext2fs_free_mem(&channel->private_data);
668 ext2fs_free_mem(&channel->name);
669 ext2fs_free_mem(&channel);
673 static errcode_t unix_set_blksize(io_channel channel, int blksize)
675 struct unix_private_data *data;
678 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
679 data = (struct unix_private_data *) channel->private_data;
680 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
682 if (channel->block_size != blksize) {
684 if ((retval = flush_cached_blocks(channel, data, 0)))
688 channel->block_size = blksize;
690 if ((retval = alloc_cache(channel, data)))
697 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
698 int count, void *buf)
700 struct unix_private_data *data;
701 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
706 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
707 data = (struct unix_private_data *) channel->private_data;
708 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
711 return raw_read_blk(channel, data, block, count, buf);
714 * If we're doing an odd-sized read or a very large read,
715 * flush out the cache and then do a direct read.
717 if (count < 0 || count > WRITE_DIRECT_SIZE) {
718 if ((retval = flush_cached_blocks(channel, data, 0)))
720 return raw_read_blk(channel, data, block, count, buf);
725 /* If it's in the cache, use it! */
726 if ((cache = find_cached_block(data, block, &reuse[0]))) {
728 printf("Using cached block %lu\n", block);
730 memcpy(cp, cache->buf, channel->block_size);
733 cp += channel->block_size;
738 * Special case where we read directly into the
739 * cache buffer; important in the O_DIRECT case
742 reuse_cache(channel, data, cache, block);
743 if ((retval = raw_read_blk(channel, data, block, 1,
748 memcpy(cp, cache->buf, channel->block_size);
753 * Find the number of uncached blocks so we can do a
754 * single read request
756 for (i=1; i < count; i++)
757 if (find_cached_block(data, block+i, &reuse[i]))
760 printf("Reading %d blocks starting at %lu\n", i, block);
762 if ((retval = raw_read_blk(channel, data, block, i, cp)))
765 /* Save the results in the cache */
766 for (j=0; j < i; j++) {
769 reuse_cache(channel, data, cache, block++);
770 memcpy(cache->buf, cp, channel->block_size);
771 cp += channel->block_size;
775 #endif /* NO_IO_CACHE */
778 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
779 int count, void *buf)
781 return unix_read_blk64(channel, block, count, buf);
784 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
785 int count, const void *buf)
787 struct unix_private_data *data;
788 struct unix_cache *cache, *reuse;
789 errcode_t retval = 0;
793 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
794 data = (struct unix_private_data *) channel->private_data;
795 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
798 return raw_write_blk(channel, data, block, count, buf);
801 * If we're doing an odd-sized write or a very large write,
802 * flush out the cache completely and then do a direct write.
804 if (count < 0 || count > WRITE_DIRECT_SIZE) {
805 if ((retval = flush_cached_blocks(channel, data, 1)))
807 return raw_write_blk(channel, data, block, count, buf);
811 * For a moderate-sized multi-block write, first force a write
812 * if we're in write-through cache mode, and then fill the
813 * cache with the blocks.
815 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
817 retval = raw_write_blk(channel, data, block, count, buf);
821 cache = find_cached_block(data, block, &reuse);
824 reuse_cache(channel, data, cache, block);
826 if (cache->buf != cp)
827 memcpy(cache->buf, cp, channel->block_size);
828 cache->dirty = !writethrough;
831 cp += channel->block_size;
834 #endif /* NO_IO_CACHE */
837 static errcode_t unix_cache_readahead(io_channel channel,
838 unsigned long long block,
839 unsigned long long count)
841 #ifdef POSIX_FADV_WILLNEED
842 struct unix_private_data *data;
844 data = (struct unix_private_data *)channel->private_data;
845 return posix_fadvise(data->dev,
846 (ext2_loff_t)block * channel->block_size,
847 (ext2_loff_t)count * channel->block_size,
848 POSIX_FADV_WILLNEED);
850 return EXT2_ET_OP_NOT_SUPPORTED;
854 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
855 int count, const void *buf)
857 return unix_write_blk64(channel, block, count, buf);
860 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
861 int size, const void *buf)
863 struct unix_private_data *data;
864 errcode_t retval = 0;
867 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
868 data = (struct unix_private_data *) channel->private_data;
869 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
871 if (channel->align != 0) {
873 printf("unix_write_byte: O_DIRECT fallback\n");
875 return EXT2_ET_UNIMPLEMENTED;
880 * Flush out the cache completely
882 if ((retval = flush_cached_blocks(channel, data, 1)))
886 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
889 actual = write(data->dev, buf, size);
891 return EXT2_ET_SHORT_WRITE;
897 * Flush data buffers to disk.
899 static errcode_t unix_flush(io_channel channel)
901 struct unix_private_data *data;
902 errcode_t retval = 0;
904 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
905 data = (struct unix_private_data *) channel->private_data;
906 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
909 retval = flush_cached_blocks(channel, data, 0);
915 static errcode_t unix_set_option(io_channel channel, const char *option,
918 struct unix_private_data *data;
919 unsigned long long tmp;
922 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
923 data = (struct unix_private_data *) channel->private_data;
924 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
926 if (!strcmp(option, "offset")) {
928 return EXT2_ET_INVALID_ARGUMENT;
930 tmp = strtoull(arg, &end, 0);
932 return EXT2_ET_INVALID_ARGUMENT;
934 if (data->offset < 0)
935 return EXT2_ET_INVALID_ARGUMENT;
938 return EXT2_ET_INVALID_ARGUMENT;
941 #if defined(__linux__) && !defined(BLKDISCARD)
942 #define BLKDISCARD _IO(0x12,119)
945 static errcode_t unix_discard(io_channel channel, unsigned long long block,
946 unsigned long long count)
948 struct unix_private_data *data;
951 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
952 data = (struct unix_private_data *) channel->private_data;
953 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
955 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
959 range[0] = (__u64)(block) * channel->block_size;
960 range[1] = (__u64)(count) * channel->block_size;
962 ret = ioctl(data->dev, BLKDISCARD, &range);
967 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
969 * If we are not on block device, try to use punch hole
970 * to reclaim free space.
972 ret = fallocate(data->dev,
973 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
974 (off_t)(block) * channel->block_size,
975 (off_t)(count) * channel->block_size);
981 if (errno == EOPNOTSUPP)
987 return EXT2_ET_UNIMPLEMENTED;
990 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
991 unsigned long long count)
993 struct unix_private_data *data;
996 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
997 data = (struct unix_private_data *) channel->private_data;
998 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1000 if (getenv("UNIX_IO_NOZEROOUT"))
1003 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1004 /* Not implemented until the BLKZEROOUT mess is fixed */
1007 /* Regular file, try to use truncate/punch/zero. */
1008 #if defined(HAVE_FALLOCATE) && (defined(FALLOC_FL_ZERO_RANGE) || \
1009 (defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)))
1010 struct stat statbuf;
1015 * If we're trying to zero a range past the end of the file,
1016 * extend the file size, then punch (or zero_range) everything.
1018 ret = fstat(data->dev, &statbuf);
1021 if (statbuf.st_size < (block + count) * channel->block_size) {
1022 ret = ftruncate(data->dev,
1023 (block + count) * channel->block_size);
1027 #if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1028 ret = fallocate(data->dev,
1029 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1030 (off_t)(block) * channel->block_size,
1031 (off_t)(count) * channel->block_size);
1035 #ifdef FALLOC_FL_ZERO_RANGE
1036 ret = fallocate(data->dev,
1037 FALLOC_FL_ZERO_RANGE,
1038 (off_t)(block) * channel->block_size,
1039 (off_t)(count) * channel->block_size);
1043 #endif /* HAVE_FALLOCATE && (ZERO_RANGE || (PUNCH_HOLE && KEEP_SIZE)) */
1047 if (errno == EOPNOTSUPP)
1053 return EXT2_ET_UNIMPLEMENTED;
1056 static struct struct_io_manager struct_unix_manager = {
1057 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1058 .name = "Unix I/O Manager",
1060 .close = unix_close,
1061 .set_blksize = unix_set_blksize,
1062 .read_blk = unix_read_blk,
1063 .write_blk = unix_write_blk,
1064 .flush = unix_flush,
1065 .write_byte = unix_write_byte,
1066 .set_option = unix_set_option,
1067 .get_stats = unix_get_stats,
1068 .read_blk64 = unix_read_blk64,
1069 .write_blk64 = unix_write_blk64,
1070 .discard = unix_discard,
1071 .cache_readahead = unix_cache_readahead,
1072 .zeroout = unix_zeroout,
1075 io_manager unix_io_manager = &struct_unix_manager;