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). */
82 * For checking structure magic numbers...
85 #define EXT2_CHECK_MAGIC(struct, code) \
86 if ((struct)->magic != (code)) return (code)
90 unsigned long long block
;
97 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
98 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
100 struct unix_private_data
{
107 struct unix_cache cache
[CACHE_SIZE
];
109 struct struct_io_stats io_stats
;
112 #define IS_ALIGNED(n, align) ((((uintptr_t) n) & \
113 ((uintptr_t) ((align)-1))) == 0)
115 static errcode_t
unix_get_stats(io_channel channel
, io_stats
*stats
)
117 errcode_t retval
= 0;
119 struct unix_private_data
*data
;
121 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
122 data
= (struct unix_private_data
*) channel
->private_data
;
123 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
126 *stats
= &data
->io_stats
;
131 static char *safe_getenv(const char *arg
)
133 if ((getuid() != geteuid()) || (getgid() != getegid()))
136 if (prctl(PR_GET_DUMPABLE
, 0, 0, 0, 0) == 0)
139 #if (defined(linux) && defined(SYS_prctl))
140 if (syscall(SYS_prctl
, PR_GET_DUMPABLE
, 0, 0, 0, 0) == 0)
145 #if defined(HAVE_SECURE_GETENV)
146 return secure_getenv(arg
);
147 #elif defined(HAVE___SECURE_GETENV)
148 return __secure_getenv(arg
);
155 * Here are the raw I/O functions
157 static errcode_t
raw_read_blk(io_channel channel
,
158 struct unix_private_data
*data
,
159 unsigned long long block
,
160 int count
, void *bufv
)
164 ext2_loff_t location
;
166 unsigned char *buf
= bufv
;
167 ssize_t really_read
= 0;
169 size
= (count
< 0) ? -count
: count
* channel
->block_size
;
170 data
->io_stats
.bytes_read
+= size
;
171 location
= ((ext2_loff_t
) block
* channel
->block_size
) + data
->offset
;
173 if (data
->flags
& IO_FLAG_FORCE_BOUNCE
) {
174 if (ext2fs_llseek(data
->dev
, location
, SEEK_SET
) != location
) {
175 retval
= errno
? errno
: EXT2_ET_LLSEEK_FAILED
;
182 /* Try an aligned pread */
183 if ((channel
->align
== 0) ||
184 (IS_ALIGNED(buf
, channel
->align
) &&
185 IS_ALIGNED(size
, channel
->align
))) {
186 actual
= pread64(data
->dev
, buf
, size
, location
);
192 /* Try an aligned pread */
193 if ((sizeof(off_t
) >= sizeof(ext2_loff_t
)) &&
194 ((channel
->align
== 0) ||
195 (IS_ALIGNED(buf
, channel
->align
) &&
196 IS_ALIGNED(size
, channel
->align
)))) {
197 actual
= pread(data
->dev
, buf
, size
, location
);
202 #endif /* HAVE_PREAD */
204 if (ext2fs_llseek(data
->dev
, location
, SEEK_SET
) != location
) {
205 retval
= errno
? errno
: EXT2_ET_LLSEEK_FAILED
;
208 if ((channel
->align
== 0) ||
209 (IS_ALIGNED(buf
, channel
->align
) &&
210 IS_ALIGNED(size
, channel
->align
))) {
211 actual
= read(data
->dev
, buf
, size
);
212 if (actual
!= size
) {
218 retval
= EXT2_ET_SHORT_READ
;
225 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf
,
226 (unsigned long) size
);
230 * The buffer or size which we're trying to read isn't aligned
231 * to the O_DIRECT rules, so we need to do this the hard way...
235 actual
= read(data
->dev
, data
->bounce
, channel
->block_size
);
236 if (actual
!= channel
->block_size
) {
237 actual
= really_read
;
243 if (size
> channel
->block_size
)
244 actual
= channel
->block_size
;
245 memcpy(buf
, data
->bounce
, actual
);
246 really_read
+= actual
;
253 if (actual
>= 0 && actual
< size
)
254 memset((char *) buf
+actual
, 0, size
-actual
);
255 if (channel
->read_error
)
256 retval
= (channel
->read_error
)(channel
, block
, count
, buf
,
257 size
, actual
, retval
);
261 static errcode_t
raw_write_blk(io_channel channel
,
262 struct unix_private_data
*data
,
263 unsigned long long block
,
264 int count
, const void *bufv
)
267 ext2_loff_t location
;
270 const unsigned char *buf
= bufv
;
273 size
= channel
->block_size
;
278 size
= count
* channel
->block_size
;
280 data
->io_stats
.bytes_written
+= size
;
282 location
= ((ext2_loff_t
) block
* channel
->block_size
) + data
->offset
;
284 if (data
->flags
& IO_FLAG_FORCE_BOUNCE
) {
285 if (ext2fs_llseek(data
->dev
, location
, SEEK_SET
) != location
) {
286 retval
= errno
? errno
: EXT2_ET_LLSEEK_FAILED
;
293 /* Try an aligned pwrite */
294 if ((channel
->align
== 0) ||
295 (IS_ALIGNED(buf
, channel
->align
) &&
296 IS_ALIGNED(size
, channel
->align
))) {
297 actual
= pwrite64(data
->dev
, buf
, size
, location
);
302 /* Try an aligned pwrite */
303 if ((sizeof(off_t
) >= sizeof(ext2_loff_t
)) &&
304 ((channel
->align
== 0) ||
305 (IS_ALIGNED(buf
, channel
->align
) &&
306 IS_ALIGNED(size
, channel
->align
)))) {
307 actual
= pwrite(data
->dev
, buf
, size
, location
);
311 #endif /* HAVE_PWRITE */
313 if (ext2fs_llseek(data
->dev
, location
, SEEK_SET
) != location
) {
314 retval
= errno
? errno
: EXT2_ET_LLSEEK_FAILED
;
318 if ((channel
->align
== 0) ||
319 (IS_ALIGNED(buf
, channel
->align
) &&
320 IS_ALIGNED(size
, channel
->align
))) {
321 actual
= write(data
->dev
, buf
, size
);
326 if (actual
!= size
) {
328 retval
= EXT2_ET_SHORT_WRITE
;
335 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf
,
336 (unsigned long) size
);
339 * The buffer or size which we're trying to write isn't aligned
340 * to the O_DIRECT rules, so we need to do this the hard way...
344 if (size
< channel
->block_size
) {
345 actual
= read(data
->dev
, data
->bounce
,
346 channel
->block_size
);
347 if (actual
!= channel
->block_size
) {
352 memset((char *) data
->bounce
+ actual
, 0,
353 channel
->block_size
- actual
);
357 if (size
> channel
->block_size
)
358 actual
= channel
->block_size
;
359 memcpy(data
->bounce
, buf
, actual
);
360 if (ext2fs_llseek(data
->dev
, location
, SEEK_SET
) != location
) {
361 retval
= errno
? errno
: EXT2_ET_LLSEEK_FAILED
;
364 actual
= write(data
->dev
, data
->bounce
, channel
->block_size
);
369 if (actual
!= channel
->block_size
)
378 if (channel
->write_error
)
379 retval
= (channel
->write_error
)(channel
, block
, count
, buf
,
380 size
, actual
, retval
);
386 * Here we implement the cache functions
389 /* Allocate the cache buffers */
390 static errcode_t
alloc_cache(io_channel channel
,
391 struct unix_private_data
*data
)
394 struct unix_cache
*cache
;
397 data
->access_time
= 0;
398 for (i
=0, cache
= data
->cache
; i
< CACHE_SIZE
; i
++, cache
++) {
400 cache
->access_time
= 0;
404 ext2fs_free_mem(&cache
->buf
);
405 retval
= io_channel_alloc_buf(channel
, 0, &cache
->buf
);
409 if (channel
->align
|| data
->flags
& IO_FLAG_FORCE_BOUNCE
) {
411 ext2fs_free_mem(&data
->bounce
);
412 retval
= io_channel_alloc_buf(channel
, 0, &data
->bounce
);
417 /* Free the cache buffers */
418 static void free_cache(struct unix_private_data
*data
)
420 struct unix_cache
*cache
;
423 data
->access_time
= 0;
424 for (i
=0, cache
= data
->cache
; i
< CACHE_SIZE
; i
++, cache
++) {
426 cache
->access_time
= 0;
430 ext2fs_free_mem(&cache
->buf
);
433 ext2fs_free_mem(&data
->bounce
);
438 * Try to find a block in the cache. If the block is not found, and
439 * eldest is a non-zero pointer, then fill in eldest with the cache
440 * entry to that should be reused.
442 static struct unix_cache
*find_cached_block(struct unix_private_data
*data
,
443 unsigned long long block
,
444 struct unix_cache
**eldest
)
446 struct unix_cache
*cache
, *unused_cache
, *oldest_cache
;
449 unused_cache
= oldest_cache
= 0;
450 for (i
=0, cache
= data
->cache
; i
< CACHE_SIZE
; i
++, cache
++) {
451 if (!cache
->in_use
) {
453 unused_cache
= cache
;
456 if (cache
->block
== block
) {
457 cache
->access_time
= ++data
->access_time
;
461 (cache
->access_time
< oldest_cache
->access_time
))
462 oldest_cache
= cache
;
465 *eldest
= (unused_cache
) ? unused_cache
: oldest_cache
;
470 * Reuse a particular cache entry for another block.
472 static void reuse_cache(io_channel channel
, struct unix_private_data
*data
,
473 struct unix_cache
*cache
, unsigned long long block
)
475 if (cache
->dirty
&& cache
->in_use
)
476 raw_write_blk(channel
, data
, cache
->block
, 1, cache
->buf
);
480 cache
->block
= block
;
481 cache
->access_time
= ++data
->access_time
;
485 * Flush all of the blocks in the cache
487 static errcode_t
flush_cached_blocks(io_channel channel
,
488 struct unix_private_data
*data
,
492 struct unix_cache
*cache
;
493 errcode_t retval
, retval2
;
497 for (i
=0, cache
= data
->cache
; i
< CACHE_SIZE
; i
++, cache
++) {
507 retval
= raw_write_blk(channel
, data
,
508 cache
->block
, 1, cache
->buf
);
516 #endif /* NO_IO_CACHE */
519 #ifndef BLKDISCARDZEROES
520 #define BLKDISCARDZEROES _IO(0x12,124)
524 int ext2fs_open_file(const char *pathname
, int flags
, mode_t mode
)
527 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
528 return open64(pathname
, flags
, mode
);
530 return open64(pathname
, flags
);
532 return open(pathname
, flags
, mode
);
534 return open(pathname
, flags
);
538 int ext2fs_stat(const char *path
, ext2fs_struct_stat
*buf
)
540 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
541 return stat64(path
, buf
);
543 return stat(path
, buf
);
547 int ext2fs_fstat(int fd
, ext2fs_struct_stat
*buf
)
549 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
550 return fstat64(fd
, buf
);
552 return fstat(fd
, buf
);
557 static errcode_t
unix_open_channel(const char *name
, int fd
,
558 int flags
, io_channel
*channel
,
561 io_channel io
= NULL
;
562 struct unix_private_data
*data
= NULL
;
564 ext2fs_struct_stat st
;
569 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
570 flags
|= IO_FLAG_FORCE_BOUNCE
;
574 * We need to make sure any previous errors in the block
575 * device are thrown away, sigh.
580 retval
= ext2fs_get_mem(sizeof(struct struct_io_channel
), &io
);
583 memset(io
, 0, sizeof(struct struct_io_channel
));
584 io
->magic
= EXT2_ET_MAGIC_IO_CHANNEL
;
585 retval
= ext2fs_get_mem(sizeof(struct unix_private_data
), &data
);
589 io
->manager
= io_mgr
;
590 retval
= ext2fs_get_mem(strlen(name
)+1, &io
->name
);
594 strcpy(io
->name
, name
);
595 io
->private_data
= data
;
596 io
->block_size
= 1024;
601 memset(data
, 0, sizeof(struct unix_private_data
));
602 data
->magic
= EXT2_ET_MAGIC_UNIX_IO_CHANNEL
;
603 data
->io_stats
.num_fields
= 2;
607 #if defined(O_DIRECT)
608 if (flags
& IO_FLAG_DIRECT_IO
)
609 io
->align
= ext2fs_get_dio_alignment(data
->dev
);
610 #elif defined(F_NOCACHE)
611 if (flags
& IO_FLAG_DIRECT_IO
)
616 * If the device is really a block device, then set the
617 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
618 * because we are going to use punch hole instead of discard
619 * and if it succeed, subsequent read from sparse area returns
622 if (ext2fs_fstat(data
->dev
, &st
) == 0) {
623 if (ext2fsP_is_disk_device(st
.st_mode
))
624 io
->flags
|= CHANNEL_FLAGS_BLOCK_DEVICE
;
626 io
->flags
|= CHANNEL_FLAGS_DISCARD_ZEROES
;
629 #ifdef BLKDISCARDZEROES
632 if (ioctl(data
->dev
, BLKDISCARDZEROES
, &zeroes
) == 0 &&
634 io
->flags
|= CHANNEL_FLAGS_DISCARD_ZEROES
;
638 #if defined(__CYGWIN__)
640 * Some operating systems require that the buffers be aligned,
641 * regardless of O_DIRECT
647 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
648 if (io
->flags
& CHANNEL_FLAGS_BLOCK_DEVICE
) {
649 int dio_align
= ext2fs_get_dio_alignment(fd
);
651 if (io
->align
< dio_align
)
652 io
->align
= dio_align
;
656 if ((retval
= alloc_cache(io
, data
)))
660 if (flags
& IO_FLAG_RW
) {
664 /* Is the block device actually writable? */
665 error
= ioctl(data
->dev
, BLKROGET
, &readonly
);
666 if (!error
&& readonly
) {
675 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
676 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
678 #define RLIM_INFINITY (~0UL)
681 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
682 * block devices are wrongly getting hit by the filesize
683 * limit. This workaround isn't perfect, since it won't work
684 * if glibc wasn't built against 2.2 header files. (Sigh.)
687 if ((flags
& IO_FLAG_RW
) &&
689 ((ut
.release
[0] == '2') && (ut
.release
[1] == '.') &&
690 (ut
.release
[2] == '4') && (ut
.release
[3] == '.') &&
691 (ut
.release
[4] == '1') && (ut
.release
[5] >= '0') &&
692 (ut
.release
[5] < '8')) &&
693 (ext2fs_fstat(data
->dev
, &st
) == 0) &&
694 (ext2fsP_is_disk_device(st
.st_mode
))) {
697 rlim
.rlim_cur
= rlim
.rlim_max
= (unsigned long) RLIM_INFINITY
;
698 setrlimit(RLIMIT_FSIZE
, &rlim
);
699 getrlimit(RLIMIT_FSIZE
, &rlim
);
700 if (((unsigned long) rlim
.rlim_cur
) <
701 ((unsigned long) rlim
.rlim_max
)) {
702 rlim
.rlim_cur
= rlim
.rlim_max
;
703 setrlimit(RLIMIT_FSIZE
, &rlim
);
715 ext2fs_free_mem(&data
);
719 ext2fs_free_mem(&io
->name
);
721 ext2fs_free_mem(&io
);
726 static errcode_t
unixfd_open(const char *str_fd
, int flags
,
733 #if defined(HAVE_FCNTL)
734 fd_flags
= fcntl(fd
, F_GETFD
);
739 if (fd_flags
& O_RDWR
)
741 if (fd_flags
& O_EXCL
)
742 flags
|= IO_FLAG_EXCLUSIVE
;
743 #if defined(O_DIRECT)
744 if (fd_flags
& O_DIRECT
)
745 flags
|= IO_FLAG_DIRECT_IO
;
747 #endif /* HAVE_FCNTL */
749 return unix_open_channel(str_fd
, fd
, flags
, channel
, unixfd_io_manager
);
752 static errcode_t
unix_open(const char *name
, int flags
,
759 return EXT2_ET_BAD_DEVICE_NAME
;
761 open_flags
= (flags
& IO_FLAG_RW
) ? O_RDWR
: O_RDONLY
;
762 if (flags
& IO_FLAG_EXCLUSIVE
)
763 open_flags
|= O_EXCL
;
764 #if defined(O_DIRECT)
765 if (flags
& IO_FLAG_DIRECT_IO
)
766 open_flags
|= O_DIRECT
;
768 fd
= ext2fs_open_file(name
, open_flags
, 0);
771 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
772 if (flags
& IO_FLAG_DIRECT_IO
) {
773 if (fcntl(fd
, F_NOCACHE
, 1) < 0)
777 return unix_open_channel(name
, fd
, flags
, channel
, unix_io_manager
);
780 static errcode_t
unix_close(io_channel channel
)
782 struct unix_private_data
*data
;
783 errcode_t retval
= 0;
785 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
786 data
= (struct unix_private_data
*) channel
->private_data
;
787 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
789 if (--channel
->refcount
> 0)
793 retval
= flush_cached_blocks(channel
, data
, 0);
796 if (close(data
->dev
) < 0)
800 ext2fs_free_mem(&channel
->private_data
);
802 ext2fs_free_mem(&channel
->name
);
803 ext2fs_free_mem(&channel
);
807 static errcode_t
unix_set_blksize(io_channel channel
, int blksize
)
809 struct unix_private_data
*data
;
812 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
813 data
= (struct unix_private_data
*) channel
->private_data
;
814 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
816 if (channel
->block_size
!= blksize
) {
818 if ((retval
= flush_cached_blocks(channel
, data
, 0)))
822 channel
->block_size
= blksize
;
824 if ((retval
= alloc_cache(channel
, data
)))
830 static errcode_t
unix_read_blk64(io_channel channel
, unsigned long long block
,
831 int count
, void *buf
)
833 struct unix_private_data
*data
;
834 struct unix_cache
*cache
, *reuse
[READ_DIRECT_SIZE
];
839 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
840 data
= (struct unix_private_data
*) channel
->private_data
;
841 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
844 return raw_read_blk(channel
, data
, block
, count
, buf
);
847 * If we're doing an odd-sized read or a very large read,
848 * flush out the cache and then do a direct read.
850 if (count
< 0 || count
> WRITE_DIRECT_SIZE
) {
851 if ((retval
= flush_cached_blocks(channel
, data
, 0)))
853 return raw_read_blk(channel
, data
, block
, count
, buf
);
858 /* If it's in the cache, use it! */
859 if ((cache
= find_cached_block(data
, block
, &reuse
[0]))) {
861 printf("Using cached block %lu\n", block
);
863 memcpy(cp
, cache
->buf
, channel
->block_size
);
866 cp
+= channel
->block_size
;
871 * Special case where we read directly into the
872 * cache buffer; important in the O_DIRECT case
875 reuse_cache(channel
, data
, cache
, block
);
876 if ((retval
= raw_read_blk(channel
, data
, block
, 1,
881 memcpy(cp
, cache
->buf
, channel
->block_size
);
886 * Find the number of uncached blocks so we can do a
887 * single read request
889 for (i
=1; i
< count
; i
++)
890 if (find_cached_block(data
, block
+i
, &reuse
[i
]))
893 printf("Reading %d blocks starting at %lu\n", i
, block
);
895 if ((retval
= raw_read_blk(channel
, data
, block
, i
, cp
)))
898 /* Save the results in the cache */
899 for (j
=0; j
< i
; j
++) {
902 reuse_cache(channel
, data
, cache
, block
++);
903 memcpy(cache
->buf
, cp
, channel
->block_size
);
904 cp
+= channel
->block_size
;
908 #endif /* NO_IO_CACHE */
911 static errcode_t
unix_read_blk(io_channel channel
, unsigned long block
,
912 int count
, void *buf
)
914 return unix_read_blk64(channel
, block
, count
, buf
);
917 static errcode_t
unix_write_blk64(io_channel channel
, unsigned long long block
,
918 int count
, const void *buf
)
920 struct unix_private_data
*data
;
921 struct unix_cache
*cache
, *reuse
;
922 errcode_t retval
= 0;
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
);
931 return raw_write_blk(channel
, data
, block
, count
, buf
);
934 * If we're doing an odd-sized write or a very large write,
935 * flush out the cache completely and then do a direct write.
937 if (count
< 0 || count
> WRITE_DIRECT_SIZE
) {
938 if ((retval
= flush_cached_blocks(channel
, data
, 1)))
940 return raw_write_blk(channel
, data
, block
, count
, buf
);
944 * For a moderate-sized multi-block write, first force a write
945 * if we're in write-through cache mode, and then fill the
946 * cache with the blocks.
948 writethrough
= channel
->flags
& CHANNEL_FLAGS_WRITETHROUGH
;
950 retval
= raw_write_blk(channel
, data
, block
, count
, buf
);
954 cache
= find_cached_block(data
, block
, &reuse
);
957 reuse_cache(channel
, data
, cache
, block
);
959 if (cache
->buf
!= cp
)
960 memcpy(cache
->buf
, cp
, channel
->block_size
);
961 cache
->dirty
= !writethrough
;
964 cp
+= channel
->block_size
;
967 #endif /* NO_IO_CACHE */
970 static errcode_t
unix_cache_readahead(io_channel channel
,
971 unsigned long long block
,
972 unsigned long long count
)
974 #ifdef POSIX_FADV_WILLNEED
975 struct unix_private_data
*data
;
977 data
= (struct unix_private_data
*)channel
->private_data
;
978 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
979 return posix_fadvise(data
->dev
,
980 (ext2_loff_t
)block
* channel
->block_size
+ data
->offset
,
981 (ext2_loff_t
)count
* channel
->block_size
,
982 POSIX_FADV_WILLNEED
);
984 return EXT2_ET_OP_NOT_SUPPORTED
;
988 static errcode_t
unix_write_blk(io_channel channel
, unsigned long block
,
989 int count
, const void *buf
)
991 return unix_write_blk64(channel
, block
, count
, buf
);
994 static errcode_t
unix_write_byte(io_channel channel
, unsigned long offset
,
995 int size
, const void *buf
)
997 struct unix_private_data
*data
;
998 errcode_t retval
= 0;
1001 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
1002 data
= (struct unix_private_data
*) channel
->private_data
;
1003 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
1005 if (channel
->align
!= 0) {
1007 printf("unix_write_byte: O_DIRECT fallback\n");
1009 return EXT2_ET_UNIMPLEMENTED
;
1014 * Flush out the cache completely
1016 if ((retval
= flush_cached_blocks(channel
, data
, 1)))
1020 if (lseek(data
->dev
, offset
+ data
->offset
, SEEK_SET
) < 0)
1023 actual
= write(data
->dev
, buf
, size
);
1027 return EXT2_ET_SHORT_WRITE
;
1033 * Flush data buffers to disk.
1035 static errcode_t
unix_flush(io_channel channel
)
1037 struct unix_private_data
*data
;
1038 errcode_t retval
= 0;
1040 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
1041 data
= (struct unix_private_data
*) channel
->private_data
;
1042 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
1045 retval
= flush_cached_blocks(channel
, data
, 0);
1048 if (!retval
&& fsync(data
->dev
) != 0)
1054 static errcode_t
unix_set_option(io_channel channel
, const char *option
,
1057 struct unix_private_data
*data
;
1058 unsigned long long tmp
;
1061 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
1062 data
= (struct unix_private_data
*) channel
->private_data
;
1063 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
1065 if (!strcmp(option
, "offset")) {
1067 return EXT2_ET_INVALID_ARGUMENT
;
1069 tmp
= strtoull(arg
, &end
, 0);
1071 return EXT2_ET_INVALID_ARGUMENT
;
1073 if (data
->offset
< 0)
1074 return EXT2_ET_INVALID_ARGUMENT
;
1077 return EXT2_ET_INVALID_ARGUMENT
;
1080 #if defined(__linux__) && !defined(BLKDISCARD)
1081 #define BLKDISCARD _IO(0x12,119)
1084 static errcode_t
unix_discard(io_channel channel
, unsigned long long block
,
1085 unsigned long long count
)
1087 struct unix_private_data
*data
;
1090 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
1091 data
= (struct unix_private_data
*) channel
->private_data
;
1092 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
1094 if (channel
->flags
& CHANNEL_FLAGS_BLOCK_DEVICE
) {
1098 range
[0] = (__u64
)(block
) * channel
->block_size
+ data
->offset
;
1099 range
[1] = (__u64
)(count
) * channel
->block_size
;
1101 ret
= ioctl(data
->dev
, BLKDISCARD
, &range
);
1106 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1108 * If we are not on block device, try to use punch hole
1109 * to reclaim free space.
1111 ret
= fallocate(data
->dev
,
1112 FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
1113 (off_t
)(block
) * channel
->block_size
+ data
->offset
,
1114 (off_t
)(count
) * channel
->block_size
);
1120 if (errno
== EOPNOTSUPP
)
1126 return EXT2_ET_UNIMPLEMENTED
;
1130 * If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because
1131 * ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because
1132 * it always invalidates page cache, and libext2fs requires that reads after
1133 * ZERO_RANGE return zeroes.
1135 static int __unix_zeroout(int fd
, off_t offset
, off_t len
)
1139 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
1140 ret
= fallocate(fd
, FALLOC_FL_ZERO_RANGE
, offset
, len
);
1144 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1145 ret
= fallocate(fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
1154 /* parameters might not be used if OS doesn't support zeroout */
1155 #if __GNUC_PREREQ (4, 6)
1156 #pragma GCC diagnostic push
1157 #pragma GCC diagnostic ignored "-Wunused-parameter"
1159 static errcode_t
unix_zeroout(io_channel channel
, unsigned long long block
,
1160 unsigned long long count
)
1162 struct unix_private_data
*data
;
1165 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
1166 data
= (struct unix_private_data
*) channel
->private_data
;
1167 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
1169 if (safe_getenv("UNIX_IO_NOZEROOUT"))
1172 if (!(channel
->flags
& CHANNEL_FLAGS_BLOCK_DEVICE
)) {
1173 /* Regular file, try to use truncate/punch/zero. */
1174 struct stat statbuf
;
1179 * If we're trying to zero a range past the end of the file,
1180 * extend the file size, then truncate everything.
1182 ret
= fstat(data
->dev
, &statbuf
);
1185 if ((unsigned long long) statbuf
.st_size
<
1186 (block
+ count
) * channel
->block_size
+ data
->offset
) {
1187 ret
= ftruncate(data
->dev
,
1188 (block
+ count
) * channel
->block_size
+ data
->offset
);
1194 ret
= __unix_zeroout(data
->dev
,
1195 (off_t
)(block
) * channel
->block_size
+ data
->offset
,
1196 (off_t
)(count
) * channel
->block_size
);
1199 if (errno
== EOPNOTSUPP
)
1205 return EXT2_ET_UNIMPLEMENTED
;
1207 #if __GNUC_PREREQ (4, 6)
1208 #pragma GCC diagnostic pop
1211 static struct struct_io_manager struct_unix_manager
= {
1212 .magic
= EXT2_ET_MAGIC_IO_MANAGER
,
1213 .name
= "Unix I/O Manager",
1215 .close
= unix_close
,
1216 .set_blksize
= unix_set_blksize
,
1217 .read_blk
= unix_read_blk
,
1218 .write_blk
= unix_write_blk
,
1219 .flush
= unix_flush
,
1220 .write_byte
= unix_write_byte
,
1221 .set_option
= unix_set_option
,
1222 .get_stats
= unix_get_stats
,
1223 .read_blk64
= unix_read_blk64
,
1224 .write_blk64
= unix_write_blk64
,
1225 .discard
= unix_discard
,
1226 .cache_readahead
= unix_cache_readahead
,
1227 .zeroout
= unix_zeroout
,
1230 io_manager unix_io_manager
= &struct_unix_manager
;
1232 static struct struct_io_manager struct_unixfd_manager
= {
1233 .magic
= EXT2_ET_MAGIC_IO_MANAGER
,
1234 .name
= "Unix fd I/O Manager",
1235 .open
= unixfd_open
,
1236 .close
= unix_close
,
1237 .set_blksize
= unix_set_blksize
,
1238 .read_blk
= unix_read_blk
,
1239 .write_blk
= unix_write_blk
,
1240 .flush
= unix_flush
,
1241 .write_byte
= unix_write_byte
,
1242 .set_option
= unix_set_option
,
1243 .get_stats
= unix_get_stats
,
1244 .read_blk64
= unix_read_blk64
,
1245 .write_blk64
= unix_write_blk64
,
1246 .discard
= unix_discard
,
1247 .cache_readahead
= unix_cache_readahead
,
1248 .zeroout
= unix_zeroout
,
1251 io_manager unixfd_io_manager
= &struct_unixfd_manager
;