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;
222 size = (count < 0) ? -count : (ext2_loff_t) count * channel->block_size;
223 mutex_lock(data, STATS_MTX);
224 data->io_stats.bytes_read += size;
225 mutex_unlock(data, STATS_MTX);
226 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
228 if (data->flags & IO_FLAG_FORCE_BOUNCE) {
229 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
230 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
237 /* Try an aligned pread */
238 if ((channel->align == 0) ||
239 (IS_ALIGNED(buf, channel->align) &&
240 IS_ALIGNED(size, channel->align))) {
241 actual = pread64(data->dev, buf, size, location);
247 /* Try an aligned pread */
248 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
249 ((channel->align == 0) ||
250 (IS_ALIGNED(buf, channel->align) &&
251 IS_ALIGNED(size, channel->align)))) {
252 actual = pread(data->dev, buf, size, location);
257 #endif /* HAVE_PREAD */
259 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
260 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
263 if ((channel->align == 0) ||
264 (IS_ALIGNED(buf, 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...
290 mutex_lock(data, BOUNCE_MTX);
291 actual = read(data->dev, data->bounce, channel->block_size);
292 if (actual != channel->block_size) {
293 mutex_unlock(data, BOUNCE_MTX);
294 actual = really_read;
300 if (size > channel->block_size)
301 actual = channel->block_size;
302 memcpy(buf, data->bounce, actual);
303 really_read += actual;
306 mutex_unlock(data, BOUNCE_MTX);
311 if (actual >= 0 && actual < size)
312 memset((char *) buf+actual, 0, size-actual);
313 if (channel->read_error)
314 retval = (channel->read_error)(channel, block, count, buf,
315 size, actual, retval);
319 static errcode_t raw_write_blk(io_channel channel,
320 struct unix_private_data *data,
321 unsigned long long block,
322 int count, const void *bufv)
325 ext2_loff_t location;
328 const unsigned char *buf = bufv;
331 size = channel->block_size;
336 size = (ext2_loff_t) count * channel->block_size;
338 mutex_lock(data, STATS_MTX);
339 data->io_stats.bytes_written += size;
340 mutex_unlock(data, STATS_MTX);
342 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
344 if (data->flags & IO_FLAG_FORCE_BOUNCE) {
345 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
346 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
353 /* Try an aligned pwrite */
354 if ((channel->align == 0) ||
355 (IS_ALIGNED(buf, channel->align) &&
356 IS_ALIGNED(size, channel->align))) {
357 actual = pwrite64(data->dev, buf, size, location);
362 /* Try an aligned pwrite */
363 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
364 ((channel->align == 0) ||
365 (IS_ALIGNED(buf, channel->align) &&
366 IS_ALIGNED(size, channel->align)))) {
367 actual = pwrite(data->dev, buf, size, location);
371 #endif /* HAVE_PWRITE */
373 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
374 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
378 if ((channel->align == 0) ||
379 (IS_ALIGNED(buf, channel->align) &&
380 IS_ALIGNED(size, channel->align))) {
381 actual = write(data->dev, buf, size);
386 if (actual != size) {
388 retval = EXT2_ET_SHORT_WRITE;
395 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
396 (unsigned long) size);
399 * The buffer or size which we're trying to write isn't aligned
400 * to the O_DIRECT rules, so we need to do this the hard way...
404 mutex_lock(data, BOUNCE_MTX);
405 if (size < channel->block_size) {
406 actual = read(data->dev, data->bounce,
407 channel->block_size);
408 if (actual != channel->block_size) {
410 mutex_unlock(data, BOUNCE_MTX);
414 memset((char *) data->bounce + actual, 0,
415 channel->block_size - actual);
419 if (size > channel->block_size)
420 actual = channel->block_size;
421 memcpy(data->bounce, buf, actual);
422 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
423 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
426 actual = write(data->dev, data->bounce, channel->block_size);
427 mutex_unlock(data, BOUNCE_MTX);
432 if (actual != channel->block_size)
441 if (channel->write_error)
442 retval = (channel->write_error)(channel, block, count, buf,
443 size, actual, retval);
449 * Here we implement the cache functions
452 /* Allocate the cache buffers */
453 static errcode_t alloc_cache(io_channel channel,
454 struct unix_private_data *data)
457 struct unix_cache *cache;
460 data->access_time = 0;
461 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
463 cache->access_time = 0;
467 ext2fs_free_mem(&cache->buf);
468 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
472 if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
474 ext2fs_free_mem(&data->bounce);
475 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
480 /* Free the cache buffers */
481 static void free_cache(struct unix_private_data *data)
483 struct unix_cache *cache;
486 data->access_time = 0;
487 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
489 cache->access_time = 0;
493 ext2fs_free_mem(&cache->buf);
496 ext2fs_free_mem(&data->bounce);
501 * Try to find a block in the cache. If the block is not found, and
502 * eldest is a non-zero pointer, then fill in eldest with the cache
503 * entry to that should be reused.
505 static struct unix_cache *find_cached_block(struct unix_private_data *data,
506 unsigned long long block,
507 struct unix_cache **eldest)
509 struct unix_cache *cache, *unused_cache, *oldest_cache;
512 unused_cache = oldest_cache = 0;
513 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
514 if (!cache->in_use) {
516 unused_cache = cache;
519 if (cache->block == block) {
520 cache->access_time = ++data->access_time;
524 (cache->access_time < oldest_cache->access_time))
525 oldest_cache = cache;
528 *eldest = (unused_cache) ? unused_cache : oldest_cache;
533 * Reuse a particular cache entry for another block.
535 static void reuse_cache(io_channel channel, struct unix_private_data *data,
536 struct unix_cache *cache, unsigned long long block)
538 if (cache->dirty && cache->in_use)
539 raw_write_blk(channel, data, cache->block, 1, cache->buf);
543 cache->block = block;
544 cache->access_time = ++data->access_time;
547 #define FLUSH_INVALIDATE 0x01
548 #define FLUSH_NOLOCK 0x02
551 * Flush all of the blocks in the cache
553 static errcode_t flush_cached_blocks(io_channel channel,
554 struct unix_private_data *data,
557 struct unix_cache *cache;
558 errcode_t retval, retval2;
562 if ((flags & FLUSH_NOLOCK) == 0)
563 mutex_lock(data, CACHE_MTX);
564 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
568 if (flags & FLUSH_INVALIDATE)
574 retval = raw_write_blk(channel, data,
575 cache->block, 1, cache->buf);
581 if ((flags & FLUSH_NOLOCK) == 0)
582 mutex_unlock(data, CACHE_MTX);
585 #endif /* NO_IO_CACHE */
588 #ifndef BLKDISCARDZEROES
589 #define BLKDISCARDZEROES _IO(0x12,124)
593 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
596 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
597 return open64(pathname, flags, mode);
599 return open64(pathname, flags);
601 return open(pathname, flags, mode);
603 return open(pathname, flags);
607 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
609 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
610 return stat64(path, buf);
612 return stat(path, buf);
616 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
618 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
619 return fstat64(fd, buf);
621 return fstat(fd, buf);
626 static errcode_t unix_open_channel(const char *name, int fd,
627 int flags, io_channel *channel,
630 io_channel io = NULL;
631 struct unix_private_data *data = NULL;
633 ext2fs_struct_stat st;
638 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
639 flags |= IO_FLAG_FORCE_BOUNCE;
643 * We need to make sure any previous errors in the block
644 * device are thrown away, sigh.
649 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
652 memset(io, 0, sizeof(struct struct_io_channel));
653 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
654 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
658 io->manager = io_mgr;
659 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
663 strcpy(io->name, name);
664 io->private_data = data;
665 io->block_size = 1024;
671 memset(data, 0, sizeof(struct unix_private_data));
672 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
673 data->io_stats.num_fields = 2;
677 #if defined(O_DIRECT)
678 if (flags & IO_FLAG_DIRECT_IO)
679 io->align = ext2fs_get_dio_alignment(data->dev);
680 #elif defined(F_NOCACHE)
681 if (flags & IO_FLAG_DIRECT_IO)
686 * If the device is really a block device, then set the
687 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
688 * because we are going to use punch hole instead of discard
689 * and if it succeed, subsequent read from sparse area returns
692 if (ext2fs_fstat(data->dev, &st) == 0) {
693 if (ext2fsP_is_disk_device(st.st_mode))
694 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
696 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
699 #ifdef BLKDISCARDZEROES
702 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
704 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
708 #if defined(__CYGWIN__)
710 * Some operating systems require that the buffers be aligned,
711 * regardless of O_DIRECT
717 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
718 if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
719 int dio_align = ext2fs_get_dio_alignment(fd);
721 if (io->align < dio_align)
722 io->align = dio_align;
726 if ((retval = alloc_cache(io, data)))
730 if (flags & IO_FLAG_RW) {
734 /* Is the block device actually writable? */
735 error = ioctl(data->dev, BLKROGET, &readonly);
736 if (!error && readonly) {
745 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
746 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
748 #define RLIM_INFINITY (~0UL)
751 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
752 * block devices are wrongly getting hit by the filesize
753 * limit. This workaround isn't perfect, since it won't work
754 * if glibc wasn't built against 2.2 header files. (Sigh.)
757 if ((flags & IO_FLAG_RW) &&
759 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
760 (ut.release[2] == '4') && (ut.release[3] == '.') &&
761 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
762 (ut.release[5] < '8')) &&
763 (ext2fs_fstat(data->dev, &st) == 0) &&
764 (ext2fsP_is_disk_device(st.st_mode))) {
767 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
768 setrlimit(RLIMIT_FSIZE, &rlim);
769 getrlimit(RLIMIT_FSIZE, &rlim);
770 if (((unsigned long) rlim.rlim_cur) <
771 ((unsigned long) rlim.rlim_max)) {
772 rlim.rlim_cur = rlim.rlim_max;
773 setrlimit(RLIMIT_FSIZE, &rlim);
778 if (flags & IO_FLAG_THREADS) {
779 io->flags |= CHANNEL_FLAGS_THREADS;
780 retval = pthread_mutex_init(&data->cache_mutex, NULL);
783 retval = pthread_mutex_init(&data->bounce_mutex, NULL);
785 pthread_mutex_destroy(&data->cache_mutex);
788 retval = pthread_mutex_init(&data->stats_mutex, NULL);
790 pthread_mutex_destroy(&data->cache_mutex);
791 pthread_mutex_destroy(&data->bounce_mutex);
804 ext2fs_free_mem(&data);
808 ext2fs_free_mem(&io->name);
810 ext2fs_free_mem(&io);
815 static errcode_t unixfd_open(const char *str_fd, int flags,
822 #if defined(HAVE_FCNTL)
823 fd_flags = fcntl(fd, F_GETFD);
828 if (fd_flags & O_RDWR)
830 if (fd_flags & O_EXCL)
831 flags |= IO_FLAG_EXCLUSIVE;
832 #if defined(O_DIRECT)
833 if (fd_flags & O_DIRECT)
834 flags |= IO_FLAG_DIRECT_IO;
836 #endif /* HAVE_FCNTL */
838 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
841 static errcode_t unix_open(const char *name, int flags,
848 return EXT2_ET_BAD_DEVICE_NAME;
850 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
851 if (flags & IO_FLAG_EXCLUSIVE)
852 open_flags |= O_EXCL;
853 #if defined(O_DIRECT)
854 if (flags & IO_FLAG_DIRECT_IO)
855 open_flags |= O_DIRECT;
857 fd = ext2fs_open_file(name, open_flags, 0);
860 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
861 if (flags & IO_FLAG_DIRECT_IO) {
862 if (fcntl(fd, F_NOCACHE, 1) < 0)
866 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
869 static errcode_t unix_close(io_channel channel)
871 struct unix_private_data *data;
872 errcode_t retval = 0;
874 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
875 data = (struct unix_private_data *) channel->private_data;
876 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
878 if (--channel->refcount > 0)
882 retval = flush_cached_blocks(channel, data, 0);
885 if (close(data->dev) < 0)
889 if (data->flags & IO_FLAG_THREADS) {
890 pthread_mutex_destroy(&data->cache_mutex);
891 pthread_mutex_destroy(&data->bounce_mutex);
892 pthread_mutex_destroy(&data->stats_mutex);
896 ext2fs_free_mem(&channel->private_data);
898 ext2fs_free_mem(&channel->name);
899 ext2fs_free_mem(&channel);
903 static errcode_t unix_set_blksize(io_channel channel, int blksize)
905 struct unix_private_data *data;
906 errcode_t retval = 0;
908 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
909 data = (struct unix_private_data *) channel->private_data;
910 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
912 if (channel->block_size != blksize) {
913 mutex_lock(data, CACHE_MTX);
914 mutex_lock(data, BOUNCE_MTX);
916 if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK)))
920 channel->block_size = blksize;
922 retval = alloc_cache(channel, data);
923 mutex_unlock(data, BOUNCE_MTX);
924 mutex_unlock(data, CACHE_MTX);
929 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
930 int count, void *buf)
932 struct unix_private_data *data;
933 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
934 errcode_t retval = 0;
938 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
939 data = (struct unix_private_data *) channel->private_data;
940 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
943 return raw_read_blk(channel, data, block, count, buf);
945 if (data->flags & IO_FLAG_NOCACHE)
946 return raw_read_blk(channel, data, block, count, buf);
948 * If we're doing an odd-sized read or a very large read,
949 * flush out the cache and then do a direct read.
951 if (count < 0 || count > WRITE_DIRECT_SIZE) {
952 if ((retval = flush_cached_blocks(channel, data, 0)))
954 return raw_read_blk(channel, data, block, count, buf);
958 mutex_lock(data, CACHE_MTX);
960 /* If it's in the cache, use it! */
961 if ((cache = find_cached_block(data, block, &reuse[0]))) {
963 printf("Using cached block %lu\n", block);
965 memcpy(cp, cache->buf, channel->block_size);
968 cp += channel->block_size;
973 * Special case where we read directly into the
974 * cache buffer; important in the O_DIRECT case
977 reuse_cache(channel, data, cache, block);
978 if ((retval = raw_read_blk(channel, data, block, 1,
983 memcpy(cp, cache->buf, channel->block_size);
989 * Find the number of uncached blocks so we can do a
990 * single read request
992 for (i=1; i < count; i++)
993 if (find_cached_block(data, block+i, &reuse[i]))
996 printf("Reading %d blocks starting at %lu\n", i, block);
998 if ((retval = raw_read_blk(channel, data, block, i, cp)))
1001 /* Save the results in the cache */
1002 for (j=0; j < i; j++) {
1005 reuse_cache(channel, data, cache, block++);
1006 memcpy(cache->buf, cp, channel->block_size);
1007 cp += channel->block_size;
1010 mutex_unlock(data, CACHE_MTX);
1012 #endif /* NO_IO_CACHE */
1015 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
1016 int count, void *buf)
1018 return unix_read_blk64(channel, block, count, buf);
1021 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
1022 int count, const void *buf)
1024 struct unix_private_data *data;
1025 struct unix_cache *cache, *reuse;
1026 errcode_t retval = 0;
1030 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1031 data = (struct unix_private_data *) channel->private_data;
1032 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1035 return raw_write_blk(channel, data, block, count, buf);
1037 if (data->flags & IO_FLAG_NOCACHE)
1038 return raw_write_blk(channel, data, block, count, buf);
1040 * If we're doing an odd-sized write or a very large write,
1041 * flush out the cache completely and then do a direct write.
1043 if (count < 0 || count > WRITE_DIRECT_SIZE) {
1044 if ((retval = flush_cached_blocks(channel, data,
1047 return raw_write_blk(channel, data, block, count, buf);
1051 * For a moderate-sized multi-block write, first force a write
1052 * if we're in write-through cache mode, and then fill the
1053 * cache with the blocks.
1055 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
1057 retval = raw_write_blk(channel, data, block, count, buf);
1060 mutex_lock(data, CACHE_MTX);
1062 cache = find_cached_block(data, block, &reuse);
1065 reuse_cache(channel, data, cache, block);
1067 if (cache->buf != cp)
1068 memcpy(cache->buf, cp, channel->block_size);
1069 cache->dirty = !writethrough;
1072 cp += channel->block_size;
1074 mutex_unlock(data, CACHE_MTX);
1076 #endif /* NO_IO_CACHE */
1079 static errcode_t unix_cache_readahead(io_channel channel,
1080 unsigned long long block,
1081 unsigned long long count)
1083 #ifdef POSIX_FADV_WILLNEED
1084 struct unix_private_data *data;
1086 data = (struct unix_private_data *)channel->private_data;
1087 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1088 return posix_fadvise(data->dev,
1089 (ext2_loff_t)block * channel->block_size + data->offset,
1090 (ext2_loff_t)count * channel->block_size,
1091 POSIX_FADV_WILLNEED);
1093 return EXT2_ET_OP_NOT_SUPPORTED;
1097 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
1098 int count, const void *buf)
1100 return unix_write_blk64(channel, block, count, buf);
1103 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
1104 int size, const void *buf)
1106 struct unix_private_data *data;
1107 errcode_t retval = 0;
1110 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1111 data = (struct unix_private_data *) channel->private_data;
1112 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1114 if (channel->align != 0) {
1116 printf("unix_write_byte: O_DIRECT fallback\n");
1118 return EXT2_ET_UNIMPLEMENTED;
1123 * Flush out the cache completely
1125 if ((retval = flush_cached_blocks(channel, data, FLUSH_INVALIDATE)))
1129 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
1132 actual = write(data->dev, buf, size);
1136 return EXT2_ET_SHORT_WRITE;
1142 * Flush data buffers to disk.
1144 static errcode_t unix_flush(io_channel channel)
1146 struct unix_private_data *data;
1147 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);
1154 retval = flush_cached_blocks(channel, data, 0);
1157 if (!retval && fsync(data->dev) != 0)
1163 static errcode_t unix_set_option(io_channel channel, const char *option,
1166 struct unix_private_data *data;
1167 unsigned long long tmp;
1171 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1172 data = (struct unix_private_data *) channel->private_data;
1173 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1175 if (!strcmp(option, "offset")) {
1177 return EXT2_ET_INVALID_ARGUMENT;
1179 tmp = strtoull(arg, &end, 0);
1181 return EXT2_ET_INVALID_ARGUMENT;
1183 if (data->offset < 0)
1184 return EXT2_ET_INVALID_ARGUMENT;
1187 if (!strcmp(option, "cache")) {
1189 return EXT2_ET_INVALID_ARGUMENT;
1190 if (!strcmp(arg, "on")) {
1191 data->flags &= ~IO_FLAG_NOCACHE;
1194 if (!strcmp(arg, "off")) {
1195 retval = flush_cached_blocks(channel, data, 0);
1196 data->flags |= IO_FLAG_NOCACHE;
1199 return EXT2_ET_INVALID_ARGUMENT;
1201 return EXT2_ET_INVALID_ARGUMENT;
1204 #if defined(__linux__) && !defined(BLKDISCARD)
1205 #define BLKDISCARD _IO(0x12,119)
1208 static errcode_t unix_discard(io_channel channel, unsigned long long block,
1209 unsigned long long count)
1211 struct unix_private_data *data;
1214 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1215 data = (struct unix_private_data *) channel->private_data;
1216 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1218 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1222 range[0] = (__u64)(block) * channel->block_size + data->offset;
1223 range[1] = (__u64)(count) * channel->block_size;
1225 ret = ioctl(data->dev, BLKDISCARD, &range);
1230 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1232 * If we are not on block device, try to use punch hole
1233 * to reclaim free space.
1235 ret = fallocate(data->dev,
1236 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1237 (off_t)(block) * channel->block_size + data->offset,
1238 (off_t)(count) * channel->block_size);
1244 if (errno == EOPNOTSUPP)
1250 return EXT2_ET_UNIMPLEMENTED;
1254 * If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because
1255 * ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because
1256 * it always invalidates page cache, and libext2fs requires that reads after
1257 * ZERO_RANGE return zeroes.
1259 static int __unix_zeroout(int fd, off_t offset, off_t len)
1263 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
1264 ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, offset, len);
1268 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1269 ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1278 /* parameters might not be used if OS doesn't support zeroout */
1279 #if __GNUC_PREREQ (4, 6)
1280 #pragma GCC diagnostic push
1281 #pragma GCC diagnostic ignored "-Wunused-parameter"
1283 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1284 unsigned long long count)
1286 struct unix_private_data *data;
1289 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1290 data = (struct unix_private_data *) channel->private_data;
1291 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1293 if (safe_getenv("UNIX_IO_NOZEROOUT"))
1296 if (!(channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE)) {
1297 /* Regular file, try to use truncate/punch/zero. */
1298 struct stat statbuf;
1303 * If we're trying to zero a range past the end of the file,
1304 * extend the file size, then truncate everything.
1306 ret = fstat(data->dev, &statbuf);
1309 if ((unsigned long long) statbuf.st_size <
1310 (block + count) * channel->block_size + data->offset) {
1311 ret = ftruncate(data->dev,
1312 (block + count) * channel->block_size + data->offset);
1318 ret = __unix_zeroout(data->dev,
1319 (off_t)(block) * channel->block_size + data->offset,
1320 (off_t)(count) * channel->block_size);
1323 if (errno == EOPNOTSUPP)
1329 return EXT2_ET_UNIMPLEMENTED;
1331 #if __GNUC_PREREQ (4, 6)
1332 #pragma GCC diagnostic pop
1335 static struct struct_io_manager struct_unix_manager = {
1336 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1337 .name = "Unix I/O Manager",
1339 .close = unix_close,
1340 .set_blksize = unix_set_blksize,
1341 .read_blk = unix_read_blk,
1342 .write_blk = unix_write_blk,
1343 .flush = unix_flush,
1344 .write_byte = unix_write_byte,
1345 .set_option = unix_set_option,
1346 .get_stats = unix_get_stats,
1347 .read_blk64 = unix_read_blk64,
1348 .write_blk64 = unix_write_blk64,
1349 .discard = unix_discard,
1350 .cache_readahead = unix_cache_readahead,
1351 .zeroout = unix_zeroout,
1354 io_manager unix_io_manager = &struct_unix_manager;
1356 static struct struct_io_manager struct_unixfd_manager = {
1357 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1358 .name = "Unix fd I/O Manager",
1359 .open = unixfd_open,
1360 .close = unix_close,
1361 .set_blksize = unix_set_blksize,
1362 .read_blk = unix_read_blk,
1363 .write_blk = unix_write_blk,
1364 .flush = unix_flush,
1365 .write_byte = unix_write_byte,
1366 .set_option = unix_set_option,
1367 .get_stats = unix_get_stats,
1368 .read_blk64 = unix_read_blk64,
1369 .write_blk64 = unix_write_blk64,
1370 .discard = unix_discard,
1371 .cache_readahead = unix_cache_readahead,
1372 .zeroout = unix_zeroout,
1375 io_manager unixfd_io_manager = &struct_unixfd_manager;