| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 2 | * unix_io.c --- This is the Unix (well, really POSIX) implementation |
| 3 | * of the I/O manager. |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 4 | * |
| 5 | * Implements a one-block write-through cache. |
| 6 | * |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 7 | * Includes support for Windows NT support under Cygwin. |
| 8 | * |
| Theodore Ts'o | 64e1b27 | 2002-02-23 18:50:32 -0500 | [diff] [blame] | 9 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
| 10 | * 2002 by Theodore Ts'o. |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 11 | * |
| 12 | * %Begin-Header% |
| 13 | * This file may be redistributed under the terms of the GNU Public |
| 14 | * License. |
| 15 | * %End-Header% |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 18 | #define _LARGEFILE_SOURCE |
| 19 | #define _LARGEFILE64_SOURCE |
| 20 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 23 | #if HAVE_UNISTD_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 25 | #endif |
| Theodore Ts'o | c4e749a | 1998-02-20 05:33:14 +0000 | [diff] [blame] | 26 | #if HAVE_ERRNO_H |
| 27 | #include <errno.h> |
| 28 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <time.h> |
| Theodore Ts'o | f154d2f | 2002-07-14 08:33:32 -0400 | [diff] [blame] | 31 | #ifdef __linux__ |
| 32 | #include <sys/utsname.h> |
| 33 | #endif |
| Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 34 | #if HAVE_SYS_STAT_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 35 | #include <sys/stat.h> |
| Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 36 | #endif |
| 37 | #if HAVE_SYS_TYPES_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
| Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 39 | #endif |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 40 | #if HAVE_SYS_RESOURCE_H |
| Theodore Ts'o | 8880e75 | 2001-11-26 21:05:36 -0500 | [diff] [blame] | 41 | #include <sys/resource.h> |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 42 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 43 | |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 44 | #include "ext2_fs.h" |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 45 | #include "ext2fs.h" |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 46 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 47 | /* |
| 48 | * For checking structure magic numbers... |
| 49 | */ |
| 50 | |
| 51 | #define EXT2_CHECK_MAGIC(struct, code) \ |
| 52 | if ((struct)->magic != (code)) return (code) |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 53 | |
| 54 | struct unix_cache { |
| 55 | char *buf; |
| 56 | unsigned long block; |
| 57 | int access_time; |
| Matthias Andree | 83e692e | 2004-03-30 04:17:14 +0200 | [diff] [blame] | 58 | unsigned dirty:1; |
| 59 | unsigned in_use:1; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | #define CACHE_SIZE 8 |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 63 | #define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */ |
| 64 | #define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */ |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 65 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 66 | struct unix_private_data { |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 67 | int magic; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 68 | int dev; |
| 69 | int flags; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 70 | int access_time; |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 71 | ext2_loff_t offset; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 72 | struct unix_cache cache[CACHE_SIZE]; |
| Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame^] | 73 | struct struct_io_stats io_stats; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static errcode_t unix_open(const char *name, int flags, io_channel *channel); |
| 77 | static errcode_t unix_close(io_channel channel); |
| 78 | static errcode_t unix_set_blksize(io_channel channel, int blksize); |
| 79 | static errcode_t unix_read_blk(io_channel channel, unsigned long block, |
| 80 | int count, void *data); |
| 81 | static errcode_t unix_write_blk(io_channel channel, unsigned long block, |
| 82 | int count, const void *data); |
| 83 | static errcode_t unix_flush(io_channel channel); |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 84 | static errcode_t unix_write_byte(io_channel channel, unsigned long offset, |
| 85 | int size, const void *data); |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 86 | static errcode_t unix_set_option(io_channel channel, const char *option, |
| 87 | const char *arg); |
| Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame^] | 88 | static errcode_t unix_get_stats(io_channel channel, io_stats *stats) |
| 89 | ; |
| Theodore Ts'o | 23b7c8b | 2003-01-22 18:30:01 -0500 | [diff] [blame] | 90 | static void reuse_cache(io_channel channel, struct unix_private_data *data, |
| 91 | struct unix_cache *cache, unsigned long block); |
| 92 | |
| Matthias Andree | 289e055 | 2004-03-30 03:57:41 +0200 | [diff] [blame] | 93 | /* __FreeBSD_kernel__ is defined by GNU/kFreeBSD - the FreeBSD kernel |
| 94 | * does not know buffered block devices - everything is raw. */ |
| 95 | #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| Matthias Andree | b34cbdd | 2003-12-28 18:21:26 +0100 | [diff] [blame] | 96 | #define NEED_BOUNCE_BUFFER |
| 97 | #else |
| 98 | #undef NEED_BOUNCE_BUFFER |
| 99 | #endif |
| 100 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 101 | static struct struct_io_manager struct_unix_manager = { |
| 102 | EXT2_ET_MAGIC_IO_MANAGER, |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 103 | "Unix I/O Manager", |
| 104 | unix_open, |
| 105 | unix_close, |
| 106 | unix_set_blksize, |
| 107 | unix_read_blk, |
| 108 | unix_write_blk, |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 109 | unix_flush, |
| Matthias Andree | b34cbdd | 2003-12-28 18:21:26 +0100 | [diff] [blame] | 110 | #ifdef NEED_BOUNCE_BUFFER |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 111 | 0, |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 112 | #else |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 113 | unix_write_byte, |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 114 | #endif |
| Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame^] | 115 | unix_set_option, |
| 116 | unix_get_stats, |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | io_manager unix_io_manager = &struct_unix_manager; |
| 120 | |
| Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame^] | 121 | static errcode_t unix_get_stats(io_channel channel, io_stats *stats) |
| 122 | { |
| 123 | errcode_t retval = 0; |
| 124 | |
| 125 | struct unix_private_data *data; |
| 126 | |
| 127 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| 128 | data = (struct unix_private_data *) channel->private_data; |
| 129 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| 130 | |
| 131 | if (stats) |
| 132 | *stats = &data->io_stats; |
| 133 | |
| 134 | return retval; |
| 135 | } |
| 136 | |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 137 | /* |
| 138 | * Here are the raw I/O functions |
| 139 | */ |
| Matthias Andree | b34cbdd | 2003-12-28 18:21:26 +0100 | [diff] [blame] | 140 | #ifndef NEED_BOUNCE_BUFFER |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 141 | static errcode_t raw_read_blk(io_channel channel, |
| 142 | struct unix_private_data *data, |
| 143 | unsigned long block, |
| 144 | int count, void *buf) |
| 145 | { |
| 146 | errcode_t retval; |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 147 | ssize_t size; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 148 | ext2_loff_t location; |
| 149 | int actual = 0; |
| 150 | |
| 151 | size = (count < 0) ? -count : count * channel->block_size; |
| Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame^] | 152 | data->io_stats.bytes_read += size; |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 153 | location = ((ext2_loff_t) block * channel->block_size) + data->offset; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 154 | if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) { |
| 155 | retval = errno ? errno : EXT2_ET_LLSEEK_FAILED; |
| 156 | goto error_out; |
| 157 | } |
| 158 | actual = read(data->dev, buf, size); |
| 159 | if (actual != size) { |
| 160 | if (actual < 0) |
| 161 | actual = 0; |
| 162 | retval = EXT2_ET_SHORT_READ; |
| 163 | goto error_out; |
| 164 | } |
| 165 | return 0; |
| 166 | |
| 167 | error_out: |
| 168 | memset((char *) buf+actual, 0, size-actual); |
| 169 | if (channel->read_error) |
| 170 | retval = (channel->read_error)(channel, block, count, buf, |
| 171 | size, actual, retval); |
| 172 | return retval; |
| 173 | } |
| Matthias Andree | b34cbdd | 2003-12-28 18:21:26 +0100 | [diff] [blame] | 174 | #else /* NEED_BOUNCE_BUFFER */ |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 175 | /* |
| Matthias Andree | b34cbdd | 2003-12-28 18:21:26 +0100 | [diff] [blame] | 176 | * Windows and FreeBSD block devices only allow sector alignment IO in offset and size |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 177 | */ |
| 178 | static errcode_t raw_read_blk(io_channel channel, |
| 179 | struct unix_private_data *data, |
| 180 | unsigned long block, |
| 181 | int count, void *buf) |
| 182 | { |
| 183 | errcode_t retval; |
| 184 | size_t size, alignsize, fragment; |
| 185 | ext2_loff_t location; |
| 186 | int total = 0, actual; |
| 187 | #define BLOCKALIGN 512 |
| 188 | char sector[BLOCKALIGN]; |
| 189 | |
| 190 | size = (count < 0) ? -count : count * channel->block_size; |
| Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame^] | 191 | data->io_stats.bytes_read += size; |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 192 | location = ((ext2_loff_t) block * channel->block_size) + data->offset; |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 193 | #ifdef DEBUG |
| Eric Sandeen | d0ff90d | 2006-09-12 14:56:15 -0400 | [diff] [blame] | 194 | printf("count=%d, size=%d, block=%lu, blk_size=%d, location=%llx\n", |
| 195 | count, size, block, channel->block_size, (long long)location); |
| Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 196 | #endif |
| 197 | if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) { |
| 198 | retval = errno ? errno : EXT2_ET_LLSEEK_FAILED; |
| 199 | goto error_out; |
| 200 | } |
| 201 | fragment = size % BLOCKALIGN; |
| 202 | alignsize = size - fragment; |
| 203 | if (alignsize) { |
| 204 | actual = read(data->dev, buf, alignsize); |
| 205 | if (actual != alignsize) |
| 206 | goto short_read; |
| 207 | } |
| 208 | if (fragment) { |
| 209 | actual = read(data->dev, sector, BLOCKALIGN); |
| 210 | if (actual != BLOCKALIGN) |
| 211 | goto short_read; |
| 212 | memcpy(buf+alignsize, sector, fragment); |
| 213 | } |
| 214 | return 0; |
| 215 | |
| 216 | short_read: |
| 217 | if (actual>0) |
| 218 | total += actual; |
| 219 | retval = EXT2_ET_SHORT_READ; |
| 220 | |
| 221 | error_out: |
| 222 | memset((char *) buf+total, 0, size-actual); |
| 223 | if (channel->read_error) |
| 224 | retval = (channel->read_error)(channel, block, count, buf, |
| 225 | size, actual, retval); |
| 226 | return retval; |
| 227 | } |
| 228 | #endif |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 229 | |
| 230 | static errcode_t raw_write_blk(io_channel channel, |
| 231 | struct unix_private_data *data, |
| 232 | unsigned long block, |
| 233 | int count, const void *buf) |
| 234 | { |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 235 | ssize_t size; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 236 | ext2_loff_t location; |
| 237 | int actual = 0; |
| 238 | errcode_t retval; |
| 239 | |
| 240 | if (count == 1) |
| 241 | size = channel->block_size; |
| 242 | else { |
| 243 | if (count < 0) |
| 244 | size = -count; |
| 245 | else |
| 246 | size = count * channel->block_size; |
| 247 | } |
| Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame^] | 248 | data->io_stats.bytes_written += size; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 249 | |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 250 | location = ((ext2_loff_t) block * channel->block_size) + data->offset; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 251 | if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) { |
| 252 | retval = errno ? errno : EXT2_ET_LLSEEK_FAILED; |
| 253 | goto error_out; |
| 254 | } |
| 255 | |
| 256 | actual = write(data->dev, buf, size); |
| 257 | if (actual != size) { |
| 258 | retval = EXT2_ET_SHORT_WRITE; |
| 259 | goto error_out; |
| 260 | } |
| 261 | return 0; |
| 262 | |
| 263 | error_out: |
| 264 | if (channel->write_error) |
| 265 | retval = (channel->write_error)(channel, block, count, buf, |
| 266 | size, actual, retval); |
| 267 | return retval; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /* |
| 272 | * Here we implement the cache functions |
| 273 | */ |
| 274 | |
| 275 | /* Allocate the cache buffers */ |
| 276 | static errcode_t alloc_cache(io_channel channel, |
| 277 | struct unix_private_data *data) |
| 278 | { |
| 279 | errcode_t retval; |
| 280 | struct unix_cache *cache; |
| 281 | int i; |
| 282 | |
| 283 | data->access_time = 0; |
| 284 | for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) { |
| 285 | cache->block = 0; |
| 286 | cache->access_time = 0; |
| 287 | cache->dirty = 0; |
| 288 | cache->in_use = 0; |
| 289 | if ((retval = ext2fs_get_mem(channel->block_size, |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 290 | &cache->buf))) |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 291 | return retval; |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* Free the cache buffers */ |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 297 | static void free_cache(struct unix_private_data *data) |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 298 | { |
| 299 | struct unix_cache *cache; |
| 300 | int i; |
| 301 | |
| 302 | data->access_time = 0; |
| 303 | for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) { |
| 304 | cache->block = 0; |
| 305 | cache->access_time = 0; |
| 306 | cache->dirty = 0; |
| 307 | cache->in_use = 0; |
| 308 | if (cache->buf) |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 309 | ext2fs_free_mem(&cache->buf); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 310 | cache->buf = 0; |
| 311 | } |
| 312 | } |
| 313 | |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 314 | #ifndef NO_IO_CACHE |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 315 | /* |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 316 | * Try to find a block in the cache. If the block is not found, and |
| 317 | * eldest is a non-zero pointer, then fill in eldest with the cache |
| 318 | * entry to that should be reused. |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 319 | */ |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 320 | static struct unix_cache *find_cached_block(struct unix_private_data *data, |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 321 | unsigned long block, |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 322 | struct unix_cache **eldest) |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 323 | { |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 324 | struct unix_cache *cache, *unused_cache, *oldest_cache; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 325 | int i; |
| 326 | |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 327 | unused_cache = oldest_cache = 0; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 328 | for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) { |
| 329 | if (!cache->in_use) { |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 330 | if (!unused_cache) |
| 331 | unused_cache = cache; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 332 | continue; |
| 333 | } |
| 334 | if (cache->block == block) { |
| 335 | cache->access_time = ++data->access_time; |
| 336 | return cache; |
| 337 | } |
| 338 | if (!oldest_cache || |
| 339 | (cache->access_time < oldest_cache->access_time)) |
| 340 | oldest_cache = cache; |
| 341 | } |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 342 | if (eldest) |
| 343 | *eldest = (unused_cache) ? unused_cache : oldest_cache; |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Reuse a particular cache entry for another block. |
| 349 | */ |
| Theodore Ts'o | 23b7c8b | 2003-01-22 18:30:01 -0500 | [diff] [blame] | 350 | static void reuse_cache(io_channel channel, struct unix_private_data *data, |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 351 | struct unix_cache *cache, unsigned long block) |
| 352 | { |
| 353 | if (cache->dirty && cache->in_use) |
| 354 | raw_write_blk(channel, data, cache->block, 1, cache->buf); |
| 355 | |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 356 | cache->in_use = 1; |
| Theodore Ts'o | 1d47dfb | 2002-11-09 10:33:49 -0500 | [diff] [blame] | 357 | cache->dirty = 0; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 358 | cache->block = block; |
| 359 | cache->access_time = ++data->access_time; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Flush all of the blocks in the cache |
| 364 | */ |
| 365 | static errcode_t flush_cached_blocks(io_channel channel, |
| 366 | struct unix_private_data *data, |
| 367 | int invalidate) |
| 368 | |
| 369 | { |
| 370 | struct unix_cache *cache; |
| 371 | errcode_t retval, retval2; |
| 372 | int i; |
| 373 | |
| 374 | retval2 = 0; |
| 375 | for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) { |
| 376 | if (!cache->in_use) |
| 377 | continue; |
| 378 | |
| 379 | if (invalidate) |
| 380 | cache->in_use = 0; |
| 381 | |
| 382 | if (!cache->dirty) |
| 383 | continue; |
| 384 | |
| 385 | retval = raw_write_blk(channel, data, |
| 386 | cache->block, 1, cache->buf); |
| 387 | if (retval) |
| 388 | retval2 = retval; |
| 389 | else |
| 390 | cache->dirty = 0; |
| 391 | } |
| 392 | return retval2; |
| 393 | } |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 394 | #endif /* NO_IO_CACHE */ |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 395 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 396 | static errcode_t unix_open(const char *name, int flags, io_channel *channel) |
| 397 | { |
| 398 | io_channel io = NULL; |
| 399 | struct unix_private_data *data = NULL; |
| 400 | errcode_t retval; |
| Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 401 | int open_flags; |
| Theodore Ts'o | 8880e75 | 2001-11-26 21:05:36 -0500 | [diff] [blame] | 402 | struct stat st; |
| Theodore Ts'o | f154d2f | 2002-07-14 08:33:32 -0400 | [diff] [blame] | 403 | #ifdef __linux__ |
| 404 | struct utsname ut; |
| 405 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 406 | |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 407 | if (name == 0) |
| 408 | return EXT2_ET_BAD_DEVICE_NAME; |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 409 | retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 410 | if (retval) |
| 411 | return retval; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 412 | memset(io, 0, sizeof(struct struct_io_channel)); |
| 413 | io->magic = EXT2_ET_MAGIC_IO_CHANNEL; |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 414 | retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 415 | if (retval) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 416 | goto cleanup; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 417 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 418 | io->manager = unix_io_manager; |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 419 | retval = ext2fs_get_mem(strlen(name)+1, &io->name); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 420 | if (retval) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 421 | goto cleanup; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 422 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 423 | strcpy(io->name, name); |
| 424 | io->private_data = data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 425 | io->block_size = 1024; |
| 426 | io->read_error = 0; |
| 427 | io->write_error = 0; |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 428 | io->refcount = 1; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 429 | |
| 430 | memset(data, 0, sizeof(struct unix_private_data)); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 431 | data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL; |
| Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame^] | 432 | data->io_stats.num_fields = 2; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 433 | |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 434 | if ((retval = alloc_cache(io, data))) |
| 435 | goto cleanup; |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 436 | |
| Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 437 | open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY; |
| Theodore Ts'o | fa6c653 | 2006-03-18 18:57:44 -0500 | [diff] [blame] | 438 | if (flags & IO_FLAG_EXCLUSIVE) |
| 439 | open_flags |= O_EXCL; |
| Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 440 | #ifdef HAVE_OPEN64 |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 441 | data->dev = open64(io->name, open_flags); |
| Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 442 | #else |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 443 | data->dev = open(io->name, open_flags); |
| Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 444 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 445 | if (data->dev < 0) { |
| 446 | retval = errno; |
| 447 | goto cleanup; |
| 448 | } |
| Theodore Ts'o | 64e1b27 | 2002-02-23 18:50:32 -0500 | [diff] [blame] | 449 | |
| 450 | #ifdef __linux__ |
| 451 | #undef RLIM_INFINITY |
| 452 | #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4))) |
| 453 | #define RLIM_INFINITY ((unsigned long)(~0UL>>1)) |
| 454 | #else |
| 455 | #define RLIM_INFINITY (~0UL) |
| 456 | #endif |
| Theodore Ts'o | 8880e75 | 2001-11-26 21:05:36 -0500 | [diff] [blame] | 457 | /* |
| Theodore Ts'o | f154d2f | 2002-07-14 08:33:32 -0400 | [diff] [blame] | 458 | * Work around a bug in 2.4.10-2.4.18 kernels where writes to |
| 459 | * block devices are wrongly getting hit by the filesize |
| 460 | * limit. This workaround isn't perfect, since it won't work |
| 461 | * if glibc wasn't built against 2.2 header files. (Sigh.) |
| 462 | * |
| Theodore Ts'o | 8880e75 | 2001-11-26 21:05:36 -0500 | [diff] [blame] | 463 | */ |
| Theodore Ts'o | f154d2f | 2002-07-14 08:33:32 -0400 | [diff] [blame] | 464 | if ((flags & IO_FLAG_RW) && |
| 465 | (uname(&ut) == 0) && |
| 466 | ((ut.release[0] == '2') && (ut.release[1] == '.') && |
| 467 | (ut.release[2] == '4') && (ut.release[3] == '.') && |
| 468 | (ut.release[4] == '1') && (ut.release[5] >= '0') && |
| 469 | (ut.release[5] < '8')) && |
| Theodore Ts'o | 8880e75 | 2001-11-26 21:05:36 -0500 | [diff] [blame] | 470 | (fstat(data->dev, &st) == 0) && |
| 471 | (S_ISBLK(st.st_mode))) { |
| 472 | struct rlimit rlim; |
| 473 | |
| Theodore Ts'o | 64e1b27 | 2002-02-23 18:50:32 -0500 | [diff] [blame] | 474 | rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY; |
| Theodore Ts'o | 8880e75 | 2001-11-26 21:05:36 -0500 | [diff] [blame] | 475 | setrlimit(RLIMIT_FSIZE, &rlim); |
| 476 | getrlimit(RLIMIT_FSIZE, &rlim); |
| Theodore Ts'o | bd27880 | 2001-12-03 05:47:32 +0100 | [diff] [blame] | 477 | if (((unsigned long) rlim.rlim_cur) < |
| 478 | ((unsigned long) rlim.rlim_max)) { |
| Theodore Ts'o | 8880e75 | 2001-11-26 21:05:36 -0500 | [diff] [blame] | 479 | rlim.rlim_cur = rlim.rlim_max; |
| 480 | setrlimit(RLIMIT_FSIZE, &rlim); |
| 481 | } |
| 482 | } |
| Theodore Ts'o | 64e1b27 | 2002-02-23 18:50:32 -0500 | [diff] [blame] | 483 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 484 | *channel = io; |
| 485 | return 0; |
| 486 | |
| 487 | cleanup: |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 488 | if (data) { |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 489 | free_cache(data); |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 490 | ext2fs_free_mem(&data); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 491 | } |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 492 | if (io) |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 493 | ext2fs_free_mem(&io); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 494 | return retval; |
| 495 | } |
| 496 | |
| 497 | static errcode_t unix_close(io_channel channel) |
| 498 | { |
| 499 | struct unix_private_data *data; |
| 500 | errcode_t retval = 0; |
| 501 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 502 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 503 | data = (struct unix_private_data *) channel->private_data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 504 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 505 | |
| 506 | if (--channel->refcount > 0) |
| 507 | return 0; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 508 | |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 509 | #ifndef NO_IO_CACHE |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 510 | retval = flush_cached_blocks(channel, data, 0); |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 511 | #endif |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 512 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 513 | if (close(data->dev) < 0) |
| 514 | retval = errno; |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 515 | free_cache(data); |
| Theodore Ts'o | f12e285 | 2002-02-20 01:06:25 -0500 | [diff] [blame] | 516 | |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 517 | ext2fs_free_mem(&channel->private_data); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 518 | if (channel->name) |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 519 | ext2fs_free_mem(&channel->name); |
| 520 | ext2fs_free_mem(&channel); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 521 | return retval; |
| 522 | } |
| 523 | |
| 524 | static errcode_t unix_set_blksize(io_channel channel, int blksize) |
| 525 | { |
| 526 | struct unix_private_data *data; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 527 | errcode_t retval; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 528 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 529 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 530 | data = (struct unix_private_data *) channel->private_data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 531 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| 532 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 533 | if (channel->block_size != blksize) { |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 534 | #ifndef NO_IO_CACHE |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 535 | if ((retval = flush_cached_blocks(channel, data, 0))) |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 536 | return retval; |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 537 | #endif |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 538 | |
| 539 | channel->block_size = blksize; |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 540 | free_cache(data); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 541 | if ((retval = alloc_cache(channel, data))) |
| 542 | return retval; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 543 | } |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | static errcode_t unix_read_blk(io_channel channel, unsigned long block, |
| 549 | int count, void *buf) |
| 550 | { |
| 551 | struct unix_private_data *data; |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 552 | struct unix_cache *cache, *reuse[READ_DIRECT_SIZE]; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 553 | errcode_t retval; |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 554 | char *cp; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 555 | int i, j; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 556 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 557 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 558 | data = (struct unix_private_data *) channel->private_data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 559 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 560 | |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 561 | #ifdef NO_IO_CACHE |
| 562 | return raw_read_blk(channel, data, block, count, buf); |
| 563 | #else |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 564 | /* |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 565 | * If we're doing an odd-sized read or a very large read, |
| 566 | * flush out the cache and then do a direct read. |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 567 | */ |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 568 | if (count < 0 || count > WRITE_DIRECT_SIZE) { |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 569 | if ((retval = flush_cached_blocks(channel, data, 0))) |
| 570 | return retval; |
| 571 | return raw_read_blk(channel, data, block, count, buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 572 | } |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 573 | |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 574 | cp = buf; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 575 | while (count > 0) { |
| 576 | /* If it's in the cache, use it! */ |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 577 | if ((cache = find_cached_block(data, block, &reuse[0]))) { |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 578 | #ifdef DEBUG |
| Eric Sandeen | d0ff90d | 2006-09-12 14:56:15 -0400 | [diff] [blame] | 579 | printf("Using cached block %lu\n", block); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 580 | #endif |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 581 | memcpy(cp, cache->buf, channel->block_size); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 582 | count--; |
| 583 | block++; |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 584 | cp += channel->block_size; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 585 | continue; |
| 586 | } |
| 587 | /* |
| 588 | * Find the number of uncached blocks so we can do a |
| 589 | * single read request |
| 590 | */ |
| 591 | for (i=1; i < count; i++) |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 592 | if (find_cached_block(data, block+i, &reuse[i])) |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 593 | break; |
| 594 | #ifdef DEBUG |
| Eric Sandeen | d0ff90d | 2006-09-12 14:56:15 -0400 | [diff] [blame] | 595 | printf("Reading %d blocks starting at %lu\n", i, block); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 596 | #endif |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 597 | if ((retval = raw_read_blk(channel, data, block, i, cp))) |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 598 | return retval; |
| 599 | |
| 600 | /* Save the results in the cache */ |
| 601 | for (j=0; j < i; j++) { |
| 602 | count--; |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 603 | cache = reuse[j]; |
| 604 | reuse_cache(channel, data, cache, block++); |
| 605 | memcpy(cache->buf, cp, channel->block_size); |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 606 | cp += channel->block_size; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 607 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 608 | } |
| 609 | return 0; |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 610 | #endif /* NO_IO_CACHE */ |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static errcode_t unix_write_blk(io_channel channel, unsigned long block, |
| 614 | int count, const void *buf) |
| 615 | { |
| 616 | struct unix_private_data *data; |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 617 | struct unix_cache *cache, *reuse; |
| Theodore Ts'o | 23b7c8b | 2003-01-22 18:30:01 -0500 | [diff] [blame] | 618 | errcode_t retval = 0; |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 619 | const char *cp; |
| 620 | int writethrough; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 621 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 622 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 623 | data = (struct unix_private_data *) channel->private_data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 624 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 625 | |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 626 | #ifdef NO_IO_CACHE |
| 627 | return raw_write_blk(channel, data, block, count, buf); |
| 628 | #else |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 629 | /* |
| 630 | * If we're doing an odd-sized write or a very large write, |
| 631 | * flush out the cache completely and then do a direct write. |
| 632 | */ |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 633 | if (count < 0 || count > WRITE_DIRECT_SIZE) { |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 634 | if ((retval = flush_cached_blocks(channel, data, 1))) |
| 635 | return retval; |
| 636 | return raw_write_blk(channel, data, block, count, buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 639 | /* |
| 640 | * For a moderate-sized multi-block write, first force a write |
| 641 | * if we're in write-through cache mode, and then fill the |
| 642 | * cache with the blocks. |
| 643 | */ |
| 644 | writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH; |
| 645 | if (writethrough) |
| 646 | retval = raw_write_blk(channel, data, block, count, buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 647 | |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 648 | cp = buf; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 649 | while (count > 0) { |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 650 | cache = find_cached_block(data, block, &reuse); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 651 | if (!cache) { |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 652 | cache = reuse; |
| 653 | reuse_cache(channel, data, cache, block); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 654 | } |
| Theodore Ts'o | 82c4660 | 2002-11-09 14:56:17 -0500 | [diff] [blame] | 655 | memcpy(cache->buf, cp, channel->block_size); |
| 656 | cache->dirty = !writethrough; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 657 | count--; |
| 658 | block++; |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 659 | cp += channel->block_size; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 660 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 661 | return retval; |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 662 | #endif /* NO_IO_CACHE */ |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 665 | static errcode_t unix_write_byte(io_channel channel, unsigned long offset, |
| 666 | int size, const void *buf) |
| 667 | { |
| 668 | struct unix_private_data *data; |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 669 | errcode_t retval = 0; |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 670 | ssize_t actual; |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 671 | |
| 672 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| 673 | data = (struct unix_private_data *) channel->private_data; |
| 674 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| 675 | |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 676 | #ifndef NO_IO_CACHE |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 677 | /* |
| 678 | * Flush out the cache completely |
| 679 | */ |
| 680 | if ((retval = flush_cached_blocks(channel, data, 1))) |
| 681 | return retval; |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 682 | #endif |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 683 | |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 684 | if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0) |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 685 | return errno; |
| 686 | |
| 687 | actual = write(data->dev, buf, size); |
| 688 | if (actual != size) |
| 689 | return EXT2_ET_SHORT_WRITE; |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 694 | /* |
| Theodore Ts'o | 36f2143 | 1997-06-14 07:25:40 +0000 | [diff] [blame] | 695 | * Flush data buffers to disk. |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 696 | */ |
| 697 | static errcode_t unix_flush(io_channel channel) |
| 698 | { |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 699 | struct unix_private_data *data; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 700 | errcode_t retval = 0; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 701 | |
| 702 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| 703 | data = (struct unix_private_data *) channel->private_data; |
| 704 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 705 | |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 706 | #ifndef NO_IO_CACHE |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 707 | retval = flush_cached_blocks(channel, data, 0); |
| Theodore Ts'o | b8a9531 | 2003-05-13 23:41:29 -0400 | [diff] [blame] | 708 | #endif |
| Theodore Ts'o | 36f2143 | 1997-06-14 07:25:40 +0000 | [diff] [blame] | 709 | fsync(data->dev); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame] | 710 | return retval; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 713 | static errcode_t unix_set_option(io_channel channel, const char *option, |
| 714 | const char *arg) |
| 715 | { |
| 716 | struct unix_private_data *data; |
| Theodore Ts'o | 2aee23f | 2006-11-12 10:40:40 -0500 | [diff] [blame] | 717 | unsigned long long tmp; |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 718 | char *end; |
| 719 | |
| 720 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| 721 | data = (struct unix_private_data *) channel->private_data; |
| 722 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| 723 | |
| 724 | if (!strcmp(option, "offset")) { |
| 725 | if (!arg) |
| 726 | return EXT2_ET_INVALID_ARGUMENT; |
| 727 | |
| Theodore Ts'o | 2aee23f | 2006-11-12 10:40:40 -0500 | [diff] [blame] | 728 | tmp = strtoull(arg, &end, 0); |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 729 | if (*end) |
| 730 | return EXT2_ET_INVALID_ARGUMENT; |
| 731 | data->offset = tmp; |
| Theodore Ts'o | 2aee23f | 2006-11-12 10:40:40 -0500 | [diff] [blame] | 732 | if (data->offset < 0) |
| 733 | return EXT2_ET_INVALID_ARGUMENT; |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 734 | return 0; |
| 735 | } |
| 736 | return EXT2_ET_INVALID_ARGUMENT; |
| 737 | } |