| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * unix_io.c --- This is the Unix I/O interface to the I/O manager. |
| 3 | * |
| 4 | * Implements a one-block write-through cache. |
| 5 | * |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 6 | * Copyright (C) 1993, 1994, 1995 Theodore Ts'o. |
| 7 | * |
| 8 | * %Begin-Header% |
| 9 | * This file may be redistributed under the terms of the GNU Public |
| 10 | * License. |
| 11 | * %End-Header% |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 14 | #define _LARGEFILE_SOURCE |
| 15 | #define _LARGEFILE64_SOURCE |
| 16 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 19 | #if HAVE_UNISTD_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 21 | #endif |
| Theodore Ts'o | c4e749a | 1998-02-20 05:33:14 +0000 | [diff] [blame] | 22 | #if HAVE_ERRNO_H |
| 23 | #include <errno.h> |
| 24 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | #include <time.h> |
| Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 27 | #if HAVE_SYS_STAT_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 28 | #include <sys/stat.h> |
| Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 29 | #endif |
| 30 | #if HAVE_SYS_TYPES_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 32 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 33 | |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 34 | #if EXT2_FLAT_INCLUDES |
| 35 | #include "ext2_fs.h" |
| 36 | #else |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 37 | #include <linux/ext2_fs.h> |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 38 | #endif |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 39 | |
| 40 | #include "ext2fs.h" |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 41 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 42 | /* |
| 43 | * For checking structure magic numbers... |
| 44 | */ |
| 45 | |
| 46 | #define EXT2_CHECK_MAGIC(struct, code) \ |
| 47 | if ((struct)->magic != (code)) return (code) |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 48 | |
| 49 | struct unix_cache { |
| 50 | char *buf; |
| 51 | unsigned long block; |
| 52 | int access_time; |
| 53 | int dirty:1; |
| 54 | int in_use:1; |
| 55 | }; |
| 56 | |
| 57 | #define CACHE_SIZE 8 |
| 58 | #define WRITE_VIA_CACHE_SIZE 4 /* Must be smaller than CACHE_SIZE */ |
| 59 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 60 | struct unix_private_data { |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 61 | int magic; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 62 | int dev; |
| 63 | int flags; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 64 | int access_time; |
| 65 | struct unix_cache cache[CACHE_SIZE]; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static errcode_t unix_open(const char *name, int flags, io_channel *channel); |
| 69 | static errcode_t unix_close(io_channel channel); |
| 70 | static errcode_t unix_set_blksize(io_channel channel, int blksize); |
| 71 | static errcode_t unix_read_blk(io_channel channel, unsigned long block, |
| 72 | int count, void *data); |
| 73 | static errcode_t unix_write_blk(io_channel channel, unsigned long block, |
| 74 | int count, const void *data); |
| 75 | static errcode_t unix_flush(io_channel channel); |
| 76 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 77 | static struct struct_io_manager struct_unix_manager = { |
| 78 | EXT2_ET_MAGIC_IO_MANAGER, |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 79 | "Unix I/O Manager", |
| 80 | unix_open, |
| 81 | unix_close, |
| 82 | unix_set_blksize, |
| 83 | unix_read_blk, |
| 84 | unix_write_blk, |
| 85 | unix_flush |
| 86 | }; |
| 87 | |
| 88 | io_manager unix_io_manager = &struct_unix_manager; |
| 89 | |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 90 | /* |
| 91 | * Here are the raw I/O functions |
| 92 | */ |
| 93 | static errcode_t raw_read_blk(io_channel channel, |
| 94 | struct unix_private_data *data, |
| 95 | unsigned long block, |
| 96 | int count, void *buf) |
| 97 | { |
| 98 | errcode_t retval; |
| 99 | size_t size; |
| 100 | ext2_loff_t location; |
| 101 | int actual = 0; |
| 102 | |
| 103 | size = (count < 0) ? -count : count * channel->block_size; |
| 104 | location = (ext2_loff_t) block * channel->block_size; |
| 105 | if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) { |
| 106 | retval = errno ? errno : EXT2_ET_LLSEEK_FAILED; |
| 107 | goto error_out; |
| 108 | } |
| 109 | actual = read(data->dev, buf, size); |
| 110 | if (actual != size) { |
| 111 | if (actual < 0) |
| 112 | actual = 0; |
| 113 | retval = EXT2_ET_SHORT_READ; |
| 114 | goto error_out; |
| 115 | } |
| 116 | return 0; |
| 117 | |
| 118 | error_out: |
| 119 | memset((char *) buf+actual, 0, size-actual); |
| 120 | if (channel->read_error) |
| 121 | retval = (channel->read_error)(channel, block, count, buf, |
| 122 | size, actual, retval); |
| 123 | return retval; |
| 124 | } |
| 125 | |
| 126 | static errcode_t raw_write_blk(io_channel channel, |
| 127 | struct unix_private_data *data, |
| 128 | unsigned long block, |
| 129 | int count, const void *buf) |
| 130 | { |
| 131 | size_t size; |
| 132 | ext2_loff_t location; |
| 133 | int actual = 0; |
| 134 | errcode_t retval; |
| 135 | |
| 136 | if (count == 1) |
| 137 | size = channel->block_size; |
| 138 | else { |
| 139 | if (count < 0) |
| 140 | size = -count; |
| 141 | else |
| 142 | size = count * channel->block_size; |
| 143 | } |
| 144 | |
| 145 | location = (ext2_loff_t) block * channel->block_size; |
| 146 | if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) { |
| 147 | retval = errno ? errno : EXT2_ET_LLSEEK_FAILED; |
| 148 | goto error_out; |
| 149 | } |
| 150 | |
| 151 | actual = write(data->dev, buf, size); |
| 152 | if (actual != size) { |
| 153 | retval = EXT2_ET_SHORT_WRITE; |
| 154 | goto error_out; |
| 155 | } |
| 156 | return 0; |
| 157 | |
| 158 | error_out: |
| 159 | if (channel->write_error) |
| 160 | retval = (channel->write_error)(channel, block, count, buf, |
| 161 | size, actual, retval); |
| 162 | return retval; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /* |
| 167 | * Here we implement the cache functions |
| 168 | */ |
| 169 | |
| 170 | /* Allocate the cache buffers */ |
| 171 | static errcode_t alloc_cache(io_channel channel, |
| 172 | struct unix_private_data *data) |
| 173 | { |
| 174 | errcode_t retval; |
| 175 | struct unix_cache *cache; |
| 176 | int i; |
| 177 | |
| 178 | data->access_time = 0; |
| 179 | for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) { |
| 180 | cache->block = 0; |
| 181 | cache->access_time = 0; |
| 182 | cache->dirty = 0; |
| 183 | cache->in_use = 0; |
| 184 | if ((retval = ext2fs_get_mem(channel->block_size, |
| 185 | (void **) &cache->buf))) |
| 186 | return retval; |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* Free the cache buffers */ |
| 192 | static void free_cache(io_channel channel, |
| 193 | struct unix_private_data *data) |
| 194 | { |
| 195 | struct unix_cache *cache; |
| 196 | int i; |
| 197 | |
| 198 | data->access_time = 0; |
| 199 | for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) { |
| 200 | cache->block = 0; |
| 201 | cache->access_time = 0; |
| 202 | cache->dirty = 0; |
| 203 | cache->in_use = 0; |
| 204 | if (cache->buf) |
| 205 | ext2fs_free_mem((void **) &cache->buf); |
| 206 | cache->buf = 0; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Try to find a block in the cache. If get_cache is non-zero, then |
| 212 | * if the block isn't in the cache, evict the oldest block in the |
| 213 | * cache and create a new cache entry for the requested block. |
| 214 | */ |
| 215 | struct unix_cache *find_cached_block(io_channel channel, |
| 216 | struct unix_private_data *data, |
| 217 | unsigned long block, |
| 218 | int get_cache) |
| 219 | { |
| 220 | struct unix_cache *cache, *free_cache, *oldest_cache; |
| 221 | int i; |
| 222 | |
| 223 | free_cache = oldest_cache = 0; |
| 224 | for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) { |
| 225 | if (!cache->in_use) { |
| 226 | free_cache = cache; |
| 227 | continue; |
| 228 | } |
| 229 | if (cache->block == block) { |
| 230 | cache->access_time = ++data->access_time; |
| 231 | return cache; |
| 232 | } |
| 233 | if (!oldest_cache || |
| 234 | (cache->access_time < oldest_cache->access_time)) |
| 235 | oldest_cache = cache; |
| 236 | } |
| 237 | if (!get_cache) |
| 238 | return 0; |
| 239 | |
| 240 | /* |
| 241 | * Try to allocate cache slot. |
| 242 | */ |
| 243 | if (free_cache) |
| 244 | cache = free_cache; |
| 245 | else { |
| 246 | cache = oldest_cache; |
| 247 | if (cache->dirty) |
| 248 | raw_write_blk(channel, data, |
| 249 | cache->block, 1, cache->buf); |
| 250 | } |
| 251 | cache->in_use = 1; |
| 252 | cache->block = block; |
| 253 | cache->access_time = ++data->access_time; |
| 254 | return cache; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Flush all of the blocks in the cache |
| 259 | */ |
| 260 | static errcode_t flush_cached_blocks(io_channel channel, |
| 261 | struct unix_private_data *data, |
| 262 | int invalidate) |
| 263 | |
| 264 | { |
| 265 | struct unix_cache *cache; |
| 266 | errcode_t retval, retval2; |
| 267 | int i; |
| 268 | |
| 269 | retval2 = 0; |
| 270 | for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) { |
| 271 | if (!cache->in_use) |
| 272 | continue; |
| 273 | |
| 274 | if (invalidate) |
| 275 | cache->in_use = 0; |
| 276 | |
| 277 | if (!cache->dirty) |
| 278 | continue; |
| 279 | |
| 280 | retval = raw_write_blk(channel, data, |
| 281 | cache->block, 1, cache->buf); |
| 282 | if (retval) |
| 283 | retval2 = retval; |
| 284 | else |
| 285 | cache->dirty = 0; |
| 286 | } |
| 287 | return retval2; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 292 | static errcode_t unix_open(const char *name, int flags, io_channel *channel) |
| 293 | { |
| 294 | io_channel io = NULL; |
| 295 | struct unix_private_data *data = NULL; |
| 296 | errcode_t retval; |
| Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 297 | int open_flags; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 298 | |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 299 | if (name == 0) |
| 300 | return EXT2_ET_BAD_DEVICE_NAME; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 301 | retval = ext2fs_get_mem(sizeof(struct struct_io_channel), |
| 302 | (void **) &io); |
| 303 | if (retval) |
| 304 | return retval; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 305 | memset(io, 0, sizeof(struct struct_io_channel)); |
| 306 | io->magic = EXT2_ET_MAGIC_IO_CHANNEL; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 307 | retval = ext2fs_get_mem(sizeof(struct unix_private_data), |
| 308 | (void **) &data); |
| 309 | if (retval) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 310 | goto cleanup; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 311 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 312 | io->manager = unix_io_manager; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 313 | retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name); |
| 314 | if (retval) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 315 | goto cleanup; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 316 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 317 | strcpy(io->name, name); |
| 318 | io->private_data = data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 319 | io->block_size = 1024; |
| 320 | io->read_error = 0; |
| 321 | io->write_error = 0; |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 322 | io->refcount = 1; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 323 | |
| 324 | memset(data, 0, sizeof(struct unix_private_data)); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 325 | data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 326 | |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 327 | if ((retval = alloc_cache(io, data))) |
| 328 | goto cleanup; |
| 329 | |
| Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 330 | open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY; |
| 331 | #ifdef HAVE_OPEN64 |
| 332 | data->dev = open64(name, open_flags); |
| 333 | #else |
| 334 | data->dev = open(name, open_flags); |
| 335 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 336 | if (data->dev < 0) { |
| 337 | retval = errno; |
| 338 | goto cleanup; |
| 339 | } |
| 340 | *channel = io; |
| 341 | return 0; |
| 342 | |
| 343 | cleanup: |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 344 | if (data) { |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 345 | free_cache(io, data); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 346 | ext2fs_free_mem((void **) &data); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 347 | } |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 348 | if (io) |
| 349 | ext2fs_free_mem((void **) &io); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 350 | return retval; |
| 351 | } |
| 352 | |
| 353 | static errcode_t unix_close(io_channel channel) |
| 354 | { |
| 355 | struct unix_private_data *data; |
| 356 | errcode_t retval = 0; |
| 357 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 358 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 359 | data = (struct unix_private_data *) channel->private_data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 360 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 361 | |
| 362 | if (--channel->refcount > 0) |
| 363 | return 0; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 364 | |
| 365 | retval = flush_cached_blocks(channel, data, 0); |
| 366 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 367 | if (close(data->dev) < 0) |
| 368 | retval = errno; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 369 | free_cache(channel, data); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 370 | if (channel->private_data) |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 371 | ext2fs_free_mem((void **) &channel->private_data); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 372 | if (channel->name) |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 373 | ext2fs_free_mem((void **) &channel->name); |
| 374 | ext2fs_free_mem((void **) &channel); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 375 | return retval; |
| 376 | } |
| 377 | |
| 378 | static errcode_t unix_set_blksize(io_channel channel, int blksize) |
| 379 | { |
| 380 | struct unix_private_data *data; |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 381 | errcode_t retval; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 382 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 383 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 384 | data = (struct unix_private_data *) channel->private_data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 385 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| 386 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 387 | if (channel->block_size != blksize) { |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 388 | if ((retval = flush_cached_blocks(channel, data, 0))) |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 389 | return retval; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 390 | |
| 391 | channel->block_size = blksize; |
| 392 | free_cache(channel, data); |
| 393 | if ((retval = alloc_cache(channel, data))) |
| 394 | return retval; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 395 | } |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | static errcode_t unix_read_blk(io_channel channel, unsigned long block, |
| 401 | int count, void *buf) |
| 402 | { |
| 403 | struct unix_private_data *data; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 404 | struct unix_cache *cache; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 405 | errcode_t retval; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 406 | int i, j; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 407 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 408 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 409 | data = (struct unix_private_data *) channel->private_data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 410 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 411 | |
| 412 | /* |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 413 | * If we're doing an odd-sized read, flush out the cache and |
| 414 | * then do a direct read. |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 415 | */ |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 416 | if (count < 0) { |
| 417 | if ((retval = flush_cached_blocks(channel, data, 0))) |
| 418 | return retval; |
| 419 | return raw_read_blk(channel, data, block, count, buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 420 | } |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 421 | |
| 422 | while (count > 0) { |
| 423 | /* If it's in the cache, use it! */ |
| 424 | if ((cache = find_cached_block(channel, data, block, 0))) { |
| 425 | #ifdef DEBUG |
| 426 | printf("Using cached block %d\n", block); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 427 | #endif |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 428 | memcpy(buf, cache->buf, channel->block_size); |
| 429 | count--; |
| 430 | block++; |
| 431 | buf += channel->block_size; |
| 432 | continue; |
| 433 | } |
| 434 | /* |
| 435 | * Find the number of uncached blocks so we can do a |
| 436 | * single read request |
| 437 | */ |
| 438 | for (i=1; i < count; i++) |
| 439 | if (find_cached_block(channel, data, block+i, 0)) |
| 440 | break; |
| 441 | #ifdef DEBUG |
| 442 | printf("Reading %d blocks starting at %d\n", i, block); |
| 443 | #endif |
| 444 | if ((retval = raw_read_blk(channel, data, block, i, buf))) |
| 445 | return retval; |
| 446 | |
| 447 | /* Save the results in the cache */ |
| 448 | for (j=0; j < i; j++) { |
| 449 | count--; |
| 450 | cache = find_cached_block(channel, data, block++, 1); |
| 451 | if (cache) |
| 452 | memcpy(cache->buf, buf, channel->block_size); |
| 453 | buf += channel->block_size; |
| 454 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 455 | } |
| 456 | return 0; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static errcode_t unix_write_blk(io_channel channel, unsigned long block, |
| 460 | int count, const void *buf) |
| 461 | { |
| 462 | struct unix_private_data *data; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 463 | struct unix_cache *cache; |
| 464 | errcode_t retval = 0, retval2; |
| 465 | char *cp; |
| 466 | int i, writethrough; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 467 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 468 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 469 | data = (struct unix_private_data *) channel->private_data; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 470 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 471 | |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 472 | /* |
| 473 | * If we're doing an odd-sized write or a very large write, |
| 474 | * flush out the cache completely and then do a direct write. |
| 475 | */ |
| 476 | if (count < 0 || count > WRITE_VIA_CACHE_SIZE) { |
| 477 | if ((retval = flush_cached_blocks(channel, data, 1))) |
| 478 | return retval; |
| 479 | return raw_write_blk(channel, data, block, count, buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 482 | /* |
| 483 | * For a moderate-sized multi-block write, first force a write |
| 484 | * if we're in write-through cache mode, and then fill the |
| 485 | * cache with the blocks. |
| 486 | */ |
| 487 | writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH; |
| 488 | if (writethrough) |
| 489 | retval = raw_write_blk(channel, data, block, count, buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 490 | |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 491 | while (count > 0) { |
| 492 | cache = find_cached_block(channel, data, block, 1); |
| 493 | if (!cache) { |
| 494 | /* |
| 495 | * Oh shit, we couldn't get cache descriptor. |
| 496 | * Force the write directly. |
| 497 | */ |
| 498 | if ((retval2 = raw_write_blk(channel, data, block, |
| 499 | 1, buf))) |
| 500 | retval = retval2; |
| 501 | } else { |
| 502 | memcpy(cache->buf, buf, channel->block_size); |
| 503 | cache->dirty = !writethrough; |
| 504 | } |
| 505 | count--; |
| 506 | block++; |
| 507 | buf += channel->block_size; |
| 508 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 509 | return retval; |
| 510 | } |
| 511 | |
| 512 | /* |
| Theodore Ts'o | 36f2143 | 1997-06-14 07:25:40 +0000 | [diff] [blame] | 513 | * Flush data buffers to disk. |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 514 | */ |
| 515 | static errcode_t unix_flush(io_channel channel) |
| 516 | { |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 517 | struct unix_private_data *data; |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 518 | errcode_t retval = 0; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 519 | |
| 520 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| 521 | data = (struct unix_private_data *) channel->private_data; |
| 522 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 523 | |
| 524 | retval = flush_cached_blocks(channel, data, 0); |
| Theodore Ts'o | 36f2143 | 1997-06-14 07:25:40 +0000 | [diff] [blame] | 525 | fsync(data->dev); |
| Theodore Ts'o | adfc8c6 | 2000-10-18 19:22:24 +0000 | [diff] [blame^] | 526 | return retval; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 527 | } |
| 528 | |