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