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