blob: 0f04cfa3d68377ba68522a61431759c8b32a412d [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;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500415 if (flags & IO_FLAG_EXCLUSIVE)
416 open_flags |= O_EXCL;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000417#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500418 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000419#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500420 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000421#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422 if (data->dev < 0) {
423 retval = errno;
424 goto cleanup;
425 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500426
427#ifdef __linux__
428#undef RLIM_INFINITY
429#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
430#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
431#else
432#define RLIM_INFINITY (~0UL)
433#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500434 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400435 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
436 * block devices are wrongly getting hit by the filesize
437 * limit. This workaround isn't perfect, since it won't work
438 * if glibc wasn't built against 2.2 header files. (Sigh.)
439 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500440 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400441 if ((flags & IO_FLAG_RW) &&
442 (uname(&ut) == 0) &&
443 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
444 (ut.release[2] == '4') && (ut.release[3] == '.') &&
445 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
446 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500447 (fstat(data->dev, &st) == 0) &&
448 (S_ISBLK(st.st_mode))) {
449 struct rlimit rlim;
450
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500451 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500452 setrlimit(RLIMIT_FSIZE, &rlim);
453 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100454 if (((unsigned long) rlim.rlim_cur) <
455 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500456 rlim.rlim_cur = rlim.rlim_max;
457 setrlimit(RLIMIT_FSIZE, &rlim);
458 }
459 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500460#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 *channel = io;
462 return 0;
463
464cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500466 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400467 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000469 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400470 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471 return retval;
472}
473
474static errcode_t unix_close(io_channel channel)
475{
476 struct unix_private_data *data;
477 errcode_t retval = 0;
478
Theodore Ts'of3db3561997-04-26 13:34:30 +0000479 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000481 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000482
483 if (--channel->refcount > 0)
484 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000485
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400486#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000487 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400488#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000489
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 if (close(data->dev) < 0)
491 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500492 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500493
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400494 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400496 ext2fs_free_mem(&channel->name);
497 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 return retval;
499}
500
501static errcode_t unix_set_blksize(io_channel channel, int blksize)
502{
503 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000504 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505
Theodore Ts'of3db3561997-04-26 13:34:30 +0000506 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000508 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
509
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400511#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000512 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000513 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400514#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000515
516 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500517 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000518 if ((retval = alloc_cache(channel, data)))
519 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000520 }
521 return 0;
522}
523
524
525static errcode_t unix_read_blk(io_channel channel, unsigned long block,
526 int count, void *buf)
527{
528 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500529 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000531 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000532 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533
Theodore Ts'of3db3561997-04-26 13:34:30 +0000534 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000536 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400538#ifdef NO_IO_CACHE
539 return raw_read_blk(channel, data, block, count, buf);
540#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500542 * If we're doing an odd-sized read or a very large read,
543 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500545 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000546 if ((retval = flush_cached_blocks(channel, data, 0)))
547 return retval;
548 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000550
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000551 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000552 while (count > 0) {
553 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500554 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000555#ifdef DEBUG
556 printf("Using cached block %d\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000557#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000558 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000559 count--;
560 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000561 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000562 continue;
563 }
564 /*
565 * Find the number of uncached blocks so we can do a
566 * single read request
567 */
568 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500569 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000570 break;
571#ifdef DEBUG
572 printf("Reading %d blocks starting at %d\n", i, block);
573#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000574 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000575 return retval;
576
577 /* Save the results in the cache */
578 for (j=0; j < i; j++) {
579 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500580 cache = reuse[j];
581 reuse_cache(channel, data, cache, block++);
582 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000583 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000584 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585 }
586 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400587#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588}
589
590static errcode_t unix_write_blk(io_channel channel, unsigned long block,
591 int count, const void *buf)
592{
593 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500594 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500595 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000596 const char *cp;
597 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598
Theodore Ts'of3db3561997-04-26 13:34:30 +0000599 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000601 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000602
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400603#ifdef NO_IO_CACHE
604 return raw_write_blk(channel, data, block, count, buf);
605#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000606 /*
607 * If we're doing an odd-sized write or a very large write,
608 * flush out the cache completely and then do a direct write.
609 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500610 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000611 if ((retval = flush_cached_blocks(channel, data, 1)))
612 return retval;
613 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614 }
615
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000616 /*
617 * For a moderate-sized multi-block write, first force a write
618 * if we're in write-through cache mode, and then fill the
619 * cache with the blocks.
620 */
621 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
622 if (writethrough)
623 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000625 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000626 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500627 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000628 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500629 cache = reuse;
630 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000631 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500632 memcpy(cache->buf, cp, channel->block_size);
633 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000634 count--;
635 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000636 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000637 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400639#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000640}
641
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000642static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
643 int size, const void *buf)
644{
645 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000646 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500647 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000648
649 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
650 data = (struct unix_private_data *) channel->private_data;
651 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
652
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400653#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000654 /*
655 * Flush out the cache completely
656 */
657 if ((retval = flush_cached_blocks(channel, data, 1)))
658 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400659#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000660
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500661 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000662 return errno;
663
664 actual = write(data->dev, buf, size);
665 if (actual != size)
666 return EXT2_ET_SHORT_WRITE;
667
668 return 0;
669}
670
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000672 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673 */
674static errcode_t unix_flush(io_channel channel)
675{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000676 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000677 errcode_t retval = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000678
679 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
680 data = (struct unix_private_data *) channel->private_data;
681 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000682
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400683#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000684 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400685#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000686 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000687 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688}
689
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500690static errcode_t unix_set_option(io_channel channel, const char *option,
691 const char *arg)
692{
693 struct unix_private_data *data;
694 unsigned long tmp;
695 char *end;
696
697 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
698 data = (struct unix_private_data *) channel->private_data;
699 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
700
701 if (!strcmp(option, "offset")) {
702 if (!arg)
703 return EXT2_ET_INVALID_ARGUMENT;
704
705 tmp = strtoul(arg, &end, 0);
706 if (*end)
707 return EXT2_ET_INVALID_ARGUMENT;
708 data->offset = tmp;
709 return 0;
710 }
711 return EXT2_ET_INVALID_ARGUMENT;
712}