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