blob: e1df0d6ce84c98a1f3966ac4097b33bd490abcd3 [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'offf45482003-04-13 00:44:19 -04007 * Includes support for Windows NT support under Cygwin.
8 *
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%
13 * This file may be redistributed under the terms of the GNU Public
14 * License.
15 * %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
20
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <stdio.h>
22#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000023#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000025#endif
Theodore Ts'oc4e749a1998-02-20 05:33:14 +000026#if HAVE_ERRNO_H
27#include <errno.h>
28#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include <fcntl.h>
30#include <time.h>
Theodore Ts'of154d2f2002-07-14 08:33:32 -040031#ifdef __linux__
32#include <sys/utsname.h>
33#endif
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000034#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000035#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000036#endif
37#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000038#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000039#endif
Theodore Ts'offf45482003-04-13 00:44:19 -040040#if HAVE_SYS_RESOURCE_H
Theodore Ts'o8880e752001-11-26 21:05:36 -050041#include <sys/resource.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040042#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000043
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000044#include "ext2_fs.h"
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000045#include "ext2fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000046
Theodore Ts'of3db3561997-04-26 13:34:30 +000047/*
48 * For checking structure magic numbers...
49 */
50
51#define EXT2_CHECK_MAGIC(struct, code) \
52 if ((struct)->magic != (code)) return (code)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000053
54struct unix_cache {
55 char *buf;
56 unsigned long block;
57 int access_time;
Matthias Andree83e692e2004-03-30 04:17:14 +020058 unsigned dirty:1;
59 unsigned in_use:1;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000060};
61
62#define CACHE_SIZE 8
Theodore Ts'o82c46602002-11-09 14:56:17 -050063#define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
64#define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000065
Theodore Ts'o3839e651997-04-26 13:21:57 +000066struct unix_private_data {
Theodore Ts'of3db3561997-04-26 13:34:30 +000067 int magic;
Theodore Ts'o3839e651997-04-26 13:21:57 +000068 int dev;
69 int flags;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000070 int access_time;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050071 ext2_loff_t offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000072 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +000073};
74
75static errcode_t unix_open(const char *name, int flags, io_channel *channel);
76static errcode_t unix_close(io_channel channel);
77static errcode_t unix_set_blksize(io_channel channel, int blksize);
78static errcode_t unix_read_blk(io_channel channel, unsigned long block,
79 int count, void *data);
80static errcode_t unix_write_blk(io_channel channel, unsigned long block,
81 int count, const void *data);
82static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +000083static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
84 int size, const void *data);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050085static errcode_t unix_set_option(io_channel channel, const char *option,
86 const char *arg);
Theodore Ts'o3839e651997-04-26 13:21:57 +000087
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -050088static void reuse_cache(io_channel channel, struct unix_private_data *data,
89 struct unix_cache *cache, unsigned long block);
90
Matthias Andree289e0552004-03-30 03:57:41 +020091/* __FreeBSD_kernel__ is defined by GNU/kFreeBSD - the FreeBSD kernel
92 * does not know buffered block devices - everything is raw. */
93#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
Matthias Andreeb34cbdd2003-12-28 18:21:26 +010094#define NEED_BOUNCE_BUFFER
95#else
96#undef NEED_BOUNCE_BUFFER
97#endif
98
Theodore Ts'of3db3561997-04-26 13:34:30 +000099static struct struct_io_manager struct_unix_manager = {
100 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 "Unix I/O Manager",
102 unix_open,
103 unix_close,
104 unix_set_blksize,
105 unix_read_blk,
106 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000107 unix_flush,
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100108#ifdef NEED_BOUNCE_BUFFER
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500109 0,
Theodore Ts'offf45482003-04-13 00:44:19 -0400110#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500111 unix_write_byte,
Theodore Ts'offf45482003-04-13 00:44:19 -0400112#endif
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500113 unix_set_option
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114};
115
116io_manager unix_io_manager = &struct_unix_manager;
117
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000118/*
119 * Here are the raw I/O functions
120 */
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100121#ifndef NEED_BOUNCE_BUFFER
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000122static errcode_t raw_read_blk(io_channel channel,
123 struct unix_private_data *data,
124 unsigned long block,
125 int count, void *buf)
126{
127 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500128 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000129 ext2_loff_t location;
130 int actual = 0;
131
132 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500133 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000134 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
135 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
136 goto error_out;
137 }
138 actual = read(data->dev, buf, size);
139 if (actual != size) {
140 if (actual < 0)
141 actual = 0;
142 retval = EXT2_ET_SHORT_READ;
143 goto error_out;
144 }
145 return 0;
146
147error_out:
148 memset((char *) buf+actual, 0, size-actual);
149 if (channel->read_error)
150 retval = (channel->read_error)(channel, block, count, buf,
151 size, actual, retval);
152 return retval;
153}
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100154#else /* NEED_BOUNCE_BUFFER */
Theodore Ts'offf45482003-04-13 00:44:19 -0400155/*
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100156 * Windows and FreeBSD block devices only allow sector alignment IO in offset and size
Theodore Ts'offf45482003-04-13 00:44:19 -0400157 */
158static errcode_t raw_read_blk(io_channel channel,
159 struct unix_private_data *data,
160 unsigned long block,
161 int count, void *buf)
162{
163 errcode_t retval;
164 size_t size, alignsize, fragment;
165 ext2_loff_t location;
166 int total = 0, actual;
167#define BLOCKALIGN 512
168 char sector[BLOCKALIGN];
169
170 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500171 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'offf45482003-04-13 00:44:19 -0400172#ifdef DEBUG
173 printf("count=%d, size=%d, block=%d, blk_size=%d, location=%lx\n",
174 count, size, block, channel->block_size, location);
175#endif
176 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
177 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
178 goto error_out;
179 }
180 fragment = size % BLOCKALIGN;
181 alignsize = size - fragment;
182 if (alignsize) {
183 actual = read(data->dev, buf, alignsize);
184 if (actual != alignsize)
185 goto short_read;
186 }
187 if (fragment) {
188 actual = read(data->dev, sector, BLOCKALIGN);
189 if (actual != BLOCKALIGN)
190 goto short_read;
191 memcpy(buf+alignsize, sector, fragment);
192 }
193 return 0;
194
195short_read:
196 if (actual>0)
197 total += actual;
198 retval = EXT2_ET_SHORT_READ;
199
200error_out:
201 memset((char *) buf+total, 0, size-actual);
202 if (channel->read_error)
203 retval = (channel->read_error)(channel, block, count, buf,
204 size, actual, retval);
205 return retval;
206}
207#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000208
209static errcode_t raw_write_blk(io_channel channel,
210 struct unix_private_data *data,
211 unsigned long block,
212 int count, const void *buf)
213{
Theodore Ts'o54434922003-12-07 01:28:50 -0500214 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000215 ext2_loff_t location;
216 int actual = 0;
217 errcode_t retval;
218
219 if (count == 1)
220 size = channel->block_size;
221 else {
222 if (count < 0)
223 size = -count;
224 else
225 size = count * channel->block_size;
226 }
227
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500228 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000229 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
230 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
231 goto error_out;
232 }
233
234 actual = write(data->dev, buf, size);
235 if (actual != size) {
236 retval = EXT2_ET_SHORT_WRITE;
237 goto error_out;
238 }
239 return 0;
240
241error_out:
242 if (channel->write_error)
243 retval = (channel->write_error)(channel, block, count, buf,
244 size, actual, retval);
245 return retval;
246}
247
248
249/*
250 * Here we implement the cache functions
251 */
252
253/* Allocate the cache buffers */
254static errcode_t alloc_cache(io_channel channel,
255 struct unix_private_data *data)
256{
257 errcode_t retval;
258 struct unix_cache *cache;
259 int i;
260
261 data->access_time = 0;
262 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
263 cache->block = 0;
264 cache->access_time = 0;
265 cache->dirty = 0;
266 cache->in_use = 0;
267 if ((retval = ext2fs_get_mem(channel->block_size,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400268 &cache->buf)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000269 return retval;
270 }
271 return 0;
272}
273
274/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500275static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000276{
277 struct unix_cache *cache;
278 int i;
279
280 data->access_time = 0;
281 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
282 cache->block = 0;
283 cache->access_time = 0;
284 cache->dirty = 0;
285 cache->in_use = 0;
286 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400287 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000288 cache->buf = 0;
289 }
290}
291
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400292#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000293/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500294 * Try to find a block in the cache. If the block is not found, and
295 * eldest is a non-zero pointer, then fill in eldest with the cache
296 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000297 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500298static struct unix_cache *find_cached_block(struct unix_private_data *data,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000299 unsigned long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500300 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000301{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000302 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000303 int i;
304
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000305 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000306 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
307 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500308 if (!unused_cache)
309 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000310 continue;
311 }
312 if (cache->block == block) {
313 cache->access_time = ++data->access_time;
314 return cache;
315 }
316 if (!oldest_cache ||
317 (cache->access_time < oldest_cache->access_time))
318 oldest_cache = cache;
319 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500320 if (eldest)
321 *eldest = (unused_cache) ? unused_cache : oldest_cache;
322 return 0;
323}
324
325/*
326 * Reuse a particular cache entry for another block.
327 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500328static void reuse_cache(io_channel channel, struct unix_private_data *data,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500329 struct unix_cache *cache, unsigned long block)
330{
331 if (cache->dirty && cache->in_use)
332 raw_write_blk(channel, data, cache->block, 1, cache->buf);
333
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000334 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500335 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000336 cache->block = block;
337 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000338}
339
340/*
341 * Flush all of the blocks in the cache
342 */
343static errcode_t flush_cached_blocks(io_channel channel,
344 struct unix_private_data *data,
345 int invalidate)
346
347{
348 struct unix_cache *cache;
349 errcode_t retval, retval2;
350 int i;
351
352 retval2 = 0;
353 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
354 if (!cache->in_use)
355 continue;
356
357 if (invalidate)
358 cache->in_use = 0;
359
360 if (!cache->dirty)
361 continue;
362
363 retval = raw_write_blk(channel, data,
364 cache->block, 1, cache->buf);
365 if (retval)
366 retval2 = retval;
367 else
368 cache->dirty = 0;
369 }
370 return retval2;
371}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400372#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000373
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374static errcode_t unix_open(const char *name, int flags, io_channel *channel)
375{
376 io_channel io = NULL;
377 struct unix_private_data *data = NULL;
378 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000379 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500380 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400381#ifdef __linux__
382 struct utsname ut;
383#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000385 if (name == 0)
386 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400387 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000388 if (retval)
389 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000390 memset(io, 0, sizeof(struct struct_io_channel));
391 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400392 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000393 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000395
Theodore Ts'o3839e651997-04-26 13:21:57 +0000396 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400397 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000398 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000399 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000400
Theodore Ts'o3839e651997-04-26 13:21:57 +0000401 strcpy(io->name, name);
402 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000403 io->block_size = 1024;
404 io->read_error = 0;
405 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000406 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000407
408 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000409 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000410
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000411 if ((retval = alloc_cache(io, data)))
412 goto cleanup;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500413
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000414 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
415#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500416 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000417#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500418 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000419#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420 if (data->dev < 0) {
421 retval = errno;
422 goto cleanup;
423 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500424
425#ifdef __linux__
426#undef RLIM_INFINITY
427#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
428#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
429#else
430#define RLIM_INFINITY (~0UL)
431#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500432 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400433 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
434 * block devices are wrongly getting hit by the filesize
435 * limit. This workaround isn't perfect, since it won't work
436 * if glibc wasn't built against 2.2 header files. (Sigh.)
437 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500438 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400439 if ((flags & IO_FLAG_RW) &&
440 (uname(&ut) == 0) &&
441 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
442 (ut.release[2] == '4') && (ut.release[3] == '.') &&
443 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
444 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500445 (fstat(data->dev, &st) == 0) &&
446 (S_ISBLK(st.st_mode))) {
447 struct rlimit rlim;
448
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500449 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500450 setrlimit(RLIMIT_FSIZE, &rlim);
451 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100452 if (((unsigned long) rlim.rlim_cur) <
453 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500454 rlim.rlim_cur = rlim.rlim_max;
455 setrlimit(RLIMIT_FSIZE, &rlim);
456 }
457 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500458#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 *channel = io;
460 return 0;
461
462cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500464 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400465 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000467 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400468 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 return retval;
470}
471
472static errcode_t unix_close(io_channel channel)
473{
474 struct unix_private_data *data;
475 errcode_t retval = 0;
476
Theodore Ts'of3db3561997-04-26 13:34:30 +0000477 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000479 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000480
481 if (--channel->refcount > 0)
482 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000483
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400484#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000485 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400486#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000487
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488 if (close(data->dev) < 0)
489 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500490 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500491
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400492 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400494 ext2fs_free_mem(&channel->name);
495 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 return retval;
497}
498
499static errcode_t unix_set_blksize(io_channel channel, int blksize)
500{
501 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000502 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503
Theodore Ts'of3db3561997-04-26 13:34:30 +0000504 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000506 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
507
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400509#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000510 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000511 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400512#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000513
514 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500515 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000516 if ((retval = alloc_cache(channel, data)))
517 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 }
519 return 0;
520}
521
522
523static errcode_t unix_read_blk(io_channel channel, unsigned long block,
524 int count, void *buf)
525{
526 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500527 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000529 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000530 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000531
Theodore Ts'of3db3561997-04-26 13:34:30 +0000532 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000534 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400536#ifdef NO_IO_CACHE
537 return raw_read_blk(channel, data, block, count, buf);
538#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500540 * If we're doing an odd-sized read or a very large read,
541 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500543 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000544 if ((retval = flush_cached_blocks(channel, data, 0)))
545 return retval;
546 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000548
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000549 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000550 while (count > 0) {
551 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500552 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000553#ifdef DEBUG
554 printf("Using cached block %d\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000555#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000556 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000557 count--;
558 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000559 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000560 continue;
561 }
562 /*
563 * Find the number of uncached blocks so we can do a
564 * single read request
565 */
566 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500567 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000568 break;
569#ifdef DEBUG
570 printf("Reading %d blocks starting at %d\n", i, block);
571#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000572 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000573 return retval;
574
575 /* Save the results in the cache */
576 for (j=0; j < i; j++) {
577 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500578 cache = reuse[j];
579 reuse_cache(channel, data, cache, block++);
580 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000581 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000582 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000583 }
584 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400585#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586}
587
588static errcode_t unix_write_blk(io_channel channel, unsigned long block,
589 int count, const void *buf)
590{
591 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500592 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500593 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000594 const char *cp;
595 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000596
Theodore Ts'of3db3561997-04-26 13:34:30 +0000597 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000599 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400601#ifdef NO_IO_CACHE
602 return raw_write_blk(channel, data, block, count, buf);
603#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000604 /*
605 * If we're doing an odd-sized write or a very large write,
606 * flush out the cache completely and then do a direct write.
607 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500608 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000609 if ((retval = flush_cached_blocks(channel, data, 1)))
610 return retval;
611 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000612 }
613
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000614 /*
615 * For a moderate-sized multi-block write, first force a write
616 * if we're in write-through cache mode, and then fill the
617 * cache with the blocks.
618 */
619 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
620 if (writethrough)
621 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000622
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000623 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000624 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500625 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000626 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500627 cache = reuse;
628 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000629 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500630 memcpy(cache->buf, cp, channel->block_size);
631 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000632 count--;
633 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000634 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000635 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400637#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638}
639
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000640static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
641 int size, const void *buf)
642{
643 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000644 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500645 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000646
647 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
648 data = (struct unix_private_data *) channel->private_data;
649 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
650
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400651#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000652 /*
653 * Flush out the cache completely
654 */
655 if ((retval = flush_cached_blocks(channel, data, 1)))
656 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400657#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000658
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500659 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000660 return errno;
661
662 actual = write(data->dev, buf, size);
663 if (actual != size)
664 return EXT2_ET_SHORT_WRITE;
665
666 return 0;
667}
668
Theodore Ts'o3839e651997-04-26 13:21:57 +0000669/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000670 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671 */
672static errcode_t unix_flush(io_channel channel)
673{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000674 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000675 errcode_t retval = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000676
677 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
678 data = (struct unix_private_data *) channel->private_data;
679 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000680
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400681#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000682 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400683#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000684 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000685 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000686}
687
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500688static errcode_t unix_set_option(io_channel channel, const char *option,
689 const char *arg)
690{
691 struct unix_private_data *data;
692 unsigned long tmp;
693 char *end;
694
695 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
696 data = (struct unix_private_data *) channel->private_data;
697 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
698
699 if (!strcmp(option, "offset")) {
700 if (!arg)
701 return EXT2_ET_INVALID_ARGUMENT;
702
703 tmp = strtoul(arg, &end, 0);
704 if (*end)
705 return EXT2_ET_INVALID_ARGUMENT;
706 data->offset = tmp;
707 return 0;
708 }
709 return EXT2_ET_INVALID_ARGUMENT;
710}