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