blob: 7df3243f74dccb13b32e5f12acc0cb5423779d56 [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;
Matthias Andree83e692e2004-03-30 04:17:14 +020058 unsigned dirty:1;
59 unsigned in_use:1;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000060};
61
62#define CACHE_SIZE 8
Theodore Ts'o82c46602002-11-09 14:56:17 -050063#define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
64#define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000065
Theodore Ts'o3839e651997-04-26 13:21:57 +000066struct unix_private_data {
Theodore Ts'of3db3561997-04-26 13:34:30 +000067 int magic;
Theodore Ts'o3839e651997-04-26 13:21:57 +000068 int dev;
69 int flags;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000070 int access_time;
71 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +000072};
73
74static errcode_t unix_open(const char *name, int flags, io_channel *channel);
75static errcode_t unix_close(io_channel channel);
76static errcode_t unix_set_blksize(io_channel channel, int blksize);
77static errcode_t unix_read_blk(io_channel channel, unsigned long block,
78 int count, void *data);
79static errcode_t unix_write_blk(io_channel channel, unsigned long block,
80 int count, const void *data);
81static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +000082static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
83 int size, const void *data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -050085static void reuse_cache(io_channel channel, struct unix_private_data *data,
86 struct unix_cache *cache, unsigned long block);
87
Matthias Andree289e0552004-03-30 03:57:41 +020088/* __FreeBSD_kernel__ is defined by GNU/kFreeBSD - the FreeBSD kernel
89 * does not know buffered block devices - everything is raw. */
90#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
Matthias Andreeb34cbdd2003-12-28 18:21:26 +010091#define NEED_BOUNCE_BUFFER
92#else
93#undef NEED_BOUNCE_BUFFER
94#endif
95
Theodore Ts'of3db3561997-04-26 13:34:30 +000096static struct struct_io_manager struct_unix_manager = {
97 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +000098 "Unix I/O Manager",
99 unix_open,
100 unix_close,
101 unix_set_blksize,
102 unix_read_blk,
103 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000104 unix_flush,
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100105#ifdef NEED_BOUNCE_BUFFER
Theodore Ts'offf45482003-04-13 00:44:19 -0400106 0
107#else
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000108 unix_write_byte
Theodore Ts'offf45482003-04-13 00:44:19 -0400109#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110};
111
112io_manager unix_io_manager = &struct_unix_manager;
113
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000114/*
115 * Here are the raw I/O functions
116 */
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100117#ifndef NEED_BOUNCE_BUFFER
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000118static errcode_t raw_read_blk(io_channel channel,
119 struct unix_private_data *data,
120 unsigned long block,
121 int count, void *buf)
122{
123 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500124 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000125 ext2_loff_t location;
126 int actual = 0;
127
128 size = (count < 0) ? -count : count * channel->block_size;
129 location = (ext2_loff_t) block * channel->block_size;
130 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
131 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
132 goto error_out;
133 }
134 actual = read(data->dev, buf, size);
135 if (actual != size) {
136 if (actual < 0)
137 actual = 0;
138 retval = EXT2_ET_SHORT_READ;
139 goto error_out;
140 }
141 return 0;
142
143error_out:
144 memset((char *) buf+actual, 0, size-actual);
145 if (channel->read_error)
146 retval = (channel->read_error)(channel, block, count, buf,
147 size, actual, retval);
148 return retval;
149}
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100150#else /* NEED_BOUNCE_BUFFER */
Theodore Ts'offf45482003-04-13 00:44:19 -0400151/*
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100152 * Windows and FreeBSD block devices only allow sector alignment IO in offset and size
Theodore Ts'offf45482003-04-13 00:44:19 -0400153 */
154static errcode_t raw_read_blk(io_channel channel,
155 struct unix_private_data *data,
156 unsigned long block,
157 int count, void *buf)
158{
159 errcode_t retval;
160 size_t size, alignsize, fragment;
161 ext2_loff_t location;
162 int total = 0, actual;
163#define BLOCKALIGN 512
164 char sector[BLOCKALIGN];
165
166 size = (count < 0) ? -count : count * channel->block_size;
167 location = (ext2_loff_t) block * channel->block_size;
168#ifdef DEBUG
169 printf("count=%d, size=%d, block=%d, blk_size=%d, location=%lx\n",
170 count, size, block, channel->block_size, location);
171#endif
172 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
173 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
174 goto error_out;
175 }
176 fragment = size % BLOCKALIGN;
177 alignsize = size - fragment;
178 if (alignsize) {
179 actual = read(data->dev, buf, alignsize);
180 if (actual != alignsize)
181 goto short_read;
182 }
183 if (fragment) {
184 actual = read(data->dev, sector, BLOCKALIGN);
185 if (actual != BLOCKALIGN)
186 goto short_read;
187 memcpy(buf+alignsize, sector, fragment);
188 }
189 return 0;
190
191short_read:
192 if (actual>0)
193 total += actual;
194 retval = EXT2_ET_SHORT_READ;
195
196error_out:
197 memset((char *) buf+total, 0, size-actual);
198 if (channel->read_error)
199 retval = (channel->read_error)(channel, block, count, buf,
200 size, actual, retval);
201 return retval;
202}
203#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000204
205static errcode_t raw_write_blk(io_channel channel,
206 struct unix_private_data *data,
207 unsigned long block,
208 int count, const void *buf)
209{
Theodore Ts'o54434922003-12-07 01:28:50 -0500210 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000211 ext2_loff_t location;
212 int actual = 0;
213 errcode_t retval;
214
215 if (count == 1)
216 size = channel->block_size;
217 else {
218 if (count < 0)
219 size = -count;
220 else
221 size = count * channel->block_size;
222 }
223
224 location = (ext2_loff_t) block * channel->block_size;
225 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
226 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
227 goto error_out;
228 }
229
230 actual = write(data->dev, buf, size);
231 if (actual != size) {
232 retval = EXT2_ET_SHORT_WRITE;
233 goto error_out;
234 }
235 return 0;
236
237error_out:
238 if (channel->write_error)
239 retval = (channel->write_error)(channel, block, count, buf,
240 size, actual, retval);
241 return retval;
242}
243
244
245/*
246 * Here we implement the cache functions
247 */
248
249/* Allocate the cache buffers */
250static errcode_t alloc_cache(io_channel channel,
251 struct unix_private_data *data)
252{
253 errcode_t retval;
254 struct unix_cache *cache;
255 int i;
256
257 data->access_time = 0;
258 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
259 cache->block = 0;
260 cache->access_time = 0;
261 cache->dirty = 0;
262 cache->in_use = 0;
263 if ((retval = ext2fs_get_mem(channel->block_size,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400264 &cache->buf)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000265 return retval;
266 }
267 return 0;
268}
269
270/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500271static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000272{
273 struct unix_cache *cache;
274 int i;
275
276 data->access_time = 0;
277 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
278 cache->block = 0;
279 cache->access_time = 0;
280 cache->dirty = 0;
281 cache->in_use = 0;
282 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400283 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000284 cache->buf = 0;
285 }
286}
287
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400288#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000289/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500290 * Try to find a block in the cache. If the block is not found, and
291 * eldest is a non-zero pointer, then fill in eldest with the cache
292 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000293 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500294static struct unix_cache *find_cached_block(struct unix_private_data *data,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000295 unsigned long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500296 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000297{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000298 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000299 int i;
300
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000301 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000302 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
303 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500304 if (!unused_cache)
305 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000306 continue;
307 }
308 if (cache->block == block) {
309 cache->access_time = ++data->access_time;
310 return cache;
311 }
312 if (!oldest_cache ||
313 (cache->access_time < oldest_cache->access_time))
314 oldest_cache = cache;
315 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500316 if (eldest)
317 *eldest = (unused_cache) ? unused_cache : oldest_cache;
318 return 0;
319}
320
321/*
322 * Reuse a particular cache entry for another block.
323 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500324static void reuse_cache(io_channel channel, struct unix_private_data *data,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500325 struct unix_cache *cache, unsigned long block)
326{
327 if (cache->dirty && cache->in_use)
328 raw_write_blk(channel, data, cache->block, 1, cache->buf);
329
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000330 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500331 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000332 cache->block = block;
333 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000334}
335
336/*
337 * Flush all of the blocks in the cache
338 */
339static errcode_t flush_cached_blocks(io_channel channel,
340 struct unix_private_data *data,
341 int invalidate)
342
343{
344 struct unix_cache *cache;
345 errcode_t retval, retval2;
346 int i;
347
348 retval2 = 0;
349 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
350 if (!cache->in_use)
351 continue;
352
353 if (invalidate)
354 cache->in_use = 0;
355
356 if (!cache->dirty)
357 continue;
358
359 retval = raw_write_blk(channel, data,
360 cache->block, 1, cache->buf);
361 if (retval)
362 retval2 = retval;
363 else
364 cache->dirty = 0;
365 }
366 return retval2;
367}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400368#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000369
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370static errcode_t unix_open(const char *name, int flags, io_channel *channel)
371{
372 io_channel io = NULL;
373 struct unix_private_data *data = NULL;
374 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000375 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500376 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400377#ifdef __linux__
378 struct utsname ut;
379#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000380
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000381 if (name == 0)
382 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400383 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000384 if (retval)
385 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000386 memset(io, 0, sizeof(struct struct_io_channel));
387 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400388 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000389 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000391
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400393 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000394 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000396
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397 strcpy(io->name, name);
398 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000399 io->block_size = 1024;
400 io->read_error = 0;
401 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000402 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000403
404 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000405 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000406
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000407 if ((retval = alloc_cache(io, data)))
408 goto cleanup;
409
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000410 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
411#ifdef HAVE_OPEN64
412 data->dev = open64(name, open_flags);
413#else
414 data->dev = open(name, open_flags);
415#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416 if (data->dev < 0) {
417 retval = errno;
418 goto cleanup;
419 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500420
421#ifdef __linux__
422#undef RLIM_INFINITY
423#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
424#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
425#else
426#define RLIM_INFINITY (~0UL)
427#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500428 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400429 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
430 * block devices are wrongly getting hit by the filesize
431 * limit. This workaround isn't perfect, since it won't work
432 * if glibc wasn't built against 2.2 header files. (Sigh.)
433 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500434 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400435 if ((flags & IO_FLAG_RW) &&
436 (uname(&ut) == 0) &&
437 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
438 (ut.release[2] == '4') && (ut.release[3] == '.') &&
439 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
440 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500441 (fstat(data->dev, &st) == 0) &&
442 (S_ISBLK(st.st_mode))) {
443 struct rlimit rlim;
444
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500445 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500446 setrlimit(RLIMIT_FSIZE, &rlim);
447 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100448 if (((unsigned long) rlim.rlim_cur) <
449 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500450 rlim.rlim_cur = rlim.rlim_max;
451 setrlimit(RLIMIT_FSIZE, &rlim);
452 }
453 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500454#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455 *channel = io;
456 return 0;
457
458cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500460 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400461 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000463 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400464 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 return retval;
466}
467
468static errcode_t unix_close(io_channel channel)
469{
470 struct unix_private_data *data;
471 errcode_t retval = 0;
472
Theodore Ts'of3db3561997-04-26 13:34:30 +0000473 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000474 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000475 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000476
477 if (--channel->refcount > 0)
478 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000479
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400480#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000481 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400482#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000483
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484 if (close(data->dev) < 0)
485 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500486 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500487
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400488 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000489 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400490 ext2fs_free_mem(&channel->name);
491 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492 return retval;
493}
494
495static errcode_t unix_set_blksize(io_channel channel, int blksize)
496{
497 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000498 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499
Theodore Ts'of3db3561997-04-26 13:34:30 +0000500 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000502 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
503
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400505#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000506 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000507 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400508#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000509
510 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500511 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000512 if ((retval = alloc_cache(channel, data)))
513 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 }
515 return 0;
516}
517
518
519static errcode_t unix_read_blk(io_channel channel, unsigned long block,
520 int count, void *buf)
521{
522 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500523 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000525 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000526 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527
Theodore Ts'of3db3561997-04-26 13:34:30 +0000528 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000530 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000531
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400532#ifdef NO_IO_CACHE
533 return raw_read_blk(channel, data, block, count, buf);
534#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500536 * If we're doing an odd-sized read or a very large read,
537 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500539 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000540 if ((retval = flush_cached_blocks(channel, data, 0)))
541 return retval;
542 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000544
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000545 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000546 while (count > 0) {
547 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500548 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000549#ifdef DEBUG
550 printf("Using cached block %d\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000551#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000552 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000553 count--;
554 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000555 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000556 continue;
557 }
558 /*
559 * Find the number of uncached blocks so we can do a
560 * single read request
561 */
562 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500563 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000564 break;
565#ifdef DEBUG
566 printf("Reading %d blocks starting at %d\n", i, block);
567#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000568 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000569 return retval;
570
571 /* Save the results in the cache */
572 for (j=0; j < i; j++) {
573 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500574 cache = reuse[j];
575 reuse_cache(channel, data, cache, block++);
576 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000577 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000578 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579 }
580 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400581#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582}
583
584static errcode_t unix_write_blk(io_channel channel, unsigned long block,
585 int count, const void *buf)
586{
587 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500588 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500589 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000590 const char *cp;
591 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592
Theodore Ts'of3db3561997-04-26 13:34:30 +0000593 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000595 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000596
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400597#ifdef NO_IO_CACHE
598 return raw_write_blk(channel, data, block, count, buf);
599#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000600 /*
601 * If we're doing an odd-sized write or a very large write,
602 * flush out the cache completely and then do a direct write.
603 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500604 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000605 if ((retval = flush_cached_blocks(channel, data, 1)))
606 return retval;
607 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608 }
609
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000610 /*
611 * For a moderate-sized multi-block write, first force a write
612 * if we're in write-through cache mode, and then fill the
613 * cache with the blocks.
614 */
615 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
616 if (writethrough)
617 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000619 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000620 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500621 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000622 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500623 cache = reuse;
624 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000625 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500626 memcpy(cache->buf, cp, channel->block_size);
627 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000628 count--;
629 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000630 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000631 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400633#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000634}
635
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000636static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
637 int size, const void *buf)
638{
639 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000640 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500641 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000642
643 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
644 data = (struct unix_private_data *) channel->private_data;
645 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
646
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400647#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000648 /*
649 * Flush out the cache completely
650 */
651 if ((retval = flush_cached_blocks(channel, data, 1)))
652 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400653#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000654
655 if (lseek(data->dev, offset, SEEK_SET) < 0)
656 return errno;
657
658 actual = write(data->dev, buf, size);
659 if (actual != size)
660 return EXT2_ET_SHORT_WRITE;
661
662 return 0;
663}
664
Theodore Ts'o3839e651997-04-26 13:21:57 +0000665/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000666 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000667 */
668static errcode_t unix_flush(io_channel channel)
669{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000670 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000671 errcode_t retval = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000672
673 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
674 data = (struct unix_private_data *) channel->private_data;
675 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000676
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400677#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000678 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400679#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000680 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000681 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000682}
683