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) {
231 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
232 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
239 /* Try an aligned pread */
240 if ((channel->align == 0) ||
241 (IS_ALIGNED(buf, channel->align) &&
242 IS_ALIGNED(location, channel->align) &&
243 IS_ALIGNED(size, channel->align))) {
244 actual = pread64(data->dev, buf, size, location);
250 /* Try an aligned pread */
251 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
252 ((channel->align == 0) ||
253 (IS_ALIGNED(buf, channel->align) &&
254 IS_ALIGNED(location, channel->align) &&
255 IS_ALIGNED(size, channel->align)))) {
256 actual = pread(data->dev, buf, size, location);
261 #endif /* HAVE_PREAD */
263 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
264 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
267 if ((channel->align == 0) ||
268 (IS_ALIGNED(buf, channel->align) &&
269 IS_ALIGNED(location, channel->align) &&
270 IS_ALIGNED(size, channel->align))) {
271 actual = read(data->dev, buf, size);
272 if (actual != size) {
278 retval = EXT2_ET_SHORT_READ;
285 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
286 (unsigned long) size);
290 * The buffer or size which we're trying to read isn't aligned
291 * to the O_DIRECT rules, so we need to do this the hard way...
294 if ((channel->block_size > channel->align) &&
295 (channel->block_size % channel->align) == 0)
296 align_size = channel->block_size;
298 align_size = channel->align;
299 aligned_blk = location / align_size;
300 offset = location % align_size;
302 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
303 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
307 mutex_lock(data, BOUNCE_MTX);
308 actual = read(data->dev, data->bounce, align_size);
309 if (actual != align_size) {
310 mutex_unlock(data, BOUNCE_MTX);
311 actual = really_read;
316 if ((actual + offset) > align_size)
317 actual = align_size - offset;
320 memcpy(buf, data->bounce + offset, actual);
322 really_read += actual;
327 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) {
368 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
369 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
376 /* Try an aligned pwrite */
377 if ((channel->align == 0) ||
378 (IS_ALIGNED(buf, channel->align) &&
379 IS_ALIGNED(location, channel->align) &&
380 IS_ALIGNED(size, channel->align))) {
381 actual = pwrite64(data->dev, buf, size, location);
386 /* Try an aligned pwrite */
387 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
388 ((channel->align == 0) ||
389 (IS_ALIGNED(buf, channel->align) &&
390 IS_ALIGNED(location, channel->align) &&
391 IS_ALIGNED(size, channel->align)))) {
392 actual = pwrite(data->dev, buf, size, location);
396 #endif /* HAVE_PWRITE */
398 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
399 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
403 if ((channel->align == 0) ||
404 (IS_ALIGNED(buf, channel->align) &&
405 IS_ALIGNED(location, channel->align) &&
406 IS_ALIGNED(size, channel->align))) {
407 actual = write(data->dev, buf, size);
412 if (actual != size) {
414 retval = EXT2_ET_SHORT_WRITE;
421 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
422 (unsigned long) size);
425 * The buffer or size which we're trying to write isn't aligned
426 * to the O_DIRECT rules, so we need to do this the hard way...
429 if ((channel->block_size > channel->align) &&
430 (channel->block_size % channel->align) == 0)
431 align_size = channel->block_size;
433 align_size = channel->align;
434 aligned_blk = location / align_size;
435 offset = location % align_size;
440 mutex_lock(data, BOUNCE_MTX);
441 if (size < align_size || offset) {
442 if (ext2fs_llseek(data->dev, aligned_blk * align_size,
444 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
447 actual = read(data->dev, data->bounce,
449 if (actual != align_size) {
451 mutex_unlock(data, BOUNCE_MTX);
455 memset((char *) data->bounce + actual, 0,
456 align_size - actual);
460 if ((actual + offset) > align_size)
461 actual = align_size - offset;
464 memcpy(((char *)data->bounce) + offset, buf, actual);
465 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
466 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
469 actual_w = write(data->dev, data->bounce, align_size);
470 mutex_unlock(data, BOUNCE_MTX);
475 if (actual_w != align_size)
486 if (channel->write_error)
487 retval = (channel->write_error)(channel, block, count, buf,
488 size, actual, retval);
494 * Here we implement the cache functions
497 /* Allocate the cache buffers */
498 static errcode_t alloc_cache(io_channel channel,
499 struct unix_private_data *data)
502 struct unix_cache *cache;
505 data->access_time = 0;
506 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
508 cache->access_time = 0;
512 ext2fs_free_mem(&cache->buf);
513 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
517 if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
519 ext2fs_free_mem(&data->bounce);
520 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
525 /* Free the cache buffers */
526 static void free_cache(struct unix_private_data *data)
528 struct unix_cache *cache;
531 data->access_time = 0;
532 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
534 cache->access_time = 0;
538 ext2fs_free_mem(&cache->buf);
541 ext2fs_free_mem(&data->bounce);
546 * Try to find a block in the cache. If the block is not found, and
547 * eldest is a non-zero pointer, then fill in eldest with the cache
548 * entry to that should be reused.
550 static struct unix_cache *find_cached_block(struct unix_private_data *data,
551 unsigned long long block,
552 struct unix_cache **eldest)
554 struct unix_cache *cache, *unused_cache, *oldest_cache;
557 unused_cache = oldest_cache = 0;
558 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
559 if (!cache->in_use) {
561 unused_cache = cache;
564 if (cache->block == block) {
565 cache->access_time = ++data->access_time;
569 (cache->access_time < oldest_cache->access_time))
570 oldest_cache = cache;
573 *eldest = (unused_cache) ? unused_cache : oldest_cache;
578 * Reuse a particular cache entry for another block.
580 static void reuse_cache(io_channel channel, struct unix_private_data *data,
581 struct unix_cache *cache, unsigned long long block)
583 if (cache->dirty && cache->in_use)
584 raw_write_blk(channel, data, cache->block, 1, cache->buf);
588 cache->block = block;
589 cache->access_time = ++data->access_time;
592 #define FLUSH_INVALIDATE 0x01
593 #define FLUSH_NOLOCK 0x02
596 * Flush all of the blocks in the cache
598 static errcode_t flush_cached_blocks(io_channel channel,
599 struct unix_private_data *data,
602 struct unix_cache *cache;
603 errcode_t retval, retval2;
607 if ((flags & FLUSH_NOLOCK) == 0)
608 mutex_lock(data, CACHE_MTX);
609 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
613 if (flags & FLUSH_INVALIDATE)
619 retval = raw_write_blk(channel, data,
620 cache->block, 1, cache->buf);
626 if ((flags & FLUSH_NOLOCK) == 0)
627 mutex_unlock(data, CACHE_MTX);
630 #endif /* NO_IO_CACHE */
633 #ifndef BLKDISCARDZEROES
634 #define BLKDISCARDZEROES _IO(0x12,124)
638 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
641 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
642 return open64(pathname, flags, mode);
644 return open64(pathname, flags);
646 return open(pathname, flags, mode);
648 return open(pathname, flags);
652 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
654 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
655 return stat64(path, buf);
657 return stat(path, buf);
661 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
663 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
664 return fstat64(fd, buf);
666 return fstat(fd, buf);
671 static errcode_t unix_open_channel(const char *name, int fd,
672 int flags, io_channel *channel,
675 io_channel io = NULL;
676 struct unix_private_data *data = NULL;
678 ext2fs_struct_stat st;
683 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
684 flags |= IO_FLAG_FORCE_BOUNCE;
688 * We need to make sure any previous errors in the block
689 * device are thrown away, sigh.
694 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
697 memset(io, 0, sizeof(struct struct_io_channel));
698 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
699 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
703 io->manager = io_mgr;
704 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
708 strcpy(io->name, name);
709 io->private_data = data;
710 io->block_size = 1024;
716 memset(data, 0, sizeof(struct unix_private_data));
717 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
718 data->io_stats.num_fields = 2;
722 #if defined(O_DIRECT)
723 if (flags & IO_FLAG_DIRECT_IO)
724 io->align = ext2fs_get_dio_alignment(data->dev);
725 #elif defined(F_NOCACHE)
726 if (flags & IO_FLAG_DIRECT_IO)
731 * If the device is really a block device, then set the
732 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
733 * because we are going to use punch hole instead of discard
734 * and if it succeed, subsequent read from sparse area returns
737 if (ext2fs_fstat(data->dev, &st) == 0) {
738 if (ext2fsP_is_disk_device(st.st_mode))
739 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
741 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
744 #ifdef BLKDISCARDZEROES
747 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
749 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
753 #if defined(__CYGWIN__)
755 * Some operating systems require that the buffers be aligned,
756 * regardless of O_DIRECT
762 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
763 if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
764 int dio_align = ext2fs_get_dio_alignment(fd);
766 if (io->align < dio_align)
767 io->align = dio_align;
771 if ((retval = alloc_cache(io, data)))
775 if (flags & IO_FLAG_RW) {
779 /* Is the block device actually writable? */
780 error = ioctl(data->dev, BLKROGET, &readonly);
781 if (!error && readonly) {
790 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
791 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
793 #define RLIM_INFINITY (~0UL)
796 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
797 * block devices are wrongly getting hit by the filesize
798 * limit. This workaround isn't perfect, since it won't work
799 * if glibc wasn't built against 2.2 header files. (Sigh.)
802 if ((flags & IO_FLAG_RW) &&
804 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
805 (ut.release[2] == '4') && (ut.release[3] == '.') &&
806 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
807 (ut.release[5] < '8')) &&
808 (ext2fs_fstat(data->dev, &st) == 0) &&
809 (ext2fsP_is_disk_device(st.st_mode))) {
812 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
813 setrlimit(RLIMIT_FSIZE, &rlim);
814 getrlimit(RLIMIT_FSIZE, &rlim);
815 if (((unsigned long) rlim.rlim_cur) <
816 ((unsigned long) rlim.rlim_max)) {
817 rlim.rlim_cur = rlim.rlim_max;
818 setrlimit(RLIMIT_FSIZE, &rlim);
823 if (flags & IO_FLAG_THREADS) {
824 io->flags |= CHANNEL_FLAGS_THREADS;
825 retval = pthread_mutex_init(&data->cache_mutex, NULL);
828 retval = pthread_mutex_init(&data->bounce_mutex, NULL);
830 pthread_mutex_destroy(&data->cache_mutex);
833 retval = pthread_mutex_init(&data->stats_mutex, NULL);
835 pthread_mutex_destroy(&data->cache_mutex);
836 pthread_mutex_destroy(&data->bounce_mutex);
849 ext2fs_free_mem(&data);
853 ext2fs_free_mem(&io->name);
855 ext2fs_free_mem(&io);
860 static errcode_t unixfd_open(const char *str_fd, int flags,
867 #if defined(HAVE_FCNTL)
868 fd_flags = fcntl(fd, F_GETFD);
873 if (fd_flags & O_RDWR)
875 if (fd_flags & O_EXCL)
876 flags |= IO_FLAG_EXCLUSIVE;
877 #if defined(O_DIRECT)
878 if (fd_flags & O_DIRECT)
879 flags |= IO_FLAG_DIRECT_IO;
881 #endif /* HAVE_FCNTL */
883 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
886 static errcode_t unix_open(const char *name, int flags,
893 return EXT2_ET_BAD_DEVICE_NAME;
895 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
896 if (flags & IO_FLAG_EXCLUSIVE)
897 open_flags |= O_EXCL;
898 #if defined(O_DIRECT)
899 if (flags & IO_FLAG_DIRECT_IO)
900 open_flags |= O_DIRECT;
902 fd = ext2fs_open_file(name, open_flags, 0);
905 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
906 if (flags & IO_FLAG_DIRECT_IO) {
907 if (fcntl(fd, F_NOCACHE, 1) < 0)
911 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
914 static errcode_t unix_close(io_channel channel)
916 struct unix_private_data *data;
917 errcode_t retval = 0;
919 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
920 data = (struct unix_private_data *) channel->private_data;
921 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
923 if (--channel->refcount > 0)
927 retval = flush_cached_blocks(channel, data, 0);
930 if (close(data->dev) < 0)
934 if (data->flags & IO_FLAG_THREADS) {
935 pthread_mutex_destroy(&data->cache_mutex);
936 pthread_mutex_destroy(&data->bounce_mutex);
937 pthread_mutex_destroy(&data->stats_mutex);
941 ext2fs_free_mem(&channel->private_data);
943 ext2fs_free_mem(&channel->name);
944 ext2fs_free_mem(&channel);
948 static errcode_t unix_set_blksize(io_channel channel, int blksize)
950 struct unix_private_data *data;
951 errcode_t retval = 0;
953 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
954 data = (struct unix_private_data *) channel->private_data;
955 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
957 if (channel->block_size != blksize) {
958 mutex_lock(data, CACHE_MTX);
959 mutex_lock(data, BOUNCE_MTX);
961 if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK)))
965 channel->block_size = blksize;
967 retval = alloc_cache(channel, data);
968 mutex_unlock(data, BOUNCE_MTX);
969 mutex_unlock(data, CACHE_MTX);
974 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
975 int count, void *buf)
977 struct unix_private_data *data;
978 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
979 errcode_t retval = 0;
983 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
984 data = (struct unix_private_data *) channel->private_data;
985 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
988 return raw_read_blk(channel, data, block, count, buf);
990 if (data->flags & IO_FLAG_NOCACHE)
991 return raw_read_blk(channel, data, block, count, buf);
993 * If we're doing an odd-sized read or a very large read,
994 * flush out the cache and then do a direct read.
996 if (count < 0 || count > WRITE_DIRECT_SIZE) {
997 if ((retval = flush_cached_blocks(channel, data, 0)))
999 return raw_read_blk(channel, data, block, count, buf);
1003 mutex_lock(data, CACHE_MTX);
1005 /* If it's in the cache, use it! */
1006 if ((cache = find_cached_block(data, block, &reuse[0]))) {
1008 printf("Using cached block %lu\n", block);
1010 memcpy(cp, cache->buf, channel->block_size);
1013 cp += channel->block_size;
1018 * Special case where we read directly into the
1019 * cache buffer; important in the O_DIRECT case
1022 reuse_cache(channel, data, cache, block);
1023 if ((retval = raw_read_blk(channel, data, block, 1,
1028 memcpy(cp, cache->buf, channel->block_size);
1034 * Find the number of uncached blocks so we can do a
1035 * single read request
1037 for (i=1; i < count; i++)
1038 if (find_cached_block(data, block+i, &reuse[i]))
1041 printf("Reading %d blocks starting at %lu\n", i, block);
1043 if ((retval = raw_read_blk(channel, data, block, i, cp)))
1046 /* Save the results in the cache */
1047 for (j=0; j < i; j++) {
1050 reuse_cache(channel, data, cache, block++);
1051 memcpy(cache->buf, cp, channel->block_size);
1052 cp += channel->block_size;
1055 mutex_unlock(data, CACHE_MTX);
1057 #endif /* NO_IO_CACHE */
1060 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
1061 int count, void *buf)
1063 return unix_read_blk64(channel, block, count, buf);
1066 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
1067 int count, const void *buf)
1069 struct unix_private_data *data;
1070 struct unix_cache *cache, *reuse;
1071 errcode_t retval = 0;
1075 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1076 data = (struct unix_private_data *) channel->private_data;
1077 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1080 return raw_write_blk(channel, data, block, count, buf);
1082 if (data->flags & IO_FLAG_NOCACHE)
1083 return raw_write_blk(channel, data, block, count, buf);
1085 * If we're doing an odd-sized write or a very large write,
1086 * flush out the cache completely and then do a direct write.
1088 if (count < 0 || count > WRITE_DIRECT_SIZE) {
1089 if ((retval = flush_cached_blocks(channel, data,
1092 return raw_write_blk(channel, data, block, count, buf);
1096 * For a moderate-sized multi-block write, first force a write
1097 * if we're in write-through cache mode, and then fill the
1098 * cache with the blocks.
1100 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
1102 retval = raw_write_blk(channel, data, block, count, buf);
1105 mutex_lock(data, CACHE_MTX);
1107 cache = find_cached_block(data, block, &reuse);
1110 reuse_cache(channel, data, cache, block);
1112 if (cache->buf != cp)
1113 memcpy(cache->buf, cp, channel->block_size);
1114 cache->dirty = !writethrough;
1117 cp += channel->block_size;
1119 mutex_unlock(data, CACHE_MTX);
1121 #endif /* NO_IO_CACHE */
1124 static errcode_t unix_cache_readahead(io_channel channel,
1125 unsigned long long block,
1126 unsigned long long count)
1128 #ifdef POSIX_FADV_WILLNEED
1129 struct unix_private_data *data;
1131 data = (struct unix_private_data *)channel->private_data;
1132 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1133 return posix_fadvise(data->dev,
1134 (ext2_loff_t)block * channel->block_size + data->offset,
1135 (ext2_loff_t)count * channel->block_size,
1136 POSIX_FADV_WILLNEED);
1138 return EXT2_ET_OP_NOT_SUPPORTED;
1142 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
1143 int count, const void *buf)
1145 return unix_write_blk64(channel, block, count, buf);
1148 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
1149 int size, const void *buf)
1151 struct unix_private_data *data;
1152 errcode_t retval = 0;
1155 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1156 data = (struct unix_private_data *) channel->private_data;
1157 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1159 if (channel->align != 0) {
1161 printf("unix_write_byte: O_DIRECT fallback\n");
1163 return EXT2_ET_UNIMPLEMENTED;
1168 * Flush out the cache completely
1170 if ((retval = flush_cached_blocks(channel, data, FLUSH_INVALIDATE)))
1174 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
1177 actual = write(data->dev, buf, size);
1181 return EXT2_ET_SHORT_WRITE;
1187 * Flush data buffers to disk.
1189 static errcode_t unix_flush(io_channel channel)
1191 struct unix_private_data *data;
1192 errcode_t retval = 0;
1194 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1195 data = (struct unix_private_data *) channel->private_data;
1196 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1199 retval = flush_cached_blocks(channel, data, 0);
1202 if (!retval && fsync(data->dev) != 0)
1208 static errcode_t unix_set_option(io_channel channel, const char *option,
1211 struct unix_private_data *data;
1212 unsigned long long tmp;
1216 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1217 data = (struct unix_private_data *) channel->private_data;
1218 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1220 if (!strcmp(option, "offset")) {
1222 return EXT2_ET_INVALID_ARGUMENT;
1224 tmp = strtoull(arg, &end, 0);
1226 return EXT2_ET_INVALID_ARGUMENT;
1228 if (data->offset < 0)
1229 return EXT2_ET_INVALID_ARGUMENT;
1232 if (!strcmp(option, "cache")) {
1234 return EXT2_ET_INVALID_ARGUMENT;
1235 if (!strcmp(arg, "on")) {
1236 data->flags &= ~IO_FLAG_NOCACHE;
1239 if (!strcmp(arg, "off")) {
1240 retval = flush_cached_blocks(channel, data, 0);
1241 data->flags |= IO_FLAG_NOCACHE;
1244 return EXT2_ET_INVALID_ARGUMENT;
1246 return EXT2_ET_INVALID_ARGUMENT;
1249 #if defined(__linux__) && !defined(BLKDISCARD)
1250 #define BLKDISCARD _IO(0x12,119)
1253 static errcode_t unix_discard(io_channel channel, unsigned long long block,
1254 unsigned long long count)
1256 struct unix_private_data *data;
1259 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1260 data = (struct unix_private_data *) channel->private_data;
1261 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1263 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1267 range[0] = (__u64)(block) * channel->block_size + data->offset;
1268 range[1] = (__u64)(count) * channel->block_size;
1270 ret = ioctl(data->dev, BLKDISCARD, &range);
1275 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1277 * If we are not on block device, try to use punch hole
1278 * to reclaim free space.
1280 ret = fallocate(data->dev,
1281 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1282 (off_t)(block) * channel->block_size + data->offset,
1283 (off_t)(count) * channel->block_size);
1289 if (errno == EOPNOTSUPP)
1295 return EXT2_ET_UNIMPLEMENTED;
1299 * If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because
1300 * ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because
1301 * it always invalidates page cache, and libext2fs requires that reads after
1302 * ZERO_RANGE return zeroes.
1304 static int __unix_zeroout(int fd, off_t offset, off_t len)
1308 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
1309 ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, offset, len);
1313 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1314 ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1323 /* parameters might not be used if OS doesn't support zeroout */
1324 #if __GNUC_PREREQ (4, 6)
1325 #pragma GCC diagnostic push
1326 #pragma GCC diagnostic ignored "-Wunused-parameter"
1328 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1329 unsigned long long count)
1331 struct unix_private_data *data;
1334 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1335 data = (struct unix_private_data *) channel->private_data;
1336 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1338 if (safe_getenv("UNIX_IO_NOZEROOUT"))
1341 if (!(channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE)) {
1342 /* Regular file, try to use truncate/punch/zero. */
1343 struct stat statbuf;
1348 * If we're trying to zero a range past the end of the file,
1349 * extend the file size, then truncate everything.
1351 ret = fstat(data->dev, &statbuf);
1354 if ((unsigned long long) statbuf.st_size <
1355 (block + count) * channel->block_size + data->offset) {
1356 ret = ftruncate(data->dev,
1357 (block + count) * channel->block_size + data->offset);
1363 ret = __unix_zeroout(data->dev,
1364 (off_t)(block) * channel->block_size + data->offset,
1365 (off_t)(count) * channel->block_size);
1368 if (errno == EOPNOTSUPP)
1374 return EXT2_ET_UNIMPLEMENTED;
1376 #if __GNUC_PREREQ (4, 6)
1377 #pragma GCC diagnostic pop
1380 static struct struct_io_manager struct_unix_manager = {
1381 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1382 .name = "Unix I/O Manager",
1384 .close = unix_close,
1385 .set_blksize = unix_set_blksize,
1386 .read_blk = unix_read_blk,
1387 .write_blk = unix_write_blk,
1388 .flush = unix_flush,
1389 .write_byte = unix_write_byte,
1390 .set_option = unix_set_option,
1391 .get_stats = unix_get_stats,
1392 .read_blk64 = unix_read_blk64,
1393 .write_blk64 = unix_write_blk64,
1394 .discard = unix_discard,
1395 .cache_readahead = unix_cache_readahead,
1396 .zeroout = unix_zeroout,
1399 io_manager unix_io_manager = &struct_unix_manager;
1401 static struct struct_io_manager struct_unixfd_manager = {
1402 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1403 .name = "Unix fd I/O Manager",
1404 .open = unixfd_open,
1405 .close = unix_close,
1406 .set_blksize = unix_set_blksize,
1407 .read_blk = unix_read_blk,
1408 .write_blk = unix_write_blk,
1409 .flush = unix_flush,
1410 .write_byte = unix_write_byte,
1411 .set_option = unix_set_option,
1412 .get_stats = unix_get_stats,
1413 .read_blk64 = unix_read_blk64,
1414 .write_blk64 = unix_write_blk64,
1415 .discard = unix_discard,
1416 .cache_readahead = unix_cache_readahead,
1417 .zeroout = unix_zeroout,
1420 io_manager unixfd_io_manager = &struct_unixfd_manager;