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 (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
259 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
262 if ((channel->align == 0) ||
263 (IS_ALIGNED(buf, channel->align) &&
264 IS_ALIGNED(location, channel->align) &&
265 IS_ALIGNED(size, channel->align))) {
266 actual = read(data->dev, buf, size);
267 if (actual != size) {
273 retval = EXT2_ET_SHORT_READ;
280 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
281 (unsigned long) size);
285 * The buffer or size which we're trying to read isn't aligned
286 * to the O_DIRECT rules, so we need to do this the hard way...
289 if (channel->align == 0)
291 if ((channel->block_size > channel->align) &&
292 (channel->block_size % channel->align) == 0)
293 align_size = channel->block_size;
295 align_size = channel->align;
296 aligned_blk = location / align_size;
297 offset = location % align_size;
299 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
300 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
304 mutex_lock(data, BOUNCE_MTX);
305 actual = read(data->dev, data->bounce, align_size);
306 if (actual != align_size) {
307 mutex_unlock(data, BOUNCE_MTX);
308 actual = really_read;
313 if ((actual + offset) > align_size)
314 actual = align_size - offset;
317 memcpy(buf, data->bounce + offset, actual);
319 really_read += actual;
324 mutex_unlock(data, BOUNCE_MTX);
329 if (actual >= 0 && actual < size)
330 memset((char *) buf+actual, 0, size-actual);
331 if (channel->read_error)
332 retval = (channel->read_error)(channel, block, count, buf,
333 size, actual, retval);
337 static errcode_t raw_write_blk(io_channel channel,
338 struct unix_private_data *data,
339 unsigned long long block,
340 int count, const void *bufv)
343 ext2_loff_t location;
346 const unsigned char *buf = bufv;
347 unsigned long long aligned_blk;
348 int align_size, offset;
351 size = channel->block_size;
356 size = (ext2_loff_t) count * channel->block_size;
358 mutex_lock(data, STATS_MTX);
359 data->io_stats.bytes_written += size;
360 mutex_unlock(data, STATS_MTX);
362 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
364 if (data->flags & IO_FLAG_FORCE_BOUNCE)
368 /* Try an aligned pwrite */
369 if ((channel->align == 0) ||
370 (IS_ALIGNED(buf, channel->align) &&
371 IS_ALIGNED(location, channel->align) &&
372 IS_ALIGNED(size, channel->align))) {
373 actual = pwrite64(data->dev, buf, size, location);
378 /* Try an aligned pwrite */
379 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
380 ((channel->align == 0) ||
381 (IS_ALIGNED(buf, channel->align) &&
382 IS_ALIGNED(location, channel->align) &&
383 IS_ALIGNED(size, channel->align)))) {
384 actual = pwrite(data->dev, buf, size, location);
388 #endif /* HAVE_PWRITE */
390 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
391 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
395 if ((channel->align == 0) ||
396 (IS_ALIGNED(buf, channel->align) &&
397 IS_ALIGNED(location, channel->align) &&
398 IS_ALIGNED(size, channel->align))) {
399 actual = write(data->dev, buf, size);
404 if (actual != size) {
406 retval = EXT2_ET_SHORT_WRITE;
413 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
414 (unsigned long) size);
417 * The buffer or size which we're trying to write isn't aligned
418 * to the O_DIRECT rules, so we need to do this the hard way...
421 if (channel->align == 0)
423 if ((channel->block_size > channel->align) &&
424 (channel->block_size % channel->align) == 0)
425 align_size = channel->block_size;
427 align_size = channel->align;
428 aligned_blk = location / align_size;
429 offset = location % align_size;
434 mutex_lock(data, BOUNCE_MTX);
435 if (size < align_size || offset) {
436 if (ext2fs_llseek(data->dev, aligned_blk * align_size,
438 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
441 actual = read(data->dev, data->bounce,
443 if (actual != align_size) {
445 mutex_unlock(data, BOUNCE_MTX);
449 memset((char *) data->bounce + actual, 0,
450 align_size - actual);
454 if ((actual + offset) > align_size)
455 actual = align_size - offset;
458 memcpy(((char *)data->bounce) + offset, buf, actual);
459 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
460 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
463 actual_w = write(data->dev, data->bounce, align_size);
464 mutex_unlock(data, BOUNCE_MTX);
469 if (actual_w != align_size)
480 if (channel->write_error)
481 retval = (channel->write_error)(channel, block, count, buf,
482 size, actual, retval);
488 * Here we implement the cache functions
491 /* Allocate the cache buffers */
492 static errcode_t alloc_cache(io_channel channel,
493 struct unix_private_data *data)
496 struct unix_cache *cache;
499 data->access_time = 0;
500 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
502 cache->access_time = 0;
506 ext2fs_free_mem(&cache->buf);
507 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
511 if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
513 ext2fs_free_mem(&data->bounce);
514 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
519 /* Free the cache buffers */
520 static void free_cache(struct unix_private_data *data)
522 struct unix_cache *cache;
525 data->access_time = 0;
526 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
528 cache->access_time = 0;
532 ext2fs_free_mem(&cache->buf);
535 ext2fs_free_mem(&data->bounce);
540 * Try to find a block in the cache. If the block is not found, and
541 * eldest is a non-zero pointer, then fill in eldest with the cache
542 * entry to that should be reused.
544 static struct unix_cache *find_cached_block(struct unix_private_data *data,
545 unsigned long long block,
546 struct unix_cache **eldest)
548 struct unix_cache *cache, *unused_cache, *oldest_cache;
551 unused_cache = oldest_cache = 0;
552 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
553 if (!cache->in_use) {
555 unused_cache = cache;
558 if (cache->block == block) {
559 cache->access_time = ++data->access_time;
563 (cache->access_time < oldest_cache->access_time))
564 oldest_cache = cache;
567 *eldest = (unused_cache) ? unused_cache : oldest_cache;
572 * Reuse a particular cache entry for another block.
574 static void reuse_cache(io_channel channel, struct unix_private_data *data,
575 struct unix_cache *cache, unsigned long long block)
577 if (cache->dirty && cache->in_use)
578 raw_write_blk(channel, data, cache->block, 1, cache->buf);
582 cache->block = block;
583 cache->access_time = ++data->access_time;
586 #define FLUSH_INVALIDATE 0x01
587 #define FLUSH_NOLOCK 0x02
590 * Flush all of the blocks in the cache
592 static errcode_t flush_cached_blocks(io_channel channel,
593 struct unix_private_data *data,
596 struct unix_cache *cache;
597 errcode_t retval, retval2;
601 if ((flags & FLUSH_NOLOCK) == 0)
602 mutex_lock(data, CACHE_MTX);
603 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
607 if (flags & FLUSH_INVALIDATE)
613 retval = raw_write_blk(channel, data,
614 cache->block, 1, cache->buf);
620 if ((flags & FLUSH_NOLOCK) == 0)
621 mutex_unlock(data, CACHE_MTX);
624 #endif /* NO_IO_CACHE */
627 #ifndef BLKDISCARDZEROES
628 #define BLKDISCARDZEROES _IO(0x12,124)
632 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
635 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
636 return open64(pathname, flags, mode);
638 return open64(pathname, flags);
640 return open(pathname, flags, mode);
642 return open(pathname, flags);
646 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
648 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
649 return stat64(path, buf);
651 return stat(path, buf);
655 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
657 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
658 return fstat64(fd, buf);
660 return fstat(fd, buf);
665 static errcode_t unix_open_channel(const char *name, int fd,
666 int flags, io_channel *channel,
669 io_channel io = NULL;
670 struct unix_private_data *data = NULL;
672 ext2fs_struct_stat st;
677 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
678 flags |= IO_FLAG_FORCE_BOUNCE;
682 * We need to make sure any previous errors in the block
683 * device are thrown away, sigh.
688 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
691 memset(io, 0, sizeof(struct struct_io_channel));
692 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
693 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
697 io->manager = io_mgr;
698 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
702 strcpy(io->name, name);
703 io->private_data = data;
704 io->block_size = 1024;
710 memset(data, 0, sizeof(struct unix_private_data));
711 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
712 data->io_stats.num_fields = 2;
716 #if defined(O_DIRECT)
717 if (flags & IO_FLAG_DIRECT_IO)
718 io->align = ext2fs_get_dio_alignment(data->dev);
719 #elif defined(F_NOCACHE)
720 if (flags & IO_FLAG_DIRECT_IO)
725 * If the device is really a block device, then set the
726 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
727 * because we are going to use punch hole instead of discard
728 * and if it succeed, subsequent read from sparse area returns
731 if (ext2fs_fstat(data->dev, &st) == 0) {
732 if (ext2fsP_is_disk_device(st.st_mode))
733 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
735 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
738 #ifdef BLKDISCARDZEROES
741 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
743 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
747 #if defined(__CYGWIN__)
749 * Some operating systems require that the buffers be aligned,
750 * regardless of O_DIRECT
756 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
757 if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
758 int dio_align = ext2fs_get_dio_alignment(fd);
760 if (io->align < dio_align)
761 io->align = dio_align;
765 if ((retval = alloc_cache(io, data)))
769 if (flags & IO_FLAG_RW) {
773 /* Is the block device actually writable? */
774 error = ioctl(data->dev, BLKROGET, &readonly);
775 if (!error && readonly) {
784 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
785 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
787 #define RLIM_INFINITY (~0UL)
790 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
791 * block devices are wrongly getting hit by the filesize
792 * limit. This workaround isn't perfect, since it won't work
793 * if glibc wasn't built against 2.2 header files. (Sigh.)
796 if ((flags & IO_FLAG_RW) &&
798 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
799 (ut.release[2] == '4') && (ut.release[3] == '.') &&
800 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
801 (ut.release[5] < '8')) &&
802 (ext2fs_fstat(data->dev, &st) == 0) &&
803 (ext2fsP_is_disk_device(st.st_mode))) {
806 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
807 setrlimit(RLIMIT_FSIZE, &rlim);
808 getrlimit(RLIMIT_FSIZE, &rlim);
809 if (((unsigned long) rlim.rlim_cur) <
810 ((unsigned long) rlim.rlim_max)) {
811 rlim.rlim_cur = rlim.rlim_max;
812 setrlimit(RLIMIT_FSIZE, &rlim);
817 if (flags & IO_FLAG_THREADS) {
818 io->flags |= CHANNEL_FLAGS_THREADS;
819 retval = pthread_mutex_init(&data->cache_mutex, NULL);
822 retval = pthread_mutex_init(&data->bounce_mutex, NULL);
824 pthread_mutex_destroy(&data->cache_mutex);
827 retval = pthread_mutex_init(&data->stats_mutex, NULL);
829 pthread_mutex_destroy(&data->cache_mutex);
830 pthread_mutex_destroy(&data->bounce_mutex);
843 ext2fs_free_mem(&data);
847 ext2fs_free_mem(&io->name);
849 ext2fs_free_mem(&io);
854 static errcode_t unixfd_open(const char *str_fd, int flags,
861 #if defined(HAVE_FCNTL)
862 fd_flags = fcntl(fd, F_GETFD);
867 if (fd_flags & O_RDWR)
869 if (fd_flags & O_EXCL)
870 flags |= IO_FLAG_EXCLUSIVE;
871 #if defined(O_DIRECT)
872 if (fd_flags & O_DIRECT)
873 flags |= IO_FLAG_DIRECT_IO;
875 #endif /* HAVE_FCNTL */
877 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
880 static errcode_t unix_open(const char *name, int flags,
887 return EXT2_ET_BAD_DEVICE_NAME;
889 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
890 if (flags & IO_FLAG_EXCLUSIVE)
891 open_flags |= O_EXCL;
892 #if defined(O_DIRECT)
893 if (flags & IO_FLAG_DIRECT_IO)
894 open_flags |= O_DIRECT;
896 fd = ext2fs_open_file(name, open_flags, 0);
899 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
900 if (flags & IO_FLAG_DIRECT_IO) {
901 if (fcntl(fd, F_NOCACHE, 1) < 0)
905 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
908 static errcode_t unix_close(io_channel channel)
910 struct unix_private_data *data;
911 errcode_t retval = 0;
913 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
914 data = (struct unix_private_data *) channel->private_data;
915 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
917 if (--channel->refcount > 0)
921 retval = flush_cached_blocks(channel, data, 0);
924 if (close(data->dev) < 0)
928 if (data->flags & IO_FLAG_THREADS) {
929 pthread_mutex_destroy(&data->cache_mutex);
930 pthread_mutex_destroy(&data->bounce_mutex);
931 pthread_mutex_destroy(&data->stats_mutex);
935 ext2fs_free_mem(&channel->private_data);
937 ext2fs_free_mem(&channel->name);
938 ext2fs_free_mem(&channel);
942 static errcode_t unix_set_blksize(io_channel channel, int blksize)
944 struct unix_private_data *data;
945 errcode_t retval = 0;
947 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
948 data = (struct unix_private_data *) channel->private_data;
949 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
951 if (channel->block_size != blksize) {
952 mutex_lock(data, CACHE_MTX);
953 mutex_lock(data, BOUNCE_MTX);
955 if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK)))
959 channel->block_size = blksize;
961 retval = alloc_cache(channel, data);
962 mutex_unlock(data, BOUNCE_MTX);
963 mutex_unlock(data, CACHE_MTX);
968 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
969 int count, void *buf)
971 struct unix_private_data *data;
972 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
973 errcode_t retval = 0;
977 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
978 data = (struct unix_private_data *) channel->private_data;
979 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
982 return raw_read_blk(channel, data, block, count, buf);
984 if (data->flags & IO_FLAG_NOCACHE)
985 return raw_read_blk(channel, data, block, count, buf);
987 * If we're doing an odd-sized read or a very large read,
988 * flush out the cache and then do a direct read.
990 if (count < 0 || count > WRITE_DIRECT_SIZE) {
991 if ((retval = flush_cached_blocks(channel, data, 0)))
993 return raw_read_blk(channel, data, block, count, buf);
997 mutex_lock(data, CACHE_MTX);
999 /* If it's in the cache, use it! */
1000 if ((cache = find_cached_block(data, block, &reuse[0]))) {
1002 printf("Using cached block %lu\n", block);
1004 memcpy(cp, cache->buf, channel->block_size);
1007 cp += channel->block_size;
1012 * Special case where we read directly into the
1013 * cache buffer; important in the O_DIRECT case
1016 reuse_cache(channel, data, cache, block);
1017 if ((retval = raw_read_blk(channel, data, block, 1,
1022 memcpy(cp, cache->buf, channel->block_size);
1028 * Find the number of uncached blocks so we can do a
1029 * single read request
1031 for (i=1; i < count; i++)
1032 if (find_cached_block(data, block+i, &reuse[i]))
1035 printf("Reading %d blocks starting at %lu\n", i, block);
1037 if ((retval = raw_read_blk(channel, data, block, i, cp)))
1040 /* Save the results in the cache */
1041 for (j=0; j < i; j++) {
1044 reuse_cache(channel, data, cache, block++);
1045 memcpy(cache->buf, cp, channel->block_size);
1046 cp += channel->block_size;
1049 mutex_unlock(data, CACHE_MTX);
1051 #endif /* NO_IO_CACHE */
1054 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
1055 int count, void *buf)
1057 return unix_read_blk64(channel, block, count, buf);
1060 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
1061 int count, const void *buf)
1063 struct unix_private_data *data;
1064 struct unix_cache *cache, *reuse;
1065 errcode_t retval = 0;
1069 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1070 data = (struct unix_private_data *) channel->private_data;
1071 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1074 return raw_write_blk(channel, data, block, count, buf);
1076 if (data->flags & IO_FLAG_NOCACHE)
1077 return raw_write_blk(channel, data, block, count, buf);
1079 * If we're doing an odd-sized write or a very large write,
1080 * flush out the cache completely and then do a direct write.
1082 if (count < 0 || count > WRITE_DIRECT_SIZE) {
1083 if ((retval = flush_cached_blocks(channel, data,
1086 return raw_write_blk(channel, data, block, count, buf);
1090 * For a moderate-sized multi-block write, first force a write
1091 * if we're in write-through cache mode, and then fill the
1092 * cache with the blocks.
1094 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
1096 retval = raw_write_blk(channel, data, block, count, buf);
1099 mutex_lock(data, CACHE_MTX);
1101 cache = find_cached_block(data, block, &reuse);
1104 reuse_cache(channel, data, cache, block);
1106 if (cache->buf != cp)
1107 memcpy(cache->buf, cp, channel->block_size);
1108 cache->dirty = !writethrough;
1111 cp += channel->block_size;
1113 mutex_unlock(data, CACHE_MTX);
1115 #endif /* NO_IO_CACHE */
1118 static errcode_t unix_cache_readahead(io_channel channel,
1119 unsigned long long block,
1120 unsigned long long count)
1122 #ifdef POSIX_FADV_WILLNEED
1123 struct unix_private_data *data;
1125 data = (struct unix_private_data *)channel->private_data;
1126 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1127 return posix_fadvise(data->dev,
1128 (ext2_loff_t)block * channel->block_size + data->offset,
1129 (ext2_loff_t)count * channel->block_size,
1130 POSIX_FADV_WILLNEED);
1132 return EXT2_ET_OP_NOT_SUPPORTED;
1136 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
1137 int count, const void *buf)
1139 return unix_write_blk64(channel, block, count, buf);
1142 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
1143 int size, const void *buf)
1145 struct unix_private_data *data;
1146 errcode_t retval = 0;
1149 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1150 data = (struct unix_private_data *) channel->private_data;
1151 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1153 if (channel->align != 0) {
1155 printf("unix_write_byte: O_DIRECT fallback\n");
1157 return EXT2_ET_UNIMPLEMENTED;
1162 * Flush out the cache completely
1164 if ((retval = flush_cached_blocks(channel, data, FLUSH_INVALIDATE)))
1168 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
1171 actual = write(data->dev, buf, size);
1175 return EXT2_ET_SHORT_WRITE;
1181 * Flush data buffers to disk.
1183 static errcode_t unix_flush(io_channel channel)
1185 struct unix_private_data *data;
1186 errcode_t retval = 0;
1188 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1189 data = (struct unix_private_data *) channel->private_data;
1190 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1193 retval = flush_cached_blocks(channel, data, 0);
1196 if (!retval && fsync(data->dev) != 0)
1202 static errcode_t unix_set_option(io_channel channel, const char *option,
1205 struct unix_private_data *data;
1206 unsigned long long tmp;
1210 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1211 data = (struct unix_private_data *) channel->private_data;
1212 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1214 if (!strcmp(option, "offset")) {
1216 return EXT2_ET_INVALID_ARGUMENT;
1218 tmp = strtoull(arg, &end, 0);
1220 return EXT2_ET_INVALID_ARGUMENT;
1222 if (data->offset < 0)
1223 return EXT2_ET_INVALID_ARGUMENT;
1226 if (!strcmp(option, "cache")) {
1228 return EXT2_ET_INVALID_ARGUMENT;
1229 if (!strcmp(arg, "on")) {
1230 data->flags &= ~IO_FLAG_NOCACHE;
1233 if (!strcmp(arg, "off")) {
1234 retval = flush_cached_blocks(channel, data, 0);
1235 data->flags |= IO_FLAG_NOCACHE;
1238 return EXT2_ET_INVALID_ARGUMENT;
1240 return EXT2_ET_INVALID_ARGUMENT;
1243 #if defined(__linux__) && !defined(BLKDISCARD)
1244 #define BLKDISCARD _IO(0x12,119)
1247 static errcode_t unix_discard(io_channel channel, unsigned long long block,
1248 unsigned long long count)
1250 struct unix_private_data *data;
1253 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1254 data = (struct unix_private_data *) channel->private_data;
1255 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1257 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1261 range[0] = (__u64)(block) * channel->block_size + data->offset;
1262 range[1] = (__u64)(count) * channel->block_size;
1264 ret = ioctl(data->dev, BLKDISCARD, &range);
1269 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1271 * If we are not on block device, try to use punch hole
1272 * to reclaim free space.
1274 ret = fallocate(data->dev,
1275 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1276 (off_t)(block) * channel->block_size + data->offset,
1277 (off_t)(count) * channel->block_size);
1283 if (errno == EOPNOTSUPP)
1289 return EXT2_ET_UNIMPLEMENTED;
1293 * If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because
1294 * ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because
1295 * it always invalidates page cache, and libext2fs requires that reads after
1296 * ZERO_RANGE return zeroes.
1298 static int __unix_zeroout(int fd, off_t offset, off_t len)
1302 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
1303 ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, offset, len);
1307 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1308 ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1317 /* parameters might not be used if OS doesn't support zeroout */
1318 #if __GNUC_PREREQ (4, 6)
1319 #pragma GCC diagnostic push
1320 #pragma GCC diagnostic ignored "-Wunused-parameter"
1322 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1323 unsigned long long count)
1325 struct unix_private_data *data;
1328 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1329 data = (struct unix_private_data *) channel->private_data;
1330 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1332 if (safe_getenv("UNIX_IO_NOZEROOUT"))
1335 if (!(channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE)) {
1336 /* Regular file, try to use truncate/punch/zero. */
1337 struct stat statbuf;
1342 * If we're trying to zero a range past the end of the file,
1343 * extend the file size, then truncate everything.
1345 ret = fstat(data->dev, &statbuf);
1348 if ((unsigned long long) statbuf.st_size <
1349 (block + count) * channel->block_size + data->offset) {
1350 ret = ftruncate(data->dev,
1351 (block + count) * channel->block_size + data->offset);
1357 ret = __unix_zeroout(data->dev,
1358 (off_t)(block) * channel->block_size + data->offset,
1359 (off_t)(count) * channel->block_size);
1362 if (errno == EOPNOTSUPP)
1368 return EXT2_ET_UNIMPLEMENTED;
1370 #if __GNUC_PREREQ (4, 6)
1371 #pragma GCC diagnostic pop
1374 static struct struct_io_manager struct_unix_manager = {
1375 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1376 .name = "Unix I/O Manager",
1378 .close = unix_close,
1379 .set_blksize = unix_set_blksize,
1380 .read_blk = unix_read_blk,
1381 .write_blk = unix_write_blk,
1382 .flush = unix_flush,
1383 .write_byte = unix_write_byte,
1384 .set_option = unix_set_option,
1385 .get_stats = unix_get_stats,
1386 .read_blk64 = unix_read_blk64,
1387 .write_blk64 = unix_write_blk64,
1388 .discard = unix_discard,
1389 .cache_readahead = unix_cache_readahead,
1390 .zeroout = unix_zeroout,
1393 io_manager unix_io_manager = &struct_unix_manager;
1395 static struct struct_io_manager struct_unixfd_manager = {
1396 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1397 .name = "Unix fd I/O Manager",
1398 .open = unixfd_open,
1399 .close = unix_close,
1400 .set_blksize = unix_set_blksize,
1401 .read_blk = unix_read_blk,
1402 .write_blk = unix_write_blk,
1403 .flush = unix_flush,
1404 .write_byte = unix_write_byte,
1405 .set_option = unix_set_option,
1406 .get_stats = unix_get_stats,
1407 .read_blk64 = unix_read_blk64,
1408 .write_blk64 = unix_write_blk64,
1409 .discard = unix_discard,
1410 .cache_readahead = unix_cache_readahead,
1411 .zeroout = unix_zeroout,
1414 io_manager unixfd_io_manager = &struct_unixfd_manager;