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 actual = really_read;
314 if ((actual + offset) > align_size)
315 actual = align_size - offset;
318 memcpy(buf, (char *)data->bounce + offset, actual);
320 really_read += actual;
327 mutex_unlock(data, BOUNCE_MTX);
331 mutex_unlock(data, BOUNCE_MTX);
332 if (actual >= 0 && actual < size)
333 memset((char *) buf+actual, 0, size-actual);
334 if (channel->read_error)
335 retval = (channel->read_error)(channel, block, count, buf,
336 size, actual, retval);
340 #define RAW_WRITE_NO_HANDLER 1
342 static errcode_t raw_write_blk(io_channel channel,
343 struct unix_private_data *data,
344 unsigned long long block,
345 int count, const void *bufv,
349 ext2_loff_t location;
352 const unsigned char *buf = bufv;
353 unsigned long long aligned_blk;
354 int align_size, offset;
357 size = channel->block_size;
362 size = (ext2_loff_t) count * channel->block_size;
364 mutex_lock(data, STATS_MTX);
365 data->io_stats.bytes_written += size;
366 mutex_unlock(data, STATS_MTX);
368 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
370 if (data->flags & IO_FLAG_FORCE_BOUNCE)
374 /* Try an aligned pwrite */
375 if ((channel->align == 0) ||
376 (IS_ALIGNED(buf, channel->align) &&
377 IS_ALIGNED(location, channel->align) &&
378 IS_ALIGNED(size, channel->align))) {
379 actual = pwrite64(data->dev, buf, size, location);
384 /* Try an aligned pwrite */
385 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
386 ((channel->align == 0) ||
387 (IS_ALIGNED(buf, channel->align) &&
388 IS_ALIGNED(location, channel->align) &&
389 IS_ALIGNED(size, channel->align)))) {
390 actual = pwrite(data->dev, buf, size, location);
394 #endif /* HAVE_PWRITE */
396 if ((channel->align == 0) ||
397 (IS_ALIGNED(buf, channel->align) &&
398 IS_ALIGNED(location, channel->align) &&
399 IS_ALIGNED(size, channel->align))) {
400 mutex_lock(data, BOUNCE_MTX);
401 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
402 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
405 actual = write(data->dev, buf, size);
406 mutex_unlock(data, BOUNCE_MTX);
411 if (actual != size) {
413 retval = EXT2_ET_SHORT_WRITE;
420 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
421 (unsigned long) size);
424 * The buffer or size which we're trying to write isn't aligned
425 * to the O_DIRECT rules, so we need to do this the hard way...
428 if (channel->align == 0)
430 if ((channel->block_size > channel->align) &&
431 (channel->block_size % channel->align) == 0)
432 align_size = channel->block_size;
434 align_size = channel->align;
435 aligned_blk = location / align_size;
436 offset = location % align_size;
441 mutex_lock(data, BOUNCE_MTX);
442 if (size < align_size || offset) {
443 if (ext2fs_llseek(data->dev, aligned_blk * align_size,
445 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
448 actual = read(data->dev, data->bounce,
450 if (actual != align_size) {
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 mutex_unlock(data, BOUNCE_MTX);
488 if (((flags & RAW_WRITE_NO_HANDLER) == 0) && channel->write_error)
489 retval = (channel->write_error)(channel, block, count, buf,
490 size, actual, retval);
496 * Here we implement the cache functions
499 /* Allocate the cache buffers */
500 static errcode_t alloc_cache(io_channel channel,
501 struct unix_private_data *data)
504 struct unix_cache *cache;
507 data->access_time = 0;
508 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
510 cache->access_time = 0;
514 ext2fs_free_mem(&cache->buf);
515 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
519 if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
521 ext2fs_free_mem(&data->bounce);
522 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
527 /* Free the cache buffers */
528 static void free_cache(struct unix_private_data *data)
530 struct unix_cache *cache;
533 data->access_time = 0;
534 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
536 cache->access_time = 0;
540 ext2fs_free_mem(&cache->buf);
543 ext2fs_free_mem(&data->bounce);
548 * Try to find a block in the cache. If the block is not found, and
549 * eldest is a non-zero pointer, then fill in eldest with the cache
550 * entry to that should be reused.
552 static struct unix_cache *find_cached_block(struct unix_private_data *data,
553 unsigned long long block,
554 struct unix_cache **eldest)
556 struct unix_cache *cache, *unused_cache, *oldest_cache;
559 unused_cache = oldest_cache = 0;
560 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
561 if (!cache->in_use) {
563 unused_cache = cache;
566 if (cache->block == block) {
567 cache->access_time = ++data->access_time;
571 (cache->access_time < oldest_cache->access_time))
572 oldest_cache = cache;
575 *eldest = (unused_cache) ? unused_cache : oldest_cache;
580 * Reuse a particular cache entry for another block.
582 static errcode_t reuse_cache(io_channel channel,
583 struct unix_private_data *data, struct unix_cache *cache,
584 unsigned long long block)
586 if (cache->dirty && cache->in_use) {
589 retval = raw_write_blk(channel, data, cache->block, 1,
590 cache->buf, RAW_WRITE_NO_HANDLER);
592 cache->write_err = 1;
599 cache->write_err = 0;
600 cache->block = block;
601 cache->access_time = ++data->access_time;
605 #define FLUSH_INVALIDATE 0x01
606 #define FLUSH_NOLOCK 0x02
609 * Flush all of the blocks in the cache
611 static errcode_t flush_cached_blocks(io_channel channel,
612 struct unix_private_data *data,
615 struct unix_cache *cache;
616 errcode_t retval, retval2 = 0;
618 int errors_found = 0;
620 if ((flags & FLUSH_NOLOCK) == 0)
621 mutex_lock(data, CACHE_MTX);
622 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
623 if (!cache->in_use || !cache->dirty)
625 retval = raw_write_blk(channel, data,
626 cache->block, 1, cache->buf,
627 RAW_WRITE_NO_HANDLER);
629 cache->write_err = 1;
634 cache->write_err = 0;
635 if (flags & FLUSH_INVALIDATE)
639 if ((flags & FLUSH_NOLOCK) == 0)
640 mutex_unlock(data, CACHE_MTX);
642 while (errors_found) {
643 if ((flags & FLUSH_NOLOCK) == 0)
644 mutex_lock(data, CACHE_MTX);
646 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
647 if (!cache->in_use || !cache->write_err)
650 if (cache->write_err && channel->write_error) {
651 char *err_buf = NULL;
652 unsigned long long err_block = cache->block;
656 cache->write_err = 0;
657 if (io_channel_alloc_buf(channel, 0,
661 memcpy(err_buf, cache->buf,
662 channel->block_size);
663 mutex_unlock(data, CACHE_MTX);
664 (channel->write_error)(channel, err_block,
665 1, err_buf, channel->block_size, -1,
668 ext2fs_free_mem(&err_buf);
671 cache->write_err = 0;
673 if ((flags & FLUSH_NOLOCK) == 0)
674 mutex_unlock(data, CACHE_MTX);
678 #endif /* NO_IO_CACHE */
681 #ifndef BLKDISCARDZEROES
682 #define BLKDISCARDZEROES _IO(0x12,124)
686 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
689 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
690 return open64(pathname, flags, mode);
692 return open64(pathname, flags);
694 return open(pathname, flags, mode);
696 return open(pathname, flags);
700 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
702 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
703 return stat64(path, buf);
705 return stat(path, buf);
709 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
711 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
712 return fstat64(fd, buf);
714 return fstat(fd, buf);
719 static errcode_t unix_open_channel(const char *name, int fd,
720 int flags, io_channel *channel,
723 io_channel io = NULL;
724 struct unix_private_data *data = NULL;
726 ext2fs_struct_stat st;
731 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
732 flags |= IO_FLAG_FORCE_BOUNCE;
736 * We need to make sure any previous errors in the block
737 * device are thrown away, sigh.
742 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
745 memset(io, 0, sizeof(struct struct_io_channel));
746 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
747 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
751 io->manager = io_mgr;
752 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
756 strcpy(io->name, name);
757 io->private_data = data;
758 io->block_size = 1024;
764 if (safe_getenv("UNIX_IO_NOZEROOUT"))
765 io->flags |= CHANNEL_FLAGS_NOZEROOUT;
767 memset(data, 0, sizeof(struct unix_private_data));
768 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
769 data->io_stats.num_fields = 2;
773 #if defined(O_DIRECT)
774 if (flags & IO_FLAG_DIRECT_IO)
775 io->align = ext2fs_get_dio_alignment(data->dev);
776 #elif defined(F_NOCACHE)
777 if (flags & IO_FLAG_DIRECT_IO)
782 * If the device is really a block device, then set the
783 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
784 * because we are going to use punch hole instead of discard
785 * and if it succeed, subsequent read from sparse area returns
788 if (ext2fs_fstat(data->dev, &st) == 0) {
789 if (ext2fsP_is_disk_device(st.st_mode)) {
790 #ifdef BLKDISCARDZEROES
793 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
795 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
797 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
799 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
803 #if defined(__CYGWIN__)
805 * Some operating systems require that the buffers be aligned,
806 * regardless of O_DIRECT
812 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
813 if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
814 int dio_align = ext2fs_get_dio_alignment(fd);
816 if (io->align < dio_align)
817 io->align = dio_align;
821 if ((retval = alloc_cache(io, data)))
825 if (flags & IO_FLAG_RW) {
829 /* Is the block device actually writable? */
830 error = ioctl(data->dev, BLKROGET, &readonly);
831 if (!error && readonly) {
840 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
841 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
843 #define RLIM_INFINITY (~0UL)
846 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
847 * block devices are wrongly getting hit by the filesize
848 * limit. This workaround isn't perfect, since it won't work
849 * if glibc wasn't built against 2.2 header files. (Sigh.)
852 if ((flags & IO_FLAG_RW) &&
854 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
855 (ut.release[2] == '4') && (ut.release[3] == '.') &&
856 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
857 (ut.release[5] < '8')) &&
858 (ext2fs_fstat(data->dev, &st) == 0) &&
859 (ext2fsP_is_disk_device(st.st_mode))) {
862 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
863 setrlimit(RLIMIT_FSIZE, &rlim);
864 getrlimit(RLIMIT_FSIZE, &rlim);
865 if (((unsigned long) rlim.rlim_cur) <
866 ((unsigned long) rlim.rlim_max)) {
867 rlim.rlim_cur = rlim.rlim_max;
868 setrlimit(RLIMIT_FSIZE, &rlim);
873 if (flags & IO_FLAG_THREADS) {
874 io->flags |= CHANNEL_FLAGS_THREADS;
875 retval = pthread_mutex_init(&data->cache_mutex, NULL);
878 retval = pthread_mutex_init(&data->bounce_mutex, NULL);
880 pthread_mutex_destroy(&data->cache_mutex);
883 retval = pthread_mutex_init(&data->stats_mutex, NULL);
885 pthread_mutex_destroy(&data->cache_mutex);
886 pthread_mutex_destroy(&data->bounce_mutex);
899 ext2fs_free_mem(&data);
903 ext2fs_free_mem(&io->name);
905 ext2fs_free_mem(&io);
910 static errcode_t unixfd_open(const char *str_fd, int flags,
917 #if defined(HAVE_FCNTL)
918 fd_flags = fcntl(fd, F_GETFD);
923 if (fd_flags & O_RDWR)
925 if (fd_flags & O_EXCL)
926 flags |= IO_FLAG_EXCLUSIVE;
927 #if defined(O_DIRECT)
928 if (fd_flags & O_DIRECT)
929 flags |= IO_FLAG_DIRECT_IO;
931 #endif /* HAVE_FCNTL */
933 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
936 static errcode_t unix_open(const char *name, int flags,
943 return EXT2_ET_BAD_DEVICE_NAME;
945 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
946 if (flags & IO_FLAG_EXCLUSIVE)
947 open_flags |= O_EXCL;
948 #if defined(O_DIRECT)
949 if (flags & IO_FLAG_DIRECT_IO)
950 open_flags |= O_DIRECT;
952 fd = ext2fs_open_file(name, open_flags, 0);
955 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
956 if (flags & IO_FLAG_DIRECT_IO) {
957 if (fcntl(fd, F_NOCACHE, 1) < 0)
961 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
964 static errcode_t unix_close(io_channel channel)
966 struct unix_private_data *data;
967 errcode_t retval = 0;
969 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
970 data = (struct unix_private_data *) channel->private_data;
971 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
973 if (--channel->refcount > 0)
977 retval = flush_cached_blocks(channel, data, 0);
980 if (close(data->dev) < 0)
984 if (data->flags & IO_FLAG_THREADS) {
985 pthread_mutex_destroy(&data->cache_mutex);
986 pthread_mutex_destroy(&data->bounce_mutex);
987 pthread_mutex_destroy(&data->stats_mutex);
991 ext2fs_free_mem(&channel->private_data);
993 ext2fs_free_mem(&channel->name);
994 ext2fs_free_mem(&channel);
998 static errcode_t unix_set_blksize(io_channel channel, int blksize)
1000 struct unix_private_data *data;
1001 errcode_t retval = 0;
1003 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1004 data = (struct unix_private_data *) channel->private_data;
1005 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1007 if (channel->block_size != blksize) {
1008 mutex_lock(data, CACHE_MTX);
1009 mutex_lock(data, BOUNCE_MTX);
1011 if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK))){
1012 mutex_unlock(data, BOUNCE_MTX);
1013 mutex_unlock(data, CACHE_MTX);
1018 channel->block_size = blksize;
1020 retval = alloc_cache(channel, data);
1021 mutex_unlock(data, BOUNCE_MTX);
1022 mutex_unlock(data, CACHE_MTX);
1027 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
1028 int count, void *buf)
1030 struct unix_private_data *data;
1031 struct unix_cache *cache;
1036 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1037 data = (struct unix_private_data *) channel->private_data;
1038 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1041 return raw_read_blk(channel, data, block, count, buf);
1043 if (data->flags & IO_FLAG_NOCACHE)
1044 return raw_read_blk(channel, data, block, count, buf);
1046 * If we're doing an odd-sized read or a very large read,
1047 * flush out the cache and then do a direct read.
1049 if (count < 0 || count > WRITE_DIRECT_SIZE) {
1050 if ((retval = flush_cached_blocks(channel, data, 0)))
1052 return raw_read_blk(channel, data, block, count, buf);
1056 mutex_lock(data, CACHE_MTX);
1058 /* If it's in the cache, use it! */
1059 if ((cache = find_cached_block(data, block, NULL))) {
1061 printf("Using cached block %lu\n", block);
1063 memcpy(cp, cache->buf, channel->block_size);
1066 cp += channel->block_size;
1071 * Find the number of uncached blocks so we can do a
1072 * single read request
1074 for (i=1; i < count; i++)
1075 if (find_cached_block(data, block+i, NULL))
1078 printf("Reading %d blocks starting at %lu\n", i, block);
1080 mutex_unlock(data, CACHE_MTX);
1081 if ((retval = raw_read_blk(channel, data, block, i, cp)))
1083 mutex_lock(data, CACHE_MTX);
1085 /* Save the results in the cache */
1086 for (j=0; j < i; j++) {
1087 if (!find_cached_block(data, block, &cache)) {
1088 retval = reuse_cache(channel, data,
1091 goto call_write_handler;
1092 memcpy(cache->buf, cp, channel->block_size);
1096 cp += channel->block_size;
1099 mutex_unlock(data, CACHE_MTX);
1103 if (cache->write_err && channel->write_error) {
1104 char *err_buf = NULL;
1105 unsigned long long err_block = cache->block;
1109 cache->write_err = 0;
1110 if (io_channel_alloc_buf(channel, 0, &err_buf))
1113 memcpy(err_buf, cache->buf, channel->block_size);
1114 mutex_unlock(data, CACHE_MTX);
1115 (channel->write_error)(channel, err_block, 1, err_buf,
1116 channel->block_size, -1,
1119 ext2fs_free_mem(&err_buf);
1121 mutex_unlock(data, CACHE_MTX);
1123 #endif /* NO_IO_CACHE */
1126 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
1127 int count, void *buf)
1129 return unix_read_blk64(channel, block, count, buf);
1132 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
1133 int count, const void *buf)
1135 struct unix_private_data *data;
1136 struct unix_cache *cache, *reuse;
1137 errcode_t retval = 0;
1141 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1142 data = (struct unix_private_data *) channel->private_data;
1143 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1146 return raw_write_blk(channel, data, block, count, buf, 0);
1148 if (data->flags & IO_FLAG_NOCACHE)
1149 return raw_write_blk(channel, data, block, count, buf, 0);
1151 * If we're doing an odd-sized write or a very large write,
1152 * flush out the cache completely and then do a direct write.
1154 if (count < 0 || count > WRITE_DIRECT_SIZE) {
1155 if ((retval = flush_cached_blocks(channel, data,
1158 return raw_write_blk(channel, data, block, count, buf, 0);
1162 * For a moderate-sized multi-block write, first force a write
1163 * if we're in write-through cache mode, and then fill the
1164 * cache with the blocks.
1166 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
1168 retval = raw_write_blk(channel, data, block, count, buf, 0);
1171 mutex_lock(data, CACHE_MTX);
1173 cache = find_cached_block(data, block, &reuse);
1178 err = reuse_cache(channel, data, cache, block);
1180 goto call_write_handler;
1182 if (cache->buf != cp)
1183 memcpy(cache->buf, cp, channel->block_size);
1184 cache->dirty = !writethrough;
1187 cp += channel->block_size;
1189 mutex_unlock(data, CACHE_MTX);
1193 if (cache->write_err && channel->write_error) {
1194 char *err_buf = NULL;
1195 unsigned long long err_block = cache->block;
1199 cache->write_err = 0;
1200 if (io_channel_alloc_buf(channel, 0, &err_buf))
1203 memcpy(err_buf, cache->buf, channel->block_size);
1204 mutex_unlock(data, CACHE_MTX);
1205 (channel->write_error)(channel, err_block, 1, err_buf,
1206 channel->block_size, -1,
1209 ext2fs_free_mem(&err_buf);
1211 mutex_unlock(data, CACHE_MTX);
1213 #endif /* NO_IO_CACHE */
1216 static errcode_t unix_cache_readahead(io_channel channel,
1217 unsigned long long block,
1218 unsigned long long count)
1220 #ifdef POSIX_FADV_WILLNEED
1221 struct unix_private_data *data;
1223 data = (struct unix_private_data *)channel->private_data;
1224 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1225 return posix_fadvise(data->dev,
1226 (ext2_loff_t)block * channel->block_size + data->offset,
1227 (ext2_loff_t)count * channel->block_size,
1228 POSIX_FADV_WILLNEED);
1230 return EXT2_ET_OP_NOT_SUPPORTED;
1234 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
1235 int count, const void *buf)
1237 return unix_write_blk64(channel, block, count, buf);
1240 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
1241 int size, const void *buf)
1243 struct unix_private_data *data;
1244 errcode_t retval = 0;
1247 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1248 data = (struct unix_private_data *) channel->private_data;
1249 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1251 if (channel->align != 0) {
1253 printf("unix_write_byte: O_DIRECT fallback\n");
1255 return EXT2_ET_UNIMPLEMENTED;
1260 * Flush out the cache completely
1262 if ((retval = flush_cached_blocks(channel, data, FLUSH_INVALIDATE)))
1266 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
1269 actual = write(data->dev, buf, size);
1273 return EXT2_ET_SHORT_WRITE;
1279 * Flush data buffers to disk.
1281 static errcode_t unix_flush(io_channel channel)
1283 struct unix_private_data *data;
1284 errcode_t retval = 0;
1286 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1287 data = (struct unix_private_data *) channel->private_data;
1288 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1291 retval = flush_cached_blocks(channel, data, 0);
1294 if (!retval && fsync(data->dev) != 0)
1300 static errcode_t unix_set_option(io_channel channel, const char *option,
1303 struct unix_private_data *data;
1304 unsigned long long tmp;
1308 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1309 data = (struct unix_private_data *) channel->private_data;
1310 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1312 if (!strcmp(option, "offset")) {
1314 return EXT2_ET_INVALID_ARGUMENT;
1316 tmp = strtoull(arg, &end, 0);
1318 return EXT2_ET_INVALID_ARGUMENT;
1320 if (data->offset < 0)
1321 return EXT2_ET_INVALID_ARGUMENT;
1324 if (!strcmp(option, "cache")) {
1326 return EXT2_ET_INVALID_ARGUMENT;
1327 if (!strcmp(arg, "on")) {
1328 data->flags &= ~IO_FLAG_NOCACHE;
1331 if (!strcmp(arg, "off")) {
1332 retval = flush_cached_blocks(channel, data, 0);
1333 data->flags |= IO_FLAG_NOCACHE;
1336 return EXT2_ET_INVALID_ARGUMENT;
1338 return EXT2_ET_INVALID_ARGUMENT;
1341 #if defined(__linux__) && !defined(BLKDISCARD)
1342 #define BLKDISCARD _IO(0x12,119)
1345 static errcode_t unix_discard(io_channel channel, unsigned long long block,
1346 unsigned long long count)
1348 struct unix_private_data *data;
1349 int ret = EOPNOTSUPP;
1351 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1352 data = (struct unix_private_data *) channel->private_data;
1353 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1355 if (channel->flags & CHANNEL_FLAGS_NODISCARD)
1358 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1362 range[0] = (__u64)(block) * channel->block_size + data->offset;
1363 range[1] = (__u64)(count) * channel->block_size;
1365 ret = ioctl(data->dev, BLKDISCARD, &range);
1370 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1372 * If we are not on block device, try to use punch hole
1373 * to reclaim free space.
1375 ret = fallocate(data->dev,
1376 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1377 (off_t)(block) * channel->block_size + data->offset,
1378 (off_t)(count) * channel->block_size);
1384 if (errno == EOPNOTSUPP) {
1385 channel->flags |= CHANNEL_FLAGS_NODISCARD;
1392 return EXT2_ET_UNIMPLEMENTED;
1396 * If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because
1397 * ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because
1398 * it always invalidates page cache, and libext2fs requires that reads after
1399 * ZERO_RANGE return zeroes.
1401 static int __unix_zeroout(int fd, off_t offset, off_t len)
1405 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
1406 ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, offset, len);
1410 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1411 ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1420 /* parameters might not be used if OS doesn't support zeroout */
1421 #if __GNUC_PREREQ (4, 6)
1422 #pragma GCC diagnostic push
1423 #pragma GCC diagnostic ignored "-Wunused-parameter"
1425 static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1426 unsigned long long count)
1428 struct unix_private_data *data;
1431 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1432 data = (struct unix_private_data *) channel->private_data;
1433 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1435 if (!(channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE)) {
1436 /* Regular file, try to use truncate/punch/zero. */
1437 struct stat statbuf;
1442 * If we're trying to zero a range past the end of the file,
1443 * extend the file size, then truncate everything.
1445 ret = fstat(data->dev, &statbuf);
1448 if ((unsigned long long) statbuf.st_size <
1449 (block + count) * channel->block_size + data->offset) {
1450 ret = ftruncate(data->dev,
1451 (block + count) * channel->block_size + data->offset);
1457 if (channel->flags & CHANNEL_FLAGS_NOZEROOUT)
1460 ret = __unix_zeroout(data->dev,
1461 (off_t)(block) * channel->block_size + data->offset,
1462 (off_t)(count) * channel->block_size);
1465 if (errno == EOPNOTSUPP) {
1466 channel->flags |= CHANNEL_FLAGS_NOZEROOUT;
1473 return EXT2_ET_UNIMPLEMENTED;
1475 #if __GNUC_PREREQ (4, 6)
1476 #pragma GCC diagnostic pop
1479 static struct struct_io_manager struct_unix_manager = {
1480 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1481 .name = "Unix I/O Manager",
1483 .close = unix_close,
1484 .set_blksize = unix_set_blksize,
1485 .read_blk = unix_read_blk,
1486 .write_blk = unix_write_blk,
1487 .flush = unix_flush,
1488 .write_byte = unix_write_byte,
1489 .set_option = unix_set_option,
1490 .get_stats = unix_get_stats,
1491 .read_blk64 = unix_read_blk64,
1492 .write_blk64 = unix_write_blk64,
1493 .discard = unix_discard,
1494 .cache_readahead = unix_cache_readahead,
1495 .zeroout = unix_zeroout,
1498 io_manager unix_io_manager = &struct_unix_manager;
1500 static struct struct_io_manager struct_unixfd_manager = {
1501 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1502 .name = "Unix fd I/O Manager",
1503 .open = unixfd_open,
1504 .close = unix_close,
1505 .set_blksize = unix_set_blksize,
1506 .read_blk = unix_read_blk,
1507 .write_blk = unix_write_blk,
1508 .flush = unix_flush,
1509 .write_byte = unix_write_byte,
1510 .set_option = unix_set_option,
1511 .get_stats = unix_get_stats,
1512 .read_blk64 = unix_read_blk64,
1513 .write_blk64 = unix_write_blk64,
1514 .discard = unix_discard,
1515 .cache_readahead = unix_cache_readahead,
1516 .zeroout = unix_zeroout,
1519 io_manager unixfd_io_manager = &struct_unixfd_manager;