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 #define _LARGEFILE_SOURCE
19 #define _LARGEFILE64_SOURCE
35 #include <sys/utsname.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
40 #ifdef HAVE_SYS_MOUNT_H
41 #include <sys/mount.h>
47 #include <sys/types.h>
49 #if HAVE_SYS_RESOURCE_H
50 #include <sys/resource.h>
53 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
54 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
57 #if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET)
58 #define BLKSSZGET _IO(0x12,104)/* get block device sector size */
67 * For checking structure magic numbers...
70 #define EXT2_CHECK_MAGIC(struct, code) \
71 if ((struct)->magic != (code)) return (code)
82 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
83 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
85 struct unix_private_data
{
92 struct unix_cache cache
[CACHE_SIZE
];
94 struct struct_io_stats io_stats
;
97 #define IS_ALIGNED(n, align) ((((unsigned long) n) & \
98 ((unsigned long) ((align)-1))) == 0)
100 static errcode_t
unix_open(const char *name
, int flags
, io_channel
*channel
);
101 static errcode_t
unix_close(io_channel channel
);
102 static errcode_t
unix_set_blksize(io_channel channel
, int blksize
);
103 static errcode_t
unix_read_blk(io_channel channel
, unsigned long block
,
104 int count
, void *data
);
105 static errcode_t
unix_write_blk(io_channel channel
, unsigned long block
,
106 int count
, const void *data
);
107 static errcode_t
unix_flush(io_channel channel
);
108 static errcode_t
unix_write_byte(io_channel channel
, unsigned long offset
,
109 int size
, const void *data
);
110 static errcode_t
unix_set_option(io_channel channel
, const char *option
,
112 static errcode_t
unix_get_stats(io_channel channel
, io_stats
*stats
)
114 static void reuse_cache(io_channel channel
, struct unix_private_data
*data
,
115 struct unix_cache
*cache
, unsigned long long block
);
116 static errcode_t
unix_read_blk64(io_channel channel
, unsigned long long block
,
117 int count
, void *data
);
118 static errcode_t
unix_write_blk64(io_channel channel
, unsigned long long block
,
119 int count
, const void *data
);
120 static errcode_t
unix_discard(io_channel channel
, unsigned long long block
,
121 unsigned long long count
);
123 static struct struct_io_manager struct_unix_manager
= {
124 EXT2_ET_MAGIC_IO_MANAGER
,
140 io_manager unix_io_manager
= &struct_unix_manager
;
142 static errcode_t
unix_get_stats(io_channel channel
, io_stats
*stats
)
144 errcode_t retval
= 0;
146 struct unix_private_data
*data
;
148 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
149 data
= (struct unix_private_data
*) channel
->private_data
;
150 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
153 *stats
= &data
->io_stats
;
159 * Here are the raw I/O functions
161 static errcode_t
raw_read_blk(io_channel channel
,
162 struct unix_private_data
*data
,
163 unsigned long long block
,
164 int count
, void *buf
)
168 ext2_loff_t location
;
171 size
= (count
< 0) ? -count
: count
* channel
->block_size
;
172 data
->io_stats
.bytes_read
+= size
;
173 location
= ((ext2_loff_t
) block
* channel
->block_size
) + data
->offset
;
174 if (ext2fs_llseek(data
->dev
, location
, SEEK_SET
) != location
) {
175 retval
= errno
? errno
: EXT2_ET_LLSEEK_FAILED
;
178 if ((data
->align
== 0) ||
179 ((IS_ALIGNED(buf
, data
->align
)) && IS_ALIGNED(size
, data
->align
))) {
180 actual
= read(data
->dev
, buf
, size
);
181 if (actual
!= size
) {
185 retval
= EXT2_ET_SHORT_READ
;
192 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf
,
193 (unsigned long) size
);
197 * The buffer or size which we're trying to read isn't aligned
198 * to the O_DIRECT rules, so we need to do this the hard way...
201 actual
= read(data
->dev
, data
->bounce
, channel
->block_size
);
202 if (actual
!= channel
->block_size
)
205 if (size
> channel
->block_size
)
206 actual
= channel
->block_size
;
207 memcpy(buf
, data
->bounce
, actual
);
214 memset((char *) buf
+actual
, 0, size
-actual
);
215 if (channel
->read_error
)
216 retval
= (channel
->read_error
)(channel
, block
, count
, buf
,
217 size
, actual
, retval
);
221 static errcode_t
raw_write_blk(io_channel channel
,
222 struct unix_private_data
*data
,
223 unsigned long long block
,
224 int count
, const void *buf
)
227 ext2_loff_t location
;
232 size
= channel
->block_size
;
237 size
= count
* channel
->block_size
;
239 data
->io_stats
.bytes_written
+= size
;
241 location
= ((ext2_loff_t
) block
* channel
->block_size
) + data
->offset
;
242 if (ext2fs_llseek(data
->dev
, location
, SEEK_SET
) != location
) {
243 retval
= errno
? errno
: EXT2_ET_LLSEEK_FAILED
;
247 if ((data
->align
== 0) ||
248 ((IS_ALIGNED(buf
, data
->align
)) && IS_ALIGNED(size
, data
->align
))) {
249 actual
= write(data
->dev
, buf
, size
);
250 if (actual
!= size
) {
252 retval
= EXT2_ET_SHORT_WRITE
;
259 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf
,
260 (unsigned long) size
);
263 * The buffer or size which we're trying to write isn't aligned
264 * to the O_DIRECT rules, so we need to do this the hard way...
267 if (size
< channel
->block_size
) {
268 actual
= read(data
->dev
, data
->bounce
,
269 channel
->block_size
);
270 if (actual
!= channel
->block_size
) {
271 retval
= EXT2_ET_SHORT_READ
;
276 if (size
> channel
->block_size
)
277 actual
= channel
->block_size
;
278 memcpy(data
->bounce
, buf
, actual
);
279 actual
= write(data
->dev
, data
->bounce
, channel
->block_size
);
280 if (actual
!= channel
->block_size
)
288 if (channel
->write_error
)
289 retval
= (channel
->write_error
)(channel
, block
, count
, buf
,
290 size
, actual
, retval
);
296 * Here we implement the cache functions
299 /* Allocate the cache buffers */
300 static errcode_t
alloc_cache(io_channel channel
,
301 struct unix_private_data
*data
)
304 struct unix_cache
*cache
;
307 data
->access_time
= 0;
308 for (i
=0, cache
= data
->cache
; i
< CACHE_SIZE
; i
++, cache
++) {
310 cache
->access_time
= 0;
314 ext2fs_free_mem(&cache
->buf
);
315 retval
= ext2fs_get_memalign(channel
->block_size
,
316 data
->align
, &cache
->buf
);
322 ext2fs_free_mem(&data
->bounce
);
323 retval
= ext2fs_get_memalign(channel
->block_size
, data
->align
,
329 /* Free the cache buffers */
330 static void free_cache(struct unix_private_data
*data
)
332 struct unix_cache
*cache
;
335 data
->access_time
= 0;
336 for (i
=0, cache
= data
->cache
; i
< CACHE_SIZE
; i
++, cache
++) {
338 cache
->access_time
= 0;
342 ext2fs_free_mem(&cache
->buf
);
345 ext2fs_free_mem(&data
->bounce
);
350 * Try to find a block in the cache. If the block is not found, and
351 * eldest is a non-zero pointer, then fill in eldest with the cache
352 * entry to that should be reused.
354 static struct unix_cache
*find_cached_block(struct unix_private_data
*data
,
355 unsigned long long block
,
356 struct unix_cache
**eldest
)
358 struct unix_cache
*cache
, *unused_cache
, *oldest_cache
;
361 unused_cache
= oldest_cache
= 0;
362 for (i
=0, cache
= data
->cache
; i
< CACHE_SIZE
; i
++, cache
++) {
363 if (!cache
->in_use
) {
365 unused_cache
= cache
;
368 if (cache
->block
== block
) {
369 cache
->access_time
= ++data
->access_time
;
373 (cache
->access_time
< oldest_cache
->access_time
))
374 oldest_cache
= cache
;
377 *eldest
= (unused_cache
) ? unused_cache
: oldest_cache
;
382 * Reuse a particular cache entry for another block.
384 static void reuse_cache(io_channel channel
, struct unix_private_data
*data
,
385 struct unix_cache
*cache
, unsigned long long block
)
387 if (cache
->dirty
&& cache
->in_use
)
388 raw_write_blk(channel
, data
, cache
->block
, 1, cache
->buf
);
392 cache
->block
= block
;
393 cache
->access_time
= ++data
->access_time
;
397 * Flush all of the blocks in the cache
399 static errcode_t
flush_cached_blocks(io_channel channel
,
400 struct unix_private_data
*data
,
404 struct unix_cache
*cache
;
405 errcode_t retval
, retval2
;
409 for (i
=0, cache
= data
->cache
; i
< CACHE_SIZE
; i
++, cache
++) {
419 retval
= raw_write_blk(channel
, data
,
420 cache
->block
, 1, cache
->buf
);
428 #endif /* NO_IO_CACHE */
431 #ifndef BLKDISCARDZEROES
432 #define BLKDISCARDZEROES _IO(0x12,124)
436 static errcode_t
unix_open(const char *name
, int flags
, io_channel
*channel
)
438 io_channel io
= NULL
;
439 struct unix_private_data
*data
= NULL
;
441 int open_flags
, zeroes
= 0;
448 return EXT2_ET_BAD_DEVICE_NAME
;
449 retval
= ext2fs_get_mem(sizeof(struct struct_io_channel
), &io
);
452 memset(io
, 0, sizeof(struct struct_io_channel
));
453 io
->magic
= EXT2_ET_MAGIC_IO_CHANNEL
;
454 retval
= ext2fs_get_mem(sizeof(struct unix_private_data
), &data
);
458 io
->manager
= unix_io_manager
;
459 retval
= ext2fs_get_mem(strlen(name
)+1, &io
->name
);
463 strcpy(io
->name
, name
);
464 io
->private_data
= data
;
465 io
->block_size
= 1024;
470 memset(data
, 0, sizeof(struct unix_private_data
));
471 data
->magic
= EXT2_ET_MAGIC_UNIX_IO_CHANNEL
;
472 data
->io_stats
.num_fields
= 2;
474 open_flags
= (flags
& IO_FLAG_RW
) ? O_RDWR
: O_RDONLY
;
475 if (flags
& IO_FLAG_EXCLUSIVE
)
476 open_flags
|= O_EXCL
;
477 if (flags
& IO_FLAG_DIRECT_IO
)
478 open_flags
|= O_DIRECT
;
482 data
->dev
= open64(io
->name
, open_flags
);
484 data
->dev
= open(io
->name
, open_flags
);
492 if (flags
& IO_FLAG_DIRECT_IO
) {
493 if (ioctl(data
->dev
, BLKSSZGET
, &data
->align
) != 0)
494 data
->align
= io
->block_size
;
498 #ifdef BLKDISCARDZEROES
499 ioctl(data
->dev
, BLKDISCARDZEROES
, &zeroes
);
501 io
->flags
|= CHANNEL_FLAGS_DISCARD_ZEROES
;
504 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
506 * Some operating systems require that the buffers be aligned,
507 * regardless of O_DIRECT
513 if ((retval
= alloc_cache(io
, data
)))
517 if (flags
& IO_FLAG_RW
) {
521 /* Is the block device actually writable? */
522 error
= ioctl(data
->dev
, BLKROGET
, &readonly
);
523 if (!error
&& readonly
) {
533 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
534 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
536 #define RLIM_INFINITY (~0UL)
539 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
540 * block devices are wrongly getting hit by the filesize
541 * limit. This workaround isn't perfect, since it won't work
542 * if glibc wasn't built against 2.2 header files. (Sigh.)
545 if ((flags
& IO_FLAG_RW
) &&
547 ((ut
.release
[0] == '2') && (ut
.release
[1] == '.') &&
548 (ut
.release
[2] == '4') && (ut
.release
[3] == '.') &&
549 (ut
.release
[4] == '1') && (ut
.release
[5] >= '0') &&
550 (ut
.release
[5] < '8')) &&
551 (fstat(data
->dev
, &st
) == 0) &&
552 (S_ISBLK(st
.st_mode
))) {
555 rlim
.rlim_cur
= rlim
.rlim_max
= (unsigned long) RLIM_INFINITY
;
556 setrlimit(RLIMIT_FSIZE
, &rlim
);
557 getrlimit(RLIMIT_FSIZE
, &rlim
);
558 if (((unsigned long) rlim
.rlim_cur
) <
559 ((unsigned long) rlim
.rlim_max
)) {
560 rlim
.rlim_cur
= rlim
.rlim_max
;
561 setrlimit(RLIMIT_FSIZE
, &rlim
);
571 ext2fs_free_mem(&data
);
574 ext2fs_free_mem(&io
);
578 static errcode_t
unix_close(io_channel channel
)
580 struct unix_private_data
*data
;
581 errcode_t retval
= 0;
583 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
584 data
= (struct unix_private_data
*) channel
->private_data
;
585 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
587 if (--channel
->refcount
> 0)
591 retval
= flush_cached_blocks(channel
, data
, 0);
594 if (close(data
->dev
) < 0)
598 ext2fs_free_mem(&channel
->private_data
);
600 ext2fs_free_mem(&channel
->name
);
601 ext2fs_free_mem(&channel
);
605 static errcode_t
unix_set_blksize(io_channel channel
, int blksize
)
607 struct unix_private_data
*data
;
610 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
611 data
= (struct unix_private_data
*) channel
->private_data
;
612 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
614 if (channel
->block_size
!= blksize
) {
616 if ((retval
= flush_cached_blocks(channel
, data
, 0)))
620 channel
->block_size
= blksize
;
622 if ((retval
= alloc_cache(channel
, data
)))
629 static errcode_t
unix_read_blk64(io_channel channel
, unsigned long long block
,
630 int count
, void *buf
)
632 struct unix_private_data
*data
;
633 struct unix_cache
*cache
, *reuse
[READ_DIRECT_SIZE
];
638 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
639 data
= (struct unix_private_data
*) channel
->private_data
;
640 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
643 return raw_read_blk(channel
, data
, block
, count
, buf
);
646 * If we're doing an odd-sized read or a very large read,
647 * flush out the cache and then do a direct read.
649 if (count
< 0 || count
> WRITE_DIRECT_SIZE
) {
650 if ((retval
= flush_cached_blocks(channel
, data
, 0)))
652 return raw_read_blk(channel
, data
, block
, count
, buf
);
657 /* If it's in the cache, use it! */
658 if ((cache
= find_cached_block(data
, block
, &reuse
[0]))) {
660 printf("Using cached block %lu\n", block
);
662 memcpy(cp
, cache
->buf
, channel
->block_size
);
665 cp
+= channel
->block_size
;
670 * Special case where we read directly into the
671 * cache buffer; important in the O_DIRECT case
674 reuse_cache(channel
, data
, cache
, block
);
675 if ((retval
= raw_read_blk(channel
, data
, block
, 1,
680 memcpy(cp
, cache
->buf
, channel
->block_size
);
685 * Find the number of uncached blocks so we can do a
686 * single read request
688 for (i
=1; i
< count
; i
++)
689 if (find_cached_block(data
, block
+i
, &reuse
[i
]))
692 printf("Reading %d blocks starting at %lu\n", i
, block
);
694 if ((retval
= raw_read_blk(channel
, data
, block
, i
, cp
)))
697 /* Save the results in the cache */
698 for (j
=0; j
< i
; j
++) {
701 reuse_cache(channel
, data
, cache
, block
++);
702 memcpy(cache
->buf
, cp
, channel
->block_size
);
703 cp
+= channel
->block_size
;
707 #endif /* NO_IO_CACHE */
710 static errcode_t
unix_read_blk(io_channel channel
, unsigned long block
,
711 int count
, void *buf
)
713 return unix_read_blk64(channel
, block
, count
, buf
);
716 static errcode_t
unix_write_blk64(io_channel channel
, unsigned long long block
,
717 int count
, const void *buf
)
719 struct unix_private_data
*data
;
720 struct unix_cache
*cache
, *reuse
;
721 errcode_t retval
= 0;
725 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
726 data
= (struct unix_private_data
*) channel
->private_data
;
727 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
730 return raw_write_blk(channel
, data
, block
, count
, buf
);
733 * If we're doing an odd-sized write or a very large write,
734 * flush out the cache completely and then do a direct write.
736 if (count
< 0 || count
> WRITE_DIRECT_SIZE
) {
737 if ((retval
= flush_cached_blocks(channel
, data
, 1)))
739 return raw_write_blk(channel
, data
, block
, count
, buf
);
743 * For a moderate-sized multi-block write, first force a write
744 * if we're in write-through cache mode, and then fill the
745 * cache with the blocks.
747 writethrough
= channel
->flags
& CHANNEL_FLAGS_WRITETHROUGH
;
749 retval
= raw_write_blk(channel
, data
, block
, count
, buf
);
753 cache
= find_cached_block(data
, block
, &reuse
);
756 reuse_cache(channel
, data
, cache
, block
);
758 memcpy(cache
->buf
, cp
, channel
->block_size
);
759 cache
->dirty
= !writethrough
;
762 cp
+= channel
->block_size
;
765 #endif /* NO_IO_CACHE */
768 static errcode_t
unix_write_blk(io_channel channel
, unsigned long block
,
769 int count
, const void *buf
)
771 return unix_write_blk64(channel
, block
, count
, buf
);
774 static errcode_t
unix_write_byte(io_channel channel
, unsigned long offset
,
775 int size
, const void *buf
)
777 struct unix_private_data
*data
;
778 errcode_t retval
= 0;
781 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
782 data
= (struct unix_private_data
*) channel
->private_data
;
783 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
785 if (data
->align
!= 0) {
787 printf("unix_write_byte: O_DIRECT fallback\n");
789 return EXT2_ET_UNIMPLEMENTED
;
794 * Flush out the cache completely
796 if ((retval
= flush_cached_blocks(channel
, data
, 1)))
800 if (lseek(data
->dev
, offset
+ data
->offset
, SEEK_SET
) < 0)
803 actual
= write(data
->dev
, buf
, size
);
805 return EXT2_ET_SHORT_WRITE
;
811 * Flush data buffers to disk.
813 static errcode_t
unix_flush(io_channel channel
)
815 struct unix_private_data
*data
;
816 errcode_t retval
= 0;
818 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
819 data
= (struct unix_private_data
*) channel
->private_data
;
820 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
823 retval
= flush_cached_blocks(channel
, data
, 0);
829 static errcode_t
unix_set_option(io_channel channel
, const char *option
,
832 struct unix_private_data
*data
;
833 unsigned long long tmp
;
836 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
837 data
= (struct unix_private_data
*) channel
->private_data
;
838 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
840 if (!strcmp(option
, "offset")) {
842 return EXT2_ET_INVALID_ARGUMENT
;
844 tmp
= strtoull(arg
, &end
, 0);
846 return EXT2_ET_INVALID_ARGUMENT
;
848 if (data
->offset
< 0)
849 return EXT2_ET_INVALID_ARGUMENT
;
852 return EXT2_ET_INVALID_ARGUMENT
;
855 #if defined(__linux__) && !defined(BLKDISCARD)
856 #define BLKDISCARD _IO(0x12,119)
859 static errcode_t
unix_discard(io_channel channel
, unsigned long long block
,
860 unsigned long long count
)
863 struct unix_private_data
*data
;
867 EXT2_CHECK_MAGIC(channel
, EXT2_ET_MAGIC_IO_CHANNEL
);
868 data
= (struct unix_private_data
*) channel
->private_data
;
869 EXT2_CHECK_MAGIC(data
, EXT2_ET_MAGIC_UNIX_IO_CHANNEL
);
871 range
[0] = (__uint64_t
)(block
) * channel
->block_size
;
872 range
[1] = (__uint64_t
)(count
) * channel
->block_size
;
874 ret
= ioctl(data
->dev
, BLKDISCARD
, &range
);
879 return EXT2_ET_UNIMPLEMENTED
;