blob: a76f9d81193f2a36e39f08867f55eb64a33f9024 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * unix_io.c --- This is the Unix I/O interface to the I/O manager.
3 *
4 * Implements a one-block write-through cache.
5 *
Theodore Ts'o64e1b272002-02-23 18:50:32 -05006 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
7 * 2002 by Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00008 *
9 * %Begin-Header%
10 * This file may be redistributed under the terms of the GNU Public
11 * License.
12 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000013 */
14
Theodore Ts'odc5f68c2000-05-25 23:31:54 +000015#define _LARGEFILE_SOURCE
16#define _LARGEFILE64_SOURCE
17
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <stdio.h>
19#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000020#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000022#endif
Theodore Ts'oc4e749a1998-02-20 05:33:14 +000023#if HAVE_ERRNO_H
24#include <errno.h>
25#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <fcntl.h>
27#include <time.h>
Theodore Ts'of154d2f2002-07-14 08:33:32 -040028#ifdef __linux__
29#include <sys/utsname.h>
30#endif
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000031#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000032#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000033#endif
34#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000035#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000036#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -050037#include <sys/resource.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000038
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000039#include "ext2_fs.h"
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000040#include "ext2fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000041
Theodore Ts'of3db3561997-04-26 13:34:30 +000042/*
43 * For checking structure magic numbers...
44 */
45
46#define EXT2_CHECK_MAGIC(struct, code) \
47 if ((struct)->magic != (code)) return (code)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000048
49struct unix_cache {
50 char *buf;
51 unsigned long block;
52 int access_time;
53 int dirty:1;
54 int in_use:1;
55};
56
57#define CACHE_SIZE 8
Theodore Ts'o82c46602002-11-09 14:56:17 -050058#define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
59#define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000060
Theodore Ts'o3839e651997-04-26 13:21:57 +000061struct unix_private_data {
Theodore Ts'of3db3561997-04-26 13:34:30 +000062 int magic;
Theodore Ts'o3839e651997-04-26 13:21:57 +000063 int dev;
64 int flags;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000065 int access_time;
66 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +000067};
68
69static errcode_t unix_open(const char *name, int flags, io_channel *channel);
70static errcode_t unix_close(io_channel channel);
71static errcode_t unix_set_blksize(io_channel channel, int blksize);
72static errcode_t unix_read_blk(io_channel channel, unsigned long block,
73 int count, void *data);
74static errcode_t unix_write_blk(io_channel channel, unsigned long block,
75 int count, const void *data);
76static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +000077static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
78 int size, const void *data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000079
Theodore Ts'of3db3561997-04-26 13:34:30 +000080static struct struct_io_manager struct_unix_manager = {
81 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 "Unix I/O Manager",
83 unix_open,
84 unix_close,
85 unix_set_blksize,
86 unix_read_blk,
87 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +000088 unix_flush,
89 unix_write_byte
Theodore Ts'o3839e651997-04-26 13:21:57 +000090};
91
92io_manager unix_io_manager = &struct_unix_manager;
93
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000094/*
95 * Here are the raw I/O functions
96 */
97static errcode_t raw_read_blk(io_channel channel,
98 struct unix_private_data *data,
99 unsigned long block,
100 int count, void *buf)
101{
102 errcode_t retval;
103 size_t size;
104 ext2_loff_t location;
105 int actual = 0;
106
107 size = (count < 0) ? -count : count * channel->block_size;
108 location = (ext2_loff_t) block * channel->block_size;
109 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
110 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
111 goto error_out;
112 }
113 actual = read(data->dev, buf, size);
114 if (actual != size) {
115 if (actual < 0)
116 actual = 0;
117 retval = EXT2_ET_SHORT_READ;
118 goto error_out;
119 }
120 return 0;
121
122error_out:
123 memset((char *) buf+actual, 0, size-actual);
124 if (channel->read_error)
125 retval = (channel->read_error)(channel, block, count, buf,
126 size, actual, retval);
127 return retval;
128}
129
130static errcode_t raw_write_blk(io_channel channel,
131 struct unix_private_data *data,
132 unsigned long block,
133 int count, const void *buf)
134{
135 size_t size;
136 ext2_loff_t location;
137 int actual = 0;
138 errcode_t retval;
139
140 if (count == 1)
141 size = channel->block_size;
142 else {
143 if (count < 0)
144 size = -count;
145 else
146 size = count * channel->block_size;
147 }
148
149 location = (ext2_loff_t) block * channel->block_size;
150 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
151 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
152 goto error_out;
153 }
154
155 actual = write(data->dev, buf, size);
156 if (actual != size) {
157 retval = EXT2_ET_SHORT_WRITE;
158 goto error_out;
159 }
160 return 0;
161
162error_out:
163 if (channel->write_error)
164 retval = (channel->write_error)(channel, block, count, buf,
165 size, actual, retval);
166 return retval;
167}
168
169
170/*
171 * Here we implement the cache functions
172 */
173
174/* Allocate the cache buffers */
175static errcode_t alloc_cache(io_channel channel,
176 struct unix_private_data *data)
177{
178 errcode_t retval;
179 struct unix_cache *cache;
180 int i;
181
182 data->access_time = 0;
183 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
184 cache->block = 0;
185 cache->access_time = 0;
186 cache->dirty = 0;
187 cache->in_use = 0;
188 if ((retval = ext2fs_get_mem(channel->block_size,
189 (void **) &cache->buf)))
190 return retval;
191 }
192 return 0;
193}
194
195/* Free the cache buffers */
196static void free_cache(io_channel channel,
197 struct unix_private_data *data)
198{
199 struct unix_cache *cache;
200 int i;
201
202 data->access_time = 0;
203 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
204 cache->block = 0;
205 cache->access_time = 0;
206 cache->dirty = 0;
207 cache->in_use = 0;
208 if (cache->buf)
209 ext2fs_free_mem((void **) &cache->buf);
210 cache->buf = 0;
211 }
212}
213
214/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500215 * Try to find a block in the cache. If the block is not found, and
216 * eldest is a non-zero pointer, then fill in eldest with the cache
217 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000218 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000219static struct unix_cache *find_cached_block(io_channel channel,
220 struct unix_private_data *data,
221 unsigned long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500222 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000223{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000224 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000225 int i;
226
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000227 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000228 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
229 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500230 if (!unused_cache)
231 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000232 continue;
233 }
234 if (cache->block == block) {
235 cache->access_time = ++data->access_time;
236 return cache;
237 }
238 if (!oldest_cache ||
239 (cache->access_time < oldest_cache->access_time))
240 oldest_cache = cache;
241 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500242 if (eldest)
243 *eldest = (unused_cache) ? unused_cache : oldest_cache;
244 return 0;
245}
246
247/*
248 * Reuse a particular cache entry for another block.
249 */
250void reuse_cache(io_channel channel, struct unix_private_data *data,
251 struct unix_cache *cache, unsigned long block)
252{
253 if (cache->dirty && cache->in_use)
254 raw_write_blk(channel, data, cache->block, 1, cache->buf);
255
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000256 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500257 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000258 cache->block = block;
259 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000260}
261
262/*
263 * Flush all of the blocks in the cache
264 */
265static errcode_t flush_cached_blocks(io_channel channel,
266 struct unix_private_data *data,
267 int invalidate)
268
269{
270 struct unix_cache *cache;
271 errcode_t retval, retval2;
272 int i;
273
274 retval2 = 0;
275 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
276 if (!cache->in_use)
277 continue;
278
279 if (invalidate)
280 cache->in_use = 0;
281
282 if (!cache->dirty)
283 continue;
284
285 retval = raw_write_blk(channel, data,
286 cache->block, 1, cache->buf);
287 if (retval)
288 retval2 = retval;
289 else
290 cache->dirty = 0;
291 }
292 return retval2;
293}
294
295
296
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297static errcode_t unix_open(const char *name, int flags, io_channel *channel)
298{
299 io_channel io = NULL;
300 struct unix_private_data *data = NULL;
301 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000302 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500303 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400304#ifdef __linux__
305 struct utsname ut;
306#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000308 if (name == 0)
309 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000310 retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
311 (void **) &io);
312 if (retval)
313 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000314 memset(io, 0, sizeof(struct struct_io_channel));
315 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000316 retval = ext2fs_get_mem(sizeof(struct unix_private_data),
317 (void **) &data);
318 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000320
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321 io->manager = unix_io_manager;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000322 retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
323 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000324 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000325
Theodore Ts'o3839e651997-04-26 13:21:57 +0000326 strcpy(io->name, name);
327 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000328 io->block_size = 1024;
329 io->read_error = 0;
330 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000331 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000332
333 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000334 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000335
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000336 if ((retval = alloc_cache(io, data)))
337 goto cleanup;
338
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000339 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
340#ifdef HAVE_OPEN64
341 data->dev = open64(name, open_flags);
342#else
343 data->dev = open(name, open_flags);
344#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 if (data->dev < 0) {
346 retval = errno;
347 goto cleanup;
348 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500349
350#ifdef __linux__
351#undef RLIM_INFINITY
352#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
353#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
354#else
355#define RLIM_INFINITY (~0UL)
356#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500357 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400358 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
359 * block devices are wrongly getting hit by the filesize
360 * limit. This workaround isn't perfect, since it won't work
361 * if glibc wasn't built against 2.2 header files. (Sigh.)
362 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500363 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400364 if ((flags & IO_FLAG_RW) &&
365 (uname(&ut) == 0) &&
366 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
367 (ut.release[2] == '4') && (ut.release[3] == '.') &&
368 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
369 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500370 (fstat(data->dev, &st) == 0) &&
371 (S_ISBLK(st.st_mode))) {
372 struct rlimit rlim;
373
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500374 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500375 setrlimit(RLIMIT_FSIZE, &rlim);
376 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100377 if (((unsigned long) rlim.rlim_cur) <
378 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500379 rlim.rlim_cur = rlim.rlim_max;
380 setrlimit(RLIMIT_FSIZE, &rlim);
381 }
382 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500383#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384 *channel = io;
385 return 0;
386
387cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000388 if (data) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000389 free_cache(io, data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000390 ext2fs_free_mem((void **) &data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000392 if (io)
393 ext2fs_free_mem((void **) &io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394 return retval;
395}
396
397static errcode_t unix_close(io_channel channel)
398{
399 struct unix_private_data *data;
400 errcode_t retval = 0;
401
Theodore Ts'of3db3561997-04-26 13:34:30 +0000402 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000403 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000404 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000405
406 if (--channel->refcount > 0)
407 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000408
409 retval = flush_cached_blocks(channel, data, 0);
410
Theodore Ts'o3839e651997-04-26 13:21:57 +0000411 if (close(data->dev) < 0)
412 retval = errno;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000413 free_cache(channel, data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500414
415 ext2fs_free_mem((void **) &channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416 if (channel->name)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000417 ext2fs_free_mem((void **) &channel->name);
418 ext2fs_free_mem((void **) &channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 return retval;
420}
421
422static errcode_t unix_set_blksize(io_channel channel, int blksize)
423{
424 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000425 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000426
Theodore Ts'of3db3561997-04-26 13:34:30 +0000427 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000429 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
430
Theodore Ts'o3839e651997-04-26 13:21:57 +0000431 if (channel->block_size != blksize) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000432 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000433 return retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000434
435 channel->block_size = blksize;
436 free_cache(channel, data);
437 if ((retval = alloc_cache(channel, data)))
438 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000439 }
440 return 0;
441}
442
443
444static errcode_t unix_read_blk(io_channel channel, unsigned long block,
445 int count, void *buf)
446{
447 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500448 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000450 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000451 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452
Theodore Ts'of3db3561997-04-26 13:34:30 +0000453 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000455 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456
457 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500458 * If we're doing an odd-sized read or a very large read,
459 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500461 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000462 if ((retval = flush_cached_blocks(channel, data, 0)))
463 return retval;
464 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000466
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000467 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000468 while (count > 0) {
469 /* If it's in the cache, use it! */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500470 if ((cache = find_cached_block(channel, data, block,
471 &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000472#ifdef DEBUG
473 printf("Using cached block %d\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000474#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000475 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000476 count--;
477 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000478 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000479 continue;
480 }
481 /*
482 * Find the number of uncached blocks so we can do a
483 * single read request
484 */
485 for (i=1; i < count; i++)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500486 if (find_cached_block(channel, data, block+i,
487 &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000488 break;
489#ifdef DEBUG
490 printf("Reading %d blocks starting at %d\n", i, block);
491#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000492 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000493 return retval;
494
495 /* Save the results in the cache */
496 for (j=0; j < i; j++) {
497 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500498 cache = reuse[j];
499 reuse_cache(channel, data, cache, block++);
500 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000501 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000502 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503 }
504 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505}
506
507static errcode_t unix_write_blk(io_channel channel, unsigned long block,
508 int count, const void *buf)
509{
510 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500511 struct unix_cache *cache, *reuse;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000512 errcode_t retval = 0, retval2;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000513 const char *cp;
514 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515
Theodore Ts'of3db3561997-04-26 13:34:30 +0000516 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000518 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000520 /*
521 * If we're doing an odd-sized write or a very large write,
522 * flush out the cache completely and then do a direct write.
523 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500524 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000525 if ((retval = flush_cached_blocks(channel, data, 1)))
526 return retval;
527 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528 }
529
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000530 /*
531 * For a moderate-sized multi-block write, first force a write
532 * if we're in write-through cache mode, and then fill the
533 * cache with the blocks.
534 */
535 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
536 if (writethrough)
537 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000539 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000540 while (count > 0) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500541 cache = find_cached_block(channel, data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000542 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500543 cache = reuse;
544 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000545 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500546 memcpy(cache->buf, cp, channel->block_size);
547 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000548 count--;
549 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000550 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000551 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 return retval;
553}
554
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000555static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
556 int size, const void *buf)
557{
558 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000559 errcode_t retval = 0;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000560 size_t actual;
561
562 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
563 data = (struct unix_private_data *) channel->private_data;
564 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
565
566 /*
567 * Flush out the cache completely
568 */
569 if ((retval = flush_cached_blocks(channel, data, 1)))
570 return retval;
571
572 if (lseek(data->dev, offset, SEEK_SET) < 0)
573 return errno;
574
575 actual = write(data->dev, buf, size);
576 if (actual != size)
577 return EXT2_ET_SHORT_WRITE;
578
579 return 0;
580}
581
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000583 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584 */
585static errcode_t unix_flush(io_channel channel)
586{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000587 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000588 errcode_t retval = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000589
590 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
591 data = (struct unix_private_data *) channel->private_data;
592 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000593
594 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'o36f21431997-06-14 07:25:40 +0000595 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000596 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597}
598