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