blob: 82e0fe44867f6b948a6462273a87b5f1bc351857 [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
Lukas Czernerd8665992010-11-18 03:38:37 +0000428#ifdef __linux__
429#ifndef BLKDISCARDZEROES
430#define BLKDISCARDZEROES _IO(0x12,124)
431#endif
432#endif
433
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434static errcode_t unix_open(const char *name, int flags, io_channel *channel)
435{
436 io_channel io = NULL;
437 struct unix_private_data *data = NULL;
438 errcode_t retval;
Lukas Czernerd8665992010-11-18 03:38:37 +0000439 int open_flags, zeroes = 0;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500440 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400441#ifdef __linux__
442 struct utsname ut;
443#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000445 if (name == 0)
446 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400447 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000448 if (retval)
449 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000450 memset(io, 0, sizeof(struct struct_io_channel));
451 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400452 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000453 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000455
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400457 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000458 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000460
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 strcpy(io->name, name);
462 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000463 io->block_size = 1024;
464 io->read_error = 0;
465 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000466 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467
468 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000469 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400470 data->io_stats.num_fields = 2;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000471
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000472 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500473 if (flags & IO_FLAG_EXCLUSIVE)
474 open_flags |= O_EXCL;
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400475 if (flags & IO_FLAG_DIRECT_IO)
476 open_flags |= O_DIRECT;
477 data->flags = flags;
478
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000479#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500480 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000481#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500482 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000483#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484 if (data->dev < 0) {
485 retval = errno;
486 goto cleanup;
487 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500488
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400489#ifdef BLKSSZGET
490 if (flags & IO_FLAG_DIRECT_IO) {
491 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
492 data->align = io->block_size;
493 }
494#endif
495
Lukas Czernerd8665992010-11-18 03:38:37 +0000496#ifdef BLKDISCARDZEROES
497 ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
498 if (zeroes)
499 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
500#endif
501
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400502#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
503 /*
504 * Some operating systems require that the buffers be aligned,
505 * regardless of O_DIRECT
506 */
507 data->align = 512;
508#endif
509
510
511 if ((retval = alloc_cache(io, data)))
512 goto cleanup;
513
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -0500514#ifdef BLKROGET
515 if (flags & IO_FLAG_RW) {
516 int error;
517 int readonly = 0;
518
519 /* Is the block device actually writable? */
520 error = ioctl(data->dev, BLKROGET, &readonly);
521 if (!error && readonly) {
522 close(data->dev);
523 retval = EPERM;
524 goto cleanup;
525 }
526 }
527#endif
528
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500529#ifdef __linux__
530#undef RLIM_INFINITY
531#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
532#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
533#else
534#define RLIM_INFINITY (~0UL)
535#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500536 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400537 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
538 * block devices are wrongly getting hit by the filesize
539 * limit. This workaround isn't perfect, since it won't work
540 * if glibc wasn't built against 2.2 header files. (Sigh.)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400541 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500542 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400543 if ((flags & IO_FLAG_RW) &&
544 (uname(&ut) == 0) &&
545 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
546 (ut.release[2] == '4') && (ut.release[3] == '.') &&
547 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
548 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500549 (fstat(data->dev, &st) == 0) &&
550 (S_ISBLK(st.st_mode))) {
551 struct rlimit rlim;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400552
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500553 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500554 setrlimit(RLIMIT_FSIZE, &rlim);
555 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100556 if (((unsigned long) rlim.rlim_cur) <
557 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500558 rlim.rlim_cur = rlim.rlim_max;
559 setrlimit(RLIMIT_FSIZE, &rlim);
560 }
561 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500562#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000563 *channel = io;
564 return 0;
565
566cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500568 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400569 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000570 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000571 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400572 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000573 return retval;
574}
575
576static errcode_t unix_close(io_channel channel)
577{
578 struct unix_private_data *data;
579 errcode_t retval = 0;
580
Theodore Ts'of3db3561997-04-26 13:34:30 +0000581 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000583 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000584
585 if (--channel->refcount > 0)
586 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000587
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400588#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000589 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400590#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000591
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 if (close(data->dev) < 0)
593 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500594 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500595
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400596 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400598 ext2fs_free_mem(&channel->name);
599 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 return retval;
601}
602
603static errcode_t unix_set_blksize(io_channel channel, int blksize)
604{
605 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000606 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000607
Theodore Ts'of3db3561997-04-26 13:34:30 +0000608 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000610 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
611
Theodore Ts'o3839e651997-04-26 13:21:57 +0000612 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400613#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000614 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000615 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400616#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400617
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000618 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500619 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000620 if ((retval = alloc_cache(channel, data)))
621 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000622 }
623 return 0;
624}
625
626
Jose R. Santos59ecd322008-03-03 10:41:24 -0600627static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 int count, void *buf)
629{
630 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500631 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000633 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000634 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635
Theodore Ts'of3db3561997-04-26 13:34:30 +0000636 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000638 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400640#ifdef NO_IO_CACHE
641 return raw_read_blk(channel, data, block, count, buf);
642#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500644 * If we're doing an odd-sized read or a very large read,
645 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000646 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500647 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000648 if ((retval = flush_cached_blocks(channel, data, 0)))
649 return retval;
650 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000652
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000653 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000654 while (count > 0) {
655 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500656 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000657#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400658 printf("Using cached block %lu\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000659#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000660 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000661 count--;
662 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000663 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000664 continue;
665 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400666 if (count == 1) {
667 /*
668 * Special case where we read directly into the
669 * cache buffer; important in the O_DIRECT case
670 */
671 cache = reuse[0];
672 reuse_cache(channel, data, cache, block);
673 if ((retval = raw_read_blk(channel, data, block, 1,
674 cache->buf))) {
675 cache->in_use = 0;
676 return retval;
677 }
678 memcpy(cp, cache->buf, channel->block_size);
679 return 0;
680 }
681
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000682 /*
683 * Find the number of uncached blocks so we can do a
684 * single read request
685 */
686 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500687 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000688 break;
689#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400690 printf("Reading %d blocks starting at %lu\n", i, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000691#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000692 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000693 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400694
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000695 /* Save the results in the cache */
696 for (j=0; j < i; j++) {
697 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500698 cache = reuse[j];
699 reuse_cache(channel, data, cache, block++);
700 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000701 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000702 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000703 }
704 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400705#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000706}
707
Jose R. Santos59ecd322008-03-03 10:41:24 -0600708static errcode_t unix_read_blk(io_channel channel, unsigned long block,
709 int count, void *buf)
710{
711 return unix_read_blk64(channel, block, count, buf);
712}
713
714static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000715 int count, const void *buf)
716{
717 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500718 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500719 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000720 const char *cp;
721 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000722
Theodore Ts'of3db3561997-04-26 13:34:30 +0000723 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000724 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000725 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400727#ifdef NO_IO_CACHE
728 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400729#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000730 /*
731 * If we're doing an odd-sized write or a very large write,
732 * flush out the cache completely and then do a direct write.
733 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500734 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000735 if ((retval = flush_cached_blocks(channel, data, 1)))
736 return retval;
737 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000738 }
739
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000740 /*
741 * For a moderate-sized multi-block write, first force a write
742 * if we're in write-through cache mode, and then fill the
743 * cache with the blocks.
744 */
745 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
746 if (writethrough)
747 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400748
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000749 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000750 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500751 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000752 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500753 cache = reuse;
754 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000755 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500756 memcpy(cache->buf, cp, channel->block_size);
757 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000758 count--;
759 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000760 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000761 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000762 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400763#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000764}
765
Jose R. Santos59ecd322008-03-03 10:41:24 -0600766static errcode_t unix_write_blk(io_channel channel, unsigned long block,
767 int count, const void *buf)
768{
769 return unix_write_blk64(channel, block, count, buf);
770}
771
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000772static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
773 int size, const void *buf)
774{
775 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000776 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500777 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000778
779 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
780 data = (struct unix_private_data *) channel->private_data;
781 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
782
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400783 if (data->align != 0) {
784#ifdef ALIGN_DEBUG
785 printf("unix_write_byte: O_DIRECT fallback\n");
786#endif
787 return EXT2_ET_UNIMPLEMENTED;
788 }
789
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400790#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000791 /*
792 * Flush out the cache completely
793 */
794 if ((retval = flush_cached_blocks(channel, data, 1)))
795 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400796#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000797
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500798 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000799 return errno;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400800
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000801 actual = write(data->dev, buf, size);
802 if (actual != size)
803 return EXT2_ET_SHORT_WRITE;
804
805 return 0;
806}
807
Theodore Ts'o3839e651997-04-26 13:21:57 +0000808/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400809 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810 */
811static errcode_t unix_flush(io_channel channel)
812{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000813 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000814 errcode_t retval = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400815
Theodore Ts'of3db3561997-04-26 13:34:30 +0000816 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
817 data = (struct unix_private_data *) channel->private_data;
818 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000819
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400820#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000821 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400822#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000823 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000824 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000825}
826
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400827static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500828 const char *arg)
829{
830 struct unix_private_data *data;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500831 unsigned long long tmp;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500832 char *end;
833
834 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
835 data = (struct unix_private_data *) channel->private_data;
836 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
837
838 if (!strcmp(option, "offset")) {
839 if (!arg)
840 return EXT2_ET_INVALID_ARGUMENT;
841
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500842 tmp = strtoull(arg, &end, 0);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500843 if (*end)
844 return EXT2_ET_INVALID_ARGUMENT;
845 data->offset = tmp;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500846 if (data->offset < 0)
847 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500848 return 0;
849 }
850 return EXT2_ET_INVALID_ARGUMENT;
851}
Lukas Czernere90a59e2010-11-18 03:38:36 +0000852
853#if defined(__linux__) && !defined(BLKDISCARD)
854#define BLKDISCARD _IO(0x12,119)
855#endif
856
857static errcode_t unix_discard(io_channel channel, unsigned long long block,
858 unsigned long long count)
859{
860#ifdef BLKDISCARD
861 struct unix_private_data *data;
862 __uint64_t range[2];
863 int ret;
864
865 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
866 data = (struct unix_private_data *) channel->private_data;
867 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
868
869 range[0] = (__uint64_t)(block) * channel->block_size;
870 range[1] = (__uint64_t)(count) * channel->block_size;
871
872 ret = ioctl(data->dev, BLKDISCARD, &range);
873 if (ret < 0)
874 return errno;
875 return 0;
876#else
877 return EXT2_ET_UNIMPLEMENTED;
878#endif
879}