blob: 2302374c9d0ea3e70d4e080e9ee0496b255d00f2 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
Theodore Ts'offf45482003-04-13 00:44:19 -04002 * unix_io.c --- This is the Unix (well, really POSIX) implementation
3 * of the I/O manager.
Theodore Ts'o3839e651997-04-26 13:21:57 +00004 *
5 * Implements a one-block write-through cache.
6 *
Theodore Ts'oefc6f622008-08-27 23:07:54 -04007 * Includes support for Windows NT support under Cygwin.
Theodore Ts'offf45482003-04-13 00:44:19 -04008 *
Theodore Ts'o64e1b272002-02-23 18:50:32 -05009 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
10 * 2002 by Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000011 *
12 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -040013 * This file may be redistributed under the terms of the GNU Library
14 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000015 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000016 */
17
Theodore Ts'odc5f68c2000-05-25 23:31:54 +000018#define _LARGEFILE_SOURCE
19#define _LARGEFILE64_SOURCE
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040020#define _GNU_SOURCE
Theodore Ts'odc5f68c2000-05-25 23:31:54 +000021
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <stdio.h>
23#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000024#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000026#endif
Theodore Ts'oc4e749a1998-02-20 05:33:14 +000027#if HAVE_ERRNO_H
28#include <errno.h>
29#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include <fcntl.h>
31#include <time.h>
Theodore Ts'of154d2f2002-07-14 08:33:32 -040032#ifdef __linux__
33#include <sys/utsname.h>
34#endif
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -050035#ifdef HAVE_SYS_IOCTL_H
36#include <sys/ioctl.h>
37#endif
38#ifdef HAVE_SYS_MOUNT_H
39#include <sys/mount.h>
40#endif
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000041#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000043#endif
44#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000045#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000046#endif
Theodore Ts'offf45482003-04-13 00:44:19 -040047#if HAVE_SYS_RESOURCE_H
Theodore Ts'o8880e752001-11-26 21:05:36 -050048#include <sys/resource.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040049#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000050
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040051#if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -050052#define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
53#endif
54
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040055#if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET)
56#define BLKSSZGET _IO(0x12,104)/* get block device sector size */
57#endif
58
59#undef ALIGN_DEBUG
60
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000061#include "ext2_fs.h"
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000062#include "ext2fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000063
Theodore Ts'of3db3561997-04-26 13:34:30 +000064/*
65 * For checking structure magic numbers...
66 */
67
68#define EXT2_CHECK_MAGIC(struct, code) \
69 if ((struct)->magic != (code)) return (code)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000070
71struct unix_cache {
72 char *buf;
73 unsigned long block;
74 int access_time;
Matthias Andree83e692e2004-03-30 04:17:14 +020075 unsigned dirty:1;
76 unsigned in_use:1;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000077};
78
79#define CACHE_SIZE 8
Theodore Ts'o82c46602002-11-09 14:56:17 -050080#define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
81#define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000082
Theodore Ts'o3839e651997-04-26 13:21:57 +000083struct unix_private_data {
Theodore Ts'of3db3561997-04-26 13:34:30 +000084 int magic;
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 int dev;
86 int flags;
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040087 int align;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000088 int access_time;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050089 ext2_loff_t offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000090 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040091 void *bounce;
Theodore Ts'o6d96b002007-08-03 20:07:09 -040092 struct struct_io_stats io_stats;
Theodore Ts'o3839e651997-04-26 13:21:57 +000093};
94
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040095#define IS_ALIGNED(n, align) ((((unsigned long) n) & \
96 ((unsigned long) ((align)-1))) == 0)
97
Theodore Ts'o3839e651997-04-26 13:21:57 +000098static errcode_t unix_open(const char *name, int flags, io_channel *channel);
99static errcode_t unix_close(io_channel channel);
100static errcode_t unix_set_blksize(io_channel channel, int blksize);
101static errcode_t unix_read_blk(io_channel channel, unsigned long block,
102 int count, void *data);
103static errcode_t unix_write_blk(io_channel channel, unsigned long block,
104 int count, const void *data);
105static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000106static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
107 int size, const void *data);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400108static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500109 const char *arg);
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400110static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
111;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500112static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600113 struct unix_cache *cache, unsigned long long block);
114static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
115 int count, void *data);
116static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
117 int count, const void *data);
Lukas Czernere90a59e2010-11-18 03:38:36 +0000118static errcode_t unix_discard(io_channel channel, unsigned long long block,
119 unsigned long long count);
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500120
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121static struct struct_io_manager struct_unix_manager = {
122 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123 "Unix I/O Manager",
124 unix_open,
125 unix_close,
126 unix_set_blksize,
127 unix_read_blk,
128 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000129 unix_flush,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500130 unix_write_byte,
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400131 unix_set_option,
132 unix_get_stats,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600133 unix_read_blk64,
134 unix_write_blk64,
Lukas Czernere90a59e2010-11-18 03:38:36 +0000135 unix_discard,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136};
137
138io_manager unix_io_manager = &struct_unix_manager;
139
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400140static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
141{
142 errcode_t retval = 0;
143
144 struct unix_private_data *data;
145
146 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
147 data = (struct unix_private_data *) channel->private_data;
148 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
149
150 if (stats)
151 *stats = &data->io_stats;
152
153 return retval;
154}
155
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000156/*
157 * Here are the raw I/O functions
158 */
159static errcode_t raw_read_blk(io_channel channel,
160 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600161 unsigned long long block,
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000162 int count, void *buf)
163{
164 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500165 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000166 ext2_loff_t location;
167 int actual = 0;
168
169 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400170 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500171 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000172 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
173 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
174 goto error_out;
175 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400176 if ((data->align == 0) ||
177 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
178 actual = read(data->dev, buf, size);
179 if (actual != size) {
180 short_read:
181 if (actual < 0)
182 actual = 0;
183 retval = EXT2_ET_SHORT_READ;
184 goto error_out;
185 }
186 return 0;
187 }
188
189#ifdef ALIGN_DEBUG
190 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
191 (unsigned long) size);
192#endif
193
194 /*
195 * The buffer or size which we're trying to read isn't aligned
196 * to the O_DIRECT rules, so we need to do this the hard way...
197 */
198 while (size > 0) {
199 actual = read(data->dev, data->bounce, channel->block_size);
200 if (actual != channel->block_size)
201 goto short_read;
202 actual = size;
203 if (size > channel->block_size)
204 actual = channel->block_size;
205 memcpy(buf, data->bounce, actual);
206 size -= actual;
207 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000208 }
209 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400210
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000211error_out:
212 memset((char *) buf+actual, 0, size-actual);
213 if (channel->read_error)
214 retval = (channel->read_error)(channel, block, count, buf,
215 size, actual, retval);
216 return retval;
217}
218
219static errcode_t raw_write_blk(io_channel channel,
220 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600221 unsigned long long block,
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000222 int count, const void *buf)
223{
Theodore Ts'o54434922003-12-07 01:28:50 -0500224 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000225 ext2_loff_t location;
226 int actual = 0;
227 errcode_t retval;
228
229 if (count == 1)
230 size = channel->block_size;
231 else {
232 if (count < 0)
233 size = -count;
234 else
235 size = count * channel->block_size;
236 }
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400237 data->io_stats.bytes_written += size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000238
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500239 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000240 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
241 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
242 goto error_out;
243 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400244
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400245 if ((data->align == 0) ||
246 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
247 actual = write(data->dev, buf, size);
248 if (actual != size) {
249 short_write:
250 retval = EXT2_ET_SHORT_WRITE;
251 goto error_out;
252 }
253 return 0;
254 }
255
256#ifdef ALIGN_DEBUG
257 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
258 (unsigned long) size);
259#endif
260 /*
261 * The buffer or size which we're trying to write isn't aligned
262 * to the O_DIRECT rules, so we need to do this the hard way...
263 */
264 while (size > 0) {
265 if (size < channel->block_size) {
266 actual = read(data->dev, data->bounce,
267 channel->block_size);
268 if (actual != channel->block_size) {
269 retval = EXT2_ET_SHORT_READ;
270 goto error_out;
271 }
272 }
273 actual = size;
274 if (size > channel->block_size)
275 actual = channel->block_size;
276 memcpy(data->bounce, buf, actual);
277 actual = write(data->dev, data->bounce, channel->block_size);
278 if (actual != channel->block_size)
279 goto short_write;
280 size -= actual;
281 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000282 }
283 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400284
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000285error_out:
286 if (channel->write_error)
287 retval = (channel->write_error)(channel, block, count, buf,
288 size, actual, retval);
289 return retval;
290}
291
292
293/*
294 * Here we implement the cache functions
295 */
296
297/* Allocate the cache buffers */
298static errcode_t alloc_cache(io_channel channel,
299 struct unix_private_data *data)
300{
301 errcode_t retval;
302 struct unix_cache *cache;
303 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400304
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000305 data->access_time = 0;
306 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
307 cache->block = 0;
308 cache->access_time = 0;
309 cache->dirty = 0;
310 cache->in_use = 0;
Theodore Ts'ofaafdb72010-09-23 13:06:31 -0400311 if (cache->buf)
312 ext2fs_free_mem(&cache->buf);
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400313 retval = ext2fs_get_memalign(channel->block_size,
314 data->align, &cache->buf);
315 if (retval)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000316 return retval;
317 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400318 if (data->align) {
319 if (data->bounce)
320 ext2fs_free_mem(&data->bounce);
321 retval = ext2fs_get_memalign(channel->block_size, data->align,
322 &data->bounce);
323 }
324 return retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000325}
326
327/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500328static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000329{
330 struct unix_cache *cache;
331 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400332
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000333 data->access_time = 0;
334 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
335 cache->block = 0;
336 cache->access_time = 0;
337 cache->dirty = 0;
338 cache->in_use = 0;
339 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400340 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000341 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400342 if (data->bounce)
343 ext2fs_free_mem(&data->bounce);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000344}
345
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400346#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000347/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500348 * Try to find a block in the cache. If the block is not found, and
349 * eldest is a non-zero pointer, then fill in eldest with the cache
350 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000351 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500352static struct unix_cache *find_cached_block(struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600353 unsigned long long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500354 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000355{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000356 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000357 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400358
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000359 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000360 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
361 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500362 if (!unused_cache)
363 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000364 continue;
365 }
366 if (cache->block == block) {
367 cache->access_time = ++data->access_time;
368 return cache;
369 }
370 if (!oldest_cache ||
371 (cache->access_time < oldest_cache->access_time))
372 oldest_cache = cache;
373 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500374 if (eldest)
375 *eldest = (unused_cache) ? unused_cache : oldest_cache;
376 return 0;
377}
378
379/*
380 * Reuse a particular cache entry for another block.
381 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500382static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600383 struct unix_cache *cache, unsigned long long block)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500384{
385 if (cache->dirty && cache->in_use)
386 raw_write_blk(channel, data, cache->block, 1, cache->buf);
387
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000388 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500389 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000390 cache->block = block;
391 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000392}
393
394/*
395 * Flush all of the blocks in the cache
396 */
397static errcode_t flush_cached_blocks(io_channel channel,
398 struct unix_private_data *data,
399 int invalidate)
400
401{
402 struct unix_cache *cache;
403 errcode_t retval, retval2;
404 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400405
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000406 retval2 = 0;
407 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
408 if (!cache->in_use)
409 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400410
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000411 if (invalidate)
412 cache->in_use = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400413
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000414 if (!cache->dirty)
415 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400416
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000417 retval = raw_write_blk(channel, data,
418 cache->block, 1, cache->buf);
419 if (retval)
420 retval2 = retval;
421 else
422 cache->dirty = 0;
423 }
424 return retval2;
425}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400426#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000427
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428static errcode_t unix_open(const char *name, int flags, io_channel *channel)
429{
430 io_channel io = NULL;
431 struct unix_private_data *data = NULL;
432 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000433 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500434 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400435#ifdef __linux__
436 struct utsname ut;
437#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000439 if (name == 0)
440 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400441 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000442 if (retval)
443 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000444 memset(io, 0, sizeof(struct struct_io_channel));
445 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400446 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000447 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000449
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400451 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000452 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000454
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455 strcpy(io->name, name);
456 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000457 io->block_size = 1024;
458 io->read_error = 0;
459 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000460 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461
462 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000463 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400464 data->io_stats.num_fields = 2;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000465
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000466 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500467 if (flags & IO_FLAG_EXCLUSIVE)
468 open_flags |= O_EXCL;
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400469 if (flags & IO_FLAG_DIRECT_IO)
470 open_flags |= O_DIRECT;
471 data->flags = flags;
472
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000473#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500474 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000475#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500476 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000477#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 if (data->dev < 0) {
479 retval = errno;
480 goto cleanup;
481 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500482
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400483#ifdef BLKSSZGET
484 if (flags & IO_FLAG_DIRECT_IO) {
485 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
486 data->align = io->block_size;
487 }
488#endif
489
490#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
491 /*
492 * Some operating systems require that the buffers be aligned,
493 * regardless of O_DIRECT
494 */
495 data->align = 512;
496#endif
497
498
499 if ((retval = alloc_cache(io, data)))
500 goto cleanup;
501
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -0500502#ifdef BLKROGET
503 if (flags & IO_FLAG_RW) {
504 int error;
505 int readonly = 0;
506
507 /* Is the block device actually writable? */
508 error = ioctl(data->dev, BLKROGET, &readonly);
509 if (!error && readonly) {
510 close(data->dev);
511 retval = EPERM;
512 goto cleanup;
513 }
514 }
515#endif
516
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500517#ifdef __linux__
518#undef RLIM_INFINITY
519#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
520#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
521#else
522#define RLIM_INFINITY (~0UL)
523#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500524 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400525 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
526 * block devices are wrongly getting hit by the filesize
527 * limit. This workaround isn't perfect, since it won't work
528 * if glibc wasn't built against 2.2 header files. (Sigh.)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400529 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500530 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400531 if ((flags & IO_FLAG_RW) &&
532 (uname(&ut) == 0) &&
533 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
534 (ut.release[2] == '4') && (ut.release[3] == '.') &&
535 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
536 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500537 (fstat(data->dev, &st) == 0) &&
538 (S_ISBLK(st.st_mode))) {
539 struct rlimit rlim;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400540
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500541 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500542 setrlimit(RLIMIT_FSIZE, &rlim);
543 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100544 if (((unsigned long) rlim.rlim_cur) <
545 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500546 rlim.rlim_cur = rlim.rlim_max;
547 setrlimit(RLIMIT_FSIZE, &rlim);
548 }
549 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500550#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551 *channel = io;
552 return 0;
553
554cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500556 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400557 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000559 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400560 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561 return retval;
562}
563
564static errcode_t unix_close(io_channel channel)
565{
566 struct unix_private_data *data;
567 errcode_t retval = 0;
568
Theodore Ts'of3db3561997-04-26 13:34:30 +0000569 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000570 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000571 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000572
573 if (--channel->refcount > 0)
574 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000575
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400576#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000577 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400578#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000579
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580 if (close(data->dev) < 0)
581 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500582 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500583
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400584 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400586 ext2fs_free_mem(&channel->name);
587 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588 return retval;
589}
590
591static errcode_t unix_set_blksize(io_channel channel, int blksize)
592{
593 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000594 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595
Theodore Ts'of3db3561997-04-26 13:34:30 +0000596 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000598 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
599
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400601#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000602 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000603 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400604#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400605
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000606 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500607 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000608 if ((retval = alloc_cache(channel, data)))
609 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610 }
611 return 0;
612}
613
614
Jose R. Santos59ecd322008-03-03 10:41:24 -0600615static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 int count, void *buf)
617{
618 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500619 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000620 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000621 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000622 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623
Theodore Ts'of3db3561997-04-26 13:34:30 +0000624 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000625 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000626 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400628#ifdef NO_IO_CACHE
629 return raw_read_blk(channel, data, block, count, buf);
630#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500632 * If we're doing an odd-sized read or a very large read,
633 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000634 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500635 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000636 if ((retval = flush_cached_blocks(channel, data, 0)))
637 return retval;
638 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000640
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000641 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000642 while (count > 0) {
643 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500644 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000645#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400646 printf("Using cached block %lu\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000647#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000648 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000649 count--;
650 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000651 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000652 continue;
653 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400654 if (count == 1) {
655 /*
656 * Special case where we read directly into the
657 * cache buffer; important in the O_DIRECT case
658 */
659 cache = reuse[0];
660 reuse_cache(channel, data, cache, block);
661 if ((retval = raw_read_blk(channel, data, block, 1,
662 cache->buf))) {
663 cache->in_use = 0;
664 return retval;
665 }
666 memcpy(cp, cache->buf, channel->block_size);
667 return 0;
668 }
669
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000670 /*
671 * Find the number of uncached blocks so we can do a
672 * single read request
673 */
674 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500675 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000676 break;
677#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400678 printf("Reading %d blocks starting at %lu\n", i, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000679#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000680 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000681 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400682
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000683 /* Save the results in the cache */
684 for (j=0; j < i; j++) {
685 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500686 cache = reuse[j];
687 reuse_cache(channel, data, cache, block++);
688 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000689 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000690 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691 }
692 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400693#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694}
695
Jose R. Santos59ecd322008-03-03 10:41:24 -0600696static errcode_t unix_read_blk(io_channel channel, unsigned long block,
697 int count, void *buf)
698{
699 return unix_read_blk64(channel, block, count, buf);
700}
701
702static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000703 int count, const void *buf)
704{
705 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500706 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500707 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000708 const char *cp;
709 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000710
Theodore Ts'of3db3561997-04-26 13:34:30 +0000711 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000713 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400715#ifdef NO_IO_CACHE
716 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400717#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000718 /*
719 * If we're doing an odd-sized write or a very large write,
720 * flush out the cache completely and then do a direct write.
721 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500722 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000723 if ((retval = flush_cached_blocks(channel, data, 1)))
724 return retval;
725 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726 }
727
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000728 /*
729 * For a moderate-sized multi-block write, first force a write
730 * if we're in write-through cache mode, and then fill the
731 * cache with the blocks.
732 */
733 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
734 if (writethrough)
735 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400736
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000737 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000738 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500739 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000740 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500741 cache = reuse;
742 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000743 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500744 memcpy(cache->buf, cp, channel->block_size);
745 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000746 count--;
747 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000748 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000749 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000750 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400751#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752}
753
Jose R. Santos59ecd322008-03-03 10:41:24 -0600754static errcode_t unix_write_blk(io_channel channel, unsigned long block,
755 int count, const void *buf)
756{
757 return unix_write_blk64(channel, block, count, buf);
758}
759
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000760static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
761 int size, const void *buf)
762{
763 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000764 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500765 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000766
767 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
768 data = (struct unix_private_data *) channel->private_data;
769 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
770
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400771 if (data->align != 0) {
772#ifdef ALIGN_DEBUG
773 printf("unix_write_byte: O_DIRECT fallback\n");
774#endif
775 return EXT2_ET_UNIMPLEMENTED;
776 }
777
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400778#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000779 /*
780 * Flush out the cache completely
781 */
782 if ((retval = flush_cached_blocks(channel, data, 1)))
783 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400784#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000785
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500786 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000787 return errno;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400788
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000789 actual = write(data->dev, buf, size);
790 if (actual != size)
791 return EXT2_ET_SHORT_WRITE;
792
793 return 0;
794}
795
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400797 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000798 */
799static errcode_t unix_flush(io_channel channel)
800{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000801 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000802 errcode_t retval = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400803
Theodore Ts'of3db3561997-04-26 13:34:30 +0000804 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
805 data = (struct unix_private_data *) channel->private_data;
806 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000807
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400808#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000809 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400810#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000811 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000812 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000813}
814
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400815static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500816 const char *arg)
817{
818 struct unix_private_data *data;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500819 unsigned long long tmp;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500820 char *end;
821
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);
825
826 if (!strcmp(option, "offset")) {
827 if (!arg)
828 return EXT2_ET_INVALID_ARGUMENT;
829
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500830 tmp = strtoull(arg, &end, 0);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500831 if (*end)
832 return EXT2_ET_INVALID_ARGUMENT;
833 data->offset = tmp;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500834 if (data->offset < 0)
835 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500836 return 0;
837 }
838 return EXT2_ET_INVALID_ARGUMENT;
839}
Lukas Czernere90a59e2010-11-18 03:38:36 +0000840
841#if defined(__linux__) && !defined(BLKDISCARD)
842#define BLKDISCARD _IO(0x12,119)
843#endif
844
845static errcode_t unix_discard(io_channel channel, unsigned long long block,
846 unsigned long long count)
847{
848#ifdef BLKDISCARD
849 struct unix_private_data *data;
850 __uint64_t range[2];
851 int ret;
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 range[0] = (__uint64_t)(block) * channel->block_size;
858 range[1] = (__uint64_t)(count) * channel->block_size;
859
860 ret = ioctl(data->dev, BLKDISCARD, &range);
861 if (ret < 0)
862 return errno;
863 return 0;
864#else
865 return EXT2_ET_UNIMPLEMENTED;
866#endif
867}