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