blob: 2c732e9cda95b89feb8371960ab079af38d970c1 [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
Theodore Ts'of3db3561997-04-26 13:34:30 +000088static struct struct_io_manager struct_unix_manager = {
89 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +000090 "Unix I/O Manager",
91 unix_open,
92 unix_close,
93 unix_set_blksize,
94 unix_read_blk,
95 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +000096 unix_flush,
Theodore Ts'oa85e81a2003-04-18 07:22:01 -040097#ifdef __CYGWIN__
Theodore Ts'offf45482003-04-13 00:44:19 -040098 0
99#else
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000100 unix_write_byte
Theodore Ts'offf45482003-04-13 00:44:19 -0400101#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102};
103
104io_manager unix_io_manager = &struct_unix_manager;
105
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000106/*
107 * Here are the raw I/O functions
108 */
Theodore Ts'oa85e81a2003-04-18 07:22:01 -0400109#ifndef __CYGWIN__
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000110static errcode_t raw_read_blk(io_channel channel,
111 struct unix_private_data *data,
112 unsigned long block,
113 int count, void *buf)
114{
115 errcode_t retval;
116 size_t size;
117 ext2_loff_t location;
118 int actual = 0;
119
120 size = (count < 0) ? -count : count * channel->block_size;
121 location = (ext2_loff_t) block * channel->block_size;
122 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
123 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
124 goto error_out;
125 }
126 actual = read(data->dev, buf, size);
127 if (actual != size) {
128 if (actual < 0)
129 actual = 0;
130 retval = EXT2_ET_SHORT_READ;
131 goto error_out;
132 }
133 return 0;
134
135error_out:
136 memset((char *) buf+actual, 0, size-actual);
137 if (channel->read_error)
138 retval = (channel->read_error)(channel, block, count, buf,
139 size, actual, retval);
140 return retval;
141}
Theodore Ts'oa85e81a2003-04-18 07:22:01 -0400142#else /* __CYGWIN__ */
Theodore Ts'offf45482003-04-13 00:44:19 -0400143/*
144 * Windows block devices only allow sector alignment IO in offset and size
145 */
146static errcode_t raw_read_blk(io_channel channel,
147 struct unix_private_data *data,
148 unsigned long block,
149 int count, void *buf)
150{
151 errcode_t retval;
152 size_t size, alignsize, fragment;
153 ext2_loff_t location;
154 int total = 0, actual;
155#define BLOCKALIGN 512
156 char sector[BLOCKALIGN];
157
158 size = (count < 0) ? -count : count * channel->block_size;
159 location = (ext2_loff_t) block * channel->block_size;
160#ifdef DEBUG
161 printf("count=%d, size=%d, block=%d, blk_size=%d, location=%lx\n",
162 count, size, block, channel->block_size, location);
163#endif
164 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
165 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
166 goto error_out;
167 }
168 fragment = size % BLOCKALIGN;
169 alignsize = size - fragment;
170 if (alignsize) {
171 actual = read(data->dev, buf, alignsize);
172 if (actual != alignsize)
173 goto short_read;
174 }
175 if (fragment) {
176 actual = read(data->dev, sector, BLOCKALIGN);
177 if (actual != BLOCKALIGN)
178 goto short_read;
179 memcpy(buf+alignsize, sector, fragment);
180 }
181 return 0;
182
183short_read:
184 if (actual>0)
185 total += actual;
186 retval = EXT2_ET_SHORT_READ;
187
188error_out:
189 memset((char *) buf+total, 0, size-actual);
190 if (channel->read_error)
191 retval = (channel->read_error)(channel, block, count, buf,
192 size, actual, retval);
193 return retval;
194}
195#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000196
197static errcode_t raw_write_blk(io_channel channel,
198 struct unix_private_data *data,
199 unsigned long block,
200 int count, const void *buf)
201{
202 size_t size;
203 ext2_loff_t location;
204 int actual = 0;
205 errcode_t retval;
206
207 if (count == 1)
208 size = channel->block_size;
209 else {
210 if (count < 0)
211 size = -count;
212 else
213 size = count * channel->block_size;
214 }
215
216 location = (ext2_loff_t) block * channel->block_size;
217 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
218 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
219 goto error_out;
220 }
221
222 actual = write(data->dev, buf, size);
223 if (actual != size) {
224 retval = EXT2_ET_SHORT_WRITE;
225 goto error_out;
226 }
227 return 0;
228
229error_out:
230 if (channel->write_error)
231 retval = (channel->write_error)(channel, block, count, buf,
232 size, actual, retval);
233 return retval;
234}
235
236
237/*
238 * Here we implement the cache functions
239 */
240
241/* Allocate the cache buffers */
242static errcode_t alloc_cache(io_channel channel,
243 struct unix_private_data *data)
244{
245 errcode_t retval;
246 struct unix_cache *cache;
247 int i;
248
249 data->access_time = 0;
250 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
251 cache->block = 0;
252 cache->access_time = 0;
253 cache->dirty = 0;
254 cache->in_use = 0;
255 if ((retval = ext2fs_get_mem(channel->block_size,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400256 &cache->buf)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000257 return retval;
258 }
259 return 0;
260}
261
262/* Free the cache buffers */
263static void free_cache(io_channel channel,
264 struct unix_private_data *data)
265{
266 struct unix_cache *cache;
267 int i;
268
269 data->access_time = 0;
270 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
271 cache->block = 0;
272 cache->access_time = 0;
273 cache->dirty = 0;
274 cache->in_use = 0;
275 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400276 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000277 cache->buf = 0;
278 }
279}
280
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400281#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000282/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500283 * Try to find a block in the cache. If the block is not found, and
284 * eldest is a non-zero pointer, then fill in eldest with the cache
285 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000286 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000287static struct unix_cache *find_cached_block(io_channel channel,
288 struct unix_private_data *data,
289 unsigned long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500290 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000291{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000292 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000293 int i;
294
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000295 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000296 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
297 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500298 if (!unused_cache)
299 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000300 continue;
301 }
302 if (cache->block == block) {
303 cache->access_time = ++data->access_time;
304 return cache;
305 }
306 if (!oldest_cache ||
307 (cache->access_time < oldest_cache->access_time))
308 oldest_cache = cache;
309 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500310 if (eldest)
311 *eldest = (unused_cache) ? unused_cache : oldest_cache;
312 return 0;
313}
314
315/*
316 * Reuse a particular cache entry for another block.
317 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500318static void reuse_cache(io_channel channel, struct unix_private_data *data,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500319 struct unix_cache *cache, unsigned long block)
320{
321 if (cache->dirty && cache->in_use)
322 raw_write_blk(channel, data, cache->block, 1, cache->buf);
323
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000324 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500325 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000326 cache->block = block;
327 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000328}
329
330/*
331 * Flush all of the blocks in the cache
332 */
333static errcode_t flush_cached_blocks(io_channel channel,
334 struct unix_private_data *data,
335 int invalidate)
336
337{
338 struct unix_cache *cache;
339 errcode_t retval, retval2;
340 int i;
341
342 retval2 = 0;
343 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
344 if (!cache->in_use)
345 continue;
346
347 if (invalidate)
348 cache->in_use = 0;
349
350 if (!cache->dirty)
351 continue;
352
353 retval = raw_write_blk(channel, data,
354 cache->block, 1, cache->buf);
355 if (retval)
356 retval2 = retval;
357 else
358 cache->dirty = 0;
359 }
360 return retval2;
361}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400362#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000363
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364static errcode_t unix_open(const char *name, int flags, io_channel *channel)
365{
366 io_channel io = NULL;
367 struct unix_private_data *data = NULL;
368 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000369 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500370 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400371#ifdef __linux__
372 struct utsname ut;
373#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000375 if (name == 0)
376 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400377 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000378 if (retval)
379 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000380 memset(io, 0, sizeof(struct struct_io_channel));
381 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400382 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000383 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000385
Theodore Ts'o3839e651997-04-26 13:21:57 +0000386 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400387 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000388 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000389 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000390
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391 strcpy(io->name, name);
392 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000393 io->block_size = 1024;
394 io->read_error = 0;
395 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000396 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397
398 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000399 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000400
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000401 if ((retval = alloc_cache(io, data)))
402 goto cleanup;
403
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000404 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
405#ifdef HAVE_OPEN64
406 data->dev = open64(name, open_flags);
407#else
408 data->dev = open(name, open_flags);
409#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410 if (data->dev < 0) {
411 retval = errno;
412 goto cleanup;
413 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500414
415#ifdef __linux__
416#undef RLIM_INFINITY
417#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
418#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
419#else
420#define RLIM_INFINITY (~0UL)
421#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500422 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400423 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
424 * block devices are wrongly getting hit by the filesize
425 * limit. This workaround isn't perfect, since it won't work
426 * if glibc wasn't built against 2.2 header files. (Sigh.)
427 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500428 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400429 if ((flags & IO_FLAG_RW) &&
430 (uname(&ut) == 0) &&
431 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
432 (ut.release[2] == '4') && (ut.release[3] == '.') &&
433 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
434 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500435 (fstat(data->dev, &st) == 0) &&
436 (S_ISBLK(st.st_mode))) {
437 struct rlimit rlim;
438
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500439 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500440 setrlimit(RLIMIT_FSIZE, &rlim);
441 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100442 if (((unsigned long) rlim.rlim_cur) <
443 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500444 rlim.rlim_cur = rlim.rlim_max;
445 setrlimit(RLIMIT_FSIZE, &rlim);
446 }
447 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500448#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 *channel = io;
450 return 0;
451
452cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 if (data) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000454 free_cache(io, data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400455 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000457 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400458 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 return retval;
460}
461
462static errcode_t unix_close(io_channel channel)
463{
464 struct unix_private_data *data;
465 errcode_t retval = 0;
466
Theodore Ts'of3db3561997-04-26 13:34:30 +0000467 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000469 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000470
471 if (--channel->refcount > 0)
472 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000473
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400474#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000475 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400476#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000477
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 if (close(data->dev) < 0)
479 retval = errno;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000480 free_cache(channel, data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500481
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400482 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400484 ext2fs_free_mem(&channel->name);
485 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000486 return retval;
487}
488
489static errcode_t unix_set_blksize(io_channel channel, int blksize)
490{
491 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000492 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493
Theodore Ts'of3db3561997-04-26 13:34:30 +0000494 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000496 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
497
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400499#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000500 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000501 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400502#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000503
504 channel->block_size = blksize;
505 free_cache(channel, data);
506 if ((retval = alloc_cache(channel, data)))
507 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 }
509 return 0;
510}
511
512
513static errcode_t unix_read_blk(io_channel channel, unsigned long block,
514 int count, void *buf)
515{
516 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500517 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000519 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000520 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521
Theodore Ts'of3db3561997-04-26 13:34:30 +0000522 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000523 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000524 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400526#ifdef NO_IO_CACHE
527 return raw_read_blk(channel, data, block, count, buf);
528#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500530 * If we're doing an odd-sized read or a very large read,
531 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500533 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000534 if ((retval = flush_cached_blocks(channel, data, 0)))
535 return retval;
536 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000538
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000539 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000540 while (count > 0) {
541 /* If it's in the cache, use it! */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500542 if ((cache = find_cached_block(channel, data, block,
543 &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000544#ifdef DEBUG
545 printf("Using cached block %d\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000546#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000547 memcpy(cp, cache->buf, channel->block_size);
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 continue;
552 }
553 /*
554 * Find the number of uncached blocks so we can do a
555 * single read request
556 */
557 for (i=1; i < count; i++)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500558 if (find_cached_block(channel, data, block+i,
559 &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000560 break;
561#ifdef DEBUG
562 printf("Reading %d blocks starting at %d\n", i, block);
563#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000564 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000565 return retval;
566
567 /* Save the results in the cache */
568 for (j=0; j < i; j++) {
569 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500570 cache = reuse[j];
571 reuse_cache(channel, data, cache, block++);
572 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000573 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000574 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000575 }
576 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400577#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000578}
579
580static errcode_t unix_write_blk(io_channel channel, unsigned long block,
581 int count, const void *buf)
582{
583 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500584 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500585 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000586 const char *cp;
587 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588
Theodore Ts'of3db3561997-04-26 13:34:30 +0000589 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000590 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000591 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400593#ifdef NO_IO_CACHE
594 return raw_write_blk(channel, data, block, count, buf);
595#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000596 /*
597 * If we're doing an odd-sized write or a very large write,
598 * flush out the cache completely and then do a direct write.
599 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500600 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000601 if ((retval = flush_cached_blocks(channel, data, 1)))
602 return retval;
603 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000604 }
605
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000606 /*
607 * For a moderate-sized multi-block write, first force a write
608 * if we're in write-through cache mode, and then fill the
609 * cache with the blocks.
610 */
611 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
612 if (writethrough)
613 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000615 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000616 while (count > 0) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500617 cache = find_cached_block(channel, data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000618 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500619 cache = reuse;
620 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000621 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500622 memcpy(cache->buf, cp, channel->block_size);
623 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000624 count--;
625 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000626 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000627 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400629#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000630}
631
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000632static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
633 int size, const void *buf)
634{
635 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000636 errcode_t retval = 0;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000637 size_t actual;
638
639 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
640 data = (struct unix_private_data *) channel->private_data;
641 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
642
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400643#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000644 /*
645 * Flush out the cache completely
646 */
647 if ((retval = flush_cached_blocks(channel, data, 1)))
648 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400649#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000650
651 if (lseek(data->dev, offset, SEEK_SET) < 0)
652 return errno;
653
654 actual = write(data->dev, buf, size);
655 if (actual != size)
656 return EXT2_ET_SHORT_WRITE;
657
658 return 0;
659}
660
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000662 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663 */
664static errcode_t unix_flush(io_channel channel)
665{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000666 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000667 errcode_t retval = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000668
669 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
670 data = (struct unix_private_data *) channel->private_data;
671 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000672
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400673#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000674 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400675#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000676 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000677 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000678}
679