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 Public
18 #define _LARGEFILE_SOURCE
19 #define _LARGEFILE64_SOURCE
32 #include <sys/utsname.h>
38 #include <sys/types.h>
40 #if HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
48 * For checking structure magic numbers...
51 #define EXT2_CHECK_MAGIC(struct, code) \
52 if ((struct)->magic != (code)) return (code)
63 #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
64 #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
66 struct unix_private_data {
72 struct unix_cache cache[CACHE_SIZE];
75 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
76 static errcode_t unix_close(io_channel channel);
77 static errcode_t unix_set_blksize(io_channel channel, int blksize);
78 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
79 int count, void *data);
80 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
81 int count, const void *data);
82 static errcode_t unix_flush(io_channel channel);
83 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
84 int size, const void *data);
85 static errcode_t unix_set_option(io_channel channel, const char *option,
88 static void reuse_cache(io_channel channel, struct unix_private_data *data,
89 struct unix_cache *cache, unsigned long block);
91 /* __FreeBSD_kernel__ is defined by GNU/kFreeBSD - the FreeBSD kernel
92 * does not know buffered block devices - everything is raw. */
93 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
94 #define NEED_BOUNCE_BUFFER
96 #undef NEED_BOUNCE_BUFFER
99 static struct struct_io_manager struct_unix_manager = {
100 EXT2_ET_MAGIC_IO_MANAGER,
108 #ifdef NEED_BOUNCE_BUFFER
116 io_manager unix_io_manager = &struct_unix_manager;
119 * Here are the raw I/O functions
121 #ifndef NEED_BOUNCE_BUFFER
122 static errcode_t raw_read_blk(io_channel channel,
123 struct unix_private_data *data,
125 int count, void *buf)
129 ext2_loff_t location;
132 size = (count < 0) ? -count : count * channel->block_size;
133 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
134 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
135 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
138 actual = read(data->dev, buf, size);
139 if (actual != size) {
142 retval = EXT2_ET_SHORT_READ;
148 memset((char *) buf+actual, 0, size-actual);
149 if (channel->read_error)
150 retval = (channel->read_error)(channel, block, count, buf,
151 size, actual, retval);
154 #else /* NEED_BOUNCE_BUFFER */
156 * Windows and FreeBSD block devices only allow sector alignment IO in offset and size
158 static errcode_t raw_read_blk(io_channel channel,
159 struct unix_private_data *data,
161 int count, void *buf)
164 size_t size, alignsize, fragment;
165 ext2_loff_t location;
166 int total = 0, actual;
167 #define BLOCKALIGN 512
168 char sector[BLOCKALIGN];
170 size = (count < 0) ? -count : count * channel->block_size;
171 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
173 printf("count=%d, size=%d, block=%lu, blk_size=%d, location=%llx\n",
174 count, size, block, channel->block_size, (long long)location);
176 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
177 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
180 fragment = size % BLOCKALIGN;
181 alignsize = size - fragment;
183 actual = read(data->dev, buf, alignsize);
184 if (actual != alignsize)
188 actual = read(data->dev, sector, BLOCKALIGN);
189 if (actual != BLOCKALIGN)
191 memcpy(buf+alignsize, sector, fragment);
198 retval = EXT2_ET_SHORT_READ;
201 memset((char *) buf+total, 0, size-actual);
202 if (channel->read_error)
203 retval = (channel->read_error)(channel, block, count, buf,
204 size, actual, retval);
209 static errcode_t raw_write_blk(io_channel channel,
210 struct unix_private_data *data,
212 int count, const void *buf)
215 ext2_loff_t location;
220 size = channel->block_size;
225 size = count * channel->block_size;
228 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
229 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
230 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
234 actual = write(data->dev, buf, size);
235 if (actual != size) {
236 retval = EXT2_ET_SHORT_WRITE;
242 if (channel->write_error)
243 retval = (channel->write_error)(channel, block, count, buf,
244 size, actual, retval);
250 * Here we implement the cache functions
253 /* Allocate the cache buffers */
254 static errcode_t alloc_cache(io_channel channel,
255 struct unix_private_data *data)
258 struct unix_cache *cache;
261 data->access_time = 0;
262 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
264 cache->access_time = 0;
267 if ((retval = ext2fs_get_mem(channel->block_size,
274 /* Free the cache buffers */
275 static void free_cache(struct unix_private_data *data)
277 struct unix_cache *cache;
280 data->access_time = 0;
281 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
283 cache->access_time = 0;
287 ext2fs_free_mem(&cache->buf);
294 * Try to find a block in the cache. If the block is not found, and
295 * eldest is a non-zero pointer, then fill in eldest with the cache
296 * entry to that should be reused.
298 static struct unix_cache *find_cached_block(struct unix_private_data *data,
300 struct unix_cache **eldest)
302 struct unix_cache *cache, *unused_cache, *oldest_cache;
305 unused_cache = oldest_cache = 0;
306 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
307 if (!cache->in_use) {
309 unused_cache = cache;
312 if (cache->block == block) {
313 cache->access_time = ++data->access_time;
317 (cache->access_time < oldest_cache->access_time))
318 oldest_cache = cache;
321 *eldest = (unused_cache) ? unused_cache : oldest_cache;
326 * Reuse a particular cache entry for another block.
328 static void reuse_cache(io_channel channel, struct unix_private_data *data,
329 struct unix_cache *cache, unsigned long block)
331 if (cache->dirty && cache->in_use)
332 raw_write_blk(channel, data, cache->block, 1, cache->buf);
336 cache->block = block;
337 cache->access_time = ++data->access_time;
341 * Flush all of the blocks in the cache
343 static errcode_t flush_cached_blocks(io_channel channel,
344 struct unix_private_data *data,
348 struct unix_cache *cache;
349 errcode_t retval, retval2;
353 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
363 retval = raw_write_blk(channel, data,
364 cache->block, 1, cache->buf);
372 #endif /* NO_IO_CACHE */
374 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
376 io_channel io = NULL;
377 struct unix_private_data *data = NULL;
386 return EXT2_ET_BAD_DEVICE_NAME;
387 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
390 memset(io, 0, sizeof(struct struct_io_channel));
391 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
392 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
396 io->manager = unix_io_manager;
397 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
401 strcpy(io->name, name);
402 io->private_data = data;
403 io->block_size = 1024;
408 memset(data, 0, sizeof(struct unix_private_data));
409 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
411 if ((retval = alloc_cache(io, data)))
414 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
415 if (flags & IO_FLAG_EXCLUSIVE)
416 open_flags |= O_EXCL;
418 data->dev = open64(io->name, open_flags);
420 data->dev = open(io->name, open_flags);
429 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
430 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
432 #define RLIM_INFINITY (~0UL)
435 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
436 * block devices are wrongly getting hit by the filesize
437 * limit. This workaround isn't perfect, since it won't work
438 * if glibc wasn't built against 2.2 header files. (Sigh.)
441 if ((flags & IO_FLAG_RW) &&
443 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
444 (ut.release[2] == '4') && (ut.release[3] == '.') &&
445 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
446 (ut.release[5] < '8')) &&
447 (fstat(data->dev, &st) == 0) &&
448 (S_ISBLK(st.st_mode))) {
451 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
452 setrlimit(RLIMIT_FSIZE, &rlim);
453 getrlimit(RLIMIT_FSIZE, &rlim);
454 if (((unsigned long) rlim.rlim_cur) <
455 ((unsigned long) rlim.rlim_max)) {
456 rlim.rlim_cur = rlim.rlim_max;
457 setrlimit(RLIMIT_FSIZE, &rlim);
467 ext2fs_free_mem(&data);
470 ext2fs_free_mem(&io);
474 static errcode_t unix_close(io_channel channel)
476 struct unix_private_data *data;
477 errcode_t retval = 0;
479 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
480 data = (struct unix_private_data *) channel->private_data;
481 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
483 if (--channel->refcount > 0)
487 retval = flush_cached_blocks(channel, data, 0);
490 if (close(data->dev) < 0)
494 ext2fs_free_mem(&channel->private_data);
496 ext2fs_free_mem(&channel->name);
497 ext2fs_free_mem(&channel);
501 static errcode_t unix_set_blksize(io_channel channel, int blksize)
503 struct unix_private_data *data;
506 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
507 data = (struct unix_private_data *) channel->private_data;
508 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
510 if (channel->block_size != blksize) {
512 if ((retval = flush_cached_blocks(channel, data, 0)))
516 channel->block_size = blksize;
518 if ((retval = alloc_cache(channel, data)))
525 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
526 int count, void *buf)
528 struct unix_private_data *data;
529 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
534 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
535 data = (struct unix_private_data *) channel->private_data;
536 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
539 return raw_read_blk(channel, data, block, count, buf);
542 * If we're doing an odd-sized read or a very large read,
543 * flush out the cache and then do a direct read.
545 if (count < 0 || count > WRITE_DIRECT_SIZE) {
546 if ((retval = flush_cached_blocks(channel, data, 0)))
548 return raw_read_blk(channel, data, block, count, buf);
553 /* If it's in the cache, use it! */
554 if ((cache = find_cached_block(data, block, &reuse[0]))) {
556 printf("Using cached block %lu\n", block);
558 memcpy(cp, cache->buf, channel->block_size);
561 cp += channel->block_size;
565 * Find the number of uncached blocks so we can do a
566 * single read request
568 for (i=1; i < count; i++)
569 if (find_cached_block(data, block+i, &reuse[i]))
572 printf("Reading %d blocks starting at %lu\n", i, block);
574 if ((retval = raw_read_blk(channel, data, block, i, cp)))
577 /* Save the results in the cache */
578 for (j=0; j < i; j++) {
581 reuse_cache(channel, data, cache, block++);
582 memcpy(cache->buf, cp, channel->block_size);
583 cp += channel->block_size;
587 #endif /* NO_IO_CACHE */
590 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
591 int count, const void *buf)
593 struct unix_private_data *data;
594 struct unix_cache *cache, *reuse;
595 errcode_t retval = 0;
599 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
600 data = (struct unix_private_data *) channel->private_data;
601 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
604 return raw_write_blk(channel, data, block, count, buf);
607 * If we're doing an odd-sized write or a very large write,
608 * flush out the cache completely and then do a direct write.
610 if (count < 0 || count > WRITE_DIRECT_SIZE) {
611 if ((retval = flush_cached_blocks(channel, data, 1)))
613 return raw_write_blk(channel, data, block, count, buf);
617 * For a moderate-sized multi-block write, first force a write
618 * if we're in write-through cache mode, and then fill the
619 * cache with the blocks.
621 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
623 retval = raw_write_blk(channel, data, block, count, buf);
627 cache = find_cached_block(data, block, &reuse);
630 reuse_cache(channel, data, cache, block);
632 memcpy(cache->buf, cp, channel->block_size);
633 cache->dirty = !writethrough;
636 cp += channel->block_size;
639 #endif /* NO_IO_CACHE */
642 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
643 int size, const void *buf)
645 struct unix_private_data *data;
646 errcode_t retval = 0;
649 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
650 data = (struct unix_private_data *) channel->private_data;
651 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
655 * Flush out the cache completely
657 if ((retval = flush_cached_blocks(channel, data, 1)))
661 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
664 actual = write(data->dev, buf, size);
666 return EXT2_ET_SHORT_WRITE;
672 * Flush data buffers to disk.
674 static errcode_t unix_flush(io_channel channel)
676 struct unix_private_data *data;
677 errcode_t retval = 0;
679 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
680 data = (struct unix_private_data *) channel->private_data;
681 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
684 retval = flush_cached_blocks(channel, data, 0);
690 static errcode_t unix_set_option(io_channel channel, const char *option,
693 struct unix_private_data *data;
697 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
698 data = (struct unix_private_data *) channel->private_data;
699 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
701 if (!strcmp(option, "offset")) {
703 return EXT2_ET_INVALID_ARGUMENT;
705 tmp = strtoul(arg, &end, 0);
707 return EXT2_ET_INVALID_ARGUMENT;
711 return EXT2_ET_INVALID_ARGUMENT;