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;
101 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
102 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
104 struct unix_private_data {
111 struct unix_cache cache[CACHE_SIZE];
113 struct struct_io_stats io_stats;
115 pthread_mutex_t cache_mutex;
116 pthread_mutex_t bounce_mutex;
117 pthread_mutex_t stats_mutex;
121 #define IS_ALIGNED(n, align) ((((uintptr_t) n) & \
122 ((uintptr_t) ((align)-1))) == 0)
124 typedef enum lock_kind {
125 CACHE_MTX, BOUNCE_MTX, STATS_MTX
129 static inline pthread_mutex_t *get_mutex(struct unix_private_data *data,
132 if (data->flags & IO_FLAG_THREADS) {
135 return &data->cache_mutex;
137 return &data->bounce_mutex;
139 return &data->stats_mutex;
146 static inline void mutex_lock(struct unix_private_data *data, kind_t kind)
149 pthread_mutex_t *mtx = get_mutex(data,kind);
152 pthread_mutex_lock(mtx);
156 static inline void mutex_unlock(struct unix_private_data *data, kind_t kind)
159 pthread_mutex_t *mtx = get_mutex(data,kind);
162 pthread_mutex_unlock(mtx);
166 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
168 errcode_t retval = 0;
170 struct unix_private_data *data;
172 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
173 data = (struct unix_private_data *) channel->private_data;
174 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
177 mutex_lock(data, STATS_MTX);
178 *stats = &data->io_stats;
179 mutex_unlock(data, STATS_MTX);
185 static char *safe_getenv(const char *arg)
187 if ((getuid() != geteuid()) || (getgid() != getegid()))
190 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
193 #if (defined(linux) && defined(SYS_prctl))
194 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
199 #if defined(HAVE_SECURE_GETENV)
200 return secure_getenv(arg);
201 #elif defined(HAVE___SECURE_GETENV)
202 return __secure_getenv(arg);
209 * Here are the raw I/O functions
211 static errcode_t raw_read_blk(io_channel channel,
212 struct unix_private_data *data,
213 unsigned long long block,
214 int count, void *bufv)
218 ext2_loff_t location;
220 unsigned char *buf = bufv;
221 ssize_t really_read = 0;
222 unsigned long long aligned_blk;
223 int align_size, offset;
225 size = (count < 0) ? -count : (ext2_loff_t) count * channel->block_size;
226 mutex_lock(data, STATS_MTX);
227 data->io_stats.bytes_read += size;
228 mutex_unlock(data, STATS_MTX);
229 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
231 if (data->flags & IO_FLAG_FORCE_BOUNCE)
235 /* Try an aligned pread */
236 if ((channel->align == 0) ||
237 (IS_ALIGNED(buf, channel->align) &&
238 IS_ALIGNED(location, channel->align) &&
239 IS_ALIGNED(size, channel->align))) {
240 actual = pread64(data->dev, buf, size, location);
246 /* Try an aligned pread */
247 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
248 ((channel->align == 0) ||
249 (IS_ALIGNED(buf, channel->align) &&
250 IS_ALIGNED(location, channel->align) &&
251 IS_ALIGNED(size, channel->align)))) {
252 actual = pread(data->dev, buf, size, location);
257 #endif /* HAVE_PREAD */
259 if ((channel->align == 0) ||
260 (IS_ALIGNED(buf, channel->align) &&
261 IS_ALIGNED(location, channel->align) &&
262 IS_ALIGNED(size, channel->align))) {
263 mutex_lock(data, BOUNCE_MTX);
264 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
265 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
268 actual = read(data->dev, buf, size);
269 if (actual != size) {
275 retval = EXT2_ET_SHORT_READ;
282 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
283 (unsigned long) size);
287 * The buffer or size which we're trying to read isn't aligned
288 * to the O_DIRECT rules, so we need to do this the hard way...
291 if (channel->align == 0)
293 if ((channel->block_size > channel->align) &&
294 (channel->block_size % channel->align) == 0)
295 align_size = channel->block_size;
297 align_size = channel->align;
298 aligned_blk = location / align_size;
299 offset = location % align_size;
301 mutex_lock(data, BOUNCE_MTX);
302 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
303 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
307 actual = read(data->dev, data->bounce, align_size);
308 if (actual != align_size) {
309 mutex_unlock(data, BOUNCE_MTX);
310 actual = really_read;
315 if ((actual + offset) > align_size)
316 actual = align_size - offset;
319 memcpy(buf, (char *)data->bounce + offset, actual);
321 really_read += actual;
328 mutex_unlock(data, BOUNCE_MTX);
332 mutex_unlock(data, BOUNCE_MTX);
333 if (actual >= 0 && actual < size)
334 memset((char *) buf+actual, 0, size-actual);
335 if (channel->read_error)
336 retval = (channel->read_error)(channel, block, count, buf,
337 size, actual, retval);
341 #define RAW_WRITE_NO_HANDLER 1
343 static errcode_t raw_write_blk(io_channel channel,
344 struct unix_private_data *data,
345 unsigned long long block,
346 int count, const void *bufv,
350 ext2_loff_t location;
353 const unsigned char *buf = bufv;
354 unsigned long long aligned_blk;
355 int align_size, offset;
358 size = channel->block_size;
363 size = (ext2_loff_t) count * channel->block_size;
365 mutex_lock(data, STATS_MTX);
366 data->io_stats.bytes_written += size;
367 mutex_unlock(data, STATS_MTX);
369 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
371 if (data->flags & IO_FLAG_FORCE_BOUNCE)
375 /* Try an aligned pwrite */
376 if ((channel->align == 0) ||
377 (IS_ALIGNED(buf, channel->align) &&
378 IS_ALIGNED(location, channel->align) &&
379 IS_ALIGNED(size, channel->align))) {
380 actual = pwrite64(data->dev, buf, size, location);
385 /* Try an aligned pwrite */
386 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
387 ((channel->align == 0) ||
388 (IS_ALIGNED(buf, channel->align) &&
389 IS_ALIGNED(location, channel->align) &&
390 IS_ALIGNED(size, channel->align)))) {
391 actual = pwrite(data->dev, buf, size, location);
395 #endif /* HAVE_PWRITE */
397 if ((channel->align == 0) ||
398 (IS_ALIGNED(buf, channel->align) &&
399 IS_ALIGNED(location, channel->align) &&
400 IS_ALIGNED(size, channel->align))) {
401 mutex_lock(data, BOUNCE_MTX);
402 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
403 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
406 actual = write(data->dev, buf, size);
407 mutex_unlock(data, BOUNCE_MTX);
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->align == 0)
431 if ((channel->block_size > channel->align) &&
432 (channel->block_size % channel->align) == 0)
433 align_size = channel->block_size;
435 align_size = channel->align;
436 aligned_blk = location / align_size;
437 offset = location % align_size;
442 mutex_lock(data, BOUNCE_MTX);
443 if (size < align_size || offset) {
444 if (ext2fs_llseek(data->dev, aligned_blk * align_size,
446 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
449 actual = read(data->dev, data->bounce,
451 if (actual != align_size) {
456 memset((char *) data->bounce + actual, 0,
457 align_size - actual);
461 if ((actual + offset) > align_size)
462 actual = align_size - offset;
465 memcpy(((char *)data->bounce) + offset, buf, actual);
466 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
467 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
470 actual_w = write(data->dev, data->bounce, align_size);
471 mutex_unlock(data, BOUNCE_MTX);
476 if (actual_w != align_size)
487 mutex_unlock(data, BOUNCE_MTX);
489 if (((flags & RAW_WRITE_NO_HANDLER) == 0) && channel->write_error)
490 retval = (channel->write_error)(channel, block, count, buf,
491 size, actual, retval);
497 * Here we implement the cache functions
500 /* Allocate the cache buffers */
501 static errcode_t alloc_cache(io_channel channel,
502 struct unix_private_data *data)
505 struct unix_cache *cache;
508 data->access_time = 0;
509 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
511 cache->access_time = 0;
515 ext2fs_free_mem(&cache->buf);
516 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
520 if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
522 ext2fs_free_mem(&data->bounce);
523 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
528 /* Free the cache buffers */
529 static void free_cache(struct unix_private_data *data)
531 struct unix_cache *cache;
534 data->access_time = 0;
535 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
537 cache->access_time = 0;
541 ext2fs_free_mem(&cache->buf);
544 ext2fs_free_mem(&data->bounce);
549 * Try to find a block in the cache. If the block is not found, and
550 * eldest is a non-zero pointer, then fill in eldest with the cache
551 * entry to that should be reused.
553 static struct unix_cache *find_cached_block(struct unix_private_data *data,
554 unsigned long long block,
555 struct unix_cache **eldest)
557 struct unix_cache *cache, *unused_cache, *oldest_cache;
560 unused_cache = oldest_cache = 0;
561 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
562 if (!cache->in_use) {
564 unused_cache = cache;
567 if (cache->block == block) {
568 cache->access_time = ++data->access_time;
572 (cache->access_time < oldest_cache->access_time))
573 oldest_cache = cache;
576 *eldest = (unused_cache) ? unused_cache : oldest_cache;
581 * Reuse a particular cache entry for another block.
583 static errcode_t reuse_cache(io_channel channel,
584 struct unix_private_data *data, struct unix_cache *cache,
585 unsigned long long block)
587 if (cache->dirty && cache->in_use) {
590 retval = raw_write_blk(channel, data, cache->block, 1,
591 cache->buf, RAW_WRITE_NO_HANDLER);
593 cache->write_err = 1;
600 cache->write_err = 0;
601 cache->block = block;
602 cache->access_time = ++data->access_time;
606 #define FLUSH_INVALIDATE 0x01
607 #define FLUSH_NOLOCK 0x02
610 * Flush all of the blocks in the cache
612 static errcode_t flush_cached_blocks(io_channel channel,
613 struct unix_private_data *data,
616 struct unix_cache *cache;
617 errcode_t retval, retval2 = 0;
619 int errors_found = 0;
621 if ((flags & FLUSH_NOLOCK) == 0)
622 mutex_lock(data, CACHE_MTX);
623 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
624 if (!cache->in_use || !cache->dirty)
626 retval = raw_write_blk(channel, data,
627 cache->block, 1, cache->buf,
628 RAW_WRITE_NO_HANDLER);
630 cache->write_err = 1;
635 cache->write_err = 0;
636 if (flags & FLUSH_INVALIDATE)
640 if ((flags & FLUSH_NOLOCK) == 0)
641 mutex_unlock(data, CACHE_MTX);
643 while (errors_found) {
644 if ((flags & FLUSH_NOLOCK) == 0)
645 mutex_lock(data, CACHE_MTX);
647 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
648 if (!cache->in_use || !cache->write_err)
651 if (cache->write_err && channel->write_error) {
652 char *err_buf = NULL;
653 unsigned long long err_block = cache->block;
657 cache->write_err = 0;
658 if (io_channel_alloc_buf(channel, 0,
662 memcpy(err_buf, cache->buf,
663 channel->block_size);
664 mutex_unlock(data, CACHE_MTX);
665 (channel->write_error)(channel, err_block,
666 1, err_buf, channel->block_size, -1,
669 ext2fs_free_mem(&err_buf);
672 cache->write_err = 0;
674 if ((flags & FLUSH_NOLOCK) == 0)
675 mutex_unlock(data, CACHE_MTX);
679 #endif /* NO_IO_CACHE */
682 #ifndef BLKDISCARDZEROES
683 #define BLKDISCARDZEROES _IO(0x12,124)
687 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
690 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
691 return open64(pathname, flags, mode);
693 return open64(pathname, flags);
695 return open(pathname, flags, mode);
697 return open(pathname, flags);
701 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
703 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
704 return stat64(path, buf);
706 return stat(path, buf);
710 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
712 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
713 return fstat64(fd, buf);
715 return fstat(fd, buf);
720 static errcode_t unix_open_channel(const char *name, int fd,
721 int flags, io_channel *channel,
724 io_channel io = NULL;
725 struct unix_private_data *data = NULL;
727 ext2fs_struct_stat st;
732 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
733 flags |= IO_FLAG_FORCE_BOUNCE;
737 * We need to make sure any previous errors in the block
738 * device are thrown away, sigh.
743 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
746 memset(io, 0, sizeof(struct struct_io_channel));
747 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
748 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
752 io->manager = io_mgr;
753 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
757 strcpy(io->name, name);
758 io->private_data = data;
759 io->block_size = 1024;
765 memset(data, 0, sizeof(struct unix_private_data));
766 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
767 data->io_stats.num_fields = 2;
771 #if defined(O_DIRECT)
772 if (flags & IO_FLAG_DIRECT_IO)
773 io->align = ext2fs_get_dio_alignment(data->dev);
774 #elif defined(F_NOCACHE)
775 if (flags & IO_FLAG_DIRECT_IO)
780 * If the device is really a block device, then set the
781 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
782 * because we are going to use punch hole instead of discard
783 * and if it succeed, subsequent read from sparse area returns
786 if (ext2fs_fstat(data->dev, &st) == 0) {
787 if (ext2fsP_is_disk_device(st.st_mode))
788 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
790 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
793 #ifdef BLKDISCARDZEROES
796 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
798 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
802 #if defined(__CYGWIN__)
804 * Some operating systems require that the buffers be aligned,
805 * regardless of O_DIRECT
811 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
812 if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
813 int dio_align = ext2fs_get_dio_alignment(fd);
815 if (io->align < dio_align)
816 io->align = dio_align;
820 if ((retval = alloc_cache(io, data)))
824 if (flags & IO_FLAG_RW) {
828 /* Is the block device actually writable? */
829 error = ioctl(data->dev, BLKROGET, &readonly);
830 if (!error && readonly) {
839 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
840 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
842 #define RLIM_INFINITY (~0UL)
845 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
846 * block devices are wrongly getting hit by the filesize
847 * limit. This workaround isn't perfect, since it won't work
848 * if glibc wasn't built against 2.2 header files. (Sigh.)
851 if ((flags & IO_FLAG_RW) &&
853 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
854 (ut.release[2] == '4') && (ut.release[3] == '.') &&
855 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
856 (ut.release[5] < '8')) &&
857 (ext2fs_fstat(data->dev, &st) == 0) &&
858 (ext2fsP_is_disk_device(st.st_mode))) {
861 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
862 setrlimit(RLIMIT_FSIZE, &rlim);
863 getrlimit(RLIMIT_FSIZE, &rlim);
864 if (((unsigned long) rlim.rlim_cur) <
865 ((unsigned long) rlim.rlim_max)) {
866 rlim.rlim_cur = rlim.rlim_max;
867 setrlimit(RLIMIT_FSIZE, &rlim);
872 if (flags & IO_FLAG_THREADS) {
873 io->flags |= CHANNEL_FLAGS_THREADS;
874 retval = pthread_mutex_init(&data->cache_mutex, NULL);
877 retval = pthread_mutex_init(&data->bounce_mutex, NULL);
879 pthread_mutex_destroy(&data->cache_mutex);
882 retval = pthread_mutex_init(&data->stats_mutex, NULL);
884 pthread_mutex_destroy(&data->cache_mutex);
885 pthread_mutex_destroy(&data->bounce_mutex);
898 ext2fs_free_mem(&data);
902 ext2fs_free_mem(&io->name);
904 ext2fs_free_mem(&io);
909 static errcode_t unixfd_open(const char *str_fd, int flags,
916 #if defined(HAVE_FCNTL)
917 fd_flags = fcntl(fd, F_GETFD);
922 if (fd_flags & O_RDWR)
924 if (fd_flags & O_EXCL)
925 flags |= IO_FLAG_EXCLUSIVE;
926 #if defined(O_DIRECT)
927 if (fd_flags & O_DIRECT)
928 flags |= IO_FLAG_DIRECT_IO;
930 #endif /* HAVE_FCNTL */
932 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
935 static errcode_t unix_open(const char *name, int flags,
942 return EXT2_ET_BAD_DEVICE_NAME;
944 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
945 if (flags & IO_FLAG_EXCLUSIVE)
946 open_flags |= O_EXCL;
947 #if defined(O_DIRECT)
948 if (flags & IO_FLAG_DIRECT_IO)
949 open_flags |= O_DIRECT;
951 fd = ext2fs_open_file(name, open_flags, 0);
954 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
955 if (flags & IO_FLAG_DIRECT_IO) {
956 if (fcntl(fd, F_NOCACHE, 1) < 0)
960 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
963 static errcode_t unix_close(io_channel channel)
965 struct unix_private_data *data;
966 errcode_t retval = 0;
968 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
969 data = (struct unix_private_data *) channel->private_data;
970 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
972 if (--channel->refcount > 0)
976 retval = flush_cached_blocks(channel, data, 0);
979 if (close(data->dev) < 0)
983 if (data->flags & IO_FLAG_THREADS) {
984 pthread_mutex_destroy(&data->cache_mutex);
985 pthread_mutex_destroy(&data->bounce_mutex);
986 pthread_mutex_destroy(&data->stats_mutex);
990 ext2fs_free_mem(&channel->private_data);
992 ext2fs_free_mem(&channel->name);
993 ext2fs_free_mem(&channel);
997 static errcode_t unix_set_blksize(io_channel channel, int blksize)
999 struct unix_private_data *data;
1000 errcode_t retval = 0;
1002 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1003 data = (struct unix_private_data *) channel->private_data;
1004 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1006 if (channel->block_size != blksize) {
1007 mutex_lock(data, CACHE_MTX);
1008 mutex_lock(data, BOUNCE_MTX);
1010 if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK))){
1011 mutex_unlock(data, BOUNCE_MTX);
1012 mutex_unlock(data, CACHE_MTX);
1017 channel->block_size = blksize;
1019 retval = alloc_cache(channel, data);
1020 mutex_unlock(data, BOUNCE_MTX);
1021 mutex_unlock(data, CACHE_MTX);
1026 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
1027 int count, void *buf)
1029 struct unix_private_data *data;
1030 struct unix_cache *cache;
1035 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1036 data = (struct unix_private_data *) channel->private_data;
1037 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1040 return raw_read_blk(channel, data, block, count, buf);
1042 if (data->flags & IO_FLAG_NOCACHE)
1043 return raw_read_blk(channel, data, block, count, buf);
1045 * If we're doing an odd-sized read or a very large read,
1046 * flush out the cache and then do a direct read.
1048 if (count < 0 || count > WRITE_DIRECT_SIZE) {
1049 if ((retval = flush_cached_blocks(channel, data, 0)))
1051 return raw_read_blk(channel, data, block, count, buf);
1055 mutex_lock(data, CACHE_MTX);
1057 /* If it's in the cache, use it! */
1058 if ((cache = find_cached_block(data, block, NULL))) {
1060 printf("Using cached block %lu\n", block);
1062 memcpy(cp, cache->buf, channel->block_size);
1065 cp += channel->block_size;
1070 * Find the number of uncached blocks so we can do a
1071 * single read request
1073 for (i=1; i < count; i++)
1074 if (find_cached_block(data, block+i, NULL))
1077 printf("Reading %d blocks starting at %lu\n", i, block);
1079 mutex_unlock(data, CACHE_MTX);
1080 if ((retval = raw_read_blk(channel, data, block, i, cp)))
1082 mutex_lock(data, CACHE_MTX);
1084 /* Save the results in the cache */
1085 for (j=0; j < i; j++) {
1086 if (!find_cached_block(data, block, &cache)) {
1087 retval = reuse_cache(channel, data,
1090 goto call_write_handler;
1091 memcpy(cache->buf, cp, channel->block_size);
1095 cp += channel->block_size;
1098 mutex_unlock(data, CACHE_MTX);
1102 if (cache->write_err && channel->write_error) {
1103 char *err_buf = NULL;
1104 unsigned long long err_block = cache->block;
1108 cache->write_err = 0;
1109 if (io_channel_alloc_buf(channel, 0, &err_buf))
1112 memcpy(err_buf, cache->buf, channel->block_size);
1113 mutex_unlock(data, CACHE_MTX);
1114 (channel->write_error)(channel, err_block, 1, err_buf,
1115 channel->block_size, -1,
1118 ext2fs_free_mem(&err_buf);
1120 mutex_unlock(data, CACHE_MTX);
1122 #endif /* NO_IO_CACHE */
1125 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
1126 int count, void *buf)
1128 return unix_read_blk64(channel, block, count, buf);
1131 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
1132 int count, const void *buf)
1134 struct unix_private_data *data;
1135 struct unix_cache *cache, *reuse;
1136 errcode_t retval = 0;
1140 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1141 data = (struct unix_private_data *) channel->private_data;
1142 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1145 return raw_write_blk(channel, data, block, count, buf, 0);
1147 if (data->flags & IO_FLAG_NOCACHE)
1148 return raw_write_blk(channel, data, block, count, buf, 0);
1150 * If we're doing an odd-sized write or a very large write,
1151 * flush out the cache completely and then do a direct write.
1153 if (count < 0 || count > WRITE_DIRECT_SIZE) {
1154 if ((retval = flush_cached_blocks(channel, data,
1157 return raw_write_blk(channel, data, block, count, buf, 0);
1161 * For a moderate-sized multi-block write, first force a write
1162 * if we're in write-through cache mode, and then fill the
1163 * cache with the blocks.
1165 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
1167 retval = raw_write_blk(channel, data, block, count, buf, 0);
1170 mutex_lock(data, CACHE_MTX);
1172 cache = find_cached_block(data, block, &reuse);
1177 err = reuse_cache(channel, data, cache, block);
1179 goto call_write_handler;
1181 if (cache->buf != cp)
1182 memcpy(cache->buf, cp, channel->block_size);
1183 cache->dirty = !writethrough;
1186 cp += channel->block_size;
1188 mutex_unlock(data, CACHE_MTX);
1192 if (cache->write_err && channel->write_error) {
1193 char *err_buf = NULL;
1194 unsigned long long err_block = cache->block;
1198 cache->write_err = 0;
1199 if (io_channel_alloc_buf(channel, 0, &err_buf))
1202 memcpy(err_buf, cache->buf, channel->block_size);
1203 mutex_unlock(data, CACHE_MTX);
1204 (channel->write_error)(channel, err_block, 1, err_buf,
1205 channel->block_size, -1,
1208 ext2fs_free_mem(&err_buf);
1210 mutex_unlock(data, CACHE_MTX);
1212 #endif /* NO_IO_CACHE */
1215 static errcode_t unix_cache_readahead(io_channel channel,
1216 unsigned long long block,
1217 unsigned long long count)
1219 #ifdef POSIX_FADV_WILLNEED
1220 struct unix_private_data *data;
1222 data = (struct unix_private_data *)channel->private_data;
1223 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1224 return posix_fadvise(data->dev,
1225 (ext2_loff_t)block * channel->block_size + data->offset,
1226 (ext2_loff_t)count * channel->block_size,
1227 POSIX_FADV_WILLNEED);
1229 return EXT2_ET_OP_NOT_SUPPORTED;
1233 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
1234 int count, const void *buf)
1236 return unix_write_blk64(channel, block, count, buf);
1239 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
1240 int size, const void *buf)
1242 struct unix_private_data *data;
1243 errcode_t retval = 0;
1246 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1247 data = (struct unix_private_data *) channel->private_data;
1248 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1250 if (channel->align != 0) {
1252 printf("unix_write_byte: O_DIRECT fallback\n");
1254 return EXT2_ET_UNIMPLEMENTED;
1259 * Flush out the cache completely
1261 if ((retval = flush_cached_blocks(channel, data, FLUSH_INVALIDATE)))
1265 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
1268 actual = write(data->dev, buf, size);
1272 return EXT2_ET_SHORT_WRITE;
1278 * Flush data buffers to disk.
1280 static errcode_t unix_flush(io_channel channel)
1282 struct unix_private_data *data;
1283 errcode_t retval = 0;
1285 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1286 data = (struct unix_private_data *) channel->private_data;
1287 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1290 retval = flush_cached_blocks(channel, data, 0);
1293 if (!retval && fsync(data->dev) != 0)
1299 static errcode_t unix_set_option(io_channel channel, const char *option,
1302 struct unix_private_data *data;
1303 unsigned long long tmp;
1307 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1308 data = (struct unix_private_data *) channel->private_data;
1309 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1311 if (!strcmp(option, "offset")) {
1313 return EXT2_ET_INVALID_ARGUMENT;
1315 tmp = strtoull(arg, &end, 0);
1317 return EXT2_ET_INVALID_ARGUMENT;
1319 if (data->offset < 0)
1320 return EXT2_ET_INVALID_ARGUMENT;
1323 if (!strcmp(option, "cache")) {
1325 return EXT2_ET_INVALID_ARGUMENT;
1326 if (!strcmp(arg, "on")) {
1327 data->flags &= ~IO_FLAG_NOCACHE;
1330 if (!strcmp(arg, "off")) {
1331 retval = flush_cached_blocks(channel, data, 0);
1332 data->flags |= IO_FLAG_NOCACHE;
1335 return EXT2_ET_INVALID_ARGUMENT;
1337 return EXT2_ET_INVALID_ARGUMENT;
1340 #if defined(__linux__) && !defined(BLKDISCARD)
1341 #define BLKDISCARD _IO(0x12,119)
1344 static errcode_t unix_discard(io_channel channel, unsigned long long block,
1345 unsigned long long count)
1347 struct unix_private_data *data;
1350 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1351 data = (struct unix_private_data *) channel->private_data;
1352 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1354 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1358 range[0] = (__u64)(block) * channel->block_size + data->offset;
1359 range[1] = (__u64)(count) * channel->block_size;
1361 ret = ioctl(data->dev, BLKDISCARD, &range);
1366 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1368 * If we are not on block device, try to use punch hole
1369 * to reclaim free space.
1371 ret = fallocate(data->dev,
1372 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1373 (off_t)(block) * channel->block_size + data->offset,
1374 (off_t)(count) * channel->block_size);
1380 if (errno == EOPNOTSUPP)
1386 return EXT2_ET_UNIMPLEMENTED;
1390 * If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because
1391 * ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because
1392 * it always invalidates page cache, and libext2fs requires that reads after
1393 * ZERO_RANGE return zeroes.
1395 static int __unix_zeroout(int fd, off_t offset, off_t len)
1399 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
1400 ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, offset, len);
1404 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1405 ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1414 /* parameters might not be used if OS doesn't support zeroout */
1415 #if __GNUC_PREREQ (4, 6)
1416 #pragma GCC diagnostic push
1417 #pragma GCC diagnostic ignored "-Wunused-parameter"
1419 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1420 unsigned long long count)
1422 struct unix_private_data *data;
1425 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1426 data = (struct unix_private_data *) channel->private_data;
1427 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1429 if (safe_getenv("UNIX_IO_NOZEROOUT"))
1432 if (!(channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE)) {
1433 /* Regular file, try to use truncate/punch/zero. */
1434 struct stat statbuf;
1439 * If we're trying to zero a range past the end of the file,
1440 * extend the file size, then truncate everything.
1442 ret = fstat(data->dev, &statbuf);
1445 if ((unsigned long long) statbuf.st_size <
1446 (block + count) * channel->block_size + data->offset) {
1447 ret = ftruncate(data->dev,
1448 (block + count) * channel->block_size + data->offset);
1454 ret = __unix_zeroout(data->dev,
1455 (off_t)(block) * channel->block_size + data->offset,
1456 (off_t)(count) * channel->block_size);
1459 if (errno == EOPNOTSUPP)
1465 return EXT2_ET_UNIMPLEMENTED;
1467 #if __GNUC_PREREQ (4, 6)
1468 #pragma GCC diagnostic pop
1471 static struct struct_io_manager struct_unix_manager = {
1472 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1473 .name = "Unix I/O Manager",
1475 .close = unix_close,
1476 .set_blksize = unix_set_blksize,
1477 .read_blk = unix_read_blk,
1478 .write_blk = unix_write_blk,
1479 .flush = unix_flush,
1480 .write_byte = unix_write_byte,
1481 .set_option = unix_set_option,
1482 .get_stats = unix_get_stats,
1483 .read_blk64 = unix_read_blk64,
1484 .write_blk64 = unix_write_blk64,
1485 .discard = unix_discard,
1486 .cache_readahead = unix_cache_readahead,
1487 .zeroout = unix_zeroout,
1490 io_manager unix_io_manager = &struct_unix_manager;
1492 static struct struct_io_manager struct_unixfd_manager = {
1493 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1494 .name = "Unix fd I/O Manager",
1495 .open = unixfd_open,
1496 .close = unix_close,
1497 .set_blksize = unix_set_blksize,
1498 .read_blk = unix_read_blk,
1499 .write_blk = unix_write_blk,
1500 .flush = unix_flush,
1501 .write_byte = unix_write_byte,
1502 .set_option = unix_set_option,
1503 .get_stats = unix_get_stats,
1504 .read_blk64 = unix_read_blk64,
1505 .write_blk64 = unix_write_blk64,
1506 .discard = unix_discard,
1507 .cache_readahead = unix_cache_readahead,
1508 .zeroout = unix_zeroout,
1511 io_manager unixfd_io_manager = &struct_unixfd_manager;