blob: 19c6926ef6801c71ffd6ec268a1ae5d41a4158b0 [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'o19c78dc1997-04-29 16:17:09 +00006 * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000012 */
13
Theodore Ts'odc5f68c2000-05-25 23:31:54 +000014#define _LARGEFILE_SOURCE
15#define _LARGEFILE64_SOURCE
16
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <stdio.h>
18#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000019#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000021#endif
Theodore Ts'oc4e749a1998-02-20 05:33:14 +000022#if HAVE_ERRNO_H
23#include <errno.h>
24#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <fcntl.h>
26#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000027#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000029#endif
30#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000031#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000032#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000033
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000034#if EXT2_FLAT_INCLUDES
35#include "ext2_fs.h"
36#else
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000037#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000038#endif
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000039
40#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);
76
Theodore Ts'of3db3561997-04-26 13:34:30 +000077static struct struct_io_manager struct_unix_manager = {
78 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +000079 "Unix I/O Manager",
80 unix_open,
81 unix_close,
82 unix_set_blksize,
83 unix_read_blk,
84 unix_write_blk,
85 unix_flush
86};
87
88io_manager unix_io_manager = &struct_unix_manager;
89
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000090/*
91 * Here are the raw I/O functions
92 */
93static errcode_t raw_read_blk(io_channel channel,
94 struct unix_private_data *data,
95 unsigned long block,
96 int count, void *buf)
97{
98 errcode_t retval;
99 size_t size;
100 ext2_loff_t location;
101 int actual = 0;
102
103 size = (count < 0) ? -count : count * channel->block_size;
104 location = (ext2_loff_t) block * channel->block_size;
105 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
106 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
107 goto error_out;
108 }
109 actual = read(data->dev, buf, size);
110 if (actual != size) {
111 if (actual < 0)
112 actual = 0;
113 retval = EXT2_ET_SHORT_READ;
114 goto error_out;
115 }
116 return 0;
117
118error_out:
119 memset((char *) buf+actual, 0, size-actual);
120 if (channel->read_error)
121 retval = (channel->read_error)(channel, block, count, buf,
122 size, actual, retval);
123 return retval;
124}
125
126static errcode_t raw_write_blk(io_channel channel,
127 struct unix_private_data *data,
128 unsigned long block,
129 int count, const void *buf)
130{
131 size_t size;
132 ext2_loff_t location;
133 int actual = 0;
134 errcode_t retval;
135
136 if (count == 1)
137 size = channel->block_size;
138 else {
139 if (count < 0)
140 size = -count;
141 else
142 size = count * channel->block_size;
143 }
144
145 location = (ext2_loff_t) block * channel->block_size;
146 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
147 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
148 goto error_out;
149 }
150
151 actual = write(data->dev, buf, size);
152 if (actual != size) {
153 retval = EXT2_ET_SHORT_WRITE;
154 goto error_out;
155 }
156 return 0;
157
158error_out:
159 if (channel->write_error)
160 retval = (channel->write_error)(channel, block, count, buf,
161 size, actual, retval);
162 return retval;
163}
164
165
166/*
167 * Here we implement the cache functions
168 */
169
170/* Allocate the cache buffers */
171static errcode_t alloc_cache(io_channel channel,
172 struct unix_private_data *data)
173{
174 errcode_t retval;
175 struct unix_cache *cache;
176 int i;
177
178 data->access_time = 0;
179 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
180 cache->block = 0;
181 cache->access_time = 0;
182 cache->dirty = 0;
183 cache->in_use = 0;
184 if ((retval = ext2fs_get_mem(channel->block_size,
185 (void **) &cache->buf)))
186 return retval;
187 }
188 return 0;
189}
190
191/* Free the cache buffers */
192static void free_cache(io_channel channel,
193 struct unix_private_data *data)
194{
195 struct unix_cache *cache;
196 int i;
197
198 data->access_time = 0;
199 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
200 cache->block = 0;
201 cache->access_time = 0;
202 cache->dirty = 0;
203 cache->in_use = 0;
204 if (cache->buf)
205 ext2fs_free_mem((void **) &cache->buf);
206 cache->buf = 0;
207 }
208}
209
210/*
211 * Try to find a block in the cache. If get_cache is non-zero, then
212 * if the block isn't in the cache, evict the oldest block in the
213 * cache and create a new cache entry for the requested block.
214 */
215struct unix_cache *find_cached_block(io_channel channel,
216 struct unix_private_data *data,
217 unsigned long block,
218 int get_cache)
219{
220 struct unix_cache *cache, *free_cache, *oldest_cache;
221 int i;
222
223 free_cache = oldest_cache = 0;
224 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
225 if (!cache->in_use) {
226 free_cache = cache;
227 continue;
228 }
229 if (cache->block == block) {
230 cache->access_time = ++data->access_time;
231 return cache;
232 }
233 if (!oldest_cache ||
234 (cache->access_time < oldest_cache->access_time))
235 oldest_cache = cache;
236 }
237 if (!get_cache)
238 return 0;
239
240 /*
241 * Try to allocate cache slot.
242 */
243 if (free_cache)
244 cache = free_cache;
245 else {
246 cache = oldest_cache;
247 if (cache->dirty)
248 raw_write_blk(channel, data,
249 cache->block, 1, cache->buf);
250 }
251 cache->in_use = 1;
252 cache->block = block;
253 cache->access_time = ++data->access_time;
254 return cache;
255}
256
257/*
258 * Flush all of the blocks in the cache
259 */
260static errcode_t flush_cached_blocks(io_channel channel,
261 struct unix_private_data *data,
262 int invalidate)
263
264{
265 struct unix_cache *cache;
266 errcode_t retval, retval2;
267 int i;
268
269 retval2 = 0;
270 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
271 if (!cache->in_use)
272 continue;
273
274 if (invalidate)
275 cache->in_use = 0;
276
277 if (!cache->dirty)
278 continue;
279
280 retval = raw_write_blk(channel, data,
281 cache->block, 1, cache->buf);
282 if (retval)
283 retval2 = retval;
284 else
285 cache->dirty = 0;
286 }
287 return retval2;
288}
289
290
291
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292static errcode_t unix_open(const char *name, int flags, io_channel *channel)
293{
294 io_channel io = NULL;
295 struct unix_private_data *data = NULL;
296 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000297 int open_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000299 if (name == 0)
300 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000301 retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
302 (void **) &io);
303 if (retval)
304 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000305 memset(io, 0, sizeof(struct struct_io_channel));
306 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000307 retval = ext2fs_get_mem(sizeof(struct unix_private_data),
308 (void **) &data);
309 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000311
Theodore Ts'o3839e651997-04-26 13:21:57 +0000312 io->manager = unix_io_manager;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000313 retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
314 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000316
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317 strcpy(io->name, name);
318 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000319 io->block_size = 1024;
320 io->read_error = 0;
321 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000322 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323
324 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000325 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000326
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000327 if ((retval = alloc_cache(io, data)))
328 goto cleanup;
329
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000330 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
331#ifdef HAVE_OPEN64
332 data->dev = open64(name, open_flags);
333#else
334 data->dev = open(name, open_flags);
335#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000336 if (data->dev < 0) {
337 retval = errno;
338 goto cleanup;
339 }
340 *channel = io;
341 return 0;
342
343cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344 if (data) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000345 free_cache(io, data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000346 ext2fs_free_mem((void **) &data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000348 if (io)
349 ext2fs_free_mem((void **) &io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000350 return retval;
351}
352
353static errcode_t unix_close(io_channel channel)
354{
355 struct unix_private_data *data;
356 errcode_t retval = 0;
357
Theodore Ts'of3db3561997-04-26 13:34:30 +0000358 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000360 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000361
362 if (--channel->refcount > 0)
363 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000364
365 retval = flush_cached_blocks(channel, data, 0);
366
Theodore Ts'o3839e651997-04-26 13:21:57 +0000367 if (close(data->dev) < 0)
368 retval = errno;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000369 free_cache(channel, data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370 if (channel->private_data)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000371 ext2fs_free_mem((void **) &channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000372 if (channel->name)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000373 ext2fs_free_mem((void **) &channel->name);
374 ext2fs_free_mem((void **) &channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000375 return retval;
376}
377
378static errcode_t unix_set_blksize(io_channel channel, int blksize)
379{
380 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000381 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382
Theodore Ts'of3db3561997-04-26 13:34:30 +0000383 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000385 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
386
Theodore Ts'o3839e651997-04-26 13:21:57 +0000387 if (channel->block_size != blksize) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000388 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000389 return retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000390
391 channel->block_size = blksize;
392 free_cache(channel, data);
393 if ((retval = alloc_cache(channel, data)))
394 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395 }
396 return 0;
397}
398
399
400static errcode_t unix_read_blk(io_channel channel, unsigned long block,
401 int count, void *buf)
402{
403 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000404 struct unix_cache *cache;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000405 errcode_t retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000406 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000407
Theodore Ts'of3db3561997-04-26 13:34:30 +0000408 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000409 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000410 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000411
412 /*
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000413 * If we're doing an odd-sized read, flush out the cache and
414 * then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415 */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000416 if (count < 0) {
417 if ((retval = flush_cached_blocks(channel, data, 0)))
418 return retval;
419 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000421
422 while (count > 0) {
423 /* If it's in the cache, use it! */
424 if ((cache = find_cached_block(channel, data, block, 0))) {
425#ifdef DEBUG
426 printf("Using cached block %d\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000427#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000428 memcpy(buf, cache->buf, channel->block_size);
429 count--;
430 block++;
431 buf += channel->block_size;
432 continue;
433 }
434 /*
435 * Find the number of uncached blocks so we can do a
436 * single read request
437 */
438 for (i=1; i < count; i++)
439 if (find_cached_block(channel, data, block+i, 0))
440 break;
441#ifdef DEBUG
442 printf("Reading %d blocks starting at %d\n", i, block);
443#endif
444 if ((retval = raw_read_blk(channel, data, block, i, buf)))
445 return retval;
446
447 /* Save the results in the cache */
448 for (j=0; j < i; j++) {
449 count--;
450 cache = find_cached_block(channel, data, block++, 1);
451 if (cache)
452 memcpy(cache->buf, buf, channel->block_size);
453 buf += channel->block_size;
454 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455 }
456 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457}
458
459static errcode_t unix_write_blk(io_channel channel, unsigned long block,
460 int count, const void *buf)
461{
462 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000463 struct unix_cache *cache;
464 errcode_t retval = 0, retval2;
465 char *cp;
466 int i, writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467
Theodore Ts'of3db3561997-04-26 13:34:30 +0000468 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000470 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000472 /*
473 * If we're doing an odd-sized write or a very large write,
474 * flush out the cache completely and then do a direct write.
475 */
476 if (count < 0 || count > WRITE_VIA_CACHE_SIZE) {
477 if ((retval = flush_cached_blocks(channel, data, 1)))
478 return retval;
479 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480 }
481
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000482 /*
483 * For a moderate-sized multi-block write, first force a write
484 * if we're in write-through cache mode, and then fill the
485 * cache with the blocks.
486 */
487 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
488 if (writethrough)
489 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000491 while (count > 0) {
492 cache = find_cached_block(channel, data, block, 1);
493 if (!cache) {
494 /*
495 * Oh shit, we couldn't get cache descriptor.
496 * Force the write directly.
497 */
498 if ((retval2 = raw_write_blk(channel, data, block,
499 1, buf)))
500 retval = retval2;
501 } else {
502 memcpy(cache->buf, buf, channel->block_size);
503 cache->dirty = !writethrough;
504 }
505 count--;
506 block++;
507 buf += channel->block_size;
508 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 return retval;
510}
511
512/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000513 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 */
515static errcode_t unix_flush(io_channel channel)
516{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000517 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000518 errcode_t retval = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000519
520 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
521 data = (struct unix_private_data *) channel->private_data;
522 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000523
524 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'o36f21431997-06-14 07:25:40 +0000525 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000526 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527}
528