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