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