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
32 #include <sys/utsname.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
37 #ifdef HAVE_SYS_MOUNT_H
38 #include <sys/mount.h>
44 #include <sys/types.h>
46 #if HAVE_SYS_RESOURCE_H
47 #include <sys/resource.h>
50 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
51 #define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
58 * For checking structure magic numbers...
61 #define EXT2_CHECK_MAGIC(struct, code) \
62 if ((struct)->magic != (code)) return (code)
73 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
74 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
76 struct unix_private_data {
82 struct unix_cache cache[CACHE_SIZE];
83 struct struct_io_stats io_stats;
86 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
87 static errcode_t unix_close(io_channel channel);
88 static errcode_t unix_set_blksize(io_channel channel, int blksize);
89 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
90 int count, void *data);
91 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
92 int count, const void *data);
93 static errcode_t unix_flush(io_channel channel);
94 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
95 int size, const void *data);
96 static errcode_t unix_set_option(io_channel channel, const char *option,
98 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
100 static void reuse_cache(io_channel channel, struct unix_private_data *data,
101 struct unix_cache *cache, unsigned long long block);
102 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
103 int count, void *data);
104 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
105 int count, const void *data);
107 /* __FreeBSD_kernel__ is defined by GNU/kFreeBSD - the FreeBSD kernel
108 * does not know buffered block devices - everything is raw. */
109 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
110 #define NEED_BOUNCE_BUFFER
112 #undef NEED_BOUNCE_BUFFER
115 static struct struct_io_manager struct_unix_manager = {
116 EXT2_ET_MAGIC_IO_MANAGER,
124 #ifdef NEED_BOUNCE_BUFFER
135 io_manager unix_io_manager = &struct_unix_manager;
137 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
139 errcode_t retval = 0;
141 struct unix_private_data *data;
143 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
144 data = (struct unix_private_data *) channel->private_data;
145 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
148 *stats = &data->io_stats;
154 * Here are the raw I/O functions
156 #ifndef NEED_BOUNCE_BUFFER
157 static errcode_t raw_read_blk(io_channel channel,
158 struct unix_private_data *data,
159 unsigned long long block,
160 int count, void *buf)
164 ext2_loff_t location;
167 size = (count < 0) ? -count : count * channel->block_size;
168 data->io_stats.bytes_read += size;
169 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
170 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
171 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
174 actual = read(data->dev, buf, size);
175 if (actual != size) {
178 retval = EXT2_ET_SHORT_READ;
184 memset((char *) buf+actual, 0, size-actual);
185 if (channel->read_error)
186 retval = (channel->read_error)(channel, block, count, buf,
187 size, actual, retval);
190 #else /* NEED_BOUNCE_BUFFER */
192 * Windows and FreeBSD block devices only allow sector alignment IO in offset and size
194 static errcode_t raw_read_blk(io_channel channel,
195 struct unix_private_data *data,
197 int count, void *buf)
200 size_t size, alignsize, fragment;
201 ext2_loff_t location;
202 int total = 0, actual;
203 #define BLOCKALIGN 512
204 char sector[BLOCKALIGN];
206 size = (count < 0) ? -count : count * channel->block_size;
207 data->io_stats.bytes_read += size;
208 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
210 printf("count=%d, size=%d, block=%lu, blk_size=%d, location=%llx\n",
211 count, size, block, channel->block_size, (long long)location);
213 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
214 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
217 fragment = size % BLOCKALIGN;
218 alignsize = size - fragment;
220 actual = read(data->dev, buf, alignsize);
221 if (actual != alignsize)
225 actual = read(data->dev, sector, BLOCKALIGN);
226 if (actual != BLOCKALIGN)
228 memcpy(buf+alignsize, sector, fragment);
235 retval = EXT2_ET_SHORT_READ;
238 memset((char *) buf+total, 0, size-actual);
239 if (channel->read_error)
240 retval = (channel->read_error)(channel, block, count, buf,
241 size, actual, retval);
246 static errcode_t raw_write_blk(io_channel channel,
247 struct unix_private_data *data,
248 unsigned long long block,
249 int count, const void *buf)
252 ext2_loff_t location;
257 size = channel->block_size;
262 size = count * channel->block_size;
264 data->io_stats.bytes_written += size;
266 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
267 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
268 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
272 actual = write(data->dev, buf, size);
273 if (actual != size) {
274 retval = EXT2_ET_SHORT_WRITE;
280 if (channel->write_error)
281 retval = (channel->write_error)(channel, block, count, buf,
282 size, actual, retval);
288 * Here we implement the cache functions
291 /* Allocate the cache buffers */
292 static errcode_t alloc_cache(io_channel channel,
293 struct unix_private_data *data)
296 struct unix_cache *cache;
299 data->access_time = 0;
300 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
302 cache->access_time = 0;
305 if ((retval = ext2fs_get_mem(channel->block_size,
312 /* Free the cache buffers */
313 static void free_cache(struct unix_private_data *data)
315 struct unix_cache *cache;
318 data->access_time = 0;
319 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
321 cache->access_time = 0;
325 ext2fs_free_mem(&cache->buf);
332 * Try to find a block in the cache. If the block is not found, and
333 * eldest is a non-zero pointer, then fill in eldest with the cache
334 * entry to that should be reused.
336 static struct unix_cache *find_cached_block(struct unix_private_data *data,
337 unsigned long long block,
338 struct unix_cache **eldest)
340 struct unix_cache *cache, *unused_cache, *oldest_cache;
343 unused_cache = oldest_cache = 0;
344 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
345 if (!cache->in_use) {
347 unused_cache = cache;
350 if (cache->block == block) {
351 cache->access_time = ++data->access_time;
355 (cache->access_time < oldest_cache->access_time))
356 oldest_cache = cache;
359 *eldest = (unused_cache) ? unused_cache : oldest_cache;
364 * Reuse a particular cache entry for another block.
366 static void reuse_cache(io_channel channel, struct unix_private_data *data,
367 struct unix_cache *cache, unsigned long long block)
369 if (cache->dirty && cache->in_use)
370 raw_write_blk(channel, data, cache->block, 1, cache->buf);
374 cache->block = block;
375 cache->access_time = ++data->access_time;
379 * Flush all of the blocks in the cache
381 static errcode_t flush_cached_blocks(io_channel channel,
382 struct unix_private_data *data,
386 struct unix_cache *cache;
387 errcode_t retval, retval2;
391 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
401 retval = raw_write_blk(channel, data,
402 cache->block, 1, cache->buf);
410 #endif /* NO_IO_CACHE */
412 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
414 io_channel io = NULL;
415 struct unix_private_data *data = NULL;
424 return EXT2_ET_BAD_DEVICE_NAME;
425 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
428 memset(io, 0, sizeof(struct struct_io_channel));
429 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
430 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
434 io->manager = unix_io_manager;
435 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
439 strcpy(io->name, name);
440 io->private_data = data;
441 io->block_size = 1024;
446 memset(data, 0, sizeof(struct unix_private_data));
447 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
448 data->io_stats.num_fields = 2;
450 if ((retval = alloc_cache(io, data)))
453 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
454 if (flags & IO_FLAG_EXCLUSIVE)
455 open_flags |= O_EXCL;
457 data->dev = open64(io->name, open_flags);
459 data->dev = open(io->name, open_flags);
467 if (flags & IO_FLAG_RW) {
471 /* Is the block device actually writable? */
472 error = ioctl(data->dev, BLKROGET, &readonly);
473 if (!error && readonly) {
483 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
484 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
486 #define RLIM_INFINITY (~0UL)
489 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
490 * block devices are wrongly getting hit by the filesize
491 * limit. This workaround isn't perfect, since it won't work
492 * if glibc wasn't built against 2.2 header files. (Sigh.)
495 if ((flags & IO_FLAG_RW) &&
497 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
498 (ut.release[2] == '4') && (ut.release[3] == '.') &&
499 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
500 (ut.release[5] < '8')) &&
501 (fstat(data->dev, &st) == 0) &&
502 (S_ISBLK(st.st_mode))) {
505 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
506 setrlimit(RLIMIT_FSIZE, &rlim);
507 getrlimit(RLIMIT_FSIZE, &rlim);
508 if (((unsigned long) rlim.rlim_cur) <
509 ((unsigned long) rlim.rlim_max)) {
510 rlim.rlim_cur = rlim.rlim_max;
511 setrlimit(RLIMIT_FSIZE, &rlim);
521 ext2fs_free_mem(&data);
524 ext2fs_free_mem(&io);
528 static errcode_t unix_close(io_channel channel)
530 struct unix_private_data *data;
531 errcode_t retval = 0;
533 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
534 data = (struct unix_private_data *) channel->private_data;
535 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
537 if (--channel->refcount > 0)
541 retval = flush_cached_blocks(channel, data, 0);
544 if (close(data->dev) < 0)
548 ext2fs_free_mem(&channel->private_data);
550 ext2fs_free_mem(&channel->name);
551 ext2fs_free_mem(&channel);
555 static errcode_t unix_set_blksize(io_channel channel, int blksize)
557 struct unix_private_data *data;
560 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
561 data = (struct unix_private_data *) channel->private_data;
562 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
564 if (channel->block_size != blksize) {
566 if ((retval = flush_cached_blocks(channel, data, 0)))
570 channel->block_size = blksize;
572 if ((retval = alloc_cache(channel, data)))
579 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
580 int count, void *buf)
582 struct unix_private_data *data;
583 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
588 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
589 data = (struct unix_private_data *) channel->private_data;
590 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
593 return raw_read_blk(channel, data, block, count, buf);
596 * If we're doing an odd-sized read or a very large read,
597 * flush out the cache and then do a direct read.
599 if (count < 0 || count > WRITE_DIRECT_SIZE) {
600 if ((retval = flush_cached_blocks(channel, data, 0)))
602 return raw_read_blk(channel, data, block, count, buf);
607 /* If it's in the cache, use it! */
608 if ((cache = find_cached_block(data, block, &reuse[0]))) {
610 printf("Using cached block %lu\n", block);
612 memcpy(cp, cache->buf, channel->block_size);
615 cp += channel->block_size;
619 * Find the number of uncached blocks so we can do a
620 * single read request
622 for (i=1; i < count; i++)
623 if (find_cached_block(data, block+i, &reuse[i]))
626 printf("Reading %d blocks starting at %lu\n", i, block);
628 if ((retval = raw_read_blk(channel, data, block, i, cp)))
631 /* Save the results in the cache */
632 for (j=0; j < i; j++) {
635 reuse_cache(channel, data, cache, block++);
636 memcpy(cache->buf, cp, channel->block_size);
637 cp += channel->block_size;
641 #endif /* NO_IO_CACHE */
644 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
645 int count, void *buf)
647 return unix_read_blk64(channel, block, count, buf);
650 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
651 int count, const void *buf)
653 struct unix_private_data *data;
654 struct unix_cache *cache, *reuse;
655 errcode_t retval = 0;
659 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
660 data = (struct unix_private_data *) channel->private_data;
661 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
664 return raw_write_blk(channel, data, block, count, buf);
667 * If we're doing an odd-sized write or a very large write,
668 * flush out the cache completely and then do a direct write.
670 if (count < 0 || count > WRITE_DIRECT_SIZE) {
671 if ((retval = flush_cached_blocks(channel, data, 1)))
673 return raw_write_blk(channel, data, block, count, buf);
677 * For a moderate-sized multi-block write, first force a write
678 * if we're in write-through cache mode, and then fill the
679 * cache with the blocks.
681 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
683 retval = raw_write_blk(channel, data, block, count, buf);
687 cache = find_cached_block(data, block, &reuse);
690 reuse_cache(channel, data, cache, block);
692 memcpy(cache->buf, cp, channel->block_size);
693 cache->dirty = !writethrough;
696 cp += channel->block_size;
699 #endif /* NO_IO_CACHE */
702 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
703 int count, const void *buf)
705 return unix_write_blk64(channel, block, count, buf);
708 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
709 int size, const void *buf)
711 struct unix_private_data *data;
712 errcode_t retval = 0;
715 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
716 data = (struct unix_private_data *) channel->private_data;
717 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
721 * Flush out the cache completely
723 if ((retval = flush_cached_blocks(channel, data, 1)))
727 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
730 actual = write(data->dev, buf, size);
732 return EXT2_ET_SHORT_WRITE;
738 * Flush data buffers to disk.
740 static errcode_t unix_flush(io_channel channel)
742 struct unix_private_data *data;
743 errcode_t retval = 0;
745 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
746 data = (struct unix_private_data *) channel->private_data;
747 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
750 retval = flush_cached_blocks(channel, data, 0);
756 static errcode_t unix_set_option(io_channel channel, const char *option,
759 struct unix_private_data *data;
760 unsigned long long tmp;
763 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
764 data = (struct unix_private_data *) channel->private_data;
765 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
767 if (!strcmp(option, "offset")) {
769 return EXT2_ET_INVALID_ARGUMENT;
771 tmp = strtoull(arg, &end, 0);
773 return EXT2_ET_INVALID_ARGUMENT;
775 if (data->offset < 0)
776 return EXT2_ET_INVALID_ARGUMENT;
779 return EXT2_ET_INVALID_ARGUMENT;