blob: 1df1fddc249d32a849ec2aaf44ab37f644d5409b [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);
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500118
Theodore Ts'of3db3561997-04-26 13:34:30 +0000119static struct struct_io_manager struct_unix_manager = {
120 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121 "Unix I/O Manager",
122 unix_open,
123 unix_close,
124 unix_set_blksize,
125 unix_read_blk,
126 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000127 unix_flush,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500128 unix_write_byte,
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400129 unix_set_option,
130 unix_get_stats,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600131 unix_read_blk64,
132 unix_write_blk64,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133};
134
135io_manager unix_io_manager = &struct_unix_manager;
136
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400137static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
138{
139 errcode_t retval = 0;
140
141 struct unix_private_data *data;
142
143 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
144 data = (struct unix_private_data *) channel->private_data;
145 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
146
147 if (stats)
148 *stats = &data->io_stats;
149
150 return retval;
151}
152
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000153/*
154 * Here are the raw I/O functions
155 */
156static errcode_t raw_read_blk(io_channel channel,
157 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600158 unsigned long long block,
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000159 int count, void *buf)
160{
161 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500162 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000163 ext2_loff_t location;
164 int actual = 0;
165
166 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400167 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500168 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000169 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
170 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
171 goto error_out;
172 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400173 if ((data->align == 0) ||
174 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
175 actual = read(data->dev, buf, size);
176 if (actual != size) {
177 short_read:
178 if (actual < 0)
179 actual = 0;
180 retval = EXT2_ET_SHORT_READ;
181 goto error_out;
182 }
183 return 0;
184 }
185
186#ifdef ALIGN_DEBUG
187 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
188 (unsigned long) size);
189#endif
190
191 /*
192 * The buffer or size which we're trying to read isn't aligned
193 * to the O_DIRECT rules, so we need to do this the hard way...
194 */
195 while (size > 0) {
196 actual = read(data->dev, data->bounce, channel->block_size);
197 if (actual != channel->block_size)
198 goto short_read;
199 actual = size;
200 if (size > channel->block_size)
201 actual = channel->block_size;
202 memcpy(buf, data->bounce, actual);
203 size -= actual;
204 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000205 }
206 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400207
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000208error_out:
209 memset((char *) buf+actual, 0, size-actual);
210 if (channel->read_error)
211 retval = (channel->read_error)(channel, block, count, buf,
212 size, actual, retval);
213 return retval;
214}
215
216static errcode_t raw_write_blk(io_channel channel,
217 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600218 unsigned long long block,
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000219 int count, const void *buf)
220{
Theodore Ts'o54434922003-12-07 01:28:50 -0500221 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000222 ext2_loff_t location;
223 int actual = 0;
224 errcode_t retval;
225
226 if (count == 1)
227 size = channel->block_size;
228 else {
229 if (count < 0)
230 size = -count;
231 else
232 size = count * channel->block_size;
233 }
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400234 data->io_stats.bytes_written += size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000235
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500236 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000237 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
238 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
239 goto error_out;
240 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400241
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400242 if ((data->align == 0) ||
243 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
244 actual = write(data->dev, buf, size);
245 if (actual != size) {
246 short_write:
247 retval = EXT2_ET_SHORT_WRITE;
248 goto error_out;
249 }
250 return 0;
251 }
252
253#ifdef ALIGN_DEBUG
254 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
255 (unsigned long) size);
256#endif
257 /*
258 * The buffer or size which we're trying to write isn't aligned
259 * to the O_DIRECT rules, so we need to do this the hard way...
260 */
261 while (size > 0) {
262 if (size < channel->block_size) {
263 actual = read(data->dev, data->bounce,
264 channel->block_size);
265 if (actual != channel->block_size) {
266 retval = EXT2_ET_SHORT_READ;
267 goto error_out;
268 }
269 }
270 actual = size;
271 if (size > channel->block_size)
272 actual = channel->block_size;
273 memcpy(data->bounce, buf, actual);
274 actual = write(data->dev, data->bounce, channel->block_size);
275 if (actual != channel->block_size)
276 goto short_write;
277 size -= actual;
278 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000279 }
280 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400281
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000282error_out:
283 if (channel->write_error)
284 retval = (channel->write_error)(channel, block, count, buf,
285 size, actual, retval);
286 return retval;
287}
288
289
290/*
291 * Here we implement the cache functions
292 */
293
294/* Allocate the cache buffers */
295static errcode_t alloc_cache(io_channel channel,
296 struct unix_private_data *data)
297{
298 errcode_t retval;
299 struct unix_cache *cache;
300 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400301
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000302 data->access_time = 0;
303 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
304 cache->block = 0;
305 cache->access_time = 0;
306 cache->dirty = 0;
307 cache->in_use = 0;
Theodore Ts'ofaafdb72010-09-23 13:06:31 -0400308 if (cache->buf)
309 ext2fs_free_mem(&cache->buf);
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400310 retval = ext2fs_get_memalign(channel->block_size,
311 data->align, &cache->buf);
312 if (retval)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000313 return retval;
314 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400315 if (data->align) {
316 if (data->bounce)
317 ext2fs_free_mem(&data->bounce);
318 retval = ext2fs_get_memalign(channel->block_size, data->align,
319 &data->bounce);
320 }
321 return retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000322}
323
324/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500325static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000326{
327 struct unix_cache *cache;
328 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400329
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000330 data->access_time = 0;
331 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
332 cache->block = 0;
333 cache->access_time = 0;
334 cache->dirty = 0;
335 cache->in_use = 0;
336 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400337 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000338 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400339 if (data->bounce)
340 ext2fs_free_mem(&data->bounce);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000341}
342
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400343#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000344/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500345 * Try to find a block in the cache. If the block is not found, and
346 * eldest is a non-zero pointer, then fill in eldest with the cache
347 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000348 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500349static struct unix_cache *find_cached_block(struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600350 unsigned long long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500351 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000352{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000353 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000354 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400355
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000356 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000357 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
358 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500359 if (!unused_cache)
360 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000361 continue;
362 }
363 if (cache->block == block) {
364 cache->access_time = ++data->access_time;
365 return cache;
366 }
367 if (!oldest_cache ||
368 (cache->access_time < oldest_cache->access_time))
369 oldest_cache = cache;
370 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500371 if (eldest)
372 *eldest = (unused_cache) ? unused_cache : oldest_cache;
373 return 0;
374}
375
376/*
377 * Reuse a particular cache entry for another block.
378 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500379static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600380 struct unix_cache *cache, unsigned long long block)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500381{
382 if (cache->dirty && cache->in_use)
383 raw_write_blk(channel, data, cache->block, 1, cache->buf);
384
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000385 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500386 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000387 cache->block = block;
388 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000389}
390
391/*
392 * Flush all of the blocks in the cache
393 */
394static errcode_t flush_cached_blocks(io_channel channel,
395 struct unix_private_data *data,
396 int invalidate)
397
398{
399 struct unix_cache *cache;
400 errcode_t retval, retval2;
401 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400402
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000403 retval2 = 0;
404 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
405 if (!cache->in_use)
406 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400407
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000408 if (invalidate)
409 cache->in_use = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400410
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000411 if (!cache->dirty)
412 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400413
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000414 retval = raw_write_blk(channel, data,
415 cache->block, 1, cache->buf);
416 if (retval)
417 retval2 = retval;
418 else
419 cache->dirty = 0;
420 }
421 return retval2;
422}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400423#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000424
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425static errcode_t unix_open(const char *name, int flags, io_channel *channel)
426{
427 io_channel io = NULL;
428 struct unix_private_data *data = NULL;
429 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000430 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500431 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400432#ifdef __linux__
433 struct utsname ut;
434#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000436 if (name == 0)
437 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400438 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000439 if (retval)
440 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000441 memset(io, 0, sizeof(struct struct_io_channel));
442 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400443 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000444 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000446
Theodore Ts'o3839e651997-04-26 13:21:57 +0000447 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400448 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000449 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000451
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 strcpy(io->name, name);
453 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000454 io->block_size = 1024;
455 io->read_error = 0;
456 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000457 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458
459 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000460 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400461 data->io_stats.num_fields = 2;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000462
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000463 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500464 if (flags & IO_FLAG_EXCLUSIVE)
465 open_flags |= O_EXCL;
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400466 if (flags & IO_FLAG_DIRECT_IO)
467 open_flags |= O_DIRECT;
468 data->flags = flags;
469
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000470#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500471 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000472#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500473 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000474#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 if (data->dev < 0) {
476 retval = errno;
477 goto cleanup;
478 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500479
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400480#ifdef BLKSSZGET
481 if (flags & IO_FLAG_DIRECT_IO) {
482 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
483 data->align = io->block_size;
484 }
485#endif
486
487#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
488 /*
489 * Some operating systems require that the buffers be aligned,
490 * regardless of O_DIRECT
491 */
492 data->align = 512;
493#endif
494
495
496 if ((retval = alloc_cache(io, data)))
497 goto cleanup;
498
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -0500499#ifdef BLKROGET
500 if (flags & IO_FLAG_RW) {
501 int error;
502 int readonly = 0;
503
504 /* Is the block device actually writable? */
505 error = ioctl(data->dev, BLKROGET, &readonly);
506 if (!error && readonly) {
507 close(data->dev);
508 retval = EPERM;
509 goto cleanup;
510 }
511 }
512#endif
513
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500514#ifdef __linux__
515#undef RLIM_INFINITY
516#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
517#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
518#else
519#define RLIM_INFINITY (~0UL)
520#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500521 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400522 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
523 * block devices are wrongly getting hit by the filesize
524 * limit. This workaround isn't perfect, since it won't work
525 * if glibc wasn't built against 2.2 header files. (Sigh.)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400526 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500527 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400528 if ((flags & IO_FLAG_RW) &&
529 (uname(&ut) == 0) &&
530 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
531 (ut.release[2] == '4') && (ut.release[3] == '.') &&
532 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
533 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500534 (fstat(data->dev, &st) == 0) &&
535 (S_ISBLK(st.st_mode))) {
536 struct rlimit rlim;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400537
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500538 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500539 setrlimit(RLIMIT_FSIZE, &rlim);
540 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100541 if (((unsigned long) rlim.rlim_cur) <
542 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500543 rlim.rlim_cur = rlim.rlim_max;
544 setrlimit(RLIMIT_FSIZE, &rlim);
545 }
546 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500547#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548 *channel = io;
549 return 0;
550
551cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500553 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400554 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000556 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400557 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 return retval;
559}
560
561static errcode_t unix_close(io_channel channel)
562{
563 struct unix_private_data *data;
564 errcode_t retval = 0;
565
Theodore Ts'of3db3561997-04-26 13:34:30 +0000566 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000568 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000569
570 if (--channel->refcount > 0)
571 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000572
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400573#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000574 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400575#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000576
Theodore Ts'o3839e651997-04-26 13:21:57 +0000577 if (close(data->dev) < 0)
578 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500579 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500580
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400581 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400583 ext2fs_free_mem(&channel->name);
584 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585 return retval;
586}
587
588static errcode_t unix_set_blksize(io_channel channel, int blksize)
589{
590 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000591 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592
Theodore Ts'of3db3561997-04-26 13:34:30 +0000593 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000595 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
596
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400598#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000599 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000600 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400601#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400602
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000603 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500604 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000605 if ((retval = alloc_cache(channel, data)))
606 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000607 }
608 return 0;
609}
610
611
Jose R. Santos59ecd322008-03-03 10:41:24 -0600612static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613 int count, void *buf)
614{
615 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500616 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000618 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000619 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000620
Theodore Ts'of3db3561997-04-26 13:34:30 +0000621 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000622 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000623 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400625#ifdef NO_IO_CACHE
626 return raw_read_blk(channel, data, block, count, buf);
627#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500629 * If we're doing an odd-sized read or a very large read,
630 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500632 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000633 if ((retval = flush_cached_blocks(channel, data, 0)))
634 return retval;
635 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000637
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000638 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000639 while (count > 0) {
640 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500641 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000642#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400643 printf("Using cached block %lu\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000644#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000645 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000646 count--;
647 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000648 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000649 continue;
650 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400651 if (count == 1) {
652 /*
653 * Special case where we read directly into the
654 * cache buffer; important in the O_DIRECT case
655 */
656 cache = reuse[0];
657 reuse_cache(channel, data, cache, block);
658 if ((retval = raw_read_blk(channel, data, block, 1,
659 cache->buf))) {
660 cache->in_use = 0;
661 return retval;
662 }
663 memcpy(cp, cache->buf, channel->block_size);
664 return 0;
665 }
666
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000667 /*
668 * Find the number of uncached blocks so we can do a
669 * single read request
670 */
671 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500672 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000673 break;
674#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400675 printf("Reading %d blocks starting at %lu\n", i, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000676#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000677 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000678 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400679
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000680 /* Save the results in the cache */
681 for (j=0; j < i; j++) {
682 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500683 cache = reuse[j];
684 reuse_cache(channel, data, cache, block++);
685 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000686 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000687 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688 }
689 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400690#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691}
692
Jose R. Santos59ecd322008-03-03 10:41:24 -0600693static errcode_t unix_read_blk(io_channel channel, unsigned long block,
694 int count, void *buf)
695{
696 return unix_read_blk64(channel, block, count, buf);
697}
698
699static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700 int count, const void *buf)
701{
702 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500703 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500704 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000705 const char *cp;
706 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000707
Theodore Ts'of3db3561997-04-26 13:34:30 +0000708 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000710 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000711
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400712#ifdef NO_IO_CACHE
713 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400714#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000715 /*
716 * If we're doing an odd-sized write or a very large write,
717 * flush out the cache completely and then do a direct write.
718 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500719 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000720 if ((retval = flush_cached_blocks(channel, data, 1)))
721 return retval;
722 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723 }
724
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000725 /*
726 * For a moderate-sized multi-block write, first force a write
727 * if we're in write-through cache mode, and then fill the
728 * cache with the blocks.
729 */
730 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
731 if (writethrough)
732 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400733
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000734 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000735 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500736 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000737 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500738 cache = reuse;
739 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000740 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500741 memcpy(cache->buf, cp, channel->block_size);
742 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000743 count--;
744 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000745 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000746 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000747 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400748#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749}
750
Jose R. Santos59ecd322008-03-03 10:41:24 -0600751static errcode_t unix_write_blk(io_channel channel, unsigned long block,
752 int count, const void *buf)
753{
754 return unix_write_blk64(channel, block, count, buf);
755}
756
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000757static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
758 int size, const void *buf)
759{
760 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000761 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500762 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000763
764 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
765 data = (struct unix_private_data *) channel->private_data;
766 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
767
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400768 if (data->align != 0) {
769#ifdef ALIGN_DEBUG
770 printf("unix_write_byte: O_DIRECT fallback\n");
771#endif
772 return EXT2_ET_UNIMPLEMENTED;
773 }
774
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400775#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000776 /*
777 * Flush out the cache completely
778 */
779 if ((retval = flush_cached_blocks(channel, data, 1)))
780 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400781#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000782
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500783 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000784 return errno;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400785
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000786 actual = write(data->dev, buf, size);
787 if (actual != size)
788 return EXT2_ET_SHORT_WRITE;
789
790 return 0;
791}
792
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400794 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795 */
796static errcode_t unix_flush(io_channel channel)
797{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000798 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000799 errcode_t retval = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400800
Theodore Ts'of3db3561997-04-26 13:34:30 +0000801 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
802 data = (struct unix_private_data *) channel->private_data;
803 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000804
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400805#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000806 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400807#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000808 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000809 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810}
811
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400812static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500813 const char *arg)
814{
815 struct unix_private_data *data;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500816 unsigned long long tmp;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500817 char *end;
818
819 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
820 data = (struct unix_private_data *) channel->private_data;
821 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
822
823 if (!strcmp(option, "offset")) {
824 if (!arg)
825 return EXT2_ET_INVALID_ARGUMENT;
826
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500827 tmp = strtoull(arg, &end, 0);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500828 if (*end)
829 return EXT2_ET_INVALID_ARGUMENT;
830 data->offset = tmp;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500831 if (data->offset < 0)
832 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500833 return 0;
834 }
835 return EXT2_ET_INVALID_ARGUMENT;
836}