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>
74 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
75 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
85 * For checking structure magic numbers...
88 #define EXT2_CHECK_MAGIC(struct, code) \
89 if ((struct)->magic != (code)) return (code)
93 unsigned long long block;
100 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
101 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
103 struct unix_private_data {
110 struct unix_cache cache[CACHE_SIZE];
112 struct struct_io_stats io_stats;
114 pthread_mutex_t cache_mutex;
115 pthread_mutex_t bounce_mutex;
116 pthread_mutex_t stats_mutex;
120 #define IS_ALIGNED(n, align) ((((uintptr_t) n) & \
121 ((uintptr_t) ((align)-1))) == 0)
123 typedef enum lock_kind {
124 CACHE_MTX, BOUNCE_MTX, STATS_MTX
128 static inline pthread_mutex_t *get_mutex(struct unix_private_data *data,
131 if (data->flags & IO_FLAG_THREADS) {
134 return &data->cache_mutex;
136 return &data->bounce_mutex;
138 return &data->stats_mutex;
145 static inline void mutex_lock(struct unix_private_data *data, kind_t kind)
148 pthread_mutex_t *mtx = get_mutex(data,kind);
151 pthread_mutex_lock(mtx);
155 static inline void mutex_unlock(struct unix_private_data *data, kind_t kind)
158 pthread_mutex_t *mtx = get_mutex(data,kind);
161 pthread_mutex_unlock(mtx);
165 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
167 errcode_t retval = 0;
169 struct unix_private_data *data;
171 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
172 data = (struct unix_private_data *) channel->private_data;
173 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
176 mutex_lock(data, STATS_MTX);
177 *stats = &data->io_stats;
178 mutex_unlock(data, STATS_MTX);
184 static char *safe_getenv(const char *arg)
186 if ((getuid() != geteuid()) || (getgid() != getegid()))
189 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
192 #if (defined(linux) && defined(SYS_prctl))
193 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
198 #if defined(HAVE_SECURE_GETENV)
199 return secure_getenv(arg);
200 #elif defined(HAVE___SECURE_GETENV)
201 return __secure_getenv(arg);
208 * Here are the raw I/O functions
210 static errcode_t raw_read_blk(io_channel channel,
211 struct unix_private_data *data,
212 unsigned long long block,
213 int count, void *bufv)
217 ext2_loff_t location;
219 unsigned char *buf = bufv;
220 ssize_t really_read = 0;
221 unsigned long long aligned_blk;
222 int align_size, offset;
224 size = (count < 0) ? -count : (ext2_loff_t) count * channel->block_size;
225 mutex_lock(data, STATS_MTX);
226 data->io_stats.bytes_read += size;
227 mutex_unlock(data, STATS_MTX);
228 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
230 if (data->flags & IO_FLAG_FORCE_BOUNCE)
234 /* Try an aligned pread */
235 if ((channel->align == 0) ||
236 (IS_ALIGNED(buf, channel->align) &&
237 IS_ALIGNED(location, channel->align) &&
238 IS_ALIGNED(size, channel->align))) {
239 actual = pread64(data->dev, buf, size, location);
245 /* Try an aligned pread */
246 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
247 ((channel->align == 0) ||
248 (IS_ALIGNED(buf, channel->align) &&
249 IS_ALIGNED(location, channel->align) &&
250 IS_ALIGNED(size, channel->align)))) {
251 actual = pread(data->dev, buf, size, location);
256 #endif /* HAVE_PREAD */
258 if ((channel->align == 0) ||
259 (IS_ALIGNED(buf, channel->align) &&
260 IS_ALIGNED(location, channel->align) &&
261 IS_ALIGNED(size, channel->align))) {
262 mutex_lock(data, BOUNCE_MTX);
263 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
264 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
267 actual = read(data->dev, buf, size);
268 if (actual != size) {
274 retval = EXT2_ET_SHORT_READ;
281 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
282 (unsigned long) size);
286 * The buffer or size which we're trying to read isn't aligned
287 * to the O_DIRECT rules, so we need to do this the hard way...
290 if (channel->align == 0)
292 if ((channel->block_size > channel->align) &&
293 (channel->block_size % channel->align) == 0)
294 align_size = channel->block_size;
296 align_size = channel->align;
297 aligned_blk = location / align_size;
298 offset = location % align_size;
300 mutex_lock(data, BOUNCE_MTX);
301 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
302 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
306 actual = read(data->dev, data->bounce, align_size);
307 if (actual != align_size) {
308 mutex_unlock(data, BOUNCE_MTX);
309 actual = really_read;
314 if ((actual + offset) > align_size)
315 actual = align_size - offset;
318 memcpy(buf, (char *)data->bounce + offset, actual);
320 really_read += actual;
327 mutex_unlock(data, BOUNCE_MTX);
331 mutex_unlock(data, BOUNCE_MTX);
332 if (actual >= 0 && actual < size)
333 memset((char *) buf+actual, 0, size-actual);
334 if (channel->read_error)
335 retval = (channel->read_error)(channel, block, count, buf,
336 size, actual, retval);
340 static errcode_t raw_write_blk(io_channel channel,
341 struct unix_private_data *data,
342 unsigned long long block,
343 int count, const void *bufv)
346 ext2_loff_t location;
349 const unsigned char *buf = bufv;
350 unsigned long long aligned_blk;
351 int align_size, offset;
354 size = channel->block_size;
359 size = (ext2_loff_t) count * channel->block_size;
361 mutex_lock(data, STATS_MTX);
362 data->io_stats.bytes_written += size;
363 mutex_unlock(data, STATS_MTX);
365 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
367 if (data->flags & IO_FLAG_FORCE_BOUNCE)
371 /* Try an aligned pwrite */
372 if ((channel->align == 0) ||
373 (IS_ALIGNED(buf, channel->align) &&
374 IS_ALIGNED(location, channel->align) &&
375 IS_ALIGNED(size, channel->align))) {
376 actual = pwrite64(data->dev, buf, size, location);
381 /* Try an aligned pwrite */
382 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
383 ((channel->align == 0) ||
384 (IS_ALIGNED(buf, channel->align) &&
385 IS_ALIGNED(location, channel->align) &&
386 IS_ALIGNED(size, channel->align)))) {
387 actual = pwrite(data->dev, buf, size, location);
391 #endif /* HAVE_PWRITE */
393 if ((channel->align == 0) ||
394 (IS_ALIGNED(buf, channel->align) &&
395 IS_ALIGNED(location, channel->align) &&
396 IS_ALIGNED(size, channel->align))) {
397 mutex_lock(data, BOUNCE_MTX);
398 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
399 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
402 actual = write(data->dev, buf, size);
403 mutex_unlock(data, BOUNCE_MTX);
408 if (actual != size) {
410 retval = EXT2_ET_SHORT_WRITE;
417 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
418 (unsigned long) size);
421 * The buffer or size which we're trying to write isn't aligned
422 * to the O_DIRECT rules, so we need to do this the hard way...
425 if (channel->align == 0)
427 if ((channel->block_size > channel->align) &&
428 (channel->block_size % channel->align) == 0)
429 align_size = channel->block_size;
431 align_size = channel->align;
432 aligned_blk = location / align_size;
433 offset = location % align_size;
438 mutex_lock(data, BOUNCE_MTX);
439 if (size < align_size || offset) {
440 if (ext2fs_llseek(data->dev, aligned_blk * align_size,
442 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
445 actual = read(data->dev, data->bounce,
447 if (actual != align_size) {
452 memset((char *) data->bounce + actual, 0,
453 align_size - actual);
457 if ((actual + offset) > align_size)
458 actual = align_size - offset;
461 memcpy(((char *)data->bounce) + offset, buf, actual);
462 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
463 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
466 actual_w = write(data->dev, data->bounce, align_size);
467 mutex_unlock(data, BOUNCE_MTX);
472 if (actual_w != align_size)
483 mutex_unlock(data, BOUNCE_MTX);
485 if (channel->write_error)
486 retval = (channel->write_error)(channel, block, count, buf,
487 size, actual, retval);
493 * Here we implement the cache functions
496 /* Allocate the cache buffers */
497 static errcode_t alloc_cache(io_channel channel,
498 struct unix_private_data *data)
501 struct unix_cache *cache;
504 data->access_time = 0;
505 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
507 cache->access_time = 0;
511 ext2fs_free_mem(&cache->buf);
512 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
516 if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
518 ext2fs_free_mem(&data->bounce);
519 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
524 /* Free the cache buffers */
525 static void free_cache(struct unix_private_data *data)
527 struct unix_cache *cache;
530 data->access_time = 0;
531 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
533 cache->access_time = 0;
537 ext2fs_free_mem(&cache->buf);
540 ext2fs_free_mem(&data->bounce);
545 * Try to find a block in the cache. If the block is not found, and
546 * eldest is a non-zero pointer, then fill in eldest with the cache
547 * entry to that should be reused.
549 static struct unix_cache *find_cached_block(struct unix_private_data *data,
550 unsigned long long block,
551 struct unix_cache **eldest)
553 struct unix_cache *cache, *unused_cache, *oldest_cache;
556 unused_cache = oldest_cache = 0;
557 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
558 if (!cache->in_use) {
560 unused_cache = cache;
563 if (cache->block == block) {
564 cache->access_time = ++data->access_time;
568 (cache->access_time < oldest_cache->access_time))
569 oldest_cache = cache;
572 *eldest = (unused_cache) ? unused_cache : oldest_cache;
577 * Reuse a particular cache entry for another block.
579 static void reuse_cache(io_channel channel, struct unix_private_data *data,
580 struct unix_cache *cache, unsigned long long block)
582 if (cache->dirty && cache->in_use)
583 raw_write_blk(channel, data, cache->block, 1, cache->buf);
587 cache->block = block;
588 cache->access_time = ++data->access_time;
591 #define FLUSH_INVALIDATE 0x01
592 #define FLUSH_NOLOCK 0x02
595 * Flush all of the blocks in the cache
597 static errcode_t flush_cached_blocks(io_channel channel,
598 struct unix_private_data *data,
601 struct unix_cache *cache;
602 errcode_t retval, retval2;
606 if ((flags & FLUSH_NOLOCK) == 0)
607 mutex_lock(data, CACHE_MTX);
608 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
612 if (flags & FLUSH_INVALIDATE)
618 retval = raw_write_blk(channel, data,
619 cache->block, 1, cache->buf);
625 if ((flags & FLUSH_NOLOCK) == 0)
626 mutex_unlock(data, CACHE_MTX);
629 #endif /* NO_IO_CACHE */
632 #ifndef BLKDISCARDZEROES
633 #define BLKDISCARDZEROES _IO(0x12,124)
637 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
640 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
641 return open64(pathname, flags, mode);
643 return open64(pathname, flags);
645 return open(pathname, flags, mode);
647 return open(pathname, flags);
651 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
653 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
654 return stat64(path, buf);
656 return stat(path, buf);
660 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
662 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
663 return fstat64(fd, buf);
665 return fstat(fd, buf);
670 static errcode_t unix_open_channel(const char *name, int fd,
671 int flags, io_channel *channel,
674 io_channel io = NULL;
675 struct unix_private_data *data = NULL;
677 ext2fs_struct_stat st;
682 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
683 flags |= IO_FLAG_FORCE_BOUNCE;
687 * We need to make sure any previous errors in the block
688 * device are thrown away, sigh.
693 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
696 memset(io, 0, sizeof(struct struct_io_channel));
697 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
698 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
702 io->manager = io_mgr;
703 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
707 strcpy(io->name, name);
708 io->private_data = data;
709 io->block_size = 1024;
715 memset(data, 0, sizeof(struct unix_private_data));
716 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
717 data->io_stats.num_fields = 2;
721 #if defined(O_DIRECT)
722 if (flags & IO_FLAG_DIRECT_IO)
723 io->align = ext2fs_get_dio_alignment(data->dev);
724 #elif defined(F_NOCACHE)
725 if (flags & IO_FLAG_DIRECT_IO)
730 * If the device is really a block device, then set the
731 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
732 * because we are going to use punch hole instead of discard
733 * and if it succeed, subsequent read from sparse area returns
736 if (ext2fs_fstat(data->dev, &st) == 0) {
737 if (ext2fsP_is_disk_device(st.st_mode))
738 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
740 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
743 #ifdef BLKDISCARDZEROES
746 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
748 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
752 #if defined(__CYGWIN__)
754 * Some operating systems require that the buffers be aligned,
755 * regardless of O_DIRECT
761 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
762 if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
763 int dio_align = ext2fs_get_dio_alignment(fd);
765 if (io->align < dio_align)
766 io->align = dio_align;
770 if ((retval = alloc_cache(io, data)))
774 if (flags & IO_FLAG_RW) {
778 /* Is the block device actually writable? */
779 error = ioctl(data->dev, BLKROGET, &readonly);
780 if (!error && readonly) {
789 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
790 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
792 #define RLIM_INFINITY (~0UL)
795 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
796 * block devices are wrongly getting hit by the filesize
797 * limit. This workaround isn't perfect, since it won't work
798 * if glibc wasn't built against 2.2 header files. (Sigh.)
801 if ((flags & IO_FLAG_RW) &&
803 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
804 (ut.release[2] == '4') && (ut.release[3] == '.') &&
805 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
806 (ut.release[5] < '8')) &&
807 (ext2fs_fstat(data->dev, &st) == 0) &&
808 (ext2fsP_is_disk_device(st.st_mode))) {
811 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
812 setrlimit(RLIMIT_FSIZE, &rlim);
813 getrlimit(RLIMIT_FSIZE, &rlim);
814 if (((unsigned long) rlim.rlim_cur) <
815 ((unsigned long) rlim.rlim_max)) {
816 rlim.rlim_cur = rlim.rlim_max;
817 setrlimit(RLIMIT_FSIZE, &rlim);
822 if (flags & IO_FLAG_THREADS) {
823 io->flags |= CHANNEL_FLAGS_THREADS;
824 retval = pthread_mutex_init(&data->cache_mutex, NULL);
827 retval = pthread_mutex_init(&data->bounce_mutex, NULL);
829 pthread_mutex_destroy(&data->cache_mutex);
832 retval = pthread_mutex_init(&data->stats_mutex, NULL);
834 pthread_mutex_destroy(&data->cache_mutex);
835 pthread_mutex_destroy(&data->bounce_mutex);
848 ext2fs_free_mem(&data);
852 ext2fs_free_mem(&io->name);
854 ext2fs_free_mem(&io);
859 static errcode_t unixfd_open(const char *str_fd, int flags,
866 #if defined(HAVE_FCNTL)
867 fd_flags = fcntl(fd, F_GETFD);
872 if (fd_flags & O_RDWR)
874 if (fd_flags & O_EXCL)
875 flags |= IO_FLAG_EXCLUSIVE;
876 #if defined(O_DIRECT)
877 if (fd_flags & O_DIRECT)
878 flags |= IO_FLAG_DIRECT_IO;
880 #endif /* HAVE_FCNTL */
882 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
885 static errcode_t unix_open(const char *name, int flags,
892 return EXT2_ET_BAD_DEVICE_NAME;
894 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
895 if (flags & IO_FLAG_EXCLUSIVE)
896 open_flags |= O_EXCL;
897 #if defined(O_DIRECT)
898 if (flags & IO_FLAG_DIRECT_IO)
899 open_flags |= O_DIRECT;
901 fd = ext2fs_open_file(name, open_flags, 0);
904 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
905 if (flags & IO_FLAG_DIRECT_IO) {
906 if (fcntl(fd, F_NOCACHE, 1) < 0)
910 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
913 static errcode_t unix_close(io_channel channel)
915 struct unix_private_data *data;
916 errcode_t retval = 0;
918 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
919 data = (struct unix_private_data *) channel->private_data;
920 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
922 if (--channel->refcount > 0)
926 retval = flush_cached_blocks(channel, data, 0);
929 if (close(data->dev) < 0)
933 if (data->flags & IO_FLAG_THREADS) {
934 pthread_mutex_destroy(&data->cache_mutex);
935 pthread_mutex_destroy(&data->bounce_mutex);
936 pthread_mutex_destroy(&data->stats_mutex);
940 ext2fs_free_mem(&channel->private_data);
942 ext2fs_free_mem(&channel->name);
943 ext2fs_free_mem(&channel);
947 static errcode_t unix_set_blksize(io_channel channel, int blksize)
949 struct unix_private_data *data;
950 errcode_t retval = 0;
952 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
953 data = (struct unix_private_data *) channel->private_data;
954 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
956 if (channel->block_size != blksize) {
957 mutex_lock(data, CACHE_MTX);
958 mutex_lock(data, BOUNCE_MTX);
960 if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK))){
961 mutex_unlock(data, BOUNCE_MTX);
962 mutex_unlock(data, CACHE_MTX);
967 channel->block_size = blksize;
969 retval = alloc_cache(channel, data);
970 mutex_unlock(data, BOUNCE_MTX);
971 mutex_unlock(data, CACHE_MTX);
976 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
977 int count, void *buf)
979 struct unix_private_data *data;
980 struct unix_cache *cache;
985 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
986 data = (struct unix_private_data *) channel->private_data;
987 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
990 return raw_read_blk(channel, data, block, count, buf);
992 if (data->flags & IO_FLAG_NOCACHE)
993 return raw_read_blk(channel, data, block, count, buf);
995 * If we're doing an odd-sized read or a very large read,
996 * flush out the cache and then do a direct read.
998 if (count < 0 || count > WRITE_DIRECT_SIZE) {
999 if ((retval = flush_cached_blocks(channel, data, 0)))
1001 return raw_read_blk(channel, data, block, count, buf);
1005 mutex_lock(data, CACHE_MTX);
1007 /* If it's in the cache, use it! */
1008 if ((cache = find_cached_block(data, block, NULL))) {
1010 printf("Using cached block %lu\n", block);
1012 memcpy(cp, cache->buf, channel->block_size);
1015 cp += channel->block_size;
1020 * Find the number of uncached blocks so we can do a
1021 * single read request
1023 for (i=1; i < count; i++)
1024 if (find_cached_block(data, block+i, NULL))
1027 printf("Reading %d blocks starting at %lu\n", i, block);
1029 mutex_unlock(data, CACHE_MTX);
1030 if ((retval = raw_read_blk(channel, data, block, i, cp)))
1032 mutex_lock(data, CACHE_MTX);
1034 /* Save the results in the cache */
1035 for (j=0; j < i; j++) {
1036 if (!find_cached_block(data, block, &cache)) {
1037 reuse_cache(channel, data, cache, block);
1038 memcpy(cache->buf, cp, channel->block_size);
1042 cp += channel->block_size;
1045 mutex_unlock(data, CACHE_MTX);
1047 #endif /* NO_IO_CACHE */
1050 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
1051 int count, void *buf)
1053 return unix_read_blk64(channel, block, count, buf);
1056 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
1057 int count, const void *buf)
1059 struct unix_private_data *data;
1060 struct unix_cache *cache, *reuse;
1061 errcode_t retval = 0;
1065 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1066 data = (struct unix_private_data *) channel->private_data;
1067 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1070 return raw_write_blk(channel, data, block, count, buf);
1072 if (data->flags & IO_FLAG_NOCACHE)
1073 return raw_write_blk(channel, data, block, count, buf);
1075 * If we're doing an odd-sized write or a very large write,
1076 * flush out the cache completely and then do a direct write.
1078 if (count < 0 || count > WRITE_DIRECT_SIZE) {
1079 if ((retval = flush_cached_blocks(channel, data,
1082 return raw_write_blk(channel, data, block, count, buf);
1086 * For a moderate-sized multi-block write, first force a write
1087 * if we're in write-through cache mode, and then fill the
1088 * cache with the blocks.
1090 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
1092 retval = raw_write_blk(channel, data, block, count, buf);
1095 mutex_lock(data, CACHE_MTX);
1097 cache = find_cached_block(data, block, &reuse);
1100 reuse_cache(channel, data, cache, block);
1102 if (cache->buf != cp)
1103 memcpy(cache->buf, cp, channel->block_size);
1104 cache->dirty = !writethrough;
1107 cp += channel->block_size;
1109 mutex_unlock(data, CACHE_MTX);
1111 #endif /* NO_IO_CACHE */
1114 static errcode_t unix_cache_readahead(io_channel channel,
1115 unsigned long long block,
1116 unsigned long long count)
1118 #ifdef POSIX_FADV_WILLNEED
1119 struct unix_private_data *data;
1121 data = (struct unix_private_data *)channel->private_data;
1122 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1123 return posix_fadvise(data->dev,
1124 (ext2_loff_t)block * channel->block_size + data->offset,
1125 (ext2_loff_t)count * channel->block_size,
1126 POSIX_FADV_WILLNEED);
1128 return EXT2_ET_OP_NOT_SUPPORTED;
1132 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
1133 int count, const void *buf)
1135 return unix_write_blk64(channel, block, count, buf);
1138 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
1139 int size, const void *buf)
1141 struct unix_private_data *data;
1142 errcode_t retval = 0;
1145 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1146 data = (struct unix_private_data *) channel->private_data;
1147 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1149 if (channel->align != 0) {
1151 printf("unix_write_byte: O_DIRECT fallback\n");
1153 return EXT2_ET_UNIMPLEMENTED;
1158 * Flush out the cache completely
1160 if ((retval = flush_cached_blocks(channel, data, FLUSH_INVALIDATE)))
1164 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
1167 actual = write(data->dev, buf, size);
1171 return EXT2_ET_SHORT_WRITE;
1177 * Flush data buffers to disk.
1179 static errcode_t unix_flush(io_channel channel)
1181 struct unix_private_data *data;
1182 errcode_t retval = 0;
1184 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1185 data = (struct unix_private_data *) channel->private_data;
1186 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1189 retval = flush_cached_blocks(channel, data, 0);
1192 if (!retval && fsync(data->dev) != 0)
1198 static errcode_t unix_set_option(io_channel channel, const char *option,
1201 struct unix_private_data *data;
1202 unsigned long long tmp;
1206 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1207 data = (struct unix_private_data *) channel->private_data;
1208 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1210 if (!strcmp(option, "offset")) {
1212 return EXT2_ET_INVALID_ARGUMENT;
1214 tmp = strtoull(arg, &end, 0);
1216 return EXT2_ET_INVALID_ARGUMENT;
1218 if (data->offset < 0)
1219 return EXT2_ET_INVALID_ARGUMENT;
1222 if (!strcmp(option, "cache")) {
1224 return EXT2_ET_INVALID_ARGUMENT;
1225 if (!strcmp(arg, "on")) {
1226 data->flags &= ~IO_FLAG_NOCACHE;
1229 if (!strcmp(arg, "off")) {
1230 retval = flush_cached_blocks(channel, data, 0);
1231 data->flags |= IO_FLAG_NOCACHE;
1234 return EXT2_ET_INVALID_ARGUMENT;
1236 return EXT2_ET_INVALID_ARGUMENT;
1239 #if defined(__linux__) && !defined(BLKDISCARD)
1240 #define BLKDISCARD _IO(0x12,119)
1243 static errcode_t unix_discard(io_channel channel, unsigned long long block,
1244 unsigned long long count)
1246 struct unix_private_data *data;
1249 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1250 data = (struct unix_private_data *) channel->private_data;
1251 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1253 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1257 range[0] = (__u64)(block) * channel->block_size + data->offset;
1258 range[1] = (__u64)(count) * channel->block_size;
1260 ret = ioctl(data->dev, BLKDISCARD, &range);
1265 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1267 * If we are not on block device, try to use punch hole
1268 * to reclaim free space.
1270 ret = fallocate(data->dev,
1271 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1272 (off_t)(block) * channel->block_size + data->offset,
1273 (off_t)(count) * channel->block_size);
1279 if (errno == EOPNOTSUPP)
1285 return EXT2_ET_UNIMPLEMENTED;
1289 * If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because
1290 * ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because
1291 * it always invalidates page cache, and libext2fs requires that reads after
1292 * ZERO_RANGE return zeroes.
1294 static int __unix_zeroout(int fd, off_t offset, off_t len)
1298 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
1299 ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, offset, len);
1303 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1304 ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1313 /* parameters might not be used if OS doesn't support zeroout */
1314 #if __GNUC_PREREQ (4, 6)
1315 #pragma GCC diagnostic push
1316 #pragma GCC diagnostic ignored "-Wunused-parameter"
1318 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1319 unsigned long long count)
1321 struct unix_private_data *data;
1324 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1325 data = (struct unix_private_data *) channel->private_data;
1326 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1328 if (safe_getenv("UNIX_IO_NOZEROOUT"))
1331 if (!(channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE)) {
1332 /* Regular file, try to use truncate/punch/zero. */
1333 struct stat statbuf;
1338 * If we're trying to zero a range past the end of the file,
1339 * extend the file size, then truncate everything.
1341 ret = fstat(data->dev, &statbuf);
1344 if ((unsigned long long) statbuf.st_size <
1345 (block + count) * channel->block_size + data->offset) {
1346 ret = ftruncate(data->dev,
1347 (block + count) * channel->block_size + data->offset);
1353 ret = __unix_zeroout(data->dev,
1354 (off_t)(block) * channel->block_size + data->offset,
1355 (off_t)(count) * channel->block_size);
1358 if (errno == EOPNOTSUPP)
1364 return EXT2_ET_UNIMPLEMENTED;
1366 #if __GNUC_PREREQ (4, 6)
1367 #pragma GCC diagnostic pop
1370 static struct struct_io_manager struct_unix_manager = {
1371 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1372 .name = "Unix I/O Manager",
1374 .close = unix_close,
1375 .set_blksize = unix_set_blksize,
1376 .read_blk = unix_read_blk,
1377 .write_blk = unix_write_blk,
1378 .flush = unix_flush,
1379 .write_byte = unix_write_byte,
1380 .set_option = unix_set_option,
1381 .get_stats = unix_get_stats,
1382 .read_blk64 = unix_read_blk64,
1383 .write_blk64 = unix_write_blk64,
1384 .discard = unix_discard,
1385 .cache_readahead = unix_cache_readahead,
1386 .zeroout = unix_zeroout,
1389 io_manager unix_io_manager = &struct_unix_manager;
1391 static struct struct_io_manager struct_unixfd_manager = {
1392 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1393 .name = "Unix fd I/O Manager",
1394 .open = unixfd_open,
1395 .close = unix_close,
1396 .set_blksize = unix_set_blksize,
1397 .read_blk = unix_read_blk,
1398 .write_blk = unix_write_blk,
1399 .flush = unix_flush,
1400 .write_byte = unix_write_byte,
1401 .set_option = unix_set_option,
1402 .get_stats = unix_get_stats,
1403 .read_blk64 = unix_read_blk64,
1404 .write_blk64 = unix_write_blk64,
1405 .discard = unix_discard,
1406 .cache_readahead = unix_cache_readahead,
1407 .zeroout = unix_zeroout,
1410 io_manager unixfd_io_manager = &struct_unixfd_manager;