blob: 312313662bd161c384b3a3d3c1a01c61cc69865b [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'o23b7c8b2003-01-22 18:30:01 -050080static void reuse_cache(io_channel channel, struct unix_private_data *data,
81 struct unix_cache *cache, unsigned long block);
82
Theodore Ts'of3db3561997-04-26 13:34:30 +000083static struct struct_io_manager struct_unix_manager = {
84 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 "Unix I/O Manager",
86 unix_open,
87 unix_close,
88 unix_set_blksize,
89 unix_read_blk,
90 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +000091 unix_flush,
92 unix_write_byte
Theodore Ts'o3839e651997-04-26 13:21:57 +000093};
94
95io_manager unix_io_manager = &struct_unix_manager;
96
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000097/*
98 * Here are the raw I/O functions
99 */
100static errcode_t raw_read_blk(io_channel channel,
101 struct unix_private_data *data,
102 unsigned long block,
103 int count, void *buf)
104{
105 errcode_t retval;
106 size_t size;
107 ext2_loff_t location;
108 int actual = 0;
109
110 size = (count < 0) ? -count : count * channel->block_size;
111 location = (ext2_loff_t) block * channel->block_size;
112 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
113 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
114 goto error_out;
115 }
116 actual = read(data->dev, buf, size);
117 if (actual != size) {
118 if (actual < 0)
119 actual = 0;
120 retval = EXT2_ET_SHORT_READ;
121 goto error_out;
122 }
123 return 0;
124
125error_out:
126 memset((char *) buf+actual, 0, size-actual);
127 if (channel->read_error)
128 retval = (channel->read_error)(channel, block, count, buf,
129 size, actual, retval);
130 return retval;
131}
132
133static errcode_t raw_write_blk(io_channel channel,
134 struct unix_private_data *data,
135 unsigned long block,
136 int count, const void *buf)
137{
138 size_t size;
139 ext2_loff_t location;
140 int actual = 0;
141 errcode_t retval;
142
143 if (count == 1)
144 size = channel->block_size;
145 else {
146 if (count < 0)
147 size = -count;
148 else
149 size = count * channel->block_size;
150 }
151
152 location = (ext2_loff_t) block * channel->block_size;
153 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
154 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
155 goto error_out;
156 }
157
158 actual = write(data->dev, buf, size);
159 if (actual != size) {
160 retval = EXT2_ET_SHORT_WRITE;
161 goto error_out;
162 }
163 return 0;
164
165error_out:
166 if (channel->write_error)
167 retval = (channel->write_error)(channel, block, count, buf,
168 size, actual, retval);
169 return retval;
170}
171
172
173/*
174 * Here we implement the cache functions
175 */
176
177/* Allocate the cache buffers */
178static errcode_t alloc_cache(io_channel channel,
179 struct unix_private_data *data)
180{
181 errcode_t retval;
182 struct unix_cache *cache;
183 int i;
184
185 data->access_time = 0;
186 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
187 cache->block = 0;
188 cache->access_time = 0;
189 cache->dirty = 0;
190 cache->in_use = 0;
191 if ((retval = ext2fs_get_mem(channel->block_size,
192 (void **) &cache->buf)))
193 return retval;
194 }
195 return 0;
196}
197
198/* Free the cache buffers */
199static void free_cache(io_channel channel,
200 struct unix_private_data *data)
201{
202 struct unix_cache *cache;
203 int i;
204
205 data->access_time = 0;
206 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
207 cache->block = 0;
208 cache->access_time = 0;
209 cache->dirty = 0;
210 cache->in_use = 0;
211 if (cache->buf)
212 ext2fs_free_mem((void **) &cache->buf);
213 cache->buf = 0;
214 }
215}
216
217/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500218 * Try to find a block in the cache. If the block is not found, and
219 * eldest is a non-zero pointer, then fill in eldest with the cache
220 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000221 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000222static struct unix_cache *find_cached_block(io_channel channel,
223 struct unix_private_data *data,
224 unsigned long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500225 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000226{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000227 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000228 int i;
229
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000230 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000231 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
232 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500233 if (!unused_cache)
234 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000235 continue;
236 }
237 if (cache->block == block) {
238 cache->access_time = ++data->access_time;
239 return cache;
240 }
241 if (!oldest_cache ||
242 (cache->access_time < oldest_cache->access_time))
243 oldest_cache = cache;
244 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500245 if (eldest)
246 *eldest = (unused_cache) ? unused_cache : oldest_cache;
247 return 0;
248}
249
250/*
251 * Reuse a particular cache entry for another block.
252 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500253static void reuse_cache(io_channel channel, struct unix_private_data *data,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500254 struct unix_cache *cache, unsigned long block)
255{
256 if (cache->dirty && cache->in_use)
257 raw_write_blk(channel, data, cache->block, 1, cache->buf);
258
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000259 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500260 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000261 cache->block = block;
262 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000263}
264
265/*
266 * Flush all of the blocks in the cache
267 */
268static errcode_t flush_cached_blocks(io_channel channel,
269 struct unix_private_data *data,
270 int invalidate)
271
272{
273 struct unix_cache *cache;
274 errcode_t retval, retval2;
275 int i;
276
277 retval2 = 0;
278 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
279 if (!cache->in_use)
280 continue;
281
282 if (invalidate)
283 cache->in_use = 0;
284
285 if (!cache->dirty)
286 continue;
287
288 retval = raw_write_blk(channel, data,
289 cache->block, 1, cache->buf);
290 if (retval)
291 retval2 = retval;
292 else
293 cache->dirty = 0;
294 }
295 return retval2;
296}
297
298
299
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300static errcode_t unix_open(const char *name, int flags, io_channel *channel)
301{
302 io_channel io = NULL;
303 struct unix_private_data *data = NULL;
304 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000305 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500306 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400307#ifdef __linux__
308 struct utsname ut;
309#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000311 if (name == 0)
312 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000313 retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
314 (void **) &io);
315 if (retval)
316 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000317 memset(io, 0, sizeof(struct struct_io_channel));
318 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000319 retval = ext2fs_get_mem(sizeof(struct unix_private_data),
320 (void **) &data);
321 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000322 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000323
Theodore Ts'o3839e651997-04-26 13:21:57 +0000324 io->manager = unix_io_manager;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000325 retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
326 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000327 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000328
Theodore Ts'o3839e651997-04-26 13:21:57 +0000329 strcpy(io->name, name);
330 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000331 io->block_size = 1024;
332 io->read_error = 0;
333 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000334 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000335
336 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000337 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000338
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000339 if ((retval = alloc_cache(io, data)))
340 goto cleanup;
341
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000342 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
343#ifdef HAVE_OPEN64
344 data->dev = open64(name, open_flags);
345#else
346 data->dev = open(name, open_flags);
347#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348 if (data->dev < 0) {
349 retval = errno;
350 goto cleanup;
351 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500352
353#ifdef __linux__
354#undef RLIM_INFINITY
355#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
356#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
357#else
358#define RLIM_INFINITY (~0UL)
359#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500360 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400361 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
362 * block devices are wrongly getting hit by the filesize
363 * limit. This workaround isn't perfect, since it won't work
364 * if glibc wasn't built against 2.2 header files. (Sigh.)
365 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500366 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400367 if ((flags & IO_FLAG_RW) &&
368 (uname(&ut) == 0) &&
369 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
370 (ut.release[2] == '4') && (ut.release[3] == '.') &&
371 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
372 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500373 (fstat(data->dev, &st) == 0) &&
374 (S_ISBLK(st.st_mode))) {
375 struct rlimit rlim;
376
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500377 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500378 setrlimit(RLIMIT_FSIZE, &rlim);
379 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100380 if (((unsigned long) rlim.rlim_cur) <
381 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500382 rlim.rlim_cur = rlim.rlim_max;
383 setrlimit(RLIMIT_FSIZE, &rlim);
384 }
385 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500386#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000387 *channel = io;
388 return 0;
389
390cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391 if (data) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000392 free_cache(io, data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000393 ext2fs_free_mem((void **) &data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000395 if (io)
396 ext2fs_free_mem((void **) &io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397 return retval;
398}
399
400static errcode_t unix_close(io_channel channel)
401{
402 struct unix_private_data *data;
403 errcode_t retval = 0;
404
Theodore Ts'of3db3561997-04-26 13:34:30 +0000405 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000407 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000408
409 if (--channel->refcount > 0)
410 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000411
412 retval = flush_cached_blocks(channel, data, 0);
413
Theodore Ts'o3839e651997-04-26 13:21:57 +0000414 if (close(data->dev) < 0)
415 retval = errno;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000416 free_cache(channel, data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500417
418 ext2fs_free_mem((void **) &channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 if (channel->name)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000420 ext2fs_free_mem((void **) &channel->name);
421 ext2fs_free_mem((void **) &channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422 return retval;
423}
424
425static errcode_t unix_set_blksize(io_channel channel, int blksize)
426{
427 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000428 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429
Theodore Ts'of3db3561997-04-26 13:34:30 +0000430 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000431 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000432 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
433
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434 if (channel->block_size != blksize) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000435 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000436 return retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000437
438 channel->block_size = blksize;
439 free_cache(channel, data);
440 if ((retval = alloc_cache(channel, data)))
441 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 }
443 return 0;
444}
445
446
447static errcode_t unix_read_blk(io_channel channel, unsigned long block,
448 int count, void *buf)
449{
450 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500451 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000453 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000454 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455
Theodore Ts'of3db3561997-04-26 13:34:30 +0000456 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000458 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459
460 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500461 * If we're doing an odd-sized read or a very large read,
462 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500464 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000465 if ((retval = flush_cached_blocks(channel, data, 0)))
466 return retval;
467 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000469
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000470 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000471 while (count > 0) {
472 /* If it's in the cache, use it! */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500473 if ((cache = find_cached_block(channel, data, block,
474 &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000475#ifdef DEBUG
476 printf("Using cached block %d\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000477#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000478 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000479 count--;
480 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000481 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000482 continue;
483 }
484 /*
485 * Find the number of uncached blocks so we can do a
486 * single read request
487 */
488 for (i=1; i < count; i++)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500489 if (find_cached_block(channel, data, block+i,
490 &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000491 break;
492#ifdef DEBUG
493 printf("Reading %d blocks starting at %d\n", i, block);
494#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000495 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000496 return retval;
497
498 /* Save the results in the cache */
499 for (j=0; j < i; j++) {
500 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500501 cache = reuse[j];
502 reuse_cache(channel, data, cache, block++);
503 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000504 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000505 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506 }
507 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508}
509
510static errcode_t unix_write_blk(io_channel channel, unsigned long block,
511 int count, const void *buf)
512{
513 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500514 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500515 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000516 const char *cp;
517 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518
Theodore Ts'of3db3561997-04-26 13:34:30 +0000519 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000520 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000521 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000523 /*
524 * If we're doing an odd-sized write or a very large write,
525 * flush out the cache completely and then do a direct write.
526 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500527 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000528 if ((retval = flush_cached_blocks(channel, data, 1)))
529 return retval;
530 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000531 }
532
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000533 /*
534 * For a moderate-sized multi-block write, first force a write
535 * if we're in write-through cache mode, and then fill the
536 * cache with the blocks.
537 */
538 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
539 if (writethrough)
540 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000542 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000543 while (count > 0) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500544 cache = find_cached_block(channel, data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000545 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500546 cache = reuse;
547 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000548 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500549 memcpy(cache->buf, cp, channel->block_size);
550 cache->dirty = !writethrough;
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 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 return retval;
556}
557
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000558static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
559 int size, const void *buf)
560{
561 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000562 errcode_t retval = 0;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000563 size_t actual;
564
565 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
566 data = (struct unix_private_data *) channel->private_data;
567 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
568
569 /*
570 * Flush out the cache completely
571 */
572 if ((retval = flush_cached_blocks(channel, data, 1)))
573 return retval;
574
575 if (lseek(data->dev, offset, SEEK_SET) < 0)
576 return errno;
577
578 actual = write(data->dev, buf, size);
579 if (actual != size)
580 return EXT2_ET_SHORT_WRITE;
581
582 return 0;
583}
584
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000586 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587 */
588static errcode_t unix_flush(io_channel channel)
589{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000590 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000591 errcode_t retval = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000592
593 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
594 data = (struct unix_private_data *) channel->private_data;
595 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000596
597 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'o36f21431997-06-14 07:25:40 +0000598 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000599 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600}
601