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 */
428 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
430 io_channel io = NULL;
431 struct unix_private_data *data = NULL;
440 return EXT2_ET_BAD_DEVICE_NAME;
441 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
444 memset(io, 0, sizeof(struct struct_io_channel));
445 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
446 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
450 io->manager = unix_io_manager;
451 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
455 strcpy(io->name, name);
456 io->private_data = data;
457 io->block_size = 1024;
462 memset(data, 0, sizeof(struct unix_private_data));
463 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
464 data->io_stats.num_fields = 2;
466 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
467 if (flags & IO_FLAG_EXCLUSIVE)
468 open_flags |= O_EXCL;
469 if (flags & IO_FLAG_DIRECT_IO)
470 open_flags |= O_DIRECT;
474 data->dev = open64(io->name, open_flags);
476 data->dev = open(io->name, open_flags);
484 if (flags & IO_FLAG_DIRECT_IO) {
485 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
486 data->align = io->block_size;
490 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
492 * Some operating systems require that the buffers be aligned,
493 * regardless of O_DIRECT
499 if ((retval = alloc_cache(io, data)))
503 if (flags & IO_FLAG_RW) {
507 /* Is the block device actually writable? */
508 error = ioctl(data->dev, BLKROGET, &readonly);
509 if (!error && readonly) {
519 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
520 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
522 #define RLIM_INFINITY (~0UL)
525 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
526 * block devices are wrongly getting hit by the filesize
527 * limit. This workaround isn't perfect, since it won't work
528 * if glibc wasn't built against 2.2 header files. (Sigh.)
531 if ((flags & IO_FLAG_RW) &&
533 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
534 (ut.release[2] == '4') && (ut.release[3] == '.') &&
535 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
536 (ut.release[5] < '8')) &&
537 (fstat(data->dev, &st) == 0) &&
538 (S_ISBLK(st.st_mode))) {
541 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
542 setrlimit(RLIMIT_FSIZE, &rlim);
543 getrlimit(RLIMIT_FSIZE, &rlim);
544 if (((unsigned long) rlim.rlim_cur) <
545 ((unsigned long) rlim.rlim_max)) {
546 rlim.rlim_cur = rlim.rlim_max;
547 setrlimit(RLIMIT_FSIZE, &rlim);
557 ext2fs_free_mem(&data);
560 ext2fs_free_mem(&io);
564 static errcode_t unix_close(io_channel channel)
566 struct unix_private_data *data;
567 errcode_t retval = 0;
569 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
570 data = (struct unix_private_data *) channel->private_data;
571 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
573 if (--channel->refcount > 0)
577 retval = flush_cached_blocks(channel, data, 0);
580 if (close(data->dev) < 0)
584 ext2fs_free_mem(&channel->private_data);
586 ext2fs_free_mem(&channel->name);
587 ext2fs_free_mem(&channel);
591 static errcode_t unix_set_blksize(io_channel channel, int blksize)
593 struct unix_private_data *data;
596 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
597 data = (struct unix_private_data *) channel->private_data;
598 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
600 if (channel->block_size != blksize) {
602 if ((retval = flush_cached_blocks(channel, data, 0)))
606 channel->block_size = blksize;
608 if ((retval = alloc_cache(channel, data)))
615 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
616 int count, void *buf)
618 struct unix_private_data *data;
619 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
624 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
625 data = (struct unix_private_data *) channel->private_data;
626 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
629 return raw_read_blk(channel, data, block, count, buf);
632 * If we're doing an odd-sized read or a very large read,
633 * flush out the cache and then do a direct read.
635 if (count < 0 || count > WRITE_DIRECT_SIZE) {
636 if ((retval = flush_cached_blocks(channel, data, 0)))
638 return raw_read_blk(channel, data, block, count, buf);
643 /* If it's in the cache, use it! */
644 if ((cache = find_cached_block(data, block, &reuse[0]))) {
646 printf("Using cached block %lu\n", block);
648 memcpy(cp, cache->buf, channel->block_size);
651 cp += channel->block_size;
656 * Special case where we read directly into the
657 * cache buffer; important in the O_DIRECT case
660 reuse_cache(channel, data, cache, block);
661 if ((retval = raw_read_blk(channel, data, block, 1,
666 memcpy(cp, cache->buf, channel->block_size);
671 * Find the number of uncached blocks so we can do a
672 * single read request
674 for (i=1; i < count; i++)
675 if (find_cached_block(data, block+i, &reuse[i]))
678 printf("Reading %d blocks starting at %lu\n", i, block);
680 if ((retval = raw_read_blk(channel, data, block, i, cp)))
683 /* Save the results in the cache */
684 for (j=0; j < i; j++) {
687 reuse_cache(channel, data, cache, block++);
688 memcpy(cache->buf, cp, channel->block_size);
689 cp += channel->block_size;
693 #endif /* NO_IO_CACHE */
696 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
697 int count, void *buf)
699 return unix_read_blk64(channel, block, count, buf);
702 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
703 int count, const void *buf)
705 struct unix_private_data *data;
706 struct unix_cache *cache, *reuse;
707 errcode_t retval = 0;
711 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
712 data = (struct unix_private_data *) channel->private_data;
713 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
716 return raw_write_blk(channel, data, block, count, buf);
719 * If we're doing an odd-sized write or a very large write,
720 * flush out the cache completely and then do a direct write.
722 if (count < 0 || count > WRITE_DIRECT_SIZE) {
723 if ((retval = flush_cached_blocks(channel, data, 1)))
725 return raw_write_blk(channel, data, block, count, buf);
729 * For a moderate-sized multi-block write, first force a write
730 * if we're in write-through cache mode, and then fill the
731 * cache with the blocks.
733 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
735 retval = raw_write_blk(channel, data, block, count, buf);
739 cache = find_cached_block(data, block, &reuse);
742 reuse_cache(channel, data, cache, block);
744 memcpy(cache->buf, cp, channel->block_size);
745 cache->dirty = !writethrough;
748 cp += channel->block_size;
751 #endif /* NO_IO_CACHE */
754 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
755 int count, const void *buf)
757 return unix_write_blk64(channel, block, count, buf);
760 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
761 int size, const void *buf)
763 struct unix_private_data *data;
764 errcode_t retval = 0;
767 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
768 data = (struct unix_private_data *) channel->private_data;
769 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
771 if (data->align != 0) {
773 printf("unix_write_byte: O_DIRECT fallback\n");
775 return EXT2_ET_UNIMPLEMENTED;
780 * Flush out the cache completely
782 if ((retval = flush_cached_blocks(channel, data, 1)))
786 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
789 actual = write(data->dev, buf, size);
791 return EXT2_ET_SHORT_WRITE;
797 * Flush data buffers to disk.
799 static errcode_t unix_flush(io_channel channel)
801 struct unix_private_data *data;
802 errcode_t retval = 0;
804 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
805 data = (struct unix_private_data *) channel->private_data;
806 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
809 retval = flush_cached_blocks(channel, data, 0);
815 static errcode_t unix_set_option(io_channel channel, const char *option,
818 struct unix_private_data *data;
819 unsigned long long tmp;
822 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
823 data = (struct unix_private_data *) channel->private_data;
824 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
826 if (!strcmp(option, "offset")) {
828 return EXT2_ET_INVALID_ARGUMENT;
830 tmp = strtoull(arg, &end, 0);
832 return EXT2_ET_INVALID_ARGUMENT;
834 if (data->offset < 0)
835 return EXT2_ET_INVALID_ARGUMENT;
838 return EXT2_ET_INVALID_ARGUMENT;
841 #if defined(__linux__) && !defined(BLKDISCARD)
842 #define BLKDISCARD _IO(0x12,119)
845 static errcode_t unix_discard(io_channel channel, unsigned long long block,
846 unsigned long long count)
849 struct unix_private_data *data;
853 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
854 data = (struct unix_private_data *) channel->private_data;
855 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
857 range[0] = (__uint64_t)(block) * channel->block_size;
858 range[1] = (__uint64_t)(count) * channel->block_size;
860 ret = ioctl(data->dev, BLKDISCARD, &range);
865 return EXT2_ET_UNIMPLEMENTED;