blob: 1dcca4ad28687fcbdd3ab0403ec4506841fdc3b2 [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
58#define WRITE_VIA_CACHE_SIZE 4 /* Must be smaller than CACHE_SIZE */
59
Theodore Ts'o3839e651997-04-26 13:21:57 +000060struct unix_private_data {
Theodore Ts'of3db3561997-04-26 13:34:30 +000061 int magic;
Theodore Ts'o3839e651997-04-26 13:21:57 +000062 int dev;
63 int flags;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000064 int access_time;
65 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +000066};
67
68static errcode_t unix_open(const char *name, int flags, io_channel *channel);
69static errcode_t unix_close(io_channel channel);
70static errcode_t unix_set_blksize(io_channel channel, int blksize);
71static errcode_t unix_read_blk(io_channel channel, unsigned long block,
72 int count, void *data);
73static errcode_t unix_write_blk(io_channel channel, unsigned long block,
74 int count, const void *data);
75static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +000076static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
77 int size, const void *data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000078
Theodore Ts'of3db3561997-04-26 13:34:30 +000079static struct struct_io_manager struct_unix_manager = {
80 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 "Unix I/O Manager",
82 unix_open,
83 unix_close,
84 unix_set_blksize,
85 unix_read_blk,
86 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +000087 unix_flush,
88 unix_write_byte
Theodore Ts'o3839e651997-04-26 13:21:57 +000089};
90
91io_manager unix_io_manager = &struct_unix_manager;
92
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000093/*
94 * Here are the raw I/O functions
95 */
96static errcode_t raw_read_blk(io_channel channel,
97 struct unix_private_data *data,
98 unsigned long block,
99 int count, void *buf)
100{
101 errcode_t retval;
102 size_t size;
103 ext2_loff_t location;
104 int actual = 0;
105
106 size = (count < 0) ? -count : count * channel->block_size;
107 location = (ext2_loff_t) block * channel->block_size;
108 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
109 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
110 goto error_out;
111 }
112 actual = read(data->dev, buf, size);
113 if (actual != size) {
114 if (actual < 0)
115 actual = 0;
116 retval = EXT2_ET_SHORT_READ;
117 goto error_out;
118 }
119 return 0;
120
121error_out:
122 memset((char *) buf+actual, 0, size-actual);
123 if (channel->read_error)
124 retval = (channel->read_error)(channel, block, count, buf,
125 size, actual, retval);
126 return retval;
127}
128
129static errcode_t raw_write_blk(io_channel channel,
130 struct unix_private_data *data,
131 unsigned long block,
132 int count, const void *buf)
133{
134 size_t size;
135 ext2_loff_t location;
136 int actual = 0;
137 errcode_t retval;
138
139 if (count == 1)
140 size = channel->block_size;
141 else {
142 if (count < 0)
143 size = -count;
144 else
145 size = count * channel->block_size;
146 }
147
148 location = (ext2_loff_t) block * channel->block_size;
149 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
150 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
151 goto error_out;
152 }
153
154 actual = write(data->dev, buf, size);
155 if (actual != size) {
156 retval = EXT2_ET_SHORT_WRITE;
157 goto error_out;
158 }
159 return 0;
160
161error_out:
162 if (channel->write_error)
163 retval = (channel->write_error)(channel, block, count, buf,
164 size, actual, retval);
165 return retval;
166}
167
168
169/*
170 * Here we implement the cache functions
171 */
172
173/* Allocate the cache buffers */
174static errcode_t alloc_cache(io_channel channel,
175 struct unix_private_data *data)
176{
177 errcode_t retval;
178 struct unix_cache *cache;
179 int i;
180
181 data->access_time = 0;
182 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
183 cache->block = 0;
184 cache->access_time = 0;
185 cache->dirty = 0;
186 cache->in_use = 0;
187 if ((retval = ext2fs_get_mem(channel->block_size,
188 (void **) &cache->buf)))
189 return retval;
190 }
191 return 0;
192}
193
194/* Free the cache buffers */
195static void free_cache(io_channel channel,
196 struct unix_private_data *data)
197{
198 struct unix_cache *cache;
199 int i;
200
201 data->access_time = 0;
202 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
203 cache->block = 0;
204 cache->access_time = 0;
205 cache->dirty = 0;
206 cache->in_use = 0;
207 if (cache->buf)
208 ext2fs_free_mem((void **) &cache->buf);
209 cache->buf = 0;
210 }
211}
212
213/*
214 * Try to find a block in the cache. If get_cache is non-zero, then
215 * if the block isn't in the cache, evict the oldest block in the
216 * cache and create a new cache entry for the requested block.
217 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000218static struct unix_cache *find_cached_block(io_channel channel,
219 struct unix_private_data *data,
220 unsigned long block,
221 int get_cache)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000222{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000223 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000224 int i;
225
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000226 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000227 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
228 if (!cache->in_use) {
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000229 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000230 continue;
231 }
232 if (cache->block == block) {
233 cache->access_time = ++data->access_time;
234 return cache;
235 }
236 if (!oldest_cache ||
237 (cache->access_time < oldest_cache->access_time))
238 oldest_cache = cache;
239 }
240 if (!get_cache)
241 return 0;
242
243 /*
244 * Try to allocate cache slot.
245 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000246 if (unused_cache)
247 cache = unused_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000248 else {
249 cache = oldest_cache;
250 if (cache->dirty)
251 raw_write_blk(channel, data,
252 cache->block, 1, cache->buf);
253 }
254 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500255 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000256 cache->block = block;
257 cache->access_time = ++data->access_time;
258 return cache;
259}
260
261/*
262 * Flush all of the blocks in the cache
263 */
264static errcode_t flush_cached_blocks(io_channel channel,
265 struct unix_private_data *data,
266 int invalidate)
267
268{
269 struct unix_cache *cache;
270 errcode_t retval, retval2;
271 int i;
272
273 retval2 = 0;
274 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
275 if (!cache->in_use)
276 continue;
277
278 if (invalidate)
279 cache->in_use = 0;
280
281 if (!cache->dirty)
282 continue;
283
284 retval = raw_write_blk(channel, data,
285 cache->block, 1, cache->buf);
286 if (retval)
287 retval2 = retval;
288 else
289 cache->dirty = 0;
290 }
291 return retval2;
292}
293
294
295
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296static errcode_t unix_open(const char *name, int flags, io_channel *channel)
297{
298 io_channel io = NULL;
299 struct unix_private_data *data = NULL;
300 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000301 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500302 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400303#ifdef __linux__
304 struct utsname ut;
305#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000306
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000307 if (name == 0)
308 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000309 retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
310 (void **) &io);
311 if (retval)
312 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000313 memset(io, 0, sizeof(struct struct_io_channel));
314 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000315 retval = ext2fs_get_mem(sizeof(struct unix_private_data),
316 (void **) &data);
317 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000318 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000319
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320 io->manager = unix_io_manager;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000321 retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
322 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000324
Theodore Ts'o3839e651997-04-26 13:21:57 +0000325 strcpy(io->name, name);
326 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000327 io->block_size = 1024;
328 io->read_error = 0;
329 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000330 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000331
332 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000333 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000334
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000335 if ((retval = alloc_cache(io, data)))
336 goto cleanup;
337
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000338 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
339#ifdef HAVE_OPEN64
340 data->dev = open64(name, open_flags);
341#else
342 data->dev = open(name, open_flags);
343#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344 if (data->dev < 0) {
345 retval = errno;
346 goto cleanup;
347 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500348
349#ifdef __linux__
350#undef RLIM_INFINITY
351#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
352#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
353#else
354#define RLIM_INFINITY (~0UL)
355#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500356 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400357 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
358 * block devices are wrongly getting hit by the filesize
359 * limit. This workaround isn't perfect, since it won't work
360 * if glibc wasn't built against 2.2 header files. (Sigh.)
361 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500362 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400363 if ((flags & IO_FLAG_RW) &&
364 (uname(&ut) == 0) &&
365 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
366 (ut.release[2] == '4') && (ut.release[3] == '.') &&
367 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
368 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500369 (fstat(data->dev, &st) == 0) &&
370 (S_ISBLK(st.st_mode))) {
371 struct rlimit rlim;
372
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500373 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500374 setrlimit(RLIMIT_FSIZE, &rlim);
375 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100376 if (((unsigned long) rlim.rlim_cur) <
377 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500378 rlim.rlim_cur = rlim.rlim_max;
379 setrlimit(RLIMIT_FSIZE, &rlim);
380 }
381 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500382#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000383 *channel = io;
384 return 0;
385
386cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000387 if (data) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000388 free_cache(io, data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000389 ext2fs_free_mem((void **) &data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000391 if (io)
392 ext2fs_free_mem((void **) &io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000393 return retval;
394}
395
396static errcode_t unix_close(io_channel channel)
397{
398 struct unix_private_data *data;
399 errcode_t retval = 0;
400
Theodore Ts'of3db3561997-04-26 13:34:30 +0000401 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000402 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000403 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000404
405 if (--channel->refcount > 0)
406 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000407
408 retval = flush_cached_blocks(channel, data, 0);
409
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410 if (close(data->dev) < 0)
411 retval = errno;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000412 free_cache(channel, data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500413
414 ext2fs_free_mem((void **) &channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415 if (channel->name)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000416 ext2fs_free_mem((void **) &channel->name);
417 ext2fs_free_mem((void **) &channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418 return retval;
419}
420
421static errcode_t unix_set_blksize(io_channel channel, int blksize)
422{
423 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000424 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425
Theodore Ts'of3db3561997-04-26 13:34:30 +0000426 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000428 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
429
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 if (channel->block_size != blksize) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000431 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000432 return retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000433
434 channel->block_size = blksize;
435 free_cache(channel, data);
436 if ((retval = alloc_cache(channel, data)))
437 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 }
439 return 0;
440}
441
442
443static errcode_t unix_read_blk(io_channel channel, unsigned long block,
444 int count, void *buf)
445{
446 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000447 struct unix_cache *cache;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000449 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000450 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451
Theodore Ts'of3db3561997-04-26 13:34:30 +0000452 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000454 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455
456 /*
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000457 * If we're doing an odd-sized read, flush out the cache and
458 * then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000460 if (count < 0) {
461 if ((retval = flush_cached_blocks(channel, data, 0)))
462 return retval;
463 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000465
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000466 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000467 while (count > 0) {
468 /* If it's in the cache, use it! */
469 if ((cache = find_cached_block(channel, data, block, 0))) {
470#ifdef DEBUG
471 printf("Using cached block %d\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000472#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000473 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000474 count--;
475 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000476 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000477 continue;
478 }
479 /*
480 * Find the number of uncached blocks so we can do a
481 * single read request
482 */
483 for (i=1; i < count; i++)
484 if (find_cached_block(channel, data, block+i, 0))
485 break;
486#ifdef DEBUG
487 printf("Reading %d blocks starting at %d\n", i, block);
488#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000489 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000490 return retval;
491
492 /* Save the results in the cache */
493 for (j=0; j < i; j++) {
494 count--;
495 cache = find_cached_block(channel, data, block++, 1);
496 if (cache)
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000497 memcpy(cache->buf, cp, channel->block_size);
498 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000499 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000500 }
501 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502}
503
504static errcode_t unix_write_blk(io_channel channel, unsigned long block,
505 int count, const void *buf)
506{
507 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000508 struct unix_cache *cache;
509 errcode_t retval = 0, retval2;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000510 const char *cp;
511 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512
Theodore Ts'of3db3561997-04-26 13:34:30 +0000513 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000515 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000517 /*
518 * If we're doing an odd-sized write or a very large write,
519 * flush out the cache completely and then do a direct write.
520 */
521 if (count < 0 || count > WRITE_VIA_CACHE_SIZE) {
522 if ((retval = flush_cached_blocks(channel, data, 1)))
523 return retval;
524 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525 }
526
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000527 /*
528 * For a moderate-sized multi-block write, first force a write
529 * if we're in write-through cache mode, and then fill the
530 * cache with the blocks.
531 */
532 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
533 if (writethrough)
534 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000536 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000537 while (count > 0) {
538 cache = find_cached_block(channel, data, block, 1);
539 if (!cache) {
540 /*
541 * Oh shit, we couldn't get cache descriptor.
542 * Force the write directly.
543 */
544 if ((retval2 = raw_write_blk(channel, data, block,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000545 1, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000546 retval = retval2;
547 } else {
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000548 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000549 cache->dirty = !writethrough;
550 }
551 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