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=%d, blk_size=%d, location=%lx\n",
174 count, size, block, channel->block_size, 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;
416 data->dev = open64(io->name, open_flags);
418 data->dev = open(io->name, open_flags);
427 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
428 #define RLIM_INFINITY ((unsigned long)(~0UL>>1))
430 #define RLIM_INFINITY (~0UL)
433 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
434 * block devices are wrongly getting hit by the filesize
435 * limit. This workaround isn't perfect, since it won't work
436 * if glibc wasn't built against 2.2 header files. (Sigh.)
439 if ((flags & IO_FLAG_RW) &&
441 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
442 (ut.release[2] == '4') && (ut.release[3] == '.') &&
443 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
444 (ut.release[5] < '8')) &&
445 (fstat(data->dev, &st) == 0) &&
446 (S_ISBLK(st.st_mode))) {
449 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
450 setrlimit(RLIMIT_FSIZE, &rlim);
451 getrlimit(RLIMIT_FSIZE, &rlim);
452 if (((unsigned long) rlim.rlim_cur) <
453 ((unsigned long) rlim.rlim_max)) {
454 rlim.rlim_cur = rlim.rlim_max;
455 setrlimit(RLIMIT_FSIZE, &rlim);
465 ext2fs_free_mem(&data);
468 ext2fs_free_mem(&io);
472 static errcode_t unix_close(io_channel channel)
474 struct unix_private_data *data;
475 errcode_t retval = 0;
477 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
478 data = (struct unix_private_data *) channel->private_data;
479 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
481 if (--channel->refcount > 0)
485 retval = flush_cached_blocks(channel, data, 0);
488 if (close(data->dev) < 0)
492 ext2fs_free_mem(&channel->private_data);
494 ext2fs_free_mem(&channel->name);
495 ext2fs_free_mem(&channel);
499 static errcode_t unix_set_blksize(io_channel channel, int blksize)
501 struct unix_private_data *data;
504 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
505 data = (struct unix_private_data *) channel->private_data;
506 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
508 if (channel->block_size != blksize) {
510 if ((retval = flush_cached_blocks(channel, data, 0)))
514 channel->block_size = blksize;
516 if ((retval = alloc_cache(channel, data)))
523 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
524 int count, void *buf)
526 struct unix_private_data *data;
527 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
532 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
533 data = (struct unix_private_data *) channel->private_data;
534 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
537 return raw_read_blk(channel, data, block, count, buf);
540 * If we're doing an odd-sized read or a very large read,
541 * flush out the cache and then do a direct read.
543 if (count < 0 || count > WRITE_DIRECT_SIZE) {
544 if ((retval = flush_cached_blocks(channel, data, 0)))
546 return raw_read_blk(channel, data, block, count, buf);
551 /* If it's in the cache, use it! */
552 if ((cache = find_cached_block(data, block, &reuse[0]))) {
554 printf("Using cached block %d\n", block);
556 memcpy(cp, cache->buf, channel->block_size);
559 cp += channel->block_size;
563 * Find the number of uncached blocks so we can do a
564 * single read request
566 for (i=1; i < count; i++)
567 if (find_cached_block(data, block+i, &reuse[i]))
570 printf("Reading %d blocks starting at %d\n", i, block);
572 if ((retval = raw_read_blk(channel, data, block, i, cp)))
575 /* Save the results in the cache */
576 for (j=0; j < i; j++) {
579 reuse_cache(channel, data, cache, block++);
580 memcpy(cache->buf, cp, channel->block_size);
581 cp += channel->block_size;
585 #endif /* NO_IO_CACHE */
588 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
589 int count, const void *buf)
591 struct unix_private_data *data;
592 struct unix_cache *cache, *reuse;
593 errcode_t retval = 0;
597 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
598 data = (struct unix_private_data *) channel->private_data;
599 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
602 return raw_write_blk(channel, data, block, count, buf);
605 * If we're doing an odd-sized write or a very large write,
606 * flush out the cache completely and then do a direct write.
608 if (count < 0 || count > WRITE_DIRECT_SIZE) {
609 if ((retval = flush_cached_blocks(channel, data, 1)))
611 return raw_write_blk(channel, data, block, count, buf);
615 * For a moderate-sized multi-block write, first force a write
616 * if we're in write-through cache mode, and then fill the
617 * cache with the blocks.
619 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
621 retval = raw_write_blk(channel, data, block, count, buf);
625 cache = find_cached_block(data, block, &reuse);
628 reuse_cache(channel, data, cache, block);
630 memcpy(cache->buf, cp, channel->block_size);
631 cache->dirty = !writethrough;
634 cp += channel->block_size;
637 #endif /* NO_IO_CACHE */
640 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
641 int size, const void *buf)
643 struct unix_private_data *data;
644 errcode_t retval = 0;
647 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
648 data = (struct unix_private_data *) channel->private_data;
649 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
653 * Flush out the cache completely
655 if ((retval = flush_cached_blocks(channel, data, 1)))
659 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
662 actual = write(data->dev, buf, size);
664 return EXT2_ET_SHORT_WRITE;
670 * Flush data buffers to disk.
672 static errcode_t unix_flush(io_channel channel)
674 struct unix_private_data *data;
675 errcode_t retval = 0;
677 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
678 data = (struct unix_private_data *) channel->private_data;
679 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
682 retval = flush_cached_blocks(channel, data, 0);
688 static errcode_t unix_set_option(io_channel channel, const char *option,
691 struct unix_private_data *data;
695 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
696 data = (struct unix_private_data *) channel->private_data;
697 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
699 if (!strcmp(option, "offset")) {
701 return EXT2_ET_INVALID_ARGUMENT;
703 tmp = strtoul(arg, &end, 0);
705 return EXT2_ET_INVALID_ARGUMENT;
709 return EXT2_ET_INVALID_ARGUMENT;