blob: accd71f49699a1af10e049691947b7149bb0c1ea [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;
58 int dirty:1;
59 int in_use:1;
60};
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;
71 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +000072};
73
74static errcode_t unix_open(const char *name, int flags, io_channel *channel);
75static errcode_t unix_close(io_channel channel);
76static errcode_t unix_set_blksize(io_channel channel, int blksize);
77static errcode_t unix_read_blk(io_channel channel, unsigned long block,
78 int count, void *data);
79static errcode_t unix_write_blk(io_channel channel, unsigned long block,
80 int count, const void *data);
81static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +000082static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
83 int size, const void *data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -050085static void reuse_cache(io_channel channel, struct unix_private_data *data,
86 struct unix_cache *cache, unsigned long block);
87
Matthias Andreeb34cbdd2003-12-28 18:21:26 +010088#if defined(__CYGWIN__) || defined(__FreeBSD__)
89#define NEED_BOUNCE_BUFFER
90#else
91#undef NEED_BOUNCE_BUFFER
92#endif
93
Theodore Ts'of3db3561997-04-26 13:34:30 +000094static struct struct_io_manager struct_unix_manager = {
95 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 "Unix I/O Manager",
97 unix_open,
98 unix_close,
99 unix_set_blksize,
100 unix_read_blk,
101 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000102 unix_flush,
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100103#ifdef NEED_BOUNCE_BUFFER
Theodore Ts'offf45482003-04-13 00:44:19 -0400104 0
105#else
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000106 unix_write_byte
Theodore Ts'offf45482003-04-13 00:44:19 -0400107#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108};
109
110io_manager unix_io_manager = &struct_unix_manager;
111
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000112/*
113 * Here are the raw I/O functions
114 */
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100115#ifndef NEED_BOUNCE_BUFFER
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000116static errcode_t raw_read_blk(io_channel channel,
117 struct unix_private_data *data,
118 unsigned long block,
119 int count, void *buf)
120{
121 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500122 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000123 ext2_loff_t location;
124 int actual = 0;
125
126 size = (count < 0) ? -count : count * channel->block_size;
127 location = (ext2_loff_t) block * channel->block_size;
128 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
129 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
130 goto error_out;
131 }
132 actual = read(data->dev, buf, size);
133 if (actual != size) {
134 if (actual < 0)
135 actual = 0;
136 retval = EXT2_ET_SHORT_READ;
137 goto error_out;
138 }
139 return 0;
140
141error_out:
142 memset((char *) buf+actual, 0, size-actual);
143 if (channel->read_error)
144 retval = (channel->read_error)(channel, block, count, buf,
145 size, actual, retval);
146 return retval;
147}
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100148#else /* NEED_BOUNCE_BUFFER */
Theodore Ts'offf45482003-04-13 00:44:19 -0400149/*
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100150 * Windows and FreeBSD block devices only allow sector alignment IO in offset and size
Theodore Ts'offf45482003-04-13 00:44:19 -0400151 */
152static errcode_t raw_read_blk(io_channel channel,
153 struct unix_private_data *data,
154 unsigned long block,
155 int count, void *buf)
156{
157 errcode_t retval;
158 size_t size, alignsize, fragment;
159 ext2_loff_t location;
160 int total = 0, actual;
161#define BLOCKALIGN 512
162 char sector[BLOCKALIGN];
163
164 size = (count < 0) ? -count : count * channel->block_size;
165 location = (ext2_loff_t) block * channel->block_size;
166#ifdef DEBUG
167 printf("count=%d, size=%d, block=%d, blk_size=%d, location=%lx\n",
168 count, size, block, channel->block_size, location);
169#endif
170 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
171 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
172 goto error_out;
173 }
174 fragment = size % BLOCKALIGN;
175 alignsize = size - fragment;
176 if (alignsize) {
177 actual = read(data->dev, buf, alignsize);
178 if (actual != alignsize)
179 goto short_read;
180 }
181 if (fragment) {
182 actual = read(data->dev, sector, BLOCKALIGN);
183 if (actual != BLOCKALIGN)
184 goto short_read;
185 memcpy(buf+alignsize, sector, fragment);
186 }
187 return 0;
188
189short_read:
190 if (actual>0)
191 total += actual;
192 retval = EXT2_ET_SHORT_READ;
193
194error_out:
195 memset((char *) buf+total, 0, size-actual);
196 if (channel->read_error)
197 retval = (channel->read_error)(channel, block, count, buf,
198 size, actual, retval);
199 return retval;
200}
201#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000202
203static errcode_t raw_write_blk(io_channel channel,
204 struct unix_private_data *data,
205 unsigned long block,
206 int count, const void *buf)
207{
Theodore Ts'o54434922003-12-07 01:28:50 -0500208 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000209 ext2_loff_t location;
210 int actual = 0;
211 errcode_t retval;
212
213 if (count == 1)
214 size = channel->block_size;
215 else {
216 if (count < 0)
217 size = -count;
218 else
219 size = count * channel->block_size;
220 }
221
222 location = (ext2_loff_t) block * channel->block_size;
223 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
224 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
225 goto error_out;
226 }
227
228 actual = write(data->dev, buf, size);
229 if (actual != size) {
230 retval = EXT2_ET_SHORT_WRITE;
231 goto error_out;
232 }
233 return 0;
234
235error_out:
236 if (channel->write_error)
237 retval = (channel->write_error)(channel, block, count, buf,
238 size, actual, retval);
239 return retval;
240}
241
242
243/*
244 * Here we implement the cache functions
245 */
246
247/* Allocate the cache buffers */
248static errcode_t alloc_cache(io_channel channel,
249 struct unix_private_data *data)
250{
251 errcode_t retval;
252 struct unix_cache *cache;
253 int i;
254
255 data->access_time = 0;
256 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
257 cache->block = 0;
258 cache->access_time = 0;
259 cache->dirty = 0;
260 cache->in_use = 0;
261 if ((retval = ext2fs_get_mem(channel->block_size,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400262 &cache->buf)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000263 return retval;
264 }
265 return 0;
266}
267
268/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500269static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000270{
271 struct unix_cache *cache;
272 int i;
273
274 data->access_time = 0;
275 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
276 cache->block = 0;
277 cache->access_time = 0;
278 cache->dirty = 0;
279 cache->in_use = 0;
280 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400281 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000282 cache->buf = 0;
283 }
284}
285
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400286#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000287/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500288 * Try to find a block in the cache. If the block is not found, and
289 * eldest is a non-zero pointer, then fill in eldest with the cache
290 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000291 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500292static struct unix_cache *find_cached_block(struct unix_private_data *data,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000293 unsigned long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500294 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000295{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000296 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000297 int i;
298
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000299 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000300 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
301 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500302 if (!unused_cache)
303 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000304 continue;
305 }
306 if (cache->block == block) {
307 cache->access_time = ++data->access_time;
308 return cache;
309 }
310 if (!oldest_cache ||
311 (cache->access_time < oldest_cache->access_time))
312 oldest_cache = cache;
313 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500314 if (eldest)
315 *eldest = (unused_cache) ? unused_cache : oldest_cache;
316 return 0;
317}
318
319/*
320 * Reuse a particular cache entry for another block.
321 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500322static void reuse_cache(io_channel channel, struct unix_private_data *data,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500323 struct unix_cache *cache, unsigned long block)
324{
325 if (cache->dirty && cache->in_use)
326 raw_write_blk(channel, data, cache->block, 1, cache->buf);
327
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000328 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500329 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000330 cache->block = block;
331 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000332}
333
334/*
335 * Flush all of the blocks in the cache
336 */
337static errcode_t flush_cached_blocks(io_channel channel,
338 struct unix_private_data *data,
339 int invalidate)
340
341{
342 struct unix_cache *cache;
343 errcode_t retval, retval2;
344 int i;
345
346 retval2 = 0;
347 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
348 if (!cache->in_use)
349 continue;
350
351 if (invalidate)
352 cache->in_use = 0;
353
354 if (!cache->dirty)
355 continue;
356
357 retval = raw_write_blk(channel, data,
358 cache->block, 1, cache->buf);
359 if (retval)
360 retval2 = retval;
361 else
362 cache->dirty = 0;
363 }
364 return retval2;
365}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400366#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000367
Theodore Ts'o3839e651997-04-26 13:21:57 +0000368static errcode_t unix_open(const char *name, int flags, io_channel *channel)
369{
370 io_channel io = NULL;
371 struct unix_private_data *data = NULL;
372 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000373 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500374 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400375#ifdef __linux__
376 struct utsname ut;
377#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000379 if (name == 0)
380 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400381 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000382 if (retval)
383 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000384 memset(io, 0, sizeof(struct struct_io_channel));
385 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400386 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000387 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000388 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000389
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400391 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000392 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000393 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000394
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395 strcpy(io->name, name);
396 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000397 io->block_size = 1024;
398 io->read_error = 0;
399 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000400 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000401
402 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000403 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000404
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000405 if ((retval = alloc_cache(io, data)))
406 goto cleanup;
407
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000408 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
409#ifdef HAVE_OPEN64
410 data->dev = open64(name, open_flags);
411#else
412 data->dev = open(name, open_flags);
413#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000414 if (data->dev < 0) {
415 retval = errno;
416 goto cleanup;
417 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500418
419#ifdef __linux__
420#undef RLIM_INFINITY
421#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
422#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
423#else
424#define RLIM_INFINITY (~0UL)
425#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500426 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400427 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
428 * block devices are wrongly getting hit by the filesize
429 * limit. This workaround isn't perfect, since it won't work
430 * if glibc wasn't built against 2.2 header files. (Sigh.)
431 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500432 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400433 if ((flags & IO_FLAG_RW) &&
434 (uname(&ut) == 0) &&
435 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
436 (ut.release[2] == '4') && (ut.release[3] == '.') &&
437 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
438 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500439 (fstat(data->dev, &st) == 0) &&
440 (S_ISBLK(st.st_mode))) {
441 struct rlimit rlim;
442
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500443 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500444 setrlimit(RLIMIT_FSIZE, &rlim);
445 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100446 if (((unsigned long) rlim.rlim_cur) <
447 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500448 rlim.rlim_cur = rlim.rlim_max;
449 setrlimit(RLIMIT_FSIZE, &rlim);
450 }
451 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500452#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 *channel = io;
454 return 0;
455
456cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500458 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400459 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000461 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400462 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 return retval;
464}
465
466static errcode_t unix_close(io_channel channel)
467{
468 struct unix_private_data *data;
469 errcode_t retval = 0;
470
Theodore Ts'of3db3561997-04-26 13:34:30 +0000471 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000473 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000474
475 if (--channel->refcount > 0)
476 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000477
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400478#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000479 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400480#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000481
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482 if (close(data->dev) < 0)
483 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500484 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500485
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400486 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400488 ext2fs_free_mem(&channel->name);
489 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 return retval;
491}
492
493static errcode_t unix_set_blksize(io_channel channel, int blksize)
494{
495 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000496 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497
Theodore Ts'of3db3561997-04-26 13:34:30 +0000498 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000500 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
501
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400503#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000504 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000505 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400506#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000507
508 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500509 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000510 if ((retval = alloc_cache(channel, data)))
511 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 }
513 return 0;
514}
515
516
517static errcode_t unix_read_blk(io_channel channel, unsigned long block,
518 int count, void *buf)
519{
520 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500521 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000523 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000524 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525
Theodore Ts'of3db3561997-04-26 13:34:30 +0000526 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000528 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400530#ifdef NO_IO_CACHE
531 return raw_read_blk(channel, data, block, count, buf);
532#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500534 * If we're doing an odd-sized read or a very large read,
535 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500537 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000538 if ((retval = flush_cached_blocks(channel, data, 0)))
539 return retval;
540 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000542
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000543 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000544 while (count > 0) {
545 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500546 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000547#ifdef DEBUG
548 printf("Using cached block %d\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000549#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000550 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000551 count--;
552 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000553 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000554 continue;
555 }
556 /*
557 * Find the number of uncached blocks so we can do a
558 * single read request
559 */
560 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500561 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000562 break;
563#ifdef DEBUG
564 printf("Reading %d blocks starting at %d\n", i, block);
565#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000566 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000567 return retval;
568
569 /* Save the results in the cache */
570 for (j=0; j < i; j++) {
571 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500572 cache = reuse[j];
573 reuse_cache(channel, data, cache, block++);
574 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000575 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000576 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000577 }
578 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400579#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580}
581
582static errcode_t unix_write_blk(io_channel channel, unsigned long block,
583 int count, const void *buf)
584{
585 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500586 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500587 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000588 const char *cp;
589 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000590
Theodore Ts'of3db3561997-04-26 13:34:30 +0000591 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000593 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400595#ifdef NO_IO_CACHE
596 return raw_write_blk(channel, data, block, count, buf);
597#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000598 /*
599 * If we're doing an odd-sized write or a very large write,
600 * flush out the cache completely and then do a direct write.
601 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500602 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000603 if ((retval = flush_cached_blocks(channel, data, 1)))
604 return retval;
605 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606 }
607
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000608 /*
609 * For a moderate-sized multi-block write, first force a write
610 * if we're in write-through cache mode, and then fill the
611 * cache with the blocks.
612 */
613 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
614 if (writethrough)
615 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000617 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000618 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500619 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000620 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500621 cache = reuse;
622 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000623 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500624 memcpy(cache->buf, cp, channel->block_size);
625 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000626 count--;
627 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000628 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000629 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000630 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400631#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632}
633
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000634static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
635 int size, const void *buf)
636{
637 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000638 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500639 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000640
641 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
642 data = (struct unix_private_data *) channel->private_data;
643 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
644
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400645#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000646 /*
647 * Flush out the cache completely
648 */
649 if ((retval = flush_cached_blocks(channel, data, 1)))
650 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400651#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000652
653 if (lseek(data->dev, offset, SEEK_SET) < 0)
654 return errno;
655
656 actual = write(data->dev, buf, size);
657 if (actual != size)
658 return EXT2_ET_SHORT_WRITE;
659
660 return 0;
661}
662
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000664 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000665 */
666static errcode_t unix_flush(io_channel channel)
667{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000668 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000669 errcode_t retval = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000670
671 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
672 data = (struct unix_private_data *) channel->private_data;
673 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000674
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400675#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000676 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400677#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000678 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000679 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680}
681