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
33 #include <sys/utsname.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
38 #ifdef HAVE_SYS_MOUNT_H
39 #include <sys/mount.h>
45 #include <sys/types.h>
47 #if HAVE_SYS_RESOURCE_H
48 #include <sys/resource.h>
51 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
52 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
55 #if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET)
56 #define BLKSSZGET _IO(0x12,104)/* get block device sector size */
65 * For checking structure magic numbers...
68 #define EXT2_CHECK_MAGIC(struct, code) \
69 if ((struct)->magic != (code)) return (code)
80 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
81 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
83 struct unix_private_data {
90 struct unix_cache cache[CACHE_SIZE];
92 struct struct_io_stats io_stats;
95 #define IS_ALIGNED(n, align) ((((unsigned long) n) & \
96 ((unsigned long) ((align)-1))) == 0)
98 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
99 static errcode_t unix_close(io_channel channel);
100 static errcode_t unix_set_blksize(io_channel channel, int blksize);
101 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
102 int count, void *data);
103 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
104 int count, const void *data);
105 static errcode_t unix_flush(io_channel channel);
106 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
107 int size, const void *data);
108 static errcode_t unix_set_option(io_channel channel, const char *option,
110 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
112 static void reuse_cache(io_channel channel, struct unix_private_data *data,
113 struct unix_cache *cache, unsigned long long block);
114 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
115 int count, void *data);
116 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
117 int count, const void *data);
118 static errcode_t unix_discard(io_channel channel, unsigned long long block,
119 unsigned long long count);
121 static struct struct_io_manager struct_unix_manager = {
122 EXT2_ET_MAGIC_IO_MANAGER,
138 io_manager unix_io_manager = &struct_unix_manager;
140 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
142 errcode_t retval = 0;
144 struct unix_private_data *data;
146 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
147 data = (struct unix_private_data *) channel->private_data;
148 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
151 *stats = &data->io_stats;
157 * Here are the raw I/O functions
159 static errcode_t raw_read_blk(io_channel channel,
160 struct unix_private_data *data,
161 unsigned long long block,
162 int count, void *buf)
166 ext2_loff_t location;
169 size = (count < 0) ? -count : count * channel->block_size;
170 data->io_stats.bytes_read += size;
171 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
172 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
173 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
176 if ((data->align == 0) ||
177 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
178 actual = read(data->dev, buf, size);
179 if (actual != size) {
183 retval = EXT2_ET_SHORT_READ;
190 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
191 (unsigned long) size);
195 * The buffer or size which we're trying to read isn't aligned
196 * to the O_DIRECT rules, so we need to do this the hard way...
199 actual = read(data->dev, data->bounce, channel->block_size);
200 if (actual != channel->block_size)
203 if (size > channel->block_size)
204 actual = channel->block_size;
205 memcpy(buf, data->bounce, actual);
212 memset((char *) buf+actual, 0, size-actual);
213 if (channel->read_error)
214 retval = (channel->read_error)(channel, block, count, buf,
215 size, actual, retval);
219 static errcode_t raw_write_blk(io_channel channel,
220 struct unix_private_data *data,
221 unsigned long long block,
222 int count, const void *buf)
225 ext2_loff_t location;
230 size = channel->block_size;
235 size = count * channel->block_size;
237 data->io_stats.bytes_written += size;
239 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
240 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
241 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
245 if ((data->align == 0) ||
246 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
247 actual = write(data->dev, buf, size);
248 if (actual != size) {
250 retval = EXT2_ET_SHORT_WRITE;
257 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
258 (unsigned long) size);
261 * The buffer or size which we're trying to write isn't aligned
262 * to the O_DIRECT rules, so we need to do this the hard way...
265 if (size < channel->block_size) {
266 actual = read(data->dev, data->bounce,
267 channel->block_size);
268 if (actual != channel->block_size) {
269 retval = EXT2_ET_SHORT_READ;
274 if (size > channel->block_size)
275 actual = channel->block_size;
276 memcpy(data->bounce, buf, actual);
277 actual = write(data->dev, data->bounce, channel->block_size);
278 if (actual != channel->block_size)
286 if (channel->write_error)
287 retval = (channel->write_error)(channel, block, count, buf,
288 size, actual, retval);
294 * Here we implement the cache functions
297 /* Allocate the cache buffers */
298 static errcode_t alloc_cache(io_channel channel,
299 struct unix_private_data *data)
302 struct unix_cache *cache;
305 data->access_time = 0;
306 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
308 cache->access_time = 0;
312 ext2fs_free_mem(&cache->buf);
313 retval = ext2fs_get_memalign(channel->block_size,
314 data->align, &cache->buf);
320 ext2fs_free_mem(&data->bounce);
321 retval = ext2fs_get_memalign(channel->block_size, data->align,
327 /* Free the cache buffers */
328 static void free_cache(struct unix_private_data *data)
330 struct unix_cache *cache;
333 data->access_time = 0;
334 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
336 cache->access_time = 0;
340 ext2fs_free_mem(&cache->buf);
343 ext2fs_free_mem(&data->bounce);
348 * Try to find a block in the cache. If the block is not found, and
349 * eldest is a non-zero pointer, then fill in eldest with the cache
350 * entry to that should be reused.
352 static struct unix_cache *find_cached_block(struct unix_private_data *data,
353 unsigned long long block,
354 struct unix_cache **eldest)
356 struct unix_cache *cache, *unused_cache, *oldest_cache;
359 unused_cache = oldest_cache = 0;
360 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
361 if (!cache->in_use) {
363 unused_cache = cache;
366 if (cache->block == block) {
367 cache->access_time = ++data->access_time;
371 (cache->access_time < oldest_cache->access_time))
372 oldest_cache = cache;
375 *eldest = (unused_cache) ? unused_cache : oldest_cache;
380 * Reuse a particular cache entry for another block.
382 static void reuse_cache(io_channel channel, struct unix_private_data *data,
383 struct unix_cache *cache, unsigned long long block)
385 if (cache->dirty && cache->in_use)
386 raw_write_blk(channel, data, cache->block, 1, cache->buf);
390 cache->block = block;
391 cache->access_time = ++data->access_time;
395 * Flush all of the blocks in the cache
397 static errcode_t flush_cached_blocks(io_channel channel,
398 struct unix_private_data *data,
402 struct unix_cache *cache;
403 errcode_t retval, retval2;
407 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
417 retval = raw_write_blk(channel, data,
418 cache->block, 1, cache->buf);
426 #endif /* NO_IO_CACHE */
429 #ifndef BLKDISCARDZEROES
430 #define BLKDISCARDZEROES _IO(0x12,124)
434 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
436 io_channel io = NULL;
437 struct unix_private_data *data = NULL;
439 int open_flags, zeroes = 0;
446 return EXT2_ET_BAD_DEVICE_NAME;
447 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
450 memset(io, 0, sizeof(struct struct_io_channel));
451 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
452 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
456 io->manager = unix_io_manager;
457 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
461 strcpy(io->name, name);
462 io->private_data = data;
463 io->block_size = 1024;
468 memset(data, 0, sizeof(struct unix_private_data));
469 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
470 data->io_stats.num_fields = 2;
472 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
473 if (flags & IO_FLAG_EXCLUSIVE)
474 open_flags |= O_EXCL;
475 if (flags & IO_FLAG_DIRECT_IO)
476 open_flags |= O_DIRECT;
480 data->dev = open64(io->name, open_flags);
482 data->dev = open(io->name, open_flags);
490 if (flags & IO_FLAG_DIRECT_IO) {
491 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
492 data->align = io->block_size;
496 #ifdef BLKDISCARDZEROES
497 ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
499 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
502 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
504 * Some operating systems require that the buffers be aligned,
505 * regardless of O_DIRECT
511 if ((retval = alloc_cache(io, data)))
515 if (flags & IO_FLAG_RW) {
519 /* Is the block device actually writable? */
520 error = ioctl(data->dev, BLKROGET, &readonly);
521 if (!error && readonly) {
531 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
532 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
534 #define RLIM_INFINITY (~0UL)
537 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
538 * block devices are wrongly getting hit by the filesize
539 * limit. This workaround isn't perfect, since it won't work
540 * if glibc wasn't built against 2.2 header files. (Sigh.)
543 if ((flags & IO_FLAG_RW) &&
545 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
546 (ut.release[2] == '4') && (ut.release[3] == '.') &&
547 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
548 (ut.release[5] < '8')) &&
549 (fstat(data->dev, &st) == 0) &&
550 (S_ISBLK(st.st_mode))) {
553 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
554 setrlimit(RLIMIT_FSIZE, &rlim);
555 getrlimit(RLIMIT_FSIZE, &rlim);
556 if (((unsigned long) rlim.rlim_cur) <
557 ((unsigned long) rlim.rlim_max)) {
558 rlim.rlim_cur = rlim.rlim_max;
559 setrlimit(RLIMIT_FSIZE, &rlim);
569 ext2fs_free_mem(&data);
572 ext2fs_free_mem(&io);
576 static errcode_t unix_close(io_channel channel)
578 struct unix_private_data *data;
579 errcode_t retval = 0;
581 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
582 data = (struct unix_private_data *) channel->private_data;
583 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
585 if (--channel->refcount > 0)
589 retval = flush_cached_blocks(channel, data, 0);
592 if (close(data->dev) < 0)
596 ext2fs_free_mem(&channel->private_data);
598 ext2fs_free_mem(&channel->name);
599 ext2fs_free_mem(&channel);
603 static errcode_t unix_set_blksize(io_channel channel, int blksize)
605 struct unix_private_data *data;
608 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
609 data = (struct unix_private_data *) channel->private_data;
610 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
612 if (channel->block_size != blksize) {
614 if ((retval = flush_cached_blocks(channel, data, 0)))
618 channel->block_size = blksize;
620 if ((retval = alloc_cache(channel, data)))
627 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
628 int count, void *buf)
630 struct unix_private_data *data;
631 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
636 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
637 data = (struct unix_private_data *) channel->private_data;
638 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
641 return raw_read_blk(channel, data, block, count, buf);
644 * If we're doing an odd-sized read or a very large read,
645 * flush out the cache and then do a direct read.
647 if (count < 0 || count > WRITE_DIRECT_SIZE) {
648 if ((retval = flush_cached_blocks(channel, data, 0)))
650 return raw_read_blk(channel, data, block, count, buf);
655 /* If it's in the cache, use it! */
656 if ((cache = find_cached_block(data, block, &reuse[0]))) {
658 printf("Using cached block %lu\n", block);
660 memcpy(cp, cache->buf, channel->block_size);
663 cp += channel->block_size;
668 * Special case where we read directly into the
669 * cache buffer; important in the O_DIRECT case
672 reuse_cache(channel, data, cache, block);
673 if ((retval = raw_read_blk(channel, data, block, 1,
678 memcpy(cp, cache->buf, channel->block_size);
683 * Find the number of uncached blocks so we can do a
684 * single read request
686 for (i=1; i < count; i++)
687 if (find_cached_block(data, block+i, &reuse[i]))
690 printf("Reading %d blocks starting at %lu\n", i, block);
692 if ((retval = raw_read_blk(channel, data, block, i, cp)))
695 /* Save the results in the cache */
696 for (j=0; j < i; j++) {
699 reuse_cache(channel, data, cache, block++);
700 memcpy(cache->buf, cp, channel->block_size);
701 cp += channel->block_size;
705 #endif /* NO_IO_CACHE */
708 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
709 int count, void *buf)
711 return unix_read_blk64(channel, block, count, buf);
714 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
715 int count, const void *buf)
717 struct unix_private_data *data;
718 struct unix_cache *cache, *reuse;
719 errcode_t retval = 0;
723 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
724 data = (struct unix_private_data *) channel->private_data;
725 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
728 return raw_write_blk(channel, data, block, count, buf);
731 * If we're doing an odd-sized write or a very large write,
732 * flush out the cache completely and then do a direct write.
734 if (count < 0 || count > WRITE_DIRECT_SIZE) {
735 if ((retval = flush_cached_blocks(channel, data, 1)))
737 return raw_write_blk(channel, data, block, count, buf);
741 * For a moderate-sized multi-block write, first force a write
742 * if we're in write-through cache mode, and then fill the
743 * cache with the blocks.
745 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
747 retval = raw_write_blk(channel, data, block, count, buf);
751 cache = find_cached_block(data, block, &reuse);
754 reuse_cache(channel, data, cache, block);
756 memcpy(cache->buf, cp, channel->block_size);
757 cache->dirty = !writethrough;
760 cp += channel->block_size;
763 #endif /* NO_IO_CACHE */
766 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
767 int count, const void *buf)
769 return unix_write_blk64(channel, block, count, buf);
772 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
773 int size, const void *buf)
775 struct unix_private_data *data;
776 errcode_t retval = 0;
779 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
780 data = (struct unix_private_data *) channel->private_data;
781 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
783 if (data->align != 0) {
785 printf("unix_write_byte: O_DIRECT fallback\n");
787 return EXT2_ET_UNIMPLEMENTED;
792 * Flush out the cache completely
794 if ((retval = flush_cached_blocks(channel, data, 1)))
798 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
801 actual = write(data->dev, buf, size);
803 return EXT2_ET_SHORT_WRITE;
809 * Flush data buffers to disk.
811 static errcode_t unix_flush(io_channel channel)
813 struct unix_private_data *data;
814 errcode_t retval = 0;
816 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
817 data = (struct unix_private_data *) channel->private_data;
818 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
821 retval = flush_cached_blocks(channel, data, 0);
827 static errcode_t unix_set_option(io_channel channel, const char *option,
830 struct unix_private_data *data;
831 unsigned long long tmp;
834 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
835 data = (struct unix_private_data *) channel->private_data;
836 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
838 if (!strcmp(option, "offset")) {
840 return EXT2_ET_INVALID_ARGUMENT;
842 tmp = strtoull(arg, &end, 0);
844 return EXT2_ET_INVALID_ARGUMENT;
846 if (data->offset < 0)
847 return EXT2_ET_INVALID_ARGUMENT;
850 return EXT2_ET_INVALID_ARGUMENT;
853 #if defined(__linux__) && !defined(BLKDISCARD)
854 #define BLKDISCARD _IO(0x12,119)
857 static errcode_t unix_discard(io_channel channel, unsigned long long block,
858 unsigned long long count)
861 struct unix_private_data *data;
865 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
866 data = (struct unix_private_data *) channel->private_data;
867 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
869 range[0] = (__uint64_t)(block) * channel->block_size;
870 range[1] = (__uint64_t)(count) * channel->block_size;
872 ret = ioctl(data->dev, BLKDISCARD, &range);
877 return EXT2_ET_UNIMPLEMENTED;